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
018package org.apache.logging.log4j.core.config.plugins.processor;
019
020import java.io.BufferedInputStream;
021import java.io.BufferedOutputStream;
022import java.io.DataInputStream;
023import java.io.DataOutputStream;
024import java.io.IOException;
025import java.io.OutputStream;
026import java.net.URL;
027import java.util.Enumeration;
028import java.util.Map;
029import java.util.TreeMap;
030
031/**
032 *
033 */
034public class PluginCache {
035    private final Map<String, Map<String, PluginEntry>> categories =
036        new TreeMap<>();
037
038    /**
039     * Returns all categories of plugins in this cache.
040     *
041     * @return all categories of plugins in this cache.
042     * @since 2.1
043     */
044    public Map<String, Map<String, PluginEntry>> getAllCategories() {
045        return categories;
046    }
047
048    /**
049     * Gets or creates a category of plugins.
050     *
051     * @param category name of category to look up.
052     * @return plugin mapping of names to plugin entries.
053     */
054    public Map<String, PluginEntry> getCategory(final String category) {
055        final String key = category.toLowerCase();
056        return categories.computeIfAbsent(key, ignored -> new TreeMap<>());
057    }
058
059    /**
060     * Stores the plugin cache to a given OutputStream.
061     *
062     * @param os destination to save cache to.
063     * @throws IOException if an I/O exception occurs.
064     */
065    // NOTE: if this file format is to be changed, the filename should change and this format should still be readable
066    public void writeCache(final OutputStream os) throws IOException {
067        try (final DataOutputStream out = new DataOutputStream(new BufferedOutputStream(os))) {
068            // See PluginManager.readFromCacheFiles for the corresponding decoder. Format may not be changed
069            // without breaking existing Log4j2Plugins.dat files.
070            out.writeInt(categories.size());
071            for (final Map.Entry<String, Map<String, PluginEntry>> category : categories.entrySet()) {
072                out.writeUTF(category.getKey());
073                final Map<String, PluginEntry> m = category.getValue();
074                out.writeInt(m.size());
075                for (final Map.Entry<String, PluginEntry> entry : m.entrySet()) {
076                    final PluginEntry plugin = entry.getValue();
077                    out.writeUTF(plugin.getKey());
078                    out.writeUTF(plugin.getClassName());
079                    out.writeUTF(plugin.getName());
080                    out.writeBoolean(plugin.isPrintable());
081                    out.writeBoolean(plugin.isDefer());
082                }
083            }
084        }
085    }
086
087    /**
088     * Loads and merges all the Log4j plugin cache files specified. Usually, this is obtained via a ClassLoader.
089     *
090     * @param resources URLs to all the desired plugin cache files to load.
091     * @throws IOException if an I/O exception occurs.
092     */
093    public void loadCacheFiles(final Enumeration<URL> resources) throws IOException {
094        categories.clear();
095        while (resources.hasMoreElements()) {
096            final URL url = resources.nextElement();
097            try (final DataInputStream in = new DataInputStream(new BufferedInputStream(url.openStream()))) {
098                final int count = in.readInt();
099                for (int i = 0; i < count; i++) {
100                    final String category = in.readUTF();
101                    final Map<String, PluginEntry> m = getCategory(category);
102                    final int entries = in.readInt();
103                    for (int j = 0; j < entries; j++) {
104                        // Must always read all parts of the entry, even if not adding, so that the stream progresses
105                        final String key = in.readUTF();
106                        final String className = in.readUTF();
107                        final String name = in.readUTF();
108                        final boolean printable = in.readBoolean();
109                        final boolean defer = in.readBoolean();
110                        m.computeIfAbsent(key, k -> {
111                            final PluginEntry entry = new PluginEntry();
112                            entry.setKey(k);
113                            entry.setClassName(className);
114                            entry.setName(name);
115                            entry.setPrintable(printable);
116                            entry.setDefer(defer);
117                            entry.setCategory(category);
118                            return entry;
119                        });
120                    }
121                }
122            }
123        }
124    }
125
126    /**
127     * Gets the number of plugin categories registered.
128     *
129     * @return number of plugin categories in cache.
130     */
131    public int size() {
132        return categories.size();
133    }
134}