001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.monitoring.reporting.web.plugin;
018    
019    import org.apache.commons.monitoring.configuration.Configuration;
020    import org.apache.commons.monitoring.reporting.web.handler.internal.EndpointInfo;
021    import org.apache.commons.monitoring.reporting.web.handler.internal.Invoker;
022    
023    import java.util.Collection;
024    import java.util.Map;
025    import java.util.ServiceLoader;
026    import java.util.concurrent.CopyOnWriteArrayList;
027    import java.util.regex.Pattern;
028    
029    public final class PluginRepository {
030        public static Collection<PluginInfo> PLUGIN_INFO = new CopyOnWriteArrayList<PluginInfo>();
031    
032        static {
033            for (final Plugin plugin : ServiceLoader.load(Plugin.class, Plugin.class.getClassLoader())) {
034                final String name = plugin.name();
035                if (name == null) {
036                    throw new IllegalArgumentException("plugin name can't be null");
037                }
038                if (!Configuration.is(name + "activated", true)) {
039                    continue;
040                }
041    
042                final String mapping = plugin.mapping();
043                final Class<?> handler = plugin.endpoints();
044                if (mapping != null) {
045                    PLUGIN_INFO.add(new PluginInfo(mapping, name, EndpointInfo.build(handler, plugin.name(), plugin.mapping())));
046                }
047            }
048        }
049    
050        private PluginRepository() {
051            // no-op
052        }
053    
054        public static class PluginInfo {
055            private final String url;
056            private final String name;
057            private final EndpointInfo info;
058    
059            public PluginInfo(final String url, String name, final EndpointInfo info) {
060                this.url = url;
061                this.name = name;
062                this.info = info;
063            }
064    
065            public String getUrl() {
066                return url;
067            }
068    
069            public String getName() {
070                return name;
071            }
072    
073            public Map<Pattern, Invoker> getInvokers() {
074                return info.getInvokers();
075            }
076        }
077    }