From fb463515c54dc633d840feeaff4c0d0d08ceadb4 Mon Sep 17 00:00:00 2001 From: jvanzyl Date: Wed, 21 Jan 2009 19:12:02 +0000 Subject: [PATCH 01/31] [maven-release-plugin] prepare release plexus-containers-1.0-beta-3.0.5 --- plexus-component-annotations/pom.xml | 2 +- plexus-component-javadoc/pom.xml | 2 +- plexus-component-metadata/pom.xml | 2 +- plexus-container-default/pom.xml | 2 +- pom.xml | 8 ++++---- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/plexus-component-annotations/pom.xml b/plexus-component-annotations/pom.xml index d1023e622..ff00c088e 100644 --- a/plexus-component-annotations/pom.xml +++ b/plexus-component-annotations/pom.xml @@ -6,7 +6,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-3.0.5-SNAPSHOT + 1.0-beta-3.0.5 plexus-component-annotations Plexus :: Component Annotations diff --git a/plexus-component-javadoc/pom.xml b/plexus-component-javadoc/pom.xml index 2feaf9aa9..dc8b52273 100644 --- a/plexus-component-javadoc/pom.xml +++ b/plexus-component-javadoc/pom.xml @@ -5,7 +5,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-3.0.5-SNAPSHOT + 1.0-beta-3.0.5 plexus-component-javadoc Plexus :: Javadoc Taglets diff --git a/plexus-component-metadata/pom.xml b/plexus-component-metadata/pom.xml index 8b38bbd9b..f818e1093 100644 --- a/plexus-component-metadata/pom.xml +++ b/plexus-component-metadata/pom.xml @@ -6,7 +6,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-3.0.5-SNAPSHOT + 1.0-beta-3.0.5 plexus-component-metadata diff --git a/plexus-container-default/pom.xml b/plexus-container-default/pom.xml index d452d516f..5d4a1947d 100644 --- a/plexus-container-default/pom.xml +++ b/plexus-container-default/pom.xml @@ -3,7 +3,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-3.0.5-SNAPSHOT + 1.0-beta-3.0.5 plexus-container-default Plexus :: Default Container diff --git a/pom.xml b/pom.xml index 6d538e683..e0b262ab4 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ 2.0.2 plexus-containers - 1.0-beta-3.0.5-SNAPSHOT + 1.0-beta-3.0.5 1.4 1.4.5 @@ -23,9 +23,9 @@ plexus-container-default - scm:svn:http://svn.codehaus.org/plexus/plexus-containers/trunk - scm:svn:https://svn.codehaus.org/plexus/plexus-containers/trunk - http://fisheye.codehaus.org/browse/plexus/plexus-containers/trunk + scm:svn:http://svn.codehaus.org/plexus/plexus-containers/tags/plexus-containers-1.0-beta-3.0.5 + scm:svn:https://svn.codehaus.org/plexus/plexus-containers/tags/plexus-containers-1.0-beta-3.0.5 + http://fisheye.codehaus.org/browse/plexus/plexus-containers/tags/plexus-containers-1.0-beta-3.0.5 From ade151927040f9802779af1fbbee92a290b81838 Mon Sep 17 00:00:00 2001 From: jvanzyl Date: Wed, 21 Jan 2009 19:12:42 +0000 Subject: [PATCH 02/31] [maven-release-plugin] prepare for next development iteration --- plexus-component-annotations/pom.xml | 2 +- plexus-component-javadoc/pom.xml | 2 +- plexus-component-metadata/pom.xml | 2 +- plexus-container-default/pom.xml | 2 +- pom.xml | 8 ++++---- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/plexus-component-annotations/pom.xml b/plexus-component-annotations/pom.xml index ff00c088e..2f00d71c7 100644 --- a/plexus-component-annotations/pom.xml +++ b/plexus-component-annotations/pom.xml @@ -6,7 +6,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-3.0.5 + 1.0-beta-3.0.6-SNAPSHOT plexus-component-annotations Plexus :: Component Annotations diff --git a/plexus-component-javadoc/pom.xml b/plexus-component-javadoc/pom.xml index dc8b52273..24553c2e1 100644 --- a/plexus-component-javadoc/pom.xml +++ b/plexus-component-javadoc/pom.xml @@ -5,7 +5,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-3.0.5 + 1.0-beta-3.0.6-SNAPSHOT plexus-component-javadoc Plexus :: Javadoc Taglets diff --git a/plexus-component-metadata/pom.xml b/plexus-component-metadata/pom.xml index f818e1093..ea636e361 100644 --- a/plexus-component-metadata/pom.xml +++ b/plexus-component-metadata/pom.xml @@ -6,7 +6,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-3.0.5 + 1.0-beta-3.0.6-SNAPSHOT plexus-component-metadata diff --git a/plexus-container-default/pom.xml b/plexus-container-default/pom.xml index 5d4a1947d..c15ccdb13 100644 --- a/plexus-container-default/pom.xml +++ b/plexus-container-default/pom.xml @@ -3,7 +3,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-3.0.5 + 1.0-beta-3.0.6-SNAPSHOT plexus-container-default Plexus :: Default Container diff --git a/pom.xml b/pom.xml index e0b262ab4..b870b1ddc 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ 2.0.2 plexus-containers - 1.0-beta-3.0.5 + 1.0-beta-3.0.6-SNAPSHOT 1.4 1.4.5 @@ -23,9 +23,9 @@ plexus-container-default - scm:svn:http://svn.codehaus.org/plexus/plexus-containers/tags/plexus-containers-1.0-beta-3.0.5 - scm:svn:https://svn.codehaus.org/plexus/plexus-containers/tags/plexus-containers-1.0-beta-3.0.5 - http://fisheye.codehaus.org/browse/plexus/plexus-containers/tags/plexus-containers-1.0-beta-3.0.5 + scm:svn:http://svn.codehaus.org/plexus/plexus-containers/trunk + scm:svn:https://svn.codehaus.org/plexus/plexus-containers/trunk + http://fisheye.codehaus.org/browse/plexus/plexus-containers/trunk From 33949cf2af8da0827ee91ee238b2256fc10a8ed4 Mon Sep 17 00:00:00 2001 From: jvanzyl Date: Thu, 22 Jan 2009 00:07:10 +0000 Subject: [PATCH 03/31] o remove debug output --- .../src/main/java/org/codehaus/plexus/PlexusTestCase.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/PlexusTestCase.java b/plexus-container-default/src/main/java/org/codehaus/plexus/PlexusTestCase.java index cff7f7493..f1c346480 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/PlexusTestCase.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/PlexusTestCase.java @@ -59,8 +59,6 @@ protected void setupContainer() customizeContext( context ); boolean hasPlexusHome = context.contains( "plexus.home" ); - - System.out.println( context ); if ( !hasPlexusHome ) { @@ -74,9 +72,6 @@ protected void setupContainer() context.put( "plexus.home", f.getAbsolutePath() ); } - System.out.println( context ); - - // ---------------------------------------------------------------------------- // Configuration // ---------------------------------------------------------------------------- From 7bd91db4b02aabd95841b6d53762698fc0339115 Mon Sep 17 00:00:00 2001 From: jvanzyl Date: Tue, 10 Feb 2009 15:44:45 +0000 Subject: [PATCH 04/31] o test project for annotation processing --- .../src/it/annotation-processing-test/pom.xml | 51 +++++++++++++++++++ .../plexus/AbstractPlexusComponent.java | 10 ++++ .../java/org/codehaus/plexus/Executor.java | 6 +++ .../org/codehaus/plexus/FastExecutor.java | 13 +++++ .../org/codehaus/plexus/PlexusComponent.java | 6 +++ .../org/codehaus/plexus/SlowExecutor.java | 13 +++++ .../plexus/SubclassPlexusComponent.java | 13 +++++ .../java/org/codehaus/plexus/AppTest.java | 38 ++++++++++++++ 8 files changed, 150 insertions(+) create mode 100644 plexus-component-metadata/src/it/annotation-processing-test/pom.xml create mode 100644 plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/AbstractPlexusComponent.java create mode 100644 plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/Executor.java create mode 100644 plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/FastExecutor.java create mode 100644 plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/PlexusComponent.java create mode 100644 plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/SlowExecutor.java create mode 100644 plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/SubclassPlexusComponent.java create mode 100644 plexus-component-metadata/src/it/annotation-processing-test/src/test/java/org/codehaus/plexus/AppTest.java diff --git a/plexus-component-metadata/src/it/annotation-processing-test/pom.xml b/plexus-component-metadata/src/it/annotation-processing-test/pom.xml new file mode 100644 index 000000000..80ca7245e --- /dev/null +++ b/plexus-component-metadata/src/it/annotation-processing-test/pom.xml @@ -0,0 +1,51 @@ + + 4.0.0 + org.codehaus.plexus + annotation-processing-test + jar + 1.0-SNAPSHOT + annotation-processing-test + http://maven.apache.org + + 1.0-beta-3.0.6-SNAPSHOT + + + + junit + junit + 3.8.1 + test + + + org.codehaus.plexus + plexus-component-annotations + ${plexusVersion} + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + 1.5 + 1.5 + + + + org.codehaus.plexus + plexus-component-metadata + ${plexusVersion} + + + + generate-metadata + + + + + + + diff --git a/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/AbstractPlexusComponent.java b/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/AbstractPlexusComponent.java new file mode 100644 index 000000000..de63abbea --- /dev/null +++ b/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/AbstractPlexusComponent.java @@ -0,0 +1,10 @@ +package org.codehaus.plexus; + +import org.codehaus.plexus.component.annotations.Requirement; + +public abstract class AbstractPlexusComponent + implements PlexusComponent +{ + @Requirement + protected Executor executor; +} diff --git a/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/Executor.java b/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/Executor.java new file mode 100644 index 000000000..0d065e2d4 --- /dev/null +++ b/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/Executor.java @@ -0,0 +1,6 @@ +package org.codehaus.plexus; + +public interface Executor +{ + String execute(); +} diff --git a/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/FastExecutor.java b/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/FastExecutor.java new file mode 100644 index 000000000..964e82336 --- /dev/null +++ b/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/FastExecutor.java @@ -0,0 +1,13 @@ +package org.codehaus.plexus; + +import org.codehaus.plexus.component.annotations.Component; + +@Component(role = Executor.class, hint = "fast") +public class FastExecutor + implements Executor +{ + public String execute() + { + return "fast"; + } +} diff --git a/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/PlexusComponent.java b/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/PlexusComponent.java new file mode 100644 index 000000000..36fe51783 --- /dev/null +++ b/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/PlexusComponent.java @@ -0,0 +1,6 @@ +package org.codehaus.plexus; + +public interface PlexusComponent +{ + String execute(); +} diff --git a/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/SlowExecutor.java b/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/SlowExecutor.java new file mode 100644 index 000000000..cb70641d3 --- /dev/null +++ b/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/SlowExecutor.java @@ -0,0 +1,13 @@ +package org.codehaus.plexus; + +import org.codehaus.plexus.component.annotations.Component; + +@Component(role=Executor.class,hint="slow") +public class SlowExecutor + implements Executor +{ + public String execute() + { + return "slow"; + } +} diff --git a/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/SubclassPlexusComponent.java b/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/SubclassPlexusComponent.java new file mode 100644 index 000000000..ae00b0418 --- /dev/null +++ b/plexus-component-metadata/src/it/annotation-processing-test/src/main/java/org/codehaus/plexus/SubclassPlexusComponent.java @@ -0,0 +1,13 @@ +package org.codehaus.plexus; + +import org.codehaus.plexus.component.annotations.Component; + +@Component(role = Component.class) +public class SubclassPlexusComponent + extends AbstractPlexusComponent +{ + public String execute() + { + return executor.execute(); + } +} diff --git a/plexus-component-metadata/src/it/annotation-processing-test/src/test/java/org/codehaus/plexus/AppTest.java b/plexus-component-metadata/src/it/annotation-processing-test/src/test/java/org/codehaus/plexus/AppTest.java new file mode 100644 index 000000000..55b082dd5 --- /dev/null +++ b/plexus-component-metadata/src/it/annotation-processing-test/src/test/java/org/codehaus/plexus/AppTest.java @@ -0,0 +1,38 @@ +package org.codehaus.plexus; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} From 1edfd72d70ab89663bd90d6245aa04c4d9b3deb0 Mon Sep 17 00:00:00 2001 From: igorife Date: Tue, 10 Feb 2009 22:22:46 +0000 Subject: [PATCH 05/31] PLX-412 look for referenced classes in project classpath only --- .../plexus/metadata/ClassComponentDescriptorExtractor.java | 2 +- .../src/main/java/org/codehaus/plexus/metadata/ann/Ann.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/ClassComponentDescriptorExtractor.java b/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/ClassComponentDescriptorExtractor.java index 46b3cdc28..a42ef99ab 100644 --- a/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/ClassComponentDescriptorExtractor.java +++ b/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/ClassComponentDescriptorExtractor.java @@ -113,7 +113,7 @@ private ClassLoader createClassLoader( final List elements ) //getLogger().debug( " " + urls[i] ); } - return new URLClassLoader( urls, getClass().getClassLoader() ); + return new URLClassLoader( urls, null ); } private List> extract( final File classesDir, final ClassLoader cl, diff --git a/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/ann/Ann.java b/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/ann/Ann.java index 89188f321..6f6eb122b 100644 --- a/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/ann/Ann.java +++ b/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/ann/Ann.java @@ -52,7 +52,7 @@ public Map getParams() { @SuppressWarnings("unchecked") public T getAnnotation(Class c, ClassLoader cl) { - return (T) Proxy.newProxyInstance(cl, new Class[] { c }, // + return (T) Proxy.newProxyInstance(Ann.class.getClassLoader(), new Class[] { c }, // new AnnInvocationHandler(this, cl, c)); } From 06a556dea7b555658bf2feb01a8e9276370967b8 Mon Sep 17 00:00:00 2001 From: igorife Date: Tue, 10 Feb 2009 22:24:30 +0000 Subject: [PATCH 06/31] PLX-411 java5 annotations processor ignores requirement hints --- .../plexus/metadata/ann/AnnReader.java | 41 ++++++++++++++++++- .../AnnotationComponentGleanerTest.java | 11 +++-- .../gleaner/ann/AnnotatedComponent.java | 5 +++ 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/ann/AnnReader.java b/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/ann/AnnReader.java index 3392ce053..9ff59acf1 100644 --- a/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/ann/AnnReader.java +++ b/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/ann/AnnReader.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; import org.objectweb.asm.AnnotationVisitor; import org.objectweb.asm.Attribute; @@ -207,13 +208,49 @@ public AnnotationVisitor visitAnnotation(String name, String desc) { } public AnnotationVisitor visitArray(String name) { - // TODO Auto-generated method stub - return null; + return new AnnAnnArrayReader(ann, name); } public void visitEnd() { } } + + static class AnnAnnArrayReader implements AnnotationVisitor { + + private Ann ann; + + private String name; + + // TODO good enough for now, but does not cover general case + private ArrayList array = new ArrayList(); + + public AnnAnnArrayReader(Ann ann, String name) { + this.ann = ann; + this.name = name; + } + + public void visit(String name, Object value) { + if(value instanceof String) { + array.add((String) value); + } + } + + public AnnotationVisitor visitAnnotation(String name, String value) { + return null; + } + + public AnnotationVisitor visitArray(String arg0) { + return null; + } + + public void visitEnd() { + ann.addParam(name, array.toArray(new String[array.size()])); + } + + public void visitEnum(String arg0, String arg1, String arg2) { + } + + } } diff --git a/plexus-component-metadata/src/test/java/org/codehaus/plexus/metadata/gleaner/AnnotationComponentGleanerTest.java b/plexus-component-metadata/src/test/java/org/codehaus/plexus/metadata/gleaner/AnnotationComponentGleanerTest.java index f2ca30b46..291f9e1e7 100644 --- a/plexus-component-metadata/src/test/java/org/codehaus/plexus/metadata/gleaner/AnnotationComponentGleanerTest.java +++ b/plexus-component-metadata/src/test/java/org/codehaus/plexus/metadata/gleaner/AnnotationComponentGleanerTest.java @@ -39,14 +39,17 @@ public void testGlean() throws Exception { assertEquals("foo", descriptor.getComponentType()); assertEquals(AnnotatedComponentRole.class.getName(), descriptor.getRole()); - @SuppressWarnings("unchecked") List requirements = descriptor.getRequirements(); - assertEquals(requirements.toString(), 1, requirements.size()); - + assertEquals(requirements.toString(), 2, requirements.size()); + ComponentRequirement requirement = requirements.get(0); assertEquals("dependency", requirement.getFieldName()); assertEquals("default", requirement.getRoleHint()); - + + ComponentRequirement requirement2 = requirements.get(1); + assertEquals("dependency2", requirement2.getFieldName()); + assertEquals("release,latest,snapshot", requirement2.getRoleHint()); + PlexusConfiguration configuration = descriptor.getConfiguration(); assertEquals(1, configuration.getChildCount()); PlexusConfiguration child = configuration.getChild(0); diff --git a/plexus-component-metadata/src/test/java/org/codehaus/plexus/metadata/gleaner/ann/AnnotatedComponent.java b/plexus-component-metadata/src/test/java/org/codehaus/plexus/metadata/gleaner/ann/AnnotatedComponent.java index 91d0dc1f9..982e458f8 100644 --- a/plexus-component-metadata/src/test/java/org/codehaus/plexus/metadata/gleaner/ann/AnnotatedComponent.java +++ b/plexus-component-metadata/src/test/java/org/codehaus/plexus/metadata/gleaner/ann/AnnotatedComponent.java @@ -16,6 +16,8 @@ package org.codehaus.plexus.metadata.gleaner.ann; +import java.util.List; + import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Configuration; import org.codehaus.plexus.component.annotations.Requirement; @@ -30,4 +32,7 @@ public class AnnotatedComponent implements AnnotatedComponentRole { @Configuration(name="param", value="value") AnnotatedComponentDependency dependency; + @Requirement(role=AnnotatedComponentDependency.class, hints={"release", "latest", "snapshot"}) + List dependency2; + } From 3b2aafa487c16b37fde54e20501a3b25debd2ef7 Mon Sep 17 00:00:00 2001 From: jvanzyl Date: Tue, 10 Feb 2009 23:50:47 +0000 Subject: [PATCH 07/31] [maven-release-plugin] prepare release plexus-containers-1.0-beta-3.0.6 --- plexus-component-annotations/pom.xml | 2 +- plexus-component-javadoc/pom.xml | 2 +- plexus-component-metadata/pom.xml | 2 +- plexus-container-default/pom.xml | 2 +- pom.xml | 8 ++++---- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/plexus-component-annotations/pom.xml b/plexus-component-annotations/pom.xml index 2f00d71c7..1cb960c2c 100644 --- a/plexus-component-annotations/pom.xml +++ b/plexus-component-annotations/pom.xml @@ -6,7 +6,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-3.0.6-SNAPSHOT + 1.0-beta-3.0.6 plexus-component-annotations Plexus :: Component Annotations diff --git a/plexus-component-javadoc/pom.xml b/plexus-component-javadoc/pom.xml index 24553c2e1..8de573347 100644 --- a/plexus-component-javadoc/pom.xml +++ b/plexus-component-javadoc/pom.xml @@ -5,7 +5,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-3.0.6-SNAPSHOT + 1.0-beta-3.0.6 plexus-component-javadoc Plexus :: Javadoc Taglets diff --git a/plexus-component-metadata/pom.xml b/plexus-component-metadata/pom.xml index ea636e361..53ec8ecb5 100644 --- a/plexus-component-metadata/pom.xml +++ b/plexus-component-metadata/pom.xml @@ -6,7 +6,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-3.0.6-SNAPSHOT + 1.0-beta-3.0.6 plexus-component-metadata diff --git a/plexus-container-default/pom.xml b/plexus-container-default/pom.xml index c15ccdb13..0579ac659 100644 --- a/plexus-container-default/pom.xml +++ b/plexus-container-default/pom.xml @@ -3,7 +3,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-3.0.6-SNAPSHOT + 1.0-beta-3.0.6 plexus-container-default Plexus :: Default Container diff --git a/pom.xml b/pom.xml index b870b1ddc..c935c8ba1 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ 2.0.2 plexus-containers - 1.0-beta-3.0.6-SNAPSHOT + 1.0-beta-3.0.6 1.4 1.4.5 @@ -23,9 +23,9 @@ plexus-container-default - scm:svn:http://svn.codehaus.org/plexus/plexus-containers/trunk - scm:svn:https://svn.codehaus.org/plexus/plexus-containers/trunk - http://fisheye.codehaus.org/browse/plexus/plexus-containers/trunk + scm:svn:http://svn.codehaus.org/plexus/plexus-containers/tags/plexus-containers-1.0-beta-3.0.6 + scm:svn:https://svn.codehaus.org/plexus/plexus-containers/tags/plexus-containers-1.0-beta-3.0.6 + http://fisheye.codehaus.org/browse/plexus/plexus-containers/tags/plexus-containers-1.0-beta-3.0.6 From afa91d85e82224ec250788a24cd08668ee52729f Mon Sep 17 00:00:00 2001 From: jvanzyl Date: Tue, 10 Feb 2009 23:50:56 +0000 Subject: [PATCH 08/31] [maven-release-plugin] prepare for next development iteration --- plexus-component-annotations/pom.xml | 2 +- plexus-component-javadoc/pom.xml | 2 +- plexus-component-metadata/pom.xml | 2 +- plexus-container-default/pom.xml | 2 +- pom.xml | 8 ++++---- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/plexus-component-annotations/pom.xml b/plexus-component-annotations/pom.xml index 1cb960c2c..4e5c38efb 100644 --- a/plexus-component-annotations/pom.xml +++ b/plexus-component-annotations/pom.xml @@ -6,7 +6,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-3.0.6 + 1.0-beta-4-SNAPSHOT plexus-component-annotations Plexus :: Component Annotations diff --git a/plexus-component-javadoc/pom.xml b/plexus-component-javadoc/pom.xml index 8de573347..5458333e9 100644 --- a/plexus-component-javadoc/pom.xml +++ b/plexus-component-javadoc/pom.xml @@ -5,7 +5,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-3.0.6 + 1.0-beta-4-SNAPSHOT plexus-component-javadoc Plexus :: Javadoc Taglets diff --git a/plexus-component-metadata/pom.xml b/plexus-component-metadata/pom.xml index 53ec8ecb5..02e24221f 100644 --- a/plexus-component-metadata/pom.xml +++ b/plexus-component-metadata/pom.xml @@ -6,7 +6,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-3.0.6 + 1.0-beta-4-SNAPSHOT plexus-component-metadata diff --git a/plexus-container-default/pom.xml b/plexus-container-default/pom.xml index 0579ac659..2919839e0 100644 --- a/plexus-container-default/pom.xml +++ b/plexus-container-default/pom.xml @@ -3,7 +3,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-3.0.6 + 1.0-beta-4-SNAPSHOT plexus-container-default Plexus :: Default Container diff --git a/pom.xml b/pom.xml index c935c8ba1..1ef78fbda 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ 2.0.2 plexus-containers - 1.0-beta-3.0.6 + 1.0-beta-4-SNAPSHOT 1.4 1.4.5 @@ -23,9 +23,9 @@ plexus-container-default - scm:svn:http://svn.codehaus.org/plexus/plexus-containers/tags/plexus-containers-1.0-beta-3.0.6 - scm:svn:https://svn.codehaus.org/plexus/plexus-containers/tags/plexus-containers-1.0-beta-3.0.6 - http://fisheye.codehaus.org/browse/plexus/plexus-containers/tags/plexus-containers-1.0-beta-3.0.6 + scm:svn:http://svn.codehaus.org/plexus/plexus-containers/trunk + scm:svn:https://svn.codehaus.org/plexus/plexus-containers/trunk + http://fisheye.codehaus.org/browse/plexus/plexus-containers/trunk From fd5f5085f3bb5913a74ccb7abe8b05df9493bc6b Mon Sep 17 00:00:00 2001 From: dain Date: Wed, 11 Feb 2009 18:20:25 +0000 Subject: [PATCH 09/31] Merged changes from class-index branch --- .../resources/META-INF/plexus/components.xml | 4 - .../DefaultComponentDescriptorWriterTest.java | 9 +- .../codehaus/plexus/ComponentRegistry.java | 25 +- .../plexus/ContainerConfiguration.java | 11 - .../plexus/DefaultComponentRegistry.java | 678 ++++++++++-------- .../plexus/DefaultContainerConfiguration.java | 21 - .../plexus/DefaultPlexusContainer.java | 191 +++-- .../org/codehaus/plexus/PlexusContainer.java | 44 +- .../codehaus/plexus/component/CastUtils.java | 10 + .../plexus/component/ComponentIndex.java | 411 +++++++++++ .../plexus/component/ComponentStack.java | 76 ++ .../component/ComponentStackElement.java | 68 ++ .../builder/XBeanComponentBuilder.java | 433 +++++++---- .../AbstractComponentCollection.java | 164 ----- .../component/collections/ComponentList.java | 253 ------- .../component/collections/ComponentMap.java | 224 ------ .../component/collections/LiveList.java | 350 +++++++++ .../plexus/component/collections/LiveMap.java | 220 ++++++ .../AbstractComponentDiscoverer.java | 13 +- .../discovery/DefaultComponentDiscoverer.java | 7 +- .../PlexusXmlComponentDiscoverer.java | 5 +- .../manager/AbstractComponentManager.java | 156 +--- .../component/manager/ComponentManager.java | 20 +- .../manager/ComponentManagerFactory.java | 6 +- .../manager/PerLookupComponentManager.java | 100 ++- .../PerLookupComponentManagerFactory.java | 10 +- .../manager/SingletonComponentManager.java | 141 +++- .../SingletonComponentManagerFactory.java | 10 +- .../manager/StaticComponentManager.java | 82 +++ .../repository/ComponentDescriptor.java | 54 +- .../ComponentDescriptorListener.java | 14 + .../repository/ComponentRepository.java | 40 -- .../repository/ComponentRequirement.java | 36 +- .../DefaultComponentRepository.java | 206 ------ .../exception/ComponentLookupException.java | 78 +- .../ComponentRepositoryException.java | 105 ++- .../component/repository/io/PlexusTools.java | 38 +- .../configuration/PlexusConfiguration.java | 4 +- .../PlexusConfigurationMerger.java | 30 +- .../InitializeComponentRegistryPhase.java | 35 +- .../plexus/lifecycle/phase/AbstractPhase.java | 15 +- .../plexus/lifecycle/phase/Phase.java | 2 +- .../codehaus/plexus/logging/NullLogger.java | 34 + .../lifecycle/phase/AutoConfigurePhase.java | 4 +- .../lifecycle/phase/LogDisablePhase.java | 5 +- .../lifecycle/phase/LogEnablePhase.java | 3 +- .../lifecycle/phase/StartingException.java | 5 + .../MapOrientedComponentProcessingTest.java | 6 +- .../plexus/component/PlexusExceptionTest.java | 354 +++++++++ .../component/collection/LiveListTest.java | 471 ++++++++++++ .../component/collection/LiveMapTest.java | 345 +++++++++ .../AbstractCompositionResolverTest.java | 31 +- .../ComponentRealmCompositionTest.java | 106 ++- .../discovery/ComponentDiscovererTest.java | 28 +- .../DiscoveredComponentFactoryTest.java | 15 +- .../DefaultComponentRegistryTest.java | 8 +- .../repository/ComponentDescriptorTest.java | 8 +- .../repository/ComponentSetTest.java | 8 +- ...DefaultLoadOnStartServiceWithRoleHint.java | 2 +- .../plexus/test/PlexusContainerTest.java | 40 +- .../plexus/test/PlexusContainerTest.xml | 16 +- pom.xml | 2 +- 62 files changed, 3998 insertions(+), 1892 deletions(-) create mode 100644 plexus-container-default/src/main/java/org/codehaus/plexus/component/ComponentIndex.java create mode 100644 plexus-container-default/src/main/java/org/codehaus/plexus/component/ComponentStack.java create mode 100644 plexus-container-default/src/main/java/org/codehaus/plexus/component/ComponentStackElement.java delete mode 100644 plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/AbstractComponentCollection.java delete mode 100644 plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/ComponentList.java delete mode 100644 plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/ComponentMap.java create mode 100644 plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/LiveList.java create mode 100644 plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/LiveMap.java create mode 100644 plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/StaticComponentManager.java create mode 100644 plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/ComponentDescriptorListener.java delete mode 100644 plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/ComponentRepository.java delete mode 100644 plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/DefaultComponentRepository.java create mode 100644 plexus-container-default/src/main/java/org/codehaus/plexus/logging/NullLogger.java create mode 100644 plexus-container-default/src/test/java/org/codehaus/plexus/component/PlexusExceptionTest.java create mode 100644 plexus-container-default/src/test/java/org/codehaus/plexus/component/collection/LiveListTest.java create mode 100644 plexus-container-default/src/test/java/org/codehaus/plexus/component/collection/LiveMapTest.java diff --git a/plexus-component-metadata/src/main/resources/META-INF/plexus/components.xml b/plexus-component-metadata/src/main/resources/META-INF/plexus/components.xml index 1b9789f48..e47b2f97b 100644 --- a/plexus-component-metadata/src/main/resources/META-INF/plexus/components.xml +++ b/plexus-component-metadata/src/main/resources/META-INF/plexus/components.xml @@ -8,10 +8,6 @@ qdox org.codehaus.plexus.metadata.gleaner.QDoxComponentGleaner - - org.codehaus.plexus.metadata.ComponentDescriptorCreator - org.codehaus.plexus.metadata.DefaultComponentDescriptorCreator - org.codehaus.plexus.metadata.ComponentDescriptorWriter org.codehaus.plexus.metadata.DefaultComponentDescriptorWriter diff --git a/plexus-component-metadata/src/test/java/org/codehaus/plexus/metadata/DefaultComponentDescriptorWriterTest.java b/plexus-component-metadata/src/test/java/org/codehaus/plexus/metadata/DefaultComponentDescriptorWriterTest.java index b6fe357ab..a3800aa68 100644 --- a/plexus-component-metadata/src/test/java/org/codehaus/plexus/metadata/DefaultComponentDescriptorWriterTest.java +++ b/plexus-component-metadata/src/test/java/org/codehaus/plexus/metadata/DefaultComponentDescriptorWriterTest.java @@ -20,8 +20,10 @@ import java.util.List; import org.codehaus.plexus.PlexusTestCase; -import org.codehaus.plexus.component.repository.ComponentDescriptor; +import org.codehaus.plexus.classworlds.ClassWorld; +import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.codehaus.plexus.component.repository.ComponentSetDescriptor; +import org.codehaus.plexus.component.repository.ComponentDescriptor; import org.codehaus.plexus.component.repository.io.PlexusTools; import org.codehaus.plexus.configuration.PlexusConfiguration; @@ -54,6 +56,7 @@ public void testBasic() throws Exception { ComponentSetDescriptor set = new ComponentSetDescriptor(); ComponentDescriptor component = new ComponentDescriptor(); + component.setImplementation("java.lang.String"); component.setRole("foo"); component.setRoleHint("bar"); component.setComponentFactory("baz"); @@ -72,7 +75,9 @@ public void testBasic() throws Exception { PlexusConfiguration config = PlexusTools.buildConfiguration(xml); assertNotNull(config); - org.codehaus.plexus.component.repository.ComponentSetDescriptor set2 = PlexusTools.buildComponentSet(config); + ClassWorld classWorld = new ClassWorld( "test", Thread.currentThread().getContextClassLoader() ); + ClassRealm realm = classWorld.getRealm( "test" ); + org.codehaus.plexus.component.repository.ComponentSetDescriptor set2 = PlexusTools.buildComponentSet(config, realm); assertNotNull(set2); List components = set2.getComponents(); diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/ComponentRegistry.java b/plexus-container-default/src/main/java/org/codehaus/plexus/ComponentRegistry.java index c8a3fd23a..f9592b93a 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/ComponentRegistry.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/ComponentRegistry.java @@ -24,6 +24,7 @@ import org.codehaus.plexus.component.repository.exception.ComponentRepositoryException; import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; import org.codehaus.plexus.component.repository.ComponentDescriptor; +import org.codehaus.plexus.component.repository.ComponentDescriptorListener; import org.codehaus.plexus.component.manager.ComponentManagerFactory; /** @@ -35,27 +36,33 @@ public interface ComponentRegistry { void registerComponentManagerFactory( ComponentManagerFactory componentManagerFactory ); - void addComponentDescriptor( ComponentDescriptor componentDescriptor ) + void addComponentDescriptor( ComponentDescriptor componentDescriptor ) throws ComponentRepositoryException; - ComponentDescriptor getComponentDescriptor( Class type, String role, String roleHint ); + ComponentDescriptor getComponentDescriptor( Class type, String roleHint ); - @Deprecated - ComponentDescriptor getComponentDescriptor( String role, String roleHint, ClassRealm realm ); + List> getComponentDescriptorList( Class type ); - List> getComponentDescriptorList( Class type, String role ); + Map> getComponentDescriptorMap( Class type ); - Map> getComponentDescriptorMap( Class type, String role ); + void addComponent( T instance, Class type, String roleHint, ClassRealm realm ) throws ComponentRepositoryException; - T lookup( Class type, String role, String roleHint ) + T lookup( Class type, String roleHint ) throws ComponentLookupException; - List lookupList( Class type, String role, List hints ) + T lookup( ComponentDescriptor componentDescriptor ) throws ComponentLookupException; - Map lookupMap( Class type, String role, List hints ) + List lookupList( Class type, List hints ) throws ComponentLookupException; + Map lookupMap( Class type, List hints ) + throws ComponentLookupException; + + void addComponentDescriptorListener( ComponentDescriptorListener listener ); + + void removeComponentDescriptorListener( ComponentDescriptorListener listener ); + void release( Object component ) throws ComponentLifecycleException; void removeComponentRealm( ClassRealm classRealm ) throws PlexusContainerException; diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/ContainerConfiguration.java b/plexus-container-default/src/main/java/org/codehaus/plexus/ContainerConfiguration.java index d3b2e0ba3..47b48f313 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/ContainerConfiguration.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/ContainerConfiguration.java @@ -9,7 +9,6 @@ import org.codehaus.plexus.component.discovery.ComponentDiscovererManager; import org.codehaus.plexus.component.discovery.ComponentDiscoveryListener; import org.codehaus.plexus.component.factory.ComponentFactoryManager; -import org.codehaus.plexus.component.repository.ComponentRepository; import org.codehaus.plexus.configuration.source.ConfigurationSource; import org.codehaus.plexus.container.initialization.ContainerInitializationPhase; import org.codehaus.plexus.lifecycle.LifecycleHandler; @@ -75,16 +74,6 @@ public interface ContainerConfiguration ComponentFactoryManager getComponentFactoryManager(); - // Component manager manager - - // Component repository - - ContainerConfiguration setComponentRepository( ComponentRepository componentRepository ); - - ComponentRepository getComponentRepository(); - - // Component composer - // Lifecycle handler manager ContainerConfiguration addLifecycleHandler( LifecycleHandler lifecycleHandler ); diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultComponentRegistry.java b/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultComponentRegistry.java index f9b10d71d..022cd833e 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultComponentRegistry.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultComponentRegistry.java @@ -1,27 +1,22 @@ package org.codehaus.plexus; -import static org.codehaus.plexus.component.CastUtils.isAssignableFrom; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.IdentityHashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; -import java.util.Comparator; -import java.util.Map.Entry; - +import static com.google.common.base.ReferenceType.WEAK; +import static com.google.common.collect.Iterables.concat; +import com.google.common.collect.ListMultimap; +import static com.google.common.collect.Lists.newArrayList; +import static com.google.common.collect.Maps.newConcurrentHashMap; +import com.google.common.collect.Multimaps; +import com.google.common.collect.ReferenceMap; import org.codehaus.plexus.classworlds.realm.ClassRealm; -import org.codehaus.plexus.component.factory.ComponentInstantiationException; +import static org.codehaus.plexus.component.CastUtils.cast; +import org.codehaus.plexus.component.ComponentIndex; +import static org.codehaus.plexus.component.ComponentStack.pushComponentStack; +import static org.codehaus.plexus.component.ComponentStack.popComponentStack; import org.codehaus.plexus.component.manager.ComponentManager; import org.codehaus.plexus.component.manager.ComponentManagerFactory; +import org.codehaus.plexus.component.manager.StaticComponentManager; import org.codehaus.plexus.component.repository.ComponentDescriptor; -import org.codehaus.plexus.component.repository.ComponentRepository; +import org.codehaus.plexus.component.repository.ComponentDescriptorListener; import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.component.repository.exception.ComponentRepositoryException; @@ -29,44 +24,70 @@ import org.codehaus.plexus.lifecycle.LifecycleHandlerManager; import org.codehaus.plexus.lifecycle.UndefinedLifecycleHandlerException; import org.codehaus.plexus.logging.Logger; +import org.codehaus.plexus.logging.NullLogger; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicBoolean; public class DefaultComponentRegistry implements ComponentRegistry { private static final String DEFAULT_INSTANTIATION_STRATEGY = "singleton"; private final MutablePlexusContainer container; - private final ComponentRepository repository; private final LifecycleHandlerManager lifecycleHandlerManager; private final Logger logger; - private boolean disposingComponents; + private final AtomicBoolean disposed = new AtomicBoolean( false ); + + private final ConcurrentMap componentManagerFactories = newConcurrentHashMap(); + + private final ComponentIndex> index = new ComponentIndex>(); + private final Map, ComponentManager> componentManagersByComponentDescriptor = + new ReferenceMap, ComponentManager>( WEAK, WEAK); - private final Map componentManagerFactories = - Collections.synchronizedMap( new TreeMap() ); + private final Map> componentManagersByComponent = + new ReferenceMap>( WEAK, WEAK); - private final Map> componentManagers = new TreeMap>(); - private final Map> componentManagersByComponent = new IdentityHashMap>(); + private final ListMultimap, String>, ComponentDescriptorListener> listeners = Multimaps.newArrayListMultimap(); - public DefaultComponentRegistry( MutablePlexusContainer container, - ComponentRepository repository, - LifecycleHandlerManager lifecycleHandlerManager ) + public DefaultComponentRegistry( MutablePlexusContainer container, LifecycleHandlerManager lifecycleHandlerManager ) { this.container = container; - this.repository = repository; this.lifecycleHandlerManager = lifecycleHandlerManager; - logger = container.getLogger(); + + Logger containerLogger = container.getLogger(); + if ( containerLogger != null ) + { + logger = containerLogger; + } + else + { + logger = new NullLogger(); + } } public void dispose() { + if (disposed.getAndSet( true )) { + // already disposed + return; + } + List> managers; - synchronized ( this ) + synchronized ( index ) { - managers = new ArrayList>( componentManagers.values() ); - componentManagers.clear(); + managers = new ArrayList>( index.clear() ); + componentManagersByComponentDescriptor.clear(); componentManagersByComponent.clear(); - disposingComponents = true; } // reverse sort the managers by startId @@ -89,81 +110,180 @@ else if (left.getStartId() == right.getStartId() ) }); // Call dispose callback outside of synchronized lock to avoid deadlocks - try + for ( ComponentManager componentManager : managers ) { - for ( ComponentManager componentManager : managers ) + try { - try - { - componentManager.dispose(); - } - catch ( Exception e ) - { - // todo dain use a monitor instead of a logger - logger.error( "Error while disposing component manager. Continuing with the rest", e ); - } + componentManager.dispose(); } - } - finally - { - synchronized ( this ) + catch ( Exception e ) { - disposingComponents = false; + // todo dain use a monitor instead of a logger + logger.error( "Error while disposing component manager. Continuing with the rest", e ); } + fireComponentDescriptorRemoved( componentManager.getComponentDescriptor() ); } } + // + // Component Manager Factories + // + public void registerComponentManagerFactory( ComponentManagerFactory componentManagerFactory ) { + if ( disposed.get() ) + { + throw new IllegalStateException("ComponentRegistry has been disposed"); + } + componentManagerFactories.put( componentManagerFactory.getId(), componentManagerFactory ); } - public void addComponentDescriptor( ComponentDescriptor componentDescriptor ) throws ComponentRepositoryException + // + // Component Descriptors + // + + public void addComponentDescriptor( ComponentDescriptor componentDescriptor ) throws ComponentRepositoryException { - repository.addComponentDescriptor( componentDescriptor ); + if ( disposed.get() ) + { + throw new ComponentRepositoryException("ComponentRegistry has been disposed", componentDescriptor); + } + + // verify the descriptor matches the role hint and type + verifyComponentDescriptor( componentDescriptor ); + + // Get the ComponentManagerFactory + String instantiationStrategy = componentDescriptor.getInstantiationStrategy(); + if ( instantiationStrategy == null ) + { + instantiationStrategy = DEFAULT_INSTANTIATION_STRATEGY; + } + ComponentManagerFactory componentManagerFactory = componentManagerFactories.get( instantiationStrategy ); + if ( componentManagerFactory == null ) + { + throw new ComponentRepositoryException( "Unsupported instantiation strategy: " + instantiationStrategy, + componentDescriptor ); + } + + // Get the LifecycleHandler + LifecycleHandler lifecycleHandler; + try + { + lifecycleHandler = lifecycleHandlerManager.getLifecycleHandler( componentDescriptor.getLifecycleHandler() ); + } + catch ( UndefinedLifecycleHandlerException e ) + { + throw new ComponentRepositoryException( "Undefined lifecycle handler: " + componentDescriptor.getLifecycleHandler(), + componentDescriptor ); + } + + // Create the ComponentManager + ComponentManager componentManager = componentManagerFactory.createComponentManager( container, + lifecycleHandler, + componentDescriptor ); + + // Add componentManager to indexe + synchronized ( index ) + { + index.add( componentDescriptor.getRealm(), + componentDescriptor.getRoleClass(), + componentDescriptor.getRoleHint(), + componentManager); + componentManagersByComponentDescriptor.put(componentDescriptor, componentManager); + } + + fireComponentDescriptorAdded( componentDescriptor ); } - public ComponentDescriptor getComponentDescriptor( Class type, String role, String roleHint ) + public ComponentDescriptor getComponentDescriptor( Class type, String roleHint ) { - return repository.getComponentDescriptor( type, role, roleHint ); + ComponentManager componentManager = (ComponentManager) index.get( type, roleHint ); + if ( componentManager == null ) + { + return null; + } + return componentManager.getComponentDescriptor(); } - @Deprecated - public ComponentDescriptor getComponentDescriptor( String role, String roleHint, ClassRealm realm ) + public Map> getComponentDescriptorMap( Class type ) { - return repository.getComponentDescriptor( role, roleHint, realm ); + Map> componentManagers = cast( index.getAllAsMap( type ) ); + + Map> descriptors = new LinkedHashMap>(componentManagers.size()); + for ( Entry> entry : componentManagers.entrySet() ) + { + descriptors.put(entry.getKey(), entry.getValue().getComponentDescriptor()); + } + + return Collections.unmodifiableMap( descriptors ); } - public Map> getComponentDescriptorMap( Class type, String role ) + public List> getComponentDescriptorList( Class type ) { - return repository.getComponentDescriptorMap( type, role ); + List> componentManagers = cast( index.getAll( type ) ); + + List> descriptors = new ArrayList>(componentManagers.size()); + for ( ComponentManager componentManager : componentManagers ) + { + descriptors.add( componentManager.getComponentDescriptor() ); + } + + return Collections.unmodifiableList( descriptors ); } - public List> getComponentDescriptorList( Class type, String role ) + // + // Component Instances + // + + public void addComponent( T instance, Class type, String roleHint, ClassRealm realm ) throws ComponentRepositoryException { - return repository.getComponentDescriptorList( type, role ); + if ( disposed.get() ) + { + throw new ComponentRepositoryException("ComponentRegistry has been disposed", type, roleHint, realm); + } + + StaticComponentManager componentManager = new StaticComponentManager( container, instance, type, roleHint, realm ); + + // verify descriptor is consistent + ComponentDescriptor descriptor = componentManager.getComponentDescriptor(); + verifyComponentDescriptor( descriptor ); + + synchronized ( index ) + { + index.add( descriptor.getRealm(), descriptor.getRoleClass(), descriptor.getRoleHint(), componentManager); + componentManagersByComponentDescriptor.put( descriptor, componentManager); + } + + fireComponentDescriptorAdded( descriptor ); } - public T lookup( Class type, String role, String roleHint ) throws ComponentLookupException + public T lookup( Class type, String roleHint ) throws ComponentLookupException { // verify arguments if ( type == null ) { throw new NullPointerException( "type is null" ); } - if ( role == null ) - { - throw new NullPointerException( "role is null" ); - } if ( roleHint == null ) { roleHint = PlexusConstants.PLEXUS_DEFAULT_HINT; } - return getComponent( type, role, roleHint, null ); + return getComponent( type, roleHint ); + } + + public T lookup( ComponentDescriptor componentDescriptor ) throws ComponentLookupException + { + ComponentManager componentManager = (ComponentManager) componentManagersByComponentDescriptor.get( componentDescriptor ); + if ( componentManager == null ) + { + throw new ComponentLookupException( "Component descriptor is not registered with PlexusContainer", componentDescriptor ); + } + return getComponent( componentManager ); } - public Map lookupMap( Class type, String role, List roleHints ) + public Map lookupMap( Class type, List roleHints ) throws ComponentLookupException { // verify arguments @@ -171,23 +291,21 @@ public Map lookupMap( Class type, String role, List ro { throw new NullPointerException( "type is null" ); } - if ( role == null ) - { - throw new NullPointerException( "role is null" ); - } // if no hints provided, get all valid hints for this role Map components = new LinkedHashMap(); if ( roleHints == null ) { - Map> componentDescriptors = getComponentDescriptorMap( type, role ); - for ( Entry> entry : componentDescriptors.entrySet() ) + Map> componentManagers = cast( index.getAllAsMap( type ) ); + + for ( Entry> entry : componentManagers.entrySet() ) { String roleHint = entry.getKey(); - ComponentDescriptor componentDescriptor = entry.getValue(); + ComponentManager componentManager = entry.getValue(); + // todo dain catch the exception... it isn't the callers problem when one component in a collection fails - T component = getComponent( type, role, roleHint, componentDescriptor ); - components.put( roleHint, component ); + T component = getComponent( componentManager ); + components.put( roleHint, component); } } else @@ -195,7 +313,7 @@ public Map lookupMap( Class type, String role, List ro for ( String roleHint : roleHints ) { // todo dain catch the exception... it isn't the callers problem when one component in a collection fails - T component = getComponent( type, role, roleHint, null ); + T component = getComponent( type, roleHint ); components.put( roleHint, component ); } } @@ -203,28 +321,24 @@ public Map lookupMap( Class type, String role, List ro return components; } - public List lookupList( Class type, String role, List roleHints ) throws ComponentLookupException + public List lookupList( Class type, List roleHints ) throws ComponentLookupException { // verify arguments if ( type == null ) { throw new NullPointerException( "type is null" ); } - if ( role == null ) - { - throw new NullPointerException( "role is null" ); - } // if no hints provided, get all valid hints for this role List components = new ArrayList(); if ( roleHints == null ) { - List> componentDescriptors = getComponentDescriptorList( type, role ); - for ( ComponentDescriptor componentDescriptor : componentDescriptors ) + List> componentManagers = cast( index.getAll( type ) ); + for ( ComponentManager componentManager : componentManagers ) { // todo dain catch the exception... it isn't the callers problem when one component in a collection fails - T component = getComponent( type, role, componentDescriptor.getRoleHint(), componentDescriptor ); - components.add( component ); + T component = getComponent( componentManager ); + components.add( component); } } else @@ -232,7 +346,7 @@ public List lookupList( Class type, String role, List roleHint for ( String roleHint : roleHints ) { // todo dain catch the exception... it isn't the callers problem when one component in a collection fails - T component = getComponent( type, role, roleHint, null ); + T component = getComponent( type, roleHint ); components.add( component ); } } @@ -240,259 +354,257 @@ public List lookupList( Class type, String role, List roleHint return components; } - public void release( Object component ) - throws ComponentLifecycleException + public void addComponentDescriptorListener( ComponentDescriptorListener listener ) { - if ( component == null ) + Class type = listener.getType(); + List roleHints = listener.getRoleHints(); + synchronized ( this ) { - return; + if (roleHints == null ) + { + listeners.put(new Pair, String>(type, null), listener); + } + else + { + for ( String roleHint : roleHints ) + { + listeners.put(new Pair, String>(type, roleHint), listener); + } + } } - // get the component manager - ComponentManager componentManager; - synchronized ( this ) + // if no hints provided, get all valid hints for this role + if ( roleHints == null ) { - componentManager = componentManagersByComponent.get( component ); - if ( componentManager == null ) + List> componentManagers = cast( index.getAll( type ) ); + for ( ComponentManager componentManager : componentManagers ) { - // This needs to be tracked down but the user doesn't need to see this - // during the maven bootstrap this logger is null. - //logger.debug( "Component manager not found for returned component. Ignored. component=" + component ); - return; + listener.componentDescriptorAdded( componentManager.getComponentDescriptor() ); } } + else + { + for ( String roleHint : roleHints ) + { + ComponentManager componentManager = (ComponentManager) index.get( type, roleHint ); + if (componentManager != null) { + listener.componentDescriptorAdded( componentManager.getComponentDescriptor() ); + } + } + } + } - // release the component from the manager - componentManager.release( component ); - - // only drop the reference to this component if there are no other uses of the component - // multiple uses of a component is common with singleton beans - if ( componentManager.getConnections() <= 0 ) + public synchronized void removeComponentDescriptorListener( ComponentDescriptorListener listener ) + { + Class type = listener.getType(); + List roleHints = listener.getRoleHints(); + if (roleHints == null || roleHints.isEmpty()) + { + listeners.remove(new Pair, String>(type, null), listener); + } + else { - synchronized ( this ) + for ( String roleHint : roleHints ) { - componentManagersByComponent.remove( component ); + listeners.remove(new Pair, String>(type, roleHint), listener); } } } - public void removeComponentRealm( ClassRealm classRealm ) throws PlexusContainerException + private synchronized List> getListeners(ComponentDescriptor descriptor) { - repository.removeComponentRealm( classRealm ); + List> allHintListeners = + cast( listeners.get( new Pair, String>( descriptor.getRoleClass(), null ) ) ); + List> specificHintListeners = + cast( listeners.get( new Pair, String>( descriptor.getRoleClass(), descriptor.getRoleHint() ) ) ); - List> dispose = new ArrayList>(); - try + return newArrayList( concat(allHintListeners, specificHintListeners) ); + } + + private void fireComponentDescriptorAdded( ComponentDescriptor componentDescriptor ) + { + for ( ComponentDescriptorListener listener : getListeners( componentDescriptor )) { - synchronized ( this ) + try { - for ( Iterator>> it = componentManagers.entrySet().iterator(); it.hasNext(); ) - { - Entry> entry = it.next(); - Key key = entry.getKey(); - - ComponentManager componentManager = entry.getValue(); - - if ( key.realm.equals( classRealm ) ) - { - dispose.add( componentManager ); - it.remove(); - } - else - { - componentManager.dissociateComponentRealm( classRealm ); - } - } + listener.componentDescriptorAdded( componentDescriptor ); } - - // Call dispose callback outside of synchronized lock to avoid deadlocks - for ( ComponentManager componentManager : dispose ) + catch ( Throwable e ) { - componentManager.dispose(); + logger.debug( "ComponentDescriptorListener threw exception while processing " + componentDescriptor, e ); } } - catch ( ComponentLifecycleException e ) - { - throw new PlexusContainerException( "Failed to dissociate component realm: " + classRealm.getId(), e ); - } } - private T getComponent( Class type, String role, String roleHint, ComponentDescriptor descriptor ) - throws ComponentLookupException + private void fireComponentDescriptorRemoved( ComponentDescriptor componentDescriptor ) { - ComponentManager componentManager = getComponentManager( type, role, roleHint, descriptor ); - - // Get instance from manager... may result in creation - try + for ( ComponentDescriptorListener listener : getListeners( componentDescriptor )) { - T component = componentManager.getComponent(); - synchronized ( this ) + try { - componentManagersByComponent.put( component, componentManager ); + listener.componentDescriptorRemoved( componentDescriptor ); + } + catch ( Throwable e ) + { + logger.error( "ComponentDescriptorListener threw exception while processing " + componentDescriptor, e ); } - return component; - } - catch ( ComponentInstantiationException e ) - { - throw new ComponentLookupException( - "Unable to lookup component '" + componentManager.getRole() + "', it could not be created.", - componentManager.getRole(), componentManager.getRoleHint(), componentManager.getRealm(), e ); - } - catch ( ComponentLifecycleException e ) - { - throw new ComponentLookupException( - "Unable to lookup component '" + componentManager.getRole() + "', it could not be started.", - componentManager.getRole(), componentManager.getRoleHint(), componentManager.getRealm(), e ); } } - private synchronized ComponentManager getComponentManager( Class type, String role, String roleHint, ComponentDescriptor descriptor ) - throws ComponentLookupException + public void release( Object component ) throws ComponentLifecycleException { - if ( disposingComponents ) + if ( component == null ) { - throw new ComponentLookupException("ComponentRegistry is not active", - role, - roleHint ); + return; } - ComponentManager componentManager = getComponentManager( type, role, roleHint ); + // get the component manager + ComponentManager componentManager = componentManagersByComponent.get( component ); + if ( componentManager == null ) { - // we need to create a component manager, but first we must have a descriptor - if ( descriptor == null ) + // This needs to be tracked down but the user doesn't need to see this + // during the maven bootstrap this logger is null. + //logger.debug( "Component manager not found for returned component. Ignored. component=" + component ); + return; + } + + // release the component from the manager + componentManager.release( component ); + } + + public void removeComponentRealm( ClassRealm classRealm ) throws PlexusContainerException + { + try + { + // remove all component managers associated with the realm + LinkedHashSet> dispose; + synchronized ( index ) { - descriptor = getComponentDescriptor( type, role, roleHint ); - if ( descriptor == null ) + dispose = new LinkedHashSet>(index.removeAll( classRealm )); + for ( ComponentManager componentManager : dispose ) { - throw new ComponentLookupException( - "Component descriptor cannot be found in the component repository", - role, - roleHint ); + ComponentDescriptor descriptor = componentManager.getComponentDescriptor(); + componentManagersByComponentDescriptor.remove( descriptor ); + fireComponentDescriptorRemoved( descriptor ); } } - componentManager = createComponentManager( descriptor, role, roleHint ); + // Call dispose callback outside of synchronized lock to avoid deadlocks + for ( ComponentManager componentManager : dispose ) + { + componentManager.dispose(); + } + } + catch ( ComponentLifecycleException e ) + { + throw new PlexusContainerException( "Failed to dissociate component realm: " + classRealm.getId(), e ); } - return componentManager; } - private ComponentManager getComponentManager( Class type, String role, String roleHint ) + private T getComponent( Class type, String roleHint ) throws ComponentLookupException { - Set realms = getSearchRealms(); - - // return the component in the first realm - for ( ClassRealm realm : realms ) + ComponentManager componentManager = (ComponentManager) index.get( type, roleHint ); + if ( componentManager == null ) { - ComponentManager manager = componentManagers.get( new Key( realm, role, roleHint ) ); - if ( manager != null && isAssignableFrom( type, manager.getType() ) ) - { - return (ComponentManager) manager; - } + throw new ComponentLookupException( "Component descriptor cannot be found", type, roleHint ); } - return null; + return getComponent( componentManager ); } - private Set getSearchRealms() + private T getComponent( ComponentManager componentManager ) throws ComponentLookupException { - // determine realms to search - Set realms = new LinkedHashSet(); - for ( ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); classLoader != null; classLoader = classLoader.getParent() ) + ComponentDescriptor descriptor = componentManager.getComponentDescriptor(); + + // Get instance from manager... may result in creation + pushComponentStack( descriptor ); + try + { + T component = componentManager.getComponent(); + + componentManagersByComponent.put( component, componentManager ); + + return component; + } + catch ( Exception e ) { - if ( classLoader instanceof ClassRealm ) + // get real cause + Throwable cause = e.getCause(); + if ( cause == null ) { - ClassRealm realm = (ClassRealm) classLoader; - while ( realm != null ) - { - realms.add( realm ); - realm = realm.getParentRealm(); - } + cause = e; } - } - if ( realms.isEmpty() ) + // do not rewrap ComponentLookupException + if (cause instanceof ComponentLookupException ) + { + throw (ComponentLookupException) cause; + } + + throw new ComponentLookupException( e.getMessage(), descriptor, cause ); + } + finally { - realms.addAll( container.getClassWorld().getRealms() ); + popComponentStack(); } - - return realms; } - private ComponentManager createComponentManager( ComponentDescriptor descriptor, - String role, - String roleHint ) - throws ComponentLookupException + private void verifyComponentDescriptor( ComponentDescriptor descriptor ) throws ComponentRepositoryException { - // Get the ComponentManagerFactory - String instantiationStrategy = descriptor.getInstantiationStrategy(); - if ( instantiationStrategy == null ) + ClassLoader classLoader = descriptor.getRealm(); + if ( classLoader == null) { - instantiationStrategy = DEFAULT_INSTANTIATION_STRATEGY; + throw new ComponentRepositoryException( "ComponentDescriptor realm is null", descriptor); } - ComponentManagerFactory componentManagerFactory = componentManagerFactories.get( instantiationStrategy ); - if ( componentManagerFactory == null ) + + Class implementationClass = descriptor.getImplementationClass(); + if (implementationClass.equals( Object.class )) { - throw new ComponentLookupException( "Unsupported instantiation strategy: " + instantiationStrategy, - role, - roleHint, - descriptor.getRealm() ); + throw new ComponentRepositoryException( "ComponentDescriptor implementation class could not be loaded", descriptor); } - // Get the LifecycleHandler - LifecycleHandler lifecycleHandler; + String role = descriptor.getRole(); + if (role == null) + { + throw new ComponentRepositoryException( "ComponentDescriptor role is null", descriptor); + } + + Class roleClass; try { - lifecycleHandler = lifecycleHandlerManager.getLifecycleHandler( descriptor.getLifecycleHandler() ); + roleClass = classLoader.loadClass( role ); } - catch ( UndefinedLifecycleHandlerException e ) + catch ( ClassNotFoundException e ) { - throw new ComponentLookupException( "Undefined lifecycle handler: " + descriptor.getLifecycleHandler(), - role, - roleHint, - descriptor.getRealm() ); + throw new ComponentRepositoryException( "ComponentDescriptor role class can not be loaded", descriptor); } - // Create the ComponentManager - ComponentManager componentManager = componentManagerFactory.createComponentManager( container, - lifecycleHandler, - descriptor, - role, - roleHint ); - - // Add componentManager to indexes - Key key = new Key( descriptor.getRealm(), role, roleHint ); - componentManagers.put( key, componentManager ); - - return componentManager; + if (!roleClass.isAssignableFrom( implementationClass )) + { + throw new ComponentRepositoryException( "ComponentDescriptor implementation class does not implement the role class:" + + " implementationClass=" + implementationClass.getName() + " roleClass=" + roleClass.getName(), + descriptor); + } } - private static class Key implements Comparable - { - private final ClassRealm realm; - private final String role; - private final String roleHint; - private final int hashCode; + public static class Pair { + private final L left; + private final R right; - private Key( ClassRealm realm, String role, String roleHint ) + public Pair( L left, R right ) { - this.realm = realm; - - if ( role == null ) - { - role = "null"; - } - this.role = role; + this.left = left; + this.right = right; + } - if ( roleHint == null ) - { - roleHint = "null"; - } - this.roleHint = roleHint; + public L getLeft() + { + return left; + } - int hashCode; - hashCode = ( realm != null ? realm.hashCode() : 0 ); - hashCode = 31 * hashCode + role.hashCode(); - hashCode = 31 * hashCode + roleHint.hashCode(); - this.hashCode = hashCode; + public R getRight() + { + return right; } public boolean equals( Object o ) @@ -501,50 +613,32 @@ public boolean equals( Object o ) { return true; } - if ( o == null || getClass() != o.getClass() ) + if ( !( o instanceof Pair ) ) { return false; } - Key key = (Key) o; + Pair pair = (Pair) o; - return !( realm != null ? !realm.equals( key.realm ) : key.realm != null ) && - role.equals( key.role ) && - roleHint.equals( key.roleHint ); + return + ( left == null ? pair.left == null : left.equals( pair.left ) ) && + ( right == null ? pair.right == null : right.equals( pair.right ) ); } public int hashCode() { - return hashCode; + int result; + result = ( left != null ? left.hashCode() : 0 ); + result = 31 * result + ( right != null ? right.hashCode() : 0 ); + return result; } public String toString() { - return realm + "/" + role + "/" + roleHint; - } - - public int compareTo( Key o ) - { - int value; - if ( realm != null ) - { - value = realm.getId().compareTo( o.realm.getId() ); - } - else - { - value = o.realm == null ? 0 : 1; - } - - if ( value == 0 ) - { - value = role.compareTo( o.role ); - if ( value == 0 ) - { - value = roleHint.compareTo( o.roleHint ); - } - } - return value; + StringBuilder buf = new StringBuilder( ); + buf.append("[").append(left).append(", ").append(right).append("]"); + return buf.toString(); } } } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultContainerConfiguration.java b/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultContainerConfiguration.java index 1d3ebecd4..c44b8ffc6 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultContainerConfiguration.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultContainerConfiguration.java @@ -13,8 +13,6 @@ import org.codehaus.plexus.component.discovery.PlexusXmlComponentDiscoverer; import org.codehaus.plexus.component.factory.ComponentFactoryManager; import org.codehaus.plexus.component.factory.DefaultComponentFactoryManager; -import org.codehaus.plexus.component.repository.ComponentRepository; -import org.codehaus.plexus.component.repository.DefaultComponentRepository; import org.codehaus.plexus.configuration.source.ConfigurationSource; import org.codehaus.plexus.container.initialization.ContainerInitializationPhase; import org.codehaus.plexus.container.initialization.InitializeComponentDiscovererManagerPhase; @@ -212,25 +210,6 @@ public ContainerConfiguration setComponentFactoryManager( ComponentFactoryManage return this; } - private ComponentRepository componentRepository; - - public ContainerConfiguration setComponentRepository( ComponentRepository componentRepository ) - { - this.componentRepository = componentRepository; - - return this; - } - - public ComponentRepository getComponentRepository() - { - if ( componentRepository == null ) - { - componentRepository = new DefaultComponentRepository(); - } - - return componentRepository; - } - // Lifecycle handler manager private LifecycleHandlerManager lifecycleHandlerManager; diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultPlexusContainer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultPlexusContainer.java index 38f531013..72f836141 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultPlexusContainer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultPlexusContainer.java @@ -27,7 +27,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import org.codehaus.plexus.classworlds.ClassWorld; import org.codehaus.plexus.classworlds.realm.ClassRealm; @@ -40,6 +39,7 @@ import org.codehaus.plexus.component.factory.ComponentFactoryManager; import org.codehaus.plexus.component.repository.ComponentDescriptor; import org.codehaus.plexus.component.repository.ComponentSetDescriptor; +import org.codehaus.plexus.component.repository.ComponentDescriptorListener; import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.component.repository.exception.ComponentRepositoryException; @@ -50,7 +50,6 @@ import org.codehaus.plexus.configuration.source.ConfigurationSource; import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration; import org.codehaus.plexus.container.initialization.ContainerInitializationContext; -import org.codehaus.plexus.container.initialization.ContainerInitializationException; import org.codehaus.plexus.container.initialization.ContainerInitializationPhase; import org.codehaus.plexus.context.Context; import org.codehaus.plexus.context.ContextException; @@ -127,15 +126,26 @@ public class DefaultPlexusContainer public void addComponent( Object component, String role ) throws ComponentRepositoryException { - ComponentDescriptor cd = new ComponentDescriptor(); + ClassRealm classRealm = null; - cd.setRole( role ); - - cd.setRoleHint( PLEXUS_DEFAULT_HINT ); + // find a realm for this instance + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + if ( classLoader instanceof ClassRealm ) + { + classRealm = (ClassRealm) classLoader; + } + if (classRealm == null) + { + classRealm = getContainerRealm(); + } - cd.setImplementation( role ); + addComponent( component, getRoleClass( role ), PLEXUS_DEFAULT_HINT, classRealm ); + } - addComponentDescriptor( cd ); + public void addComponent( T component, Class type, String roleHint, ClassRealm classRealm ) + throws ComponentRepositoryException + { + componentRegistry.addComponent( component, type, roleHint, classRealm ); } public ClassRealm setLookupRealm( ClassRealm realm ) @@ -233,10 +243,10 @@ private void construct( ContainerConfiguration c ) if ( c.getContext() != null ) { - containerContext = new DefaultContext( c.getContext() ); + containerContext = new DefaultContext( c.getContext() ); } else - { + { containerContext = new DefaultContext(); } @@ -290,41 +300,47 @@ private void construct( ContainerConfiguration c ) private Class getInterfaceClass( String role, String hint ) { - ComponentDescriptor cd; + if ( hint == null ) hint = PLEXUS_DEFAULT_HINT; - if ( hint == null ) + try { - cd = getComponentDescriptor( role ); + ClassRealm realm = getLookupRealm(); + + if ( realm != null ) + { + return realm.loadClass( role ); + } } - else + catch ( Throwable e ) { - cd = getComponentDescriptor( role, hint ); } - - if ( cd != null ) - { - try + + try + { + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + if ( loader != null ) { - ClassRealm realm = getLookupRealm(); + return loader.loadClass( role ); + } + } + catch ( Throwable e ) + { + } - if ( realm != null ) + try + { + ComponentDescriptor cd = componentRegistry.getComponentDescriptor( Object.class, hint ); + if ( cd != null ) + { + ClassLoader loader = cd.getImplementationClass().getClassLoader(); + if ( loader != null ) { - return realm.loadClass( role ); - } - else - { - ClassLoader loader = cd.getImplementationClass().getClassLoader(); - - if ( loader != null ) - { - return loader.loadClass( role ); - } + return loader.loadClass( role ); } } - catch ( ClassNotFoundException e ) - { - return Object.class; - } + } + catch ( Throwable ignored ) + { } return Object.class; @@ -342,68 +358,73 @@ private Class getRoleClass( String role, String hint ) public Object lookup( String role ) throws ComponentLookupException { - return componentRegistry.lookup( getRoleClass( role ), role, PLEXUS_DEFAULT_HINT ); + return componentRegistry.lookup( getRoleClass( role ), PLEXUS_DEFAULT_HINT ); } public Object lookup( String role, String roleHint ) throws ComponentLookupException { - return componentRegistry.lookup( getRoleClass( role, roleHint ), role, roleHint ); + return componentRegistry.lookup( getRoleClass( role, roleHint ), roleHint ); } public T lookup( Class type ) throws ComponentLookupException { - return componentRegistry.lookup( type, type.getName(), PLEXUS_DEFAULT_HINT ); + return componentRegistry.lookup( type, PLEXUS_DEFAULT_HINT ); } public T lookup( Class type, String roleHint ) throws ComponentLookupException { - return componentRegistry.lookup( type, type.getName(), roleHint ); + return componentRegistry.lookup( type, roleHint ); } + @Deprecated public T lookup( Class type, String role, String roleHint ) throws ComponentLookupException { - - return componentRegistry.lookup( type, role, roleHint ); + return componentRegistry.lookup( type, roleHint ); + } + + public T lookup( ComponentDescriptor descriptor ) throws ComponentLookupException + { + return componentRegistry.lookup( descriptor ); } public List lookupList( String role ) throws ComponentLookupException { - return cast(componentRegistry.lookupList( getRoleClass( role ), role, null)); + return cast(componentRegistry.lookupList( getRoleClass( role ), null)); } public List lookupList( String role, List roleHints ) throws ComponentLookupException { - return cast(componentRegistry.lookupList( getRoleClass( role ), role, roleHints )); + return cast(componentRegistry.lookupList( getRoleClass( role ), roleHints )); } public List lookupList( Class type ) throws ComponentLookupException { - return componentRegistry.lookupList( type, type.getName(), null ); + return componentRegistry.lookupList( type, null ); } public List lookupList( Class type, List roleHints ) throws ComponentLookupException { - return componentRegistry.lookupList( type, type.getName(), roleHints ); + return componentRegistry.lookupList( type, roleHints ); } public Map lookupMap( String role ) throws ComponentLookupException { - return cast(componentRegistry.lookupMap( getRoleClass( role ), role, null )); + return cast(componentRegistry.lookupMap( getRoleClass( role ), null )); } public Map lookupMap( String role, List roleHints ) throws ComponentLookupException { - return cast(componentRegistry.lookupMap( getRoleClass( role ), role, roleHints )); + return cast(componentRegistry.lookupMap( getRoleClass( role ), roleHints )); } public Map lookupMap( Class type ) throws ComponentLookupException { - return componentRegistry.lookupMap( type, type.getName(), null ); + return componentRegistry.lookupMap( type, null ); } public Map lookupMap( Class type, List roleHints ) throws ComponentLookupException { - return componentRegistry.lookupMap( type, type.getName(), roleHints ); + return componentRegistry.lookupMap( type, roleHints ); } // ---------------------------------------------------------------------- @@ -412,62 +433,63 @@ public Map lookupMap( Class type, List roleHints ) thr public boolean hasComponent( String role ) { - return componentRegistry.getComponentDescriptor( Object.class, role, PLEXUS_DEFAULT_HINT ) != null; + return componentRegistry.getComponentDescriptor( getRoleClass( role ), PLEXUS_DEFAULT_HINT ) != null; } public boolean hasComponent( String role, String roleHint ) { - return componentRegistry.getComponentDescriptor( Object.class, role, roleHint ) != null; + return componentRegistry.getComponentDescriptor( getRoleClass( role ), roleHint ) != null; } public boolean hasComponent( Class type ) { - return componentRegistry.getComponentDescriptor( type, type.getName(), PLEXUS_DEFAULT_HINT ) != null; + return componentRegistry.getComponentDescriptor( type, PLEXUS_DEFAULT_HINT ) != null; } public boolean hasComponent( Class type, String roleHint ) { - return componentRegistry.getComponentDescriptor( type, type.getName(), roleHint ) != null; + return componentRegistry.getComponentDescriptor( type, roleHint ) != null; } - public boolean hasComponent( Class type, String role, String roleHint ) + public ComponentDescriptor getComponentDescriptor( String role ) { - return componentRegistry.getComponentDescriptor( type, role, roleHint ) != null; + return componentRegistry.getComponentDescriptor( getRoleClass( role ), PLEXUS_DEFAULT_HINT ); } - public ComponentDescriptor getComponentDescriptor( String role ) + public ComponentDescriptor getComponentDescriptor( String role, String roleHint ) { - return componentRegistry.getComponentDescriptor( Object.class, role, PLEXUS_DEFAULT_HINT ); + return componentRegistry.getComponentDescriptor( getRoleClass( role ), roleHint ); } - public ComponentDescriptor getComponentDescriptor( String role, String roleHint ) + public ComponentDescriptor getComponentDescriptor( Class type ) { - return componentRegistry.getComponentDescriptor( Object.class, role, roleHint ); + return componentRegistry.getComponentDescriptor( type, PLEXUS_DEFAULT_HINT ); } - public ComponentDescriptor getComponentDescriptor( Class type, String role, String roleHint ) + public ComponentDescriptor getComponentDescriptor( Class type, String roleHint ) { - return componentRegistry.getComponentDescriptor( type, role, roleHint ); + return componentRegistry.getComponentDescriptor( type, roleHint ); } public Map> getComponentDescriptorMap( String role ) { - return cast(componentRegistry.getComponentDescriptorMap( Object.class, role )); + return cast(componentRegistry.getComponentDescriptorMap( getRoleClass( role ) )); } + @Deprecated public Map> getComponentDescriptorMap( Class type, String role ) { - return componentRegistry.getComponentDescriptorMap( type, role ); + return componentRegistry.getComponentDescriptorMap( type ); } public List> getComponentDescriptorList( String role ) { - return cast(componentRegistry.getComponentDescriptorList( Object.class, role )); + return cast(componentRegistry.getComponentDescriptorList( getRoleClass( role ) )); } - public List> getComponentDescriptorList( Class type, String role ) + public List> getComponentDescriptorList( Class type ) { - return componentRegistry.getComponentDescriptorList( type, role ); + return componentRegistry.getComponentDescriptorList( type ); } public void addComponentDescriptor( ComponentDescriptor componentDescriptor ) throws ComponentRepositoryException @@ -524,7 +546,7 @@ protected void initialize( ContainerConfiguration containerConfiguration ) containerContext.put( PlexusConstants.PLEXUS_KEY, this ); discoverComponents( getContainerRealm() ); - + PlexusConfiguration[] loadOnStartComponents = getConfiguration().getChild( "load-on-start" ).getChildren( "component" ); getLogger().debug( "Found " + loadOnStartComponents.length + " components to load on start" ); @@ -571,7 +593,7 @@ protected void initialize( ContainerConfiguration containerConfiguration ) { Thread.currentThread().setContextClassLoader( prevCl ); } - + } catch ( ContextException e ) { @@ -690,6 +712,20 @@ public Context getContext() return containerContext; } + // ---------------------------------------------------------------------- + // ComponentListener + // ---------------------------------------------------------------------- + + public void addComponentDescriptorListener( ComponentDescriptorListener listener ) + { + componentRegistry.addComponentDescriptorListener( listener ); + } + + public void removeComponentDescriptorListener( ComponentDescriptorListener listener ) + { + componentRegistry.removeComponentDescriptorListener( listener ); + } + // ---------------------------------------------------------------------- // Configuration // ---------------------------------------------------------------------- @@ -917,21 +953,26 @@ public void setLoggerManager( LoggerManager loggerManager ) public List> discoverComponents( ClassRealm realm ) throws PlexusConfigurationException, ComponentRepositoryException { - List> discoveredComponentDescriptors = new ArrayList>(); + List componentSets = new ArrayList(); for ( ComponentDiscoverer componentDiscoverer : getComponentDiscovererManager().getComponentDiscoverers() ) { for ( ComponentSetDescriptor componentSet : componentDiscoverer.findComponents( getContext(), realm ) ) { - for ( ComponentDescriptor componentDescriptor : componentSet.getComponents() ) - { - addComponentDescriptor( componentDescriptor ); - - discoveredComponentDescriptors.add( componentDescriptor ); - } + componentSets.add(componentSet); } } + List> discoveredComponentDescriptors = new ArrayList>(); + for ( ComponentSetDescriptor componentSet : componentSets ) + { + for ( ComponentDescriptor componentDescriptor : componentSet.getComponents() ) + { + addComponentDescriptor( componentDescriptor ); + + discoveredComponentDescriptors.add( componentDescriptor ); + } + } return discoveredComponentDescriptors; } } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/PlexusContainer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/PlexusContainer.java index c31a5ed35..de54564a9 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/PlexusContainer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/PlexusContainer.java @@ -19,6 +19,7 @@ import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.codehaus.plexus.component.discovery.ComponentDiscoveryListener; import org.codehaus.plexus.component.repository.ComponentDescriptor; +import org.codehaus.plexus.component.repository.ComponentDescriptorListener; import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.component.repository.exception.ComponentRepositoryException; @@ -74,6 +75,14 @@ T lookup( Class type ) T lookup( Class type, String roleHint ) throws ComponentLookupException; + /** + * Looks up and returns a component object for the specified and registered component descriptor. + * @param descriptor a conponent descriptor registered with the container + * @return a Plexus component object + */ + T lookup( ComponentDescriptor descriptor ) + throws ComponentLookupException; + /** * Looks up and returns a component object with the given unique role/role-hint combination. * @param type the non-unique type of the component @@ -81,6 +90,7 @@ T lookup( Class type, String roleHint ) * @param roleHint a hint for the desired component implementation * @return a Plexus component object */ + @Deprecated T lookup( Class type, String role, String roleHint ) throws ComponentLookupException; @@ -169,15 +179,22 @@ Map lookupMap( Class type, List roleHints ) */ ComponentDescriptor getComponentDescriptor( String role, String roleHint ); + /** + * Returns the ComponentDescriptor with the given component role and the default role hint. + * Searches up the hierarchy until one is found, null if none is found. + * @param type the unique type of the component within the container + * @return the ComponentDescriptor with the given component role + */ + ComponentDescriptor getComponentDescriptor( Class type ); + /** * Returns the ComponentDescriptor with the given component role and hint. * Searches up the hierarchy until one is found, null if none is found. - * @param type the Java type of the desired component - * @param role a unique role for the desired component's descriptor + * @param type the non-unique type of the component * @param roleHint a hint showing which implementation should be used * @return the ComponentDescriptor with the given component role */ - ComponentDescriptor getComponentDescriptor( Class type, String role, String roleHint ); + ComponentDescriptor getComponentDescriptor( Class type, String roleHint ); /** * Returns a Map of ComponentDescriptors with the given role, keyed by role-hint. Searches up the hierarchy until @@ -194,6 +211,7 @@ Map lookupMap( Class type, List roleHints ) * @param role a non-unique key for the desired components * @return a Map of component descriptors keyed by role-hint */ + @Deprecated Map> getComponentDescriptorMap( Class type, String role ); /** @@ -208,10 +226,9 @@ Map lookupMap( Class type, List roleHints ) * Returns a List of ComponentDescriptors with the given role. Searches up the hierarchy until all are found, an * empty List if none are found. * @param type the Java type of the desired components - * @param role a non-unique key for the desired components * @return a List of component descriptors */ - List> getComponentDescriptorList( Class type, String role ); + List> getComponentDescriptorList( Class type ); /** * Adds a component descriptor to this container. componentDescriptor should have realmId set. @@ -279,15 +296,6 @@ void releaseAll( List components ) */ boolean hasComponent( Class type, String roleHint ); - /** - * Returns true if this container has a component with the given role/role-hint. - * @param type the non-unique type of the component - * @param role a non-unique key for the desired component - * @param roleHint a hint for the desired component implementation - * @return true if this container has a component with the given role/role-hint - */ - boolean hasComponent( Class type, String role, String roleHint ); - /** * Disposes of this container, which in turn disposes all of it's components. This container should also remove * itself from the container hierarchy. @@ -319,6 +327,13 @@ void releaseAll( List components ) */ ClassRealm getContainerRealm(); + // ---------------------------------------------------------------------- + // ComponentListener + // ---------------------------------------------------------------------- + void addComponentDescriptorListener( ComponentDescriptorListener listener ); + + void removeComponentDescriptorListener( ComponentDescriptorListener listener ); + // ---------------------------------------------------------------------- // Discovery // ---------------------------------------------------------------------- @@ -339,7 +354,6 @@ void releaseAll( List components ) /** * Discovers components in the given realm. * @param childRealm - * @param override wheter to override/merge any conflicting components, where the new component takes precedence. * @return * @throws PlexusConfigurationException * @throws ComponentRepositoryException diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/CastUtils.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/CastUtils.java index 0a3577281..2b691c855 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/CastUtils.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/CastUtils.java @@ -8,6 +8,8 @@ import java.util.Map; import java.util.Set; +import com.google.common.collect.Multimap; + @SuppressWarnings({"unchecked", "UnusedDeclaration"}) public final class CastUtils { private CastUtils() { @@ -22,6 +24,14 @@ public static Map cast(Map p, Class t, Class u) { return (Map) p; } + public static Multimap cast(Multimap p) { + return (Multimap) p; + } + + public static Multimap cast(Multimap p, Class t, Class u) { + return (Multimap) p; + } + public static Collection cast(Collection p) { return (Collection) p; } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/ComponentIndex.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/ComponentIndex.java new file mode 100644 index 000000000..02eb4f29f --- /dev/null +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/ComponentIndex.java @@ -0,0 +1,411 @@ +package org.codehaus.plexus.component; + +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import com.google.common.collect.Multimaps; +import org.codehaus.plexus.classworlds.realm.ClassRealm; +import static org.codehaus.plexus.PlexusConstants.PLEXUS_DEFAULT_HINT; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Comparator; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.SortedMap; +import java.util.TreeMap; +import java.util.Iterator; +import java.util.Collections; + +public class ComponentIndex +{ + /** + * Should values be indexed by all types or simply the supplied type? + */ + private final boolean indexByAllTypes; + + /** + * This is the actual index. + * ClassLoader -> Class -> RoleHint -> Values + */ + private final Map, Multimap>> index = + new LinkedHashMap, Multimap>>(); + + /** + * Creates a component index that indexes by all super types and interfaces of supplied type. + */ + public ComponentIndex() + { + this( false ); + } + + /** + * Creates a component index. + * @param indexByAllTypes if true, values are indexed by all super types and interfaces of supplied type; otherwise + * values are only indexed by supplied type + */ + public ComponentIndex( boolean indexByAllTypes ) + { + this.indexByAllTypes = indexByAllTypes; + } + + /** + * Are values are indexed by all super types and interfaces of supplied type? + * @return true, values are indexed by all super types and interfaces of supplied type; otherwise + * false and values are only indexed by supplied type + */ + public boolean isIndexByAllTypes() + { + return indexByAllTypes; + } + + /** + * Gets the value associated with the specified type and roleHint. + * + * Values are searched for in classloader order starting from the thread context class loader or type class loader + * if thread context class loader is not set. + * + * @param type the type (or super type if enabled) associated with the value; not null + * @param roleHint the roleHint associated with the value, or null for the default roleHint + * @return the value associated with the type and roleHint, or null + */ + public synchronized V get( Class type, String roleHint ) + { + return get( type, roleHint, Thread.currentThread().getContextClassLoader() ); + } + + /** + * Gets the value associated with the specified type and roleHint. + * + * Values are searched for in classloader order starting from the specified class loader, or thread context class + * loader or type class loader if specified class loader is null. + * + * @param type the type (or super type if enabled) associated with the value; not null + * @param roleHint the roleHint associated with the value, or null for the default roleHint + * @param classLoader the class loader to search from + * @return the value associated with the type and roleHint, or null + */ + public synchronized V get( Class type, String roleHint, ClassLoader classLoader ) + { + if ( type == null ) + { + throw new NullPointerException( "type is null" ); + } + if ( roleHint == null ) + { + roleHint = PLEXUS_DEFAULT_HINT; + } + + Collection values = findAll( type, classLoader ).get( roleHint ); + if ( values.isEmpty() ) + { + return null; + } + return values.iterator().next(); + } + + public synchronized Collection getAll( ) + { + ArrayList values = new ArrayList(); + + for ( SortedMap, Multimap> roleIndex : index.values() ) + { + for ( Multimap roleHintIndex : roleIndex.values() ) + { + values.addAll(roleHintIndex.values()); + } + } + return values; + } + + /** + * Gets all values associated with the specified type. + * + * Values are searched for in classloader order starting from the thread context class loader or type class loader + * if thread context class loader is not set. + * + * The values are sorted in class loader search order then by registration order. + * + * @param type the type (or super type if enabled) associated with the value; not null + * @return all values associated with the type; never null + */ + public synchronized List getAll( Class type ) + { + return getAll( type, Thread.currentThread().getContextClassLoader() ); + } + + /** + * Gets all values associated with the specified type. + * + * Values are searched for in classloader order starting from the specified class loader, or thread context class + * loader or type class loader if specified class loader is null. + * + * The values are sorted in class loader search order then by registration order. + * + * @param type the type (or super type if enabled) associated with the value; not null + * @param classLoader the class loader to search from + * @return all values associated with the type; never null + */ + public synchronized List getAll( Class type, ClassLoader classLoader ) + { + if ( type == null ) + { + throw new NullPointerException( "type is null" ); + } + + return new ArrayList( findAll( type, classLoader ).values() ); + } + + /** + * Gets a map of all values associated with the specified type indexed by roleHint. + * + * Values are searched for in classloader order starting from the thread context class loader or type class loader + * if thread context class loader is not set. + * + * @param type the type (or super type if enabled) associated with the value; not null + * @return all of the value associated with the type; never null + */ + public synchronized Map getAllAsMap( Class type ) + { + return getAllAsMap( type, Thread.currentThread().getContextClassLoader() ); + } + + /** + * Gets a map of all values associated with the specified type indexed by roleHint. + * + * Values are searched for in classloader order starting from the specified class loader, or thread context class + * loader or type class loader if specified class loader is null. + * + * @param type the type (or super type if enabled) associated with the value; not null + * @param classLoader the class loader to search from + * @return all of the value associated with the type; never null + */ + public synchronized Map getAllAsMap( Class type, ClassLoader classLoader ) + { + if ( type == null ) + { + throw new NullPointerException( "type is null" ); + } + + Map descriptors = new TreeMap(); + for ( Entry entry : findAll( type, classLoader ).entries() ) + { + if ( !descriptors.containsKey( entry.getKey() ) ) + { + descriptors.put( entry.getKey(), entry.getValue() ); + } + } + return descriptors; + } + + private synchronized Multimap findAll( Class type, ClassLoader classLoader ) + { + if ( classLoader == null ) + { + classLoader = type.getClassLoader(); + } + + // Determine class loaders to search + LinkedHashSet classLoaders = new LinkedHashSet(); + for ( ClassLoader cl = classLoader; cl != null; cl = cl.getParent() ) + { + if ( cl instanceof ClassRealm ) + { + ClassRealm realm = (ClassRealm) cl; + while ( realm != null ) + { + classLoaders.add( realm ); + realm = realm.getParentRealm(); + } + } + else + { + // todo lots of plexus code depends on a global search when there is a class loader associated with + // the thread but the cl is not a class realm + // classLoaders.add( cl ); + } + } + // todo remove this when plexus code is updated to manage thread context class loader correctly + if ( classLoaders.isEmpty() ) + { + classLoaders.addAll( index.keySet() ); + } + + // Get all valid component descriptors + Multimap roleHintIndex = Multimaps.newHashMultimap(); + for ( ClassLoader cl : classLoaders ) + { + SortedMap, Multimap> roleIndex = index.get( cl ); + if ( roleIndex != null ) + { + Multimap values = roleIndex.get( type ); + if ( values != null ) + { + roleHintIndex.putAll( values ); + } + } + } + return Multimaps.unmodifiableMultimap( roleHintIndex ); + } + + /** + * Associate a value with the specified class loader, type and roleHint. The value is also associated with all + * superclasses and interfaces of the specified type unless index by all types is disabled. + */ + public synchronized void add( ClassLoader classLoader, Class type, String roleHint, V value ) + { + if ( classLoader == null ) + { + throw new NullPointerException( "classLoader is null" ); + } + if ( type == null ) + { + throw new NullPointerException( "type is null" ); + } + if ( roleHint == null ) + { + roleHint = PLEXUS_DEFAULT_HINT; + } + if ( value == null ) + { + throw new NullPointerException( "value is null" ); + } + + SortedMap, Multimap> roleIndex = index.get( classLoader ); + if ( roleIndex == null ) + { + roleIndex = new TreeMap, Multimap>( ClassComparator.INSTANCE ); + index.put( classLoader, roleIndex ); + } + + for ( Class clazz : getAllTypes( type ) ) + { + Multimap roleHintIndex = roleIndex.get( clazz ); + if ( roleHintIndex == null ) + { + roleHintIndex = new ArrayListMultimap(); + roleIndex.put( clazz, roleHintIndex ); + } + roleHintIndex.put( roleHint, value ); + } + } + + /** + * Removes the specified value from the index. This is operation requires a linear search of the whole index, and + * is therefor very expensive. + * @param value the value to remove + */ + public synchronized void remove( V value ) + { + if ( value == null ) + { + throw new NullPointerException( "value is null" ); + } + + for ( SortedMap, Multimap> roleIndex : index.values() ) + { + for ( Multimap roleHintIndex : roleIndex.values() ) + { + for ( Iterator iterator = roleHintIndex.values().iterator(); iterator.hasNext(); ) + { + V v = iterator.next(); + if ( value.equals( v ) ) + { + iterator.remove(); + } + } + } + } + } + + /** + * Removes all values associated with the specified class loader. This operation is very fast. + */ + public synchronized List removeAll( ClassLoader classLoader ) + { + if ( classLoader == null ) + { + throw new NullPointerException( "classLoader is null" ); + } + + ArrayList values = new ArrayList(); + + SortedMap, Multimap> roleIndex = index.remove( classLoader ); + for ( Multimap roleHintIndex : roleIndex.values() ) + { + values.addAll(roleHintIndex.values()); + } + return values; + } + + /** + * Removes all values from this index. + */ + public synchronized Collection clear() + { + Collection all = getAll(); + index.clear(); + return all; + } + + private Set> getAllTypes( Class type ) + { + if ( type.isArray() ) + { + throw new IllegalArgumentException( "type is an array: type=" + type ); + } + + // if we are not indexing by all types, simply return a set containing the source type + if ( !indexByAllTypes ) + { + return Collections.>singleton( type ); + } + + // All found types + Set> allTypes = new LinkedHashSet>(); + + // Types that must still be processed... may contain entries that + // have already been added to allTypes, so check all types before + // actuall processing to avoid infinite loops + LinkedList> typesToProcess = new LinkedList>(); + typesToProcess.add( type ); + + while ( !typesToProcess.isEmpty() ) + { + Class clazz = typesToProcess.removeFirst(); + + // have we already processed this type + if ( !allTypes.contains( clazz ) ) + { + allTypes.add( clazz ); + + // schedule superclass for processing + Class superclass = clazz.getSuperclass(); + if ( superclass != null ) + { + typesToProcess.addFirst( superclass ); + } + + // schedule all interfaces for processing + typesToProcess.addAll( 0, Arrays.>asList( clazz.getInterfaces() ) ); + } + } + return allTypes; + } + + private static final class ClassComparator implements Comparator>, Serializable + { + private static final ClassComparator INSTANCE = new ClassComparator(); + + public int compare( Class class1, Class class2 ) + { + return class1.getName().compareTo( class2.getName() ); + } + } +} diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/ComponentStack.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/ComponentStack.java new file mode 100644 index 000000000..1a8972802 --- /dev/null +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/ComponentStack.java @@ -0,0 +1,76 @@ +package org.codehaus.plexus.component; + +import org.codehaus.plexus.component.repository.ComponentDescriptor; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public final class ComponentStack +{ + private static final ThreadLocal> STACK = + new ThreadLocal>() + { + protected LinkedList initialValue() + { + return new LinkedList(); + } + }; + + public static List getComponentStack() + { + LinkedList stack = STACK.get(); + ArrayList copy = new ArrayList( stack.size() ); + for ( ComponentStackElement element : stack ) + { + copy.add( new ComponentStackElement( element ) ); + } + return Collections.unmodifiableList( copy ); + } + + public static void pushComponentStack( ComponentDescriptor descriptor ) throws ComponentLookupException + { + if ( descriptor == null ) + { + throw new IllegalArgumentException( "descriptor is null" ); + } + + LinkedList stack = STACK.get(); + for ( ComponentStackElement element : stack ) + { + if ( descriptor.equals( element.getDescriptor() ) ) + { + // HACK: add descriptor to stack before creating exception so it is in the stack + stack.addFirst( new ComponentStackElement( descriptor ) ); + ComponentLookupException exception = new ComponentLookupException( "Creation circularity", descriptor ); + stack.removeFirst(); + + throw exception; + } + } + + stack.addFirst( new ComponentStackElement( descriptor ) ); + } + + public static void popComponentStack() + { + STACK.get().removeFirst(); + } + + public static void setComponentStackProperty( String property ) + { + LinkedList stackElementLinkedList = STACK.get(); + if (stackElementLinkedList.isEmpty()) { + return; + } + + ComponentStackElement element = stackElementLinkedList.getLast(); + element.setProperty( property ); + } + + private ComponentStack() + { + } +} diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/ComponentStackElement.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/ComponentStackElement.java new file mode 100644 index 000000000..49cbbf871 --- /dev/null +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/ComponentStackElement.java @@ -0,0 +1,68 @@ +package org.codehaus.plexus.component; + +import org.codehaus.plexus.component.repository.ComponentDescriptor; + +public class ComponentStackElement +{ + private ComponentDescriptor descriptor; + private String property; + + public ComponentStackElement( ComponentDescriptor descriptor ) + { + this.descriptor = descriptor; + } + + public ComponentStackElement( ComponentStackElement element ) + { + descriptor = element.descriptor; + property = element.property; + } + + public ComponentDescriptor getDescriptor() + { + return descriptor; + } + + public String getProperty() + { + return property; + } + + public void setProperty( String property ) + { + this.property = property; + } + + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( !( o instanceof ComponentStackElement ) ) + { + return false; + } + + ComponentStackElement that = (ComponentStackElement) o; + + return descriptor.equals( that.descriptor ) && + ( property != null ? property.equals( that.property ) : that.property == null ); + + } + + public int hashCode() + { + int result; + result = descriptor.hashCode(); + result = 31 * result + ( property != null ? property.hashCode() : 0 ); + return result; + } + + public String toString() + { + return descriptor.getImplementationClass().getName() + + ( property != null ? "." + property : "" ) + + ( descriptor.getSource() != null ? "(" + descriptor.getSource() + ")" : "(Unknown Source)" ); + } +} diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/builder/XBeanComponentBuilder.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/builder/XBeanComponentBuilder.java index cff990558..d4c66fe17 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/builder/XBeanComponentBuilder.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/builder/XBeanComponentBuilder.java @@ -25,11 +25,13 @@ import org.codehaus.plexus.MutablePlexusContainer; import org.codehaus.plexus.PlexusConstants; import org.codehaus.plexus.PlexusContainer; -import org.codehaus.plexus.ComponentRegistry; +import static org.codehaus.plexus.PlexusConstants.PLEXUS_DEFAULT_HINT; import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.codehaus.plexus.component.MapOrientedComponent; -import org.codehaus.plexus.component.collections.ComponentList; -import org.codehaus.plexus.component.collections.ComponentMap; +import static org.codehaus.plexus.component.ComponentStack.setComponentStackProperty; +import static org.codehaus.plexus.component.CastUtils.isAssignableFrom; +import org.codehaus.plexus.component.collections.LiveMap; +import org.codehaus.plexus.component.collections.LiveList; import org.codehaus.plexus.component.configurator.BasicComponentConfigurator; import org.codehaus.plexus.component.configurator.ComponentConfigurationException; import org.codehaus.plexus.component.configurator.ComponentConfigurator; @@ -41,6 +43,7 @@ import org.codehaus.plexus.component.configurator.expression.DefaultExpressionEvaluator; import org.codehaus.plexus.component.factory.ComponentFactory; import org.codehaus.plexus.component.factory.ComponentInstantiationException; +import org.codehaus.plexus.component.factory.UndefinedComponentFactoryException; import org.codehaus.plexus.component.factory.java.JavaComponentFactory; import org.codehaus.plexus.component.manager.ComponentManager; import org.codehaus.plexus.component.repository.ComponentDescriptor; @@ -61,18 +64,8 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.LinkedHashSet; public class XBeanComponentBuilder implements ComponentBuilder { - private static final ThreadLocal>> STACK = - new ThreadLocal>>() - { - protected LinkedHashSet> initialValue() - { - return new LinkedHashSet>(); - } - }; - private ComponentManager componentManager; public XBeanComponentBuilder() { @@ -94,53 +87,27 @@ protected MutablePlexusContainer getContainer() { return componentManager.getContainer(); } - public T build( ComponentDescriptor descriptor, ClassRealm realm, ComponentBuildListener listener ) - throws ComponentInstantiationException, ComponentLifecycleException - { - LinkedHashSet> stack = STACK.get(); - if ( stack.contains( descriptor ) ) - { - // create list of circularity - List> circularity = new ArrayList>( stack ); - circularity.subList( circularity.indexOf( descriptor ), circularity.size() ); - circularity.add( descriptor ); - - // nice circularity message - String message = "Creation circularity: "; - for ( ComponentDescriptor componentDescriptor : circularity ) - { - message += "\n\t[" + componentDescriptor.getRole() + ", " + componentDescriptor.getRoleHint() + "]"; - } - throw new ComponentInstantiationException( message ); + public T build(ComponentDescriptor descriptor, ClassRealm realm, ComponentBuildListener listener) throws ComponentInstantiationException, ComponentLifecycleException { + if (listener != null) { + listener.beforeComponentCreate(descriptor, realm); } - stack.add( descriptor ); - try - { - if (listener != null) { - listener.beforeComponentCreate(descriptor, realm); - } - - T component = createComponentInstance(descriptor, realm); - if (listener != null) { - listener.componentCreated(descriptor, component, realm); - } + T component = createComponentInstance(descriptor, realm); - startComponentLifecycle(component, realm); + if (listener != null) { + listener.componentCreated(descriptor, component, realm); + } - if (listener != null) { - listener.componentConfigured(descriptor, component, realm); - } + startComponentLifecycle(component, realm); - return component; - } - finally - { - stack.remove( descriptor ); + if (listener != null) { + listener.componentConfigured(descriptor, component, realm); } + + return component; } - protected T createComponentInstance(ComponentDescriptor descriptor, ClassRealm realm) throws ComponentInstantiationException, ComponentLifecycleException { + protected T createComponentInstance(ComponentDescriptor descriptor, ClassRealm realm) throws ComponentInstantiationException { MutablePlexusContainer container = getContainer(); if (realm == null) { realm = descriptor.getRealm(); @@ -170,14 +137,39 @@ protected T createComponentInstance(ComponentDescriptor descriptor, ClassReal } return instance; - } catch (Exception e) { - throw new ComponentLifecycleException("Error constructing component " + descriptor.getHumanReadableKey(), e); - } finally { - Thread.currentThread().setContextClassLoader(oldClassLoader); + } + catch ( ConstructionException e ) + { + Throwable cause = unwrapConstructionException( e ); + + // do not rewrap + if (cause instanceof ComponentInstantiationException) + { + throw (ComponentInstantiationException) cause; + } + + // reuse original exception message.. the ones from XBean contain a lot of information + if ( cause != null ) + { + // wrap real cause if we got one + throw new ComponentInstantiationException( e.getMessage(), cause ); + } + else + { + throw new ComponentInstantiationException( e.getMessage() ); + } + } + catch ( UndefinedComponentFactoryException e ) + { + throw new ComponentInstantiationException( e ); + } + finally + { + Thread.currentThread().setContextClassLoader( oldClassLoader ); } } - public ObjectRecipe createObjectRecipe(ComponentDescriptor descriptor, ClassRealm realm) throws ComponentInstantiationException, PlexusConfigurationException { + public ObjectRecipe createObjectRecipe(ComponentDescriptor descriptor, ClassRealm realm) throws ComponentInstantiationException { String factoryMethod = null; String[] constructorArgNames = null; Class[] constructorArgTypes = null; @@ -246,6 +238,7 @@ protected boolean shouldConfigure( ComponentDescriptor descriptor ) { return true; } + protected String fromXML(String elementName) { return StringUtils.lowercaseFirstLetter(StringUtils.removeAndHump(elementName, "-")); } @@ -253,8 +246,15 @@ protected String fromXML(String elementName) { protected void startComponentLifecycle(Object component, ClassRealm realm) throws ComponentLifecycleException { try { componentManager.start(component); - } catch (PhaseExecutionException e) { - throw new ComponentLifecycleException("Error starting component", e); + } catch (Exception e) { + Throwable cause = e; + // if we got a PhaseExecutionException, unwrap it + if ( e instanceof PhaseExecutionException && e.getCause() != null ) + { + cause = e.getCause(); + } + + throw new ComponentLifecycleException("Error invoking start method", cause); } } @@ -286,9 +286,17 @@ public boolean canCreate(Type expectedType) { // if the type to be created is an instance of the expected type, return true try { - ComponentRegistry componentRegistry = container.getComponentRegistry(); - return componentRegistry.getComponentDescriptor(propertyType, requirement.getRole(), requirement.getRoleHint()) != null; + String roleHint = requirement.getRoleHint(); + Class roleType = getInterfaceClass( container, requirement.getRole(), roleHint ); + + for ( ComponentDescriptor descriptor : container.getComponentDescriptorList( roleType ) ) + { + if ( descriptor.getRoleHint().equals( roleHint ) && isAssignableFrom( propertyType, descriptor.getImplementationClass() ) ) + { + return true; + } + } } catch (Exception e) { } @@ -296,90 +304,126 @@ public boolean canCreate(Type expectedType) { } @Override - protected Object internalCreate(Type expectedType, boolean lazyRefAllowed) throws ConstructionException { + protected Object internalCreate(Type expectedType, boolean lazyRefAllowed) throws ConstructionException + { Class propertyType = toClass(expectedType); - try { - String role = requirement.getRole(); + // push requirement property name on the stack + setComponentStackProperty( requirement.getFieldName() ); + try + { + Class roleType = getInterfaceClass( container, requirement.getRole(), requirement.getRoleHint() ); List roleHints = null; - if (requirement instanceof ComponentRequirementList) { - roleHints = ((ComponentRequirementList) requirement).getRoleHints(); + if ( requirement instanceof ComponentRequirementList ) + { + roleHints = ( (ComponentRequirementList) requirement ).getRoleHints(); } Object assignment; - if (propertyType.isArray()) { - assignment = new ArrayList(container.lookupList(role, roleHints)); + if ( propertyType.isArray() ) + { + assignment = new ArrayList( container.lookupList( roleType, roleHints ) ); } // Map.class.isAssignableFrom( clazz ) doesn't make sense, since Map.class doesn't really // have a meaningful superclass. - else { - if (Map.class.equals(propertyType)) { + else + { + if ( Map.class.equals( propertyType ) ) + { // todo this is a lazy map // get component type Type keyType = Object.class; Type valueType = Object.class; - Type[] typeParameters = RecipeHelper.getTypeParameters(Collection.class, expectedType); - if (typeParameters != null && typeParameters.length == 2) { - if (typeParameters[0] instanceof Class) { + Type[] typeParameters = RecipeHelper.getTypeParameters( Collection.class, expectedType ); + if ( typeParameters != null && typeParameters.length == 2 ) + { + if ( typeParameters[0] instanceof Class ) + { keyType = typeParameters[0]; } - if (typeParameters[1] instanceof Class) { + if ( typeParameters[1] instanceof Class ) + { valueType = typeParameters[1]; } } + // if no generic type, load the roll as a class + Class valueClass = toClass( valueType ); + if ( valueClass.equals( Object.class ) ) + { + valueClass = roleType; + } + // todo verify key type is String - assignment = new ComponentMap(container, - toClass(valueType), - role, - roleHints, - componentDescriptor.getHumanReadableKey()); + assignment = new LiveMap( container, + roleType, + roleHints, + componentDescriptor.getHumanReadableKey() ); } // List.class.isAssignableFrom( clazz ) doesn't make sense, since List.class doesn't really // have a meaningful superclass other than Collection.class, which we'll handle next. - else if (List.class.equals(propertyType)) { - // todo this is a lazy list - + else if ( List.class.equals( propertyType ) ) + { // get component type - Type[] typeParameters = RecipeHelper.getTypeParameters(Collection.class, expectedType); + Type[] typeParameters = RecipeHelper.getTypeParameters( Collection.class, expectedType ); Type componentType = Object.class; - if (typeParameters != null && typeParameters.length == 1 && typeParameters[0] instanceof Class) { + if ( typeParameters != null && typeParameters.length == 1 && typeParameters[0] instanceof Class ) + { componentType = typeParameters[0]; } - assignment = new ComponentList(container, - toClass( componentType ), - role, - roleHints, - componentDescriptor.getHumanReadableKey()); + // if no generic type, load the roll as a class + Class componentClass = toClass( componentType ); + if ( componentClass.equals( Object.class ) ) + { + componentClass = roleType; + } + + assignment = new LiveList( container, + roleType, + roleHints, + componentDescriptor.getHumanReadableKey() ); } // Set.class.isAssignableFrom( clazz ) doesn't make sense, since Set.class doesn't really // have a meaningful superclass other than Collection.class, and that would make this // if-else cascade unpredictable (both List and Set extend Collection, so we'll put another // check in for Collection.class. - else if (Set.class.equals(propertyType) || Collection.class.isAssignableFrom(propertyType)) { + else if ( Set.class.equals( propertyType ) || Collection.class.isAssignableFrom( propertyType ) ) + { // todo why isn't this lazy as above? - assignment = container.lookupMap(role, roleHints); - } else if (Logger.class.equals(propertyType)) { - // todo magic reference - assignment = container.getLoggerManager().getLoggerForComponent(componentDescriptor.getRole()); - } else if (PlexusContainer.class.equals(propertyType)) { - // todo magic reference + assignment = container.lookupMap( roleType, roleHints ); + } + else if ( Logger.class.equals( propertyType ) ) + { + // todo magic reference types should not be handled here + assignment = container.getLoggerManager().getLoggerForComponent( + componentDescriptor.getRole() ); + } + else if ( PlexusContainer.class.equals( propertyType ) ) + { + // todo magic reference types should not be handled here assignment = container; - } else { + } + else + { String roleHint = requirement.getRoleHint(); - assignment = container.lookup(propertyType, role, roleHint); + assignment = container.lookup( roleType, roleHint ); } } return assignment; - } catch (ComponentLookupException e) { - throw new ConstructionException("Composition failed of field " + requirement.getFieldName() + " " - + "in object of type " + componentDescriptor.getImplementation() + " because the requirement " - + requirement + " was missing)", e); + } + catch ( ComponentLookupException e ) + { + // simply wrap exception, so it can be unwrapped and rethrown + throw new ConstructionException( e ); + } + finally + { + setComponentStackProperty( null ); } } @@ -389,50 +433,74 @@ public String toString() { } } - private class PlexusConfigurationRecipe extends AbstractRecipe { + private class PlexusConfigurationRecipe extends AbstractRecipe + { private final PlexusConfiguration child; - public PlexusConfigurationRecipe(PlexusConfiguration child) { + public PlexusConfigurationRecipe( PlexusConfiguration child ) + { this.child = child; } - public boolean canCreate(Type type) { - try { + public boolean canCreate( Type type ) + { + try + { ConverterLookup lookup = createConverterLookup(); - lookup.lookupConverterForType(toClass(type)); + lookup.lookupConverterForType( toClass( type ) ); return true; - } catch (ComponentConfigurationException e) { + } + catch ( ComponentConfigurationException e ) + { return false; } } @Override - protected Object internalCreate(Type expectedType, boolean lazyRefAllowed) throws ConstructionException { - try { + protected Object internalCreate( Type expectedType, boolean lazyRefAllowed ) throws ConstructionException + { + setComponentStackProperty( child.getName() ); + try + { ConverterLookup lookup = createConverterLookup(); - ConfigurationConverter converter = lookup.lookupConverterForType(toClass(expectedType)); + ConfigurationConverter converter = lookup.lookupConverterForType( toClass( expectedType ) ); // todo this will not work for static factories ObjectRecipe caller = (ObjectRecipe) RecipeHelper.getCaller(); - Class parentClass = toClass(caller.getType()); + Class parentClass = toClass( caller.getType() ); + + Object value = converter.fromConfiguration( + lookup, + child, + toClass( expectedType ), + parentClass, + Thread.currentThread().getContextClassLoader(), + new DefaultExpressionEvaluator() ); - Object value = converter.fromConfiguration(lookup, child, toClass(expectedType), parentClass, Thread.currentThread().getContextClassLoader(), new DefaultExpressionEvaluator()); return value; - } catch (ComponentConfigurationException e) { - throw new ConstructionException("Unable to convert configuration for property " + child.getName() + " to " + toClass(expectedType).getName()); + } + catch ( ComponentConfigurationException e ) + { + // simply wrap exception, so it can be unwrapped and rethrown + throw new ConstructionException( e ); + } + finally + { + setComponentStackProperty( null ); } } - private ConverterLookup createConverterLookup() { + private ConverterLookup createConverterLookup() + { ClassRealm realm = (ClassRealm) Thread.currentThread().getContextClassLoader(); ConverterLookup lookup = new DefaultConverterLookup(); - lookup.registerConverter( new ClassRealmConverter(realm) ); + lookup.registerConverter( new ClassRealmConverter( realm ) ); return lookup; } } - private void processMapOrientedComponent(ComponentDescriptor descriptor, MapOrientedComponent mapOrientedComponent, ClassRealm realm) throws ComponentConfigurationException, ComponentLookupException { + private void processMapOrientedComponent(ComponentDescriptor descriptor, MapOrientedComponent mapOrientedComponent, ClassRealm realm) throws ComponentInstantiationException { MutablePlexusContainer container = getContainer(); for (ComponentRequirement requirement : descriptor.getRequirements()) { @@ -440,23 +508,34 @@ private void processMapOrientedComponent(ComponentDescriptor descriptor, MapO String hint = requirement.getRoleHint(); String mappingType = requirement.getFieldMappingType(); - Object value; - - // if the hint is not empty (and not default), we don't care about mapping type... - // it's a single-value, not a collection. - if (StringUtils.isNotEmpty(hint) && !hint.equals(PlexusConstants.PLEXUS_DEFAULT_HINT)) { - value = container.lookup(role, hint); - } else if ("single".equals(mappingType)) { - value = container.lookup(role, hint); - } else if ("map".equals(mappingType)) { - value = container.lookupMap(role); - } else if ("set".equals(mappingType)) { - value = new HashSet(container.lookupList(role)); - } else { - value = container.lookup(role, hint); - } - mapOrientedComponent.addComponentRequirement(requirement, value); + try + { + // if the hint is not empty (and not default), we don't care about mapping type... + // it's a single-value, not a collection. + Object value; + if (StringUtils.isNotEmpty(hint) && !hint.equals(PlexusConstants.PLEXUS_DEFAULT_HINT)) { + value = container.lookup(role, hint); + } else if ("single".equals(mappingType)) { + value = container.lookup(role, hint); + } else if ("map".equals(mappingType)) { + value = container.lookupMap(role); + } else if ("set".equals(mappingType)) { + value = new HashSet(container.lookupList(role)); + } else { + value = container.lookup(role, hint); + } + + mapOrientedComponent.addComponentRequirement(requirement, value); + } + catch ( ComponentLookupException e ) + { + throw new ComponentInstantiationException( "Error looking up requirement of MapOrientedComponent ", e ); + } + catch ( ComponentConfigurationException e ) + { + throw new ComponentInstantiationException( "Error adding requirement to MapOrientedComponent ", e ); + } } MapConverter converter = new MapConverter(); @@ -466,15 +545,83 @@ private void processMapOrientedComponent(ComponentDescriptor descriptor, MapO if ( configuration != null ) { - Map context = (Map) converter.fromConfiguration(converterLookup, - configuration, - null, - null, - realm, - expressionEvaluator, - null ); - - mapOrientedComponent.setComponentConfiguration( context ); + try + { + Map context = (Map) converter.fromConfiguration(converterLookup, + configuration, + null, + null, + realm, + expressionEvaluator, + null ); + + mapOrientedComponent.setComponentConfiguration( context ); + } + catch ( ComponentConfigurationException e ) + { + throw new ComponentInstantiationException( "Error adding configuration to MapOrientedComponent ", e ); + } + } + } + + private static Class getInterfaceClass( PlexusContainer container, String role, String hint ) + { + if ( hint == null ) hint = PLEXUS_DEFAULT_HINT; + + try + { + ClassRealm realm = container.getLookupRealm(); + + if ( realm != null ) + { + return realm.loadClass( role ); + } + } + catch ( Throwable e ) + { + } + + try + { + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + if ( loader != null ) + { + return loader.loadClass( role ); + } + } + catch ( Throwable e ) + { + } + + try + { + ComponentDescriptor cd = container.getComponentDescriptor( role, hint ); + if ( cd != null ) + { + ClassLoader loader = cd.getImplementationClass().getClassLoader(); + if ( loader != null ) + { + return loader.loadClass( role ); + } + } + } + catch ( Throwable ignored ) + { + } + + return Object.class; + } + + /** + * There are a few bugs in XBean reflect where a constuction exception is wrapped with another constuction exception. + */ + private static Throwable unwrapConstructionException( ConstructionException e ) + { + Throwable cause = e; + while ( cause instanceof ConstructionException && e.getCause() != null) + { + cause = cause.getCause(); } + return cause; } } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/AbstractComponentCollection.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/AbstractComponentCollection.java deleted file mode 100644 index 79f378f0d..000000000 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/AbstractComponentCollection.java +++ /dev/null @@ -1,164 +0,0 @@ -package org.codehaus.plexus.component.collections; - -import org.codehaus.plexus.MutablePlexusContainer; -import org.codehaus.plexus.classworlds.realm.ClassRealm; -import org.codehaus.plexus.classworlds.ClassWorld; -import org.codehaus.plexus.component.repository.ComponentDescriptor; -import org.codehaus.plexus.component.repository.exception.ComponentLookupException; -import org.codehaus.plexus.logging.Logger; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/* - * Copyright 2001-2006 Codehaus Foundation. - * - * Licensed 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. - */ - -/** @author Jason van Zyl */ - -// We need to have the collection notified when a new implementation of a given role has -// been added to the container. We probably need some options so that we know when new -// component descriptors have been added to the system, and an option to keep the collection -// up-to-date when new implementations are added. -// -// NOTE: This includes component additions, but also component purges from the -// container, as when a component realm is disposed -// (and PlexusContainer.removeComponentRealm(..) is called). -public abstract class AbstractComponentCollection -{ - /** The reference to the PlexusContainer */ - protected MutablePlexusContainer container; - - /** The type of the components held by this collection*/ - protected final Class componentType; - - /** The role of the components we are holding in this Collection. */ - protected String role; - - /** The role hint of the components we are holding in this Collection. */ - protected List roleHints; - - /** The component that requires this collection of components */ - protected String hostComponent; - - /** Used to log errors in the component lookup process. */ - protected Logger logger; - - private List realms; - - private int lastRealmCount = -1; - - private Map> componentDescriptorMap; - private ClassWorld world; - - public AbstractComponentCollection( MutablePlexusContainer container, - Class componentType, - String role, - List roleHints, - String hostComponent ) - { - this.container = container; - - this.componentType = componentType; - - this.role = role; - - this.roleHints = roleHints; - - this.hostComponent = hostComponent; - - logger = container.getLoggerManager().getLoggerForComponent( role ); - - world = container.getContainerRealm().getWorld(); - } - - private boolean realmsHaveChanged() - { - return ( realms == null ) || ( world.getRealms().size() != lastRealmCount ); - } - - protected Map> getComponentDescriptorMap() - { - checkUpdate(); - - return componentDescriptorMap; - } - - protected boolean checkUpdate() - { - if ( componentDescriptorMap != null && !realmsHaveChanged() ) - { - return false; - } - - Map> newComponentDescriptors = new HashMap>(); - Map> componentMap = container.getComponentDescriptorMap( componentType, role ); - - if ( roleHints != null && !roleHints.isEmpty() ) - { - for ( String roleHint : roleHints ) - { - ComponentDescriptor componentDescriptor = componentMap.get( roleHint ); - if ( componentDescriptor != null ) - { - newComponentDescriptors.put( roleHint, componentDescriptor ); - } - } - } - else - { - newComponentDescriptors.putAll( componentMap ); - } - - if ( componentDescriptorMap == null || newComponentDescriptors.size() != componentDescriptorMap.size() ) - { - componentDescriptorMap = newComponentDescriptors; - - return true; - } - - return false; - } - - protected T lookup( String role, String roleHint ) - { - try - { - return container.lookup( componentType, role, roleHint ); - } - catch ( ComponentLookupException e ) - { - logger.debug( "Failed to lookup a member of active collection with role: " + role - + " and role-hint: " + roleHint, e ); - - return null; - } - } - - public void clear() - { - releaseAllCallback(); - - componentDescriptorMap.clear(); - componentDescriptorMap = null; - - realms = null; - lastRealmCount = -1; - } - - protected abstract void releaseAllCallback(); - -} diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/ComponentList.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/ComponentList.java deleted file mode 100644 index 5c8e62339..000000000 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/ComponentList.java +++ /dev/null @@ -1,253 +0,0 @@ -package org.codehaus.plexus.component.collections; - -/* - * Copyright 2001-2006 Codehaus Foundation. - * - * Licensed 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. - */ - -import org.codehaus.plexus.MutablePlexusContainer; -import org.codehaus.plexus.component.repository.ComponentDescriptor; -import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.ListIterator; -import java.util.Map; -import java.util.Map.Entry; - -/** - * @author Jason van Zyl FIXME: [jdcasey] We need to review the efficiency (in speed and memory) of this collection... - */ -public class ComponentList - extends AbstractComponentCollection - implements List -{ - private List components; - - public ComponentList( MutablePlexusContainer container, Class type, String role, List roleHints, String hostComponent ) - { - super( container, type, role, roleHints, hostComponent ); - } - - public int size() - { - return getComponentDescriptorMap().size(); - } - - public boolean isEmpty() - { - return getComponentDescriptorMap().isEmpty(); - } - - public boolean contains( Object object ) - { - return getList().contains( object ); - } - - public Iterator iterator() - { - return getList().iterator(); - } - - public Object[] toArray() - { - return getList().toArray(); - } - - public Object[] toArray( Object[] ts ) - { - return getList().toArray( ts ); - } - - public boolean add( T object ) - { - if ( components == null ) - { - components = new ArrayList(); - } - - components.add( object ); - - /* - * PLX-352 This is strictly to support the hack in the Ant Run plugin that tries to poke in a custom converter. - * We need a better way to register converters to plexus and not hit the default converter lookup directly. - * throw new UnsupportedOperationException( "You cannot modify this list. This list is a requirement of " + - * hostComponent + " and managed by the container." ); - */ - - return true; - } - - public boolean remove( Object object ) - { - throw new UnsupportedOperationException( "You cannot modify this list. This list is a requirement of " - + hostComponent + " and managed by the container." ); - } - - public boolean containsAll( Collection collection ) - { - return getList().containsAll( collection ); - } - - public boolean addAll( Collection collection ) - { - throw new UnsupportedOperationException( "You cannot modify this list. This list is a requirement of " - + hostComponent + " and managed by the container." ); - } - - public boolean addAll( int i, Collection collection ) - { - throw new UnsupportedOperationException( "You cannot modify this list. This list is a requirement of " - + hostComponent + " and managed by the container." ); - } - - public boolean removeAll( Collection collection ) - { - return getList().removeAll( collection ); - } - - public boolean retainAll( Collection collection ) - { - return getList().retainAll( collection ); - } - - public boolean equals( Object o ) - { - if ( this == o ) - { - return true; - } - if ( !( o instanceof List ) ) - { - return false; - } - - List other = (List) o; - return getList().equals( other ); - } - - public int hashCode() - { - return getList().hashCode(); - } - - public T get( int i ) - { - return getList().get( i ); - } - - public T set( int i, T object ) - { - throw new UnsupportedOperationException( "You cannot modify this list. This list is a requirement of " - + hostComponent + " and managed by the container." ); - } - - public void add( int i, T object ) - { - throw new UnsupportedOperationException( "You cannot modify this list. This list is a requirement of " - + hostComponent + " and managed by the container." ); - } - - public T remove( int i ) - { - throw new UnsupportedOperationException( "You cannot modify this list. This list is a requirement of " - + hostComponent + " and managed by the container." ); - } - - public int indexOf( Object object ) - { - return getList().indexOf( object ); - } - - public int lastIndexOf( Object object ) - { - return getList().lastIndexOf( object ); - } - - public ListIterator listIterator() - { - return getList().listIterator(); - } - - public ListIterator listIterator( int index ) - { - return getList().listIterator( index ); - } - - public List subList( int fromIndex, int toIndex ) - { - return getList().subList( fromIndex, toIndex ); - } - - private List getList() - { - // NOTE: If we cache the component map, we have a problem with releasing any of the - // components in this map...we need to be able to release them all. - if ( ( components == null ) || checkUpdate() ) - { - List componentList = new ArrayList(); - - Map> descriptorMap = getComponentDescriptorMap(); - - if ( roleHints != null ) - { - // we must follow the order in roleHints - for ( String roleHint : roleHints ) - { - T component = lookup( role, roleHint ); - if ( component != null ) - { - componentList.add( component ); - } - } - } - else - { - for ( Entry> entry : descriptorMap.entrySet() ) - { - String roleHint = entry.getKey(); - - T component = lookup( role, roleHint ); - if ( component != null ) - { - componentList.add( component ); - } - } - } - components = componentList; - } - - return components; - } - - protected void releaseAllCallback() - { - if ( components != null ) - { - try - { - container.releaseAll( components ); - } - catch ( ComponentLifecycleException e ) - { - logger.debug( "Error releasing components in active collection: " + e.getMessage(), e ); - } - - components.clear(); - components = null; - } - } -} diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/ComponentMap.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/ComponentMap.java deleted file mode 100644 index 3ab39f5d1..000000000 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/ComponentMap.java +++ /dev/null @@ -1,224 +0,0 @@ -package org.codehaus.plexus.component.collections; - -/* - * Copyright 2001-2006 Codehaus Foundation. - * - * Licensed 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. - */ - -import org.codehaus.plexus.MutablePlexusContainer; -import org.codehaus.plexus.component.repository.ComponentDescriptor; -import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; - -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * @author Jason van Zyl FIXME: [jdcasey] We need to review the efficiency (in speed and memory) of this collection... - */ -public class ComponentMap - extends AbstractComponentCollection - implements Map -{ - private Map components; - - private Map customAdditions = new LinkedHashMap(); - - public ComponentMap( MutablePlexusContainer container, Class type, String role, List roleHints, String hostComponent ) - { - super( container, type, role, roleHints, hostComponent ); - } - - public int size() - { - return getComponentDescriptorMap().size(); - } - - public boolean isEmpty() - { - return getComponentDescriptorMap().isEmpty(); - } - - public boolean containsKey( Object key ) - { - Map> descriptorMap = getComponentDescriptorMap(); - return descriptorMap.containsKey( key ); - } - - public boolean containsValue( Object value ) - { - return getMap().containsValue( value ); - } - - public T get( Object k ) - { - Map> descriptorMap = getComponentDescriptorMap(); - if ( k instanceof String ) - { - String key = (String) k; - if ( descriptorMap.containsKey( key ) ) - { - return lookup( role, key ); - } - } - - return null; - } - - public T put( String key, T value ) - { - logger.warn( "Custom " - + role - + " implementations should NOT be added directly to this Map. Instead, add them as Plexus components." ); - - T prev = customAdditions.put( key, value ); - if ( prev == null ) - { - prev = getComponentMap().get( key ); - } - - return prev; - } - - public void putAll( Map map ) - { - logger.warn( "Custom " - + role - + " implementations should NOT be added directly to this Map. Instead, add them as Plexus components." ); - - customAdditions.putAll( map ); - } - - public Set keySet() - { - return getMap().keySet(); - } - - public Collection values() - { - return getMap().values(); - } - - public Set> entrySet() - { - return getMap().entrySet(); - } - - public boolean equals( Object o ) - { - if ( this == o ) - { - return true; - } - if ( !( o instanceof Map ) ) - { - return false; - } - - Map object = (Map) o; - return getMap().equals( object ); - } - - public int hashCode() - { - return getMap().hashCode(); - } - - public T remove( Object k ) - { - logger.warn( "Items in this Map should NOT be removed directly. If the matching entry is a component, it will NOT be removed." ); - - if ( k instanceof String ) - { - String key = (String) k; - if ( customAdditions.containsKey( key ) ) - { - return customAdditions.remove( key ); - } - } - - return null; - } - - private Map getMap() - { - Map result = getComponentMap(); - - if ( !customAdditions.isEmpty() ) - { - result.putAll( customAdditions ); - } - - return result; - } - - private Map getComponentMap() - { - if ( ( components == null ) || checkUpdate() ) - { - components = new LinkedHashMap(); - - Map> descriptorMap = getComponentDescriptorMap(); - - if ( roleHints != null ) - { - // we must follow the order given in roleHints - for ( String roleHint : roleHints ) - { - T component = lookup( role, roleHint ); - if ( component != null ) - { - components.put( roleHint, component ); - } - } - } - else - { - for ( Entry> entry : descriptorMap.entrySet() ) - { - String roleHint = entry.getKey(); - - T component = lookup( role, roleHint ); - if ( component != null ) - { - components.put( roleHint, component ); - } - } - } - } - - return components; - } - - protected void releaseAllCallback() - { - if ( components != null ) - { - try - { - container.releaseAll( components ); - } - catch ( ComponentLifecycleException e ) - { - logger.debug( "Error releasing components in active collection: " + e.getMessage(), e ); - } - - components.clear(); - components = null; - } - } - -} diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/LiveList.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/LiveList.java new file mode 100644 index 000000000..adfb2b471 --- /dev/null +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/LiveList.java @@ -0,0 +1,350 @@ +package org.codehaus.plexus.component.collections; + +/* + * Copyright 2001-2006 Codehaus Foundation. + * + * Licensed 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. + */ + +import static com.google.common.base.ReferenceType.STRONG; +import static com.google.common.base.ReferenceType.WEAK; +import com.google.common.collect.ReferenceMap; +import org.codehaus.plexus.MutablePlexusContainer; +import org.codehaus.plexus.component.repository.ComponentDescriptor; +import org.codehaus.plexus.component.repository.ComponentDescriptorListener; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; + +import java.util.ArrayList; +import java.util.Collection; +import static java.util.Collections.unmodifiableList; +import static java.util.Collections.unmodifiableSet; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.LinkedHashSet; +import java.util.Set; +import java.util.concurrent.ConcurrentMap; + +public class LiveList + implements List +{ + /** The reference to the PlexusContainer */ + private final MutablePlexusContainer container; + + /** The type of the components held by this collection */ + private final Class type; + + /** The role hint of the components we are holding in this Collection. */ + private final Set roleHints; + + /** The component that requires this collection of components */ + private final String hostComponent; + + private List components = new ArrayList(); + private List componentHints = new ArrayList(); + + private boolean hasExternalReference; + + private final ConcurrentMap, T> componentsByDescriptor = + new ReferenceMap, T>( STRONG, WEAK ); + + public LiveList( MutablePlexusContainer container, Class type, List roleHints, String hostComponent ) + { + this.container = container; + + this.type = type; + + if ( roleHints == null ) + { + this.roleHints = null; + } + else + { + this.roleHints = unmodifiableSet( new LinkedHashSet( roleHints ) ); + } + + this.hostComponent = hostComponent; + + container.addComponentDescriptorListener( new LiveListDescriptorListener() ); + } + + public String getHostComponent() + { + return hostComponent; + } + + public synchronized boolean isEmpty() + { + return components.isEmpty(); + } + + public synchronized int size() + { + return components.size(); + } + + public synchronized T get( int index ) + { + return components.get( index ); + } + + public synchronized boolean contains( Object o ) + { + return components.contains( o ); + } + + public synchronized boolean containsAll( Collection c ) + { + return components.containsAll( c ); + } + + public synchronized int indexOf( Object o ) + { + return components.indexOf( o ); + } + + public synchronized int lastIndexOf( Object o ) + { + return components.lastIndexOf( o ); + } + + public synchronized List subList( int fromIndex, int toIndex ) + { + hasExternalReference = true; + return unmodifiableList( components ).subList( fromIndex, toIndex ); + } + + public synchronized Iterator iterator() + { + hasExternalReference = true; + return unmodifiableList( components ).iterator(); + } + + public synchronized ListIterator listIterator() + { + hasExternalReference = true; + return unmodifiableList( components ).listIterator(); + } + + public synchronized ListIterator listIterator( int index ) + { + hasExternalReference = true; + return unmodifiableList( components ).listIterator( index ); + } + + public synchronized Object[] toArray() + { + return components.toArray(); + } + + public synchronized T[] toArray( T[] a ) + { + return components.toArray( a ); + } + + public synchronized boolean equals( Object o ) + { + return this == o || ( o instanceof List && components.equals( o ) ); + } + + public synchronized int hashCode() + { + return components.hashCode(); + } + + public synchronized String toString() + { + return components.toString(); + } + + // + // Unsupported Operations -- Map is immutable + // + + public T set( int index, T element ) + { + throw new UnsupportedOperationException(); + } + + public boolean add( T o ) + { + throw new UnsupportedOperationException(); + } + + public void add( int index, T element ) + { + throw new UnsupportedOperationException(); + } + + public boolean addAll( Collection c ) + { + throw new UnsupportedOperationException(); + } + + public boolean addAll( int index, Collection c ) + { + throw new UnsupportedOperationException(); + } + + public boolean remove( Object k ) + { + throw new UnsupportedOperationException(); + } + + public T remove( int index ) + { + throw new UnsupportedOperationException(); + } + + public boolean removeAll( Collection c ) + { + throw new UnsupportedOperationException(); + } + + public boolean retainAll( Collection c ) + { + throw new UnsupportedOperationException(); + } + + public void clear() + { + throw new UnsupportedOperationException(); + } + + private synchronized void componentDescriptorAdded( ComponentDescriptor descriptor ) + { + String roleHint = descriptor.getRoleHint(); + if ( !componentsByDescriptor.containsKey( descriptor ) && ( roleHints == null || roleHints.contains( roleHint ) ) ) + { + T component = null; + try + { + component = container.lookup( descriptor ); + } + catch ( ComponentLookupException ignored ) + { + // ignored - component can't be created which + } + + // if there is an external reference to the components, make a copy before modifying + if ( hasExternalReference ) + { + components = new ArrayList( components ); + hasExternalReference = false; + } + + insertSorted( component, roleHint ); + componentsByDescriptor.put( descriptor, component ); + } + } + + private synchronized void insertSorted( T targetComponent, String targetHint ) + { + // if we are not sorting, simply add the component to the end + if ( roleHints == null ) + { + components.add( targetComponent ); + return; + } + + // + // This algorithm uses iterators, hintOrder over the required sorted order of the components (specified + // during consturction of LiveList, and componentIterator over the hints of the actual components in the + // current LiveList. The componentIterator is advanced until it points to a hint after the targetHint. + // Then the targetComponent is inserted before this position in the list. + // + + Iterator hintOrder = roleHints.iterator(); + + // inspect each component role until we find one with a role after the target hint + for ( ListIterator componentIterator = componentHints.listIterator(); componentIterator.hasNext(); ) + { + String currentHint = componentIterator.next(); + + // advance hint iterator until it points to the current hint + for ( String nextHint = hintOrder.next(); !nextHint.equals( currentHint ); nextHint = hintOrder.next() ) + { + if ( nextHint.equals( targetHint ) ) + { + // the current component's hint is after our target hint, so insert here + components.add( componentIterator.previousIndex(), targetComponent ); + componentHints.add( componentIterator.previousIndex(), targetHint ); + + // break + return; + } + } + } + + // walked through whole list and didn't find a hint after our target hint, so insert at the end + components.add( targetComponent ); + componentHints.add( targetHint ); + } + + private synchronized void componentDescriptorRemoved( ComponentDescriptor descriptor ) + { + T component = componentsByDescriptor.remove( descriptor ); + if ( component != null ) + { + // if there is an external reference to the components, make a copy before modifying + if ( hasExternalReference ) + { + components = new ArrayList( components ); + hasExternalReference = false; + } + + Iterator hintIterator = componentHints.iterator(); + for ( Iterator componentIterator = components.iterator(); componentIterator.hasNext(); ) + { + T current = componentIterator.next(); + hintIterator.next(); + + if ( current == component ) + { + componentIterator.remove(); + hintIterator.remove(); + } + } + } + } + + private class LiveListDescriptorListener implements ComponentDescriptorListener + { + public Class getType() + { + return type; + } + + public List getRoleHints() + { + if ( roleHints != null ) + { + return new ArrayList( roleHints ); + } + else + { + return null; + } + } + + public void componentDescriptorAdded( ComponentDescriptor descriptor ) + { + LiveList.this.componentDescriptorAdded( descriptor ); + } + + public void componentDescriptorRemoved( ComponentDescriptor descriptor ) + { + LiveList.this.componentDescriptorRemoved( descriptor ); + } + } +} \ No newline at end of file diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/LiveMap.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/LiveMap.java new file mode 100644 index 000000000..4b674578a --- /dev/null +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/collections/LiveMap.java @@ -0,0 +1,220 @@ +package org.codehaus.plexus.component.collections; + +/* + * Copyright 2001-2006 Codehaus Foundation. + * + * Licensed 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. + */ + +import static com.google.common.base.ReferenceType.STRONG; +import static com.google.common.base.ReferenceType.WEAK; +import com.google.common.collect.ReferenceMap; +import org.codehaus.plexus.MutablePlexusContainer; +import org.codehaus.plexus.component.repository.ComponentDescriptor; +import org.codehaus.plexus.component.repository.ComponentDescriptorListener; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; + +import java.util.ArrayList; +import java.util.Collection; +import static java.util.Collections.unmodifiableList; +import static java.util.Collections.unmodifiableMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentMap; + +public class LiveMap + implements ConcurrentMap +{ + /** The reference to the PlexusContainer */ + private final MutablePlexusContainer container; + + /** The type of the components held by this collection */ + private final Class type; + + /** The role hint of the components we are holding in this Collection. */ + private final List roleHints; + + /** The component that requires this collection of components */ + private final String hostComponent; + + private final ConcurrentMap components = new ReferenceMap( STRONG, WEAK ); + private final Map immutableComponents = unmodifiableMap( components ); + private final ConcurrentMap, T> componentsByDescriptor = + new ReferenceMap, T>( STRONG, WEAK ); + + public LiveMap( MutablePlexusContainer container, Class type, List roleHints, String hostComponent ) + { + this.container = container; + + this.type = type; + + if ( roleHints == null ) + { + this.roleHints = null; + } + else + { + this.roleHints = unmodifiableList( new ArrayList( roleHints ) ); + } + + this.hostComponent = hostComponent; + + container.addComponentDescriptorListener( new LiveMapDescriptorListener() ); + } + + public String getHostComponent() + { + return hostComponent; + } + + public boolean isEmpty() + { + return immutableComponents.isEmpty(); + } + + public int size() + { + return immutableComponents.size(); + } + + public boolean containsKey( Object key ) + { + return immutableComponents.containsKey( key ); + } + + public boolean containsValue( Object value ) + { + return immutableComponents.containsValue( value ); + } + + public T get( Object key ) + { + return immutableComponents.get( key ); + } + + public Set keySet() + { + return immutableComponents.keySet(); + } + + public Collection values() + { + return immutableComponents.values(); + } + + public Set> entrySet() + { + return immutableComponents.entrySet(); + } + + public boolean equals( Object o ) + { + return this == o || ( o instanceof Map && immutableComponents.equals( o ) ); + } + + public int hashCode() + { + return immutableComponents.hashCode(); + } + + public String toString() + { + return immutableComponents.toString(); + } + + // + // Unsupported Operations -- Map is immutable + // + + public T put( String key, T value ) + { + throw new UnsupportedOperationException(); + } + + public T putIfAbsent( String key, T value ) + { + throw new UnsupportedOperationException(); + } + + public void putAll( Map t ) + { + throw new UnsupportedOperationException(); + } + + public T remove( Object k ) + { + throw new UnsupportedOperationException(); + } + + public boolean remove( Object key, Object value ) + { + throw new UnsupportedOperationException(); + } + + public boolean replace( String key, T oldValue, T newValue ) + { + throw new UnsupportedOperationException(); + } + + public T replace( String key, T value ) + { + throw new UnsupportedOperationException(); + } + + public void clear() + { + throw new UnsupportedOperationException(); + } + + private class LiveMapDescriptorListener implements ComponentDescriptorListener + { + public Class getType() + { + return type; + } + + public List getRoleHints() + { + return roleHints; + } + + public synchronized void componentDescriptorAdded( ComponentDescriptor descriptor ) + { + String roleHint = descriptor.getRoleHint(); + if ( !components.containsKey( roleHint ) && ( roleHints == null || roleHints.contains( roleHint ) ) ) + { + T component = null; + try + { + component = container.lookup( descriptor ); + } + catch ( ComponentLookupException ignored ) + { + // ignored - component can't be created which + } + components.put( roleHint, component ); + componentsByDescriptor.put( descriptor, component ); + } + } + + public synchronized void componentDescriptorRemoved( ComponentDescriptor descriptor ) + { + T component = componentsByDescriptor.remove( descriptor ); + if ( component != null ) + { + components.remove( descriptor.getRoleHint(), component ); + } + } + } +} \ No newline at end of file diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractComponentDiscoverer.java index a74d1dc15..6354bfd79 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractComponentDiscoverer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractComponentDiscoverer.java @@ -52,7 +52,7 @@ public abstract class AbstractComponentDiscoverer protected abstract String getComponentDescriptorLocation(); protected abstract ComponentSetDescriptor createComponentDescriptors( Reader reader, - String source ) + String source, ClassRealm realm ) throws PlexusConfigurationException; // ---------------------------------------------------------------------- @@ -107,16 +107,7 @@ public List findComponents( Context context, ClassRealm new InterpolationFilterReader( reader, new ContextMapAdapter( context ) ); ComponentSetDescriptor componentSetDescriptor = - createComponentDescriptors( interpolationFilterReader, url.toString() ); - - if ( componentSetDescriptor.getComponents() != null ) - { - for ( ComponentDescriptor cd : componentSetDescriptor.getComponents() ) - { - cd.setComponentSetDescriptor( componentSetDescriptor ); - cd.setRealm( realm ); - } - } + createComponentDescriptors( interpolationFilterReader, url.toString(), realm ); componentSetDescriptors.add( componentSetDescriptor ); diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscoverer.java index 0cd01506d..49d280aae 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscoverer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscoverer.java @@ -21,6 +21,7 @@ import org.codehaus.plexus.component.repository.io.PlexusTools; import org.codehaus.plexus.configuration.PlexusConfiguration; import org.codehaus.plexus.configuration.PlexusConfigurationException; +import org.codehaus.plexus.classworlds.realm.ClassRealm; import java.io.Reader; import java.util.ArrayList; @@ -38,7 +39,7 @@ public String getComponentDescriptorLocation() return "META-INF/plexus/components.xml"; } - public ComponentSetDescriptor createComponentDescriptors( Reader componentDescriptorReader, String source ) + public ComponentSetDescriptor createComponentDescriptors( Reader componentDescriptorReader, String source, ClassRealm realm ) throws PlexusConfigurationException { PlexusConfiguration componentDescriptorConfiguration = PlexusTools.buildConfiguration( source, componentDescriptorReader ); @@ -55,7 +56,7 @@ public ComponentSetDescriptor createComponentDescriptors( Reader componentDescri ComponentDescriptor componentDescriptor; try { - componentDescriptor = PlexusTools.buildComponentDescriptor( componentConfiguration ); + componentDescriptor = PlexusTools.buildComponentDescriptor( componentConfiguration, realm ); } catch ( PlexusConfigurationException e ) { @@ -66,6 +67,8 @@ public ComponentSetDescriptor createComponentDescriptors( Reader componentDescri componentDescriptor.setComponentType( "plexus" ); + componentDescriptor.setComponentSetDescriptor( componentSetDescriptor ); + componentDescriptors.add( componentDescriptor ); } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/PlexusXmlComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/PlexusXmlComponentDiscoverer.java index d5f527793..2e0c63a01 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/PlexusXmlComponentDiscoverer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/PlexusXmlComponentDiscoverer.java @@ -141,7 +141,8 @@ private ComponentSetDescriptor createComponentDescriptors( PlexusConfiguration c ComponentDescriptor componentDescriptor; try { - componentDescriptor = PlexusTools.buildComponentDescriptor( componentConfiguration ); + componentDescriptor = PlexusTools.buildComponentDescriptor( componentConfiguration, realm ); + if (componentDescriptor == null) continue; } catch ( PlexusConfigurationException e ) { @@ -152,8 +153,6 @@ private ComponentSetDescriptor createComponentDescriptors( PlexusConfiguration c componentDescriptor.setComponentType( "plexus" ); - componentDescriptor.setRealm( realm ); - componentDescriptor.setComponentSetDescriptor( componentSetDescriptor ); componentSetDescriptor.addComponentDescriptor( componentDescriptor ); diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/AbstractComponentManager.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/AbstractComponentManager.java index d117e3dc7..db64d341f 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/AbstractComponentManager.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/AbstractComponentManager.java @@ -17,60 +17,29 @@ */ import org.codehaus.plexus.MutablePlexusContainer; -import org.codehaus.plexus.classworlds.realm.ClassRealm; -import org.codehaus.plexus.component.builder.AbstractComponentBuildListener; import org.codehaus.plexus.component.builder.ComponentBuilder; import org.codehaus.plexus.component.builder.XBeanComponentBuilder; import org.codehaus.plexus.component.factory.ComponentInstantiationException; import org.codehaus.plexus.component.repository.ComponentDescriptor; import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; import org.codehaus.plexus.lifecycle.LifecycleHandler; -import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.personality.plexus.lifecycle.phase.PhaseExecutionException; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Map.Entry; - -public abstract class AbstractComponentManager - implements ComponentManager +public abstract class AbstractComponentManager implements ComponentManager { - protected final MutablePlexusContainer container; - - private final ClassRealm realm; - - protected final ComponentDescriptor componentDescriptor; - - private final Class type; - - private final String role; - - private final String roleHint; + private final MutablePlexusContainer container; - protected final ComponentBuilder builder = new XBeanComponentBuilder(this); + private final ComponentDescriptor componentDescriptor; + private final ComponentBuilder builder = new XBeanComponentBuilder( this ); private final LifecycleHandler lifecycleHandler; - /** - * Contains a mapping from singleton instances to the realms - * they were used to configure with. This realm will be used to - * call all lifecycle methods. - * @return a synchronized map, make sure to synchronize the map when iterating. - */ - protected final Map componentContextRealms = Collections.synchronizedMap(new HashMap()); - - private int connections; - private long startId; public AbstractComponentManager( MutablePlexusContainer container, - LifecycleHandler lifecycleHandler, - ComponentDescriptor componentDescriptor, - String role, - String roleHint) + LifecycleHandler lifecycleHandler, + ComponentDescriptor componentDescriptor ) { if ( container == null ) { @@ -89,22 +58,6 @@ public AbstractComponentManager( MutablePlexusContainer container, throw new NullPointerException( "componentDescriptor is null" ); } this.componentDescriptor = componentDescriptor; - - if ( role == null ) - { - throw new NullPointerException( "role is null" ); - } - this.role = role; - - if ( roleHint == null ) - { - throw new NullPointerException( "roleHint is null" ); - } - this.roleHint = roleHint; - - this.realm = componentDescriptor.getRealm(); - - this.type = componentDescriptor.getImplementationClass(); } public ComponentDescriptor getComponentDescriptor() @@ -112,49 +65,26 @@ public ComponentDescriptor getComponentDescriptor() return componentDescriptor; } - public Class getType() - { - return type; - } - - public ClassRealm getRealm() - { - return realm; - } - - public String getRole() - { - return role; - } - - public String getRoleHint() - { - return roleHint; - } - public LifecycleHandler getLifecycleHandler() { return lifecycleHandler; } - protected void incrementConnectionCount() - { - connections++; - } - - protected void decrementConnectionCount() - { - connections--; - } - - protected boolean connected() + protected T createInstance() throws ComponentInstantiationException, ComponentLifecycleException { - return connections > 0; + return builder.build( componentDescriptor, componentDescriptor.getRealm(), null ); } - public int getConnections() + protected void destroyInstance( Object component ) throws ComponentLifecycleException { - return connections; + try + { + lifecycleHandler.end( component, this, componentDescriptor.getRealm() ); + } + catch ( PhaseExecutionException e ) + { + throw new ComponentLifecycleException( "Error ending component lifecycle", e ); + } } // ---------------------------------------------------------------------- @@ -175,60 +105,8 @@ public long getStartId() return startId; } - protected T createComponentInstance() - throws ComponentInstantiationException, ComponentLifecycleException - { - return builder.build(componentDescriptor, realm, new AbstractComponentBuildListener() { - public void componentCreated( ComponentDescriptor componentDescriptor, Object component, ClassRealm realm) { - componentContextRealms.put( component, realm ); - } - }); - } - - protected void endComponentLifecycle( Object component ) - throws ComponentLifecycleException - { - ClassRealm contextRealm = componentContextRealms.remove( component ); - if ( contextRealm == null ) - { - contextRealm = container.getLookupRealm( component ); - } - - try - { - getLifecycleHandler().end( component, this, contextRealm ); - } - catch ( PhaseExecutionException e ) - { - throw new ComponentLifecycleException( "Error ending component lifecycle", e ); - } - } - public MutablePlexusContainer getContainer() { return container; } - - public Logger getLogger() - { - return container.getLogger(); - } - - public void dissociateComponentRealm( ClassRealm realm ) - throws ComponentLifecycleException - { - synchronized ( componentContextRealms ) - { - for ( Iterator> iterator = componentContextRealms.entrySet().iterator(); iterator.hasNext(); ) - { - Entry entry = iterator.next(); - ClassRealm componentRealm = entry.getValue(); - - if ( componentRealm.getId().equals( realm.getId() ) ) - { - iterator.remove(); - } - } - } - } } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/ComponentManager.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/ComponentManager.java index 88a56148c..eb2d72b83 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/ComponentManager.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/ComponentManager.java @@ -19,7 +19,6 @@ import org.codehaus.plexus.MutablePlexusContainer; import org.codehaus.plexus.personality.plexus.lifecycle.phase.PhaseExecutionException; -import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.codehaus.plexus.component.factory.ComponentInstantiationException; import org.codehaus.plexus.component.repository.ComponentDescriptor; import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; @@ -46,36 +45,21 @@ public interface ComponentManager */ AtomicLong NEXT_START_ID = new AtomicLong( 1 ); - int getConnections(); - /** * @deprecated use start instead */ LifecycleHandler getLifecycleHandler(); - void dispose() - throws ComponentLifecycleException; + void dispose() throws ComponentLifecycleException; - void release( Object component ) - throws ComponentLifecycleException; + void release( Object component ) throws ComponentLifecycleException; T getComponent() throws ComponentInstantiationException, ComponentLifecycleException; ComponentDescriptor getComponentDescriptor(); - Class getType(); - - String getRole(); - - String getRoleHint(); - MutablePlexusContainer getContainer(); - void dissociateComponentRealm( ClassRealm realm ) - throws ComponentLifecycleException; - - ClassRealm getRealm(); - void start(Object component) throws PhaseExecutionException; /** diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/ComponentManagerFactory.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/ComponentManagerFactory.java index 0a3f996fb..fe16a164c 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/ComponentManagerFactory.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/ComponentManagerFactory.java @@ -17,8 +17,6 @@ public interface ComponentManagerFactory * Creates a new component manager for the specified component descriptor. */ ComponentManager createComponentManager( MutablePlexusContainer container, - LifecycleHandler lifecycleHandler, - ComponentDescriptor componentDescriptor, - String role, - String roleHint ); + LifecycleHandler lifecycleHandler, + ComponentDescriptor componentDescriptor ); } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/PerLookupComponentManager.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/PerLookupComponentManager.java index 34f4e259b..4b1381427 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/PerLookupComponentManager.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/PerLookupComponentManager.java @@ -22,6 +22,12 @@ import org.codehaus.plexus.MutablePlexusContainer; import org.codehaus.plexus.lifecycle.LifecycleHandler; +import java.util.Map; +import java.util.Set; + +import com.google.common.collect.ReferenceMap; +import static com.google.common.base.ReferenceType.WEAK; + /** * Creates a new component manager for every lookup * @@ -32,33 +38,97 @@ public class PerLookupComponentManager extends AbstractComponentManager { + private boolean disposed; + private final Map instances = new ReferenceMap( WEAK, WEAK); + public PerLookupComponentManager( MutablePlexusContainer container, LifecycleHandler lifecycleHandler, - ComponentDescriptor componentDescriptor, - String role, - String roleHint ) + ComponentDescriptor componentDescriptor ) { - super( container, lifecycleHandler, componentDescriptor, role, roleHint ); + super( container, lifecycleHandler, componentDescriptor ); } - public void dispose() + public void dispose() throws ComponentLifecycleException { + Set instances; + synchronized (this) { + disposed = true; + instances = this.instances.keySet(); + this.instances.clear(); + } + + ComponentLifecycleException componentLifecycleException = null; + for ( T instance : instances ) + { + try + { + // do not call destroyInstance inside of a synchronized block because + // destroyInstance results in several callbacks to user code which + // could result in a dead lock + destroyInstance( instance ); + } + catch ( ComponentLifecycleException e ) + { + if (componentLifecycleException == null) { + componentLifecycleException = e; + } + } + } + + if (componentLifecycleException == null) { + throw componentLifecycleException; + } } - public T getComponent( ) - throws ComponentInstantiationException, ComponentLifecycleException + public T getComponent( ) throws ComponentInstantiationException, ComponentLifecycleException { - T component = createComponentInstance(); + synchronized (this) { + if (disposed) + { + throw new ComponentLifecycleException("This ComponentManager has already been destroyed"); + } + } + + // do not call createInstance inside of a synchronized block because + // createInstance results in several callbacks to user code which + // could result in a dead lock + T instance = createInstance(); - return component; + synchronized (this) { + // if this manager has been destroyed during create, destroy newly + // created component + if (disposed) + { + try + { + destroyInstance( instance ); + } + catch ( ComponentLifecycleException e ) + { + // todo: log ignored exception + } + + throw new ComponentLifecycleException("This ComponentManager has already been destroyed"); + } + + instances.put(instance, instance); + } + + return instance; } - public void release( Object component ) - throws ComponentLifecycleException + public void release( Object component ) throws ComponentLifecycleException { - decrementConnectionCount(); - endComponentLifecycle( component ); - // non cleanup map references for per-lookup cause leak - componentContextRealms.remove( component ); + T instance; + synchronized (this) { + instance = instances.remove(component); + } + + // do not call destroyInstance inside of a synchronized block because + // destroyInstance results in several callbacks to user code which + // could result in a dead lock + if (instance != null ) { + destroyInstance( component ); + } } } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/PerLookupComponentManagerFactory.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/PerLookupComponentManagerFactory.java index 69f180ba6..349a4b685 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/PerLookupComponentManagerFactory.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/PerLookupComponentManagerFactory.java @@ -12,12 +12,10 @@ public String getId() } @SuppressWarnings({"RawUseOfParameterizedType"}) - public ComponentManager createComponentManager( MutablePlexusContainer container, - LifecycleHandler lifecycleHandler, - ComponentDescriptor componentDescriptor, - String role, - String roleHint ) + public ComponentManager createComponentManager( MutablePlexusContainer container, + LifecycleHandler lifecycleHandler, + ComponentDescriptor componentDescriptor ) { - return new PerLookupComponentManager( container, lifecycleHandler, componentDescriptor, role, roleHint ); + return new PerLookupComponentManager( container, lifecycleHandler, componentDescriptor ); } } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/SingletonComponentManager.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/SingletonComponentManager.java index bf489d850..f418c0e25 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/SingletonComponentManager.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/SingletonComponentManager.java @@ -14,6 +14,11 @@ * the License. */ +import java.util.concurrent.Callable; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.ExecutionException; + import org.codehaus.plexus.component.factory.ComponentInstantiationException; import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; import org.codehaus.plexus.component.repository.ComponentDescriptor; @@ -26,49 +31,141 @@ * * @author Jason van Zyl */ -public class SingletonComponentManager - extends AbstractComponentManager +public class SingletonComponentManager extends AbstractComponentManager { - private T singleton; + private boolean disposed; + private Future singletonFuture; public SingletonComponentManager( MutablePlexusContainer container, LifecycleHandler lifecycleHandler, - ComponentDescriptor componentDescriptor, - String role, - String roleHint ) + ComponentDescriptor componentDescriptor ) { - super( container, lifecycleHandler, componentDescriptor, role, roleHint ); + super( container, lifecycleHandler, componentDescriptor ); } - public synchronized void release( Object component ) - throws ComponentLifecycleException + public synchronized void dispose() throws ComponentLifecycleException { - if ( singleton == component ) + T singleton; + synchronized ( this ) { - dispose(); + disposed = true; + singleton = getExistingInstance(true); + } + + // do not call destroyInstance inside of a synchronized block because + // destroyInstance results in several callbacks to user code which + // could result in a dead lock + if ( singleton != null ) + { + destroyInstance( singleton ); } } - public synchronized void dispose() - throws ComponentLifecycleException + public T getComponent( ) throws ComponentInstantiationException, ComponentLifecycleException { - if ( singleton != null ) + FutureTask singletonFuture; + synchronized (this) { + if (disposed) + { + throw new ComponentLifecycleException("This ComponentManager has already been destroyed"); + } + + // if singleton already created, simply return the existing singleton + T singleton = getExistingInstance( false ); + if (singleton != null) { + return singleton; + } + + // no existing singleton, create a new one + singletonFuture = new FutureTask(new CreateInstance()); + this.singletonFuture = singletonFuture; + } + + // do not call CreateInstance.get() inside of a synchronized block because createInstance results in + // several callbacks to user code which could result in a dead lock + if ( singletonFuture != null ) + { + singletonFuture.run(); + } + + // try to get the future instance + try + { + return singletonFuture.get(); + } + catch ( Exception e ) { - endComponentLifecycle( singleton ); - singleton = null; + // creation failed... clear future reference + synchronized ( this ) + { + // only clear if still refering to this method's future + if ( this.singletonFuture == singletonFuture ) + { + this.singletonFuture = null; + } + } + + // future.get() normally throws an execution execption which contains the real cause + Throwable cause = e; + if ( e instanceof ExecutionException && e.getCause() != null ) + { + cause = e.getCause(); + } + + // rethrow ComponentInstantiationException + if ( cause instanceof ComponentInstantiationException ) + { + throw (ComponentInstantiationException) cause; + } + + // rethrow ComponentLifecycleException + if ( cause instanceof ComponentLifecycleException ) + { + throw (ComponentLifecycleException) cause; + } + + // nothing else was expected + throw new ComponentLifecycleException( "Unexpected error obtaining singleton instance", cause ); } } - public synchronized T getComponent( ) - throws ComponentInstantiationException, ComponentLifecycleException + public void release( Object component ) throws ComponentLifecycleException { - if ( singleton == null ) + T singleton = getExistingInstance(true); + + // do not call destroyInstance inside of a synchronized block because + // destroyInstance results in several callbacks to user code which + // could result in a dead lock + if ( singleton != null ) { - singleton = createComponentInstance(); + destroyInstance( singleton ); } + } - incrementConnectionCount(); + public synchronized String toString() + { + T singleton = getExistingInstance(false); + return "SingletonComponentManager[" + singleton == null ? getComponentDescriptor().getImplementationClass().getName() : singleton + "]"; + } - return singleton; + private T getExistingInstance(boolean clearFuture) { + synchronized (this) { + try { + return singletonFuture.get(); + } catch (Exception e) { + // ignored - exception will have been reported in the createInstance method + } finally { + if (clearFuture) { + singletonFuture = null; + } + } + } + return null; + } + + private class CreateInstance implements Callable { + public T call() throws Exception { + return createInstance(); + } } } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/SingletonComponentManagerFactory.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/SingletonComponentManagerFactory.java index db2528df3..c6d057e91 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/SingletonComponentManagerFactory.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/SingletonComponentManagerFactory.java @@ -12,12 +12,10 @@ public String getId() } @SuppressWarnings({"RawUseOfParameterizedType"}) - public ComponentManager createComponentManager( MutablePlexusContainer container, - LifecycleHandler lifecycleHandler, - ComponentDescriptor componentDescriptor, - String role, - String roleHint ) + public ComponentManager createComponentManager( MutablePlexusContainer container, + LifecycleHandler lifecycleHandler, + ComponentDescriptor componentDescriptor ) { - return new SingletonComponentManager( container, lifecycleHandler, componentDescriptor, role, roleHint ); + return new SingletonComponentManager( container, lifecycleHandler, componentDescriptor ); } } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/StaticComponentManager.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/StaticComponentManager.java new file mode 100644 index 000000000..9af7c8220 --- /dev/null +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/manager/StaticComponentManager.java @@ -0,0 +1,82 @@ +package org.codehaus.plexus.component.manager; + +import org.codehaus.plexus.lifecycle.LifecycleHandler; +import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; +import org.codehaus.plexus.component.repository.ComponentDescriptor; +import org.codehaus.plexus.component.factory.ComponentInstantiationException; +import org.codehaus.plexus.MutablePlexusContainer; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.PhaseExecutionException; +import org.codehaus.plexus.classworlds.realm.ClassRealm; + +public class StaticComponentManager implements ComponentManager +{ + private final MutablePlexusContainer container; + private T instance; + private final ComponentDescriptor descriptor; + + private long startId; + private boolean disposed; + + public StaticComponentManager( MutablePlexusContainer container, T instance, Class role, String roleHint, ClassRealm realm) + { + this.container = container; + this.instance = instance; + + descriptor = new ComponentDescriptor(); + descriptor.setRole( role.getName() ); + descriptor.setRoleHint( roleHint ); + descriptor.setRealm( realm ); + descriptor.setImplementationClass( (Class) instance.getClass() ); + } + + public LifecycleHandler getLifecycleHandler() + { + return null; + } + + public synchronized T getComponent() throws ComponentLifecycleException + { + if (disposed) + { + throw new ComponentLifecycleException("This ComponentManager has already been destroyed"); + } + return instance; + } + + public synchronized void dispose() throws ComponentLifecycleException + { + disposed = true; + instance = null; + } + + public synchronized void release( Object component ) throws ComponentLifecycleException + { + instance = null; + } + + public ComponentDescriptor getComponentDescriptor() + { + return descriptor; + } + + public MutablePlexusContainer getContainer() + { + return container; + } + + public void start( Object component ) throws PhaseExecutionException + { + startId = NEXT_START_ID.getAndIncrement(); + } + + public long getStartId() + { + return startId; + } + + public synchronized String toString() + { + return "StaticComponentManager[" + instance == null ? getComponentDescriptor().getImplementationClass().getName() : instance + "]"; + } + +} diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/ComponentDescriptor.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/ComponentDescriptor.java index 4132f929a..82737b2d4 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/ComponentDescriptor.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/ComponentDescriptor.java @@ -39,6 +39,8 @@ public class ComponentDescriptor private String role = null; + private Class roleClass; + private String roleHint = PlexusConstants.PLEXUS_DEFAULT_HINT; private String implementation; @@ -92,9 +94,11 @@ public ComponentDescriptor() { } - public ComponentDescriptor( Class implementationClass ) + public ComponentDescriptor( Class implementationClass, ClassRealm realm ) { this.implementationClass = implementationClass; + this.implementation = implementationClass.getName(); + this.realm = realm; } /** @@ -169,6 +173,33 @@ public String getRole() return role; } + public Class getRoleClass() + { + attemptRoleLoad(); + + if (roleClass == null) { + return (Class) Object.class; + } + return roleClass; + } + + private void attemptRoleLoad() + { + if ( roleClass == null && role != null && realm != null ) + { + try + { + roleClass = realm.loadClass( role ); + Thread.currentThread(); + } + catch ( Throwable ignored ) + { + Thread.currentThread(); + } + } + } + + /** * Sets the role of this component. * @@ -177,6 +208,21 @@ public String getRole() public void setRole( String role ) { this.role = role; + + // reload role class + roleClass = null; + attemptRoleLoad(); + } + + public void setRoleClass( Class roleClass ) + { + this.roleClass = roleClass; + + if (roleClass == null) { + role = null; + } else { + role = roleClass.getName(); + } } /** @@ -254,9 +300,11 @@ private void attemptImplementationLoad() try { implementationClass = realm.loadClass( implementation ); + Thread.currentThread(); } catch ( Throwable ignored ) { + Thread.currentThread(); } } } @@ -599,6 +647,10 @@ public void setRealm( ClassRealm realm ) // reload implementation class implementationClass = null; attemptImplementationLoad(); + + // reload role class + roleClass = null; + attemptRoleLoad(); } // Component identity established here! diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/ComponentDescriptorListener.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/ComponentDescriptorListener.java new file mode 100644 index 000000000..a5e9fff44 --- /dev/null +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/ComponentDescriptorListener.java @@ -0,0 +1,14 @@ +package org.codehaus.plexus.component.repository; + +import java.util.List; + +public interface ComponentDescriptorListener +{ + Class getType(); + + List getRoleHints(); + + void componentDescriptorAdded(ComponentDescriptor componentDescriptor); + + void componentDescriptorRemoved(ComponentDescriptor componentDescriptor); +} diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/ComponentRepository.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/ComponentRepository.java deleted file mode 100644 index c49f21fcf..000000000 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/ComponentRepository.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.codehaus.plexus.component.repository; - -/* - * Copyright 2001-2006 Codehaus Foundation. - * - * Licensed 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. - */ - -import org.codehaus.plexus.classworlds.realm.ClassRealm; -import org.codehaus.plexus.component.repository.exception.ComponentRepositoryException; - -import java.util.List; -import java.util.Map; - -public interface ComponentRepository -{ - void addComponentDescriptor( ComponentDescriptor componentDescriptor ) - throws ComponentRepositoryException; - - ComponentDescriptor getComponentDescriptor( Class type, String role, String roleHint ); - - @Deprecated - ComponentDescriptor getComponentDescriptor( String role, String roleHint, ClassRealm realm ); - - Map> getComponentDescriptorMap( Class type, String role ); - - List> getComponentDescriptorList( Class type, String role ); - - void removeComponentRealm( ClassRealm classRealm ); -} diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/ComponentRequirement.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/ComponentRequirement.java index 5b8c4d474..49c3baa0c 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/ComponentRequirement.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/ComponentRequirement.java @@ -26,14 +26,44 @@ */ public class ComponentRequirement { + private String fieldName; + private String role; private String roleHint = PlexusConstants.PLEXUS_DEFAULT_HINT; - private String fieldName; - private String fieldMappingType; - + + public ComponentRequirement() + { + } + + public ComponentRequirement( String fieldName, String role ) + { + this( fieldName, role, null ); + } + + public ComponentRequirement( String fieldName, String role, String roleHint ) + { + this.fieldName = fieldName; + this.role = role; + + if ( roleHint != null ) + { + this.roleHint = roleHint; + } + } + + public ComponentRequirement( String fieldName, Class type ) + { + this( fieldName, type.getName(), null ); + } + + public ComponentRequirement( String fieldName, Class type, String roleHint ) + { + this( fieldName, type.getName(), roleHint ); + } + /** * Returns the field name that this component requirement will inject. * @return the field name that this component requirement will inject diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/DefaultComponentRepository.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/DefaultComponentRepository.java deleted file mode 100644 index b8fc4f804..000000000 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/DefaultComponentRepository.java +++ /dev/null @@ -1,206 +0,0 @@ -package org.codehaus.plexus.component.repository; - -/* - * Copyright 2001-2006 Codehaus Foundation. - * - * Licensed 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. - */ - -import static org.codehaus.plexus.component.CastUtils.isAssignableFrom; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.SortedMap; -import java.util.TreeMap; - -import org.codehaus.plexus.classworlds.realm.ClassRealm; -import org.codehaus.plexus.component.composition.CompositionException; -import org.codehaus.plexus.component.composition.CompositionResolver; -import org.codehaus.plexus.component.composition.DefaultCompositionResolver; -import org.codehaus.plexus.component.repository.exception.ComponentRepositoryException; -import org.codehaus.plexus.logging.AbstractLogEnabled; - -import com.google.common.collect.Multimap; -import com.google.common.collect.Multimaps; - -/** - * @author Jason van Zyl - */ -public class DefaultComponentRepository - extends AbstractLogEnabled - implements ComponentRepository -{ - private final Map>>> index = new LinkedHashMap>>>(); - - private final CompositionResolver compositionResolver = new DefaultCompositionResolver(); - - public DefaultComponentRepository() - { - } - - // ---------------------------------------------------------------------- - // Accessors - // ---------------------------------------------------------------------- - - private Multimap> getComponentDescriptors( String role ) - { - // verify arguments - if ( role == null ) - { - throw new NullPointerException( "role is null" ); - } - - // determine realms to search - LinkedHashSet realms = new LinkedHashSet(); - for (ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); classLoader != null; classLoader = classLoader.getParent()) { - if ( classLoader instanceof ClassRealm ) - { - ClassRealm realm = (ClassRealm) classLoader; - while (realm != null) { - realms.add(realm); - realm = realm.getParentRealm(); - } - } - } - if (realms.isEmpty()) { - realms.addAll( index.keySet() ); - } - - // Get all valid component descriptors - Multimap> roleHintIndex = Multimaps.newLinkedHashMultimap(); - for ( ClassRealm realm : realms ) - { - SortedMap>> roleIndex = index.get( realm ); - if (roleIndex != null) { - Multimap> descriptors = roleIndex.get( role ); - if ( descriptors != null ) - { - roleHintIndex.putAll( descriptors ); - } - } - } - return Multimaps.unmodifiableMultimap( roleHintIndex ); - } - - public ComponentDescriptor getComponentDescriptor( Class type, String role, String roleHint ) - { - for ( ComponentDescriptor descriptor : getComponentDescriptors( role ).get( roleHint ) ) - { - if ( isAssignableFrom( type, descriptor.getImplementationClass() ) ) - { - return (ComponentDescriptor) descriptor; - } - } - - return null; - } - - public Map> getComponentDescriptorMap( Class type, String role ) - { - Map> descriptors = new TreeMap>(); - for ( ComponentDescriptor descriptor : getComponentDescriptors( role ).values() ) - { - if ( !descriptors.containsKey( descriptor.getRoleHint() ) ) - { - if ( isAssignableFrom( type, descriptor.getImplementationClass() ) ) - { - descriptors.put( descriptor.getRoleHint(), (ComponentDescriptor) descriptor ); - } - } - } - return descriptors; - } - - public List> getComponentDescriptorList( Class type, String role ) - { - List> descriptors = new ArrayList>(); - for ( ComponentDescriptor descriptor : getComponentDescriptors( role ).values() ) - { - if ( isAssignableFrom( type, descriptor.getImplementationClass() ) ) - { - descriptors.add( (ComponentDescriptor) descriptor ); - } - } - return descriptors; - } - - @Deprecated - public ComponentDescriptor getComponentDescriptor( String role, String roleHint, ClassRealm realm ) - { - // find all realms from our realm to the root realm - Set realms = new HashSet(); - for ( ClassRealm r = realm; r != null; r = r.getParentRealm() ) - { - realms.add( r ); - } - - // get the component descriptors by roleHint - for ( ComponentDescriptor componentDescriptor : getComponentDescriptors( role ).get( roleHint ) ) - { - // return the first descriptor from our target realms - if ( realms.contains( componentDescriptor.getRealm() ) ) - { - return componentDescriptor; - } - } - - return null; - } - - public void removeComponentRealm( ClassRealm classRealm ) - { - index.remove( classRealm ); - } - - // ---------------------------------------------------------------------- - // Lifecylce Management - // ---------------------------------------------------------------------- - - // ---------------------------------------------------------------------- - // Component Descriptor processing. - // ---------------------------------------------------------------------- - - public void addComponentDescriptor( ComponentDescriptor componentDescriptor ) - throws ComponentRepositoryException - { - ClassRealm classRealm = componentDescriptor.getRealm(); - SortedMap>> roleIndex = index.get( classRealm ); - if (roleIndex == null) { - roleIndex = new TreeMap>>(); - index.put(classRealm, roleIndex); - } - - String role = componentDescriptor.getRole(); - Multimap> roleHintIndex = roleIndex.get( role ); - if ( roleHintIndex == null ) - { - roleHintIndex = Multimaps.newLinkedHashMultimap(); - roleIndex.put( role, roleHintIndex ); - } - roleHintIndex.put( componentDescriptor.getRoleHint(), componentDescriptor ); - - try - { - compositionResolver.addComponentDescriptor( componentDescriptor ); - } - catch ( CompositionException e ) - { - throw new ComponentRepositoryException( e.getMessage(), e ); - } - } -} diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/exception/ComponentLookupException.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/exception/ComponentLookupException.java index eb666f390..67c3bbaf7 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/exception/ComponentLookupException.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/exception/ComponentLookupException.java @@ -1,6 +1,14 @@ package org.codehaus.plexus.component.repository.exception; import org.codehaus.plexus.classworlds.realm.ClassRealm; +import org.codehaus.plexus.component.repository.ComponentDescriptor; +import org.codehaus.plexus.component.ComponentStackElement; +import org.codehaus.plexus.component.ComponentStack; + +import java.util.List; +import java.util.ArrayList; +import static java.util.Collections.emptyList; +import static java.util.Collections.unmodifiableList; /* * Copyright 2001-2006 Codehaus Foundation. @@ -36,44 +44,70 @@ public class ComponentLookupException private ClassRealm realm; + private List componentStack; + public ComponentLookupException( String message, String role, String roleHint ) { - super( message ); - - this.role = role; + this( message, role, roleHint, null, null); + } - this.roleHint = roleHint; + public ComponentLookupException( String message, Class type, String roleHint ) + { + this( message, type.getName(), roleHint, null, null); } public ComponentLookupException( String message, String role, String roleHint, Throwable cause ) { - super( message, cause ); - - this.role = role; + this( message, role, roleHint, null, cause); + } - this.roleHint = roleHint; + public ComponentLookupException( String message, Class type, String roleHint, Throwable cause ) + { + this( message, type.getName(), roleHint, null, cause); } public ComponentLookupException( String message, String role, String roleHint, ClassRealm realm ) { - super( message ); - - this.role = role; - - this.roleHint = roleHint; + this( message, role, roleHint, realm, null ); + } - this.realm = realm; + public ComponentLookupException( String message, Class type, String roleHint, ClassRealm realm ) + { + this( message, type.getName(), roleHint, realm, null); } public ComponentLookupException( String message, String role, String roleHint, ClassRealm realm, Throwable cause ) { super( message, cause ); - this.role = role; - this.roleHint = roleHint; - this.realm = realm; + this.componentStack = ComponentStack.getComponentStack(); + } + + public ComponentLookupException( String message, ComponentDescriptor descriptor ) { + this( message, descriptor.getRole(), descriptor.getRoleHint(), descriptor.getRealm(), null ); + } + + public ComponentLookupException( String message, ComponentDescriptor descriptor, Throwable cause ) { + this( message, descriptor.getRole(), descriptor.getRoleHint(), descriptor.getRealm(), cause ); + } + + public List getComponentStack() + { + if ( componentStack != null ) + { + return componentStack; + } + else + { + return emptyList(); + } + } + + public void setComponentStack( List componentStack ) + { + this.componentStack = unmodifiableList( new ArrayList(componentStack) ); } public String getMessage() @@ -93,7 +127,17 @@ public String getMessage() { sb.append( "none specified" ); } + sb.append( LS ); + + sb.append( LS ); + sb.append( "Component stack:" ).append( LS ); + for ( ComponentStackElement element : componentStack ) + { + sb.append( "\tin " ).append( element ).append( LS ); + } + sb.append( LS ); + sb.append( "Code stack:" ); return sb.toString(); } } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/exception/ComponentRepositoryException.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/exception/ComponentRepositoryException.java index 73799f129..380ee3a63 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/exception/ComponentRepositoryException.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/exception/ComponentRepositoryException.java @@ -1,5 +1,8 @@ package org.codehaus.plexus.component.repository.exception; +import org.codehaus.plexus.classworlds.realm.ClassRealm; +import org.codehaus.plexus.component.repository.ComponentDescriptor; + /* * Copyright 2001-2006 Codehaus Foundation. * @@ -28,22 +31,102 @@ public class ComponentRepositoryException { private static final long serialVersionUID = 3698017788731736736L; - /** - * Construct a new ComponentRepositoryException instance. - * @param message exception message - */ - public ComponentRepositoryException( String message ) + private String LS = System.getProperty( "line.separator" ); + + private String role; + + private String roleHint; + + private ClassRealm realm; + + public ComponentRepositoryException( String message, String role, String roleHint ) + { + super( message ); + + this.role = role; + + this.roleHint = roleHint; + } + + public ComponentRepositoryException( String message, Class type, String roleHint ) { super( message ); + + this.role = type.getName(); + + this.roleHint = roleHint; } - /** - * Construct a new ComponentRepositoryException instance. - * @param message exception message - * @param cause causing exception to chain - */ - public ComponentRepositoryException( String message, Throwable cause ) + public ComponentRepositoryException( String message, String role, String roleHint, Throwable cause ) { super( message, cause ); + + this.role = role; + + this.roleHint = roleHint; + } + + public ComponentRepositoryException( String message, String role, String roleHint, ClassRealm realm ) + { + super( message ); + + this.role = role; + + this.roleHint = roleHint; + + this.realm = realm; + } + + public ComponentRepositoryException( String message, String role, String roleHint, ClassRealm realm, Throwable cause ) + { + super( message, cause ); + + this.role = role; + + this.roleHint = roleHint; + + this.realm = realm; + } + + public ComponentRepositoryException( String message, Class type, String roleHint, ClassRealm realm ) + { + super( message ); + + this.role = type.getName(); + + this.roleHint = roleHint; + + this.realm = realm; + } + + public ComponentRepositoryException( String message, ComponentDescriptor descriptor ) + { + this( message, descriptor.getRole(), descriptor.getRoleHint(), descriptor.getRealm() ); + } + + public ComponentRepositoryException( String message, ComponentDescriptor descriptor, Throwable cause ) + { + this( message, descriptor.getRole(), descriptor.getRoleHint(), descriptor.getRealm(), cause ); + } + + public String getMessage() + { + StringBuffer sb = new StringBuffer() + .append( super.getMessage() ).append( LS ) + .append( " role: " ).append( role ).append( LS ) + .append( " roleHint: " ).append( roleHint ).append( LS ) + .append( "classRealm: " ); + + if ( realm != null ) + { + sb.append( realm.getId() ); + realm.display(); + } + else + { + sb.append( "none specified" ); + } + + return sb.toString(); } } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/io/PlexusTools.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/io/PlexusTools.java index fdc2f04a1..185b82e17 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/io/PlexusTools.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/io/PlexusTools.java @@ -31,6 +31,7 @@ import org.codehaus.plexus.configuration.PlexusConfiguration; import org.codehaus.plexus.configuration.PlexusConfigurationException; import org.codehaus.plexus.configuration.io.XmlPlexusConfigurationReader; +import org.codehaus.plexus.classworlds.realm.ClassRealm; /** @@ -68,23 +69,44 @@ public static PlexusConfiguration buildConfiguration( String configuration ) return buildConfiguration( "", new StringReader( configuration ) ); } - public static ComponentDescriptor buildComponentDescriptor( String configuration ) + public static ComponentDescriptor buildComponentDescriptor( String configuration, ClassRealm realm ) throws PlexusConfigurationException { - return buildComponentDescriptor( buildConfiguration( configuration ) ); + return buildComponentDescriptor( buildConfiguration( configuration ), realm ); } - public static ComponentDescriptor buildComponentDescriptor( PlexusConfiguration configuration ) + public static ComponentDescriptor buildComponentDescriptor( PlexusConfiguration configuration, ClassRealm realm ) throws PlexusConfigurationException + { - ComponentDescriptor cd = new ComponentDescriptor(); + if ( realm == null ) + { + throw new NullPointerException( "realm is null" ); + } + + + String implementation = configuration.getChild( "implementation" ).getValue(); + if (implementation == null) + { + throw new PlexusConfigurationException( "implementation is null" ); + } + + ComponentDescriptor cd; + try + { + Class implementationClass = realm.loadClass( implementation ); + cd = new ComponentDescriptor(implementationClass, realm); + } + catch ( Throwable e ) + { + throw new PlexusConfigurationException("Can not load implementation class " + implementation + + " from realm " + realm, e); + } cd.setRole( configuration.getChild( "role" ).getValue() ); cd.setRoleHint( configuration.getChild( "role-hint" ).getValue() ); - cd.setImplementation( configuration.getChild( "implementation" ).getValue() ); - cd.setVersion( configuration.getChild( "version" ).getValue() ); cd.setComponentType( configuration.getChild( "component-type" ).getValue() ); @@ -161,7 +183,7 @@ public static ComponentDescriptor buildComponentDescriptor( PlexusConfiguration return cd; } - public static ComponentSetDescriptor buildComponentSet( PlexusConfiguration c ) + public static ComponentSetDescriptor buildComponentSet( PlexusConfiguration c, ClassRealm realm ) throws PlexusConfigurationException { ComponentSetDescriptor csd = new ComponentSetDescriptor(); @@ -176,7 +198,7 @@ public static ComponentSetDescriptor buildComponentSet( PlexusConfiguration c ) { PlexusConfiguration component = components[i]; - csd.addComponentDescriptor( buildComponentDescriptor( component ) ); + csd.addComponentDescriptor( buildComponentDescriptor( component, realm ) ); } // ---------------------------------------------------------------------- diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/configuration/PlexusConfiguration.java b/plexus-container-default/src/main/java/org/codehaus/plexus/configuration/PlexusConfiguration.java index ee8f366d0..f61f9c9e6 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/configuration/PlexusConfiguration.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/configuration/PlexusConfiguration.java @@ -46,10 +46,8 @@ public interface PlexusConfiguration /** * Returns the value of this configuration. * @return the value of this configuration - * @throws PlexusConfigurationException */ - String getValue() - throws PlexusConfigurationException; + String getValue(); /** * Returns the value of this configuration, or default if one cannot be diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/configuration/PlexusConfigurationMerger.java b/plexus-container-default/src/main/java/org/codehaus/plexus/configuration/PlexusConfigurationMerger.java index 0f32754cb..229ea1e62 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/configuration/PlexusConfigurationMerger.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/configuration/PlexusConfigurationMerger.java @@ -255,20 +255,13 @@ private static void copyResources( PlexusConfiguration source, PlexusConfigurati private static void copyComponentManagers( PlexusConfiguration source, PlexusConfiguration destination ) { - try - { - PlexusConfiguration id = destination.getChild( "default-component-manager-id" ); + PlexusConfiguration id = destination.getChild( "default-component-manager-id" ); - String sid = source.getChild( "default-component-manager-id" ).getValue(); + String sid = source.getChild( "default-component-manager-id" ).getValue(); - if ( id.getValue() == null ) - { - id.setValue( sid ); - } - } - catch ( PlexusConfigurationException e ) + if ( id.getValue() == null ) { - // do nothing + id.setValue( sid ); } @@ -308,20 +301,13 @@ private static void copyComponentFactories( PlexusConfiguration source, PlexusCo private static void copyComponentComposers( PlexusConfiguration source, PlexusConfiguration destination ) { - try - { - PlexusConfiguration id = destination.getChild( "default-component-composer-id" ); + PlexusConfiguration id = destination.getChild( "default-component-composer-id" ); - String sid = source.getChild( "default-component-composer-id" ).getValue(); + String sid = source.getChild( "default-component-composer-id" ).getValue(); - if ( id.getValue() == null ) - { - id.setValue( sid ); - } - } - catch ( PlexusConfigurationException e ) + if ( id.getValue() == null ) { - // do nothing + id.setValue( sid ); } PlexusConfiguration composers[] = source.getChild( "component-composers" ).getChildren( "component-composer" ); diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/container/initialization/InitializeComponentRegistryPhase.java b/plexus-container-default/src/main/java/org/codehaus/plexus/container/initialization/InitializeComponentRegistryPhase.java index ad709c3b0..b2c4a6961 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/container/initialization/InitializeComponentRegistryPhase.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/container/initialization/InitializeComponentRegistryPhase.java @@ -16,16 +16,15 @@ * limitations under the License. */ -import org.codehaus.plexus.component.repository.ComponentRepository; -import org.codehaus.plexus.component.repository.ComponentDescriptor; -import org.codehaus.plexus.component.repository.io.PlexusTools; -import org.codehaus.plexus.component.repository.exception.ComponentRepositoryException; +import org.codehaus.plexus.ComponentRegistry; +import org.codehaus.plexus.DefaultComponentRegistry; import org.codehaus.plexus.component.manager.PerLookupComponentManagerFactory; import org.codehaus.plexus.component.manager.SingletonComponentManagerFactory; +import org.codehaus.plexus.component.repository.ComponentDescriptor; +import org.codehaus.plexus.component.repository.exception.ComponentRepositoryException; +import org.codehaus.plexus.component.repository.io.PlexusTools; import org.codehaus.plexus.configuration.PlexusConfiguration; import org.codehaus.plexus.configuration.PlexusConfigurationException; -import org.codehaus.plexus.DefaultComponentRegistry; -import org.codehaus.plexus.ComponentRegistry; import org.codehaus.plexus.lifecycle.LifecycleHandlerManager; /** @@ -36,37 +35,40 @@ public class InitializeComponentRegistryPhase implements ContainerInitialization public void execute( ContainerInitializationContext context ) throws ContainerInitializationException { - ComponentRepository repository = getComponentRepository( context ); - LifecycleHandlerManager lifecycleHandlerManager = getLifecycleHandlerManager( context ); ComponentRegistry componentRegistry = new DefaultComponentRegistry( context.getContainer(), - repository, lifecycleHandlerManager ); componentRegistry.registerComponentManagerFactory( new PerLookupComponentManagerFactory() ); componentRegistry.registerComponentManagerFactory( new SingletonComponentManagerFactory() ); + initializeComponentRegistry( componentRegistry, context ); + context.getContainer().setComponentRegistry( componentRegistry ); } - private ComponentRepository getComponentRepository( ContainerInitializationContext context ) + private void initializeComponentRegistry( ComponentRegistry componentRegistry, + ContainerInitializationContext context ) throws ContainerInitializationException { - ComponentRepository repository = context.getContainerConfiguration().getComponentRepository(); - // Add the components defined in the container xml configuration try { PlexusConfiguration configuration = context.getContainerXmlConfiguration(); - PlexusConfiguration[] componentConfigurations = configuration.getChild( "components" ).getChildren( "component" ); + PlexusConfiguration[] componentConfigurations = configuration.getChild( "components" ).getChildren( + "component" ); for ( PlexusConfiguration componentConfiguration : componentConfigurations ) { - ComponentDescriptor componentDescriptor = PlexusTools.buildComponentDescriptor( componentConfiguration ); - componentDescriptor.setRealm( context.getContainer().getContainerRealm() ); - repository.addComponentDescriptor( componentDescriptor ); + ComponentDescriptor componentDescriptor = PlexusTools.buildComponentDescriptor( + componentConfiguration, context.getContainer().getContainerRealm() ); + + if ( componentDescriptor != null ) + { + componentRegistry.addComponentDescriptor( componentDescriptor ); + } } } catch ( PlexusConfigurationException e ) @@ -78,7 +80,6 @@ private ComponentRepository getComponentRepository( ContainerInitializationConte { throw new ContainerInitializationException( "Error initializing component repository: ", e ); } - return repository; } private LifecycleHandlerManager getLifecycleHandlerManager( ContainerInitializationContext context ) diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/lifecycle/phase/AbstractPhase.java b/plexus-container-default/src/main/java/org/codehaus/plexus/lifecycle/phase/AbstractPhase.java index 5e16560cb..7977f8be7 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/lifecycle/phase/AbstractPhase.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/lifecycle/phase/AbstractPhase.java @@ -20,19 +20,6 @@ import org.codehaus.plexus.component.manager.ComponentManager; import org.codehaus.plexus.personality.plexus.lifecycle.phase.PhaseExecutionException; -public abstract class AbstractPhase - implements Phase +public abstract class AbstractPhase implements Phase { - /** Execute the phase. */ - public abstract void execute( Object component, ComponentManager manager, ClassRealm realm ) - throws PhaseExecutionException; - - /** - * @deprecated - */ - public final void execute( Object component, ComponentManager manager ) - throws PhaseExecutionException - { - execute( component, manager, manager.getContainer().getLookupRealm( component ) ); - } } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/lifecycle/phase/Phase.java b/plexus-container-default/src/main/java/org/codehaus/plexus/lifecycle/phase/Phase.java index 30316e6f7..854700941 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/lifecycle/phase/Phase.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/lifecycle/phase/Phase.java @@ -23,6 +23,6 @@ public interface Phase { /** Execute the phase. */ - public void execute( Object component, ComponentManager manager, ClassRealm realm ) + public void execute( Object component, ComponentManager manager, ClassRealm realm ) throws PhaseExecutionException; } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/logging/NullLogger.java b/plexus-container-default/src/main/java/org/codehaus/plexus/logging/NullLogger.java new file mode 100644 index 000000000..8727b22fe --- /dev/null +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/logging/NullLogger.java @@ -0,0 +1,34 @@ +package org.codehaus.plexus.logging; + +public class NullLogger extends AbstractLogger +{ + public NullLogger( ) + { + super( LEVEL_DISABLED, null ); + } + + public void debug( String message, Throwable throwable ) + { + } + + public void info( String message, Throwable throwable ) + { + } + + public void warn( String message, Throwable throwable ) + { + } + + public void error( String message, Throwable throwable ) + { + } + + public void fatalError( String message, Throwable throwable ) + { + } + + public Logger getChildLogger( String name ) + { + return this; + } +} diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/personality/plexus/lifecycle/phase/AutoConfigurePhase.java b/plexus-container-default/src/main/java/org/codehaus/plexus/personality/plexus/lifecycle/phase/AutoConfigurePhase.java index db89b8c4b..d6e23ad94 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/personality/plexus/lifecycle/phase/AutoConfigurePhase.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/personality/plexus/lifecycle/phase/AutoConfigurePhase.java @@ -32,7 +32,7 @@ public class AutoConfigurePhase public static final String DEFAULT_CONFIGURATOR_ID = "default"; public void execute( Object object, - ComponentManager manager, + ComponentManager manager, ClassRealm lookupRealm ) throws PhaseExecutionException { @@ -53,7 +53,7 @@ public void execute( Object object, if ( configuration != null ) { - ClassRealm realm = manager.getRealm(); + ClassRealm realm = descriptor.getRealm(); componentConfigurator.configureComponent( object, configuration, realm ); } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/personality/plexus/lifecycle/phase/LogDisablePhase.java b/plexus-container-default/src/main/java/org/codehaus/plexus/personality/plexus/lifecycle/phase/LogDisablePhase.java index 6d11978a7..7a1392eff 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/personality/plexus/lifecycle/phase/LogDisablePhase.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/personality/plexus/lifecycle/phase/LogDisablePhase.java @@ -30,13 +30,14 @@ public class LogDisablePhase extends AbstractPhase { - public void execute( Object object, ComponentManager componentManager, ClassRealm lookupRealm ) + public void execute( Object object, ComponentManager componentManager, ClassRealm lookupRealm ) throws PhaseExecutionException { if ( object instanceof LogEnabled ) { LoggerManager loggerManager = componentManager.getContainer().getLoggerManager(); - loggerManager.returnComponentLogger( componentManager.getRole(), componentManager.getRoleHint() ); + ComponentDescriptor descriptor = componentManager.getComponentDescriptor(); + loggerManager.returnComponentLogger( descriptor.getRole(), descriptor.getRoleHint() ); } } } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/personality/plexus/lifecycle/phase/LogEnablePhase.java b/plexus-container-default/src/main/java/org/codehaus/plexus/personality/plexus/lifecycle/phase/LogEnablePhase.java index a8bbee362..1e12d26a2 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/personality/plexus/lifecycle/phase/LogEnablePhase.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/personality/plexus/lifecycle/phase/LogEnablePhase.java @@ -36,7 +36,8 @@ public void execute( Object object, ComponentManager componentManager, ClassReal LoggerManager loggerManager = componentManager.getContainer().getLoggerManager(); - Logger logger = loggerManager.getLoggerForComponent( componentManager.getRole(), componentManager.getRoleHint() ); + ComponentDescriptor descriptor = componentManager.getComponentDescriptor(); + Logger logger = loggerManager.getLoggerForComponent( descriptor.getRole(), descriptor.getRoleHint() ); logEnabled.enableLogging( logger ); } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/personality/plexus/lifecycle/phase/StartingException.java b/plexus-container-default/src/main/java/org/codehaus/plexus/personality/plexus/lifecycle/phase/StartingException.java index 414ce2517..1b67a8700 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/personality/plexus/lifecycle/phase/StartingException.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/personality/plexus/lifecycle/phase/StartingException.java @@ -34,4 +34,9 @@ public StartingException( String message, Throwable cause ) { super( message, cause ); } + + public StartingException( Throwable cause ) + { + super( cause ); + } } diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/component/MapOrientedComponentProcessingTest.java b/plexus-container-default/src/test/java/org/codehaus/plexus/component/MapOrientedComponentProcessingTest.java index b0440eb9d..de1d5cb78 100644 --- a/plexus-container-default/src/test/java/org/codehaus/plexus/component/MapOrientedComponentProcessingTest.java +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/component/MapOrientedComponentProcessingTest.java @@ -35,7 +35,10 @@ public class MapOrientedComponentProcessingTest public void testShouldFindAndInitializeMapOrientedComponent() throws Exception { - ComponentDescriptor descriptor = new ComponentDescriptor(TestMapOrientedComponent.class); + PlexusContainer embedder = new DefaultPlexusContainer(); + + ComponentDescriptor descriptor = + new ComponentDescriptor(TestMapOrientedComponent.class, embedder.getContainerRealm()); descriptor.setRole( TestMapOrientedComponent.ROLE ); @@ -63,7 +66,6 @@ public void testShouldFindAndInitializeMapOrientedComponent() descriptor.setConfiguration( configuration ); - PlexusContainer embedder = new DefaultPlexusContainer(); embedder.addComponentDescriptor( descriptor ); diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/component/PlexusExceptionTest.java b/plexus-container-default/src/test/java/org/codehaus/plexus/component/PlexusExceptionTest.java new file mode 100644 index 000000000..38b7abfb1 --- /dev/null +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/component/PlexusExceptionTest.java @@ -0,0 +1,354 @@ +package org.codehaus.plexus.component; + +import org.codehaus.plexus.DefaultPlexusContainer; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.Startable; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.StartingException; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.StoppingException; +import org.codehaus.plexus.configuration.PlexusConfiguration; +import org.codehaus.plexus.configuration.DefaultPlexusConfiguration; +import org.codehaus.plexus.component.repository.ComponentDescriptor; +import org.codehaus.plexus.component.repository.ComponentRequirement; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.apache.xbean.recipe.MissingAccessorException; +import junit.framework.TestCase; + +import java.util.List; + +public class PlexusExceptionTest extends TestCase +{ + private static DefaultPlexusContainer container; + private ComponentDescriptor exceptionalDescriptor; + private ComponentDescriptor requiresDescriptor; + + private static Exception constructorException; + private static Exception setterException; + private static Exception startException; + private static boolean lookupInSetter; + private static boolean lookupInConstructor; + + protected void setUp() throws Exception + { + super.setUp(); + container = new DefaultPlexusContainer(); + + // + // ExceptionalComponent definition + // + exceptionalDescriptor = new ComponentDescriptor( + ExceptionalComponent.class, + container.getContainerRealm() ); + exceptionalDescriptor.setRoleClass( ExceptionalComponent.class ); + PlexusConfiguration exceptionalConfiguration = new DefaultPlexusConfiguration(){}; + exceptionalConfiguration.setAttribute( "name", "exceptional bean" ); + exceptionalDescriptor.setConfiguration( exceptionalConfiguration ); + exceptionalDescriptor.setSource( "exceptionalDescriptor.xml" ); + + container.addComponentDescriptor( exceptionalDescriptor ); + + // + // RequiresComponent definition + // + requiresDescriptor = new ComponentDescriptor( + RequiresComponent.class, + container.getContainerRealm() ); + requiresDescriptor.setRoleClass( RequiresComponent.class ); + PlexusConfiguration requiresConfiguration = new DefaultPlexusConfiguration(){}; + requiresConfiguration.setAttribute( "name", "requires bean" ); + requiresDescriptor.setConfiguration( requiresConfiguration ); + requiresDescriptor.setSource( "requiresDescriptor.xml" ); + + container.addComponentDescriptor( requiresDescriptor ); + + constructorException = null; + setterException = null; + startException = null; + lookupInConstructor = false; + lookupInSetter = false; + } + + public void testConstructorCheckedException() throws Exception { + constructorException = new TestCheckedException( "constructor test" ); + + assertLookupFailed( constructorException ); + } + + public void testConstructorRuntimeException() throws Exception { + constructorException = new TestRuntimeException( "constructor test" ); + + assertLookupFailed( constructorException ); + } + + public void testSetterCheckedException() throws Exception { + setterException = new TestCheckedException( "setter test"); + + assertLookupFailed( setterException ); + } + + public void testSetterRuntimeException() throws Exception { + setterException = new TestRuntimeException( "setter test"); + + assertLookupFailed( setterException ); + } + + public void testStartCheckedException() throws Exception { + startException = new StartingException( "start test"); + + assertLookupFailed( startException ); + } + + public void testStartRuntimeException() throws Exception { + startException = new TestRuntimeException( "start test"); + + assertLookupFailed( startException ); + } + + public void testMissingProperty() throws Exception { + exceptionalDescriptor.getConfiguration().addChild( "unknown", "unknown" ); + + Throwable cause = assertLookupFailed( null ); + assertTrue("cause should be an instance of MissingAccessorException", cause instanceof MissingAccessorException ); + } + + public void testMissingRequirementProperty() throws Exception { + exceptionalDescriptor.addRequirement( new ComponentRequirement( "unknown", ExceptionalComponent.class.getName()) ); + + Throwable cause = assertLookupFailed( null ); + assertTrue("cause should be an instance of MissingAccessorException", cause instanceof MissingAccessorException ); + } + + private Throwable assertLookupFailed( Exception expected ) + { + try + { + container.lookup( ExceptionalComponent.class ); + fail("Expected ComponentLookupException"); + throw new AssertionError("Unreachable statement"); + } + catch ( ComponentLookupException lookupException ) + { + lookupException.printStackTrace( ); + + // verify cause is the same excption thrown from the component constructor + Throwable cause = lookupException.getCause(); + assertNotNull( "ComponentLookupException.getCause() is null", cause ); + if ( expected != null ) + { + assertSame( "cause should be same instance thrown from component", expected, cause ); + } + + // verify stack contains only the one component + List stack = lookupException.getComponentStack(); + assertEquals( "Component stack", 1, stack.size() ); + ComponentDescriptor failedDescriptor = stack.get( 0 ).getDescriptor(); + assertSame( "Failed component descriptor should be created component", exceptionalDescriptor, failedDescriptor ); + + return lookupException.getCause(); + } + } + + public void testNestedRequiresConstructorCheckedException() throws Exception { + constructorException = new TestCheckedException( "constructor test" ); + requiresDescriptor.addRequirement( new ComponentRequirement( "component", ExceptionalComponent.class.getName()) ); + + assertNestedRequiresFailed( constructorException ); + } + + public void testNestedRequiresConstructorRuntimeException() throws Exception { + constructorException = new TestRuntimeException( "constructor test" ); + requiresDescriptor.addRequirement( new ComponentRequirement( "component", ExceptionalComponent.class.getName()) ); + + assertNestedRequiresFailed( constructorException ); + } + + public void testNestedRequiresSetterCheckedException() throws Exception { + setterException = new TestCheckedException( "setter test"); + requiresDescriptor.addRequirement( new ComponentRequirement( "component", ExceptionalComponent.class.getName()) ); + + assertNestedRequiresFailed( setterException ); + } + + public void testNestedRequiresSetterRuntimeException() throws Exception { + setterException = new TestRuntimeException( "setter test"); + requiresDescriptor.addRequirement( new ComponentRequirement( "component", ExceptionalComponent.class.getName()) ); + + assertNestedRequiresFailed( setterException ); + } + + public void testNestedRequiresStartCheckedException() throws Exception { + startException = new StartingException( "start test"); + requiresDescriptor.addRequirement( new ComponentRequirement( "component", ExceptionalComponent.class.getName()) ); + + assertNestedRequiresFailed( startException ); + } + + public void testNestedRequiresStartRuntimeException() throws Exception { + startException = new TestRuntimeException( "start test"); + requiresDescriptor.addRequirement( new ComponentRequirement( "component", ExceptionalComponent.class.getName()) ); + + assertNestedRequiresFailed( startException ); + } + + public void testNestedRequiresMissingProperty() throws Exception { + exceptionalDescriptor.getConfiguration().addChild( "unknown", "unknown" ); + requiresDescriptor.addRequirement( new ComponentRequirement( "component", ExceptionalComponent.class.getName()) ); + + Throwable cause = assertNestedRequiresFailed( null ); + assertTrue("cause should be an instance of MissingAccessorException", cause instanceof MissingAccessorException ); + } + + public void testNestedRequiresMissingRequirementProperty() throws Exception { + exceptionalDescriptor.addRequirement( new ComponentRequirement( "unknown", ExceptionalComponent.class.getName()) ); + requiresDescriptor.addRequirement( new ComponentRequirement( "component", ExceptionalComponent.class.getName()) ); + + Throwable cause = assertNestedRequiresFailed( null ); + assertTrue("cause should be an instance of MissingAccessorException", cause instanceof MissingAccessorException ); + } + + public void testNestedLookupInConstructor() throws Exception { + constructorException = new TestCheckedException( "constructor test" ); + lookupInConstructor = true; + + assertNestedRequiresFailed( constructorException ); + } + + public void testNestedLookupInSetter() throws Exception { + constructorException = new TestCheckedException( "constructor test" ); + lookupInSetter = true; + + assertNestedRequiresFailed( constructorException ); + } + + private Throwable assertNestedRequiresFailed( Exception expected ) + { + try + { + container.lookup( RequiresComponent.class ); + fail("Expected ComponentLookupException"); + throw new AssertionError("Unreachable statement"); + } + catch ( ComponentLookupException lookupException ) + { + lookupException.printStackTrace( ); + + // verify cause is the same excption thrown from the component constructor + Throwable cause = lookupException.getCause(); + assertNotNull( "ComponentLookupException.getCause() is null", cause ); + if ( expected != null ) + { + assertSame( "cause should be same instance thrown from component", expected, cause ); + } + + // verify stack contains only the one component + List stack = lookupException.getComponentStack(); + assertEquals( "Component stack", 2, stack.size() ); + ComponentDescriptor failedDescriptor = stack.get( 0 ).getDescriptor(); + assertSame( "Failed component descriptor should be created component", exceptionalDescriptor, failedDescriptor ); + ComponentStackElement wrapperElement = stack.get( 1 ); + ComponentDescriptor wrapperDescriptor = wrapperElement.getDescriptor(); + assertSame( "Wrapper component descriptor should be looked-up component", requiresDescriptor, wrapperDescriptor ); + if ( requiresDescriptor.getRequirements().size() > 0 ) + { + assertSame( "Wrapper property", "component", wrapperElement.getProperty() ); + } + + return lookupException.getCause(); + } + } + + public static class ExceptionalComponent implements Startable + { + private String myName; + + public ExceptionalComponent() throws Exception + { + if ( constructorException != null ) + { + throw constructorException; + } + } + + public void setName( String name ) throws Exception + { + if ( setterException != null ) + { + throw setterException; + } + this.myName = name; + } + + public void start() throws StartingException + { + if ( startException instanceof RuntimeException ) + { + throw (RuntimeException) startException; + + } + if ( startException instanceof StartingException ) + { + throw (StartingException) startException; + + } + if ( startException != null ) + { + throw new StartingException( "", startException ); + } + } + + public void stop() throws StoppingException + { + } + + public String toString() + { + return myName; + } + } + + public static class RequiresComponent + { + private String myName; + public ExceptionalComponent component; + + public RequiresComponent() throws Exception + { + if ( lookupInConstructor ) + { + container.lookup( ExceptionalComponent.class ); + } + } + + public void setName( String name ) throws Exception + { + if ( lookupInSetter ) + { + container.lookup( ExceptionalComponent.class ); + } + this.myName = name; + } + + public String toString() + { + return myName; + } + } + + public static class TestRuntimeException extends RuntimeException + { + + public TestRuntimeException( String message ) + { + super( message ); + } + + } + + public static class TestCheckedException extends RuntimeException + { + + public TestCheckedException( String message ) + { + super( message ); + } + + } +} diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/component/collection/LiveListTest.java b/plexus-container-default/src/test/java/org/codehaus/plexus/component/collection/LiveListTest.java new file mode 100644 index 000000000..0d92785d3 --- /dev/null +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/component/collection/LiveListTest.java @@ -0,0 +1,471 @@ +package org.codehaus.plexus.component.collection; + +import com.google.common.collect.Iterators; +import junit.framework.TestCase; +import org.codehaus.plexus.DefaultPlexusContainer; +import org.codehaus.plexus.classworlds.realm.ClassRealm; +import org.codehaus.plexus.component.collections.LiveList; +import org.codehaus.plexus.component.repository.exception.ComponentRepositoryException; + +import java.util.Arrays; +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; +import java.util.List; + +public class LiveListTest extends TestCase +{ + private DefaultPlexusContainer container; + + protected void setUp() throws Exception + { + super.setUp(); + container = new DefaultPlexusContainer(); + } + + public void testEmptyList() throws Exception + { + LiveList list = new LiveList( container, Component.class, null, null ); + + Component unknown = new Component( "Unknown" ); + + // isEmpty and size + assertTrue( list.isEmpty() ); + assertEquals( 0, list.size() ); + + // get + assertGetOutOfBounds( list, 0 ); + assertGetOutOfBounds( list, -1 ); + assertGetOutOfBounds( list, 1 ); + + // contains + assertFalse( list.contains( unknown ) ); + assertFalse( list.containsAll( asList( unknown ) ) ); + + // indexOfs + assertEquals( -1, list.indexOf( unknown ) ); + assertEquals( -1, list.lastIndexOf( unknown ) ); + + // subList + assertTrue( list.subList( 0, 0 ).isEmpty() ); + assertSubListOutOfBounds( list, 0, 1 ); + assertSubListOutOfBounds( list, -1, 0 ); + + // iterators + assertTrue( Iterators.elementsEqual( emptyList().iterator(), list.iterator() ) ); + assertTrue( Iterators.elementsEqual( emptyList().listIterator(), list.listIterator() ) ); + assertTrue( Iterators.elementsEqual( emptyList().listIterator(), list.listIterator( 0 ) ) ); + assertListIteratorOutOfBounds( list, -1 ); + assertListIteratorOutOfBounds( list, 1 ); + + // toArrays + assertTrue( Arrays.equals( new Object[0], list.toArray() ) ); + assertTrue( Arrays.equals( new Object[0], list.toArray( new Component[0] ) ) ); + + // hashCode, equals, and toString + assertEquals( emptyList().hashCode(), list.hashCode() ); + assertTrue( list.equals( emptyList() ) ); + assertNotNull( list.toString() ); + assertTrue( list.toString().length() > 0 ); + + // List must be immutable + assertImmutable( list ); + } + + public void testSingleComponentList() throws Exception + { + LiveList list = new LiveList( container, Component.class, null, null ); + + Component zero = addComponent( "zero" ); + + // isEmpty and size + assertFalse( list.isEmpty() ); + assertEquals( 1, list.size() ); + + // get + assertSame( zero, list.get( 0 ) ); + assertGetOutOfBounds( list, -1 ); + assertGetOutOfBounds( list, 1 ); + + // contains + assertTrue( list.contains( zero ) ); + assertTrue( list.containsAll( asList( zero ) ) ); + + // indexOfs + assertEquals( 0, list.indexOf( zero ) ); + assertEquals( 0, list.lastIndexOf( zero ) ); + + // subList + assertEquals( asList( zero ), list.subList( 0, 1 ) ); + assertSubListOutOfBounds( list, 0, 2 ); + assertSubListOutOfBounds( list, -1, 0 ); + + // iterators + assertTrue( Iterators.elementsEqual( asList( zero ).iterator(), list.iterator() ) ); + assertTrue( Iterators.elementsEqual( asList( zero ).listIterator(), list.listIterator() ) ); + assertTrue( Iterators.elementsEqual( asList( zero ).listIterator(), list.listIterator( 0 ) ) ); + assertTrue( Iterators.elementsEqual( emptyList().listIterator(), list.listIterator( 1 ) ) ); + assertListIteratorOutOfBounds( list, -1 ); + assertListIteratorOutOfBounds( list, 2 ); + + // toArrays + assertTrue( Arrays.equals( new Object[]{zero}, list.toArray() ) ); + assertTrue( Arrays.equals( new Object[]{zero}, list.toArray( new Component[1] ) ) ); + + // hashCode, equals and toString + assertEquals( asList( zero ).hashCode(), list.hashCode() ); + assertTrue( list.equals( asList( zero ) ) ); + assertNotNull( list.toString() ); + assertTrue( list.toString().length() > 0 ); + + // List must be immutable + assertImmutable( list ); + } + + public void testUnsortedList() throws Exception + { + // components are in the order they are added to the container + LiveList list = new LiveList( container, Component.class, null, null ); + + Component zero = addComponent( "zero" ); + Component one = addComponent( "one" ); + Component two = addComponent( "two" ); + Component three = addComponent( "three" ); + Component four = addComponent( "four" ); + + assertZeroToFourList( asList( zero, one, two, three, four ), list ); + } + + public void testSortedListForwardInsertion() throws Exception + { + LiveList list = new LiveList( container, + Component.class, + asList( "zero", "one", "two", "three", "four" ), + null ); + + Component zero = addComponent( "zero" ); + Component one = addComponent( "one" ); + Component two = addComponent( "two" ); + Component three = addComponent( "three" ); + Component four = addComponent( "four" ); + Component five = addComponent( "five" ); + + assertZeroToFourList( asList( zero, one, two, three, four ), list ); + } + + public void testSortedListReverseInsertion() throws Exception + { + LiveList list = new LiveList( container, + Component.class, + asList( "zero", "one", "two", "three", "four" ), + null ); + + Component five = addComponent( "five" ); + Component four = addComponent( "four" ); + Component three = addComponent( "three" ); + Component two = addComponent( "two" ); + Component one = addComponent( "one" ); + Component zero = addComponent( "zero" ); + + assertZeroToFourList( asList( zero, one, two, three, four ), list ); + } + + public void testSortedListRandomInsertion() throws Exception + { + LiveList list = new LiveList( container, + Component.class, + asList( "zero", "one", "two", "three", "four" ), + null ); + + Component four = addComponent( "four" ); + Component zero = addComponent( "zero" ); + Component five = addComponent( "five" ); + Component two = addComponent( "two" ); + Component one = addComponent( "one" ); + Component three = addComponent( "three" ); + + assertZeroToFourList( asList( zero, one, two, three, four ), list ); + } + + public void testSortedListWithRelease() throws Exception + { + LiveList list = new LiveList( container, + Component.class, + asList( "zero", "one", "two", "three", "four" ), + null ); + + // add each component in a child realm + ClassRealm realmZero = container.createChildRealm( "zero" ); + Component zero = addComponent( "zero", realmZero ); + + ClassRealm realmFour = container.createChildRealm( "four" ); + Component four = addComponent( "four", realmFour ); + + ClassRealm realmTwo = container.createChildRealm( "two" ); + Component two = addComponent( "two", realmTwo ); + + ClassRealm realmOne = container.createChildRealm( "one" ); + Component one = addComponent( "one", realmOne ); + + ClassRealm realmThree = container.createChildRealm( "three" ); + Component three = addComponent( "three", realmThree ); + + // verify we have a normal 0-4 list now + assertZeroToFourList( asList( zero, one, two, three, four ), list ); + + // remove realm 4 which contains component 4 + container.removeComponentRealm( realmFour ); + + // verify we now have a 0-3 list + assertEquals( asList( zero, one, two, three ), list ); + + // remove realm 2 which contains component 2 + container.removeComponentRealm( realmTwo ); + + // verify we now have a 0,1,3 list + assertEquals( asList( zero, one, three ), list ); + + // remove realm 0 which contains component 0 + container.removeComponentRealm( realmZero ); + + // verify we now have a 1,3 list + assertEquals( asList( one, three ), list ); + + // add them back + zero = addComponent( "zero", realmZero ); + four = addComponent( "four", realmFour ); + two = addComponent( "two", realmTwo ); + + // verify we have a normal 0-4 list again + assertZeroToFourList( asList( zero, one, two, three, four ), list ); + } + + private void assertZeroToFourList( List expected, LiveList list ) + { + // get input values + assertEquals( 5, expected.size() ); + Component zero = expected.get( 0 ); + Component one = expected.get( 1 ); + Component two = expected.get( 2 ); + Component three = expected.get( 3 ); + Component four = expected.get( 4 ); + + // isEmpty and size + assertFalse( list.isEmpty() ); + assertEquals( 5, list.size() ); + + // get + assertSame( zero, list.get( 0 ) ); + assertSame( one, list.get( 1 ) ); + assertSame( two, list.get( 2 ) ); + assertSame( three, list.get( 3 ) ); + assertSame( four, list.get( 4 ) ); + assertGetOutOfBounds( list, -1 ); + assertGetOutOfBounds( list, 5 ); + + // contains + assertTrue( list.contains( zero ) ); + assertTrue( list.contains( two ) ); + assertTrue( list.contains( four ) ); + assertTrue( list.containsAll( asList( zero ) ) ); + assertTrue( list.containsAll( asList( zero, two, four ) ) ); + assertTrue( list.containsAll( expected ) ); + + // indexOfs + assertEquals( 0, list.indexOf( zero ) ); + assertEquals( 0, list.lastIndexOf( zero ) ); + assertEquals( 2, list.indexOf( two ) ); + assertEquals( 2, list.lastIndexOf( two ) ); + assertEquals( 4, list.indexOf( four ) ); + assertEquals( 4, list.lastIndexOf( four ) ); + + // subList + assertEquals( asList( zero ), list.subList( 0, 1 ) ); + assertEquals( asList( zero, one, two ), list.subList( 0, 3 ) ); + assertEquals( expected, list.subList( 0, 5 ) ); + assertSubListOutOfBounds( list, 0, 6 ); + assertSubListOutOfBounds( list, -1, 1 ); + + // iterators + assertTrue( Iterators.elementsEqual( expected.iterator(), list.iterator() ) ); + assertTrue( Iterators.elementsEqual( expected.listIterator(), list.listIterator() ) ); + assertTrue( Iterators.elementsEqual( expected.listIterator(), list.listIterator( 0 ) ) ); + assertTrue( Iterators.elementsEqual( expected.listIterator( 2 ), list.listIterator( 2 ) ) ); + assertTrue( Iterators.elementsEqual( emptyList().listIterator(), list.listIterator( 5 ) ) ); + assertListIteratorOutOfBounds( list, -1 ); + assertListIteratorOutOfBounds( list, 6 ); + + // toArrays + assertTrue( Arrays.equals( expected.toArray(), list.toArray() ) ); + assertTrue( Arrays.equals( expected.toArray( new Component[5] ), list.toArray( new Component[5] ) ) ); + + // hashCode, equals and toString + assertEquals( expected.hashCode(), list.hashCode() ); + assertTrue( list.equals( expected ) ); + assertNotNull( list.toString() ); + assertTrue( list.toString().length() > 0 ); + + // List must be immutable + assertImmutable( list ); + } + + private Component addComponent( String name ) + throws ComponentRepositoryException + { + return addComponent( name, container.getContainerRealm() ); + } + + private Component addComponent( String name, ClassRealm containerRealm ) + throws ComponentRepositoryException + { + Component component = new Component( name ); + container.addComponent( component, Component.class, name, containerRealm ); + return component; + } + + private static void assertGetOutOfBounds( List list, int index ) + { + try + { + list.get( index ); + fail( "Expected IndexOutOfBoundsException from list.get(" + index + ")" ); + } + catch ( IndexOutOfBoundsException expected ) + { + } + } + + private static void assertSubListOutOfBounds( List list, int fromIndex, int toIndex ) + { + try + { + list.subList( fromIndex, toIndex ); + fail( "Expected IndexOutOfBoundsException from list.subList(" + fromIndex + "," + toIndex + ")" ); + } + catch ( IndexOutOfBoundsException expected ) + { + } + } + + private static void assertListIteratorOutOfBounds( List list, int index ) + { + try + { + list.listIterator( index ); + fail( "Expected IndexOutOfBoundsException from list.listIterator(" + index + ")" ); + } + catch ( IndexOutOfBoundsException expected ) + { + } + } + + private static void assertImmutable( List list ) + { + Component component = new Component( "component" ); + + try + { + list.set( 0, component ); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + try + { + list.add( component ); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + try + { + list.add( 0, component ); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + try + { + list.addAll( asList( component ) ); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + try + { + list.addAll( 0, asList( component ) ); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + try + { + list.remove( component ); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + try + { + list.remove( 0 ); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + try + { + list.removeAll( asList( component ) ); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + try + { + list.retainAll( asList( component ) ); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + try + { + list.clear(); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + } + + public static class Component + { + public final String name; + + public Component( String name ) + { + this.name = name; + } + + public String toString() + { + return name; + } + } +} diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/component/collection/LiveMapTest.java b/plexus-container-default/src/test/java/org/codehaus/plexus/component/collection/LiveMapTest.java new file mode 100644 index 000000000..5363081e9 --- /dev/null +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/component/collection/LiveMapTest.java @@ -0,0 +1,345 @@ +package org.codehaus.plexus.component.collection; + +import static com.google.common.collect.Sets.newHashSet; +import junit.framework.TestCase; +import org.codehaus.plexus.DefaultPlexusContainer; +import org.codehaus.plexus.classworlds.realm.ClassRealm; +import org.codehaus.plexus.component.collections.LiveMap; +import org.codehaus.plexus.component.repository.exception.ComponentRepositoryException; + +import static java.util.Arrays.asList; +import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentMap; + +public class LiveMapTest extends TestCase +{ + private DefaultPlexusContainer container; + private Component unknown; + + protected void setUp() throws Exception + { + super.setUp(); + container = new DefaultPlexusContainer(); + unknown = new Component( "Unknown" ); + } + + public void testEmpty() throws Exception + { + + LiveMap map = new LiveMap( container, Component.class, null, null ); + + Map all = emptyMap(); + + // isEmpty and size + assertTrue( map.isEmpty() ); + assertEquals( 0, map.size() ); + + // get + assertNull( map.get( "unknown" ) ); + + // contains + assertFalse( map.containsKey( "unknown" ) ); + assertFalse( map.containsValue( unknown ) ); + + // keySet, values, entrySet + assertEquals( all.keySet(), map.keySet() ); + assertEquals( newHashSet( all.values() ), newHashSet( map.values() ) ); + assertEquals( all.entrySet(), map.entrySet() ); + + // hashCode, equals, and toString + assertEquals( emptyMap().hashCode(), map.hashCode() ); + assertTrue( map.equals( emptyMap() ) ); + assertNotNull( map.toString() ); + assertTrue( map.toString().length() > 0 ); + + // List must be immutable + assertImmutable( map ); + } + + public void testSingleComponent() throws Exception + { + + LiveMap map = new LiveMap( container, Component.class, null, null ); + + Component zero = addComponent( "zero" ); + Map all = singletonMap( "zero", zero ); + + // isEmpty and size + assertFalse( map.isEmpty() ); + assertEquals( 1, map.size() ); + + // get + assertSame( zero, map.get( "zero" ) ); + assertNull( map.get( "unknown" ) ); + + // contains + assertTrue( map.containsKey( "zero" ) ); + assertFalse( map.containsKey( "unknown" ) ); + assertTrue( map.containsValue( zero ) ); + assertFalse( map.containsValue( unknown ) ); + + // keySet, values, entrySet + assertEquals( all.keySet(), map.keySet() ); + assertEquals( newHashSet( all.values() ), newHashSet( map.values() ) ); + assertEquals( all.entrySet(), map.entrySet() ); + + // hashCode, equals and toString + assertEquals( all.hashCode(), map.hashCode() ); + assertTrue( map.equals( all ) ); + assertNotNull( map.toString() ); + assertTrue( map.toString().length() > 0 ); + + // List must be immutable + assertImmutable( map ); + } + + public void testAllHints() throws Exception + { + // components are in the order they are added to the container + LiveMap map = new LiveMap( container, Component.class, null, null ); + + Map all = new LinkedHashMap(); + all.put( "zero", addComponent( "zero" ) ); + all.put( "one", addComponent( "one" ) ); + all.put( "two", addComponent( "two" ) ); + all.put( "three", addComponent( "three" ) ); + all.put( "four", addComponent( "four" ) ); + + assertZeroToFour( all, map ); + } + + public void testSpecificHints() throws Exception + { + LiveMap map = new LiveMap( container, + Component.class, + asList( "zero", "one", "two", "three", "four" ), + null ); + + Map all = new LinkedHashMap(); + all.put( "zero", addComponent( "zero" ) ); + all.put( "one", addComponent( "one" ) ); + all.put( "two", addComponent( "two" ) ); + all.put( "three", addComponent( "three" ) ); + all.put( "four", addComponent( "four" ) ); + + Component five = addComponent( "five" ); + + assertZeroToFour( all, map ); + } + + public void testSpecificHintsWithRelease() throws Exception + { + LiveMap map = new LiveMap( container, + Component.class, + asList( "zero", "one", "two", "three", "four" ), + null ); + + Map all = new LinkedHashMap(); + + // add each component in a child realm + ClassRealm realmZero = container.createChildRealm( "zero" ); + all.put( "zero", addComponent( "zero", realmZero ) ); + + ClassRealm realmOne = container.createChildRealm( "one" ); + all.put( "one", addComponent( "one", realmOne ) ); + + ClassRealm realmTwo = container.createChildRealm( "two" ); + all.put( "two", addComponent( "two", realmTwo ) ); + + ClassRealm realmThree = container.createChildRealm( "three" ); + all.put( "three", addComponent( "three", realmThree ) ); + + ClassRealm realmFour = container.createChildRealm( "four" ); + all.put( "four", addComponent( "four", realmFour ) ); + + // verify we have a normal 0-4 list now + assertZeroToFour( all, map ); + + // remove realm 4 which contains component 4 + container.removeComponentRealm( realmFour ); + all.remove( "four" ); + + // verify we now have a 0-3 list + assertEquals( all, map ); + + // remove realm 2 which contains component 2 + container.removeComponentRealm( realmTwo ); + all.remove( "two" ); + + // verify we now have a 0,1,3 list + assertEquals( all, map ); + + // remove realm 0 which contains component 0 + container.removeComponentRealm( realmZero ); + all.remove( "zero" ); + + // verify we now have a 1,3 list + assertEquals( all, map ); + + // add them back + all.put( "zero", addComponent( "zero", realmZero ) ); + all.put( "four", addComponent( "four", realmFour ) ); + all.put( "two", addComponent( "two", realmTwo ) ); + + // verify we have a normal 0-4 list again + assertZeroToFour( all, map ); + } + + private void assertZeroToFour( Map expected, LiveMap map ) + { + // get input values + assertEquals( 5, expected.size() ); + Component zero = expected.get( "zero" ); + Component one = expected.get( "one" ); + Component two = expected.get( "two" ); + Component three = expected.get( "three" ); + Component four = expected.get( "four" ); + + // isEmpty and size + assertFalse( map.isEmpty() ); + assertEquals( 5, map.size() ); + + // get + assertSame( zero, map.get( "zero" ) ); + assertSame( one, map.get( "one" ) ); + assertSame( two, map.get( "two" ) ); + assertSame( three, map.get( "three" ) ); + assertSame( four, map.get( "four" ) ); + assertNull( map.get( "unknown" ) ); + + // contains + assertTrue( map.containsKey( "zero" ) ); + assertTrue( map.containsKey( "two" ) ); + assertTrue( map.containsKey( "four" ) ); + assertFalse( map.containsKey( "unknown" ) ); + assertTrue( map.containsValue( zero ) ); + assertTrue( map.containsValue( two ) ); + assertTrue( map.containsValue( four ) ); + assertFalse( map.containsValue( unknown ) ); + + // keySet, values, entrySet + assertEquals( expected.keySet(), map.keySet() ); + assertEquals( newHashSet( expected.values() ), newHashSet( map.values() ) ); + assertEquals( expected.entrySet(), map.entrySet() ); + + // hashCode, equals and toString + assertEquals( expected.hashCode(), map.hashCode() ); + assertTrue( map.equals( expected ) ); + assertNotNull( map.toString() ); + assertTrue( map.toString().length() > 0 ); + + // List must be immutable + assertImmutable( map ); + } + + private Component addComponent( String name ) + throws ComponentRepositoryException + { + return addComponent( name, container.getContainerRealm() ); + } + + private Component addComponent( String name, ClassRealm containerRealm ) + throws ComponentRepositoryException + { + Component component = new Component( name ); + container.addComponent( component, Component.class, name, containerRealm ); + return component; + } + + private static void assertImmutable( ConcurrentMap map ) + { + Component component = new Component( "component" ); + Map all = singletonMap( "component", component ); + + try + { + map.put( "component", component ); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + try + { + map.putIfAbsent( "component", component ); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + try + { + map.putAll( all ); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + try + { + map.remove( "component" ); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + try + { + map.remove( "component", component ); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + try + { + map.replace( "component", component, component ); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + try + { + map.replace( "component", component ); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + try + { + map.clear(); + fail( "Expected UnsupportedOperationException" ); + } + catch ( UnsupportedOperationException expected ) + { + } + + } + + public static class Component + { + public final String name; + + public Component( String name ) + { + this.name = name; + } + + public String toString() + { + return name; + } + } +} \ No newline at end of file diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/component/composition/AbstractCompositionResolverTest.java b/plexus-container-default/src/test/java/org/codehaus/plexus/component/composition/AbstractCompositionResolverTest.java index 93c2a34d4..d8ffc7b20 100644 --- a/plexus-container-default/src/test/java/org/codehaus/plexus/component/composition/AbstractCompositionResolverTest.java +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/component/composition/AbstractCompositionResolverTest.java @@ -19,6 +19,8 @@ import junit.framework.TestCase; import org.codehaus.plexus.component.repository.ComponentDescriptor; import org.codehaus.plexus.component.repository.io.PlexusTools; +import org.codehaus.plexus.classworlds.ClassWorld; +import org.codehaus.plexus.classworlds.realm.ClassRealm; import java.util.List; @@ -58,6 +60,7 @@ public void testSimpleComponentResolution() { String cc1 = "" + + " java.lang.String" + " c1" + " " + " " + @@ -71,21 +74,26 @@ public void testSimpleComponentResolution() String cc2 = "" + + " java.lang.String" + " c2" + ""; String cc3 = "" + + " java.lang.String" + " c3" + ""; + ClassWorld classWorld = new ClassWorld( "test", Thread.currentThread().getContextClassLoader() ); + ClassRealm realm = classWorld.getRealm( "test" ); + CompositionResolver compositionResolver = getCompositionResolver(); - ComponentDescriptor c1 = PlexusTools.buildComponentDescriptor( cc1 ); + ComponentDescriptor c1 = PlexusTools.buildComponentDescriptor( cc1, realm ); - ComponentDescriptor c2 = PlexusTools.buildComponentDescriptor( cc2 ); + ComponentDescriptor c2 = PlexusTools.buildComponentDescriptor( cc2, realm ); - ComponentDescriptor c3 = PlexusTools.buildComponentDescriptor( cc3 ); + ComponentDescriptor c3 = PlexusTools.buildComponentDescriptor( cc3, realm ); compositionResolver.addComponentDescriptor( c1 ); @@ -128,6 +136,7 @@ public void testComplexComponentResolution() { String cc1 = "" + + " java.lang.String" + " c1" + " " + " " + @@ -141,11 +150,13 @@ public void testComplexComponentResolution() String cc2 = "" + + " java.lang.String" + " c2" + ""; String cc3 = "" + + " java.lang.String" + " c3" + " " + " " + @@ -159,26 +170,30 @@ public void testComplexComponentResolution() String cc4 = "" + + " java.lang.String" + " c4" + ""; String cc5 = "" + + " java.lang.String" + " c5" + ""; + ClassWorld classWorld = new ClassWorld( "test", Thread.currentThread().getContextClassLoader() ); + ClassRealm realm = classWorld.getRealm( "test" ); CompositionResolver compositionResolver = getCompositionResolver(); - ComponentDescriptor c1 = PlexusTools.buildComponentDescriptor( cc1 ); + ComponentDescriptor c1 = PlexusTools.buildComponentDescriptor( cc1, realm ); - ComponentDescriptor c2 = PlexusTools.buildComponentDescriptor( cc2 ); + ComponentDescriptor c2 = PlexusTools.buildComponentDescriptor( cc2, realm ); - ComponentDescriptor c3 = PlexusTools.buildComponentDescriptor( cc3 ); + ComponentDescriptor c3 = PlexusTools.buildComponentDescriptor( cc3, realm ); - ComponentDescriptor c4 = PlexusTools.buildComponentDescriptor( cc4 ); + ComponentDescriptor c4 = PlexusTools.buildComponentDescriptor( cc4, realm ); - ComponentDescriptor c5 = PlexusTools.buildComponentDescriptor( cc5 ); + ComponentDescriptor c5 = PlexusTools.buildComponentDescriptor( cc5, realm ); compositionResolver.addComponentDescriptor( c1 ); diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/component/composition/ComponentRealmCompositionTest.java b/plexus-container-default/src/test/java/org/codehaus/plexus/component/composition/ComponentRealmCompositionTest.java index 318b32992..5737618bf 100644 --- a/plexus-container-default/src/test/java/org/codehaus/plexus/component/composition/ComponentRealmCompositionTest.java +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/component/composition/ComponentRealmCompositionTest.java @@ -35,20 +35,22 @@ public class ComponentRealmCompositionTest // private static final String PLUGIN_0_REALM = "plugin0Realm"; private static final String PLUGIN_1_REALM = "plugin1Realm"; + private ClassRealm classRealm0; + private ClassRealm classRealm1; protected void setUp() throws Exception { super.setUp(); // Create ClassRealm plugin0 with plugin0 -> A, plugin0 -> B - createClassRealm( PLUGIN_0_REALM, + classRealm0 = createClassRealm( PLUGIN_0_REALM, PLUGIN_0_JAR, COMPONENT_A_JAR, COMPONENT_B_JAR, ARCHIVER_JAR ); // Create ClassRealm plugin1 with plugin1 -> A, plugin1 -> C - createClassRealm( PLUGIN_1_REALM, + classRealm1 = createClassRealm( PLUGIN_1_REALM, PLUGIN_1_JAR, COMPONENT_A_JAR, COMPONENT_C_JAR, @@ -70,19 +72,29 @@ protected void setUp() throws Exception public void testCompositionWhereTheSameImplementationExistsInDifferentRealms() throws Exception { - // Plugin0 - getContainer().lookup( PLUGIN_0_ROLE ); - - - // Plugin1 - getContainer().lookup( PLUGIN_1_ROLE ); + ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); + try + { + // Plugin0 + Thread.currentThread().setContextClassLoader( classRealm0 ); + getContainer().lookup( PLUGIN_0_ROLE ); - // Plugin0(alt) - getContainer().lookup( PLUGIN_0_ROLE, "alt" ); + // Plugin1 + Thread.currentThread().setContextClassLoader( classRealm1 ); + getContainer().lookup( PLUGIN_1_ROLE ); + // Plugin0(alt) + Thread.currentThread().setContextClassLoader( classRealm0 ); + getContainer().lookup( PLUGIN_0_ROLE, "alt" ); - // Plugin1(alt) - getContainer().lookup( PLUGIN_1_ROLE, "alt" ); + // Plugin1(alt) + Thread.currentThread().setContextClassLoader( classRealm1 ); + getContainer().lookup( PLUGIN_1_ROLE, "alt" ); + } + finally + { + Thread.currentThread().setContextClassLoader( oldClassLoader ); + } } public void testThatASingletonComponentIntheCoreRealmWhenLookedUpInComponentRealmsYieldsTheSameInstance() @@ -93,36 +105,56 @@ public void testThatASingletonComponentIntheCoreRealmWhenLookedUpInComponentReal public void testMultiRealmLookupMap() throws Exception { - Map plugin0Map = getContainer().lookupMap( PLUGIN_0_ROLE ); - assertNotNull("plugin0Map is null", plugin0Map ); - assertNotNull("plugin0Map does not contain a DefaultPlugin0", plugin0Map.get( PLEXUS_DEFAULT_HINT)); - assertNotNull("plugin0Map does not contain a AltPlugin0", plugin0Map.get( "alt")); - assertEquals("Expected only 2 components in plugin0Map", 2, plugin0Map.size()); - - Map plugin1Map = getContainer().lookupMap( PLUGIN_1_ROLE ); - assertNotNull("plugin1Map is null", plugin1Map); - assertNotNull("plugin1Map does not contain a DefaultPlugin1", plugin1Map.get( PLEXUS_DEFAULT_HINT)); - assertNotNull("plugin1Map does not contain a AltPlugin1", plugin1Map.get( "alt")); - assertEquals("Expected only 2 components in plugin1Map", 2, plugin1Map.size()); - + ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); + try + { + Thread.currentThread().setContextClassLoader( classRealm0 ); + Map plugin0Map = getContainer().lookupMap( PLUGIN_0_ROLE ); + assertNotNull("plugin0Map is null", plugin0Map ); + assertNotNull("plugin0Map does not contain a DefaultPlugin0", plugin0Map.get( PLEXUS_DEFAULT_HINT)); + assertNotNull("plugin0Map does not contain a AltPlugin0", plugin0Map.get( "alt")); + assertEquals("Expected only 2 components in plugin0Map", 2, plugin0Map.size()); + + Thread.currentThread().setContextClassLoader( classRealm1 ); + Map plugin1Map = getContainer().lookupMap( PLUGIN_1_ROLE ); + assertNotNull("plugin1Map is null", plugin1Map); + assertNotNull("plugin1Map does not contain a DefaultPlugin1", plugin1Map.get( PLEXUS_DEFAULT_HINT)); + assertNotNull("plugin1Map does not contain a AltPlugin1", plugin1Map.get( "alt")); + assertEquals("Expected only 2 components in plugin1Map", 2, plugin1Map.size()); + Thread.currentThread().setContextClassLoader( null ); + } + finally + { + Thread.currentThread().setContextClassLoader( oldClassLoader ); + } } public void testMultiRealmLookupList() throws Exception { - List plugin0List = getContainer().lookupList( PLUGIN_0_ROLE ); - assertNotNull("plugin0List is null", plugin0List ); - Map plugin0Map = mapByClassSimpleName( plugin0List ); - assertNotNull("plugin0List does not contain a DefaultPlugin0", plugin0Map.get( "DefaultPlugin0")); - assertNotNull("plugin0List does not contain a AltPlugin0", plugin0Map.get( "AltPlugin0")); - assertEquals("Expected only 2 components in plugin0Map", 2, plugin0Map.size()); - - List plugin1List = getContainer().lookupList( PLUGIN_1_ROLE ); - assertNotNull("plugin1List is null", plugin1List ); - Map plugin1Map = mapByClassSimpleName( plugin1List ); - assertNotNull("plugin1List does not contain a DefaultPlugin1", plugin1Map.get( "DefaultPlugin1")); - assertNotNull("plugin1List does not contain a AltPlugin1", plugin1Map.get( "AltPlugin1")); - assertEquals("Expected only 2 components in plugin0Map", 2, plugin1Map.size()); + ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); + try + { + Thread.currentThread().setContextClassLoader( classRealm0 ); + List plugin0List = getContainer().lookupList( PLUGIN_0_ROLE ); + assertNotNull("plugin0List is null", plugin0List ); + Map plugin0Map = mapByClassSimpleName( plugin0List ); + assertNotNull("plugin0List does not contain a DefaultPlugin0", plugin0Map.get( "DefaultPlugin0")); + assertNotNull("plugin0List does not contain a AltPlugin0", plugin0Map.get( "AltPlugin0")); + assertEquals("Expected only 2 components in plugin0Map", 2, plugin0Map.size()); + + Thread.currentThread().setContextClassLoader( classRealm1 ); + List plugin1List = getContainer().lookupList( PLUGIN_1_ROLE ); + assertNotNull("plugin1List is null", plugin1List ); + Map plugin1Map = mapByClassSimpleName( plugin1List ); + assertNotNull("plugin1List does not contain a DefaultPlugin1", plugin1Map.get( "DefaultPlugin1")); + assertNotNull("plugin1List does not contain a AltPlugin1", plugin1Map.get( "AltPlugin1")); + assertEquals("Expected only 2 components in plugin0Map", 2, plugin1Map.size()); + } + finally + { + Thread.currentThread().setContextClassLoader( oldClassLoader ); + } } private ClassRealm createClassRealm(String id, String... jars) diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/component/discovery/ComponentDiscovererTest.java b/plexus-container-default/src/test/java/org/codehaus/plexus/component/discovery/ComponentDiscovererTest.java index 857b1a2bb..f806181fc 100644 --- a/plexus-container-default/src/test/java/org/codehaus/plexus/component/discovery/ComponentDiscovererTest.java +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/component/discovery/ComponentDiscovererTest.java @@ -25,6 +25,8 @@ import java.io.File; import java.util.List; +import java.util.Map; +import java.util.TreeMap; /** * @author Jason van Zyl @@ -49,20 +51,30 @@ public void testDefaultComponentDiscoverer() core.addURL( testClasses.toURL() ); - List componentSetDescriptors = componentDiscoverer.findComponents( new DefaultContext(), core ); + File classes = new File( getBasedir(), "target/classes" ); - assertEquals( 1, componentSetDescriptors.size() ); + core.addURL( classes.toURL() ); - assertEquals( ComponentSetDescriptor.class.getName(), componentSetDescriptors.get( 0 ).getClass().getName() ); + List componentSetDescriptors = componentDiscoverer.findComponents( new DefaultContext(), core ); - ComponentSetDescriptor componentSet = componentSetDescriptors.get( 0 ); + ComponentDescriptor componentDescriptor = byImplementation(componentSetDescriptors).get( "org.codehaus.plexus.component.discovery.DefaultDiscoveredComponent" ); - List> components = componentSet.getComponents(); + assertNotNull("componentDescriptor is null", componentDescriptor ); - ComponentDescriptor cd = components.get( 0 ); + assertEquals( "org.codehaus.plexus.component.discovery.DiscoveredComponent", componentDescriptor.getRole() ); - assertEquals( "org.codehaus.plexus.component.discovery.DiscoveredComponent", cd.getRole() ); + assertEquals( "org.codehaus.plexus.component.discovery.DefaultDiscoveredComponent", componentDescriptor.getImplementation() ); + } - assertEquals( "org.codehaus.plexus.component.discovery.DefaultDiscoveredComponent", cd.getImplementation() ); + private static Map> byImplementation(List descriptorSets) { + TreeMap> index = new TreeMap>(); + for ( ComponentSetDescriptor descriptorSet : descriptorSets ) + { + for ( ComponentDescriptor descriptor : descriptorSet.getComponents() ) + { + index.put(descriptor.getImplementation(), descriptor); + } + } + return index; } } diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/component/factory/DiscoveredComponentFactoryTest.java b/plexus-container-default/src/test/java/org/codehaus/plexus/component/factory/DiscoveredComponentFactoryTest.java index 95ec7b791..c91dbf2ec 100644 --- a/plexus-container-default/src/test/java/org/codehaus/plexus/component/factory/DiscoveredComponentFactoryTest.java +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/component/factory/DiscoveredComponentFactoryTest.java @@ -46,22 +46,29 @@ public void testShouldInstantiateComponentUsingFactoryDiscoveredInComponentsXml( private void lookupTestComponent( String factoryId ) throws Exception { - ComponentDescriptor descriptor = new ComponentDescriptor(); + ComponentDescriptor descriptor = new ComponentDescriptor(); descriptor.setComponentFactory( factoryId ); - descriptor.setRole( "role" ); + descriptor.setRole( TestRole.class.getName() ); descriptor.setRoleHint( "hint" ); - descriptor.setImplementation( "something interesting" ); + descriptor.setImplementation( DefaultTestRole.class.getName() ); getContainer().addComponentDescriptor( descriptor ); - Object component = lookup( "role", "hint" ); + // this will return a TestFactoryResultComponent due to the component factory set above + Object component = lookup( TestRole.class.getName(), "hint" ); assertTrue( component instanceof TestFactoryResultComponent ); assertEquals( factoryId, ( (TestFactoryResultComponent) component ).getFactoryId() ); } + + public interface TestRole { + } + + public class DefaultTestRole implements TestRole { + } } diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/component/registry/DefaultComponentRegistryTest.java b/plexus-container-default/src/test/java/org/codehaus/plexus/component/registry/DefaultComponentRegistryTest.java index 95e45bad8..d978a41e6 100644 --- a/plexus-container-default/src/test/java/org/codehaus/plexus/component/registry/DefaultComponentRegistryTest.java +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/component/registry/DefaultComponentRegistryTest.java @@ -14,12 +14,12 @@ public class DefaultComponentRegistryTest public void testConcurrentDisposeAndLookup() throws Exception { + final PlexusContainer plexus = new DefaultPlexusContainer(); + ComponentDescriptor descriptor = new ComponentDescriptor( - TestSynchronizedComponent.class ); + TestSynchronizedComponent.class, plexus.getContainerRealm() ); descriptor.setRole( TestSynchronizedComponent.class.getCanonicalName() ); descriptor.setImplementation( TestSynchronizedComponent.class.getCanonicalName() ); - - final PlexusContainer plexus = new DefaultPlexusContainer(); plexus.addComponentDescriptor( descriptor ); TestSynchronizedComponent component = plexus.lookup( TestSynchronizedComponent.class ); @@ -45,7 +45,7 @@ public synchronized TestSynchronizedComponent getComponent() { return component; } - }; + } LookupThread lookupThread = new LookupThread(); diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/component/repository/ComponentDescriptorTest.java b/plexus-container-default/src/test/java/org/codehaus/plexus/component/repository/ComponentDescriptorTest.java index e5d2391e8..c571d40ec 100644 --- a/plexus-container-default/src/test/java/org/codehaus/plexus/component/repository/ComponentDescriptorTest.java +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/component/repository/ComponentDescriptorTest.java @@ -18,6 +18,8 @@ import junit.framework.TestCase; import org.codehaus.plexus.component.repository.io.PlexusTools; +import org.codehaus.plexus.classworlds.ClassWorld; +import org.codehaus.plexus.classworlds.realm.ClassRealm; import java.util.List; @@ -36,6 +38,7 @@ public void testSimpleComponentResolution() { String cc1 = "" + + " java.lang.String" + " c1" + " role-hint" + " component-profile" + @@ -49,7 +52,10 @@ public void testSimpleComponentResolution() " " + ""; - ComponentDescriptor c1 = PlexusTools.buildComponentDescriptor( cc1 ); + ClassWorld classWorld = new ClassWorld( "test", Thread.currentThread().getContextClassLoader() ); + ClassRealm realm = classWorld.getRealm( "test" ); + + ComponentDescriptor c1 = PlexusTools.buildComponentDescriptor( cc1, realm ); assertEquals( "c1", c1.getRole() ); diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/component/repository/ComponentSetTest.java b/plexus-container-default/src/test/java/org/codehaus/plexus/component/repository/ComponentSetTest.java index bcfd3f6a6..d9bab9b3d 100644 --- a/plexus-container-default/src/test/java/org/codehaus/plexus/component/repository/ComponentSetTest.java +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/component/repository/ComponentSetTest.java @@ -18,6 +18,8 @@ import junit.framework.TestCase; import org.codehaus.plexus.component.repository.io.PlexusTools; +import org.codehaus.plexus.classworlds.ClassWorld; +import org.codehaus.plexus.classworlds.realm.ClassRealm; import java.util.List; @@ -38,6 +40,7 @@ public void testSimpleComponentResolution() "" + " " + " " + + " java.lang.String" + " c1" + " role-hint" + " component-profile" + @@ -60,7 +63,10 @@ public void testSimpleComponentResolution() " " + ""; - ComponentSetDescriptor cs = PlexusTools.buildComponentSet( PlexusTools.buildConfiguration( xml ) ); + ClassWorld classWorld = new ClassWorld( "test", Thread.currentThread().getContextClassLoader() ); + ClassRealm realm = classWorld.getRealm( "test" ); + + ComponentSetDescriptor cs = PlexusTools.buildComponentSet( PlexusTools.buildConfiguration( xml ), realm ); ComponentDescriptor c1 = cs.getComponents().get( 0 ); diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/test/DefaultLoadOnStartServiceWithRoleHint.java b/plexus-container-default/src/test/java/org/codehaus/plexus/test/DefaultLoadOnStartServiceWithRoleHint.java index d53006403..7e3f68f02 100644 --- a/plexus-container-default/src/test/java/org/codehaus/plexus/test/DefaultLoadOnStartServiceWithRoleHint.java +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/test/DefaultLoadOnStartServiceWithRoleHint.java @@ -25,6 +25,6 @@ * @version $Id$ */ public class DefaultLoadOnStartServiceWithRoleHint - implements LoadOnStartService + implements LoadOnStartServiceWithRoleHint, LoadOnStartService { } diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/test/PlexusContainerTest.java b/plexus-container-default/src/test/java/org/codehaus/plexus/test/PlexusContainerTest.java index 76021d154..ff548938d 100644 --- a/plexus-container-default/src/test/java/org/codehaus/plexus/test/PlexusContainerTest.java +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/test/PlexusContainerTest.java @@ -219,7 +219,7 @@ public void testMultipleSingletonComponentInstances() // Make sure the component is alive. assertNotNull( serviceC1 ); - assertTrue( serviceC1.started ); + assertTrue( serviceC1.started ); assertFalse( serviceC1.stopped ); @@ -420,26 +420,6 @@ public void testComponentCompositionWhereTargetFieldIsAPartialList() assertTrue( ( (Valve) valves.get( 0 ) ).getState() ); } - public void testComponentCompositionWhereTargetFieldAMapThatMustRetainTheOrderOfComponentsGivenASetOfRoleHints() - throws Exception - { - Pipeline pipeline = container.lookup( Pipeline.class, "chubby" ); - - Map valveMap = pipeline.getValveMap(); - - List valves = new ArrayList( valveMap.values() ); - - assertEquals( "Expecting three valves.", 4, valves.size() ); - - assertTrue( "Expecting valve one.", valves.get(0) instanceof ValveOne ); - - assertTrue( "Expecting valve two.", valves.get(1) instanceof ValveTwo ); - - assertTrue( "Expecting valve three.", valves.get(2) instanceof ValveThree ); - - assertTrue( "Expecting valve four.", valves.get(3) instanceof ValveFour ); - } - public void testLookupOfComponentThatShouldBeDiscovered() throws Exception { @@ -447,28 +427,28 @@ public void testLookupOfComponentThatShouldBeDiscovered() assertNotNull( discoveredComponent ); } - + public void testStartableComponentSnake() throws Exception { StartableComponent ca = container.lookup( StartableComponent.class, "A-snake" ); - + assertNotNull( ca ); - + ca.assertStartOrderCorrect(); - + container.dispose(); - + ca.assertStopOrderCorrect(); } - + public void testStartableComponentTree() throws Exception { StartableComponent ca = container.lookup( StartableComponent.class, "A-tree" ); - + assertNotNull( ca ); - + ca.assertStartOrderCorrect(); container.dispose(); @@ -486,7 +466,7 @@ public void testLookupCircularity() } catch ( ComponentLookupException e ) { - // todo actually test nested exception is as expected when + // todo actually test nested exception is as expected when } } } diff --git a/plexus-container-default/src/test/resources/org/codehaus/plexus/test/PlexusContainerTest.xml b/plexus-container-default/src/test/resources/org/codehaus/plexus/test/PlexusContainerTest.xml index c505daf58..192ace0a1 100644 --- a/plexus-container-default/src/test/resources/org/codehaus/plexus/test/PlexusContainerTest.xml +++ b/plexus-container-default/src/test/resources/org/codehaus/plexus/test/PlexusContainerTest.xml @@ -19,10 +19,10 @@ - - org.codehaus.plexus.test.discovery.PluginManager - org.codehaus.plexus.test.discovery.MockMavenPluginManager - + + + + @@ -46,7 +46,6 @@ org.codehaus.plexus.test.ServiceD org.codehaus.plexus.test.DefaultServiceD - poolable @@ -80,8 +79,8 @@ - org.codehaus.plexus.test.logging.LoggerManager - org.codehaus.plexus.test.logging.console.ConsoleLoggerManager + org.codehaus.plexus.logging.LoggerManager + org.codehaus.plexus.logging.console.ConsoleLoggerManager basic fatal @@ -253,7 +252,8 @@ - org.codehaus.plexus.test.ComponentMissingRequirements + org.codehaus.plexus.test.Component + ComponentMissingRequirements org.codehaus.plexus.test.DefaultComponent diff --git a/pom.xml b/pom.xml index 1ef78fbda..1c31e1198 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ Parent :: Plexus Containers plexus-component-annotations - plexus-component-metadata + plexus-component-javadoc plexus-container-default From 0f0a3930d55d9479a3cc6d9fea2e07e24f8e26d2 Mon Sep 17 00:00:00 2001 From: jvanzyl Date: Wed, 11 Feb 2009 19:10:25 +0000 Subject: [PATCH 10/31] o adding in changes --- .../repository/ComponentDescriptor.java | 56 ++++++++++++++++++- .../repository/ComponentRequirement.java | 38 +++++++++++-- 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/plexus-component-metadata/src/main/java/org/codehaus/plexus/component/repository/ComponentDescriptor.java b/plexus-component-metadata/src/main/java/org/codehaus/plexus/component/repository/ComponentDescriptor.java index 554365f3f..7ea4cc3e4 100644 --- a/plexus-component-metadata/src/main/java/org/codehaus/plexus/component/repository/ComponentDescriptor.java +++ b/plexus-component-metadata/src/main/java/org/codehaus/plexus/component/repository/ComponentDescriptor.java @@ -31,7 +31,7 @@ * @author Jason van Zyl * @author bob mcwhirter * @author Michal Maczka - * @version $Id: ComponentDescriptor.java 8048 2009-01-16 00:54:39Z jvanzyl $ + * @version $Id: ComponentDescriptor.java 8109 2009-02-11 18:20:25Z dain $ */ public class ComponentDescriptor { @@ -39,6 +39,8 @@ public class ComponentDescriptor private String role = null; + private Class roleClass; + private String roleHint = PlexusConstants.PLEXUS_DEFAULT_HINT; private String implementation; @@ -92,9 +94,11 @@ public ComponentDescriptor() { } - public ComponentDescriptor( Class implementationClass ) + public ComponentDescriptor( Class implementationClass, ClassRealm realm ) { this.implementationClass = implementationClass; + this.implementation = implementationClass.getName(); + this.realm = realm; } /** @@ -169,6 +173,33 @@ public String getRole() return role; } + public Class getRoleClass() + { + attemptRoleLoad(); + + if (roleClass == null) { + return (Class) Object.class; + } + return roleClass; + } + + private void attemptRoleLoad() + { + if ( roleClass == null && role != null && realm != null ) + { + try + { + roleClass = realm.loadClass( role ); + Thread.currentThread(); + } + catch ( Throwable ignored ) + { + Thread.currentThread(); + } + } + } + + /** * Sets the role of this component. * @@ -177,6 +208,21 @@ public String getRole() public void setRole( String role ) { this.role = role; + + // reload role class + roleClass = null; + attemptRoleLoad(); + } + + public void setRoleClass( Class roleClass ) + { + this.roleClass = roleClass; + + if (roleClass == null) { + role = null; + } else { + role = roleClass.getName(); + } } /** @@ -254,9 +300,11 @@ private void attemptImplementationLoad() try { implementationClass = realm.loadClass( implementation ); + Thread.currentThread(); } catch ( Throwable ignored ) { + Thread.currentThread(); } } } @@ -599,6 +647,10 @@ public void setRealm( ClassRealm realm ) // reload implementation class implementationClass = null; attemptImplementationLoad(); + + // reload role class + roleClass = null; + attemptRoleLoad(); } // Component identity established here! diff --git a/plexus-component-metadata/src/main/java/org/codehaus/plexus/component/repository/ComponentRequirement.java b/plexus-component-metadata/src/main/java/org/codehaus/plexus/component/repository/ComponentRequirement.java index 443385729..2af2ac8a0 100644 --- a/plexus-component-metadata/src/main/java/org/codehaus/plexus/component/repository/ComponentRequirement.java +++ b/plexus-component-metadata/src/main/java/org/codehaus/plexus/component/repository/ComponentRequirement.java @@ -22,18 +22,48 @@ * This represents a component this is required by another component. * * @author Michal Maczka - * @version $Id: ComponentRequirement.java 6965 2007-10-21 05:32:27Z jvanzyl $ + * @version $Id: ComponentRequirement.java 8109 2009-02-11 18:20:25Z dain $ */ public class ComponentRequirement { + private String fieldName; + private String role; private String roleHint = PlexusConstants.PLEXUS_DEFAULT_HINT; - private String fieldName; - private String fieldMappingType; - + + public ComponentRequirement() + { + } + + public ComponentRequirement( String fieldName, String role ) + { + this( fieldName, role, null ); + } + + public ComponentRequirement( String fieldName, String role, String roleHint ) + { + this.fieldName = fieldName; + this.role = role; + + if ( roleHint != null ) + { + this.roleHint = roleHint; + } + } + + public ComponentRequirement( String fieldName, Class type ) + { + this( fieldName, type.getName(), null ); + } + + public ComponentRequirement( String fieldName, Class type, String roleHint ) + { + this( fieldName, type.getName(), roleHint ); + } + /** * Returns the field name that this component requirement will inject. * @return the field name that this component requirement will inject From dce057d36fcba7bc54542217fd11f1c9f7e5bef2 Mon Sep 17 00:00:00 2001 From: dain Date: Wed, 11 Feb 2009 19:36:12 +0000 Subject: [PATCH 11/31] Removed exception printing --- .../org/codehaus/plexus/component/PlexusExceptionTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/component/PlexusExceptionTest.java b/plexus-container-default/src/test/java/org/codehaus/plexus/component/PlexusExceptionTest.java index 38b7abfb1..c0329c726 100644 --- a/plexus-container-default/src/test/java/org/codehaus/plexus/component/PlexusExceptionTest.java +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/component/PlexusExceptionTest.java @@ -126,8 +126,6 @@ private Throwable assertLookupFailed( Exception expected ) } catch ( ComponentLookupException lookupException ) { - lookupException.printStackTrace( ); - // verify cause is the same excption thrown from the component constructor Throwable cause = lookupException.getCause(); assertNotNull( "ComponentLookupException.getCause() is null", cause ); @@ -228,8 +226,6 @@ private Throwable assertNestedRequiresFailed( Exception expected ) } catch ( ComponentLookupException lookupException ) { - lookupException.printStackTrace( ); - // verify cause is the same excption thrown from the component constructor Throwable cause = lookupException.getCause(); assertNotNull( "ComponentLookupException.getCause() is null", cause ); From 36c89f30b611cc3a29146c80d1b0f9ae5a55dcc8 Mon Sep 17 00:00:00 2001 From: dain Date: Wed, 11 Feb 2009 19:36:40 +0000 Subject: [PATCH 12/31] Don't eat exceptions in test case --- .../src/main/java/org/codehaus/plexus/PlexusTestCase.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/PlexusTestCase.java b/plexus-container-default/src/main/java/org/codehaus/plexus/PlexusTestCase.java index f1c346480..2930f4bd7 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/PlexusTestCase.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/PlexusTestCase.java @@ -101,8 +101,7 @@ protected void setupContainer() } catch ( PlexusContainerException e ) { - e.printStackTrace(); - fail( "Failed to create plexus container." ); + throw new RuntimeException( "Failed to create plexus container.", e ); } } From 8af289a1606062bf28f19fa1e6d73b6022cadc5d Mon Sep 17 00:00:00 2001 From: jvanzyl Date: Thu, 12 Feb 2009 05:44:58 +0000 Subject: [PATCH 13/31] o turn back on the plexus-component-metadata module o in the case where the implementation class cannot be found we will assume this is an old hand-written descriptor that is wrong. just ignore it. --- .../plexus/metadata/merge/ComponentsXmlMerger.java | 3 ++- .../codehaus/plexus/metadata/merge/PlexusXmlMerger.java | 3 ++- .../plexus/metadata/merge/ComponentsXmlMergerTest.java | 2 +- .../component/discovery/DefaultComponentDiscoverer.java | 9 ++++++++- pom.xml | 2 +- 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/merge/ComponentsXmlMerger.java b/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/merge/ComponentsXmlMerger.java index abe03755f..7bbc35544 100644 --- a/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/merge/ComponentsXmlMerger.java +++ b/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/merge/ComponentsXmlMerger.java @@ -24,6 +24,7 @@ * SOFTWARE. */ +import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.metadata.merge.support.ComponentSetElement; import org.jdom.Document; @@ -31,10 +32,10 @@ * @author Rahul Thakur * @version $Id$ */ +@Component(role=Merger.class,hint="componentsXml") public class ComponentsXmlMerger extends AbstractMerger { - /** * @see Merger#merge(Document, Document) */ diff --git a/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/merge/PlexusXmlMerger.java b/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/merge/PlexusXmlMerger.java index e0d9d6ad2..20ddb95e0 100644 --- a/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/merge/PlexusXmlMerger.java +++ b/plexus-component-metadata/src/main/java/org/codehaus/plexus/metadata/merge/PlexusXmlMerger.java @@ -24,6 +24,7 @@ * SOFTWARE. */ +import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.metadata.merge.support.PlexusRootElement; import org.jdom.Document; @@ -33,10 +34,10 @@ * @author Rahul Thakur * @version $Id$ */ +@Component(role=Merger.class,hint="plexusXml") public class PlexusXmlMerger extends AbstractMerger { - /** * @see org.codehaus.plexus.metadata.merge.Merger#merge(org.jdom.Document, org.jdom.Document) */ diff --git a/plexus-component-metadata/src/test/java/org/codehaus/plexus/metadata/merge/ComponentsXmlMergerTest.java b/plexus-component-metadata/src/test/java/org/codehaus/plexus/metadata/merge/ComponentsXmlMergerTest.java index bca0c7533..dee5e9f47 100644 --- a/plexus-component-metadata/src/test/java/org/codehaus/plexus/metadata/merge/ComponentsXmlMergerTest.java +++ b/plexus-component-metadata/src/test/java/org/codehaus/plexus/metadata/merge/ComponentsXmlMergerTest.java @@ -50,7 +50,7 @@ public class ComponentsXmlMergerTest public void testBasic() throws Exception { - ComponentsXmlMerger merger = (ComponentsXmlMerger) lookup( Merger.class, "componentsXml" ); + Merger merger = lookup( Merger.class, "componentsXml" ); assertNotNull( merger ); } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscoverer.java index 49d280aae..1facd9fce 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscoverer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscoverer.java @@ -60,7 +60,14 @@ public ComponentSetDescriptor createComponentDescriptors( Reader componentDescri } catch ( PlexusConfigurationException e ) { - throw new PlexusConfigurationException( "Cannot process component descriptor: " + source, e ); + // This is not the most accurate of exceptions as the only real case where this exception + // will be thrown is when the implementation class of the component sited cannot be loaded. + // In the case where role and implementation classes do not exist then we just shouldn't + // create the component descriptor. All information should be taken from annotations which + // will be correct, so in the case we can't load the class it must be coming from and older + // hand written descriptor which is incorrect. + + continue; } componentDescriptor.setSource( source ); diff --git a/pom.xml b/pom.xml index 1c31e1198..1ef78fbda 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ Parent :: Plexus Containers plexus-component-annotations - + plexus-component-metadata plexus-component-javadoc plexus-container-default From c7e035c04724aef497a069a97ecd7795a5877fdf Mon Sep 17 00:00:00 2001 From: bentmann Date: Sat, 14 Feb 2009 14:27:11 +0000 Subject: [PATCH 14/31] o Replaced Shitty with Invoker to fix bogus test failures due to improper staging --- plexus-component-metadata/pom.xml | 26 ++++++++++---- .../src/it/descriptor-1/goals.txt | 1 - .../src/it/descriptor-1/pom.xml | 4 +-- .../src/it/descriptor-1/validate.groovy | 2 ++ plexus-component-metadata/src/it/settings.xml | 35 +++++++++++++++++++ .../src/it/source-encoding/goals.txt | 1 - .../src/it/source-encoding/latin-1/pom.xml | 2 +- .../src/it/source-encoding/utf-8/pom.xml | 2 +- .../src/it/source-encoding/validate.groovy | 2 ++ 9 files changed, 63 insertions(+), 12 deletions(-) delete mode 100644 plexus-component-metadata/src/it/descriptor-1/goals.txt create mode 100644 plexus-component-metadata/src/it/settings.xml delete mode 100644 plexus-component-metadata/src/it/source-encoding/goals.txt diff --git a/plexus-component-metadata/pom.xml b/plexus-component-metadata/pom.xml index 02e24221f..f975bf745 100644 --- a/plexus-component-metadata/pom.xml +++ b/plexus-component-metadata/pom.xml @@ -81,18 +81,32 @@ its - install - org.codehaus.mojo - shitty-maven-plugin - 1.0-alpha-3 + maven-invoker-plugin + 1.3 + + true + src/it + ${project.build.directory}/it + + annotation-processing-test/* + + setup + validate + ${project.build.directory}/local-repo + src/it/settings.xml + + clean + process-classes + + + integration-test - clean install - test + run diff --git a/plexus-component-metadata/src/it/descriptor-1/goals.txt b/plexus-component-metadata/src/it/descriptor-1/goals.txt deleted file mode 100644 index 5166d7634..000000000 --- a/plexus-component-metadata/src/it/descriptor-1/goals.txt +++ /dev/null @@ -1 +0,0 @@ -clean process-classes diff --git a/plexus-component-metadata/src/it/descriptor-1/pom.xml b/plexus-component-metadata/src/it/descriptor-1/pom.xml index 85a045624..16762d4b9 100644 --- a/plexus-component-metadata/src/it/descriptor-1/pom.xml +++ b/plexus-component-metadata/src/it/descriptor-1/pom.xml @@ -29,7 +29,7 @@ org.codehaus.plexus plexus-component-annotations - 1.0-alpha-17 + @project.version@ @@ -47,7 +47,7 @@ org.codehaus.plexus plexus-component-metadata - testing + @project.version@ diff --git a/plexus-component-metadata/src/it/descriptor-1/validate.groovy b/plexus-component-metadata/src/it/descriptor-1/validate.groovy index 392ea0f14..1567d6d78 100644 --- a/plexus-component-metadata/src/it/descriptor-1/validate.groovy +++ b/plexus-component-metadata/src/it/descriptor-1/validate.groovy @@ -24,3 +24,5 @@ assert file.exists() def componentSet = new XmlParser().parse(file) assert componentSet.components.component.size() == 2 + +return true; diff --git a/plexus-component-metadata/src/it/settings.xml b/plexus-component-metadata/src/it/settings.xml new file mode 100644 index 000000000..8890c8539 --- /dev/null +++ b/plexus-component-metadata/src/it/settings.xml @@ -0,0 +1,35 @@ + + + + + it-repo + + true + + + + local.central + @localRepositoryUrl@ + + true + + + true + + + + + + local.central + @localRepositoryUrl@ + + true + + + true + + + + + + diff --git a/plexus-component-metadata/src/it/source-encoding/goals.txt b/plexus-component-metadata/src/it/source-encoding/goals.txt deleted file mode 100644 index 5166d7634..000000000 --- a/plexus-component-metadata/src/it/source-encoding/goals.txt +++ /dev/null @@ -1 +0,0 @@ -clean process-classes diff --git a/plexus-component-metadata/src/it/source-encoding/latin-1/pom.xml b/plexus-component-metadata/src/it/source-encoding/latin-1/pom.xml index 4f927f4b6..02023bff2 100644 --- a/plexus-component-metadata/src/it/source-encoding/latin-1/pom.xml +++ b/plexus-component-metadata/src/it/source-encoding/latin-1/pom.xml @@ -31,7 +31,7 @@ org.codehaus.plexus plexus-component-metadata - testing + @project.version@ diff --git a/plexus-component-metadata/src/it/source-encoding/utf-8/pom.xml b/plexus-component-metadata/src/it/source-encoding/utf-8/pom.xml index aa8990058..1f7021481 100644 --- a/plexus-component-metadata/src/it/source-encoding/utf-8/pom.xml +++ b/plexus-component-metadata/src/it/source-encoding/utf-8/pom.xml @@ -31,7 +31,7 @@ org.codehaus.plexus plexus-component-metadata - testing + @project.version@ diff --git a/plexus-component-metadata/src/it/source-encoding/validate.groovy b/plexus-component-metadata/src/it/source-encoding/validate.groovy index d0ef60365..ea852de2e 100644 --- a/plexus-component-metadata/src/it/source-encoding/validate.groovy +++ b/plexus-component-metadata/src/it/source-encoding/validate.groovy @@ -9,3 +9,5 @@ assert utf8File.exists() def utf8Chars = new XmlParser().parse(utf8File).components.component.description.text() println utf8Chars assert "TEST-CHARS: \u00DF\u0131\u03A3\u042F\u05D0\u20AC".equals( utf8Chars ) + +return true; From 451a40903ced0a3df648623d81f65df616f4aeae Mon Sep 17 00:00:00 2001 From: hboutemy Date: Thu, 26 Feb 2009 21:04:04 +0000 Subject: [PATCH 15/31] updated site to use released plexus-stylus-skin 1.0 version --- plexus-component-javadoc/src/site/site.xml | 3 +-- src/site/site.xml | 7 +++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/plexus-component-javadoc/src/site/site.xml b/plexus-component-javadoc/src/site/site.xml index 8a875161b..daf0290a5 100644 --- a/plexus-component-javadoc/src/site/site.xml +++ b/plexus-component-javadoc/src/site/site.xml @@ -4,12 +4,11 @@ http://plexus.codehaus.org/ /images/plexus-logo.png - org.codehaus.plexus plexus-stylus-skin - 1.0-SNAPSHOT + 1.0 diff --git a/src/site/site.xml b/src/site/site.xml index 021894566..9c5e553c5 100644 --- a/src/site/site.xml +++ b/src/site/site.xml @@ -4,17 +4,16 @@ http://plexus.codehaus.org/ /images/plexus-logo.png - - + org.codehaus.plexus plexus-stylus-skin - 1.0-SNAPSHOT + 1.0 - + From c7b4e0b27d4886f8d4f1b2926b7a32cfb56c09fe Mon Sep 17 00:00:00 2001 From: hboutemy Date: Thu, 26 Feb 2009 21:08:42 +0000 Subject: [PATCH 16/31] updated pom schema location --- plexus-component-annotations/pom.xml | 2 +- plexus-component-javadoc/pom.xml | 2 +- plexus-component-metadata/pom.xml | 2 +- plexus-container-default/pom.xml | 2 +- .../src/test/test-component-builds/pom.xml | 4 ++-- pom.xml | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/plexus-component-annotations/pom.xml b/plexus-component-annotations/pom.xml index 4e5c38efb..03d0d2ad5 100644 --- a/plexus-component-annotations/pom.xml +++ b/plexus-component-annotations/pom.xml @@ -1,7 +1,7 @@ - + 4.0.0 org.codehaus.plexus diff --git a/plexus-component-javadoc/pom.xml b/plexus-component-javadoc/pom.xml index 5458333e9..b534c313d 100644 --- a/plexus-component-javadoc/pom.xml +++ b/plexus-component-javadoc/pom.xml @@ -1,6 +1,6 @@ - + 4.0.0 org.codehaus.plexus diff --git a/plexus-component-metadata/pom.xml b/plexus-component-metadata/pom.xml index f975bf745..168fcd40f 100644 --- a/plexus-component-metadata/pom.xml +++ b/plexus-component-metadata/pom.xml @@ -1,6 +1,6 @@ - + 4.0.0 diff --git a/plexus-container-default/pom.xml b/plexus-container-default/pom.xml index 2919839e0..166e7a92e 100644 --- a/plexus-container-default/pom.xml +++ b/plexus-container-default/pom.xml @@ -1,4 +1,4 @@ - + 4.0.0 org.codehaus.plexus diff --git a/plexus-container-default/src/test/test-component-builds/pom.xml b/plexus-container-default/src/test/test-component-builds/pom.xml index 0cb527882..d96b7e567 100644 --- a/plexus-container-default/src/test/test-component-builds/pom.xml +++ b/plexus-container-default/src/test/test-component-builds/pom.xml @@ -8,8 +8,8 @@ component-a component-b - component-c + component-c plugin0 - plugin1 + plugin1 diff --git a/pom.xml b/pom.xml index 1ef78fbda..978b2c3f8 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,5 @@ - + 4.0.0 org.codehaus.plexus From 0b382fae2c847dc8af162b6dc64d1a986bcf98f5 Mon Sep 17 00:00:00 2001 From: hboutemy Date: Thu, 26 Feb 2009 21:16:23 +0000 Subject: [PATCH 17/31] reformatted poms --- plexus-component-annotations/pom.xml | 3 +++ plexus-component-javadoc/pom.xml | 5 ++++ plexus-component-metadata/pom.xml | 4 ++-- plexus-container-default/pom.xml | 33 +++++++++++++++------------ pom.xml | 34 +++++++++++++++++----------- 5 files changed, 50 insertions(+), 29 deletions(-) diff --git a/plexus-component-annotations/pom.xml b/plexus-component-annotations/pom.xml index 03d0d2ad5..0a9d08b65 100644 --- a/plexus-component-annotations/pom.xml +++ b/plexus-component-annotations/pom.xml @@ -3,11 +3,14 @@ 4.0.0 + org.codehaus.plexus plexus-containers 1.0-beta-4-SNAPSHOT + plexus-component-annotations + Plexus :: Component Annotations diff --git a/plexus-component-javadoc/pom.xml b/plexus-component-javadoc/pom.xml index b534c313d..a20a2a63f 100644 --- a/plexus-component-javadoc/pom.xml +++ b/plexus-component-javadoc/pom.xml @@ -2,14 +2,18 @@ 4.0.0 + org.codehaus.plexus plexus-containers 1.0-beta-4-SNAPSHOT + plexus-component-javadoc + Plexus :: Javadoc Taglets 2007 + @@ -20,6 +24,7 @@ + default-tools.jar diff --git a/plexus-component-metadata/pom.xml b/plexus-component-metadata/pom.xml index 168fcd40f..cb078a229 100644 --- a/plexus-component-metadata/pom.xml +++ b/plexus-component-metadata/pom.xml @@ -25,8 +25,8 @@ plexus-component-annotations diff --git a/plexus-container-default/pom.xml b/plexus-container-default/pom.xml index 166e7a92e..49c0556d7 100644 --- a/plexus-container-default/pom.xml +++ b/plexus-container-default/pom.xml @@ -1,26 +1,16 @@ 4.0.0 + org.codehaus.plexus plexus-containers 1.0-beta-4-SNAPSHOT + plexus-container-default + Plexus :: Default Container - - - - maven-surefire-plugin - - once - - **/Test*.java - **/Abstract*.java - - - - - + org.codehaus.plexus @@ -39,4 +29,19 @@ google-collect + + + + + maven-surefire-plugin + + once + + **/Test*.java + **/Abstract*.java + + + + + diff --git a/pom.xml b/pom.xml index 978b2c3f8..182d76389 100644 --- a/pom.xml +++ b/pom.xml @@ -1,39 +1,39 @@ 4.0.0 + org.codehaus.plexus plexus 2.0.2 + plexus-containers 1.0-beta-4-SNAPSHOT - - 1.4 - 1.4.5 - 3.4 - UTF-8 - pom + Parent :: Plexus Containers + plexus-component-annotations plexus-component-metadata plexus-component-javadoc plexus-container-default + scm:svn:http://svn.codehaus.org/plexus/plexus-containers/trunk scm:svn:https://svn.codehaus.org/plexus/plexus-containers/trunk http://fisheye.codehaus.org/browse/plexus/plexus-containers/trunk - - - junit - junit - 3.8.2 - - + + + 1.4 + 1.4.5 + 3.4 + UTF-8 + + @@ -98,6 +98,14 @@ + + + junit + junit + 3.8.2 + + + From 123cc9c386f57b76670fc07e792446abc8266264 Mon Sep 17 00:00:00 2001 From: hboutemy Date: Thu, 26 Feb 2009 21:33:13 +0000 Subject: [PATCH 18/31] added reprintg profile --- pom.xml | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/pom.xml b/pom.xml index 182d76389..e5deed6ef 100644 --- a/pom.xml +++ b/pom.xml @@ -118,4 +118,76 @@ + + + + reporting + + + + org.apache.maven.plugins + maven-project-info-reports-plugin + 2.1 + + + org.apache.maven.plugins + maven-surefire-report-plugin + 2.4.3 + + + org.apache.maven.plugins + maven-checkstyle-plugin + 2.2 + + http://svn.apache.org/repos/asf/maven/plugins/trunk/maven-checkstyle-plugin/src/main/resources/config/maven_checks.xml + http://svn.apache.org/repos/asf/maven/plugins/trunk/maven-checkstyle-plugin/src/main/resources/config/maven-header.txt + + + + org.apache.maven.plugins + maven-pmd-plugin + 2.4 + + + http://svn.apache.org/repos/asf/maven/plugins/trunk/maven-pmd-plugin/src/main/resources/rulesets/maven.xml + + + + + org.codehaus.mojo + taglist-maven-plugin + 2.2 + + + org.apache.maven.plugins + maven-jxr-plugin + 2.1 + + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.5 + + ${project.build.sourceEncoding} + + http://java.sun.com/j2ee/1.4/docs/api + http://junit.sourceforge.net/javadoc/ + + + + + + javadoc + test-javadoc + + + + + + + + From 53c3c072450f1c25c8ab122783b684767a0454ab Mon Sep 17 00:00:00 2001 From: bentmann Date: Fri, 20 Mar 2009 19:36:36 +0000 Subject: [PATCH 19/31] [PLX-415] Configurator erroneously tries to instantiate composite component when configuration is empty --- .../builder/XBeanComponentBuilder.java | 11 ++-- .../composite/ObjectWithFieldsConverter.java | 2 +- .../AbstractComponentConfiguratorTest.java | 56 +++++++++++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/builder/XBeanComponentBuilder.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/builder/XBeanComponentBuilder.java index d4c66fe17..d1f1fc228 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/builder/XBeanComponentBuilder.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/builder/XBeanComponentBuilder.java @@ -211,10 +211,13 @@ public ObjectRecipe createObjectRecipe(ComponentDescriptor descriptor, ClassR for (PlexusConfiguration child : configuration.getChildren()) { String name = child.getName(); name = fromXML(name); - if (child.getChildCount() == 0) { - recipe.setProperty(name, child.getValue()); - } else { - recipe.setProperty(name, new PlexusConfigurationRecipe(child)); + if ( child.getChildCount() > 0 ) + { + recipe.setProperty( name, new PlexusConfigurationRecipe( child ) ); + } + else if ( StringUtils.isNotEmpty( child.getValue() ) ) + { + recipe.setProperty( name, child.getValue() ); } } } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/configurator/converters/composite/ObjectWithFieldsConverter.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/configurator/converters/composite/ObjectWithFieldsConverter.java index ecfc9f4bf..1847f32f1 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/configurator/converters/composite/ObjectWithFieldsConverter.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/configurator/converters/composite/ObjectWithFieldsConverter.java @@ -75,7 +75,7 @@ public Object fromConfiguration( ConverterLookup converterLookup, { Object retValue = fromExpression( configuration, expressionEvaluator, type ); - if ( retValue == null ) + if ( retValue == null && configuration.getChildCount() > 0 ) { try { diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/component/configurator/AbstractComponentConfiguratorTest.java b/plexus-container-default/src/test/java/org/codehaus/plexus/component/configurator/AbstractComponentConfiguratorTest.java index 1f5135d07..762b18a15 100644 --- a/plexus-container-default/src/test/java/org/codehaus/plexus/component/configurator/AbstractComponentConfiguratorTest.java +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/component/configurator/AbstractComponentConfiguratorTest.java @@ -608,4 +608,60 @@ public void testComponentConfigurationWhereFieldIsBadArray() } } + public void testComponentConfigurationWithEmptyContentForBasicField() + throws Exception + { + String xml = "" // + + "
" // + + "
"; + + PlexusConfiguration configuration = PlexusTools.buildConfiguration( "", new StringReader( xml ) ); + + DefaultComponent component = new DefaultComponent(); + + ComponentDescriptor descriptor = new ComponentDescriptor(); + + descriptor.setRole( "role" ); + + descriptor.setImplementation( component.getClass().getName() ); + + descriptor.setConfiguration( configuration ); + + ClassWorld classWorld = new ClassWorld(); + + ClassRealm realm = classWorld.newRealm( "test", getClass().getClassLoader() ); + + configureComponent( component, descriptor, realm ); + + assertEquals( null, component.getAddress() ); + } + + public void testComponentConfigurationWithEmptyContentForCompositeField() + throws Exception + { + String xml = "" // + + " " // + + ""; + + PlexusConfiguration configuration = PlexusTools.buildConfiguration( "", new StringReader( xml ) ); + + ComponentWithCompositeFields component = new ComponentWithCompositeFields(); + + ComponentDescriptor descriptor = new ComponentDescriptor(); + + descriptor.setRole( "role" ); + + descriptor.setImplementation( component.getClass().getName() ); + + descriptor.setConfiguration( configuration ); + + ClassWorld classWorld = new ClassWorld(); + + ClassRealm realm = classWorld.newRealm( "test", getClass().getClassLoader() ); + + configureComponent( component, descriptor, realm ); + + assertEquals( null, component.getThing() ); + } + } From ce0fe34ebb14d1a6fd981a22f70ab5da405b59d2 Mon Sep 17 00:00:00 2001 From: jvanzyl Date: Mon, 6 Apr 2009 22:40:31 +0000 Subject: [PATCH 20/31] o decouple the discovery listeners from the manager and mediate the events from the main container --- .../plexus/ContainerConfiguration.java | 9 +++ .../plexus/DefaultContainerConfiguration.java | 28 ++++++++++ .../plexus/DefaultPlexusContainer.java | 55 ++++++++++++++----- .../AbstractComponentDiscoverer.java | 38 ++++--------- .../discovery/ComponentDiscoverer.java | 4 -- .../discovery/ComponentDiscovererManager.java | 5 +- .../DefaultComponentDiscovererManager.java | 12 ---- .../PlexusXmlComponentDiscoverer.java | 14 +---- ...ializeComponentDiscovererManagerPhase.java | 2 - .../codehaus/plexus/PlexusTestCaseTest.java | 3 +- .../discovery/ComponentDiscovererTest.java | 2 - 11 files changed, 92 insertions(+), 80 deletions(-) diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/ContainerConfiguration.java b/plexus-container-default/src/main/java/org/codehaus/plexus/ContainerConfiguration.java index 47b48f313..0aebc17e2 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/ContainerConfiguration.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/ContainerConfiguration.java @@ -1,6 +1,7 @@ package org.codehaus.plexus; import java.net.URL; +import java.util.List; import java.util.Map; import org.codehaus.plexus.classworlds.ClassWorld; @@ -87,5 +88,13 @@ public interface ContainerConfiguration ContainerConfiguration setConfigurationSource( ConfigurationSource configurationSource ); ConfigurationSource getConfigurationSource(); + + ContainerConfiguration addComponentDiscoverer( Class clazz ); + + ContainerConfiguration addComponentDiscoveryListener( Class clazz ); + + List getComponentDiscoverers(); + + List getComponentDiscoveryListeners(); } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultContainerConfiguration.java b/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultContainerConfiguration.java index c44b8ffc6..ce8260730 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultContainerConfiguration.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultContainerConfiguration.java @@ -1,6 +1,8 @@ package org.codehaus.plexus; import java.net.URL; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import org.codehaus.plexus.classworlds.ClassWorld; @@ -53,6 +55,10 @@ public class DefaultContainerConfiguration private ConfigurationSource configurationSource; + private List componentDiscoverers = new ArrayList(); + + private List componentDiscoveryListeners = new ArrayList(); + public ContainerConfiguration setName( String name ) { this.name = name; @@ -301,5 +307,27 @@ public ContainerConfiguration setConfigurationSource( ConfigurationSource config public ConfigurationSource getConfigurationSource() { return configurationSource; + } + + public ContainerConfiguration addComponentDiscoverer( Class clazz ) + { + componentDiscoverers.add( clazz ); + return this; + } + + public ContainerConfiguration addComponentDiscoveryListener( Class clazz ) + { + componentDiscoveryListeners.add( clazz ); + return this; + } + + public List getComponentDiscoverers() + { + return componentDiscoverers; + } + + public List getComponentDiscoveryListeners() + { + return componentDiscoveryListeners; } } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultPlexusContainer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultPlexusContainer.java index 72f836141..85ceabbd4 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultPlexusContainer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultPlexusContainer.java @@ -34,6 +34,7 @@ import org.codehaus.plexus.classworlds.realm.NoSuchRealmException; import org.codehaus.plexus.component.discovery.ComponentDiscoverer; import org.codehaus.plexus.component.discovery.ComponentDiscovererManager; +import org.codehaus.plexus.component.discovery.ComponentDiscoveryEvent; import org.codehaus.plexus.component.discovery.ComponentDiscoveryListener; import org.codehaus.plexus.component.discovery.PlexusXmlComponentDiscoverer; import org.codehaus.plexus.component.factory.ComponentFactoryManager; @@ -292,6 +293,30 @@ private void construct( ContainerConfiguration c ) { IOUtil.close( configurationReader ); } + + for( Class clazz : c.getComponentDiscoverers() ) + { + try + { + ComponentDiscoverer cd = (ComponentDiscoverer) lookup( clazz ); + componentDiscovererManager.addComponentDiscoverer( cd ); + } + catch ( ComponentLookupException e ) + { + } + } + + for( Class clazz : c.getComponentDiscoveryListeners() ) + { + try + { + ComponentDiscoveryListener cdl = (ComponentDiscoveryListener) lookup( clazz ); + componentDiscovererManager.registerComponentDiscoveryListener( cdl ); + } + catch ( ComponentLookupException e ) + { + } + } } // ---------------------------------------------------------------------------- @@ -953,26 +978,30 @@ public void setLoggerManager( LoggerManager loggerManager ) public List> discoverComponents( ClassRealm realm ) throws PlexusConfigurationException, ComponentRepositoryException { - - List componentSets = new ArrayList(); - for ( ComponentDiscoverer componentDiscoverer : getComponentDiscovererManager().getComponentDiscoverers() ) - { - for ( ComponentSetDescriptor componentSet : componentDiscoverer.findComponents( getContext(), realm ) ) - { - componentSets.add(componentSet); - } - } + List componentSetDescriptors = new ArrayList(); List> discoveredComponentDescriptors = new ArrayList>(); - for ( ComponentSetDescriptor componentSet : componentSets ) + + for ( ComponentDiscoverer componentDiscoverer : getComponentDiscovererManager().getComponentDiscoverers() ) { - for ( ComponentDescriptor componentDescriptor : componentSet.getComponents() ) + for ( ComponentSetDescriptor componentSetDescriptor : componentDiscoverer.findComponents( getContext(), realm ) ) { - addComponentDescriptor( componentDescriptor ); + componentSetDescriptors.add(componentSetDescriptor); + + for( ComponentDescriptor componentDescriptor : componentSetDescriptor.getComponents() ) + { + addComponentDescriptor( componentDescriptor ); + + discoveredComponentDescriptors.add( componentDescriptor ); + } + + // Fire the event + ComponentDiscoveryEvent event = new ComponentDiscoveryEvent( componentSetDescriptor ); - discoveredComponentDescriptors.add( componentDescriptor ); + componentDiscovererManager.fireComponentDiscoveryEvent( event ); } } + return discoveredComponentDescriptors; } } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractComponentDiscoverer.java index 6354bfd79..8f4ea3aba 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractComponentDiscoverer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractComponentDiscoverer.java @@ -16,8 +16,16 @@ * limitations under the License. */ +import java.io.IOException; +import java.io.Reader; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; + import org.codehaus.plexus.classworlds.realm.ClassRealm; -import org.codehaus.plexus.component.repository.ComponentDescriptor; import org.codehaus.plexus.component.repository.ComponentSetDescriptor; import org.codehaus.plexus.configuration.PlexusConfigurationException; import org.codehaus.plexus.context.Context; @@ -26,15 +34,6 @@ import org.codehaus.plexus.util.InterpolationFilterReader; import org.codehaus.plexus.util.ReaderFactory; -import java.io.IOException; -import java.io.Reader; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.List; -import java.util.Collections; - /** * @author Jason van Zyl * @author Trygve Laugstøl @@ -43,27 +42,15 @@ public abstract class AbstractComponentDiscoverer implements ComponentDiscoverer { - private ComponentDiscovererManager manager; - // ---------------------------------------------------------------------- // Abstract methods // ---------------------------------------------------------------------- protected abstract String getComponentDescriptorLocation(); - protected abstract ComponentSetDescriptor createComponentDescriptors( Reader reader, - String source, ClassRealm realm ) + protected abstract ComponentSetDescriptor createComponentDescriptors( Reader reader, String source, ClassRealm realm ) throws PlexusConfigurationException; - // ---------------------------------------------------------------------- - // ComponentDiscoverer - // ---------------------------------------------------------------------- - - public void setManager( ComponentDiscovererManager manager ) - { - this.manager = manager; - } - public List findComponents( Context context, ClassRealm realm ) throws PlexusConfigurationException { @@ -110,11 +97,6 @@ public List findComponents( Context context, ClassRealm createComponentDescriptors( interpolationFilterReader, url.toString(), realm ); componentSetDescriptors.add( componentSetDescriptor ); - - // Fire the event - ComponentDiscoveryEvent event = new ComponentDiscoveryEvent( componentSetDescriptor ); - - manager.fireComponentDiscoveryEvent( event ); } catch ( IOException ex ) { diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ComponentDiscoverer.java index f0229f703..dd9157038 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ComponentDiscoverer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ComponentDiscoverer.java @@ -29,10 +29,6 @@ */ public interface ComponentDiscoverer { - static String ROLE = ComponentDiscoverer.class.getName(); - - void setManager( ComponentDiscovererManager manager ); - List findComponents( Context context, ClassRealm classRealm ) throws PlexusConfigurationException; } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ComponentDiscovererManager.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ComponentDiscovererManager.java index 53daba51c..0be53328f 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ComponentDiscovererManager.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ComponentDiscovererManager.java @@ -17,7 +17,6 @@ */ import java.util.List; -import java.util.Set; import java.util.Map; /** @@ -25,7 +24,7 @@ */ public interface ComponentDiscovererManager { - //void addComponentDiscoverer( ComponentDiscoverer componentDiscoverer ); + void addComponentDiscoverer( ComponentDiscoverer componentDiscoverer ); List getComponentDiscoverers(); @@ -37,6 +36,4 @@ public interface ComponentDiscovererManager void removeComponentDiscoveryListener( ComponentDiscoveryListener listener ); void fireComponentDiscoveryEvent( ComponentDiscoveryEvent event ); - - void initialize(); } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscovererManager.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscovererManager.java index 49efe2580..51e194ad0 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscovererManager.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscovererManager.java @@ -75,16 +75,4 @@ public void fireComponentDiscoveryEvent( ComponentDiscoveryEvent event ) listener.componentDiscovered( event ); } } - - // ---------------------------------------------------------------------- - // Lifecylce Management - // ---------------------------------------------------------------------- - - public synchronized void initialize() - { - for ( ComponentDiscoverer componentDiscoverer : componentDiscoverers ) - { - componentDiscoverer.setManager( this ); - } - } } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/PlexusXmlComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/PlexusXmlComponentDiscoverer.java index 2e0c63a01..a1ca9a714 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/PlexusXmlComponentDiscoverer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/PlexusXmlComponentDiscoverer.java @@ -43,13 +43,6 @@ public class PlexusXmlComponentDiscoverer private static final String PLEXUS_XML_RESOURCE = "META-INF/plexus/plexus.xml"; - private ComponentDiscovererManager manager; - - public void setManager( ComponentDiscovererManager manager ) - { - this.manager = manager; - } - public List findComponents( Context context, ClassRealm realm ) throws PlexusConfigurationException { @@ -59,10 +52,6 @@ public List findComponents( Context context, ClassRealm // Create ComponentDescriptors defined in PlexusConfiguration ComponentSetDescriptor componentSetDescriptor = createComponentDescriptors( configuration, realm ); - // Fire the event - ComponentDiscoveryEvent event = new ComponentDiscoveryEvent( componentSetDescriptor ); - manager.fireComponentDiscoveryEvent( event ); - return Collections.singletonList( componentSetDescriptor ); } @@ -133,8 +122,7 @@ private ComponentSetDescriptor createComponentDescriptors( PlexusConfiguration c if ( configuration != null ) { - PlexusConfiguration[] componentConfigurations = configuration.getChild( "components" ).getChildren( - "component" ); + PlexusConfiguration[] componentConfigurations = configuration.getChild( "components" ).getChildren( "component" ); for ( PlexusConfiguration componentConfiguration : componentConfigurations ) { diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/container/initialization/InitializeComponentDiscovererManagerPhase.java b/plexus-container-default/src/main/java/org/codehaus/plexus/container/initialization/InitializeComponentDiscovererManagerPhase.java index a96f89557..7ccf43b3f 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/container/initialization/InitializeComponentDiscovererManagerPhase.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/container/initialization/InitializeComponentDiscovererManagerPhase.java @@ -30,8 +30,6 @@ public void initializeCoreComponent( ContainerInitializationContext context ) { ComponentDiscovererManager componentDiscovererManager = context.getContainerConfiguration().getComponentDiscovererManager(); - componentDiscovererManager.initialize(); - context.getContainer().setComponentDiscovererManager( componentDiscovererManager ); for ( ComponentDiscoveryListener listener : componentDiscovererManager.getComponentDiscoveryListeners().keySet() ) diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/PlexusTestCaseTest.java b/plexus-container-default/src/test/java/org/codehaus/plexus/PlexusTestCaseTest.java index 7f932097c..281f4bf5c 100644 --- a/plexus-container-default/src/test/java/org/codehaus/plexus/PlexusTestCaseTest.java +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/PlexusTestCaseTest.java @@ -24,8 +24,7 @@ import java.io.File; /** - * @author Jason van Zyl - * @version $Id$ + * @author Jason van Zyl */ public class PlexusTestCaseTest extends TestCase diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/component/discovery/ComponentDiscovererTest.java b/plexus-container-default/src/test/java/org/codehaus/plexus/component/discovery/ComponentDiscovererTest.java index f806181fc..c754f8f89 100644 --- a/plexus-container-default/src/test/java/org/codehaus/plexus/component/discovery/ComponentDiscovererTest.java +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/component/discovery/ComponentDiscovererTest.java @@ -41,8 +41,6 @@ public void testDefaultComponentDiscoverer() { ComponentDiscoverer componentDiscoverer = new DefaultComponentDiscoverer(); - componentDiscoverer.setManager( new DefaultComponentDiscovererManager() ); - ClassWorld classWorld = new ClassWorld(); ClassRealm core = classWorld.newRealm( "core" ); From aca9775d6ad87e935164efda5e9a35c6d1ad00e5 Mon Sep 17 00:00:00 2001 From: jvanzyl Date: Tue, 7 Apr 2009 19:48:06 +0000 Subject: [PATCH 21/31] o clean up the discovery mechanism and merging the code to find the resource based component discoverers --- .../plexus/DefaultPlexusContainer.java | 28 +--- .../AbstractComponentDiscoverer.java | 10 +- .../discovery/ComponentDiscoverer.java | 1 - .../discovery/ComponentDiscoveryListener.java | 9 +- .../discovery/DefaultComponentDiscoverer.java | 3 +- .../PlexusXmlComponentDiscoverer.java | 134 ++++-------------- .../ResourceBasedComponentDiscoverer.java | 13 ++ .../component/repository/io/PlexusTools.java | 3 - .../org/codehaus/plexus/DiscoverySystem.java | 35 +++++ .../plexus/PlexusDiscoveryTestCase.java | 17 +++ .../plexus/TestComponentDiscoverer.java | 19 +++ .../TestComponentDiscoveryListener.java | 12 ++ 12 files changed, 133 insertions(+), 151 deletions(-) create mode 100644 plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ResourceBasedComponentDiscoverer.java create mode 100644 plexus-container-default/src/test/java/org/codehaus/plexus/DiscoverySystem.java create mode 100644 plexus-container-default/src/test/java/org/codehaus/plexus/PlexusDiscoveryTestCase.java create mode 100644 plexus-container-default/src/test/java/org/codehaus/plexus/TestComponentDiscoverer.java create mode 100644 plexus-container-default/src/test/java/org/codehaus/plexus/TestComponentDiscoveryListener.java diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultPlexusContainer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultPlexusContainer.java index 85ceabbd4..506c3e941 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultPlexusContainer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultPlexusContainer.java @@ -643,12 +643,7 @@ protected void initializePhases( ContainerConfiguration containerConfiguration ) { ContainerInitializationPhase[] initPhases = containerConfiguration.getInitializationPhases(); - ContainerInitializationContext initializationContext = new ContainerInitializationContext( - this, - classWorld, - containerRealm, - configuration, - containerConfiguration ); + ContainerInitializationContext initializationContext = new ContainerInitializationContext( this, classWorld, containerRealm, configuration, containerConfiguration ); for ( ContainerInitializationPhase phase : initPhases ) { @@ -755,33 +750,20 @@ public void removeComponentDescriptorListener( ComponentDescriptorListener li // Configuration // ---------------------------------------------------------------------- - // TODO: put this in a separate helper class and turn into a component if possible, too big. - + // This is the default PlexusXmlDiscovery + protected void initializeConfiguration( ContainerConfiguration c ) - throws PlexusConfigurationException, - ContextException, - IOException + throws PlexusConfigurationException, ContextException, IOException { // We need an empty plexus configuration for merging. This is a function of removing the // plexus-boostrap.xml file. configuration = new XmlPlexusConfiguration( "plexus" ); - PlexusXmlComponentDiscoverer discoverer = new PlexusXmlComponentDiscoverer(); - - PlexusConfiguration plexusConfiguration = discoverer.discoverConfiguration( getContext(), containerRealm ); - - if ( plexusConfiguration != null ) - { - configuration = PlexusConfigurationMerger.merge( plexusConfiguration, configuration ); - } - if ( configurationReader != null ) { // User userConfiguration - PlexusConfiguration userConfiguration = PlexusTools.buildConfiguration( - "", - getInterpolationConfigurationReader( configurationReader ) ); + PlexusConfiguration userConfiguration = PlexusTools.buildConfiguration( "", getInterpolationConfigurationReader( configurationReader ) ); // Merger of bootstrapConfiguration and user userConfiguration diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractComponentDiscoverer.java index 8f4ea3aba..4753484e0 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractComponentDiscoverer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractComponentDiscoverer.java @@ -37,7 +37,6 @@ /** * @author Jason van Zyl * @author Trygve Laugstøl - * @version $Id$ */ public abstract class AbstractComponentDiscoverer implements ComponentDiscoverer @@ -73,8 +72,7 @@ public List findComponents( Context context, ClassRealm } catch ( IOException e ) { - throw new PlexusConfigurationException( "Unable to retrieve resources for: " + - getComponentDescriptorLocation() + " in class realm: " + realm.getId() ); + throw new PlexusConfigurationException( "Unable to retrieve resources for: " + getComponentDescriptorLocation() + " in class realm: " + realm.getId() ); } for ( URL url : Collections.list( resources )) @@ -90,11 +88,9 @@ public List findComponents( Context context, ClassRealm reader = ReaderFactory.newXmlReader( conn.getInputStream() ); - InterpolationFilterReader interpolationFilterReader = - new InterpolationFilterReader( reader, new ContextMapAdapter( context ) ); + InterpolationFilterReader interpolationFilterReader = new InterpolationFilterReader( reader, new ContextMapAdapter( context ) ); - ComponentSetDescriptor componentSetDescriptor = - createComponentDescriptors( interpolationFilterReader, url.toString(), realm ); + ComponentSetDescriptor componentSetDescriptor = createComponentDescriptors( interpolationFilterReader, url.toString(), realm ); componentSetDescriptors.add( componentSetDescriptor ); } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ComponentDiscoverer.java index dd9157038..9cb248a07 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ComponentDiscoverer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ComponentDiscoverer.java @@ -25,7 +25,6 @@ /** * @author Jason van Zyl - * @version $Id$ */ public interface ComponentDiscoverer { diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ComponentDiscoveryListener.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ComponentDiscoveryListener.java index b9113ca29..bfbd9a75f 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ComponentDiscoveryListener.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ComponentDiscoveryListener.java @@ -17,8 +17,7 @@ */ /** - * A listener which responds in some way to component discovery by a - * PlexusContainer. + * A listener which responds in some way to component discovery by a PlexusContainer. */ public interface ComponentDiscoveryListener { @@ -27,10 +26,4 @@ public interface ComponentDiscoveryListener * @param event the event that signals what components have been discovered */ void componentDiscovered( ComponentDiscoveryEvent event ); - - /** - * Returns this listener's unique key. - * @return this listener's unique key - */ - String getId(); } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscoverer.java index 1facd9fce..40a172fee 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscoverer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscoverer.java @@ -48,8 +48,7 @@ public ComponentSetDescriptor createComponentDescriptors( Reader componentDescri List> componentDescriptors = new ArrayList>(); - PlexusConfiguration[] componentConfigurations = - componentDescriptorConfiguration.getChild( "components" ).getChildren( "component" ); + PlexusConfiguration[] componentConfigurations = componentDescriptorConfiguration.getChild( "components" ).getChildren( "component" ); for ( PlexusConfiguration componentConfiguration : componentConfigurations ) { diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/PlexusXmlComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/PlexusXmlComponentDiscoverer.java index a1ca9a714..cd625903c 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/PlexusXmlComponentDiscoverer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/PlexusXmlComponentDiscoverer.java @@ -2,124 +2,44 @@ /* * Copyright 2001-2006 Codehaus Foundation. - * - * Licensed 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. + * + * Licensed 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. */ +import java.io.Reader; +import java.util.Arrays; + import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.codehaus.plexus.component.repository.ComponentDescriptor; import org.codehaus.plexus.component.repository.ComponentSetDescriptor; import org.codehaus.plexus.component.repository.io.PlexusTools; import org.codehaus.plexus.configuration.PlexusConfiguration; import org.codehaus.plexus.configuration.PlexusConfigurationException; -import org.codehaus.plexus.configuration.PlexusConfigurationMerger; -import org.codehaus.plexus.context.Context; -import org.codehaus.plexus.context.ContextMapAdapter; -import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.util.InterpolationFilterReader; -import org.codehaus.plexus.util.ReaderFactory; - -import java.io.IOException; -import java.io.Reader; -import java.net.URL; -import java.util.Arrays; -import java.util.Enumeration; -import java.util.List; -import java.util.Collections; public class PlexusXmlComponentDiscoverer - implements ComponentDiscoverer + extends AbstractComponentDiscoverer { - - private static final String PLEXUS_XML_RESOURCE = "META-INF/plexus/plexus.xml"; - - public List findComponents( Context context, ClassRealm realm ) - throws PlexusConfigurationException - { - // Load the PlexusConfiguration - PlexusConfiguration configuration = discoverConfiguration( context, realm ); - - // Create ComponentDescriptors defined in PlexusConfiguration - ComponentSetDescriptor componentSetDescriptor = createComponentDescriptors( configuration, realm ); - - return Collections.singletonList( componentSetDescriptor ); - } - - public PlexusConfiguration discoverConfiguration( Context context, ClassRealm realm ) - throws PlexusConfigurationException + public String getComponentDescriptorLocation() { - Enumeration resources; - try - { - // We don't always want to scan parent realms. For plexus - // testcase, most components are in the root classloader so that needs to be scanned, - // but for child realms, we don't. - if ( realm.getParentRealm() != null ) - { - resources = realm.findRealmResources( PLEXUS_XML_RESOURCE ); - } - else - { - resources = realm.findResources( PLEXUS_XML_RESOURCE ); - } - } - catch ( IOException e ) - { - throw new PlexusConfigurationException( "Error retrieving configuration resources: " + PLEXUS_XML_RESOURCE + " from class realm: " + realm.getId(), e ); - } - - PlexusConfiguration configuration = null; - for ( URL url : Collections.list( resources ) ) - { - Reader reader = null; - try - { - reader = ReaderFactory.newXmlReader( url.openStream() ); - - ContextMapAdapter contextAdapter = new ContextMapAdapter( context ); - - InterpolationFilterReader interpolationFilterReader = new InterpolationFilterReader( reader, - contextAdapter ); - - PlexusConfiguration discoveredConfig = PlexusTools.buildConfiguration( url.toExternalForm(), interpolationFilterReader ); - - if ( configuration == null ) - { - configuration = discoveredConfig; - } - else - { - configuration = PlexusConfigurationMerger.merge( configuration, discoveredConfig ); - } - } - catch ( IOException ex ) - { - throw new PlexusConfigurationException( "Error reading configuration from: " + url.toExternalForm(), ex ); - } - finally - { - IOUtil.close( reader ); - } - } - - return configuration; + return "META-INF/plexus/plexus.xml"; } - private ComponentSetDescriptor createComponentDescriptors( PlexusConfiguration configuration, ClassRealm realm ) + @Override + protected ComponentSetDescriptor createComponentDescriptors( Reader reader, String source, ClassRealm realm ) throws PlexusConfigurationException { ComponentSetDescriptor componentSetDescriptor = new ComponentSetDescriptor(); + PlexusConfiguration configuration = PlexusTools.buildConfiguration( source, reader ); + if ( configuration != null ) { PlexusConfiguration[] componentConfigurations = configuration.getChild( "components" ).getChildren( "component" ); @@ -127,16 +47,19 @@ private ComponentSetDescriptor createComponentDescriptors( PlexusConfiguration c for ( PlexusConfiguration componentConfiguration : componentConfigurations ) { ComponentDescriptor componentDescriptor; + try { componentDescriptor = PlexusTools.buildComponentDescriptor( componentConfiguration, realm ); - if (componentDescriptor == null) continue; + + if ( componentDescriptor == null ) + { + continue; + } } catch ( PlexusConfigurationException e ) { - throw new PlexusConfigurationException( - "Cannot build component descriptor from resource found in:\n" + - Arrays.asList( realm.getURLs() ), e ); + throw new PlexusConfigurationException( "Cannot build component descriptor from resource found in:\n" + Arrays.asList( realm.getURLs() ), e ); } componentDescriptor.setComponentType( "plexus" ); @@ -145,11 +68,8 @@ private ComponentSetDescriptor createComponentDescriptors( PlexusConfiguration c componentSetDescriptor.addComponentDescriptor( componentDescriptor ); } - - // TODO: read and store the dependencies } return componentSetDescriptor; } - } diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ResourceBasedComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ResourceBasedComponentDiscoverer.java new file mode 100644 index 000000000..2626ef846 --- /dev/null +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/ResourceBasedComponentDiscoverer.java @@ -0,0 +1,13 @@ +package org.codehaus.plexus.component.discovery; + +/** + * Component discoverer which relies on the metadata for the components residing in a resource that + * can be found in a classloader. + * + * @author jvanzyl + */ +public interface ResourceBasedComponentDiscoverer + extends ComponentDiscoverer +{ + String getComponentDescriptorLocation(); +} diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/io/PlexusTools.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/io/PlexusTools.java index 185b82e17..9f471587a 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/io/PlexusTools.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/repository/io/PlexusTools.java @@ -36,9 +36,6 @@ /** * @author Jason van Zyl - * @version $Id$ - * @todo these are all really tools for dealing with xml configurations so they - * should be packaged as such. */ public class PlexusTools { diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/DiscoverySystem.java b/plexus-container-default/src/test/java/org/codehaus/plexus/DiscoverySystem.java new file mode 100644 index 000000000..9d4da44df --- /dev/null +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/DiscoverySystem.java @@ -0,0 +1,35 @@ +package org.codehaus.plexus; + +import java.util.List; + +import org.codehaus.plexus.classworlds.realm.ClassRealm; +import org.codehaus.plexus.component.discovery.ComponentDiscoverer; +import org.codehaus.plexus.component.discovery.ComponentDiscovererManager; +import org.codehaus.plexus.component.discovery.ComponentDiscoveryEvent; +import org.codehaus.plexus.component.discovery.ComponentDiscoveryListener; +import org.codehaus.plexus.component.repository.ComponentSetDescriptor; +import org.codehaus.plexus.configuration.PlexusConfigurationException; +import org.codehaus.plexus.context.Context; + +public class DiscoverySystem + implements ComponentDiscoverer, ComponentDiscoveryListener +{ + public List findComponents( Context context, ClassRealm classRealm ) + throws PlexusConfigurationException + { + return null; + } + + public void setManager( ComponentDiscovererManager manager ) + { + } + + public void componentDiscovered( ComponentDiscoveryEvent event ) + { + } + + public String getId() + { + return null; + } +} diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/PlexusDiscoveryTestCase.java b/plexus-container-default/src/test/java/org/codehaus/plexus/PlexusDiscoveryTestCase.java new file mode 100644 index 000000000..5cdaf747b --- /dev/null +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/PlexusDiscoveryTestCase.java @@ -0,0 +1,17 @@ +package org.codehaus.plexus; + +public class PlexusDiscoveryTestCase + extends PlexusTestCase +{ + public void testTheAdditionOfAComponentDiscoverer() + throws Exception + { + + } + + protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration ) + { + //containerConfiguration.addComponentDiscoverer( ); + //containerConfiguration.addComponentDiscoveryListener( ); + } +} diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/TestComponentDiscoverer.java b/plexus-container-default/src/test/java/org/codehaus/plexus/TestComponentDiscoverer.java new file mode 100644 index 000000000..1bd747600 --- /dev/null +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/TestComponentDiscoverer.java @@ -0,0 +1,19 @@ +package org.codehaus.plexus; + +import java.util.List; + +import org.codehaus.plexus.classworlds.realm.ClassRealm; +import org.codehaus.plexus.component.discovery.ComponentDiscoverer; +import org.codehaus.plexus.component.repository.ComponentSetDescriptor; +import org.codehaus.plexus.configuration.PlexusConfigurationException; +import org.codehaus.plexus.context.Context; + +public class TestComponentDiscoverer + implements ComponentDiscoverer +{ + public List findComponents( Context context, ClassRealm classRealm ) + throws PlexusConfigurationException + { + return null; + } +} diff --git a/plexus-container-default/src/test/java/org/codehaus/plexus/TestComponentDiscoveryListener.java b/plexus-container-default/src/test/java/org/codehaus/plexus/TestComponentDiscoveryListener.java new file mode 100644 index 000000000..2f5a71be7 --- /dev/null +++ b/plexus-container-default/src/test/java/org/codehaus/plexus/TestComponentDiscoveryListener.java @@ -0,0 +1,12 @@ +package org.codehaus.plexus; + +import org.codehaus.plexus.component.discovery.ComponentDiscoveryEvent; +import org.codehaus.plexus.component.discovery.ComponentDiscoveryListener; + +public class TestComponentDiscoveryListener + implements ComponentDiscoveryListener +{ + public void componentDiscovered( ComponentDiscoveryEvent event ) + { + } +} From 8634038459a23ed6c443d2c70ee76cb12a8f4611 Mon Sep 17 00:00:00 2001 From: jvanzyl Date: Tue, 7 Apr 2009 22:08:37 +0000 Subject: [PATCH 22/31] o some notes on how the discovery should work --- ...> AbstractResourceBasedComponentDiscoverer.java} | 13 ++++--------- .../AnnotationBasedComponentDiscoverer.java | 11 +++++++++++ .../discovery/DefaultComponentDiscoverer.java | 3 +-- .../discovery/PlexusXmlComponentDiscoverer.java | 2 +- 4 files changed, 17 insertions(+), 12 deletions(-) rename plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/{AbstractComponentDiscoverer.java => AbstractResourceBasedComponentDiscoverer.java} (87%) create mode 100644 plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AnnotationBasedComponentDiscoverer.java diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractResourceBasedComponentDiscoverer.java similarity index 87% rename from plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractComponentDiscoverer.java rename to plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractResourceBasedComponentDiscoverer.java index 4753484e0..5ca8a5c55 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractComponentDiscoverer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractResourceBasedComponentDiscoverer.java @@ -34,17 +34,15 @@ import org.codehaus.plexus.util.InterpolationFilterReader; import org.codehaus.plexus.util.ReaderFactory; +//TODO: this should be a default strategy of searching through classloaders. a discoverer should really not have to be +// concerned finding a particular resource and how to turn it into a set of component descriptors. + /** * @author Jason van Zyl - * @author Trygve Laugstøl */ -public abstract class AbstractComponentDiscoverer +public abstract class AbstractResourceBasedComponentDiscoverer implements ComponentDiscoverer { - // ---------------------------------------------------------------------- - // Abstract methods - // ---------------------------------------------------------------------- - protected abstract String getComponentDescriptorLocation(); protected abstract ComponentSetDescriptor createComponentDescriptors( Reader reader, String source, ClassRealm realm ) @@ -58,9 +56,6 @@ public List findComponents( Context context, ClassRealm Enumeration resources; try { - // We don't always want to scan parent realms. For plexus - // testcase, most components are in the root classloader so that needs to be scanned, - // but for child realms, we don't. if ( realm.getParentRealm() != null ) { resources = realm.findRealmResources( getComponentDescriptorLocation() ); diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AnnotationBasedComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AnnotationBasedComponentDiscoverer.java new file mode 100644 index 000000000..1fbfbd4b4 --- /dev/null +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AnnotationBasedComponentDiscoverer.java @@ -0,0 +1,11 @@ +package org.codehaus.plexus.component.discovery; + +/** + * Component discoverer which relies on annotation information to define component definitions. + * + * @author jvanzyl + */ +public interface AnnotationBasedComponentDiscoverer + extends ComponentDiscoverer +{ +} diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscoverer.java index 40a172fee..a975a90ec 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscoverer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/DefaultComponentDiscoverer.java @@ -29,10 +29,9 @@ /** * @author Jason van Zyl - * @version $Id$ */ public class DefaultComponentDiscoverer - extends AbstractComponentDiscoverer + extends AbstractResourceBasedComponentDiscoverer { public String getComponentDescriptorLocation() { diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/PlexusXmlComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/PlexusXmlComponentDiscoverer.java index cd625903c..778911312 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/PlexusXmlComponentDiscoverer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/PlexusXmlComponentDiscoverer.java @@ -25,7 +25,7 @@ import org.codehaus.plexus.configuration.PlexusConfigurationException; public class PlexusXmlComponentDiscoverer - extends AbstractComponentDiscoverer + extends AbstractResourceBasedComponentDiscoverer { public String getComponentDescriptorLocation() { From 612e79c1226c25bbee7016e076cffc0508be7302 Mon Sep 17 00:00:00 2001 From: jvanzyl Date: Tue, 7 Apr 2009 22:43:51 +0000 Subject: [PATCH 23/31] o classworlds will take care of looking in the parent. --- .../AbstractResourceBasedComponentDiscoverer.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractResourceBasedComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractResourceBasedComponentDiscoverer.java index 5ca8a5c55..40a46aa0b 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractResourceBasedComponentDiscoverer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractResourceBasedComponentDiscoverer.java @@ -54,16 +54,10 @@ public List findComponents( Context context, ClassRealm List componentSetDescriptors = new ArrayList(); Enumeration resources; + try { - if ( realm.getParentRealm() != null ) - { - resources = realm.findRealmResources( getComponentDescriptorLocation() ); - } - else - { - resources = realm.findResources( getComponentDescriptorLocation() ); - } + resources = realm.findResources( getComponentDescriptorLocation() ); } catch ( IOException e ) { From 7321094b4ec4ac4c3123c2dddd32bcb67777b0bf Mon Sep 17 00:00:00 2001 From: jvanzyl Date: Thu, 23 Apr 2009 14:48:07 +0000 Subject: [PATCH 24/31] o moving the trunk to 1.1 and i'm going to take the 1.0-beta-3.0.6 and use that as the basis for 1.0. i can't get trunk working in Maven so we'll call that 1.1 and when that works we'll switch over to using that. right now 1.0-beta-3.0.6 is very stable. --- plexus-component-annotations/pom.xml | 2 +- plexus-component-javadoc/pom.xml | 86 -------- plexus-component-javadoc/src/it/basic/pom.xml | 47 ----- .../basic/src/main/java/plexus/test/App.java | 63 ------ .../src/it/basic/verify.bsh | 57 ------ plexus-component-javadoc/src/it/settings.xml | 35 ---- .../plexus/javadoc/AbstractPlexusTaglet.java | 188 ------------------ .../plexus/javadoc/PlexusComponentTaglet.java | 139 ------------- .../javadoc/PlexusConfigurationTaglet.java | 127 ------------ .../javadoc/PlexusRequirementTaglet.java | 139 ------------- .../org/codehaus/plexus/javadoc/package.html | 35 ---- .../src/site/apt/index.apt | 37 ---- .../src/site/apt/usage.apt | 77 ------- plexus-component-javadoc/src/site/site.xml | 25 --- plexus-component-metadata/pom.xml | 2 +- plexus-container-default/pom.xml | 2 +- .../plexus/DefaultPlexusContainer.java | 8 +- ...tractResourceBasedComponentDiscoverer.java | 1 + pom.xml | 3 +- 19 files changed, 11 insertions(+), 1062 deletions(-) delete mode 100644 plexus-component-javadoc/pom.xml delete mode 100644 plexus-component-javadoc/src/it/basic/pom.xml delete mode 100644 plexus-component-javadoc/src/it/basic/src/main/java/plexus/test/App.java delete mode 100644 plexus-component-javadoc/src/it/basic/verify.bsh delete mode 100644 plexus-component-javadoc/src/it/settings.xml delete mode 100644 plexus-component-javadoc/src/main/java/org/codehaus/plexus/javadoc/AbstractPlexusTaglet.java delete mode 100644 plexus-component-javadoc/src/main/java/org/codehaus/plexus/javadoc/PlexusComponentTaglet.java delete mode 100644 plexus-component-javadoc/src/main/java/org/codehaus/plexus/javadoc/PlexusConfigurationTaglet.java delete mode 100644 plexus-component-javadoc/src/main/java/org/codehaus/plexus/javadoc/PlexusRequirementTaglet.java delete mode 100755 plexus-component-javadoc/src/main/javadoc/org/codehaus/plexus/javadoc/package.html delete mode 100644 plexus-component-javadoc/src/site/apt/index.apt delete mode 100644 plexus-component-javadoc/src/site/apt/usage.apt delete mode 100644 plexus-component-javadoc/src/site/site.xml diff --git a/plexus-component-annotations/pom.xml b/plexus-component-annotations/pom.xml index 0a9d08b65..882a4ed28 100644 --- a/plexus-component-annotations/pom.xml +++ b/plexus-component-annotations/pom.xml @@ -7,7 +7,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-4-SNAPSHOT + 1.1-SNAPSHOT plexus-component-annotations diff --git a/plexus-component-javadoc/pom.xml b/plexus-component-javadoc/pom.xml deleted file mode 100644 index a20a2a63f..000000000 --- a/plexus-component-javadoc/pom.xml +++ /dev/null @@ -1,86 +0,0 @@ - - - - 4.0.0 - - - org.codehaus.plexus - plexus-containers - 1.0-beta-4-SNAPSHOT - - - plexus-component-javadoc - - Plexus :: Javadoc Taglets - 2007 - - - - - maven-javadoc-plugin - - - maven-jxr-plugin - - - - - - - default-tools.jar - - - java.vendor - Sun Microsystems Inc. - - - - - com.sun - tools - 1.4.2 - system - ${java.home}/../lib/tools.jar - - - - - integration-tests - - - maven.test.skip - !true - - - - - - maven-invoker-plugin - 1.3 - - true - ${project.build.directory}/it - setup - verify - ${project.build.directory}/local-repo - src/it/settings.xml - - clean - javadoc:javadoc - - - - - integration-test - - install - run - - - - - - - - - diff --git a/plexus-component-javadoc/src/it/basic/pom.xml b/plexus-component-javadoc/src/it/basic/pom.xml deleted file mode 100644 index 30987679c..000000000 --- a/plexus-component-javadoc/src/it/basic/pom.xml +++ /dev/null @@ -1,47 +0,0 @@ - - - - 4.0.0 - - org.codehaus.plexus.plexus-javadoc - test - 1.0-SNAPSHOT - jar - - 2008 - Test Plexus Tags - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.3 - - ${basedir}/target/site/apidocs - true - private - UTF-8 - UTF-8 - UTF-8 - - - org.codehaus.plexus.javadoc.PlexusComponentTaglet - - - org.codehaus.plexus.javadoc.PlexusConfigurationTaglet - - - org.codehaus.plexus.javadoc.PlexusRequirementTaglet - - - - org.codehaus.plexus - plexus-component-javadoc - @project.version@ - - - - - - diff --git a/plexus-component-javadoc/src/it/basic/src/main/java/plexus/test/App.java b/plexus-component-javadoc/src/it/basic/src/main/java/plexus/test/App.java deleted file mode 100644 index bb62fc1c2..000000000 --- a/plexus-component-javadoc/src/it/basic/src/main/java/plexus/test/App.java +++ /dev/null @@ -1,63 +0,0 @@ -package plexus.test; - - -/* - * 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. - */ - -/** - * App class - * - * @author Vincent Siveton - * @version 1.0 - * @plexus.component role="org.codehaus.plexus.test.App" role-hint="app" - */ -public class App -{ - - /** - * @plexus.configuration default-value="someone" - */ - private String name; - - /** - * @plexus.requirement - * role-hint="foo" - */ - private App app; - - /** - * The main method - * - * @param args an array of strings that contains the arguments - */ - public static void main( String[] args ) - { - System.out.println( "Sample Application." ); - } - - /** - * Sample method that prints out the parameter string. - * - * @param str The string value to be printed. - */ - protected void sampleMethod( String str ) - { - System.out.println( str ); - } -} diff --git a/plexus-component-javadoc/src/it/basic/verify.bsh b/plexus-component-javadoc/src/it/basic/verify.bsh deleted file mode 100644 index c579b55cb..000000000 --- a/plexus-component-javadoc/src/it/basic/verify.bsh +++ /dev/null @@ -1,57 +0,0 @@ -import java.io.*; -import java.util.*; -import java.util.regex.*; - -import org.codehaus.plexus.util.*; - -setAccessibility( true ); - -File docsDir = new File( basedir, "target/site/apidocs" ); -System.out.println( "Checking for existence of: " + docsDir ); -if ( !docsDir.exists() ) -{ - throw new FileNotFoundException( docsDir + " was not created" ); -} - -File docFile = new File( docsDir, "plexus/test/App.html" ); -System.out.println( "Checking for existence of: " + docFile ); -if ( !docFile.isFile() ) -{ - throw new FileNotFoundException( docFile + " was not created" ); -} - -String html = FileUtils.fileRead( docFile, "UTF-8" ).replaceAll( "(\r\n)|(\r)|(\n)", "" ); - -System.out.println( "Checking for documentation of Plexus component" ); -String plexusComponent = "
plexus component:
" - + "
" + "
" - + "
role:
" - + "
\"org.codehaus.plexus.test.app\"
" - + "
role-hint:
" + "
\"app\"
" - + "
"; -if ( html.toLowerCase( Locale.ENGLISH ).indexOf( ( plexusComponent ).toLowerCase( Locale.ENGLISH ) ) < 0 ) -{ - throw new IllegalStateException( "Plexus component doc wrong" ); -} - -System.out.println( "Checking for documentation of Plexus configuration" ); -String plexusConfig = "
plexus configuration:
" - + "
" - + "
default-value:
" - + "
\"someone\"
"; -if ( html.toLowerCase( Locale.ENGLISH ).indexOf( ( plexusConfig ).toLowerCase( Locale.ENGLISH ) ) < 0 ) -{ - throw new IllegalStateException( "Plexus configuration doc wrong" ); -} - -System.out.println( "Checking for documentation of Plexus requirement" ); -String plexusRequirement = "
plexus requirement:
" - + "
" - + "
role-hint:
\"foo\"
" - + "
"; -if ( html.toLowerCase( Locale.ENGLISH ).indexOf( ( plexusRequirement ).toLowerCase( Locale.ENGLISH ) ) < 0 ) -{ - throw new IllegalStateException( "Plexus requirement doc wrong" ); -} - -return true; diff --git a/plexus-component-javadoc/src/it/settings.xml b/plexus-component-javadoc/src/it/settings.xml deleted file mode 100644 index 8890c8539..000000000 --- a/plexus-component-javadoc/src/it/settings.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - it-repo - - true - - - - local.central - @localRepositoryUrl@ - - true - - - true - - - - - - local.central - @localRepositoryUrl@ - - true - - - true - - - - - - diff --git a/plexus-component-javadoc/src/main/java/org/codehaus/plexus/javadoc/AbstractPlexusTaglet.java b/plexus-component-javadoc/src/main/java/org/codehaus/plexus/javadoc/AbstractPlexusTaglet.java deleted file mode 100644 index 33f936e2a..000000000 --- a/plexus-component-javadoc/src/main/java/org/codehaus/plexus/javadoc/AbstractPlexusTaglet.java +++ /dev/null @@ -1,188 +0,0 @@ -package org.codehaus.plexus.javadoc; - -/* - * 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. - */ - -import java.util.Enumeration; -import java.util.StringTokenizer; - -import javax.swing.text.AttributeSet; -import javax.swing.text.MutableAttributeSet; -import javax.swing.text.SimpleAttributeSet; - -import com.sun.javadoc.Tag; -import com.sun.tools.doclets.Taglet; - -/** - * Abstract Taglet for Plexus tags. - * - * @see - * http://plexus.codehaus.org/guides/developer-guide/appendices/javadoc-tags-reference.html - * - * @author Vincent Siveton - * @version $Id$ - */ -public abstract class AbstractPlexusTaglet - implements Taglet -{ - /** {@inheritDoc} */ - public String toString( Tag tag ) - { - if ( tag == null ) - { - return null; - } - - String tagText = tag.text(); - MutableAttributeSet att = getAttributes( tagText ); - - StringBuffer sb = new StringBuffer(); - if ( ( att == null ) || ( att.getAttributeCount() == 0 ) ) - { - sb.append( "
" ).append( getHeader() ).append( "
" ); - } - else - { - sb.append( "
" ).append( getHeader() ).append( ":
" ); - sb.append( "
" ); - - appendPlexusTag( sb, att ); - - sb.append( "
" ); - } - - return sb.toString(); - } - - /** {@inheritDoc} */ - public String toString( Tag[] tags ) - { - if ( tags.length == 0 ) - { - return null; - } - - boolean hasParameters = false; - for ( int i = 0; i < tags.length; i++ ) - { - String tagText = tags[i].text(); - MutableAttributeSet att = getAttributes( tagText ); - if ( att != null ) - { - hasParameters = att.getAttributeCount() > 0; - } - } - - StringBuffer sb = new StringBuffer(); - sb.append( "
" ).append( getHeader() ).append( ( hasParameters ? ":" : "" ) ).append( "
" ); - sb.append( "
" ); - for ( int i = 0; i < tags.length; i++ ) - { - if ( i > 0 ) - { - sb.append( ", " ); - } - - String tagText = tags[i].text(); - appendPlexusTag( sb, getAttributes( tagText ) ); - } - - sb.append( "
" ); - - return sb.toString(); - } - - /** - * @return the header to display - */ - public abstract String getHeader(); - - /** - * @param text the Tag text returned by {@link Tag#text()} - * @return a MutableAttributeSet or null if text was null - */ - private MutableAttributeSet getAttributes( String text ) - { - if ( text == null || text.trim().length() == 0 ) - { - return null; - } - - MutableAttributeSet att = new SimpleAttributeSet(); - - StringTokenizer token = new StringTokenizer( text, " " ); - while ( token.hasMoreTokens() ) - { - String nextToken = token.nextToken(); - - StringTokenizer token2 = new StringTokenizer( nextToken, "=" ); - if ( token2.countTokens() != 2 ) - { - System.err.println( "The annotation '" + getName() + "' has a wrong Plexus annotations: " + text ); - continue; - } - - String name = token2.nextToken(); - String value = token2.nextToken(); - - att.addAttribute( name, value ); - } - - return att; - } - - /** - * Append the wanted display in the javadoc. - * - * @param sb - * @param att - */ - private static void appendPlexusTag( StringBuffer sb, MutableAttributeSet att ) - { - if ( att == null ) - { - return; - } - - if ( att.getAttributeCount() > 0 ) - { - sb.append( "
" ); - - Enumeration names = att.getAttributeNames(); - - while ( names.hasMoreElements() ) - { - Object key = names.nextElement(); - Object value = att.getAttribute( key ); - - if ( value instanceof AttributeSet ) - { - // ignored - } - else - { - sb.append( "
" ).append( key ).append( ":
" ); - sb.append( "
" ).append( value ).append( "
" ); - } - } - - sb.append( "
" ); - } - } -} diff --git a/plexus-component-javadoc/src/main/java/org/codehaus/plexus/javadoc/PlexusComponentTaglet.java b/plexus-component-javadoc/src/main/java/org/codehaus/plexus/javadoc/PlexusComponentTaglet.java deleted file mode 100644 index e16ba220f..000000000 --- a/plexus-component-javadoc/src/main/java/org/codehaus/plexus/javadoc/PlexusComponentTaglet.java +++ /dev/null @@ -1,139 +0,0 @@ -package org.codehaus.plexus.javadoc; - -/* - * 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. - */ - -import java.util.Map; - -import com.sun.tools.doclets.Taglet; - -/** - * The @plexus.component tag is used to show that the class it annotates is a plexus component. - * This will add a <component> element to the <components> in components.xml. - * The following parameters are available, but remember the role is required. - * The text in the javadoc tag describing this class is copied into the component's <description> tag. - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
ParameterRequiredDescription
roleYesThe role that this class provides an implementation for (usually the class name - * of an implemented Interface
role-hintNoThe hints are used to differentiate multiple implementations of the same role
versionNoSet the version of the component
- * - * @see - * http://plexus.codehaus.org/guides/developer-guide/appendices/javadoc-tags-reference.html - * - * @author Vincent Siveton - * @version $Id$ - */ -public class PlexusComponentTaglet - extends AbstractPlexusTaglet -{ - private static final String NAME = "plexus.component"; - - private static final String HEADER = "Plexus component"; - - /** {@inheritDoc} */ - public String getHeader() - { - return HEADER; - } - - /** {@inheritDoc} */ - public String getName() - { - return NAME; - } - - /** {@inheritDoc} */ - public boolean inConstructor() - { - return false; - } - - /** {@inheritDoc} */ - public boolean inField() - { - return false; - } - - /** {@inheritDoc} */ - public boolean inMethod() - { - return false; - } - - /** {@inheritDoc} */ - public boolean inOverview() - { - return false; - } - - /** {@inheritDoc} */ - public boolean inPackage() - { - return false; - } - - /** {@inheritDoc} */ - public boolean inType() - { - return true; - } - - /** {@inheritDoc} */ - public boolean isInlineTag() - { - return false; - } - - /** - * Register this Taglet. - * - * @param tagletMap the map to register this tag to. - */ - public static void register( Map tagletMap ) - { - PlexusComponentTaglet tag = new PlexusComponentTaglet(); - Taglet t = (Taglet) tagletMap.get( tag.getName() ); - if ( t != null ) - { - tagletMap.remove( tag.getName() ); - } - tagletMap.put( tag.getName(), tag ); - } -} diff --git a/plexus-component-javadoc/src/main/java/org/codehaus/plexus/javadoc/PlexusConfigurationTaglet.java b/plexus-component-javadoc/src/main/java/org/codehaus/plexus/javadoc/PlexusConfigurationTaglet.java deleted file mode 100644 index 4af632492..000000000 --- a/plexus-component-javadoc/src/main/java/org/codehaus/plexus/javadoc/PlexusConfigurationTaglet.java +++ /dev/null @@ -1,127 +0,0 @@ -package org.codehaus.plexus.javadoc; - -/* - * 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. - */ - -import java.util.Map; - -import com.sun.tools.doclets.Taglet; - -/** - * The @plexus.configuration tags are used to mark fields in a class for configuration through - * the components.xml. - * - * - * - * - * - * - * - * - * - * - * - * - * - *
ParameterRequiredDescription
default-valueCurrentlyThe default values are currently required for the <configuration> - * tag to be written to components.xml.
- * - * @see - * http://plexus.codehaus.org/guides/developer-guide/appendices/javadoc-tags-reference.html - * - * @author Vincent Siveton - * @version $Id$ - */ -public class PlexusConfigurationTaglet - extends AbstractPlexusTaglet -{ - private static final String NAME = "plexus.configuration"; - - private static final String HEADER = "Plexus configuration"; - - /** {@inheritDoc} */ - public String getHeader() - { - return HEADER; - } - - /** {@inheritDoc} */ - public String getName() - { - return NAME; - } - - /** {@inheritDoc} */ - public boolean inConstructor() - { - return false; - } - - /** {@inheritDoc} */ - public boolean inField() - { - return true; - } - - /** {@inheritDoc} */ - public boolean inMethod() - { - return false; - } - - /** {@inheritDoc} */ - public boolean inOverview() - { - return false; - } - - /** {@inheritDoc} */ - public boolean inPackage() - { - return false; - } - - /** {@inheritDoc} */ - public boolean inType() - { - return false; - } - - /** {@inheritDoc} */ - public boolean isInlineTag() - { - return false; - } - - /** - * Register this Taglet. - * - * @param tagletMap the map to register this tag to. - */ - public static void register( Map tagletMap ) - { - PlexusConfigurationTaglet tag = new PlexusConfigurationTaglet(); - Taglet t = (Taglet) tagletMap.get( tag.getName() ); - if ( t != null ) - { - tagletMap.remove( tag.getName() ); - } - tagletMap.put( tag.getName(), tag ); - } -} diff --git a/plexus-component-javadoc/src/main/java/org/codehaus/plexus/javadoc/PlexusRequirementTaglet.java b/plexus-component-javadoc/src/main/java/org/codehaus/plexus/javadoc/PlexusRequirementTaglet.java deleted file mode 100644 index 657107091..000000000 --- a/plexus-component-javadoc/src/main/java/org/codehaus/plexus/javadoc/PlexusRequirementTaglet.java +++ /dev/null @@ -1,139 +0,0 @@ -package org.codehaus.plexus.javadoc; - -/* - * 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. - */ - -import java.util.Map; - -import com.sun.tools.doclets.Taglet; - -/** - * Tagging a field with @plexus.requirement will tell plexus to inject the required component or - * list of components before the component itself is started. Fields can be of the type of the Interface - * defining the component you wish to be injected or of type java.util.List or java.util.Map. - *
- * Fields of type java.lang.List will have a list of components injected whereas a field of - * type java.lang.Map will have a mapping in the form of role-hint -> component. - * The role parameter is required if using a List or Map whereas a role-hint is - * not allowed. - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
ParameterRequiredDescription
roleNo. Yes if the field is a List or a MapThe role parameter is used to tell plexus what Component role you are interested in. - * For singleton fields their type is used as a default.
role-hintNo. Not allowed if the field is a List or a MapLinks to the role-hint defined by a component when looking up a component. - * Not allowed with List or Map fields, as they return all role-hints.
- * - * @see - * http://plexus.codehaus.org/guides/developer-guide/appendices/javadoc-tags-reference.html - * - * @author Vincent Siveton - * @version $Id$ - */ -public class PlexusRequirementTaglet - extends AbstractPlexusTaglet -{ - private static final String NAME = "plexus.requirement"; - - private static final String HEADER = "Plexus requirement"; - - /** {@inheritDoc} */ - public String getHeader() - { - return HEADER; - } - - /** {@inheritDoc} */ - public String getName() - { - return NAME; - } - - /** {@inheritDoc} */ - public boolean inConstructor() - { - return false; - } - - /** {@inheritDoc} */ - public boolean inField() - { - return true; - } - - /** {@inheritDoc} */ - public boolean inMethod() - { - return false; - } - - /** {@inheritDoc} */ - public boolean inOverview() - { - return false; - } - - /** {@inheritDoc} */ - public boolean inPackage() - { - return false; - } - - /** {@inheritDoc} */ - public boolean inType() - { - return false; - } - - /** {@inheritDoc} */ - public boolean isInlineTag() - { - return false; - } - - /** - * Register this Taglet. - * - * @param tagletMap the map to register this tag to. - */ - public static void register( Map tagletMap ) - { - PlexusRequirementTaglet tag = new PlexusRequirementTaglet(); - Taglet t = (Taglet) tagletMap.get( tag.getName() ); - if ( t != null ) - { - tagletMap.remove( tag.getName() ); - } - tagletMap.put( tag.getName(), tag ); - } -} diff --git a/plexus-component-javadoc/src/main/javadoc/org/codehaus/plexus/javadoc/package.html b/plexus-component-javadoc/src/main/javadoc/org/codehaus/plexus/javadoc/package.html deleted file mode 100755 index 54c9da4f0..000000000 --- a/plexus-component-javadoc/src/main/javadoc/org/codehaus/plexus/javadoc/package.html +++ /dev/null @@ -1,35 +0,0 @@ - -

Javadoc Tags used by Plexus tools

-

- Here is a reference of the Javadoc annotations that can be used to 'decorate' the Java sources - to allow Plexus tools like Component Descriptor Creator (CDC) to generate Component Descriptors. -

- - - - - - - - - - - - - - - - - - - - - - - - - - -
Tag/AnnotationAvailable onRequiredDescriptor Element
@plexus.componentClassYesIndicate to Plexus CDC that the class is a component
@plexus.configurationFieldNoTell Plexus CDC that this item should be configured through - the components.xml
@plexus.requirementFieldNoIndicate to Plexus CDC that the field is required by the component
- \ No newline at end of file diff --git a/plexus-component-javadoc/src/site/apt/index.apt b/plexus-component-javadoc/src/site/apt/index.apt deleted file mode 100644 index 1b6d487ea..000000000 --- a/plexus-component-javadoc/src/site/apt/index.apt +++ /dev/null @@ -1,37 +0,0 @@ - ------ - Introduction - ------ - Vincent Siveton - ------ - August 2007 - ------ - -~~ 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. - -~~ NOTE: For help with the syntax of this file, see: -~~ http://maven.apache.org/guides/mini/guide-apt-format.html - -Plexus Doclet - - The Plexus Doclet project is a collection of several tags handle by the javadoc tool. - For more information about the standard Javadoc tool, please refer to - {{{http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/javadoc.html}Reference Guide}}. - -* Usage - - Instructions on how to use this project can be found {{{usage.html}here}}. diff --git a/plexus-component-javadoc/src/site/apt/usage.apt b/plexus-component-javadoc/src/site/apt/usage.apt deleted file mode 100644 index 2574b62c1..000000000 --- a/plexus-component-javadoc/src/site/apt/usage.apt +++ /dev/null @@ -1,77 +0,0 @@ - ------ - Usage - ------ - Vincent Siveton - ------ - August 2007 - ------ - -~~ 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. - -~~ NOTE: For help with the syntax of this file, see: -~~ http://maven.apache.org/guides/mini/guide-apt-format.html - - -Usage - - The doclets could be use directly in the {{{http://maven.apache.org/plugins/maven-javadoc-plugin/}maven-javadoc-plugin/}}. - -+-----+ - - ... - - - - org.apache.maven.plugins - maven-javadoc-plugin - - - - org.codehaus.plexus.javadoc.PlexusComponentTaglet - - org.codehaus.plexus - plexus-javadoc - 1.0-SNAPSHOT - - - - org.codehaus.plexus.javadoc.PlexusConfigurationTaglet - - org.codehaus.plexus - plexus-javadoc - 1.0-SNAPSHOT - - - - org.codehaus.plexus.javadoc.PlexusRequirementTaglet - - org.codehaus.plexus - plexus-javadoc - 1.0-SNAPSHOT - - - - ... - - - - ... - - ... - -+-----+ diff --git a/plexus-component-javadoc/src/site/site.xml b/plexus-component-javadoc/src/site/site.xml deleted file mode 100644 index daf0290a5..000000000 --- a/plexus-component-javadoc/src/site/site.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - Plexus - http://plexus.codehaus.org/ - /images/plexus-logo.png - - - - org.codehaus.plexus - plexus-stylus-skin - 1.0 - - - - - - - - - - - - - - diff --git a/plexus-component-metadata/pom.xml b/plexus-component-metadata/pom.xml index cb078a229..1ed7d5dbe 100644 --- a/plexus-component-metadata/pom.xml +++ b/plexus-component-metadata/pom.xml @@ -6,7 +6,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-4-SNAPSHOT + 1.1-SNAPSHOT plexus-component-metadata diff --git a/plexus-container-default/pom.xml b/plexus-container-default/pom.xml index 49c0556d7..2d7774bee 100644 --- a/plexus-container-default/pom.xml +++ b/plexus-container-default/pom.xml @@ -4,7 +4,7 @@ org.codehaus.plexus plexus-containers - 1.0-beta-4-SNAPSHOT + 1.1-SNAPSHOT plexus-container-default diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultPlexusContainer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultPlexusContainer.java index 506c3e941..783562a09 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultPlexusContainer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/DefaultPlexusContainer.java @@ -36,11 +36,10 @@ import org.codehaus.plexus.component.discovery.ComponentDiscovererManager; import org.codehaus.plexus.component.discovery.ComponentDiscoveryEvent; import org.codehaus.plexus.component.discovery.ComponentDiscoveryListener; -import org.codehaus.plexus.component.discovery.PlexusXmlComponentDiscoverer; import org.codehaus.plexus.component.factory.ComponentFactoryManager; import org.codehaus.plexus.component.repository.ComponentDescriptor; -import org.codehaus.plexus.component.repository.ComponentSetDescriptor; import org.codehaus.plexus.component.repository.ComponentDescriptorListener; +import org.codehaus.plexus.component.repository.ComponentSetDescriptor; import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.component.repository.exception.ComponentRepositoryException; @@ -968,6 +967,11 @@ public List> discoverComponents( ClassRealm realm ) { for ( ComponentSetDescriptor componentSetDescriptor : componentDiscoverer.findComponents( getContext(), realm ) ) { + // Here we should collect all the urls + // do the interpolation against the context + // register all the components + // allow interception and replacement of the components + componentSetDescriptors.add(componentSetDescriptor); for( ComponentDescriptor componentDescriptor : componentSetDescriptor.getComponents() ) diff --git a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractResourceBasedComponentDiscoverer.java b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractResourceBasedComponentDiscoverer.java index 40a46aa0b..8de4d34de 100644 --- a/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractResourceBasedComponentDiscoverer.java +++ b/plexus-container-default/src/main/java/org/codehaus/plexus/component/discovery/AbstractResourceBasedComponentDiscoverer.java @@ -67,6 +67,7 @@ public List findComponents( Context context, ClassRealm for ( URL url : Collections.list( resources )) { Reader reader = null; + try { URLConnection conn = url.openConnection(); diff --git a/pom.xml b/pom.xml index e5deed6ef..cf66ed464 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ plexus-containers - 1.0-beta-4-SNAPSHOT + 1.1-SNAPSHOT pom Parent :: Plexus Containers @@ -17,7 +17,6 @@ plexus-component-annotations plexus-component-metadata - plexus-component-javadoc plexus-container-default From 3da7ad282aa2e8a7ea5a965163f37e05947d252b Mon Sep 17 00:00:00 2001 From: bentmann Date: Tue, 12 May 2009 19:14:30 +0000 Subject: [PATCH 25/31] o Added undeclared but used dependencies (merged from r8176) --- plexus-component-metadata/pom.xml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/plexus-component-metadata/pom.xml b/plexus-component-metadata/pom.xml index 1ed7d5dbe..149b7b93e 100644 --- a/plexus-component-metadata/pom.xml +++ b/plexus-component-metadata/pom.xml @@ -24,6 +24,10 @@ org.codehaus.plexus plexus-component-annotations + + org.codehaus.plexus + plexus-utils +