You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2009/05/15 07:07:37 UTC

svn commit: r775006 - in /myfaces/core/trunk_1.2.x/impl/src: main/java/org/apache/myfaces/config/ main/java/org/apache/myfaces/config/annotation/ test/java/org/apache/myfaces/config/annotation/

Author: lu4242
Date: Fri May 15 05:07:37 2009
New Revision: 775006

URL: http://svn.apache.org/viewvc?rev=775006&view=rev
Log:
MYFACES-1761 Handling PostConstruct annotations - wrong order

Added:
    myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/LifecycleProvider2.java   (with props)
    myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBean2.java   (with props)
    myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/Myfaces1761TestCase.java   (with props)
    myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/TestLifecycleProvider2.java   (with props)
Modified:
    myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/ManagedBeanBuilder.java
    myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/NoAnnotationLifecyleProvider.java
    myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/NoInjectionAnnotationLifecycleProvider.java
    myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/TomcatAnnotationLifecycleProvider.java
    myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotationProcessorTestCase.java

Modified: myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/ManagedBeanBuilder.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/ManagedBeanBuilder.java?rev=775006&r1=775005&r2=775006&view=diff
==============================================================================
--- myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/ManagedBeanBuilder.java (original)
+++ myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/ManagedBeanBuilder.java Fri May 15 05:07:37 2009
@@ -40,8 +40,9 @@
 import org.apache.commons.beanutils.PropertyUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.myfaces.config.annotation.LifecycleProviderFactory;
 import org.apache.myfaces.config.annotation.LifecycleProvider;
+import org.apache.myfaces.config.annotation.LifecycleProvider2;
+import org.apache.myfaces.config.annotation.LifecycleProviderFactory;
 import org.apache.myfaces.config.element.ListEntries;
 import org.apache.myfaces.config.element.ListEntry;
 import org.apache.myfaces.config.element.ManagedBean;
@@ -132,6 +133,14 @@
                             + bean.getClass().getName() + " for managed bean "
                             + beanConfiguration.getManagedBeanName() + '.');
             }
+            
+            // MYFACES-1761 if implements LifecycleProvider,
+            //PostConstruct was already called, but if implements
+            //LifecycleProvider2, call it now.
+            if (lifecycleProvider instanceof LifecycleProvider2)
+            {
+                ((LifecycleProvider2)lifecycleProvider).postConstruct(bean);
+            }
             return bean;
         }
         catch (IllegalAccessException e)

Added: myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/LifecycleProvider2.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/LifecycleProvider2.java?rev=775006&view=auto
==============================================================================
--- myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/LifecycleProvider2.java (added)
+++ myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/LifecycleProvider2.java Fri May 15 05:07:37 2009
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.config.annotation;
+
+import java.lang.reflect.InvocationTargetException;
+
+import javax.naming.NamingException;
+
+/**
+ * Proposed interface to annotation service. An implementation of this class needs to know the appropriate classloader,
+ * dependencies to be injected, and lifecycle methods to be called.
+ *
+ * @version $Rev$ $Date$
+ */
+public interface LifecycleProvider2 extends LifecycleProvider
+{
+    /**
+     * Create an object of the class with the supplied name, inject dependencies as appropriate.
+     *
+     * @param className name of the class of the desired object
+     * @return a fully constructed, dependency-injected, and initialized object.
+     */
+    Object newInstance(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException, InvocationTargetException;
+
+    /**
+     * Call a postConstruct method as appropriate.
+     *
+     * @param o object to initialize
+     */
+    void postConstruct(Object o) throws IllegalAccessException, InvocationTargetException;
+}
\ No newline at end of file

Propchange: myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/LifecycleProvider2.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/LifecycleProvider2.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/NoAnnotationLifecyleProvider.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/NoAnnotationLifecyleProvider.java?rev=775006&r1=775005&r2=775006&view=diff
==============================================================================
--- myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/NoAnnotationLifecyleProvider.java (original)
+++ myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/NoAnnotationLifecyleProvider.java Fri May 15 05:07:37 2009
@@ -22,7 +22,7 @@
 
 import java.lang.reflect.InvocationTargetException;
 
-public class NoAnnotationLifecyleProvider implements LifecycleProvider
+public class NoAnnotationLifecyleProvider implements LifecycleProvider2
 {
 
 
@@ -37,4 +37,10 @@
         return clazz.newInstance();
     }
 
+    public void postConstruct(Object o) throws IllegalAccessException,
+            InvocationTargetException
+    {
+        // No op
+    }
+
 }

Modified: myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/NoInjectionAnnotationLifecycleProvider.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/NoInjectionAnnotationLifecycleProvider.java?rev=775006&r1=775005&r2=775006&view=diff
==============================================================================
--- myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/NoInjectionAnnotationLifecycleProvider.java (original)
+++ myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/NoInjectionAnnotationLifecycleProvider.java Fri May 15 05:07:37 2009
@@ -18,14 +18,15 @@
  */
 package org.apache.myfaces.config.annotation;
 
