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 2017/02/02 01:09:09 UTC

svn commit: r1781342 - in /myfaces/core/branches/2.3.x: api/src/main/java/javax/faces/annotation/ impl/src/main/java/org/apache/myfaces/cdi/bean/ impl/src/main/resources/META-INF/services/

Author: lu4242
Date: Thu Feb  2 01:09:08 2017
New Revision: 1781342

URL: http://svn.apache.org/viewvc?rev=1781342&view=rev
Log:
MYFACES-4079 Implement CDI changes for JSF 2.3 (@ManagedProperty)

Added:
    myfaces/core/branches/2.3.x/api/src/main/java/javax/faces/annotation/ManagedProperty.java
    myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/
    myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/DynamicManagedPropertyProducer.java
    myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/ManagedPropertyExtension.java
    myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/ManagedPropertyInfo.java
      - copied, changed from r1779404, myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/component/ResolveComponentInfo.java
Modified:
    myfaces/core/branches/2.3.x/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension

Added: myfaces/core/branches/2.3.x/api/src/main/java/javax/faces/annotation/ManagedProperty.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/api/src/main/java/javax/faces/annotation/ManagedProperty.java?rev=1781342&view=auto
==============================================================================
--- myfaces/core/branches/2.3.x/api/src/main/java/javax/faces/annotation/ManagedProperty.java (added)
+++ myfaces/core/branches/2.3.x/api/src/main/java/javax/faces/annotation/ManagedProperty.java Thu Feb  2 01:09:08 2017
@@ -0,0 +1,37 @@
+/*
+ * 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 javax.faces.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import javax.inject.Qualifier;
+
+/**
+ *
+ */
+@Qualifier
+@Target(value=ElementType.FIELD)
+@Retention(value=RetentionPolicy.RUNTIME)
+public @interface ManagedProperty
+{
+    public String value() default "";
+}

Added: myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/DynamicManagedPropertyProducer.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/DynamicManagedPropertyProducer.java?rev=1781342&view=auto
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/DynamicManagedPropertyProducer.java (added)
+++ myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/DynamicManagedPropertyProducer.java Thu Feb  2 01:09:08 2017
@@ -0,0 +1,155 @@
+/*
+ * 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.cdi.bean;
+
+import java.io.Serializable;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import static java.util.Arrays.asList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import javax.enterprise.context.Dependent;
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.inject.spi.PassivationCapable;
+import javax.enterprise.util.AnnotationLiteral;
+import javax.faces.annotation.ManagedProperty;
+import javax.faces.context.FacesContext;
+import org.apache.myfaces.shared.util.ClassUtils;
+
+/**
+ *
+ */
+public class DynamicManagedPropertyProducer implements Bean<Object>, Serializable, PassivationCapable
+{
+    private static final long serialVersionUID = 1L;
+    
+
+    private BeanManager beanManager;
+    private ManagedPropertyInfo typeInfo;
+    private Set<Type> types;
+    private Class<?> beanClass;
+
+    public DynamicManagedPropertyProducer(BeanManager beanManager, ManagedPropertyInfo typeInfo)
+    {
+        this.beanManager = beanManager;
+        this.typeInfo = typeInfo;
+        types = new HashSet<Type>(asList(typeInfo.getType(), Object.class));
+        beanClass = ClassUtils.simpleClassForName(typeInfo.getType().getTypeName());
+    }
+    
+    @Override
+    public String getId()
+    {
+        return typeInfo.getType()+"_"+typeInfo.getExpression();
+    }
+
+    public static class DefaultAnnotationLiteral extends AnnotationLiteral<ManagedProperty> implements ManagedProperty
+    {
+        private static final long serialVersionUID = 1L;
+        
+        private String value;
+
+        public DefaultAnnotationLiteral(String value)
+        {
+            this.value = value;
+        }
+
+        @Override
+        public String value()
+        {
+            return value;
+        }
+    }
+    
+    @Override
+    public Class<?> getBeanClass()
+    {
+        return beanClass;
+    }
+
+    @Override
+    public Set<Type> getTypes()
+    {
+        return types;
+    }
+    
+    @Override
+    public Set<Annotation> getQualifiers()
+    {
+        return Collections.singleton(
+                (Annotation) new DynamicManagedPropertyProducer.DefaultAnnotationLiteral(typeInfo.getExpression()));
+    }
+    
+
+    @Override
+    public Class<? extends Annotation> getScope()
+    {
+        return Dependent.class;
+    }
+
+    @Override
+    public String getName()
+    {
+        return null;
+    }
+    
+
+    @Override
+    public Set<Class<? extends Annotation>> getStereotypes()
+    {
+        return Collections.emptySet();
+    }
+
+    @Override
+    public boolean isAlternative()
+    {
+        return false;
+    }
+    
+    @Override
+    public Set<InjectionPoint> getInjectionPoints()
+    {
+        return Collections.emptySet();
+    }
+
+    @Override
+    public boolean isNullable()
+    {
+        return true;
+    }
+    
+    @Override
+    public Object create(CreationalContext<Object> cc)
+    {
+        FacesContext facesContext = FacesContext.getCurrentInstance();
+        return facesContext.getApplication().evaluateExpressionGet(
+                facesContext, typeInfo.getExpression(), beanClass);
+    }
+
+    @Override
+    public void destroy(Object t, CreationalContext<Object> cc)
+    {
+    }
+    
+}

