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.logging.log4j.core.config.plugins;
018    
019    import org.apache.logging.log4j.Logger;
020    import org.apache.logging.log4j.core.helpers.Loader;
021    import org.apache.logging.log4j.status.StatusLogger;
022    
023    import java.io.BufferedInputStream;
024    import java.io.BufferedOutputStream;
025    import java.io.DataInputStream;
026    import java.io.DataOutputStream;
027    import java.io.File;
028    import java.io.FileOutputStream;
029    import java.io.IOException;
030    import java.io.InputStream;
031    import java.net.URL;
032    import java.text.DecimalFormat;
033    import java.util.Enumeration;
034    import java.util.HashMap;
035    import java.util.Map;
036    import java.util.concurrent.ConcurrentHashMap;
037    import java.util.concurrent.ConcurrentMap;
038    import java.util.concurrent.CopyOnWriteArrayList;
039    
040    /**
041     * Loads and manages all the plugins.
042     */
043    public class PluginManager {
044    
045        private static final long NANOS_PER_SECOND = 1000000000L;
046    
047        private static ConcurrentMap<String, ConcurrentMap<String, PluginType>> pluginTypeMap =
048            new ConcurrentHashMap<String, ConcurrentMap<String, PluginType>>();
049    
050        private static final CopyOnWriteArrayList<String> PACKAGES = new CopyOnWriteArrayList<String>();
051        private static final String PATH = "org/apache/logging/log4j/core/config/plugins/";
052        private static final String FILENAME = "Log4j2Plugins.dat";
053        private static final String LOG4J_PACKAGES = "org.apache.logging.log4j.core";
054    
055        private static final Logger LOGGER = StatusLogger.getLogger();
056    
057        private static String rootDir;
058    
059        private Map<String, PluginType> plugins = new HashMap<String, PluginType>();
060        private final String type;
061        private final Class<?> clazz;
062    
063        /**
064         * Constructor that takes only a type name.
065         * @param type The type name.
066         */
067        public PluginManager(final String type) {
068            this.type = type;
069            this.clazz = null;
070        }
071    
072        /**
073         * Constructor that takes a type name and a Class.
074         * @param type The type that must be matched.
075         * @param clazz The Class each match must be an instance of.
076         */
077        public PluginManager(final String type, final Class<?> clazz) {
078            this.type = type;
079            this.clazz = clazz;
080        }
081    
082        public static void main(final String[] args) throws Exception {
083            if (args == null || args.length < 1) {
084                System.err.println("A target directory must be specified");
085                System.exit(-1);
086            }
087            rootDir = args[0].endsWith("/") || args[0].endsWith("\\") ? args[0] : args[0] + "/";
088    
089            final PluginManager manager = new PluginManager("Core");
090            final String packages = args.length == 2 ? args[1] : null;
091    
092            manager.collectPlugins(false, packages);
093            encode(pluginTypeMap);
094        }
095    
096        /**
097         * Adds a package name to be scanned for plugins. Must be invoked prior to plugins being collected.
098         * @param p The package name.
099         */
100        public static void addPackage(final String p) {
101            if (PACKAGES.addIfAbsent(p))
102            {
103                //set of available plugins could have changed, reset plugin cache for newly-retrieved managers
104                pluginTypeMap.clear();
105            }
106        }
107    
108        /**
109         * Returns the type of a specified plugin.
110         * @param name The name of the plugin.
111         * @return The plugin's type.
112         */
113        public PluginType getPluginType(final String name) {
114            return plugins.get(name.toLowerCase());
115        }
116    
117        /**
118         * Returns all the matching plugins.
119         * @return A Map containing the name of the plugin and its type.
120         */
121        public Map<String, PluginType> getPlugins() {
122            return plugins;
123        }
124    
125        /**
126         * Locates all the plugins.
127         */
128        public void collectPlugins() {
129            collectPlugins(true, null);
130        }
131    
132        /**
133         * Collects plugins, optionally obtaining them from a preload map.
134         * @param preLoad if true, plugins will be obtained from the preload map.
135         * @param pkgs A comma separated list of package names to scan for plugins. If
136         * null the default Log4j package name will be used.
137         */
138        public void collectPlugins(boolean preLoad, final String pkgs) {
139            if (pluginTypeMap.containsKey(type)) {
140                plugins = pluginTypeMap.get(type);
141                preLoad = false;
142            }
143            final long start = System.nanoTime();
144            final ResolverUtil resolver = new ResolverUtil();
145            final ClassLoader loader = Loader.getClassLoader();
146            if (loader != null) {
147                resolver.setClassLoader(loader);
148            }
149            if (preLoad) {
150                final ConcurrentMap<String, ConcurrentMap<String, PluginType>> map = decode(loader);
151                if (map != null) {
152                    pluginTypeMap = map;
153                    plugins = map.get(type);
154                } else {
155                    LOGGER.warn("Plugin preloads not available");
156                }
157            }
158            if (plugins == null || plugins.size() == 0) {
159                if (pkgs == null) {
160                    if (!PACKAGES.contains(LOG4J_PACKAGES)) {
161                        PACKAGES.add(LOG4J_PACKAGES);
162                    }
163                } else {
164                    final String[] names = pkgs.split(",");
165                    for (final String name : names) {
166                        PACKAGES.add(name);
167                    }
168                }
169            }
170            final ResolverUtil.Test test = new PluginTest(clazz);
171            for (final String pkg : PACKAGES) {
172                resolver.findInPackage(test, pkg);
173            }
174            for (final Class<?> clazz : resolver.getClasses()) {
175                final Plugin plugin = clazz.getAnnotation(Plugin.class);
176                final String pluginType = plugin.type();
177                if (!pluginTypeMap.containsKey(pluginType)) {
178                    pluginTypeMap.putIfAbsent(pluginType, new ConcurrentHashMap<String, PluginType>());
179                }
180                final Map<String, PluginType> map = pluginTypeMap.get(pluginType);
181                final String type = plugin.elementType().equals(Plugin.EMPTY) ? plugin.name() : plugin.elementType();
182                map.put(plugin.name().toLowerCase(), new PluginType(clazz, type, plugin.printObject(),
183                    plugin.deferChildren()));
184            }
185            long elapsed = System.nanoTime() - start;
186            plugins = pluginTypeMap.get(type);
187            final StringBuilder sb = new StringBuilder("Generated plugins");
188            sb.append(" in ");
189            DecimalFormat numFormat = new DecimalFormat("#0");
190            final long seconds = elapsed / NANOS_PER_SECOND;
191            elapsed %= NANOS_PER_SECOND;
192            sb.append(numFormat.format(seconds)).append('.');
193            numFormat = new DecimalFormat("000000000");
194            sb.append(numFormat.format(elapsed)).append(" seconds");
195            LOGGER.debug(sb.toString());
196        }
197    
198        private static ConcurrentMap<String, ConcurrentMap<String, PluginType>> decode(final ClassLoader loader) {
199            Enumeration<URL> resources;
200            try {
201                resources = loader.getResources(PATH + FILENAME);
202            } catch (final IOException ioe) {
203                LOGGER.warn("Unable to preload plugins", ioe);
204                return null;
205            }
206            final ConcurrentMap<String, ConcurrentMap<String, PluginType>> map =
207                new ConcurrentHashMap<String, ConcurrentMap<String, PluginType>>();
208            while (resources.hasMoreElements()) {
209                try {
210                    final URL url = resources.nextElement();
211                    LOGGER.debug("Found Plugin Map at {}", url.toExternalForm());
212                    final InputStream is = url.openStream();
213                    final BufferedInputStream bis = new BufferedInputStream(is);
214                    final DataInputStream dis = new DataInputStream(bis);
215                    final int count = dis.readInt();
216                    for (int j = 0; j < count; ++j) {
217                        final String type = dis.readUTF();
218                        final int entries = dis.readInt();
219                        ConcurrentMap<String, PluginType> types = map.get(type);
220                        if (types == null) {
221                            types = new ConcurrentHashMap<String, PluginType>(count);
222                        }
223                        for (int i = 0; i < entries; ++i) {
224                            final String key = dis.readUTF();
225                            final String className = dis.readUTF();
226                            final String name = dis.readUTF();
227                            final boolean printable = dis.readBoolean();
228                            final boolean defer = dis.readBoolean();
229                            final Class<?> clazz = Class.forName(className);
230                            types.put(key, new PluginType(clazz, name, printable, defer));
231                        }
232                        map.putIfAbsent(type, types);
233                    }
234                    dis.close();
235                } catch (final Exception ex) {
236                    LOGGER.warn("Unable to preload plugins", ex);
237                    return null;
238                }
239            }
240            return map.size() == 0 ? null : map;
241        }
242    
243        private static void encode(final ConcurrentMap<String, ConcurrentMap<String, PluginType>> map) {
244            final String fileName = rootDir + PATH + FILENAME;
245            try {
246                final File file = new File(rootDir + PATH);
247                file.mkdirs();
248                final FileOutputStream fos = new FileOutputStream(fileName);
249                final BufferedOutputStream bos = new BufferedOutputStream(fos);
250                final DataOutputStream dos = new DataOutputStream(bos);
251                dos.writeInt(map.size());
252                for (final Map.Entry<String, ConcurrentMap<String, PluginType>> outer : map.entrySet()) {
253                    dos.writeUTF(outer.getKey());
254                    dos.writeInt(outer.getValue().size());
255                    for (final Map.Entry<String, PluginType> entry : outer.getValue().entrySet()) {
256                        dos.writeUTF(entry.getKey());
257                        final PluginType pt = entry.getValue();
258                        dos.writeUTF(pt.getPluginClass().getName());
259                        dos.writeUTF(pt.getElementName());
260                        dos.writeBoolean(pt.isObjectPrintable());
261                        dos.writeBoolean(pt.isDeferChildren());
262                    }
263                }
264                dos.close();
265            } catch (final Exception ex) {
266                ex.printStackTrace();
267            }
268        }
269    
270        /**
271         * A Test that checks to see if each class is annotated with a specific annotation. If it
272         * is, then the test returns true, otherwise false.
273         */
274        public static class PluginTest extends ResolverUtil.ClassTest {
275            private final Class<?> isA;
276    
277            /**
278             * Constructs an AnnotatedWith test for the specified annotation type.
279             * @param isA The class to compare against.
280             */
281            public PluginTest(final Class<?> isA) {
282                this.isA = isA;
283            }
284    
285            /**
286             * Returns true if the type is annotated with the class provided to the constructor.
287             * @param type The type to check for.
288             * @return true if the Class is of the specified type.
289             */
290            public boolean matches(final Class<?> type) {
291                return type != null && type.isAnnotationPresent(Plugin.class) &&
292                    (isA == null || isA.isAssignableFrom(type));
293            }
294    
295            @Override
296            public String toString() {
297                final StringBuilder msg = new StringBuilder("annotated with @" + Plugin.class.getSimpleName());
298                if (isA != null) {
299                    msg.append(" is assignable to " + isA.getSimpleName());
300                }
301                return msg.toString();
302            }
303        }
304    
305    }