-import org.apache.myfaces.shared_impl.util.ClassUtils;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 
 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;
+
+import org.apache.myfaces.shared_impl.util.ClassUtils;
 
 /**
  * See SRV.14.5 Servlet Specification Version 2.5 JSR 154
@@ -33,7 +34,8 @@
 
  */
 
-public class NoInjectionAnnotationLifecycleProvider implements LifecycleProvider {
+public class NoInjectionAnnotationLifecycleProvider implements LifecycleProvider2
+{
 
 
     public Object newInstance(String className)
@@ -42,14 +44,14 @@
         Class clazz = ClassUtils.classForName(className);
         Object object = clazz.newInstance();
         processAnnotations(object);
-        postConstruct(object);
+        //postConstruct(object);
         return object;
     }
 
     /**
      * Call postConstruct method on the specified instance.
      */
-    private void postConstruct(Object instance)
+    public void postConstruct(Object instance)
             throws IllegalAccessException, InvocationTargetException
     {
 

Modified: myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/TomcatAnnotationLifecycleProvider.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/TomcatAnnotationLifecycleProvider.java?rev=775006&r1=775005&r2=775006&view=diff
==============================================================================
--- myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/TomcatAnnotationLifecycleProvider.java (original)
+++ myfaces/core/trunk_1.2.x/impl/src/main/java/org/apache/myfaces/config/annotation/TomcatAnnotationLifecycleProvider.java Fri May 15 05:07:37 2009
@@ -27,7 +27,8 @@
 import javax.servlet.ServletContext;
 import java.lang.reflect.InvocationTargetException;
 
-public class TomcatAnnotationLifecycleProvider implements DiscoverableLifecycleProvider
+public class TomcatAnnotationLifecycleProvider implements 
+    DiscoverableLifecycleProvider, LifecycleProvider2
 {
     private static Log log = LogFactory.getLog(TomcatAnnotationLifecycleProvider.class);
 
@@ -46,7 +47,7 @@
         log.info("Creating instance of " + className);
         Object object = clazz.newInstance();
         annotationProcessor.processAnnotations(object);
-        annotationProcessor.postConstruct(object);
+        //annotationProcessor.postConstruct(object);
         return object;
     }
 
@@ -70,4 +71,11 @@
         return false;
     }
 
+
+    public void postConstruct(Object o) throws IllegalAccessException,
+            InvocationTargetException
+    {
+        annotationProcessor.postConstruct(o);
+    }
+
 }

Added: myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBean2.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBean2.java?rev=775006&view=auto
==============================================================================
--- myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBean2.java (added)
+++ myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBean2.java Fri May 15 05:07:37 2009
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.myfaces.config.annotation;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+
+/**
+ * @author Leonardo Uribe
+ */
+
+public class AnnotatedManagedBean2 {
+
+    private boolean postConstructCalled = false; // using a stub for a mock
+
+    private boolean preDestroyCalled = false; // using a stob for a mock here
+
+    boolean throwExcetion;
+
+    private String managedProperty;
+
+    public AnnotatedManagedBean2()
+    {
+    }
+
+    public AnnotatedManagedBean2(boolean throwExcetion) {
+        this.throwExcetion = throwExcetion;
+    }
+
+    @PostConstruct
+    public void postConstruct()  {
+        
+        if (managedProperty == null)
+        {
+            throw new RuntimeException("managedProperty must be initialized before call of postConstruct() method");
+        }
+        
+        postConstructCalled = true;
+
+        if (throwExcetion) {
+            throw new RuntimeException();
+        }
+    }
+
+    @PreDestroy
+    public void preDestroy() {
+        preDestroyCalled = true;
+
+        if (throwExcetion) {
+            throw new RuntimeException();
+        }
+    }
+
+    boolean isPostConstructCalled() {
+        return postConstructCalled;
+    }
+
+    boolean isPreDestroyCalled() {
+        return preDestroyCalled;
+    }
+    
+    public String getManagedProperty() {
+        return managedProperty;
+    }
+
+    public void setManagedProperty(String managedProperty) {
+        //Set throught injection
+        if (postConstructCalled)
+        {
+            throw new RuntimeException();
+        }
+            
+        this.managedProperty = managedProperty;
+    }
+
+}

