You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2013/01/05 16:14:04 UTC
svn commit: r1429309 [1/3] - in /camel/trunk: components/camel-guice/
components/camel-guice/src/main/java/org/apache/camel/guice/
components/camel-guice/src/main/java/org/apache/camel/guice/impl/
components/camel-guice/src/main/java/org/apache/camel/g...
Author: ningjiang
Date: Sat Jan 5 15:14:02 2013
New Revision: 1429309
URL: http://svn.apache.org/viewvc?rev=1429309&view=rev
Log:
CAMEL-4209 Upgraded camel-guice to guice 3.0
Added:
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/inject/
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/inject/Configures.java
- copied, changed from r1429285, camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/inject/Injectors.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/GuiceInitialContextFactory.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/JndiBind.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/JndiBindings.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/internal/
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/internal/Classes.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/internal/JndiContext.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/AnnotatedMethodCache.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/Jsr250Module.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/NamedProviderSupport.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/PreDestroyCloser.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/ResourceMemberProvider.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/AnnotationMemberProvider.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/AnnotationMemberProviderSupport.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/CloseErrors.java
- copied, changed from r1429285, camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/CloseFailedException.java
- copied, changed from r1429285, camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/CloseTask.java
- copied, changed from r1429285, camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/Closer.java
- copied, changed from r1429285, camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/Closers.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/CompositeCloser.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/EncounterProvider.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/GuiceyFruitModule.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/HasScopeAnnotation.java
- copied, changed from r1429285, camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/MethodHandler.java
- copied, changed from r1429285, camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/Reflectors.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/internal/
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/internal/CloseErrorsImpl.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/internal/MethodKey.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/util/
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/util/CachedValue.java
- copied, changed from r1429285, camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/util/CachingProvider.java
- copied, changed from r1429285, camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/util/CloseableScope.java
camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/testing/
camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/testing/ClassScoped.java
- copied, changed from r1429285, camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java
camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/testing/InjectorManager.java
camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/testing/TestScoped.java
camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/testing/UseModule.java
- copied, changed from r1429285, camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java
camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/testing/junit4/
camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/testing/junit4/GuiceyJUnit4.java
Modified:
camel/trunk/components/camel-guice/pom.xml
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModule.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModuleWithMatchingRoutes.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModuleWithRouteTypes.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/GuiceCamelContext.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/Main.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ConsumerInjection.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/EndpointInjector.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/GuiceInjector.java
camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ProduceInjector.java
camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/CollectionOfCustomRoutesTest.java
camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ComponentFoundInRegistryTest.java
camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java
camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/EndpointInjectionTest.java
camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/FileEndpointReferenceRouteTest.java
camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/GuiceRouteWithNamedKeysTest.java
camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/GuiceTest.java
camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/consume/ConsumeTest.java
camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/patterns/FilterTest.java
camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/produce/ProduceTest.java
camel/trunk/components/camel-guice/src/test/resources/jndi.properties
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/MyModule.java
camel/trunk/examples/camel-example-guice-jms/src/main/java/org/apache/camel/example/guice/jms/OSGiCamelContextProvider.java
camel/trunk/examples/camel-example-guice-jms/src/main/resources/guicejndi.properties
camel/trunk/parent/pom.xml
Modified: camel/trunk/components/camel-guice/pom.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/pom.xml?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/pom.xml (original)
+++ camel/trunk/components/camel-guice/pom.xml Sat Jan 5 15:14:02 2013
@@ -43,22 +43,18 @@
<artifactId>camel-core</artifactId>
</dependency>
<dependency>
- <groupId>org.guiceyfruit</groupId>
- <artifactId>guiceyfruit-core</artifactId>
- <version>${guiceyfruit-version}</version>
+ <groupId>com.google.inject</groupId>
+ <artifactId>guice</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.google.collections</groupId>
+ <artifactId>google-collections</artifactId>
</dependency>
- <!-- testing -->
+ <!-- testing -->
<dependency>
- <groupId>org.guiceyfruit</groupId>
- <artifactId>guiceyfruit-junit4</artifactId>
- <version>${guiceyfruit-version}</version>
- <exclusions>
- <exclusion>
- <groupId>org.hamcrest</groupId>
- <artifactId>hamcrest-core</artifactId>
- </exclusion>
- </exclusions>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
@@ -79,18 +75,4 @@
<scope>test</scope>
</dependency>
</dependencies>
-
- <repositories>
- <repository>
- <id>guiceyfruit.release</id>
- <name>GuiceyFruit Release Repository</name>
- <url>http://guiceyfruit.googlecode.com/svn/repo/releases/</url>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- <releases>
- <enabled>true</enabled>
- </releases>
- </repository>
- </repositories>
</project>
Modified: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModule.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModule.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModule.java (original)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModule.java Sat Jan 5 15:14:02 2013
@@ -23,7 +23,7 @@ import org.apache.camel.Produce;
import org.apache.camel.guice.impl.ConsumerInjection;
import org.apache.camel.guice.impl.EndpointInjector;
import org.apache.camel.guice.impl.ProduceInjector;
-import org.guiceyfruit.jsr250.Jsr250Module;
+import org.apache.camel.guice.jsr250.Jsr250Module;
/**
* A base Guice module for creating a {@link CamelContext} leaving it up to the users module
Modified: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModuleWithMatchingRoutes.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModuleWithMatchingRoutes.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModuleWithMatchingRoutes.java (original)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModuleWithMatchingRoutes.java Sat Jan 5 15:14:02 2013
@@ -22,8 +22,9 @@ import com.google.inject.Injector;
import com.google.inject.Provides;
import com.google.inject.matcher.Matcher;
import com.google.inject.matcher.Matchers;
+
import org.apache.camel.RoutesBuilder;
-import org.guiceyfruit.Injectors;
+import org.apache.camel.guice.inject.Injectors;
/**
* A Guice Module which injects the CamelContext with all available implementations
Modified: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModuleWithRouteTypes.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModuleWithRouteTypes.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModuleWithRouteTypes.java (original)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModuleWithRouteTypes.java Sat Jan 5 15:14:02 2013
@@ -18,10 +18,10 @@ package org.apache.camel.guice;
import java.util.HashSet;
import java.util.Set;
-
+import com.google.common.collect.Sets;
import com.google.inject.Injector;
import com.google.inject.Provides;
-import com.google.inject.internal.Sets;
+
import org.apache.camel.RoutesBuilder;
Modified: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/GuiceCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/GuiceCamelContext.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/GuiceCamelContext.java (original)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/GuiceCamelContext.java Sat Jan 5 15:14:02 2013
@@ -32,6 +32,7 @@ import org.apache.camel.RoutesBuilder;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.TypeConverter;
import org.apache.camel.guice.impl.GuiceInjector;
+import org.apache.camel.guice.inject.Injectors;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.JndiRegistry;
import org.apache.camel.spi.ComponentResolver;
@@ -40,7 +41,6 @@ import org.apache.camel.spi.InterceptStr
import org.apache.camel.spi.LanguageResolver;
import org.apache.camel.spi.LifecycleStrategy;
import org.apache.camel.spi.Registry;
-import org.guiceyfruit.Injectors;
/**
* The default CamelContext implementation for working with Guice.
Modified: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/Main.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/Main.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/Main.java (original)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/Main.java Sat Jan 5 15:14:02 2013
@@ -27,18 +27,18 @@ import javax.naming.InitialContext;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Maps;
import com.google.inject.Binding;
import com.google.inject.Injector;
import com.google.inject.Key;
-import com.google.inject.internal.Iterables;
-import com.google.inject.internal.Maps;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
+import org.apache.camel.guice.inject.Injectors;
import org.apache.camel.main.MainSupport;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.view.ModelFileGenerator;
-import org.guiceyfruit.Injectors;
/**
* A command line tool for booting up a CamelContext using a Guice Injector via JNDI
Modified: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ConsumerInjection.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ConsumerInjection.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ConsumerInjection.java (original)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ConsumerInjection.java Sat Jan 5 15:14:02 2013
@@ -23,9 +23,9 @@ import com.google.inject.Inject;
import org.apache.camel.CamelContext;
import org.apache.camel.Consume;
+import org.apache.camel.guice.support.MethodHandler;
import org.apache.camel.impl.CamelPostProcessorHelper;
import org.apache.camel.util.ObjectHelper;
-import org.guiceyfruit.support.MethodHandler;
/**
* @version
Modified: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/EndpointInjector.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/EndpointInjector.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/EndpointInjector.java (original)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/EndpointInjector.java Sat Jan 5 15:14:02 2013
@@ -24,9 +24,10 @@ import com.google.inject.TypeLiteral;
import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
+import org.apache.camel.guice.support.AnnotationMemberProvider;
import org.apache.camel.impl.CamelPostProcessorHelper;
import org.apache.camel.util.ObjectHelper;
-import org.guiceyfruit.support.AnnotationMemberProvider;
+
/**
* Injects values into the {@link EndpointInject} injection point
Modified: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/GuiceInjector.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/GuiceInjector.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/GuiceInjector.java (original)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/GuiceInjector.java Sat Jan 5 15:14:02 2013
@@ -21,8 +21,8 @@ import org.apache.camel.spi.Injector;
/**
* An injector which uses Guice to perform the dependency injection of types
- *
- * @version
+ *
+ * @version
*/
public class GuiceInjector implements Injector {
private final com.google.inject.Injector injector;
@@ -33,7 +33,7 @@ public class GuiceInjector implements In
public <T> T newInstance(Class<T> type) {
// TODO if not bound we could create an instance and inject it?
- //injector.injectMembers(instance);
+ // injector.injectMembers(instance);
return injector.getInstance(type);
}
Modified: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ProduceInjector.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ProduceInjector.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ProduceInjector.java (original)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/ProduceInjector.java Sat Jan 5 15:14:02 2013
@@ -24,9 +24,9 @@ import com.google.inject.TypeLiteral;
import org.apache.camel.CamelContext;
import org.apache.camel.Produce;
+import org.apache.camel.guice.support.AnnotationMemberProvider;
import org.apache.camel.impl.CamelPostProcessorHelper;
import org.apache.camel.util.ObjectHelper;
-import org.guiceyfruit.support.AnnotationMemberProvider;
/**
* Injects values into the {@link Produce} injection point
Copied: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/inject/Configures.java (from r1429285, camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/inject/Configures.java?p2=camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/inject/Configures.java&p1=camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java&r1=1429285&r2=1429309&rev=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java (original)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/inject/Configures.java Sat Jan 5 15:14:02 2013
@@ -14,25 +14,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.camel.guice;
+package org.apache.camel.guice.inject;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import org.junit.Assert;
-import org.junit.Test;
-/**
- * Uses a RouteBuilder to bind instances of routes to the CamelContext
- *
- * @version
- */
-public class ConciseGuiceRouteTest extends Assert {
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
- @SuppressWarnings("unchecked")
- @Test
- public void testGuice() throws Exception {
- // lets disable resource injection to avoid JNDI being used
- Injector injector = Guice.createInjector(new CamelModuleWithRouteTypes(MyRouteInstaller.class, MyHardcodeRoute.class));
- GuiceTest.assertCamelContextRunningThenCloseInjector(injector);
- }
-}
\ No newline at end of file
+/**
+ * Marks a method like @{link Provides} but where the method is used to perform
+ * additional configuration of the object
+ *
+ * @version
+ */
+@Documented
+@Target(METHOD) @Retention(RUNTIME)
+public @interface Configures {
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/inject/Injectors.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/inject/Injectors.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/inject/Injectors.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/inject/Injectors.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,457 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.camel.guice.inject;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.lang.reflect.Type;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.StringTokenizer;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import com.google.inject.AbstractModule;
+import com.google.inject.Binding;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.Key;
+import com.google.inject.Module;
+import com.google.inject.Provider;
+import com.google.inject.Scope;
+import com.google.inject.Singleton;
+import com.google.inject.TypeLiteral;
+import com.google.inject.internal.BindingImpl;
+import com.google.inject.internal.Scoping;
+import com.google.inject.matcher.Matcher;
+import com.google.inject.name.Names;
+import com.google.inject.util.Modules;
+
+import org.apache.camel.guice.jndi.GuiceInitialContextFactory;
+import org.apache.camel.guice.jndi.internal.Classes;
+import org.apache.camel.guice.support.CloseErrors;
+import org.apache.camel.guice.support.CloseFailedException;
+import org.apache.camel.guice.support.Closer;
+import org.apache.camel.guice.support.Closers;
+import org.apache.camel.guice.support.CompositeCloser;
+import org.apache.camel.guice.support.HasScopeAnnotation;
+import org.apache.camel.guice.support.internal.CloseErrorsImpl;
+
+@SuppressWarnings({ "rawtypes", "unchecked" })
+public final class Injectors {
+ public static final String MODULE_CLASS_NAMES = "org.guiceyfruit.modules";
+
+ private Injectors() {
+ //Helper class
+ }
+ /**
+ * Creates an injector from the given properties, loading any modules define
+ * by the {@link #MODULE_CLASS_NAMES} property value (space separated) along
+ * with any other modules passed as an argument.
+ *
+ * @param environment
+ * the properties used to create the injector
+ * @param overridingModules
+ * any modules which override the modules referenced in the
+ * environment such as to provide the actual JNDI context
+ */
+ public static Injector createInjector(final Map environment,
+ Module... overridingModules) throws ClassNotFoundException,
+ IllegalAccessException, InstantiationException {
+ List<Module> modules = Lists.newArrayList();
+
+ // lets bind the properties
+ modules.add(new AbstractModule() {
+ protected void configure() {
+ Names.bindProperties(binder(), environment);
+ }
+ });
+
+ Object moduleValue = environment.get(MODULE_CLASS_NAMES);
+ if (moduleValue instanceof String) {
+ String names = (String) moduleValue;
+ StringTokenizer iter = new StringTokenizer(names);
+ while (iter.hasMoreTokens()) {
+ String moduleName = iter.nextToken();
+ Module module = loadModule(moduleName);
+ if (module != null) {
+ modules.add(module);
+ }
+ }
+ }
+ Injector injector = Guice.createInjector(Modules.override(modules)
+ .with(overridingModules));
+ return injector;
+ }
+
+ /**
+ * Returns an instance of the given type with the
+ * {@link com.google.inject.name.Named} annotation value.
+ *
+ * This method allows you to switch this code
+ * <code>injector.getInstance(Key.get(type, Names.named(name)));</code>
+ *
+ * to the more concise
+ * <code>Injectors.getInstance(injector, type, name);</code>
+ */
+ public static <T> T getInstance(Injector injector, java.lang.Class<T> type,
+ String name) {
+ return injector.getInstance(Key.get(type, Names.named(name)));
+ }
+
+ /**
+ * Returns a collection of all instances of the given base type
+ *
+ * @param baseClass
+ * the base type of objects required
+ * @param <T>
+ * the base type
+ * @return a set of objects returned from this injector
+ */
+ public static <T> Set<T> getInstancesOf(Injector injector,
+ Class<T> baseClass) {
+ Set<T> answer = Sets.newHashSet();
+ Set<Entry<Key<?>, Binding<?>>> entries = injector.getBindings()
+ .entrySet();
+ for (Entry<Key<?>, Binding<?>> entry : entries) {
+ Key<?> key = entry.getKey();
+ Class<?> keyType = getKeyType(key);
+ if (keyType != null && baseClass.isAssignableFrom(keyType)) {
+ Binding<?> binding = entry.getValue();
+ Object value = binding.getProvider().get();
+ if (value != null) {
+ T castValue = baseClass.cast(value);
+ answer.add(castValue);
+ }
+ }
+ }
+ return answer;
+ }
+
+ /**
+ * Returns a collection of all instances matching the given matcher
+ *
+ * @param matcher
+ * matches the types to return instances
+ * @return a set of objects returned from this injector
+ */
+ public static <T> Set<T> getInstancesOf(Injector injector,
+ Matcher<Class> matcher) {
+ Set<T> answer = Sets.newHashSet();
+ Set<Entry<Key<?>, Binding<?>>> entries = injector.getBindings()
+ .entrySet();
+ for (Entry<Key<?>, Binding<?>> entry : entries) {
+ Key<?> key = entry.getKey();
+ Class<?> keyType = getKeyType(key);
+ if (keyType != null && matcher.matches(keyType)) {
+ Binding<?> binding = entry.getValue();
+ Object value = binding.getProvider().get();
+ answer.add((T) value);
+ }
+ }
+ return answer;
+ }
+
+ /**
+ * Returns a collection of all of the providers matching the given matcher
+ *
+ * @param matcher
+ * matches the types to return instances
+ * @return a set of objects returned from this injector
+ */
+ public static <T> Set<Provider<T>> getProvidersOf(Injector injector,
+ Matcher<Class> matcher) {
+ Set<Provider<T>> answer = Sets.newHashSet();
+ Set<Entry<Key<?>, Binding<?>>> entries = injector.getBindings()
+ .entrySet();
+ for (Entry<Key<?>, Binding<?>> entry : entries) {
+ Key<?> key = entry.getKey();
+ Class<?> keyType = getKeyType(key);
+ if (keyType != null && matcher.matches(keyType)) {
+ Binding<?> binding = entry.getValue();
+ answer.add((Provider<T>) binding.getProvider());
+ }
+ }
+ return answer;
+ }
+
+ /**
+ * Returns a collection of all providers of the given base type
+ *
+ * @param baseClass
+ * the base type of objects required
+ * @param <T>
+ * the base type
+ * @return a set of objects returned from this injector
+ */
+ public static <T> Set<Provider<T>> getProvidersOf(Injector injector,
+ Class<T> baseClass) {
+ Set<Provider<T>> answer = Sets.newHashSet();
+ Set<Entry<Key<?>, Binding<?>>> entries = injector.getBindings()
+ .entrySet();
+ for (Entry<Key<?>, Binding<?>> entry : entries) {
+ Key<?> key = entry.getKey();
+ Class<?> keyType = getKeyType(key);
+ if (keyType != null && baseClass.isAssignableFrom(keyType)) {
+ Binding<?> binding = entry.getValue();
+ answer.add((Provider<T>) binding.getProvider());
+ }
+ }
+ return answer;
+ }
+
+ /** Returns true if a binding exists for the given matcher */
+ public static boolean hasBinding(Injector injector, Matcher<Class> matcher) {
+ return !getBindingsOf(injector, matcher).isEmpty();
+ }
+
+ /** Returns true if a binding exists for the given base class */
+ public static boolean hasBinding(Injector injector, Class<?> baseClass) {
+ return !getBindingsOf(injector, baseClass).isEmpty();
+ }
+
+ /** Returns true if a binding exists for the given key */
+ public static boolean hasBinding(Injector injector, Key<?> key) {
+ Binding<?> binding = getBinding(injector, key);
+ return binding != null;
+ }
+
+ /**
+ * Returns the binding for the given key or null if there is no such binding
+ */
+ public static Binding<?> getBinding(Injector injector, Key<?> key) {
+ Map<Key<?>, Binding<?>> bindings = injector.getBindings();
+ Binding<?> binding = bindings.get(key);
+ return binding;
+ }
+
+ /**
+ * Returns a collection of all of the bindings matching the given matcher
+ *
+ * @param matcher
+ * matches the types to return instances
+ * @return a set of objects returned from this injector
+ */
+ public static Set<Binding<?>> getBindingsOf(Injector injector,
+ Matcher<Class> matcher) {
+ Set<Binding<?>> answer = Sets.newHashSet();
+ Set<Entry<Key<?>, Binding<?>>> entries = injector.getBindings()
+ .entrySet();
+ for (Entry<Key<?>, Binding<?>> entry : entries) {
+ Key<?> key = entry.getKey();
+ Class<?> keyType = getKeyType(key);
+ if (keyType != null && matcher.matches(keyType)) {
+ answer.add(entry.getValue());
+ }
+ }
+ return answer;
+ }
+
+ /**
+ * Returns a collection of all bindings of the given base type
+ *
+ * @param baseClass
+ * the base type of objects required
+ * @return a set of objects returned from this injector
+ */
+ public static Set<Binding<?>> getBindingsOf(Injector injector,
+ Class<?> baseClass) {
+ Set<Binding<?>> answer = Sets.newHashSet();
+ Set<Entry<Key<?>, Binding<?>>> entries = injector.getBindings()
+ .entrySet();
+ for (Entry<Key<?>, Binding<?>> entry : entries) {
+ Key<?> key = entry.getKey();
+ Class<?> keyType = getKeyType(key);
+ if (keyType != null && baseClass.isAssignableFrom(keyType)) {
+ answer.add(entry.getValue());
+ }
+ }
+ return answer;
+ }
+
+ /** Returns the key type of the given key */
+ public static <T> Class<?> getKeyType(Key<?> key) {
+ Class<?> keyType = null;
+ TypeLiteral<?> typeLiteral = key.getTypeLiteral();
+ Type type = typeLiteral.getType();
+ if (type instanceof Class) {
+ keyType = (Class<?>) type;
+ }
+ return keyType;
+ }
+
+ protected static Module loadModule(String moduleName)
+ throws ClassNotFoundException, IllegalAccessException,
+ InstantiationException {
+ Class<?> type = Classes.loadClass(moduleName,
+ GuiceInitialContextFactory.class.getClassLoader());
+ return (Module) type.newInstance();
+ }
+
+ /*
+ */
+ /**
+ * Closes the given scope on this injector
+ *
+ * @param injector
+ * the injector on which to close objects
+ * @param scopeAnnotation
+ * the scope on which to close the objects
+ * @throws CloseFailedException
+ * the exceptions caused if closing an object fails
+ */
+ /*
+ * public static void close(Injector injector, Annotation scopeAnnotation)
+ * throws CloseFailedException { Key<PreDestroyer> key =
+ * Key.get(PreDestroyer.class, scopeAnnotation); if (hasBinding(injector,
+ * key)) { PreDestroyer destroyer = injector.getInstance(key);
+ * destroyer.close(); } }
+ */
+
+ /**
+ * Closes any singleton objects in the injector using the currently
+ * registered {@link Closer} implementations
+ */
+ public static void close(Injector injector) throws CloseFailedException {
+ close(injector, new CloseErrorsImpl(Injectors.class));
+ }
+
+ /**
+ * Closes objects within the given scope using the currently registered
+ * {@link Closer} implementations
+ */
+ public static void close(Injector injector, CloseErrors errors)
+ throws CloseFailedException {
+ close(injector, Singleton.class, errors);
+ }
+
+ /**
+ * Closes objects within the given scope using the currently registered
+ * {@link Closer} implementations
+ */
+ public static void close(Injector injector,
+ Class<? extends Annotation> scopeAnnotationToClose)
+ throws CloseFailedException {
+ close(injector, scopeAnnotationToClose, new CloseErrorsImpl(
+ Injectors.class));
+ }
+
+ /**
+ * Closes objects within the given scope using the currently registered
+ * {@link Closer} implementations
+ */
+ public static void close(Injector injector,
+ Class<? extends Annotation> scopeAnnotationToClose,
+ CloseErrors errors) throws CloseFailedException {
+ Set<Closer> closers = getInstancesOf(injector, Closer.class);
+ Closer closer = CompositeCloser.newInstance(closers);
+ if (closer == null) {
+ return;
+ }
+
+ Set<Entry<Key<?>, Binding<?>>> entries = injector.getBindings()
+ .entrySet();
+ for (Entry<Key<?>, Binding<?>> entry : entries) {
+ Key<?> key = entry.getKey();
+ Binding<?> binding = entry.getValue();
+ closeBinding(key, binding, scopeAnnotationToClose, closer, errors);
+ }
+
+ tryCloseJitBindings(closer, injector, scopeAnnotationToClose, errors);
+ errors.throwIfNecessary();
+ }
+
+ private static void tryCloseJitBindings(Closer closer, Injector injector,
+ Class<? extends Annotation> scopeAnnotationToClose,
+ CloseErrors errors) {
+ Class<? extends Injector> type = injector.getClass();
+ Field field;
+ try {
+ field = type.getDeclaredField("jitBindings");
+ field.setAccessible(true);
+ Object bindings = field.get(injector);
+ if (bindings != null) {
+ if (bindings instanceof Map) {
+ Map<Key<?>, BindingImpl<?>> map = (Map<Key<?>, BindingImpl<?>>) bindings;
+ Set<Entry<Key<?>, BindingImpl<?>>> entries = map.entrySet();
+ for (Entry<Key<?>, BindingImpl<?>> entry : entries) {
+ closeBinding(entry.getKey(), entry.getValue(),
+ scopeAnnotationToClose, closer, errors);
+ }
+ }
+ }
+ } catch (NoSuchFieldException e) {
+ // ignore - Guice has refactored so we can't access the jit bindings
+ // System.out.println("No such field! " + e);
+ } catch (IllegalAccessException e) {
+ // ignore - Guice has refactored so we can't access the jit bindings
+ // System.out.println("Failed to access field: " + field +
+ // ". Reason: " + e);
+ }
+ }
+
+ private static void closeBinding(Key<?> key, Binding<?> binding,
+ Class<? extends Annotation> scopeAnnotationToClose, Closer closer,
+ CloseErrors errors) {
+ Provider<?> provider = binding.getProvider();
+
+ Class<? extends Annotation> scopeAnnotation = getScopeAnnotation(binding);
+ if ((scopeAnnotation != null)
+ && scopeAnnotation.equals(scopeAnnotationToClose)) {
+ Object value = provider.get();
+ if (value != null) {
+ Closers.close(key, value, closer, errors);
+ }
+ }
+ }
+
+ /**
+ * Returns the scope annotation for the given binding or null if there is no
+ * scope
+ */
+ public static Class<? extends Annotation> getScopeAnnotation(
+ Binding<?> binding) {
+ Class<? extends Annotation> scopeAnnotation = null;
+ if (binding instanceof BindingImpl) {
+ BindingImpl bindingImpl = (BindingImpl) binding;
+ Scoping scoping = bindingImpl.getScoping();
+ if (scoping != null) {
+ scopeAnnotation = scoping.getScopeAnnotation();
+
+ // TODO not sure why we need this hack???
+ if (scopeAnnotation == null) {
+ Scope scope = scoping.getScopeInstance();
+ if (scope instanceof HasScopeAnnotation) {
+ HasScopeAnnotation hasScopeAnnotation = (HasScopeAnnotation) scope;
+ scopeAnnotation = hasScopeAnnotation
+ .getScopeAnnotation();
+ }
+
+ if (scopeAnnotation == null
+ && (scoping == Scoping.EAGER_SINGLETON
+ || scoping == Scoping.SINGLETON_ANNOTATION || scoping == Scoping.SINGLETON_INSTANCE)) {
+ scopeAnnotation = Singleton.class;
+ }
+ }
+ }
+ }
+ return scopeAnnotation;
+ }
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/GuiceInitialContextFactory.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/GuiceInitialContextFactory.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/GuiceInitialContextFactory.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/GuiceInitialContextFactory.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,125 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.guice.jndi;
+
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Properties;
+import java.util.Set;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+import javax.naming.spi.InitialContextFactory;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.google.inject.Provider;
+import com.google.inject.ProvisionException;
+import com.google.inject.Scopes;
+
+import org.apache.camel.guice.inject.Injectors;
+import org.apache.camel.guice.jndi.internal.JndiContext;
+
+/**
+ * A factory of the Guice JNDI provider which creates an injector from all the
+ * available modules specified in the space separated
+ * {@link Injectors#MODULE_CLASS_NAMES} property.
+ * <p/>
+ * For more details of how this JNDI provider works see <a
+ * href="http://code.google.com/p/camel-extra/wiki/GuiceJndi">the wiki
+ * documentation</a>
+ *
+ * @version
+ */
+public class GuiceInitialContextFactory implements InitialContextFactory {
+ public static final String NAME_PREFIX = "org.guiceyfruit.jndi/";
+
+ /**
+ * Creates a new context with the given environment.
+ *
+ * @param environment
+ * the environment, must not be <tt>null</tt>
+ * @return the created context.
+ * @throws NamingException
+ * is thrown if creation failed.
+ */
+ public Context getInitialContext(final Hashtable<?, ?> environment)
+ throws NamingException {
+ try {
+ // lets avoid infinite recursion with a provider creating an
+ // InitialContext by binding the
+ // singleton initial context into the injector
+ Injector injector = Injectors.createInjector(environment,
+ new AbstractModule() {
+ protected void configure() {
+ bind(Context.class).toProvider(
+ new Provider<Context>() {
+ @Inject
+ Injector injector;
+
+ public Context get() {
+ JndiContext context = new JndiContext(
+ environment);
+ Properties jndiNames = createJndiNamesProperties(environment);
+ try {
+ JndiBindings
+ .bindInjectorAndBindings(
+ context,
+ injector,
+ jndiNames);
+ return context;
+ } catch (NamingException e) {
+ throw new ProvisionException(
+ "Failed to create JNDI bindings. Reason: "
+ + e, e);
+ }
+ }
+ }).in(Scopes.SINGLETON);
+ }
+ });
+ return injector.getInstance(Context.class);
+ } catch (Exception e) {
+ NamingException exception = new NamingException(e.getMessage());
+ exception.initCause(e);
+ throw exception;
+ }
+ }
+
+ /**
+ * Creates a properties object containing all of the values whose keys start
+ * with {@link #NAME_PREFIX} with the prefix being removed on the key
+ *
+ * @return a properties object
+ */
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ private Properties createJndiNamesProperties(Hashtable environment) {
+ Set<Map.Entry<?, ?>> set = environment.entrySet();
+ Properties answer = new Properties();
+ for (Entry<?, ?> entry : set) {
+ String key = entry.getKey().toString();
+ if (key.startsWith(NAME_PREFIX)) {
+ String name = key.substring(NAME_PREFIX.length());
+ Object value = entry.getValue();
+ answer.put(name, value);
+ }
+ }
+ return answer;
+ }
+
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/JndiBind.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/JndiBind.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/JndiBind.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/JndiBind.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,49 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.guice.jndi;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.CONSTRUCTOR;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import com.google.inject.BindingAnnotation;
+
+/**
+ * Marks an injection point so that it is bound to the current JNDI context with
+ * some JNDI name
+ *
+ * @version
+ */
+@Target({ METHOD, CONSTRUCTOR, FIELD, TYPE })
+@Retention(RUNTIME)
+@Documented
+@BindingAnnotation
+public @interface JndiBind {
+
+ /**
+ * The JNDI name to bind the value to
+ *
+ * @return the name to bind the value to in JNDI
+ */
+ String value();
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/JndiBindings.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/JndiBindings.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/JndiBindings.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/JndiBindings.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,129 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.guice.jndi;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.Map.Entry;
+import java.util.Properties;
+import java.util.Set;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+
+import com.google.inject.Binding;
+import com.google.inject.Injector;
+import com.google.inject.Key;
+import com.google.inject.Provider;
+import com.google.inject.name.Named;
+
+import org.apache.camel.guice.jndi.internal.Classes;
+
+/**
+ * @version
+ */
+public final class JndiBindings {
+
+ private JndiBindings() {
+ //Helper class
+ }
+ /**
+ * Binds the given injector and its binding providers to the given JNDI
+ * context using <a
+ * href="http://code.google.com/p/camel-extra/wiki/GuiceJndi">this mapping
+ * mechanism</a>.
+ * <p/>
+ * This will expose all of the bindings providers to JNDI along with any
+ * bindings which are annotated with {@link JndiBind} or {@link @Named} to
+ * the given JNDI context.
+ *
+ * @param context
+ * the context to export objects to
+ * @param injector
+ * the injector used to find the bindings
+ */
+ public static void bindInjectorAndBindings(Context context,
+ Injector injector, Properties jndiNames) throws NamingException {
+ // lets find all the exported bindings
+ Set<Entry<Key<?>, Binding<?>>> entries = injector.getBindings()
+ .entrySet();
+ for (Entry<Key<?>, Binding<?>> entry : entries) {
+ Key<?> key = entry.getKey();
+ Binding<?> binding = entry.getValue();
+ Annotation annotation = key.getAnnotation();
+ Type type = key.getTypeLiteral().getType();
+ JndiBind jndiBind = null;
+ if (type instanceof Class) {
+ Class<?> aClass = (Class<?>) type;
+ jndiBind = aClass.getAnnotation(JndiBind.class);
+ }
+
+ if (annotation instanceof JndiBind) {
+ jndiBind = (JndiBind) annotation;
+ }
+ String jndiName = null;
+ if (jndiBind != null) {
+ jndiName = jndiBind.value();
+ }
+ if (jndiName == null) {
+ if (annotation instanceof Named) {
+ Named named = (Named) annotation;
+ String name = named.value();
+ jndiName = type.toString() + "/" + name;
+ } else if (type instanceof Class<?>) {
+ Class<?> aClass = (Class<?>) type;
+ if (annotation == null) {
+ jndiName = aClass.getName();
+ } else {
+ jndiName = aClass.getName() + annotation;
+ }
+ }
+ }
+ if (jndiName != null) {
+ Object value = binding.getProvider();
+ if (value != null) {
+ context.bind(jndiName, value);
+ }
+ }
+ }
+
+ for (Entry<Object, Object> entry : jndiNames.entrySet()) {
+ String jndiName = entry.getKey().toString();
+ String expression = entry.getValue().toString();
+
+ Provider<?> provider = getProviderForExpression(injector,
+ expression);
+ if (provider != null) {
+ context.bind(jndiName, provider);
+ }
+ }
+ }
+
+ static Provider<?> getProviderForExpression(Injector injector,
+ String expression) {
+ // TODO we could support more complex expressions
+ // like 'className/name' to map to @Named annotations
+ // or even 'className@annotationType(values) etc
+ try {
+ Class<?> type = Classes.loadClass(expression,
+ JndiBindings.class.getClassLoader());
+ return injector.getProvider(type);
+ } catch (ClassNotFoundException e) {
+ return null;
+ }
+ }
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/internal/Classes.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/internal/Classes.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/internal/Classes.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/internal/Classes.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,55 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.guice.jndi.internal;
+
+
+public final class Classes {
+ private Classes() {
+ // Helper class
+ }
+
+ /**
+ * Attempts to load the class of the given name from the thread context
+ * class loader first then the given class loader second
+ *
+ * @param name
+ * the name of the class
+ * @param loader
+ * the class loader to use if the thread context class loader
+ * cannot find the class
+ * @return the class loaded
+ * @throws ClassNotFoundException
+ * if the class could not be found
+ */
+ public static Class<?> loadClass(String name, ClassLoader loader)
+ throws ClassNotFoundException {
+ ClassLoader contextClassLoader = Thread.currentThread()
+ .getContextClassLoader();
+ if (contextClassLoader != null) {
+ try {
+ return contextClassLoader.loadClass(name);
+ } catch (ClassNotFoundException e) {
+ try {
+ return loader.loadClass(name);
+ } catch (ClassNotFoundException e1) {
+ throw e;
+ }
+ }
+ }
+ return null;
+ }
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/internal/JndiContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/internal/JndiContext.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/internal/JndiContext.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jndi/internal/JndiContext.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,424 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.guice.jndi.internal;
+
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.naming.Binding;
+import javax.naming.CompositeName;
+import javax.naming.Context;
+import javax.naming.LinkRef;
+import javax.naming.Name;
+import javax.naming.NameClassPair;
+import javax.naming.NameNotFoundException;
+import javax.naming.NameParser;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.NotContextException;
+import javax.naming.OperationNotSupportedException;
+import javax.naming.Reference;
+import javax.naming.spi.NamingManager;
+
+import com.google.inject.Provider;
+
+/**
+ * A default JNDI context
+ *
+ * @version
+ */
+@SuppressWarnings({ "rawtypes", "unchecked" })
+public class JndiContext implements Context, Serializable {
+ public static final String SEPARATOR = "/";
+ protected static final NameParser NAME_PARSER = new NameParser() {
+ public Name parse(String name) throws NamingException {
+ return new CompositeName(name);
+ }
+ };
+ private static final long serialVersionUID = -5754338187296859149L;
+
+ private final Hashtable environment; // environment for this context
+ private final Map bindings; // bindings at my level
+ private final Map treeBindings; // all bindings under me
+ private boolean frozen;
+ private String nameInNamespace = "";
+
+ public JndiContext() throws Exception {
+ this(new Hashtable(), new HashMap());
+ }
+
+ public JndiContext(Hashtable environment) {
+ this(environment, new HashMap());
+ }
+
+ public JndiContext(Hashtable environment, Map bindings) {
+ if (environment == null) {
+ this.environment = new Hashtable();
+ } else {
+ this.environment = new Hashtable(environment);
+ }
+ this.bindings = bindings;
+ treeBindings = new HashMap();
+ }
+
+ public JndiContext(Hashtable environment, Map bindings,
+ String nameInNamespace) {
+ this(environment, bindings);
+ this.nameInNamespace = nameInNamespace;
+ }
+
+ protected JndiContext(JndiContext clone, Hashtable env) {
+ this.bindings = clone.bindings;
+ this.treeBindings = clone.treeBindings;
+ this.environment = new Hashtable(env);
+ }
+
+ protected JndiContext(JndiContext clone, Hashtable env,
+ String nameInNamespace) {
+ this(clone, env);
+ this.nameInNamespace = nameInNamespace;
+ }
+
+ public void freeze() {
+ frozen = true;
+ }
+
+ boolean isFrozen() {
+ return frozen;
+ }
+
+ /**
+ * internalBind is intended for use only during setup or possibly by
+ * suitably synchronized superclasses. It binds every possible lookup into a
+ * map in each context. To do this, each context strips off one name segment
+ * and if necessary creates a new context for it. Then it asks that context
+ * to bind the remaining name. It returns a map containing all the bindings
+ * from the next context, plus the context it just created (if it in fact
+ * created it). (the names are suitably extended by the segment originally
+ * lopped off).
+ */
+ protected Map internalBind(String name, Object value)
+ throws NamingException {
+ /*
+ * // lets wrap the value in a Provider final Object originalValue =
+ * value; value = new Provider() { public Object get() { return
+ * originalValue; } };
+ */
+ assert name != null && name.length() > 0;
+ assert !frozen;
+
+ Map newBindings = new HashMap();
+ int pos = name.indexOf('/');
+ if (pos == -1) {
+ if (treeBindings.put(name, value) != null) {
+ throw new NamingException("Something already bound at " + name);
+ }
+ bindings.put(name, value);
+ newBindings.put(name, value);
+ } else {
+ String segment = name.substring(0, pos);
+ assert segment != null;
+ assert !segment.equals("");
+ Object o = treeBindings.get(segment);
+ if (o == null) {
+ o = newContext();
+ treeBindings.put(segment, o);
+ bindings.put(segment, o);
+ newBindings.put(segment, o);
+ } else if (!(o instanceof JndiContext)) {
+ throw new NamingException(
+ "Something already bound where a subcontext should go");
+ }
+ JndiContext defaultContext = (JndiContext) o;
+ String remainder = name.substring(pos + 1);
+ Map subBindings = defaultContext.internalBind(remainder, value);
+ for (Iterator iterator = subBindings.entrySet().iterator(); iterator
+ .hasNext();) {
+ Map.Entry entry = (Map.Entry) iterator.next();
+ String subName = segment + "/" + (String) entry.getKey();
+ Object bound = entry.getValue();
+ treeBindings.put(subName, bound);
+ newBindings.put(subName, bound);
+ }
+ }
+ return newBindings;
+ }
+
+ protected JndiContext newContext() {
+ try {
+ return new JndiContext();
+ } catch (Exception e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ public Object addToEnvironment(String propName, Object propVal)
+ throws NamingException {
+ return environment.put(propName, propVal);
+ }
+
+ public Hashtable getEnvironment() throws NamingException {
+ return (Hashtable) environment.clone();
+ }
+
+ public Object removeFromEnvironment(String propName) throws NamingException {
+ return environment.remove(propName);
+ }
+
+ public Object lookup(String name) throws NamingException {
+ if (name.length() == 0) {
+ return this;
+ }
+ Object result = treeBindings.get(name);
+ if (result == null) {
+ result = bindings.get(name);
+ }
+ if (result == null) {
+ int pos = name.indexOf(':');
+ if (pos > 0) {
+ String scheme = name.substring(0, pos);
+ Context ctx = NamingManager.getURLContext(scheme, environment);
+ if (ctx == null) {
+ throw new NamingException("scheme " + scheme
+ + " not recognized");
+ }
+ return ctx.lookup(name);
+ } else {
+ // Split out the first name of the path
+ // and look for it in the bindings map.
+ CompositeName path = new CompositeName(name);
+
+ if (path.size() == 0) {
+ return this;
+ } else {
+ String first = path.get(0);
+ Object value = bindings.get(first);
+ if (value == null) {
+ throw new NameNotFoundException(name);
+ } else if (value instanceof Context && path.size() > 1) {
+ Context subContext = (Context) value;
+ value = subContext.lookup(path.getSuffix(1));
+ }
+ return value;
+ }
+ }
+ }
+ if (result instanceof Provider) {
+ Provider provider = (Provider) result;
+ result = provider.get();
+ }
+ if (result instanceof LinkRef) {
+ LinkRef ref = (LinkRef) result;
+ result = lookup(ref.getLinkName());
+ }
+ if (result instanceof Reference) {
+ try {
+ result = NamingManager.getObjectInstance(result, null, null,
+ this.environment);
+ } catch (NamingException e) {
+ throw e;
+ } catch (Exception e) {
+ throw (NamingException) new NamingException(
+ "could not look up : " + name).initCause(e);
+ }
+ }
+ if (result instanceof JndiContext) {
+ String prefix = getNameInNamespace();
+ if (prefix.length() > 0) {
+ prefix = prefix + SEPARATOR;
+ }
+ result = new JndiContext((JndiContext) result, environment, prefix
+ + name);
+ }
+ return result;
+ }
+
+ public Object lookup(Name name) throws NamingException {
+ return lookup(name.toString());
+ }
+
+ public Object lookupLink(String name) throws NamingException {
+ return lookup(name);
+ }
+
+ public Name composeName(Name name, Name prefix) throws NamingException {
+ Name result = (Name) prefix.clone();
+ result.addAll(name);
+ return result;
+ }
+
+ public String composeName(String name, String prefix)
+ throws NamingException {
+ CompositeName result = new CompositeName(prefix);
+ result.addAll(new CompositeName(name));
+ return result.toString();
+ }
+
+ public NamingEnumeration list(String name) throws NamingException {
+ Object o = lookup(name);
+ if (o == this) {
+ return new ListEnumeration();
+ } else if (o instanceof Context) {
+ return ((Context) o).list("");
+ } else {
+ throw new NotContextException();
+ }
+ }
+
+ public NamingEnumeration listBindings(String name) throws NamingException {
+ Object o = lookup(name);
+ if (o == this) {
+ return new ListBindingEnumeration();
+ } else if (o instanceof Context) {
+ return ((Context) o).listBindings("");
+ } else {
+ throw new NotContextException();
+ }
+ }
+
+ public Object lookupLink(Name name) throws NamingException {
+ return lookupLink(name.toString());
+ }
+
+ public NamingEnumeration list(Name name) throws NamingException {
+ return list(name.toString());
+ }
+
+ public NamingEnumeration listBindings(Name name) throws NamingException {
+ return listBindings(name.toString());
+ }
+
+ public void bind(Name name, Object value) throws NamingException {
+ bind(name.toString(), value);
+ }
+
+ public void bind(String name, Object value) throws NamingException {
+ if (isFrozen()) {
+ throw new OperationNotSupportedException();
+ } else {
+ internalBind(name, value);
+ }
+ }
+
+ public void close() throws NamingException {
+ // ignore
+ }
+
+ public Context createSubcontext(Name name) throws NamingException {
+ throw new OperationNotSupportedException();
+ }
+
+ public Context createSubcontext(String name) throws NamingException {
+ throw new OperationNotSupportedException();
+ }
+
+ public void destroySubcontext(Name name) throws NamingException {
+ throw new OperationNotSupportedException();
+ }
+
+ public void destroySubcontext(String name) throws NamingException {
+ throw new OperationNotSupportedException();
+ }
+
+ public String getNameInNamespace() throws NamingException {
+ return nameInNamespace;
+ }
+
+ public NameParser getNameParser(Name name) throws NamingException {
+ return NAME_PARSER;
+ }
+
+ public NameParser getNameParser(String name) throws NamingException {
+ return NAME_PARSER;
+ }
+
+ public void rebind(Name name, Object value) throws NamingException {
+ bind(name, value);
+ }
+
+ public void rebind(String name, Object value) throws NamingException {
+ bind(name, value);
+ }
+
+ public void rename(Name oldName, Name newName) throws NamingException {
+ throw new OperationNotSupportedException();
+ }
+
+ public void rename(String oldName, String newName) throws NamingException {
+ throw new OperationNotSupportedException();
+ }
+
+ public void unbind(Name name) throws NamingException {
+ throw new OperationNotSupportedException();
+ }
+
+ public void unbind(String name) throws NamingException {
+ bindings.remove(name);
+ treeBindings.remove(name);
+ }
+
+ private abstract class LocalNamingEnumeration implements NamingEnumeration {
+ private Iterator i = bindings.entrySet().iterator();
+
+ public boolean hasMore() throws NamingException {
+ return i.hasNext();
+ }
+
+ public boolean hasMoreElements() {
+ return i.hasNext();
+ }
+
+ protected Map.Entry getNext() {
+ return (Map.Entry) i.next();
+ }
+
+ public void close() throws NamingException {
+ }
+ }
+
+ private class ListEnumeration extends LocalNamingEnumeration {
+ ListEnumeration() {
+ }
+
+ public Object next() throws NamingException {
+ return nextElement();
+ }
+
+ public Object nextElement() {
+ Map.Entry entry = getNext();
+ return new NameClassPair((String) entry.getKey(), entry.getValue()
+ .getClass().getName());
+ }
+ }
+
+ private class ListBindingEnumeration extends LocalNamingEnumeration {
+ ListBindingEnumeration() {
+ }
+
+ public Object next() throws NamingException {
+ return nextElement();
+ }
+
+ public Object nextElement() {
+ Map.Entry entry = getNext();
+ return new Binding((String) entry.getKey(), entry.getValue());
+ }
+ }
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/AnnotatedMethodCache.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/AnnotatedMethodCache.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/AnnotatedMethodCache.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/AnnotatedMethodCache.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,80 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.guice.jsr250;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.Map;
+import java.util.WeakHashMap;
+
+/**
+ * A cache which maintains which method is annotated by a given annotation for
+ * each class
+ *
+ * @version
+ */
+class AnnotatedMethodCache {
+ private final Class<? extends Annotation> annotationType;
+ private Map<Class<?>, Method> methodCache = Collections
+ .synchronizedMap(new WeakHashMap<Class<?>, Method>());
+
+ public AnnotatedMethodCache(Class<? extends Annotation> annotationType) {
+ this.annotationType = annotationType;
+ }
+
+ /**
+ * Looks up the method which is annotated for the given type
+ */
+ public Method getMethod(Class<?> type) {
+ // if we are invoked concurrently it doesn't matter if we look up the
+ // method
+ // concurrently - its the same instance that will be overwritten in the
+ // map
+ Method method = methodCache.get(type);
+ if (method == null) {
+ method = findMethodWithAnnotation(type, annotationType);
+ if (method != null) {
+ if (method.getParameterTypes().length != 0) {
+ throw new IllegalArgumentException(
+ "Method should have no arguments for @PostConstruct "
+ + method);
+ }
+ methodCache.put(type, method);
+ }
+ }
+ return method;
+ }
+
+ protected Method findMethodWithAnnotation(Class<?> type,
+ Class<? extends Annotation> annotationType) {
+ Method[] methods = type.getDeclaredMethods();
+ for (Method method : methods) {
+ Annotation fromElement = method.getAnnotation(annotationType);
+ if (fromElement != null) {
+ return method;
+ }
+ }
+ if (!Object.class.equals(type)) {
+ Class<?> superclass = type.getSuperclass();
+ if (superclass != null) {
+ return findMethodWithAnnotation(superclass, annotationType);
+ }
+ }
+ return null;
+ }
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/Jsr250Module.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/Jsr250Module.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/Jsr250Module.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/Jsr250Module.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,55 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.guice.jsr250;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.Resource;
+
+import org.apache.camel.guice.support.GuiceyFruitModule;
+import org.apache.camel.guice.support.MethodHandler;
+
+/**
+ * A module which installs JSR 250 lifecycle and injection using the
+ * {@link Resource} annotation.
+ *
+ * @version
+ */
+public class Jsr250Module extends GuiceyFruitModule {
+
+ protected void configure() {
+ super.configure();
+
+ bindAnnotationInjector(Resource.class, ResourceMemberProvider.class);
+
+ bindMethodHandler(PostConstruct.class,
+ new MethodHandler<Object, Annotation>() {
+ public void afterInjection(Object injectee, Annotation annotation, Method method)
+ throws InvocationTargetException,
+ IllegalAccessException {
+
+ method.invoke(injectee);
+ }
+ });
+
+ bind(PreDestroyCloser.class);
+ }
+
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/NamedProviderSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/NamedProviderSupport.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/NamedProviderSupport.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/NamedProviderSupport.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,92 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.guice.jsr250;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Member;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import com.google.inject.Binding;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.google.inject.Key;
+import com.google.inject.ProvisionException;
+import com.google.inject.TypeLiteral;
+import com.google.inject.name.Names;
+
+import org.apache.camel.guice.inject.Injectors;
+import org.apache.camel.guice.support.AnnotationMemberProviderSupport;
+
+
+/**
+ * A useful base class for any provider which looks up values by name
+ * annotations or named entries in OSGi
+ *
+ * @version
+ */
+public abstract class NamedProviderSupport<A extends Annotation> extends
+ AnnotationMemberProviderSupport<A> {
+ @Inject
+ private Injector injector;
+ private Context context;
+
+ public Context getContext() {
+ return context;
+ }
+
+ @Inject(optional = true)
+ public void setContext(Context context) {
+ this.context = context;
+ }
+
+ protected Object provideObjectFromNamedBindingOrJndi(
+ TypeLiteral<?> requiredType, String name) {
+ Binding<?> binding = Injectors.getBinding(injector,
+ Key.get(requiredType, Names.named(name)));
+ if (binding != null) {
+ return binding.getProvider().get();
+ }
+
+ // TODO we may want to try avoid the dependency on JNDI classes
+ // for better operation in GAE?
+ try {
+ if (context == null) {
+ context = new InitialContext();
+ }
+ return context.lookup(name);
+ } catch (NamingException e) {
+ throw new ProvisionException("Failed to find name '" + name
+ + "' in JNDI. Cause: " + e, e);
+ }
+ }
+
+ /**
+ * if no valid name is present on the annotation then use the member name
+ */
+ protected String getValueName(String nameFromAnnotation, Member member) {
+ if (nameFromAnnotation == null || nameFromAnnotation.length() == 0) {
+ nameFromAnnotation = member.getName();
+ }
+ if (nameFromAnnotation == null || nameFromAnnotation.length() == 0) {
+ throw new IllegalArgumentException("No name defined");
+ }
+ return nameFromAnnotation;
+ }
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/PreDestroyCloser.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/PreDestroyCloser.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/PreDestroyCloser.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/PreDestroyCloser.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,54 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.guice.jsr250;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import javax.annotation.PreDestroy;
+
+import org.apache.camel.guice.support.Closer;
+
+/**
+ * Supports the {@link javax.annotation.PreDestroy} annotation lifecycle from
+ * JSR250.
+ * <p>
+ * To install this closer you need to register the {@link Jsr250Module} in your
+ * injector.
+ *
+ * @author james.strachan@gmail.com (James Strachan)
+ * @version
+ */
+public class PreDestroyCloser implements Closer {
+
+ private AnnotatedMethodCache methodCache = new AnnotatedMethodCache(
+ PreDestroy.class);
+
+ public void close(Object object) throws Throwable {
+ Class<? extends Object> type = object.getClass();
+ Method method = methodCache.getMethod(type);
+ if (method != null) {
+ if (method != null) {
+ try {
+ method.invoke(object);
+ } catch (InvocationTargetException e) {
+ throw e.getTargetException();
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/ResourceMemberProvider.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/ResourceMemberProvider.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/ResourceMemberProvider.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/jsr250/ResourceMemberProvider.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,47 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.guice.jsr250;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Member;
+import java.lang.reflect.Method;
+
+import javax.annotation.Resource;
+
+import com.google.inject.TypeLiteral;
+
+/**
+ * Injects fields or methods with the results of the {@link Resource} annotation
+ *
+ * @version
+ */
+public class ResourceMemberProvider extends NamedProviderSupport<Resource> {
+
+ public boolean isNullParameterAllowed(Resource annotation, Method method,
+ Class<?> parameterType, int parameterIndex) {
+ // TODO can a @Resource be optional?
+ return false;
+ }
+
+ protected Object provide(Resource resource, Member member,
+ TypeLiteral<?> requiredType, Class<?> memberType,
+ Annotation[] annotations) {
+ String name = getValueName(resource.name(), member);
+ return provideObjectFromNamedBindingOrJndi(requiredType, name);
+ }
+
+}