Added: myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/ManagedPropertyExtension.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/ManagedPropertyExtension.java?rev=1781342&view=auto
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/ManagedPropertyExtension.java (added)
+++ myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/ManagedPropertyExtension.java Thu Feb  2 01:09:08 2017
@@ -0,0 +1,65 @@
+/*
+ * 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.cdi.bean;
+
+import java.lang.reflect.Type;
+import java.util.HashSet;
+import java.util.Set;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.Annotated;
+import javax.enterprise.inject.spi.AnnotatedField;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.ProcessManagedBean;
+import javax.faces.annotation.ManagedProperty;
+
+/**
+ *
+ */
+public class ManagedPropertyExtension implements Extension
+{
+    private Set<ManagedPropertyInfo> types = new HashSet<ManagedPropertyInfo>();
+
+    public <T> void collect(@Observes ProcessManagedBean<T> event)
+    {
+        for (AnnotatedField<?> field : event.getAnnotatedBeanClass().getFields())
+        {
+            addAnnotatedTypeIfNecessary(field);
+        }
+    }
+    
+    private void addAnnotatedTypeIfNecessary(Annotated annotated)
+    {
+        if (annotated.isAnnotationPresent(ManagedProperty.class))
+        {
+            Type type = annotated.getBaseType();
+
+            types.add(new ManagedPropertyInfo(type, annotated.getAnnotation(ManagedProperty.class).value()));
+        }
+    }
+    
+    public void afterBean(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager)
+    {
+        for (ManagedPropertyInfo typeInfo : types)
+        {
+            afterBeanDiscovery.addBean(new DynamicManagedPropertyProducer(beanManager, typeInfo));
+        }
+    }
+}

Copied: myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/ManagedPropertyInfo.java (from r1779404, myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/component/ResolveComponentInfo.java)
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/ManagedPropertyInfo.java?p2=myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/ManagedPropertyInfo.java&p1=myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/component/ResolveComponentInfo.java&r1=1779404&r2=1781342&rev=1781342&view=diff
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/component/ResolveComponentInfo.java (original)
+++ myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/ManagedPropertyInfo.java Thu Feb  2 01:09:08 2017
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.myfaces.cdi.component;
+package org.apache.myfaces.cdi.bean;
 
 import java.io.Serializable;
 import java.lang.reflect.Type;
@@ -25,14 +25,14 @@ import java.util.Objects;
 /**
  *
  */
-public class ResolveComponentInfo implements Serializable
+public class ManagedPropertyInfo implements Serializable
 {
 
     private Type type;
 
     private String expression;
 
-    public ResolveComponentInfo(Type type, String expression)
+    public ManagedPropertyInfo(Type type, String expression)
     {
         this.type = type;
         this.expression = expression;
@@ -74,7 +74,7 @@ public class ResolveComponentInfo implem
         {
             return false;
         }
-        final ResolveComponentInfo other = (ResolveComponentInfo) obj;
+        final ManagedPropertyInfo other = (ManagedPropertyInfo) obj;
         if (!Objects.equals(this.type, other.type))
         {
             return false;

Modified: myfaces/core/branches/2.3.x/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension?rev=1781342&r1=1781341&r2=1781342&view=diff
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension (original)
+++ myfaces/core/branches/2.3.x/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension Thu Feb  2 01:09:08 2017
@@ -7,3 +7,4 @@ org.apache.myfaces.push.cdi.PushContextC
 org.apache.myfaces.cdi.faces.FacesScopeContextExtension
 org.apache.myfaces.cdi.viewTransient.ViewTransientScopeContextExtension
 org.apache.myfaces.cdi.component.ComponentBindingExtension
+org.apache.myfaces.cdi.bean.ManagedPropertyExtension