Author: mattsicker Date: Sun Apr 6 00:16:27 2014 New Revision: 1585217 URL: http://svn.apache.org/r1585217 Log: Create @Plugin annotation processor.
- See LOG4J2-595. Added: logging/log4j/log4j2/trunk/log4j-plugin-processor/ logging/log4j/log4j2/trunk/log4j-plugin-processor/pom.xml (with props) logging/log4j/log4j2/trunk/log4j-plugin-processor/src/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/Plugin.java - copied, changed from r1585180, logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/Plugin.java logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginAliases.java - copied unchanged from r1585155, logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginAliases.java logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginEntry.java (with props) logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java (with props) logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/package-info.java (with props) logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/resources/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/resources/META-INF/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/resources/META-INF/services/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/FakePlugin.java (with props) logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessorTest.java (with props) Modified: logging/log4j/log4j2/trunk/pom.xml Added: logging/log4j/log4j2/trunk/log4j-plugin-processor/pom.xml URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-plugin-processor/pom.xml?rev=1585217&view=auto ============================================================================== --- logging/log4j/log4j2/trunk/log4j-plugin-processor/pom.xml (added) +++ logging/log4j/log4j2/trunk/log4j-plugin-processor/pom.xml Sun Apr 6 00:16:27 2014 @@ -0,0 +1,74 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one or more + ~ contributor license agreements. See the NOTICE file distributed with + ~ this work for additional information regarding copyright ownership. + ~ The ASF licenses this file to You under the Apache License, Version 2.0 + ~ (the "License"); you may not use this file except in compliance with + ~ the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>log4j</artifactId> + <groupId>org.apache.logging.log4j</groupId> + <version>2.0-rc2-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>log4j-plugin-processor</artifactId> + <name>Apache Log4j 2 Plugin Annotation Processor</name> + <description>A Java annotation processor for pre-scanning custom Log4j 2 plugins.</description> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <executions> + <execution> + <id>default-compile</id> + <goals> + <goal>compile</goal> + </goals> + <phase>compile</phase> + <configuration> + <proc>none</proc> + </configuration> + </execution> + <execution> + <id>default-testCompile</id> + <goals> + <goal>testCompile</goal> + </goals> + <phase>test-compile</phase> + <configuration> + <annotationProcessors> + <annotationProcessor> + org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor + </annotationProcessor> + </annotationProcessors> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + +</project> \ No newline at end of file Propchange: logging/log4j/log4j2/trunk/log4j-plugin-processor/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Copied: logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/Plugin.java (from r1585180, logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/Plugin.java) URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/Plugin.java?p2=logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/Plugin.java&p1=logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/Plugin.java&r1=1585180&r2=1585217&rev=1585217&view=diff ============================================================================== --- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/Plugin.java (original) +++ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/Plugin.java Sun Apr 6 00:16:27 2014 @@ -2,7 +2,7 @@ * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache license, Version 2.0 + * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * @@ -11,8 +11,8 @@ * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the license for the specific language governing permissions and - * limitations under the license. + * See the License for the specific language governing permissions and + * limitations under the License. */ package org.apache.logging.log4j.core.config.plugins; @@ -23,7 +23,6 @@ import java.lang.annotation.Target; /** * Annotation that identifies a Class as a Plugin. - * @see PluginType */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) Added: logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginEntry.java URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginEntry.java?rev=1585217&view=auto ============================================================================== --- logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginEntry.java (added) +++ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginEntry.java Sun Apr 6 00:16:27 2014 @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.core.config.plugins.processor; + +import java.io.Serializable; + +/** + * Memento object for storing a plugin entry to a cache file. + */ +public class PluginEntry implements Serializable { + public static final long serialVersionUID = 1L; + + private String key; + private String className; + private String name; + private boolean printable; + private boolean defer; + private transient String category; + + public String getKey() { + return key; + } + + public void setKey(final String key) { + this.key = key; + } + + public String getClassName() { + return className; + } + + public void setClassName(final String className) { + this.className = className; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public boolean isPrintable() { + return printable; + } + + public void setPrintable(final boolean printable) { + this.printable = printable; + } + + public boolean isDefer() { + return defer; + } + + public void setDefer(final boolean defer) { + this.defer = defer; + } + + public String getCategory() { + return category; + } + + public void setCategory(final String category) { + this.category = category; + } +} Propchange: logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginEntry.java ------------------------------------------------------------------------------ svn:eol-style = native Added: logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java?rev=1585217&view=auto ============================================================================== --- logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java (added) +++ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java Sun Apr 6 00:16:27 2014 @@ -0,0 +1,181 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.core.config.plugins.processor; + +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.io.ObjectOutput; +import java.io.ObjectOutputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicInteger; +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; +import javax.annotation.processing.SupportedSourceVersion; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.Element; +import javax.lang.model.element.ElementVisitor; +import javax.lang.model.element.TypeElement; +import javax.lang.model.util.SimpleElementVisitor6; +import javax.tools.FileObject; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.plugins.PluginAliases; + +import static javax.tools.Diagnostic.Kind.ERROR; +import static javax.tools.StandardLocation.CLASS_OUTPUT; + +/** + * Annotation processor for pre-scanning Log4j 2 plugins. + */ +@SupportedAnnotationTypes(PluginProcessor.PLUGINS_PACKAGE_NAME + ".*") +@SupportedSourceVersion(SourceVersion.RELEASE_6) +public class PluginProcessor extends AbstractProcessor { + + /** + * Destination package for saving cache file. + */ + public static final String PLUGINS_PACKAGE_NAME = "org.apache.logging.log4j.core.config.plugins"; + + /** + * Name of cache file. + */ + public static final String FILENAME = "Log4j2Plugins.dat"; + + private static final AtomicInteger invokeCount = new AtomicInteger(); + + private final ConcurrentMap<String, ConcurrentMap<String, PluginEntry>> pluginCategories = + new ConcurrentHashMap<String, ConcurrentMap<String, PluginEntry>>(); + private final ElementVisitor<PluginEntry, Plugin> pluginVisitor = + new PluginElementVisitor(); + private final ElementVisitor<Collection<PluginEntry>, Plugin> pluginAliasesVisitor = + new PluginAliasesElementVisitor(); + + @Override + public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) { + try { + final Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(Plugin.class); + if (elements.isEmpty()) { + return false; + } + collectPlugins(elements); + writeCacheFile(elements.toArray(new Element[elements.size()])); + return true; + } catch (final IOException e) { + error(e.getMessage()); + return false; + } + } + + private void error(final CharSequence message) { + processingEnv.getMessager().printMessage(ERROR, message); + } + + private void collectPlugins(final Iterable<? extends Element> elements) { + for (final Element element : elements) { + final Plugin plugin = element.getAnnotation(Plugin.class); + final PluginEntry entry = element.accept(pluginVisitor, plugin); + pluginCategories.putIfAbsent(entry.getCategory(), new ConcurrentHashMap<String, PluginEntry>()); + final ConcurrentMap<String, PluginEntry> category = pluginCategories.get(entry.getCategory()); + category.put(entry.getKey(), entry); + final Collection<PluginEntry> entries = element.accept(pluginAliasesVisitor, plugin); + for (final PluginEntry pluginEntry : entries) { + category.put(pluginEntry.getKey(), pluginEntry); + } + } + } + + private void writeCacheFile(final Element... elements) throws IOException { + final FileObject fo = processingEnv.getFiler().createResource(CLASS_OUTPUT, PLUGINS_PACKAGE_NAME, FILENAME, elements); + final ObjectOutput out = new ObjectOutputStream(new BufferedOutputStream(fo.openOutputStream())); + try { + out.writeInt(pluginCategories.size()); + for (final Map.Entry<String, ConcurrentMap<String, PluginEntry>> category : pluginCategories.entrySet()) { + out.writeUTF(category.getKey()); + final Map<String, PluginEntry> m = category.getValue(); + out.writeInt(m.size()); + for (final Map.Entry<String, PluginEntry> entry : m.entrySet()) { + final PluginEntry plugin = entry.getValue(); + // TODO: would this work? or do we need to override the writeObject(ObjectOutputStream) method? + //out.writeObject(plugin); + out.writeUTF(plugin.getKey()); + out.writeUTF(plugin.getClassName()); + out.writeUTF(plugin.getName()); + out.writeBoolean(plugin.isPrintable()); + out.writeBoolean(plugin.isDefer()); + } + } + } finally { + out.close(); + } + } + + /** + * ElementVisitor to scan the Plugin annotation. + */ + private static class PluginElementVisitor extends SimpleElementVisitor6<PluginEntry, Plugin> { + @Override + public PluginEntry visitType(final TypeElement e, final Plugin plugin) { + if (plugin == null) { + throw new NullPointerException("Plugin annotation is null."); + } + final PluginEntry entry = new PluginEntry(); + entry.setKey(plugin.name().toLowerCase()); + entry.setClassName(e.getQualifiedName().toString()); + entry.setName(Plugin.EMPTY.equals(plugin.elementType()) ? plugin.name() : plugin.elementType()); + entry.setPrintable(plugin.printObject()); + entry.setDefer(plugin.deferChildren()); + entry.setCategory(plugin.category()); + return entry; + } + } + + /** + * ElementVisitor to scan the PluginAliases annotation. + */ + private static class PluginAliasesElementVisitor extends SimpleElementVisitor6<Collection<PluginEntry>, Plugin> { + protected PluginAliasesElementVisitor() { + super(Collections.<PluginEntry>emptyList()); + } + + @Override + public Collection<PluginEntry> visitType(final TypeElement e, final Plugin plugin) { + final PluginAliases aliases = e.getAnnotation(PluginAliases.class); + if (aliases == null) { + return DEFAULT_VALUE; + } + final Collection<PluginEntry> entries = new ArrayList<PluginEntry>(aliases.value().length); + for (final String alias : aliases.value()) { + final PluginEntry entry = new PluginEntry(); + entry.setKey(alias.toLowerCase()); + entry.setClassName(e.getQualifiedName().toString()); + entry.setName(Plugin.EMPTY.equals(plugin.elementType()) ? alias : plugin.elementType()); + entry.setPrintable(plugin.printObject()); + entry.setDefer(plugin.deferChildren()); + entry.setCategory(plugin.category()); + entries.add(entry); + } + return entries; + } + } +} Propchange: logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java ------------------------------------------------------------------------------ svn:eol-style = native Added: logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/package-info.java URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/package-info.java?rev=1585217&view=auto ============================================================================== --- logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/package-info.java (added) +++ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/package-info.java Sun Apr 6 00:16:27 2014 @@ -0,0 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Java annotation processor for pre-scanning Log4j 2 plugins. This is provided as an alternative to using the + * executable {@link org.apache.logging.log4j.core.config.plugins.PluginManager} class in your build process. + */ +package org.apache.logging.log4j.core.config.plugins.processor; \ No newline at end of file Propchange: logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/package-info.java ------------------------------------------------------------------------------ svn:eol-style = native Added: logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor?rev=1585217&view=auto ============================================================================== --- logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor (added) +++ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor Sun Apr 6 00:16:27 2014 @@ -0,0 +1 @@ +org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor Added: logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/FakePlugin.java URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/FakePlugin.java?rev=1585217&view=auto ============================================================================== --- logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/FakePlugin.java (added) +++ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/FakePlugin.java Sun Apr 6 00:16:27 2014 @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.core.config.plugins.processor; + +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.plugins.PluginAliases; + +/** + * @author msicker + * @version 1.0.0 + */ +@Plugin(name = "Fake", category = "Test") +@PluginAliases({"AnotherFake", "StillFake"}) +public class FakePlugin { +} Propchange: logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/FakePlugin.java ------------------------------------------------------------------------------ svn:eol-style = native Added: logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessorTest.java URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessorTest.java?rev=1585217&view=auto ============================================================================== --- logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessorTest.java (added) +++ logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessorTest.java Sun Apr 6 00:16:27 2014 @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.core.config.plugins.processor; + +import java.io.BufferedInputStream; +import java.io.ObjectInput; +import java.io.ObjectInputStream; +import java.net.URL; +import java.util.Enumeration; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.plugins.PluginAliases; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; + +@RunWith(JUnit4.class) +public class PluginProcessorTest { + + private static final String CACHE_FILE = "org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat"; + + private static ConcurrentMap<String, ConcurrentMap<String, PluginEntry>> pluginCategories; + + private final Plugin p = FakePlugin.class.getAnnotation(Plugin.class); + + @BeforeClass + public static void setUpClass() throws Exception { + // TODO: refactor PluginManager.decode() into this module? + pluginCategories = new ConcurrentHashMap<String, ConcurrentMap<String, PluginEntry>>(); + final Enumeration<URL> resources = PluginProcessor.class.getClassLoader().getResources(CACHE_FILE); + while (resources.hasMoreElements()) { + final URL url = resources.nextElement(); + final ObjectInput in = new ObjectInputStream(new BufferedInputStream(url.openStream())); + try { + final int count = in.readInt(); + for (int i = 0; i < count; i++) { + final String category = in.readUTF(); + pluginCategories.putIfAbsent(category, new ConcurrentHashMap<String, PluginEntry>()); + final ConcurrentMap<String, PluginEntry> m = pluginCategories.get(category); + final int entries = in.readInt(); + for (int j = 0; j < entries; j++) { + final PluginEntry entry = new PluginEntry(); + entry.setKey(in.readUTF()); + entry.setClassName(in.readUTF()); + entry.setName(in.readUTF()); + entry.setPrintable(in.readBoolean()); + entry.setDefer(in.readBoolean()); + entry.setCategory(category); + m.putIfAbsent(entry.getKey(), entry); + } + pluginCategories.putIfAbsent(category, m); + } + } finally { + in.close(); + } + } + } + + @Test + public void testTestCategoryFound() throws Exception { + assertNotNull("No plugin annotation on FakePlugin.", p); + final ConcurrentMap<String, PluginEntry> testCategory = pluginCategories.get(p.category()); + assertNotEquals("No plugins were found.", 0, pluginCategories.size()); + assertNotNull("The category '" + p.category() + "' was not found.", testCategory); + } + + @Test + public void testFakePluginFoundWithCorrectInformation() throws Exception { + final PluginEntry fake = pluginCategories.get(p.category()).get(p.name().toLowerCase()); + verifyFakePluginEntry(p.name(), fake); + } + + @Test + public void testFakePluginAliasesContainSameInformation() throws Exception { + final PluginAliases aliases = FakePlugin.class.getAnnotation(PluginAliases.class); + for (final String alias : aliases.value()) { + final PluginEntry fake = pluginCategories.get(p.category()).get(alias.toLowerCase()); + verifyFakePluginEntry(alias, fake); + } + } + + private void verifyFakePluginEntry(final String name, final PluginEntry fake) { + assertNotNull("The plugin '" + name.toLowerCase() + "' was not found.", fake); + assertEquals(FakePlugin.class.getName(), fake.getClassName()); + assertEquals(name.toLowerCase(), fake.getKey()); + assertEquals("", p.elementType()); + assertEquals(name, fake.getName()); + assertEquals(p.printObject(), fake.isPrintable()); + assertEquals(p.deferChildren(), fake.isDefer()); + } +} Propchange: logging/log4j/log4j2/trunk/log4j-plugin-processor/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessorTest.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: logging/log4j/log4j2/trunk/pom.xml URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/pom.xml?rev=1585217&r1=1585216&r2=1585217&view=diff ============================================================================== --- logging/log4j/log4j2/trunk/pom.xml (original) +++ logging/log4j/log4j2/trunk/pom.xml Sun Apr 6 00:16:27 2014 @@ -608,6 +608,18 @@ <artifactId>maven-pmd-plugin</artifactId> <version>${pmd.plugin.version}</version> </plugin> + <!-- some nice default compiler options --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.1</version> + <configuration> + <source>${maven.compile.source}</source> + <target>${maven.compile.target}</target> + <showDeprecation>true</showDeprecation> + <showWarnings>true</showWarnings> + </configuration> + </plugin> </plugins> </pluginManagement> <plugins> @@ -906,6 +918,7 @@ <module>log4j-jmx-gui</module> <module>log4j-samples</module> <module>log4j-bom</module> + <module>log4j-plugin-processor</module> </modules> <profiles> <profile>