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 [2/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...
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/AnnotationMemberProvider.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/AnnotationMemberProvider.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/AnnotationMemberProvider.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/AnnotationMemberProvider.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.support;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+import com.google.inject.TypeLiteral;
+
+/**
+ * A provider of an annotation based injection point which can use the value of
+ * an annotation together with the member on which the annotation is placed to
+ * determine the value.
+ *
+ * @version
+ */
+public interface AnnotationMemberProvider<A extends Annotation> {
+
+ /** Returns the value to be injected for the given annotated field */
+ Object provide(A annotation, TypeLiteral<?> type, Field field);
+
+ /**
+ * Returns the value to be injected for the given annotated method parameter
+ * value
+ */
+ Object provide(A annotation, TypeLiteral<?> type, Method method,
+ Class<?> parameterType, int parameterIndex);
+
+ /** Returns true if the given parameter on the annotated method can be null */
+ boolean isNullParameterAllowed(A annotation, Method method,
+ Class<?> parameterType, int parameterIndex);
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/AnnotationMemberProviderSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/AnnotationMemberProviderSupport.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/AnnotationMemberProviderSupport.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/AnnotationMemberProviderSupport.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,57 @@
+/**
+ * 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.support;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.lang.reflect.Member;
+import java.lang.reflect.Method;
+
+import com.google.inject.TypeLiteral;
+
+/**
+ * A useful base class for implementors meaning they only have to implement a
+ * single method whether a Field or Method parameter is being injected
+ *
+ * @version
+ */
+public abstract class AnnotationMemberProviderSupport<A extends Annotation>
+ implements AnnotationMemberProvider<A> {
+
+ public Object provide(A annotation, TypeLiteral<?> type, Field field) {
+ TypeLiteral<?> requiredType = type.getFieldType(field);
+ return provide(annotation, field, requiredType, field.getType(), null);
+ }
+
+ public Object provide(A annotation, TypeLiteral<?> type, Method method,
+ Class<?> parameterType, int parameterIndex) {
+
+ TypeLiteral<?> requiredType = type.getParameterTypes(method).get(
+ parameterIndex);
+ Annotation[] annotations = method.getParameterAnnotations()[parameterIndex];
+ return provide(annotation, method, requiredType,
+ method.getParameterTypes()[parameterIndex], annotations);
+ }
+
+ /**
+ * The default method to create a value for the named member of the
+ * requested type
+ */
+ protected abstract Object provide(A annotation, Member member,
+ TypeLiteral<?> requiredType, Class<?> memberType,
+ Annotation[] annotations);
+}
Copied: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/CloseErrors.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/support/CloseErrors.java?p2=camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/CloseErrors.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/support/CloseErrors.java Sat Jan 5 15:14:02 2013
@@ -14,25 +14,29 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.camel.guice;
+package org.apache.camel.guice.support;
-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
+ * A handler of exceptions when closing down resources. Implementations will
+ * typically create a collection of error messages so that all of the different
+ * close exceptions are collected together.
+ *
+ * @version
*/
-public class ConciseGuiceRouteTest extends Assert {
+public interface CloseErrors {
- @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);
- }
+ /**
+ * Notification of a close exception
+ *
+ * @param key
+ * the key of the object being closed which is usually a
+ * {@link com.google.inject.Key} or {@link String}
+ * @param object
+ * the object being closed
+ * @param cause
+ * the exception thrown when the close was attempted
+ */
+ void closeError(Object key, Object object, Exception cause);
-}
\ No newline at end of file
+ void throwIfNecessary() throws CloseFailedException;
+}
Copied: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/CloseFailedException.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/support/CloseFailedException.java?p2=camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/CloseFailedException.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/support/CloseFailedException.java Sat Jan 5 15:14:02 2013
@@ -14,25 +14,32 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.camel.guice;
+package org.apache.camel.guice.support;
+
+import java.io.IOException;
+import java.util.List;
+
+import com.google.inject.internal.Errors;
+import com.google.inject.spi.Message;
-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
+ * Indicates that an attempt to close an injector or scope failed closing one or
+ * more bindings.
+ *
+ * @author james.strachan@gmail.com (James Strachan)
+ * @version
*/
-public class ConciseGuiceRouteTest extends Assert {
+public class CloseFailedException extends IOException {
+ private static final long serialVersionUID = 4794716198859801415L;
- @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);
+ private final List<Message> messages;
+
+ public CloseFailedException(List<Message> messages) {
+ super(Errors.format("Close errors", messages));
+ this.messages = messages;
}
-}
\ No newline at end of file
+ public List<Message> getMessages() {
+ return messages;
+ }
+}
Copied: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/CloseTask.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/support/CloseTask.java?p2=camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/CloseTask.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/support/CloseTask.java Sat Jan 5 15:14:02 2013
@@ -14,25 +14,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.camel.guice;
+package org.apache.camel.guice.support;
-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
+ * Some task which may throw an exception
+ *
+ * @version
*/
-public class ConciseGuiceRouteTest extends Assert {
+public interface CloseTask {
+ Object getSource();
- @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
+ void perform() throws Exception;
+}
Copied: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/Closer.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/support/Closer.java?p2=camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/Closer.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/support/Closer.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.support;
-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
+ * Represents a strategy for closing an object down such as using the @PreDestroy
+ * lifecycle from JSR 250, invoking {@link java.io.Closeable#close()} or using
+ * the DisposableBean interface from Spring.
+ *
+ * @version
*/
-public class ConciseGuiceRouteTest extends Assert {
-
- @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
+public interface Closer {
+ /**
+ * Closes the given object
+ *
+ * @param object
+ * the object to be closed
+ * @throws Exception
+ * if the close operation caused some exception to occur
+ */
+ void close(Object object) throws Throwable;
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/Closers.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/Closers.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/Closers.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/Closers.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,53 @@
+/**
+ * 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.support;
+
+/**
+ * Some helper methods for working with the {@link Closer} interface
+ *
+ * @version
+ */
+public final class Closers {
+ private Closers() {
+ //Helper class
+ }
+
+ /**
+ * Closes the given object with the Closer if the object is not null
+ *
+ * @param key
+ * the Key or String name of the object to be closed
+ * @param objectToBeClosed
+ * the object that is going to be closed
+ * @param closer
+ * the strategy used to close the object
+ * @param errors
+ * the handler of exceptions if they occur
+ */
+ public static void close(Object key, Object objectToBeClosed,
+ Closer closer, CloseErrors errors) {
+ if (objectToBeClosed != null) {
+ try {
+ closer.close(objectToBeClosed);
+ } catch (Exception e) {
+ errors.closeError(key, objectToBeClosed, e);
+ } catch (Throwable throwable) {
+ throwable.printStackTrace();
+ }
+ }
+ }
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/CompositeCloser.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/CompositeCloser.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/CompositeCloser.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/CompositeCloser.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.support;
+
+import java.util.Collection;
+
+import com.google.common.collect.Iterables;
+
+/**
+ * A Composite implementation of {@link Closer}
+ *
+ * @version
+ */
+public class CompositeCloser implements Closer {
+ private final Iterable<Closer> closers;
+
+ public CompositeCloser(Iterable<Closer> closers) {
+ this.closers = closers;
+ }
+
+ public void close(Object object) throws Throwable {
+ for (Closer closer : closers) {
+ closer.close(object);
+ }
+ }
+
+ /**
+ * Returns a {@link Closer} for the given lists of closer strategies or
+ * returning null if the collection is empty
+ */
+ public static Closer newInstance(Collection<Closer> closers) {
+ if (closers.isEmpty()) {
+ return null;
+ }
+ if (closers.size() == 1) {
+ return Iterables.getOnlyElement(closers);
+ }
+ return new CompositeCloser(closers);
+ }
+
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/EncounterProvider.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/EncounterProvider.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/EncounterProvider.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/EncounterProvider.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,71 @@
+/**
+ * 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.support;
+
+import com.google.inject.Key;
+import com.google.inject.Provider;
+import com.google.inject.spi.TypeEncounter;
+
+/**
+ * Like a {@link com.google.inject.Provider} but which is also given an
+ * {@link TypeEncounter}
+ *
+ * @version
+ */
+public abstract class EncounterProvider<T> {
+ public abstract Provider<? extends T> get(TypeEncounter<?> encounter);
+
+ /**
+ * Returns a new encounter provider for the given key
+ */
+ public static <T> EncounterProvider<T> encounterProvider(
+ final Key<? extends T> key) {
+ return new EncounterProvider<T>() {
+ public Provider<? extends T> get(TypeEncounter<?> encounter) {
+ return encounter.getProvider(key);
+ }
+ };
+ }
+
+ /**
+ * Returns a new encounter provider for the given type
+ */
+ public static <T> EncounterProvider<T> encounterProvider(
+ final Class<? extends T> type) {
+ return new EncounterProvider<T>() {
+ public Provider<? extends T> get(TypeEncounter<?> encounter) {
+ return encounter.getProvider(type);
+ }
+ };
+ }
+
+ /**
+ * Returns a new encounter provider for the given instance
+ */
+ public static <T> EncounterProvider<T> encounterProvider(final T instance) {
+ return new EncounterProvider<T>() {
+ public Provider<? extends T> get(TypeEncounter<?> encounter) {
+ return new Provider<T>() {
+ public T get() {
+ return instance;
+ }
+ };
+ }
+ };
+ }
+
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/GuiceyFruitModule.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/GuiceyFruitModule.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/GuiceyFruitModule.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/GuiceyFruitModule.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,482 @@
+/**
+ * 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.support;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+import com.google.inject.AbstractModule;
+import com.google.inject.Key;
+import com.google.inject.MembersInjector;
+import com.google.inject.Provider;
+import com.google.inject.ProvisionException;
+import com.google.inject.TypeLiteral;
+import com.google.inject.binder.LinkedBindingBuilder;
+import com.google.inject.matcher.AbstractMatcher;
+import com.google.inject.name.Names;
+import com.google.inject.spi.InjectionListener;
+import com.google.inject.spi.TypeEncounter;
+import com.google.inject.spi.TypeListener;
+
+import org.apache.camel.guice.inject.Configures;
+import org.apache.camel.guice.support.internal.MethodKey;
+
+import static com.google.inject.matcher.Matchers.any;
+import static org.apache.camel.guice.support.EncounterProvider.encounterProvider;
+
+
+/**
+ * Adds some new helper methods to the base Guice module
+ *
+ * @version
+ */
+@SuppressWarnings({ "rawtypes", "unchecked" })
+public abstract class GuiceyFruitModule extends AbstractModule {
+
+ protected void configure() {
+ // lets find all of the configures methods
+ List<Method> configureMethods = getConfiguresMethods();
+ if (!configureMethods.isEmpty()) {
+ final GuiceyFruitModule moduleInstance = this;
+ final Class<? extends GuiceyFruitModule> moduleType = getClass();
+ TypeLiteral<? extends GuiceyFruitModule> type = TypeLiteral
+ .get(moduleType);
+
+ for (final Method method : configureMethods) {
+ int size = method.getParameterTypes().length;
+ if (size == 0) {
+ throw new ProvisionException(
+ "No arguments on @Configures method " + method);
+ } else if (size > 1) {
+ throw new ProvisionException("Too many arguments " + size
+ + " on @Configures method " + method);
+ }
+ final Class<?> paramType = getParameterType(type, method, 0);
+
+ bindListener(new AbstractMatcher<TypeLiteral<?>>() {
+ public boolean matches(TypeLiteral<?> typeLiteral) {
+ return typeLiteral.getRawType().equals(paramType);
+ }
+ }, new TypeListener() {
+ public <I> void hear(TypeLiteral<I> injectableType,
+ TypeEncounter<I> encounter) {
+ encounter.register(new MembersInjector<I>() {
+ public void injectMembers(I injectee) {
+ // lets invoke the configures method
+ try {
+ method.setAccessible(true);
+ method.invoke(moduleInstance, injectee);
+ } catch (IllegalAccessException e) {
+ throw new ProvisionException(
+ "Failed to invoke @Configures method "
+ + method + ". Reason: " + e,
+ e);
+ } catch (InvocationTargetException ie) {
+ Throwable e = ie.getTargetException();
+ throw new ProvisionException(
+ "Failed to invoke @Configures method "
+ + method + ". Reason: " + e,
+ e);
+ }
+ }
+ });
+ }
+ });
+ }
+ }
+ }
+
+ private List<Method> getConfiguresMethods() {
+ List<Method> answer = Lists.newArrayList();
+ List<Method> list = Reflectors.getAllMethods(getClass());
+ for (Method method : list) {
+ if (method.getAnnotation(Configures.class) != null) {
+ answer.add(method);
+ }
+ }
+ return answer;
+ }
+
+ /**
+ * Binds a post injection hook method annotated with the given annotation to
+ * the given method handler.
+ */
+ protected <A extends Annotation> void bindMethodHandler(
+ final Class<A> annotationType, final MethodHandler methodHandler) {
+
+ bindMethodHandler(annotationType, encounterProvider(methodHandler));
+ }
+
+ /**
+ * Binds a post injection hook method annotated with the given annotation to
+ * the given method handler.
+ */
+ protected <A extends Annotation> void bindMethodHandler(
+ final Class<A> annotationType,
+ final Key<? extends MethodHandler> methodHandlerKey) {
+
+ bindMethodHandler(annotationType, encounterProvider(methodHandlerKey));
+ }
+
+ /**
+ * Binds a post injection hook method annotated with the given annotation to
+ * the given method handler.
+ */
+ protected <A extends Annotation> void bindMethodHandler(
+ final Class<A> annotationType,
+ final Class<? extends MethodHandler> methodHandlerType) {
+
+ bindMethodHandler(annotationType, encounterProvider(methodHandlerType));
+ }
+
+ private <A extends Annotation> void bindMethodHandler(
+ final Class<A> annotationType,
+ final EncounterProvider<MethodHandler> encounterProvider) {
+
+ bindListener(any(), new TypeListener() {
+ public <I> void hear(TypeLiteral<I> injectableType,
+ TypeEncounter<I> encounter) {
+ Class<? super I> type = injectableType.getRawType();
+ Method[] methods = type.getDeclaredMethods();
+ for (final Method method : methods) {
+ final A annotation = method.getAnnotation(annotationType);
+ if (annotation != null) {
+ final Provider<? extends MethodHandler> provider = encounterProvider
+ .get(encounter);
+
+ encounter.register(new InjectionListener<I>() {
+ public void afterInjection(I injectee) {
+
+ MethodHandler methodHandler = provider.get();
+ try {
+ methodHandler.afterInjection(injectee,
+ annotation, method);
+ } catch (InvocationTargetException ie) {
+ Throwable e = ie.getTargetException();
+ throw new ProvisionException(
+ e.getMessage(), e);
+ } catch (IllegalAccessException e) {
+ throw new ProvisionException(
+ e.getMessage(), e);
+ }
+ }
+ });
+ }
+ }
+ }
+ });
+ }
+
+ /**
+ * Binds a custom injection point for a given injection annotation to the
+ * annotation member provider so that occurrences of the annotation on
+ * fields and methods with a single parameter will be injected by Guice
+ * after the constructor and @Inject have been processed.
+ *
+ * @param annotationType
+ * the annotation class used to define the injection point
+ * @param annotationMemberProviderKey
+ * the key of the annotation member provider which can be
+ * instantiated and injected by guice
+ * @param <A>
+ * the annotation type used as the injection point
+ */
+ protected <A extends Annotation> void bindAnnotationInjector(
+ Class<A> annotationType,
+ Key<? extends AnnotationMemberProvider> annotationMemberProviderKey) {
+
+ bindAnnotationInjector(annotationType,
+ encounterProvider(annotationMemberProviderKey));
+ }
+
+ /**
+ * Binds a custom injection point for a given injection annotation to the
+ * annotation member provider so that occurrences of the annotation on
+ * fields and methods with a single parameter will be injected by Guice
+ * after the constructor and @Inject have been processed.
+ *
+ * @param annotationType
+ * the annotation class used to define the injection point
+ * @param annotationMemberProvider
+ * the annotation member provider which can be instantiated and
+ * injected by guice
+ * @param <A>
+ * the annotation type used as the injection point
+ */
+ protected <A extends Annotation> void bindAnnotationInjector(
+ Class<A> annotationType,
+ AnnotationMemberProvider annotationMemberProvider) {
+
+ bindAnnotationInjector(annotationType,
+ encounterProvider(annotationMemberProvider));
+ }
+
+ /**
+ * Binds a custom injection point for a given injection annotation to the
+ * annotation member provider so that occurrences of the annotation on
+ * fields and methods with a single parameter will be injected by Guice
+ * after the constructor and @Inject have been processed.
+ *
+ * @param annotationType
+ * the annotation class used to define the injection point
+ * @param annotationMemberProviderType
+ * the type of the annotation member provider which can be
+ * instantiated and injected by guice
+ * @param <A>
+ * the annotation type used as the injection point
+ */
+ protected <A extends Annotation> void bindAnnotationInjector(
+ Class<A> annotationType,
+ Class<? extends AnnotationMemberProvider> annotationMemberProviderType) {
+
+ bindAnnotationInjector(annotationType,
+ encounterProvider(annotationMemberProviderType));
+ }
+
+ private <A extends Annotation> void bindAnnotationInjector(
+ final Class<A> annotationType,
+ final EncounterProvider<AnnotationMemberProvider> memberProviderProvider) {
+
+ bindListener(any(), new TypeListener() {
+ Provider<? extends AnnotationMemberProvider> providerProvider;
+
+ public <I> void hear(TypeLiteral<I> injectableType,
+ TypeEncounter<I> encounter) {
+
+ Set<Field> boundFields = Sets.newHashSet();
+ Map<MethodKey, Method> boundMethods = Maps.newHashMap();
+
+ TypeLiteral<?> startType = injectableType;
+ while (true) {
+ Class<?> type = startType.getRawType();
+ if (type == Object.class) {
+ break;
+ }
+
+ Field[] fields = type.getDeclaredFields();
+ for (Field field : fields) {
+ if (boundFields.add(field)) {
+ bindAnnotationInjectorToField(encounter, startType,
+ field);
+ }
+ }
+
+ Method[] methods = type.getDeclaredMethods();
+ for (final Method method : methods) {
+ MethodKey key = new MethodKey(method);
+ if (boundMethods.get(key) == null) {
+ boundMethods.put(key, method);
+ bindAnnotationInjectionToMember(encounter,
+ startType, method);
+ }
+ }
+
+ Class<?> supertype = type.getSuperclass();
+ if (supertype == Object.class) {
+ break;
+ }
+ startType = startType.getSupertype(supertype);
+ }
+ }
+
+ protected <I> void bindAnnotationInjectionToMember(
+ final TypeEncounter<I> encounter,
+ final TypeLiteral<?> type, final Method method) {
+ // TODO lets exclude methods with @Inject?
+ final A annotation = method.getAnnotation(annotationType);
+ if (annotation != null) {
+ if (providerProvider == null) {
+ providerProvider = memberProviderProvider
+ .get(encounter);
+ }
+
+ encounter.register(new MembersInjector<I>() {
+ public void injectMembers(I injectee) {
+ AnnotationMemberProvider provider = providerProvider
+ .get();
+
+ int size = method.getParameterTypes().length;
+ Object[] values = new Object[size];
+ for (int i = 0; i < size; i++) {
+ Class<?> paramType = getParameterType(type,
+ method, i);
+ Object value = provider.provide(annotation,
+ type, method, paramType, i);
+ checkInjectedValueType(value, paramType,
+ encounter);
+
+ // if we have a null value then assume the
+ // injection point cannot be satisfied
+ // which is the spring @Autowired way of doing
+ // things
+ if (value == null
+ && !provider.isNullParameterAllowed(
+ annotation, method, paramType,
+ i)) {
+ return;
+ }
+ values[i] = value;
+ }
+ try {
+ method.setAccessible(true);
+ method.invoke(injectee, values);
+ } catch (IllegalAccessException e) {
+ throw new ProvisionException(
+ "Failed to inject method " + method
+ + ". Reason: " + e, e);
+ } catch (InvocationTargetException ie) {
+ Throwable e = ie.getTargetException();
+ throw new ProvisionException(
+ "Failed to inject method " + method
+ + ". Reason: " + e, e);
+ }
+ }
+ });
+ }
+ }
+
+ protected <I> void bindAnnotationInjectorToField(
+ final TypeEncounter<I> encounter,
+ final TypeLiteral<?> type, final Field field) {
+ // TODO lets exclude fields with @Inject?
+ final A annotation = field.getAnnotation(annotationType);
+ if (annotation != null) {
+ if (providerProvider == null) {
+ providerProvider = memberProviderProvider
+ .get(encounter);
+ }
+
+ encounter.register(new InjectionListener<I>() {
+ public void afterInjection(I injectee) {
+ AnnotationMemberProvider provider = providerProvider
+ .get();
+ Object value = provider.provide(annotation, type,
+ field);
+ checkInjectedValueType(value, field.getType(),
+ encounter);
+
+ try {
+ field.setAccessible(true);
+ field.set(injectee, value);
+ } catch (IllegalAccessException e) {
+ throw new ProvisionException(
+ "Failed to inject field " + field
+ + ". Reason: " + e, e);
+ }
+ }
+ });
+ }
+ }
+ });
+ }
+
+ protected Class<?> getParameterType(TypeLiteral<?> type, Method method,
+ int i) {
+ Class<?>[] parameterTypes = method.getParameterTypes();
+ List<TypeLiteral<?>> list = type.getParameterTypes(method);
+ TypeLiteral<?> typeLiteral = list.get(i);
+
+ Class<?> paramType = typeLiteral.getRawType();
+ if (paramType == Object.class || paramType.isArray()
+ && paramType.getComponentType() == Object.class) {
+ // if the TypeLiteral ninja doesn't work, lets fall back to the
+ // actual type
+ paramType = parameterTypes[i];
+ }
+ return paramType;
+ }
+
+ /*
+ * protected void bindCloseHook() { bindListener(any(), new Listener() {
+ * public <I> void hear(InjectableType<I> injectableType, Encounter<I>
+ * encounter) { encounter.registerPostInjectListener(new
+ * InjectionListener<I>() { public void afterInjection(I injectee) {
+ *
+ * } }); } }); }
+ */
+
+ /**
+ * Returns true if the value to be injected is of the correct type otherwise
+ * an error is raised on the encounter and false is returned
+ */
+ protected <I> void checkInjectedValueType(Object value, Class<?> type,
+ TypeEncounter<I> encounter) {
+ // TODO check the type
+ }
+
+ /**
+ * A helper method to bind the given type with the binding annotation.
+ *
+ * This allows you to replace this code
+ * <code> bind(Key.get(MyType.class, SomeAnnotation.class))
+ * </code>
+ *
+ * with this <code> bind(KMyType.class, SomeAnnotation.class) </code>
+ */
+ protected <T> LinkedBindingBuilder<T> bind(Class<T> type,
+ Class<? extends Annotation> annotationType) {
+ return bind(Key.get(type, annotationType));
+ }
+
+ /**
+ * A helper method to bind the given type with the binding annotation.
+ *
+ * This allows you to replace this code
+ * <code> bind(Key.get(MyType.class, someAnnotation))
+ * </code>
+ *
+ * with this <code> bind(KMyType.class, someAnnotation) </code>
+ */
+ protected <T> LinkedBindingBuilder<T> bind(Class<T> type,
+ Annotation annotation) {
+ return bind(Key.get(type, annotation));
+ }
+
+ /**
+ * A helper method to bind the given type with the
+ * {@link com.google.inject.name.Named} annotation of the given text value.
+ *
+ * This allows you to replace this code
+ * <code> bind(Key.get(MyType.class, Names.named("myName")))
+ * </code>
+ *
+ * with this <code> bind(KMyType.class, "myName") </code>
+ */
+ protected <T> LinkedBindingBuilder<T> bind(Class<T> type, String namedText) {
+ return bind(type, Names.named(namedText));
+ }
+
+ /**
+ * A helper method which binds a named instance to a key defined by the
+ * given name and the instances type. So this method is short hand for
+ *
+ * <code> bind(instance.getClass(), name).toInstance(instance); </code>
+ */
+ protected <T> void bindInstance(String name, T instance) {
+ // TODO not sure the generics ninja to avoid this cast
+ Class<T> aClass = (Class<T>) instance.getClass();
+ bind(aClass, name).toInstance(instance);
+ }
+}
\ No newline at end of file
Copied: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/HasScopeAnnotation.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/support/HasScopeAnnotation.java?p2=camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/HasScopeAnnotation.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/support/HasScopeAnnotation.java Sat Jan 5 15:14:02 2013
@@ -14,25 +14,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.camel.guice;
+package org.apache.camel.guice.support;
+
+import java.lang.annotation.Annotation;
-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
+ * There's no simple way to associate a Scope with an annotation which often
+ * leads Scoping to return null. This interface allows Scope implementations to
+ * expose its scope annotation
+ *
+ * @version
*/
-public class ConciseGuiceRouteTest extends Assert {
-
- @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
+public interface HasScopeAnnotation {
+ /**
+ * Returns the scope annotation associated with this object
+ */
+ Class<? extends Annotation> getScopeAnnotation();
+}
Copied: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/MethodHandler.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/support/MethodHandler.java?p2=camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/MethodHandler.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/support/MethodHandler.java Sat Jan 5 15:14:02 2013
@@ -14,25 +14,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.camel.guice;
+package org.apache.camel.guice.support;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
-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
+ * Allows a method with a given annotation {@code A} on an injectee of type
+ * {@code I} to be processed in some way on each injectee using a custom
+ * strategy.
+ *
+ * @version
*/
-public class ConciseGuiceRouteTest extends Assert {
-
- @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);
- }
+public interface MethodHandler<I, A extends Annotation> {
-}
\ No newline at end of file
+ /**
+ * Process the given method which is annotated with the annotation on the
+ * injectee after the injectee has been injected
+ */
+ void afterInjection(I injectee, A annotation, Method method)
+ throws InvocationTargetException, IllegalAccessException;
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/Reflectors.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/Reflectors.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/Reflectors.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/Reflectors.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,71 @@
+/**
+ * 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.support;
+
+import java.lang.reflect.Method;
+import java.util.List;
+import java.util.Map;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.inject.TypeLiteral;
+
+import org.apache.camel.guice.support.internal.MethodKey;
+
+
+/**
+ * Some reflection helper methods
+ *
+ */
+public final class Reflectors {
+ private Reflectors() {
+ //Helper class
+ }
+ /** Returns all the methods on the given type ignoring overloaded methods */
+ public static List<Method> getAllMethods(Class<?> type) {
+ return getAllMethods(TypeLiteral.get(type));
+ }
+
+ /** Returns all the methods on the given type ignoring overloaded methods */
+ public static List<Method> getAllMethods(TypeLiteral<?> startType) {
+ List<Method> answer = Lists.newArrayList();
+ Map<MethodKey, Method> boundMethods = Maps.newHashMap();
+ while (true) {
+ Class<?> type = startType.getRawType();
+ if (type == Object.class) {
+ break;
+ }
+
+ Method[] methods = type.getDeclaredMethods();
+ for (final Method method : methods) {
+ MethodKey key = new MethodKey(method);
+ if (boundMethods.get(key) == null) {
+ boundMethods.put(key, method);
+ answer.add(method);
+ }
+ }
+
+ // startType = startType.getSupertype(type);
+ Class<?> supertype = type.getSuperclass();
+ if (supertype == Object.class) {
+ break;
+ }
+ startType = startType.getSupertype(supertype);
+ }
+ return answer;
+ }
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/internal/CloseErrorsImpl.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/internal/CloseErrorsImpl.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/internal/CloseErrorsImpl.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/internal/CloseErrorsImpl.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,50 @@
+/**
+ * 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.support.internal;
+
+import com.google.inject.internal.Errors;
+import com.google.inject.spi.Message;
+
+import org.apache.camel.guice.support.CloseErrors;
+import org.apache.camel.guice.support.CloseFailedException;
+
+/**
+ * The default implementation of @{link CloseErrors}
+ *
+ * @version
+ */
+public class CloseErrorsImpl implements CloseErrors {
+ final Errors errors;
+
+ public CloseErrorsImpl(Object source) {
+ this.errors = new Errors(source);
+ }
+
+ public void closeError(Object key, Object object, Exception cause) {
+ String message = Errors.format("Failed to close object %s with key %s",
+ object, key);
+ errors.addMessage(new Message(errors.getSources(), message, cause));
+ }
+
+ public void throwIfNecessary() throws CloseFailedException {
+ if (!errors.hasErrors()) {
+ return;
+ }
+
+ throw new CloseFailedException(errors.getMessages());
+ }
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/internal/MethodKey.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/internal/MethodKey.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/internal/MethodKey.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/support/internal/MethodKey.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,67 @@
+/**
+ * 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.support.internal;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+
+/**
+ * A key of methods comparing the method name and parameter types
+ *
+ * @version
+ */
+public class MethodKey {
+ private final String name;
+ private final Class<?>[] parameterTypes;
+
+ public MethodKey(String name, Class<?>[] parameterTypes) {
+ this.name = name;
+ this.parameterTypes = parameterTypes;
+ }
+
+ public MethodKey(Method method) {
+ this(method.getName(), method.getParameterTypes());
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ MethodKey methodKey = (MethodKey) o;
+
+ if (!name.equals(methodKey.name)) {
+ return false;
+ }
+ if (!Arrays.equals(parameterTypes, methodKey.parameterTypes)) {
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ int result = name.hashCode();
+ result = 31 * result + Arrays.hashCode(parameterTypes);
+ return result;
+ }
+}
Copied: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/util/CachedValue.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/util/CachedValue.java?p2=camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/util/CachedValue.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/util/CachedValue.java Sat Jan 5 15:14:02 2013
@@ -14,25 +14,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.camel.guice;
+package org.apache.camel.guice.util;
-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
+ * @version
*/
-public class ConciseGuiceRouteTest extends Assert {
-
- @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
+public interface CachedValue {
+ Object getCachedValue();
+}
Copied: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/util/CachingProvider.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/util/CachingProvider.java?p2=camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/util/CachingProvider.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/util/CachingProvider.java Sat Jan 5 15:14:02 2013
@@ -14,25 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.camel.guice;
+package org.apache.camel.guice.util;
+
+import com.google.inject.Provider;
-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
+ * @version
*/
-public class ConciseGuiceRouteTest extends Assert {
-
- @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);
- }
+public interface CachingProvider<T> extends Provider<T>, CachedValue {
-}
\ No newline at end of file
+}
Added: camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/util/CloseableScope.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/util/CloseableScope.java?rev=1429309&view=auto
==============================================================================
--- camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/util/CloseableScope.java (added)
+++ camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/util/CloseableScope.java Sat Jan 5 15:14:02 2013
@@ -0,0 +1,105 @@
+/**
+ * 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.util;
+
+import java.lang.annotation.Annotation;
+import java.util.Map;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Maps;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.google.inject.Key;
+import com.google.inject.Provider;
+import com.google.inject.Scope;
+
+import org.apache.camel.guice.inject.Injectors;
+import org.apache.camel.guice.support.CloseFailedException;
+import org.apache.camel.guice.support.HasScopeAnnotation;
+import org.apache.camel.guice.support.internal.CloseErrorsImpl;
+
+
+/**
+ * Represents a scope which caches objects around until the scope is closed.
+ *
+ * The scope can be closed as many times as required - there is no need to
+ * recreate the scope instance each time a scope goes out of scope.
+ *
+ * @version
+ */
+public class CloseableScope implements Scope, HasScopeAnnotation {
+
+ private Class<? extends Annotation> scopeAnnotation;
+ private final Map<Key<?>, Object> map = Maps.newHashMap();
+
+ @Inject
+ private Injector injector;
+
+ public CloseableScope(Class<? extends Annotation> scopeAnnotation) {
+ this.scopeAnnotation = scopeAnnotation;
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T> Provider<T> scope(final Key<T> key, final Provider<T> creator) {
+ return new CachingProvider<T>() {
+ public T get() {
+ Object o;
+ synchronized (map) {
+ o = map.get(key);
+ if (o == null) {
+ o = creator.get();
+ map.put(key, o);
+ }
+ }
+ return (T) o;
+ }
+
+ public T getCachedValue() {
+ synchronized (map) {
+ return (T) map.get(key);
+ }
+ }
+ };
+ }
+
+ /**
+ * Closes all of the objects within this scope using the given injector and
+ * scope annotation and clears the scope
+ */
+ public void close() throws CloseFailedException {
+ close(injector);
+ }
+
+ /**
+ * Closes all of the objects within the given injector of the specified
+ * scope and clears the scope
+ */
+ public void close(Injector injector) throws CloseFailedException {
+ Preconditions.checkNotNull(injector, "injector");
+ CloseErrorsImpl errors = new CloseErrorsImpl(this);
+ Injectors.close(injector, scopeAnnotation, errors);
+
+ synchronized (map) {
+ map.clear();
+ }
+ errors.throwIfNecessary();
+ }
+
+ public Class<? extends Annotation> getScopeAnnotation() {
+ return scopeAnnotation;
+ }
+}
Modified: camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/CollectionOfCustomRoutesTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/CollectionOfCustomRoutesTest.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/CollectionOfCustomRoutesTest.java (original)
+++ camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/CollectionOfCustomRoutesTest.java Sat Jan 5 15:14:02 2013
@@ -19,10 +19,10 @@ package org.apache.camel.guice;
import java.util.Collection;
import java.util.List;
+import com.google.common.collect.Lists;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Provides;
-import com.google.inject.internal.Lists;
import com.google.inject.name.Named;
import org.apache.camel.CamelContext;
Modified: camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ComponentFoundInRegistryTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ComponentFoundInRegistryTest.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ComponentFoundInRegistryTest.java (original)
+++ camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ComponentFoundInRegistryTest.java Sat Jan 5 15:14:02 2013
@@ -17,7 +17,6 @@
package org.apache.camel.guice;
import java.util.Hashtable;
-
import javax.naming.InitialContext;
import com.google.inject.Injector;
@@ -28,9 +27,9 @@ import org.apache.camel.Component;
import org.apache.camel.Endpoint;
import org.apache.camel.component.mock.MockComponent;
import org.apache.camel.component.mock.MockEndpoint;
-import org.guiceyfruit.Injectors;
-import org.guiceyfruit.jndi.GuiceInitialContextFactory;
-import org.guiceyfruit.jndi.JndiBind;
+import org.apache.camel.guice.inject.Injectors;
+import org.apache.camel.guice.jndi.GuiceInitialContextFactory;
+import org.apache.camel.guice.jndi.JndiBind;
import org.junit.Assert;
import org.junit.Test;
Modified: 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/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java (original)
+++ camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/ConciseGuiceRouteTest.java Sat Jan 5 15:14:02 2013
@@ -18,6 +18,7 @@ package org.apache.camel.guice;
import com.google.inject.Guice;
import com.google.inject.Injector;
+
import org.junit.Assert;
import org.junit.Test;
/**
Modified: camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/EndpointInjectionTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/EndpointInjectionTest.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/EndpointInjectionTest.java (original)
+++ camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/EndpointInjectionTest.java Sat Jan 5 15:14:02 2013
@@ -18,10 +18,10 @@ package org.apache.camel.guice;
import java.util.Collection;
+import com.google.common.collect.Lists;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Provides;
-import com.google.inject.internal.Lists;
import com.google.inject.name.Named;
import org.apache.camel.EndpointInject;
Modified: camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/FileEndpointReferenceRouteTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/FileEndpointReferenceRouteTest.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/FileEndpointReferenceRouteTest.java (original)
+++ camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/FileEndpointReferenceRouteTest.java Sat Jan 5 15:14:02 2013
@@ -17,17 +17,19 @@
package org.apache.camel.guice;
import java.util.Hashtable;
+
import javax.naming.InitialContext;
import com.google.inject.Injector;
import com.google.inject.Provides;
+
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.file.GenericFileFilter;
-import org.guiceyfruit.Injectors;
-import org.guiceyfruit.jndi.GuiceInitialContextFactory;
-import org.guiceyfruit.jndi.JndiBind;
+import org.apache.camel.guice.inject.Injectors;
+import org.apache.camel.guice.jndi.GuiceInitialContextFactory;
+import org.apache.camel.guice.jndi.JndiBind;
import org.junit.Assert;
import org.junit.Test;
Modified: camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/GuiceRouteWithNamedKeysTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/GuiceRouteWithNamedKeysTest.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/GuiceRouteWithNamedKeysTest.java (original)
+++ camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/GuiceRouteWithNamedKeysTest.java Sat Jan 5 15:14:02 2013
@@ -15,7 +15,6 @@
* limitations under the License.
*/
package org.apache.camel.guice;
-
import java.util.Collection;
import com.google.inject.Guice;
@@ -27,14 +26,14 @@ import com.google.inject.name.Named;
import com.google.inject.name.Names;
import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.guice.inject.Injectors;
import org.apache.camel.test.junit4.TestSupport;
-import org.guiceyfruit.Injectors;
import org.junit.Test;
/**
* Lets use a custom CamelModule to perform explicit binding of route builders
- *
- * @version
+ *
+ * @version
*/
public class GuiceRouteWithNamedKeysTest extends TestSupport {
@@ -51,12 +50,14 @@ public class GuiceRouteWithNamedKeysTest
public void testGuice() throws Exception {
Injector injector = Guice.createInjector(new MyModule());
- MyConfigurableRoute2 instance = injector.getInstance(Key.get(MyConfigurableRoute2.class, Names.named("foo")));
+ MyConfigurableRoute2 instance = injector.getInstance(Key.get(MyConfigurableRoute2.class,
+ Names.named("foo")));
assertNotNull("should have found a key for 'foo'", instance);
log.info("Found instance: " + instance);
- //List<Binding<RouteBuilder>> list = injector.findBindingsByType(TypeLiteral.get(RouteBuilder.class));
+ // List<Binding<RouteBuilder>> list =
+ // injector.findBindingsByType(TypeLiteral.get(RouteBuilder.class));
Collection<RouteBuilder> list = Injectors.getInstancesOf(injector, RouteBuilder.class);
log.info("RouteBuilder List: " + list);
@@ -66,21 +67,19 @@ public class GuiceRouteWithNamedKeysTest
log.info("RouteBuilder List: " + list);
assertEquals("route builder list: " + list, 1, list.size());
-/*
-
- list = Injectors.getInstancesOf(injector, Matchers.subclassesOf(RouteBuilder.class).and(Matchers.annotatedWith(Names.named("foo"))));
- log.info("RouteBuilder List: " + list);
-
- assertEquals("route builder list: " + list, 1, list.size());
-
- list = Injectors.getInstancesOf(injector, Matchers.subclassesOf(RouteBuilder.class).and(Matchers.annotatedWith(Names.named("bar"))));
- log.info("RouteBuilder List: " + list);
-
- assertEquals("route builder list: " + list, 0, list.size());
-*/
+ /*
+ * list = Injectors.getInstancesOf(injector,
+ * Matchers.subclassesOf(RouteBuilder
+ * .class).and(Matchers.annotatedWith(Names.named("foo"))));
+ * log.info("RouteBuilder List: " + list);
+ * assertEquals("route builder list: " + list, 1, list.size()); list =
+ * Injectors.getInstancesOf(injector, Matchers.subclassesOf(RouteBuilder
+ * .class).and(Matchers.annotatedWith(Names.named("bar"))));
+ * log.info("RouteBuilder List: " + list);
+ * assertEquals("route builder list: " + list, 0, list.size());
+ */
GuiceTest.assertCamelContextRunningThenCloseInjector(injector);
}
-
}
\ No newline at end of file
Modified: camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/GuiceTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/GuiceTest.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/GuiceTest.java (original)
+++ camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/GuiceTest.java Sat Jan 5 15:14:02 2013
@@ -21,25 +21,26 @@ import com.google.inject.Inject;
import com.google.inject.Injector;
import org.apache.camel.CamelContext;
+import org.apache.camel.guice.inject.Injectors;
import org.apache.camel.test.junit4.TestSupport;
-import org.guiceyfruit.Injectors;
import org.junit.Test;
/**
- * @version
+ * @version
*/
public class GuiceTest extends TestSupport {
/**
- * Asserts that the CamelContext is available in the given Injector, that its been started, then close the injector
- *
+ * Asserts that the CamelContext is available in the given Injector, that
+ * its been started, then close the injector
+ *
* @param injector
*/
public static void assertCamelContextRunningThenCloseInjector(Injector injector) throws Exception {
CamelContext camelContext = injector.getInstance(CamelContext.class);
org.hamcrest.MatcherAssert.assertThat(camelContext, org.hamcrest.Matchers.is(GuiceCamelContext.class));
- GuiceCamelContext guiceContext = (GuiceCamelContext) camelContext;
+ GuiceCamelContext guiceContext = (GuiceCamelContext)camelContext;
assertTrue("is started!", guiceContext.isStarted());
Thread.sleep(1000);
Modified: camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/consume/ConsumeTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/consume/ConsumeTest.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/consume/ConsumeTest.java (original)
+++ camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/consume/ConsumeTest.java Sat Jan 5 15:14:02 2013
@@ -21,14 +21,12 @@ import org.apache.camel.Consume;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.guice.CamelModuleWithMatchingRoutes;
-import org.guiceyfruit.testing.UseModule;
-import org.guiceyfruit.testing.junit4.GuiceyJUnit4;
+import org.apache.camel.guice.testing.UseModule;
+import org.apache.camel.guice.testing.junit4.GuiceyJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
-
-
/**
* @version
*/
Modified: camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/patterns/FilterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/patterns/FilterTest.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/patterns/FilterTest.java (original)
+++ camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/patterns/FilterTest.java Sat Jan 5 15:14:02 2013
@@ -23,8 +23,8 @@ import org.apache.camel.ProducerTemplate
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.guice.CamelModuleWithMatchingRoutes;
-import org.guiceyfruit.testing.UseModule;
-import org.guiceyfruit.testing.junit4.GuiceyJUnit4;
+import org.apache.camel.guice.testing.UseModule;
+import org.apache.camel.guice.testing.junit4.GuiceyJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
Modified: camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/produce/ProduceTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/produce/ProduceTest.java?rev=1429309&r1=1429308&r2=1429309&view=diff
==============================================================================
--- camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/produce/ProduceTest.java (original)
+++ camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/produce/ProduceTest.java Sat Jan 5 15:14:02 2013
@@ -18,8 +18,8 @@ package org.apache.camel.guice.produce;
import org.apache.camel.Produce;
import org.apache.camel.guice.CamelModuleWithMatchingRoutes;
-import org.guiceyfruit.testing.UseModule;
-import org.guiceyfruit.testing.junit4.GuiceyJUnit4;
+import org.apache.camel.guice.testing.UseModule;
+import org.apache.camel.guice.testing.junit4.GuiceyJUnit4;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Copied: camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/testing/ClassScoped.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/test/java/org/apache/camel/guice/testing/ClassScoped.java?p2=camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/testing/ClassScoped.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/test/java/org/apache/camel/guice/testing/ClassScoped.java Sat Jan 5 15:14:02 2013
@@ -14,25 +14,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.camel.guice;
+package org.apache.camel.guice.testing;
-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.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
- @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);
- }
+import com.google.inject.ScopeAnnotation;
+/**
+ * This defines a {@link com.google.inject.Scope} that lasts for all the test
+ * methods within a class
+ *
+ * @see org.apache.camel.guice.util.CloseableScope for an implementation of this
+ * scope
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+@ScopeAnnotation
+public @interface ClassScoped {
}
\ No newline at end of file