diff options
author | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2009-02-26 06:55:29 +0000 |
---|---|---|
committer | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2009-02-26 06:55:29 +0000 |
commit | 3e2bf547eb6ef513e6ab9a8279b52b9760f79942 (patch) | |
tree | b81bb3d36ecc20edac1d9c729e1b007a4b4de9eb /java | |
parent | 70b8c9ded494eece9cf2e0bbc2063acac6387473 (diff) |
Get the Stripes web framework integration working by finishing the Helper class
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@748042 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java')
-rw-r--r-- | java/sca/modules/stripes/pom.xml | 5 | ||||
-rw-r--r-- | java/sca/modules/stripes/src/main/java/org/apache/tuscany/sca/stripes/TuscanyHelper.java | 166 |
2 files changed, 81 insertions, 90 deletions
diff --git a/java/sca/modules/stripes/pom.xml b/java/sca/modules/stripes/pom.xml index 0aceedc12d..95957985c5 100644 --- a/java/sca/modules/stripes/pom.xml +++ b/java/sca/modules/stripes/pom.xml @@ -36,6 +36,11 @@ <artifactId>tuscany-host-webapp</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-implementation-web-runtime</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ </dependency>
<dependency>
<groupId>net.sourceforge.stripes</groupId>
diff --git a/java/sca/modules/stripes/src/main/java/org/apache/tuscany/sca/stripes/TuscanyHelper.java b/java/sca/modules/stripes/src/main/java/org/apache/tuscany/sca/stripes/TuscanyHelper.java index fc3b1f4b90..1ee0d9e287 100644 --- a/java/sca/modules/stripes/src/main/java/org/apache/tuscany/sca/stripes/TuscanyHelper.java +++ b/java/sca/modules/stripes/src/main/java/org/apache/tuscany/sca/stripes/TuscanyHelper.java @@ -19,16 +19,6 @@ package org.apache.tuscany.sca.stripes; -import net.sourceforge.stripes.action.ActionBeanContext; -import net.sourceforge.stripes.controller.StripesFilter; -import net.sourceforge.stripes.exception.StripesRuntimeException; -import net.sourceforge.stripes.util.Log; -import net.sourceforge.stripes.util.ReflectUtil; -import org.springframework.context.ApplicationContext; -import org.springframework.core.NestedRuntimeException; -import org.springframework.web.context.support.WebApplicationContextUtils; - -import javax.servlet.ServletContext; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Collection; @@ -36,50 +26,57 @@ import java.util.Iterator; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import javax.servlet.ServletContext; + +import net.sourceforge.stripes.action.ActionBeanContext; +import net.sourceforge.stripes.controller.StripesFilter; +import net.sourceforge.stripes.exception.StripesRuntimeException; +import net.sourceforge.stripes.util.Log; +import net.sourceforge.stripes.util.ReflectUtil; + +import org.apache.tuscany.sca.implementation.web.runtime.WebApplicationUtils; +import org.oasisopen.sca.annotation.Reference; + /** - * <p>Static helper class that is used to lookup Spring beans and inject them into objects - * (often ActionBeans). Is capable of injecting beans through setter methods (property access) + * <p>Static helper class that is used to lookup SCA references and inject them into objects + * (often ActionBeans). Is capable of injecting references through setter methods (property access) * and also through direct field access if the security policy allows it. Methods and fields - * must be annotated using the {@code @SpringBean} annotation.</p> + * must be annotated using the SCA {@code @Reference} annotation.</p> * * <p>Methods and fields may be public, protected, package-access or private. If they are not * public an attempt is made to call {@link Method#setAccessible(boolean)} in order to make * them accessible from this class. If the attempt fails, an exception will be thrown.</p> * * <p>Method names can take any form. For example {@code setSomeBean(Bean b)} or - * {@code someBean(bean b)}. In both cases, if a specific SpringBean name is not supplied, + * {@code someBean(bean b)}. In both cases, if a specific Reference name is not supplied, * the default name of {@code someBean} will be used.</p> * - * <p>The value of the {@code @SpringBean} annotation should be the bean name in the Spring - * application context if it is different from the field/property name. If the value - * is left blank, an attempt is made to auto-wire the bean; first by field/property name and - * then by type. If the value is left blank and more than one bean of the same type is found, - * an exception will be raised.</p> + * <p>The value of the {@code @Reference} annotation should be the reference on the + * SCA component with an {@code <implementation.web>} componentType. * * <p>The first time that any of the injection methods in this class is called with a specific type * of object, the object's class is examined for annotated fields and methods. The discovered * fields and methods are then cached for future usage.</p> * - * @see SpringBean - * @author Dan Hayes, Tim Fennell + * Created for Tuscany from the Stripes SpringHelper written by Dan Hayes and Tim Fennell */ public class TuscanyHelper { private static final Log log = Log.getInstance(TuscanyHelper.class); - /** Lazily filled in map of Class to methods annotated with SpringBean. */ + /** Lazily filled in map of Class to methods annotated with Reference. */ private static Map<Class<?>, Collection<Method>> methodMap = new ConcurrentHashMap<Class<?>, Collection<Method>>(); - /** Lazily filled in map of Class to fields annotated with SpringBean. */ + /** Lazily filled in map of Class to fields annotated with Reference. */ private static Map<Class<?>, Collection<Field>> fieldMap = new ConcurrentHashMap<Class<?>, Collection<Field>>(); /** - * Injects Spring managed beans into using a Web Application Context that is + * Injects SCA References using the ComponentContext that is * derived from the ServletContext, which is in turn looked up using the * ActionBeanContext. * - * @param bean the object into which to inject spring managed bean + * @param bean the object into which to inject SCA reference * @param context the ActionBeanContext represented by the current request */ public static void injectBeans(Object bean, ActionBeanContext context) { @@ -87,39 +84,27 @@ public class TuscanyHelper { } /** - * Injects Spring managed beans using a Web Application Context derived from - * the ServletContext. - * - * @param bean the object to have beans injected into - * @param ctx the ServletContext to use to find the Spring ApplicationContext - */ - public static void injectBeans(Object bean, ServletContext ctx) { - ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(ctx); - injectBeans(bean, ac); - } - - /** - * Looks for all methods and fields annotated with {@code @SpringBean} and attempts + * Looks for all methods and fields annotated with {@code @Reference} and attempts * to lookup and inject a managed bean into the field/property. If any annotated * element cannot be injected an exception is thrown. * - * @param bean the bean into which to inject spring beans - * @param ctx the Spring application context + * @param bean the bean into which to inject SCA reference + * @param ctx the SCA ComponentContext */ - public static void injectBeans(Object bean, ApplicationContext ctx) { + public static void injectBeans(Object bean, ServletContext ctx) { // First inject any values using annotated methods for (Method m : getMethods(bean.getClass())) { try { - SpringBean springBean = m.getAnnotation(SpringBean.class); - boolean nameSupplied = !"".equals(springBean.value()); - String name = nameSupplied ? springBean.value() : methodToPropertyName(m); + Reference scaReference = m.getAnnotation(Reference.class); + boolean nameSupplied = !"".equals(scaReference.name()); + String name = nameSupplied ? scaReference.name() : methodToPropertyName(m); Class<?> beanType = m.getParameterTypes()[0]; - Object managedBean = findSpringBean(ctx, name, beanType, !nameSupplied); + Object managedBean = findReference(ctx, name, beanType, !nameSupplied); m.invoke(bean, managedBean); } catch (Exception e) { throw new StripesRuntimeException("Exception while trying to lookup and inject " + - "a Spring bean into a bean of type " + bean.getClass().getSimpleName() + + "an SCA Reference into a bean of type " + bean.getClass().getSimpleName() + " using method " + m.toString(), e); } } @@ -127,28 +112,28 @@ public class TuscanyHelper { // And then inject any properties that are annotated for (Field f : getFields(bean.getClass())) { try { - SpringBean springBean = f.getAnnotation(SpringBean.class); - boolean nameSupplied = !"".equals(springBean.value()); - String name = nameSupplied ? springBean.value() : f.getName(); - Object managedBean = findSpringBean(ctx, name, f.getType(), !nameSupplied); + Reference scaReference = f.getAnnotation(Reference.class); + boolean nameSupplied = !"".equals(scaReference.name()); + String name = nameSupplied ? scaReference.name() : f.getName(); + Object managedBean = findReference(ctx, name, f.getType(), !nameSupplied); f.set(bean, managedBean); } catch (Exception e) { throw new StripesRuntimeException("Exception while trying to lookup and inject " + - "a Spring bean into a bean of type " + bean.getClass().getSimpleName() + + "an SCA Referenceinto a bean of type " + bean.getClass().getSimpleName() + " using field access on field " + f.toString(), e); } } } /** - * Fetches the methods on a class that are annotated with SpringBean. The first time it + * Fetches the methods on a class that are annotated with Reference. The first time it * is called for a particular class it will introspect the class and cache the results. * All non-overridden methods are examined, including protected and private methods. * If a method is not public an attempt it made to make it accessible - if it fails * it is removed from the collection and an error is logged. * - * @param clazz the class on which to look for SpringBean annotated methods + * @param clazz the class on which to look for Reference annotated methods * @return the collection of methods with the annotation */ protected static Collection<Method> getMethods(Class<?> clazz) { @@ -159,7 +144,7 @@ public class TuscanyHelper { while (iterator.hasNext()) { Method method = iterator.next(); - if (!method.isAnnotationPresent(SpringBean.class)) { + if (!method.isAnnotationPresent(Reference.class)) { iterator.remove(); } else { @@ -171,7 +156,7 @@ public class TuscanyHelper { catch (SecurityException se) { throw new StripesRuntimeException( "Method " + clazz.getName() + "." + method.getName() + "is marked " + - "with @SpringBean and is not public. An attempt to call " + + "with @Reference and is not public. An attempt to call " + "setAccessible(true) resulted in a SecurityException. Please " + "either make the method public or modify your JVM security " + "policy to allow Stripes to setAccessible(true).", se); @@ -181,7 +166,7 @@ public class TuscanyHelper { // Ensure the method has only the one parameter if (method.getParameterTypes().length != 1) { throw new StripesRuntimeException( - "A method marked with @SpringBean must have exactly one parameter: " + + "A method marked with @Reference must have exactly one parameter: " + "the bean to be injected. Method [" + method.toGenericString() + "] has " + method.getParameterTypes().length + " parameters." ); @@ -196,13 +181,13 @@ public class TuscanyHelper { } /** - * Fetches the fields on a class that are annotated with SpringBean. The first time it + * Fetches the fields on a class that are annotated with Refernece. The first time it * is called for a particular class it will introspect the class and cache the results. * All non-overridden fields are examined, including protected and private fields. * If a field is not public an attempt it made to make it accessible - if it fails * it is removed from the collection and an error is logged. * - * @param clazz the class on which to look for SpringBean annotated fields + * @param clazz the class on which to look for Reference annotated fields * @return the collection of methods with the annotation */ protected static Collection<Field> getFields(Class<?> clazz) { @@ -213,7 +198,7 @@ public class TuscanyHelper { while (iterator.hasNext()) { Field field = iterator.next(); - if (!field.isAnnotationPresent(SpringBean.class)) { + if (!field.isAnnotationPresent(Reference.class)) { iterator.remove(); } else if (!field.isAccessible()) { @@ -224,7 +209,7 @@ public class TuscanyHelper { catch (SecurityException se) { throw new StripesRuntimeException( "Field " + clazz.getName() + "." + field.getName() + "is marked " + - "with @SpringBean and is not public. An attempt to call " + + "with @Reference and is not public. An attempt to call " + "setAccessible(true) resulted in a SecurityException. Please " + "either make the field public, annotate a public setter instead " + "or modify your JVM security policy to allow Stripes to " + @@ -240,53 +225,54 @@ public class TuscanyHelper { } /** - * Looks up a Spring managed bean from an Application Context. First looks for a bean + * Looks up an SCA Reference from a ComponentContext. First looks for a bean * with name specified. If no such bean exists, looks for a bean by type. If there is * only one bean of the appropriate type, it is returned. If zero or more than one bean * of the correct type exists, an exception is thrown. * - * @param ctx the Spring Application Context - * @param name the name of the spring bean to look for + * @param ctx the SCA ComponentContext + * @param name the name of the reference to look for * @param type the type of bean to look for * @param allowFindByType true to indicate that finding a bean by type is acceptable * if find by name fails. * @exception RuntimeException various subclasses of RuntimeException are thrown if it - * is not possible to find a unique matching bean in the spring context given + * is not possible to find a unique matching bean in the ComponentContext given * the constraints supplied. */ - protected static Object findSpringBean(ApplicationContext ctx, + protected static Object findReference(ServletContext ctx, String name, Class<?> type, boolean allowFindByType) { // First try to lookup using the name provided - try { - Object bean = ctx.getBean(name, type); - log.debug("Found spring bean with name [", name, "] and type [", + Object bean = WebApplicationUtils.getReference(name, type, ctx); + if (bean == null) { + throw new StripesRuntimeException("no reference defined:" + name); + } + + log.debug("Found sca reference with name [", name, "] and type [", bean.getClass().getName(), "]"); return bean; - } - catch (NestedRuntimeException nre) { - if (!allowFindByType) throw nre; - } - // If we got here then we didn't find a bean yet, try by type - String[] beanNames = ctx.getBeanNamesForType(type); - if (beanNames.length == 0) { - throw new StripesRuntimeException( - "Unable to find SpringBean with name [" + name + "] or type [" + - type.getName() + "] in the Spring application context."); - } - else if (beanNames.length > 1) { - throw new StripesRuntimeException( - "Unable to find SpringBean with name [" + name + "] or unique bean with type [" + - type.getName() + "] in the Spring application context. Found " + beanNames.length + - "beans of matching type."); - } - else { - log.warn("Found unique SpringBean with type [" + type.getName() + "]. Matching on ", - "type is a little risky so watch out!"); - return ctx.getBean(beanNames[0], type); - } +// TODO: Support get by type (sca autowire?) + +// // If we got here then we didn't find a bean yet, try by type +// String[] beanNames = ctx.getBeanNamesForType(type); +// if (beanNames.length == 0) { +// throw new StripesRuntimeException( +// "Unable to find SpringBean with name [" + name + "] or type [" + +// type.getName() + "] in the Spring application context."); +// } +// else if (beanNames.length > 1) { +// throw new StripesRuntimeException( +// "Unable to find SpringBean with name [" + name + "] or unique bean with type [" + +// type.getName() + "] in the Spring application context. Found " + beanNames.length + +// "beans of matching type."); +// } +// else { +// log.warn("Found unique SpringBean with type [" + type.getName() + "]. Matching on ", +// "type is a little risky so watch out!"); +// return ctx.getBean(beanNames[0], type); +// } } /** |