View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.monitoring.reporting.web.plugin;
18  
19  import org.apache.commons.monitoring.configuration.Configuration;
20  import org.apache.commons.monitoring.reporting.web.handler.internal.EndpointInfo;
21  import org.apache.commons.monitoring.reporting.web.handler.internal.Invoker;
22  
23  import java.util.Collection;
24  import java.util.Map;
25  import java.util.ServiceLoader;
26  import java.util.concurrent.CopyOnWriteArrayList;
27  import java.util.regex.Pattern;
28  
29  public final class PluginRepository {
30      public static Collection<PluginInfo> PLUGIN_INFO = new CopyOnWriteArrayList<PluginInfo>();
31  
32      static {
33          for (final Plugin plugin : ServiceLoader.load(Plugin.class, Plugin.class.getClassLoader())) {
34              final String name = plugin.name();
35              if (name == null) {
36                  throw new IllegalArgumentException("plugin name can't be null");
37              }
38              if (!Configuration.is(name + "activated", true)) {
39                  continue;
40              }
41  
42              final String mapping = plugin.mapping();
43              final Class<?> handler = plugin.endpoints();
44              if (mapping != null) {
45                  PLUGIN_INFO.add(new PluginInfo(mapping, name, EndpointInfo.build(handler, plugin.name(), plugin.mapping())));
46              }
47          }
48      }
49  
50      private PluginRepository() {
51          // no-op
52      }
53  
54      public static class PluginInfo {
55          private final String url;
56          private final String name;
57          private final EndpointInfo info;
58  
59          public PluginInfo(final String url, String name, final EndpointInfo info) {
60              this.url = url;
61              this.name = name;
62              this.info = info;
63          }
64  
65          public String getUrl() {
66              return url;
67          }
68  
69          public String getName() {
70              return name;
71          }
72  
73          public Map<Pattern, Invoker> getInvokers() {
74              return info.getInvokers();
75          }
76      }
77  }