You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by bo...@apache.org on 2007/03/03 21:30:11 UTC
svn commit: r514237 - in /myfaces/core/branches/jsf12/impl: ./
src/main/java/org/apache/ src/main/java/org/apache/myfaces/config/
src/main/java/org/apache/myfaces/config/annotation/
src/main/java/org/apache/myfaces/webapp/ src/test/java/org/apache/myfa...
Author: bommel
Date: Sat Mar 3 12:30:10 2007
New Revision: 514237
URL: http://svn.apache.org/viewvc?view=rev&rev=514237
Log:
Added annotation handling and resource injection based on the tomcat implementation.
The AnnotationProcessor Interface is a copy from the tomcat 6 src.
Added:
myfaces/core/branches/jsf12/impl/src/main/java/org/apache/AnnotationProcessor.java
myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/AllAnnotationProcessor.java
myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/AnnotationProcessorFactory.java
myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/NoInjectionAnnotationProcessor.java
myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/NopAnnotationProcessor.java
myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/ResourceAnnotationProcessor.java
Modified:
myfaces/core/branches/jsf12/impl/pom.xml
myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/ManagedBeanBuilder.java
myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/webapp/AbstractMyFacesListener.java
myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBeanHandlerTestCase.java
Modified: myfaces/core/branches/jsf12/impl/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/pom.xml?view=diff&rev=514237&r1=514236&r2=514237
==============================================================================
--- myfaces/core/branches/jsf12/impl/pom.xml (original)
+++ myfaces/core/branches/jsf12/impl/pom.xml Sat Mar 3 12:30:10 2007
@@ -285,7 +285,18 @@
<artifactId>geronimo-annotation_1.0_spec</artifactId>
<scope>provided</scope>
</dependency>
-
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-jpa_3.0_spec</artifactId>
+ <version>1.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-ejb_3.0_spec</artifactId>
+ <version>1.0</version>
+ <scope>provided</scope>
+ </dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
Added: myfaces/core/branches/jsf12/impl/src/main/java/org/apache/AnnotationProcessor.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/main/java/org/apache/AnnotationProcessor.java?view=auto&rev=514237
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/main/java/org/apache/AnnotationProcessor.java (added)
+++ myfaces/core/branches/jsf12/impl/src/main/java/org/apache/AnnotationProcessor.java Sat Mar 3 12:30:10 2007
@@ -0,0 +1,34 @@
+package org.apache;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import javax.naming.NamingException;
+import java.lang.reflect.InvocationTargetException;
+
+/**
+ * Copy from tomcat util we can get this from a repository
+ */
+public interface AnnotationProcessor {
+
+ public void postConstruct(Object instance)
+ throws IllegalAccessException, InvocationTargetException;
+ public void preDestroy(Object instance)
+ throws IllegalAccessException, InvocationTargetException;
+ public void processAnnotations(Object instance)
+ throws IllegalAccessException, InvocationTargetException, NamingException;
+}
Modified: myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/ManagedBeanBuilder.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/ManagedBeanBuilder.java?view=diff&rev=514237&r1=514236&r2=514237
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/ManagedBeanBuilder.java (original)
+++ myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/ManagedBeanBuilder.java Sat Mar 3 12:30:10 2007
@@ -16,6 +16,7 @@
package org.apache.myfaces.config;
import java.lang.reflect.Array;
+import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
@@ -32,11 +33,12 @@
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.webapp.UIComponentTag;
+import javax.naming.NamingException;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.apache.myfaces.config.annotation.AnnotatedManagedBeanHandler;
+import org.apache.myfaces.config.annotation.AnnotationProcessorFactory;
import org.apache.myfaces.config.element.ListEntries;
import org.apache.myfaces.config.element.ListEntry;
import org.apache.myfaces.config.element.ManagedBean;
@@ -65,14 +67,34 @@
{
final Object bean = ClassUtils.newInstance(beanConfiguration.getManagedBeanClassName());
- final AnnotatedManagedBeanHandler handler = new AnnotatedManagedBeanHandler(bean,
+ /*final AnnotatedManagedBeanHandler handler = new AnnotatedManagedBeanHandler(bean,
beanConfiguration.getManagedBeanScope(), beanConfiguration.getManagedBeanName());
final boolean threwUnchecked = handler.invokePostConstruct();
-
+
if(threwUnchecked)
- return null;
-
+ return null; */
+ try
+ {
+ AnnotationProcessorFactory.getAnnotatonProcessor().processAnnotations(bean);
+ if (!beanConfiguration.getManagedBeanScope().equals(ManagedBeanBuilder.NONE))
+ {
+ AnnotationProcessorFactory.getAnnotatonProcessor().postConstruct(bean);
+ }
+ }
+ catch (IllegalAccessException e)
+ {
+ throw new FacesException(e);
+ }
+ catch (InvocationTargetException e)
+ {
+ throw new FacesException(e);
+ }
+ catch (NamingException e)
+ {
+ throw new FacesException(e);
+ }
+
switch (beanConfiguration.getInitMode())
{
case ManagedBean.INIT_MODE_PROPERTIES:
Added: myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/AllAnnotationProcessor.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/AllAnnotationProcessor.java?view=auto&rev=514237
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/AllAnnotationProcessor.java (added)
+++ myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/AllAnnotationProcessor.java Sat Mar 3 12:30:10 2007
@@ -0,0 +1,98 @@
+package org.apache.myfaces.config.annotation;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import javax.naming.NamingException;
+import javax.naming.Context;
+import javax.annotation.Resource;
+import javax.ejb.EJB;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceUnit;
+
+
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Field;
+
+public class AllAnnotationProcessor extends ResourceAnnotationProcessor
+{
+
+
+ public AllAnnotationProcessor(Context context)
+ {
+ super(context);
+ }
+
+ protected void checkMethodAnnotation(Method method, Object instance)
+ throws NamingException, IllegalAccessException, InvocationTargetException
+ {
+ super.checkMethodAnnotation(method, instance);
+ if (method.isAnnotationPresent(Resource.class))
+ {
+ Resource annotation = method.getAnnotation(Resource.class);
+ lookupMethodResource(context, instance, method, annotation.name());
+ }
+ if (method.isAnnotationPresent(EJB.class))
+ {
+ EJB annotation = method.getAnnotation(EJB.class);
+ lookupMethodResource(context, instance, method, annotation.name());
+ }
+ // TODO where i find WebServiceRef?
+ /*if (methods[i].isAnnotationPresent(WebServiceRef.class)) {
+ WebServiceRef annotation =
+ (WebServiceRef) methods[i].getAnnotation(WebServiceRef.class);
+ lookupMethodResource(context, instance, methods[i], annotation.name());
+ }*/
+ if (method.isAnnotationPresent(PersistenceContext.class))
+ {
+ PersistenceContext annotation = method.getAnnotation(PersistenceContext.class);
+ lookupMethodResource(context, instance, method, annotation.name());
+ }
+ if (method.isAnnotationPresent(PersistenceUnit.class))
+ {
+ PersistenceUnit annotation = method.getAnnotation(PersistenceUnit.class);
+ lookupMethodResource(context, instance, method, annotation.name());
+ }
+ }
+
+ protected void checkFieldAnnotation(Field field, Object instance)
+ throws NamingException, IllegalAccessException
+ {
+ super.checkFieldAnnotation(field, instance);
+ if (field.isAnnotationPresent(EJB.class))
+ {
+ EJB annotation = field.getAnnotation(EJB.class);
+ lookupFieldResource(context, instance, field, annotation.name());
+ }
+ /*if (fields[i].isAnnotationPresent(WebServiceRef.class)) {
+ WebServiceRef annotation =
+ (WebServiceRef) fields[i].getAnnotation(WebServiceRef.class);
+ lookupFieldResource(context, instance, fields[i], annotation.name());
+ }*/
+ if (field.isAnnotationPresent(PersistenceContext.class))
+ {
+ PersistenceContext annotation = field.getAnnotation(PersistenceContext.class);
+ lookupFieldResource(context, instance, field, annotation.name());
+ }
+ if (field.isAnnotationPresent(PersistenceUnit.class))
+ {
+ PersistenceUnit annotation = field.getAnnotation(PersistenceUnit.class);
+ lookupFieldResource(context, instance, field, annotation.name());
+ }
+ }
+}
Added: myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/AnnotationProcessorFactory.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/AnnotationProcessorFactory.java?view=auto&rev=514237
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/AnnotationProcessorFactory.java (added)
+++ myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/AnnotationProcessorFactory.java Sat Mar 3 12:30:10 2007
@@ -0,0 +1,77 @@
+package org.apache.myfaces.config.annotation;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.AnnotationProcessor;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.apache.myfaces.shared_impl.util.ClassUtils;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+
+public class AnnotationProcessorFactory
+{
+ private static Log log = LogFactory.getLog(AnnotationProcessorFactory.class);
+ private static AnnotationProcessor annotationProcessor;
+
+ public static AnnotationProcessor getAnnotatonProcessor()
+ {
+
+ if (annotationProcessor == null)
+ {
+ try
+ {
+ ClassUtils.classForName("javax.annotation.PreDestroy");
+ }
+ catch (ClassNotFoundException e)
+ {
+ // no annotation available don't process annotations
+ annotationProcessor = new NopAnnotationProcessor();
+ return annotationProcessor;
+ }
+ Context context;
+ try
+ {
+ context = new InitialContext();
+ try
+ {
+ ClassUtils.classForName("javax.ejb.EJB");
+ // Asume full JEE 5 container
+ annotationProcessor = new AllAnnotationProcessor(context);
+ }
+ catch (ClassNotFoundException e)
+ {
+ // something else
+ annotationProcessor = new ResourceAnnotationProcessor(context);
+ }
+ }
+ catch (NamingException e)
+ {
+ // no initial context available no injection
+ annotationProcessor = new NoInjectionAnnotationProcessor();
+ log.error("No InitialContext found. Using NoInjectionAnnotationProcessor.", e);
+
+ }
+ }
+ return annotationProcessor;
+ }
+}
Added: myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/NoInjectionAnnotationProcessor.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/NoInjectionAnnotationProcessor.java?view=auto&rev=514237
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/NoInjectionAnnotationProcessor.java (added)
+++ myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/NoInjectionAnnotationProcessor.java Sat Mar 3 12:30:10 2007
@@ -0,0 +1,118 @@
+package org.apache.myfaces.config.annotation;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.AnnotationProcessor;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.naming.NamingException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+
+public class NoInjectionAnnotationProcessor implements AnnotationProcessor
+{
+
+ /**
+ * Call postConstruct method on the specified instance.
+ */
+ public void postConstruct(Object instance)
+ throws IllegalAccessException, InvocationTargetException
+ {
+
+ Method[] methods = instance.getClass().getDeclaredMethods();
+ Method postConstruct = null;
+ for (Method method : methods)
+ {
+ if (method.isAnnotationPresent(PostConstruct.class))
+ {
+ if ((postConstruct != null)
+ || (method.getParameterTypes().length != 0)
+ || (Modifier.isStatic(method.getModifiers()))
+ || (method.getExceptionTypes().length > 0)
+ || (!method.getReturnType().getName().equals("void")))
+ {
+ throw new IllegalArgumentException("Invalid PostConstruct annotation");
+ }
+ postConstruct = method;
+ }
+ }
+
+ // At the end the postconstruct annotated
+ // method is invoked
+ if (postConstruct != null)
+ {
+ boolean accessibility = postConstruct.isAccessible();
+ postConstruct.setAccessible(true);
+ postConstruct.invoke(instance);
+ postConstruct.setAccessible(accessibility);
+ }
+
+ }
+
+
+ /**
+ * Call preDestroy method on the specified instance.
+ */
+ public void preDestroy(Object instance)
+ throws IllegalAccessException, InvocationTargetException
+ {
+
+ Method[] methods = instance.getClass().getDeclaredMethods();
+ Method preDestroy = null;
+ for (Method method : methods)
+ {
+ if (method.isAnnotationPresent(PreDestroy.class))
+ {
+ if ((preDestroy != null)
+ || (method.getParameterTypes().length != 0)
+ || (Modifier.isStatic(method.getModifiers()))
+ || (method.getExceptionTypes().length > 0)
+ || (!method.getReturnType().getName().equals("void")))
+ {
+ throw new IllegalArgumentException("Invalid PreDestroy annotation");
+ }
+ preDestroy = method;
+ }
+ }
+
+ // At the end the postconstruct annotated
+ // method is invoked
+ if (preDestroy != null)
+ {
+ boolean accessibility = preDestroy.isAccessible();
+ preDestroy.setAccessible(true);
+ preDestroy.invoke(instance);
+ preDestroy.setAccessible(accessibility);
+ }
+
+ }
+
+
+ /**
+ * Inject resources in specified instance.
+ */
+ public void processAnnotations(Object instance)
+ throws IllegalAccessException, InvocationTargetException, NamingException
+ {
+
+ }
+
+}
Added: myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/NopAnnotationProcessor.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/NopAnnotationProcessor.java?view=auto&rev=514237
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/NopAnnotationProcessor.java (added)
+++ myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/NopAnnotationProcessor.java Sat Mar 3 12:30:10 2007
@@ -0,0 +1,46 @@
+package org.apache.myfaces.config.annotation;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.AnnotationProcessor;
+
+import javax.naming.NamingException;
+import java.lang.reflect.InvocationTargetException;
+
+public class NopAnnotationProcessor implements AnnotationProcessor
+{
+
+
+ public void postConstruct(Object instance)
+ throws IllegalAccessException, InvocationTargetException
+ {
+
+ }
+
+ public void preDestroy(Object instance)
+ throws IllegalAccessException, InvocationTargetException
+ {
+
+ }
+
+ public void processAnnotations(Object instance)
+ throws IllegalAccessException, InvocationTargetException, NamingException
+ {
+
+ }
+}
Added: myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/ResourceAnnotationProcessor.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/ResourceAnnotationProcessor.java?view=auto&rev=514237
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/ResourceAnnotationProcessor.java (added)
+++ myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/config/annotation/ResourceAnnotationProcessor.java Sat Mar 3 12:30:10 2007
@@ -0,0 +1,149 @@
+package org.apache.myfaces.config.annotation;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import javax.naming.NamingException;
+import javax.naming.Context;
+import javax.annotation.Resource;
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Field;
+
+public class ResourceAnnotationProcessor extends NoInjectionAnnotationProcessor
+{
+
+ protected Context context;
+
+ public ResourceAnnotationProcessor(Context context)
+ {
+ this.context = context;
+ }
+
+
+ /**
+ * Inject resources in specified instance.
+ */
+ public void processAnnotations(Object instance)
+ throws IllegalAccessException, InvocationTargetException, NamingException
+ {
+
+ if (context == null)
+ {
+ // No resource injection
+ return;
+ }
+
+ // Initialize fields annotations
+ Field[] fields = instance.getClass().getDeclaredFields();
+ for (Field field : fields)
+ {
+ checkFieldAnnotation(field, instance);
+ }
+
+ // Initialize methods annotations
+ Method[] methods = instance.getClass().getDeclaredMethods();
+ for (Method method : methods)
+ {
+ checkMethodAnnotation(method, instance);
+ }
+
+ }
+
+ protected void checkMethodAnnotation(Method method, Object instance)
+ throws NamingException, IllegalAccessException, InvocationTargetException
+ {
+ if (method.isAnnotationPresent(Resource.class))
+ {
+ Resource annotation = method.getAnnotation(Resource.class);
+ lookupMethodResource(context, instance, method, annotation.name());
+ }
+ }
+
+ protected void checkFieldAnnotation(Field field, Object instance)
+ throws NamingException, IllegalAccessException
+ {
+ if (field.isAnnotationPresent(Resource.class))
+ {
+ Resource annotation = field.getAnnotation(Resource.class);
+ lookupFieldResource(context, instance, field, annotation.name());
+ }
+ }
+
+ /**
+ * Inject resources in specified field.
+ */
+ protected static void lookupFieldResource(javax.naming.Context context,
+ Object instance, Field field, String name)
+ throws NamingException, IllegalAccessException
+ {
+
+ Object lookedupResource;
+ boolean accessibility ;
+
+ if ((name != null) &&
+ (name.length() > 0))
+ {
+ lookedupResource = context.lookup(name);
+ }
+ else
+ {
+ lookedupResource = context.lookup(instance.getClass().getName() + "/" + field.getName());
+ }
+
+ accessibility = field.isAccessible();
+ field.setAccessible(true);
+ field.set(instance, lookedupResource);
+ field.setAccessible(accessibility);
+ }
+
+
+ /**
+ * Inject resources in specified method.
+ */
+ protected static void lookupMethodResource(javax.naming.Context context,
+ Object instance, Method method, String name)
+ throws NamingException, IllegalAccessException, InvocationTargetException
+ {
+
+ if (!method.getName().startsWith("set")
+ || method.getParameterTypes().length != 1
+ || !method.getReturnType().getName().equals("void"))
+ {
+ throw new IllegalArgumentException("Invalid method resource injection annotation");
+ }
+
+ Object lookedupResource;
+ boolean accessibility;
+
+ if ((name != null) &&
+ (name.length() > 0))
+ {
+ lookedupResource = context.lookup(name);
+ }
+ else
+ {
+ lookedupResource =
+ context.lookup(instance.getClass().getName() + "/" + method.getName().substring(3));
+ }
+
+ accessibility = method.isAccessible();
+ method.setAccessible(true);
+ method.invoke(instance, lookedupResource);
+ method.setAccessible(accessibility);
+ }
+}
Modified: myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/webapp/AbstractMyFacesListener.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/webapp/AbstractMyFacesListener.java?view=diff&rev=514237&r1=514236&r2=514237
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/webapp/AbstractMyFacesListener.java (original)
+++ myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/webapp/AbstractMyFacesListener.java Sat Mar 3 12:30:10 2007
@@ -3,16 +3,23 @@
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.http.HttpSessionBindingEvent;
+import javax.faces.FacesException;
+import javax.naming.NamingException;
-import org.apache.myfaces.config.annotation.AnnotatedManagedBeanHandler;
+import org.apache.myfaces.config.annotation.AnnotationProcessorFactory;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.lang.reflect.InvocationTargetException;
/**
* @author Dennis Byrne
*/
public abstract class AbstractMyFacesListener {
+ private static Log log = LogFactory.getLog(AbstractMyFacesListener.class);
- protected void doPreDestroy(ServletRequestAttributeEvent event, String scope) {
+ protected void doPreDestroy(ServletRequestAttributeEvent event, String scope) {
doPreDestroy(event.getValue(), event.getName(), scope);
}
@@ -28,12 +35,23 @@
if(value != null)
{
- AnnotatedManagedBeanHandler handler =
- new AnnotatedManagedBeanHandler(value, scope, name);
+ //AnnotatedManagedBeanHandler handler =
+ // new AnnotatedManagedBeanHandler(value, scope, name);
- handler.invokePreDestroy();
- }
-
+ //handler.invokePreDestroy();
+
+ try
+ {
+
+ AnnotationProcessorFactory.getAnnotatonProcessor().postConstruct(value);
+ } catch (IllegalAccessException e)
+ {
+ log.error("", e);
+ } catch (InvocationTargetException e)
+ {
+ log.error("", e);
+ }
+ }
}
}
Modified: myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBeanHandlerTestCase.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBeanHandlerTestCase.java?view=diff&rev=514237&r1=514236&r2=514237
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBeanHandlerTestCase.java (original)
+++ myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBeanHandlerTestCase.java Sat Mar 3 12:30:10 2007
@@ -3,6 +3,9 @@
import junit.framework.TestCase;
import org.apache.myfaces.config.ManagedBeanBuilder;
+import org.apache.AnnotationProcessor;
+
+import java.lang.reflect.InvocationTargetException;
/**
* @author Dennis Byrne
@@ -12,45 +15,50 @@
protected AnnotatedManagedBean managedBean;
protected AnnotatedManagedBeanHandler handler;
- protected final String NAME = "volker weber";
+ protected AnnotationProcessor processor;
+ protected final String NAME = "volker weber";
public void setUp() {
managedBean = new AnnotatedManagedBean(null);
- }
+ processor = new NoInjectionAnnotationProcessor();
+ }
- public void testPostConstructShouldNotInvokeForNoneScope() {
+ public void testPostConstructShouldNotInvokeForNoneScope() throws IllegalAccessException, InvocationTargetException {
- handler = new AnnotatedManagedBeanHandler(managedBean,
+ /*handler = new AnnotatedManagedBeanHandler(managedBean,
ManagedBeanBuilder.NONE, NAME);
-
+
boolean threwUnchecked = handler.invokePostConstruct();
- assertFalse(threwUnchecked);
- assertFalse(managedBean.isPostConstructCalled());
+ assertFalse(threwUnchecked);*/
+ processor.postConstruct(managedBean);
+ assertFalse(managedBean.isPostConstructCalled());
assertFalse(managedBean.isPreDestroyCalled());
}
- public void testPreDestroyShouldNotInvokeForNoneScope() {
+ public void testPreDestroyShouldNotInvokeForNoneScope() throws IllegalAccessException, InvocationTargetException {
- handler = new AnnotatedManagedBeanHandler(managedBean,
+ /*handler = new AnnotatedManagedBeanHandler(managedBean,
ManagedBeanBuilder.NONE, NAME);
boolean threwUnchecked = handler.invokePreDestroy();
- assertFalse(threwUnchecked);
- assertFalse(managedBean.isPostConstructCalled());
+ assertFalse(threwUnchecked);*/
+ processor.preDestroy(managedBean);
+ assertFalse(managedBean.isPostConstructCalled());
assertFalse(managedBean.isPreDestroyCalled());
}
- public void testPostConstructShouldInvokeForRequestScope() {
+ public void testPostConstructShouldInvokeForRequestScope() throws IllegalAccessException, InvocationTargetException {
- handler = new AnnotatedManagedBeanHandler(managedBean,
+ /*handler = new AnnotatedManagedBeanHandler(managedBean,
ManagedBeanBuilder.REQUEST, NAME);
boolean threwUnchecked = handler.invokePostConstruct();
- assertFalse(threwUnchecked);
- assertTrue(managedBean.isPostConstructCalled());
+ assertFalse(threwUnchecked);*/
+ processor.postConstruct(managedBean);
+ assertTrue(managedBean.isPostConstructCalled());
assertFalse(managedBean.isPreDestroyCalled());
}