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/04 13:29:48 UTC
svn commit: r514387 - in /myfaces/core/branches/jsf12/impl: ./
src/main/java/org/apache/ src/main/java/org/apache/myfaces/
src/main/java/org/apache/myfaces/config/
src/main/java/org/apache/myfaces/config/annotation/
src/main/java/org/apache/myfaces/web...
Author: bommel
Date: Sun Mar 4 04:29:47 2007
New Revision: 514387
URL: http://svn.apache.org/viewvc?view=rev&rev=514387
Log:
(MYFACES-1246)
Just added an AnnotationProcessor based on the tomcat implementation.
You can configure it with a servlet init param or a service provider.
The fallback implementation try to setup a Processor base on the classpath
Added:
myfaces/core/branches/jsf12/impl/src/main/java/org/apache/AnnotationProcessor.java
myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/DiscoverableAnnotationProcessor.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
myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotationProcessorTestCase.java
myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/OwnAnnotationProcessorTestCase.java
myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/TestAnnotationProcessor.java
myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/TestDiscoverableAnnotationProcessor.java
myfaces/core/branches/jsf12/impl/src/test/resources/META-INF/
myfaces/core/branches/jsf12/impl/src/test/resources/META-INF/services/
myfaces/core/branches/jsf12/impl/src/test/resources/META-INF/services/org.apache.myfaces.AnnotationProcessor
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/AnnotatedExceptionManagedBeanHandlerTestCase.java
myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBean.java
myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBeanHandlerTestCase.java
myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedRuntimeExceptionManagedBeanHandlerTestCase.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=514387&r1=514386&r2=514387
==============================================================================
--- myfaces/core/branches/jsf12/impl/pom.xml (original)
+++ myfaces/core/branches/jsf12/impl/pom.xml Sun Mar 4 04:29:47 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>
@@ -293,6 +304,11 @@
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>commons-discovery</groupId>
+ <artifactId>commons-discovery</artifactId>
+ <version>0.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
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=514387
==============================================================================
--- 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 Sun Mar 4 04:29:47 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 src until 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;
+}
Added: myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/DiscoverableAnnotationProcessor.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/DiscoverableAnnotationProcessor.java?view=auto&rev=514387
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/DiscoverableAnnotationProcessor.java (added)
+++ myfaces/core/branches/jsf12/impl/src/main/java/org/apache/myfaces/DiscoverableAnnotationProcessor.java Sun Mar 4 04:29:47 2007
@@ -0,0 +1,26 @@
+package org.apache.myfaces;
+
+/*
+ * 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;
+
+public interface DiscoverableAnnotationProcessor extends AnnotationProcessor
+{
+ public boolean isAvailable();
+}
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=514387&r1=514386&r2=514387
==============================================================================
--- 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 Sun Mar 4 04:29:47 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,10 +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.AnnotationProcessorFactory;
import org.apache.myfaces.config.annotation.AnnotatedManagedBeanHandler;
import org.apache.myfaces.config.element.ListEntries;
import org.apache.myfaces.config.element.ListEntry;
@@ -72,6 +75,26 @@
if(threwUnchecked)
return null;
+ /*try
+ {
+ AnnotationProcessorFactory.getAnnotatonProcessor(facesContext.getExternalContext()).processAnnotations(bean);
+ if (!beanConfiguration.getManagedBeanScope().equals(ManagedBeanBuilder.NONE))
+ {
+ AnnotationProcessorFactory.getAnnotatonProcessor(facesContext.getExternalContext()).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())
{
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=514387
==============================================================================
--- 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 Sun Mar 4 04:29:47 2007
@@ -0,0 +1,97 @@
+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 (method.isAnnotationPresent(WebServiceRef.class)) {
+ WebServiceRef annotation =
+ (WebServiceRef) method.getAnnotation(WebServiceRef.class);
+ lookupMethodResource(context, instance, methods, 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 (field.isAnnotationPresent(WebServiceRef.class)) {
+ WebServiceRef annotation =
+ (WebServiceRef) field.getAnnotation(WebServiceRef.class);
+ lookupFieldResource(context, instance, field, 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=514387
==============================================================================
--- 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 Sun Mar 4 04:29:47 2007
@@ -0,0 +1,201 @@
+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.commons.discovery.resource.names.DiscoverServiceNames;
+import org.apache.commons.discovery.resource.ClassLoaders;
+import org.apache.commons.discovery.ResourceNameIterator;
+
+import org.apache.myfaces.shared_impl.util.ClassUtils;
+import org.apache.myfaces.DiscoverableAnnotationProcessor;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.faces.context.ExternalContext;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+
+public class AnnotationProcessorFactory
+{
+ private static Log log = LogFactory.getLog(AnnotationProcessorFactory.class);
+ private static AnnotationProcessor ANNOTATION_PROCESSOR_INSTANCE;
+ public static final String ANNOTATION_PROCESSOR_PROVIDER = "org.apache.myfaces.AnnotationProcessor";
+
+
+ public static AnnotationProcessor getAnnotatonProcessor(ExternalContext externalContext)
+ {
+ if (ANNOTATION_PROCESSOR_INSTANCE == null)
+ {
+ if (externalContext == null) {
+ throw new NullPointerException("AnnotationProcessorFactory is not configured and ExternalContext is null");
+ }
+ if (!resolveAnnotationProcessorFromExternalContext(externalContext))
+ {
+ if (!resolveAnnotationProcessorFromService(externalContext))
+ {
+ resolveFallbackAnnotationProcessor();
+ }
+ }
+ }
+ return ANNOTATION_PROCESSOR_INSTANCE;
+ }
+
+ public static void release() {
+ ANNOTATION_PROCESSOR_INSTANCE = null;
+ }
+
+
+
+ private static boolean resolveAnnotationProcessorFromExternalContext(ExternalContext externalContext)
+ {
+ try
+ {
+ String annotationProcessorClassName = externalContext.getInitParameter(ANNOTATION_PROCESSOR_PROVIDER);
+ if (annotationProcessorClassName != null)
+ {
+
+ Object obj = createClass(annotationProcessorClassName, externalContext);
+
+ if (obj instanceof AnnotationProcessor) {
+ ANNOTATION_PROCESSOR_INSTANCE = (AnnotationProcessor) obj;
+ return true;
+ }
+ }
+ }
+ catch (ClassNotFoundException e)
+ {
+ log.error("", e);
+ }
+ catch (InstantiationException e)
+ {
+ log.error("", e);
+ }
+ catch (IllegalAccessException e)
+ {
+ log.error("", e);
+ }
+ catch (InvocationTargetException e)
+ {
+ log.error("", e);
+ }
+ return false;
+ }
+
+
+ private static boolean resolveAnnotationProcessorFromService(ExternalContext externalContext) {
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ ClassLoaders loaders = new ClassLoaders();
+ loaders.put(classLoader);
+ DiscoverServiceNames dsn = new DiscoverServiceNames(loaders);
+ ResourceNameIterator iter = dsn.findResourceNames(ANNOTATION_PROCESSOR_PROVIDER);
+ while (iter.hasNext()) {
+ String className = iter.nextResourceName();
+ try
+ {
+ Object obj = createClass(className, externalContext);
+ if (DiscoverableAnnotationProcessor.class.isAssignableFrom(obj.getClass())) {
+ DiscoverableAnnotationProcessor discoverableAnnotationProcessor =
+ (DiscoverableAnnotationProcessor) obj;
+ if (discoverableAnnotationProcessor.isAvailable()) {
+ ANNOTATION_PROCESSOR_INSTANCE = discoverableAnnotationProcessor;
+ return true;
+ }
+ }
+ }
+ catch (ClassNotFoundException e)
+ {
+ log.error("", e);
+ }
+ catch (NoClassDefFoundError e)
+ {
+ log.error("", e);
+ }
+ catch (InstantiationException e)
+ {
+ log.error("", e);
+ }
+ catch (IllegalAccessException e)
+ {
+ log.error("", e);
+ }
+ catch (InvocationTargetException e)
+ {
+ log.error("", e);
+ }
+ }
+ return false;
+ }
+
+ private static Object createClass(String className, ExternalContext externalContext)
+ throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
+ {
+ Class clazz = ClassUtils.classForName(className);
+
+ Object obj;
+ try {
+ Constructor constructor = clazz.getConstructor(ExternalContext.class);
+ obj = constructor.newInstance(externalContext);
+ } catch (NoSuchMethodException e) {
+ obj = clazz.newInstance();
+ }
+ return obj;
+ }
+
+
+ private static void resolveFallbackAnnotationProcessor()
+ {
+ try
+ {
+ ClassUtils.classForName("javax.annotation.PreDestroy");
+ }
+ catch (ClassNotFoundException e)
+ {
+ // no annotation available don't process annotations
+ ANNOTATION_PROCESSOR_INSTANCE = new NopAnnotationProcessor();
+ return;
+ }
+ Context context;
+ try
+ {
+ context = new InitialContext();
+ try
+ {
+ ClassUtils.classForName("javax.ejb.EJB");
+ // Asume full JEE 5 container
+ ANNOTATION_PROCESSOR_INSTANCE = new AllAnnotationProcessor(context);
+ }
+ catch (ClassNotFoundException e)
+ {
+ // something else
+ ANNOTATION_PROCESSOR_INSTANCE = new ResourceAnnotationProcessor(context);
+ }
+ }
+ catch (NamingException e)
+ {
+ // no initial context available no injection
+ ANNOTATION_PROCESSOR_INSTANCE = new NoInjectionAnnotationProcessor();
+ log.error("No InitialContext found. Using NoInjectionAnnotationProcessor.", e);
+
+ }
+ }
+}
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=514387
==============================================================================
--- 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 Sun Mar 4 04:29:47 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=514387
==============================================================================
--- 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 Sun Mar 4 04:29:47 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=514387
==============================================================================
--- 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 Sun Mar 4 04:29:47 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=514387&r1=514386&r2=514387
==============================================================================
--- 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 Sun Mar 4 04:29:47 2007
@@ -1,18 +1,40 @@
package org.apache.myfaces.webapp;
+/*
+ * 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.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.http.HttpSessionBindingEvent;
-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);
}
@@ -25,15 +47,25 @@
}
protected void doPreDestroy(Object value, String name, String scope) {
-
- if(value != null)
+
+ if(value != null)
{
- AnnotatedManagedBeanHandler handler =
- new AnnotatedManagedBeanHandler(value, scope, name);
+ //AnnotatedManagedBeanHandler handler =
+ // new AnnotatedManagedBeanHandler(value, scope, name);
- handler.invokePreDestroy();
- }
+ //handler.invokePreDestroy();
+ try
+ {
+ // AnnotationProcessor already configured
+ AnnotationProcessorFactory.getAnnotatonProcessor(null).preDestroy(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/AnnotatedExceptionManagedBeanHandlerTestCase.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedExceptionManagedBeanHandlerTestCase.java?view=diff&rev=514387&r1=514386&r2=514387
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedExceptionManagedBeanHandlerTestCase.java (original)
+++ myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedExceptionManagedBeanHandlerTestCase.java Sun Mar 4 04:29:47 2007
@@ -17,7 +17,7 @@
protected final String NAME = "sean_schofield";
public void setUp() {
- managedBean = new AnnotatedManagedBean(new Exception());
+ managedBean = new AnnotatedManagedBean(true);
}
public void testPostConstructShouldNotBlowUpForNoneScope() {
@@ -25,11 +25,11 @@
handler = new AnnotatedManagedBeanHandler(managedBean,
ManagedBeanBuilder.NONE, NAME);
- boolean exceptionThrown = false;
+ boolean exceptionThrown;
try {
- handler.invokePostConstruct();
- } catch (Exception e) {
+ exceptionThrown = handler.invokePostConstruct();
+ } catch (RuntimeException e) {
exceptionThrown = true;
}
@@ -43,11 +43,11 @@
handler = new AnnotatedManagedBeanHandler(managedBean,
ManagedBeanBuilder.NONE, NAME);
- boolean exceptionThrown = false;
+ boolean exceptionThrown;
try {
- handler.invokePreDestroy();
- } catch (Exception e) {
+ exceptionThrown = handler.invokePreDestroy();
+ } catch (RuntimeException e) {
exceptionThrown = true;
}
@@ -58,34 +58,31 @@
public void testPostConstructShouldBlowUpForRequestScope() {
- handler = new AnnotatedManagedBeanHandler(managedBean,
+ try {
+ handler = new AnnotatedManagedBeanHandler(managedBean,
ManagedBeanBuilder.REQUEST, NAME);
- boolean exceptionThrown = false;
+ boolean exceptionThrown;
- try {
- handler.invokePostConstruct();
- } catch (Exception e) {
- exceptionThrown = true;
- }
+ exceptionThrown = handler.invokePostConstruct();
assertTrue(exceptionThrown);
assertTrue(managedBean.isPostConstructCalled());
assertFalse(managedBean.isPreDestroyCalled());
- }
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
+ }
public void testPreDestroyShouldBlowUpForRequestScope() {
handler = new AnnotatedManagedBeanHandler(managedBean,
ManagedBeanBuilder.REQUEST, NAME);
- boolean exceptionThrown = false;
+ boolean exceptionThrown;
- try {
- handler.invokePreDestroy();
- } catch (Exception e) {
- exceptionThrown = true;
- }
+
+ exceptionThrown = handler.invokePreDestroy();
assertTrue(exceptionThrown);
assertFalse(managedBean.isPostConstructCalled());
@@ -97,13 +94,9 @@
handler = new AnnotatedManagedBeanHandler(managedBean,
ManagedBeanBuilder.SESSION, NAME);
- boolean exceptionThrown = false;
+ boolean exceptionThrown;
- try {
- handler.invokePostConstruct();
- } catch (Exception e) {
- exceptionThrown = true;
- }
+ exceptionThrown = handler.invokePostConstruct();
assertTrue(exceptionThrown);
assertTrue(managedBean.isPostConstructCalled());
@@ -115,13 +108,9 @@
handler = new AnnotatedManagedBeanHandler(managedBean,
ManagedBeanBuilder.SESSION, NAME);
- boolean exceptionThrown = false;
+ boolean exceptionThrown;
- try {
- handler.invokePreDestroy();
- } catch (Exception e) {
- exceptionThrown = true;
- }
+ exceptionThrown = handler.invokePreDestroy();
assertTrue(exceptionThrown);
assertFalse(managedBean.isPostConstructCalled());
@@ -133,13 +122,9 @@
handler = new AnnotatedManagedBeanHandler(managedBean,
ManagedBeanBuilder.APPLICATION, NAME);
- boolean exceptionThrown = false;
+ boolean exceptionThrown;
- try {
- handler.invokePostConstruct();
- } catch (Exception e) {
- exceptionThrown = true;
- }
+ exceptionThrown = handler.invokePostConstruct();
assertTrue(exceptionThrown);
assertTrue(managedBean.isPostConstructCalled());
@@ -151,13 +136,9 @@
handler = new AnnotatedManagedBeanHandler(managedBean,
ManagedBeanBuilder.APPLICATION, NAME);
- boolean exceptionThrown = false;
+ boolean exceptionThrown;
- try {
- handler.invokePreDestroy();
- } catch (Exception e) {
- exceptionThrown = true;
- }
+ exceptionThrown = handler.invokePreDestroy();
assertTrue(exceptionThrown);
assertFalse(managedBean.isPostConstructCalled());
Modified: myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBean.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBean.java?view=diff&rev=514387&r1=514386&r2=514387
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBean.java (original)
+++ myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBean.java Sun Mar 4 04:29:47 2007
@@ -13,26 +13,33 @@
private boolean preDestroyCalled = false; // using a stob for a mock here
- private Throwable throwable;
+ boolean throwExcetion;
- public AnnotatedManagedBean(Throwable throwable) {
- this.throwable = throwable;
+
+ public AnnotatedManagedBean()
+ {
+ }
+
+ public AnnotatedManagedBean(boolean throwExcetion) {
+ this.throwExcetion = throwExcetion;
}
@PostConstruct
- public void postConstruct() throws Throwable {
+ public void postConstruct() {
postConstructCalled = true;
- if (throwable != null)
- throw throwable;
- }
+ if (throwExcetion) {
+ throw new RuntimeException();
+ }
+ }
@PreDestroy
- public void preDestroy() throws Throwable {
+ public void preDestroy() {
preDestroyCalled = true;
- if (throwable != null)
- throw throwable;
+ if (throwExcetion) {
+ throw new RuntimeException();
+ }
}
boolean isPostConstructCalled() {
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=514387&r1=514386&r2=514387
==============================================================================
--- 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 Sun Mar 4 04:29:47 2007
@@ -1,117 +1,117 @@
-package org.apache.myfaces.config.annotation;
-
-import junit.framework.TestCase;
-
-import org.apache.myfaces.config.ManagedBeanBuilder;
-
-/**
- * @author Dennis Byrne
- */
-
-public class AnnotatedManagedBeanHandlerTestCase extends TestCase {
-
- protected AnnotatedManagedBean managedBean;
- protected AnnotatedManagedBeanHandler handler;
- protected final String NAME = "volker weber";
-
- public void setUp() {
- managedBean = new AnnotatedManagedBean(null);
- }
-
- public void testPostConstructShouldNotInvokeForNoneScope() {
-
- handler = new AnnotatedManagedBeanHandler(managedBean,
- ManagedBeanBuilder.NONE, NAME);
-
- boolean threwUnchecked = handler.invokePostConstruct();
-
- assertFalse(threwUnchecked);
- assertFalse(managedBean.isPostConstructCalled());
- assertFalse(managedBean.isPreDestroyCalled());
- }
-
- public void testPreDestroyShouldNotInvokeForNoneScope() {
-
- handler = new AnnotatedManagedBeanHandler(managedBean,
- ManagedBeanBuilder.NONE, NAME);
-
- boolean threwUnchecked = handler.invokePreDestroy();
-
- assertFalse(threwUnchecked);
- assertFalse(managedBean.isPostConstructCalled());
- assertFalse(managedBean.isPreDestroyCalled());
- }
-
- public void testPostConstructShouldInvokeForRequestScope() {
-
- handler = new AnnotatedManagedBeanHandler(managedBean,
- ManagedBeanBuilder.REQUEST, NAME);
-
- boolean threwUnchecked = handler.invokePostConstruct();
-
- assertFalse(threwUnchecked);
- assertTrue(managedBean.isPostConstructCalled());
- assertFalse(managedBean.isPreDestroyCalled());
- }
-
- public void testPreDestroyShouldInvokeForRequestScope() {
-
- handler = new AnnotatedManagedBeanHandler(managedBean,
- ManagedBeanBuilder.REQUEST, NAME);
-
- boolean threwUnchecked = handler.invokePreDestroy();
-
- assertFalse(threwUnchecked);
- assertFalse(managedBean.isPostConstructCalled());
- assertTrue(managedBean.isPreDestroyCalled());
- }
-
- public void testPostConstructShouldInvokeForSessionScope() {
-
- handler = new AnnotatedManagedBeanHandler(managedBean,
- ManagedBeanBuilder.SESSION, NAME);
-
- boolean threwUnchecked = handler.invokePostConstruct();
-
- assertFalse(threwUnchecked);
- assertTrue(managedBean.isPostConstructCalled());
- assertFalse(managedBean.isPreDestroyCalled());
- }
-
- public void testPreDestroyShouldInvokeForSessionScope() {
-
- handler = new AnnotatedManagedBeanHandler(managedBean,
- ManagedBeanBuilder.SESSION, NAME);
-
- boolean threwUnchecked = handler.invokePreDestroy();
-
- assertFalse(threwUnchecked);
- assertFalse(managedBean.isPostConstructCalled());
- assertTrue(managedBean.isPreDestroyCalled());
- }
-
- public void testPostConstructShouldInvokeForApplicationScope() {
-
- handler = new AnnotatedManagedBeanHandler(managedBean,
- ManagedBeanBuilder.APPLICATION, NAME);
-
- boolean threwUnchecked = handler.invokePostConstruct();
-
- assertFalse(threwUnchecked);
- assertTrue(managedBean.isPostConstructCalled());
- assertFalse(managedBean.isPreDestroyCalled());
- }
-
- public void testPreDestroyShouldInvokeForApplicationScope() {
-
- handler = new AnnotatedManagedBeanHandler(managedBean,
- ManagedBeanBuilder.APPLICATION, NAME);
-
- boolean threwUnchecked = handler.invokePreDestroy();
-
- assertFalse(threwUnchecked);
- assertFalse(managedBean.isPostConstructCalled());
- assertTrue(managedBean.isPreDestroyCalled());
- }
-
-}
+package org.apache.myfaces.config.annotation;
+
+import junit.framework.TestCase;
+
+import org.apache.myfaces.config.ManagedBeanBuilder;
+
+/**
+ * @author Dennis Byrne
+ */
+
+public class AnnotatedManagedBeanHandlerTestCase extends TestCase {
+
+ protected AnnotatedManagedBean managedBean;
+ protected AnnotatedManagedBeanHandler handler;
+ protected final String NAME = "volker weber";
+
+ public void setUp() {
+ managedBean = new AnnotatedManagedBean();
+ }
+
+ public void testPostConstructShouldNotInvokeForNoneScope() {
+
+ handler = new AnnotatedManagedBeanHandler(managedBean,
+ ManagedBeanBuilder.NONE, NAME);
+
+ boolean threwUnchecked = handler.invokePostConstruct();
+
+ assertFalse(threwUnchecked);
+ assertFalse(managedBean.isPostConstructCalled());
+ assertFalse(managedBean.isPreDestroyCalled());
+ }
+
+ public void testPreDestroyShouldNotInvokeForNoneScope() {
+
+ handler = new AnnotatedManagedBeanHandler(managedBean,
+ ManagedBeanBuilder.NONE, NAME);
+
+ boolean threwUnchecked = handler.invokePreDestroy();
+
+ assertFalse(threwUnchecked);
+ assertFalse(managedBean.isPostConstructCalled());
+ assertFalse(managedBean.isPreDestroyCalled());
+ }
+
+ public void testPostConstructShouldInvokeForRequestScope() {
+
+ handler = new AnnotatedManagedBeanHandler(managedBean,
+ ManagedBeanBuilder.REQUEST, NAME);
+
+ boolean threwUnchecked = handler.invokePostConstruct();
+
+ assertFalse(threwUnchecked);
+ assertTrue(managedBean.isPostConstructCalled());
+ assertFalse(managedBean.isPreDestroyCalled());
+ }
+
+ public void testPreDestroyShouldInvokeForRequestScope() {
+
+ handler = new AnnotatedManagedBeanHandler(managedBean,
+ ManagedBeanBuilder.REQUEST, NAME);
+
+ boolean threwUnchecked = handler.invokePreDestroy();
+
+ assertFalse(threwUnchecked);
+ assertFalse(managedBean.isPostConstructCalled());
+ assertTrue(managedBean.isPreDestroyCalled());
+ }
+
+ public void testPostConstructShouldInvokeForSessionScope() {
+
+ handler = new AnnotatedManagedBeanHandler(managedBean,
+ ManagedBeanBuilder.SESSION, NAME);
+
+ boolean threwUnchecked = handler.invokePostConstruct();
+
+ assertFalse(threwUnchecked);
+ assertTrue(managedBean.isPostConstructCalled());
+ assertFalse(managedBean.isPreDestroyCalled());
+ }
+
+ public void testPreDestroyShouldInvokeForSessionScope() {
+
+ handler = new AnnotatedManagedBeanHandler(managedBean,
+ ManagedBeanBuilder.SESSION, NAME);
+
+ boolean threwUnchecked = handler.invokePreDestroy();
+
+ assertFalse(threwUnchecked);
+ assertFalse(managedBean.isPostConstructCalled());
+ assertTrue(managedBean.isPreDestroyCalled());
+ }
+
+ public void testPostConstructShouldInvokeForApplicationScope() {
+
+ handler = new AnnotatedManagedBeanHandler(managedBean,
+ ManagedBeanBuilder.APPLICATION, NAME);
+
+ boolean threwUnchecked = handler.invokePostConstruct();
+
+ assertFalse(threwUnchecked);
+ assertTrue(managedBean.isPostConstructCalled());
+ assertFalse(managedBean.isPreDestroyCalled());
+ }
+
+ public void testPreDestroyShouldInvokeForApplicationScope() {
+
+ handler = new AnnotatedManagedBeanHandler(managedBean,
+ ManagedBeanBuilder.APPLICATION, NAME);
+
+ boolean threwUnchecked = handler.invokePreDestroy();
+
+ assertFalse(threwUnchecked);
+ assertFalse(managedBean.isPostConstructCalled());
+ assertTrue(managedBean.isPreDestroyCalled());
+ }
+
+}
Modified: myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedRuntimeExceptionManagedBeanHandlerTestCase.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedRuntimeExceptionManagedBeanHandlerTestCase.java?view=diff&rev=514387&r1=514386&r2=514387
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedRuntimeExceptionManagedBeanHandlerTestCase.java (original)
+++ myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedRuntimeExceptionManagedBeanHandlerTestCase.java Sun Mar 4 04:29:47 2007
@@ -15,7 +15,7 @@
protected final String NAME = "Thomas_Spiegl";
public void setUp() {
- managedBean = new AnnotatedManagedBean(new RuntimeException());
+ managedBean = new AnnotatedManagedBean(true);
}
public void testPostConstructShouldNotInvokeForNoneScope() {
Added: myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotationProcessorTestCase.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotationProcessorTestCase.java?view=auto&rev=514387
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotationProcessorTestCase.java (added)
+++ myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotationProcessorTestCase.java Sun Mar 4 04:29:47 2007
@@ -0,0 +1,68 @@
+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.shale.test.base.AbstractJsfTestCase;
+import org.apache.AnnotationProcessor;
+
+import java.lang.reflect.InvocationTargetException;
+
+
+public class AnnotationProcessorTestCase extends AbstractJsfTestCase
+{
+ protected AnnotationProcessor processor;
+ protected AnnotatedManagedBean managedBean;
+
+
+ public AnnotationProcessorTestCase(String string)
+ {
+ super(string);
+ }
+
+ public void setUp() throws Exception {
+ super.setUp();
+ processor = AnnotationProcessorFactory.getAnnotatonProcessor(externalContext);
+ managedBean = new AnnotatedManagedBean();
+ }
+
+ public void testPostConstruct() throws IllegalAccessException, InvocationTargetException
+ {
+ processor.postConstruct(managedBean);
+ assertTrue(managedBean.isPostConstructCalled());
+ assertFalse(managedBean.isPreDestroyCalled());
+ }
+
+ public void testPreDestroy() throws IllegalAccessException, InvocationTargetException
+ {
+ processor.preDestroy(managedBean);
+ assertFalse(managedBean.isPostConstructCalled());
+ assertTrue(managedBean.isPreDestroyCalled());
+ }
+
+ public void testPostConstructException() throws IllegalAccessException, InvocationTargetException
+ {
+ try
+ {
+ processor.postConstruct(new AnnotatedManagedBean(true));
+ } catch (InvocationTargetException e) {
+ return;
+ }
+ assertTrue(false);
+
+ }
+}
Added: myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/OwnAnnotationProcessorTestCase.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/OwnAnnotationProcessorTestCase.java?view=auto&rev=514387
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/OwnAnnotationProcessorTestCase.java (added)
+++ myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/OwnAnnotationProcessorTestCase.java Sun Mar 4 04:29:47 2007
@@ -0,0 +1,49 @@
+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.shale.test.base.AbstractJsfTestCase;
+import org.apache.AnnotationProcessor;
+
+
+public class OwnAnnotationProcessorTestCase extends AbstractJsfTestCase
+{
+ protected AnnotationProcessor processor;
+ protected AnnotatedManagedBean managedBean;
+ private static final String TEST_ANNOTATION_PROCESSOR = "org.apache.myfaces.config.annotation.TestAnnotationProcessor";
+
+
+ public OwnAnnotationProcessorTestCase(String string)
+ {
+ super(string);
+ }
+
+ public void setUp() throws Exception {
+ super.setUp();
+ AnnotationProcessorFactory.release();
+ servletContext.addInitParameter(AnnotationProcessorFactory.ANNOTATION_PROCESSOR_PROVIDER, TEST_ANNOTATION_PROCESSOR);
+ processor = AnnotationProcessorFactory.getAnnotatonProcessor(externalContext);
+ managedBean = new AnnotatedManagedBean();
+
+ }
+
+ public void testOwnProcessor()
+ {
+ assertEquals(TEST_ANNOTATION_PROCESSOR, processor.getClass().getName());
+ }
+}
Added: myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/TestAnnotationProcessor.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/TestAnnotationProcessor.java?view=auto&rev=514387
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/TestAnnotationProcessor.java (added)
+++ myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/TestAnnotationProcessor.java Sun Mar 4 04:29:47 2007
@@ -0,0 +1,44 @@
+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 TestAnnotationProcessor implements AnnotationProcessor
+{
+ private AnnotationProcessor processor = new NoInjectionAnnotationProcessor();
+
+ public void postConstruct(Object instance) throws IllegalAccessException, InvocationTargetException
+ {
+ processor.postConstruct(instance);
+ }
+
+ public void preDestroy(Object instance) throws IllegalAccessException, InvocationTargetException
+ {
+ processor.preDestroy(instance);
+ }
+
+ public void processAnnotations(Object instance) throws IllegalAccessException, InvocationTargetException, NamingException
+ {
+ processor.processAnnotations(instance);
+ }
+}
Added: myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/TestDiscoverableAnnotationProcessor.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/TestDiscoverableAnnotationProcessor.java?view=auto&rev=514387
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/TestDiscoverableAnnotationProcessor.java (added)
+++ myfaces/core/branches/jsf12/impl/src/test/java/org/apache/myfaces/config/annotation/TestDiscoverableAnnotationProcessor.java Sun Mar 4 04:29:47 2007
@@ -0,0 +1,51 @@
+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.myfaces.DiscoverableAnnotationProcessor;
+import org.apache.AnnotationProcessor;
+
+import javax.naming.NamingException;
+import java.lang.reflect.InvocationTargetException;
+
+
+
+public class TestDiscoverableAnnotationProcessor implements DiscoverableAnnotationProcessor
+{
+ private AnnotationProcessor processor = new NoInjectionAnnotationProcessor();
+
+ public boolean isAvailable()
+ {
+ return true;
+ }
+
+ public void postConstruct(Object instance) throws IllegalAccessException, InvocationTargetException
+ {
+ processor.postConstruct(instance);
+ }
+
+ public void preDestroy(Object instance) throws IllegalAccessException, InvocationTargetException
+ {
+ processor.preDestroy(instance);
+ }
+
+ public void processAnnotations(Object instance) throws IllegalAccessException, InvocationTargetException, NamingException
+ {
+ processor.processAnnotations(instance);
+ }
+}
Added: myfaces/core/branches/jsf12/impl/src/test/resources/META-INF/services/org.apache.myfaces.AnnotationProcessor
URL: http://svn.apache.org/viewvc/myfaces/core/branches/jsf12/impl/src/test/resources/META-INF/services/org.apache.myfaces.AnnotationProcessor?view=auto&rev=514387
==============================================================================
--- myfaces/core/branches/jsf12/impl/src/test/resources/META-INF/services/org.apache.myfaces.AnnotationProcessor (added)
+++ myfaces/core/branches/jsf12/impl/src/test/resources/META-INF/services/org.apache.myfaces.AnnotationProcessor Sun Mar 4 04:29:47 2007
@@ -0,0 +1 @@
+org.apache.myfaces.config.annotation.TestDiscoverableAnnotationProcessor
\ No newline at end of file