Propchange: myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBean2.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotatedManagedBean2.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotationProcessorTestCase.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotationProcessorTestCase.java?rev=775006&r1=775005&r2=775006&view=diff
==============================================================================
--- myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotationProcessorTestCase.java (original)
+++ myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/AnnotationProcessorTestCase.java Fri May 15 05:07:37 2009
@@ -25,7 +25,7 @@
 
 public class AnnotationProcessorTestCase extends AbstractJsfTestCase
 {
-    protected LifecycleProvider lifecycleProvider;
+    protected LifecycleProvider2 lifecycleProvider;
     protected AnnotatedManagedBean managedBean;
 
 
@@ -43,6 +43,7 @@
     public void testPostConstruct() throws IllegalAccessException, InvocationTargetException, NamingException, InstantiationException, ClassNotFoundException
     {
         AnnotatedManagedBean managedBean = (AnnotatedManagedBean) lifecycleProvider.newInstance(AnnotatedManagedBean.class.getName());
+        lifecycleProvider.postConstruct(managedBean);
         assertTrue(managedBean.isPostConstructCalled());
         assertFalse(managedBean.isPreDestroyCalled());
     }
@@ -50,6 +51,7 @@
     public void testPreDestroy() throws IllegalAccessException, InvocationTargetException, NamingException, InstantiationException, ClassNotFoundException
     {
         AnnotatedManagedBean managedBean = (AnnotatedManagedBean) lifecycleProvider.newInstance(AnnotatedManagedBean.class.getName());
+        lifecycleProvider.postConstruct(managedBean);
         lifecycleProvider.destroyInstance(managedBean);
         assertTrue(managedBean.isPostConstructCalled());
         assertTrue(managedBean.isPreDestroyCalled());

Added: myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/Myfaces1761TestCase.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/Myfaces1761TestCase.java?rev=775006&view=auto
==============================================================================
--- myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/Myfaces1761TestCase.java (added)
+++ myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/Myfaces1761TestCase.java Fri May 15 05:07:37 2009
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.myfaces.config.annotation;
+
+import org.apache.myfaces.config.ManagedBeanBuilder;
+import org.apache.myfaces.config.impl.digester.elements.ManagedBean;
+import org.apache.myfaces.config.impl.digester.elements.ManagedProperty;
+import org.apache.shale.test.base.AbstractJsfTestCase;
+
+/**
+ * Test MYFACES-1761 Handling PostConstruct annotations - wrong order 
+ * 
+ * @author Leonardo Uribe
+ *
+ */
+public class Myfaces1761TestCase extends AbstractJsfTestCase
+{
+
+    protected ManagedBeanBuilder managedBeanBuilder;
+    protected LifecycleProvider2 lifecycleProvider;
+    protected ManagedBean beanConfiguration;
+    
+    private static final String TEST_LIFECYCLE_PROVIDER = TestLifecycleProvider2.class.getName();
+    
+    protected static final String INJECTED_VALUE = "tatiana";
+    
+    public Myfaces1761TestCase(String name)
+    {
+        super(name);
+    }
+
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        managedBeanBuilder  = new ManagedBeanBuilder();
+        
+        beanConfiguration = new ManagedBean();        
+        beanConfiguration.setBeanClass(AnnotatedManagedBean2.class.getName());
+        beanConfiguration.setName("managed");
+        beanConfiguration.setScope("request");
+        
+        ManagedProperty managedProperty = new ManagedProperty();
+        managedProperty.setPropertyName("managedProperty");
+        managedProperty.setValue(INJECTED_VALUE);
+        beanConfiguration.addProperty(managedProperty);
+        
+        LifecycleProviderFactory.getLifecycleProviderFactory().release();
+        servletContext.addInitParameter(DefaultLifecycleProviderFactory.LIFECYCLE_PROVIDER, TEST_LIFECYCLE_PROVIDER);
+    }
+
+    public void tearDown() throws Exception
+    {
+        super.tearDown();
+        managedBeanBuilder = null;
+        LifecycleProviderFactory.getLifecycleProviderFactory().release();
+    }
+    
+    public void testPostConstruct() throws Exception
+    {
+        AnnotatedManagedBean2 bean = (AnnotatedManagedBean2) managedBeanBuilder.buildManagedBean(facesContext, beanConfiguration);
+        assertEquals(INJECTED_VALUE, bean.getManagedProperty());
+        assertTrue(bean.isPostConstructCalled());
+    }
+}

Propchange: myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/Myfaces1761TestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/Myfaces1761TestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/TestLifecycleProvider2.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/TestLifecycleProvider2.java?rev=775006&view=auto
==============================================================================
--- myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/TestLifecycleProvider2.java (added)
+++ myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/TestLifecycleProvider2.java Fri May 15 05:07:37 2009
@@ -0,0 +1,47 @@
+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 java.lang.reflect.InvocationTargetException;
+
+
+public class TestLifecycleProvider2 implements LifecycleProvider2
+{
+    private LifecycleProvider2 processor = new NoInjectionAnnotationLifecycleProvider();
+
+
+    public Object newInstance(String className) throws InstantiationException, NamingException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
+    {
+        return processor.newInstance(className);
+    }
+
+
+    public void destroyInstance(Object instance) throws IllegalAccessException, InvocationTargetException
+    {
+        processor.destroyInstance(instance);
+    }
+
+
+    public void postConstruct(Object instance) throws IllegalAccessException,
+            InvocationTargetException
+    {
+        processor.postConstruct(instance);
+    }
+}

Propchange: myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/TestLifecycleProvider2.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/core/trunk_1.2.x/impl/src/test/java/org/apache/myfaces/config/annotation/TestLifecycleProvider2.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL