You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by gp...@apache.org on 2010/09/29 18:05:30 UTC
svn commit: r1002705 - in /myfaces/extensions/cdi/trunk:
core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/
jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/system/
jee-modules/jsf...
Author: gpetracek
Date: Wed Sep 29 16:05:29 2010
New Revision: 1002705
URL: http://svn.apache.org/viewvc?rev=1002705&view=rev
Log:
EXTCDI-67 first draft and minor refactorings
Added:
myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/NamedLiteral.java
myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/system/CodiJsf2SystemEventListener.java
- copied, changed from r1002547, myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/system/CodiStartupListener.java
myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/scope/mapped/
myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/scope/mapped/Jsf2BeanWrapper.java
myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/scope/mapped/MappedJsf2ScopeExtension.java
Removed:
myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/system/CodiStartupListener.java
Modified:
myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/system/SystemEventBroadcaster.java
myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/META-INF/faces-config.xml
myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
Added: myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/NamedLiteral.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/NamedLiteral.java?rev=1002705&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/NamedLiteral.java (added)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/NamedLiteral.java Wed Sep 29 16:05:29 2010
@@ -0,0 +1,44 @@
+/*
+ * 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.extensions.cdi.core.impl.utils;
+
+import javax.inject.Named;
+import javax.enterprise.util.AnnotationLiteral;
+
+/**
+ * Literal for the {@link javax.inject.Named} annotation.
+ *
+ * @author Gerhard Petracek
+ */
+public class NamedLiteral extends AnnotationLiteral<Named> implements Named
+{
+ private static final long serialVersionUID = -275279485237713614L;
+
+ private final String name;
+
+ public NamedLiteral(String name)
+ {
+ this.name = name;
+ }
+
+ public String value()
+ {
+ return name;
+ }
+}
Copied: myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/system/CodiJsf2SystemEventListener.java (from r1002547, myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/system/CodiStartupListener.java)
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/system/CodiJsf2SystemEventListener.java?p2=myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/system/CodiJsf2SystemEventListener.java&p1=myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/system/CodiStartupListener.java&r1=1002547&r2=1002705&rev=1002705&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/system/CodiStartupListener.java (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/system/CodiJsf2SystemEventListener.java Wed Sep 29 16:05:29 2010
@@ -25,16 +25,17 @@ import org.apache.myfaces.extensions.cdi
import javax.faces.event.SystemEventListener;
import javax.faces.event.SystemEvent;
import javax.faces.event.PostConstructApplicationEvent;
+import javax.faces.event.PreDestroyApplicationEvent;
import javax.faces.application.Application;
/**
* @author Gerhard Petracek
*/
-public class CodiStartupListener implements SystemEventListener, Deactivatable
+public class CodiJsf2SystemEventListener implements SystemEventListener, Deactivatable
{
private final boolean deactivated;
- public CodiStartupListener()
+ public CodiJsf2SystemEventListener()
{
this.deactivated = !isActivated();
}
@@ -50,9 +51,9 @@ public class CodiStartupListener impleme
{
resolveBroadcaster().broadcastApplicationStartupEvent((PostConstructApplicationEvent)systemEvent);
}
- else
+ else if(systemEvent instanceof PreDestroyApplicationEvent)
{
- resolveBroadcaster().logError(systemEvent);
+ resolveBroadcaster().broadcastShutdownApplicationEvent((PreDestroyApplicationEvent)systemEvent);
}
}
Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/system/SystemEventBroadcaster.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/system/SystemEventBroadcaster.java?rev=1002705&r1=1002704&r2=1002705&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/system/SystemEventBroadcaster.java (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/system/SystemEventBroadcaster.java Wed Sep 29 16:05:29 2010
@@ -22,8 +22,8 @@ import org.apache.myfaces.extensions.cdi
import javax.inject.Inject;
import javax.inject.Named;
-import javax.faces.event.SystemEvent;
import javax.faces.event.PostConstructApplicationEvent;
+import javax.faces.event.PreDestroyApplicationEvent;
import javax.enterprise.event.Event;
import javax.enterprise.context.Dependent;
import java.util.logging.Logger;
@@ -40,7 +40,10 @@ public class SystemEventBroadcaster
private final Logger logger = Logger.getLogger(SystemEventBroadcaster.class.getName());
@Inject
- private Event<PostConstructApplicationEvent> postConstructApplicationEventEvent;
+ private Event<PostConstructApplicationEvent> postConstructApplicationEvent;
+
+ @Inject
+ private Event<PreDestroyApplicationEvent> preDestroyApplicationEvent;
@Inject
private ProjectStage projectStage;
@@ -51,23 +54,11 @@ public class SystemEventBroadcaster
void broadcastApplicationStartupEvent(PostConstructApplicationEvent postConstructApplicationEvent)
{
- this.postConstructApplicationEventEvent.fire(postConstructApplicationEvent);
+ this.postConstructApplicationEvent.fire(postConstructApplicationEvent);
}
- void logError(SystemEvent systemEvent)
+ void broadcastShutdownApplicationEvent(PreDestroyApplicationEvent preDestroyApplicationEvent)
{
- if(ProjectStage.Development.equals(this.projectStage))
- {
- String messageText = "failed to fire " + PostConstructApplicationEvent.class.getName();
-
- if(systemEvent == null)
- {
- this.logger.severe(messageText + " - the event is null.");
- }
- else
- {
- this.logger.severe(messageText + " - the event is of type: " + systemEvent.getClass().getName());
- }
- }
+ this.preDestroyApplicationEvent.fire(preDestroyApplicationEvent);
}
}
Added: myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/scope/mapped/Jsf2BeanWrapper.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/scope/mapped/Jsf2BeanWrapper.java?rev=1002705&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/scope/mapped/Jsf2BeanWrapper.java (added)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/scope/mapped/Jsf2BeanWrapper.java Wed Sep 29 16:05:29 2010
@@ -0,0 +1,114 @@
+/*
+ * 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.extensions.cdi.jsf2.impl.scope.mapped;
+
+import org.apache.myfaces.extensions.cdi.core.api.tools.DefaultAnnotation;
+import org.apache.myfaces.extensions.cdi.core.impl.utils.NamedLiteral;
+
+import javax.enterprise.inject.spi.AnnotatedType;
+import javax.faces.bean.ManagedBean;
+import javax.inject.Named;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Set;
+import java.util.HashSet;
+import java.lang.reflect.Type;
+import java.lang.annotation.Annotation;
+
+/**
+ * @author Gerhard Petracek
+ */
+class Jsf2BeanWrapper implements AnnotatedType<Object>
+{
+ private final AnnotatedType wrapped;
+ private Map<Class<? extends Annotation>, Annotation> annotations;
+ private Set<Annotation> annotationSet; //TODO
+
+ public Jsf2BeanWrapper(AnnotatedType wrapped,
+ Class<? extends Annotation> cdiScopeAnnotation,
+ Class<? extends Annotation> jsf2ScopeAnnotation)
+ {
+ this.wrapped = wrapped;
+ Set<Annotation> originalAnnotationSet = wrapped.getAnnotations();
+ this.annotations = new HashMap<Class<? extends Annotation>, Annotation>(originalAnnotationSet.size());
+
+ for(Annotation originalAnnotation : originalAnnotationSet)
+ {
+ if(originalAnnotation.annotationType().equals(ManagedBean.class))
+ {
+ this.annotations.put(Named.class, new NamedLiteral(((ManagedBean)originalAnnotation).name()));
+ }
+ else if(!originalAnnotation.annotationType().equals(jsf2ScopeAnnotation))
+ {
+ this.annotations.put(originalAnnotation.annotationType(), originalAnnotation);
+ }
+ }
+
+ //TODO
+ this.annotations.put(cdiScopeAnnotation, DefaultAnnotation.of(cdiScopeAnnotation));
+
+ this.annotationSet = new HashSet<Annotation>(this.annotations.size());
+ this.annotationSet.addAll(this.annotations.values());
+ }
+
+ public Class getJavaClass()
+ {
+ return wrapped.getJavaClass();
+ }
+
+ public Set getConstructors()
+ {
+ return wrapped.getConstructors();
+ }
+
+ public Set getMethods()
+ {
+ return wrapped.getMethods();
+ }
+
+ public Set getFields()
+ {
+ return wrapped.getFields();
+ }
+
+ public Type getBaseType()
+ {
+ return wrapped.getBaseType();
+ }
+
+ public Set<Type> getTypeClosure()
+ {
+ return wrapped.getTypeClosure();
+ }
+
+ public <T extends Annotation> T getAnnotation(Class<T> targetClass)
+ {
+ return (T)this.annotations.get(targetClass);
+ }
+
+ public Set<Annotation> getAnnotations()
+ {
+ return this.annotationSet;
+ }
+
+ public boolean isAnnotationPresent(Class<? extends Annotation> targetClass)
+ {
+ return this.annotations.containsKey(targetClass);
+ }
+}
Added: myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/scope/mapped/MappedJsf2ScopeExtension.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/scope/mapped/MappedJsf2ScopeExtension.java?rev=1002705&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/scope/mapped/MappedJsf2ScopeExtension.java (added)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/scope/mapped/MappedJsf2ScopeExtension.java Wed Sep 29 16:05:29 2010
@@ -0,0 +1,153 @@
+/*
+ * 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.extensions.cdi.jsf2.impl.scope.mapped;
+
+import org.apache.myfaces.extensions.cdi.core.api.Deactivatable;
+import org.apache.myfaces.extensions.cdi.core.api.projectstage.ProjectStage;
+import org.apache.myfaces.extensions.cdi.core.api.util.ClassUtils;
+import org.apache.myfaces.extensions.cdi.core.impl.projectstage.ProjectStageProducer;
+
+import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.ProcessAnnotatedType;
+import javax.enterprise.inject.spi.AnnotatedType;
+import javax.enterprise.event.Observes;
+import javax.faces.bean.ManagedBean;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.logging.Logger;
+import java.lang.annotation.Annotation;
+
+/**
+ * @author Gerhard Petracek
+ */
+public class MappedJsf2ScopeExtension implements Extension, Deactivatable
+{
+ private final Logger logger = Logger.getLogger(MappedJsf2ScopeExtension.class.getName());
+
+ private Map<Class<? extends Annotation>, Class<? extends Annotation>> mappedJsfScopes
+ = new HashMap<Class<? extends Annotation>, Class<? extends Annotation>>();
+
+ public MappedJsf2ScopeExtension()
+ {
+ this.mappedJsfScopes.put(javax.faces.bean.ApplicationScoped.class,
+ javax.enterprise.context.ApplicationScoped.class);
+ this.mappedJsfScopes.put(javax.faces.bean.SessionScoped.class,
+ javax.enterprise.context.SessionScoped.class);
+ this.mappedJsfScopes.put(javax.faces.bean.RequestScoped.class,
+ javax.enterprise.context.RequestScoped.class);
+ }
+
+ protected void convertJsf2Scopes(@Observes ProcessAnnotatedType processAnnotatedType)
+ {
+ if(!isActivated())
+ {
+ return;
+ }
+
+ Class<? extends Annotation> jsf2ScopeAnnotation = getJsf2ScopeAnnotation(processAnnotatedType);
+
+ if(jsf2ScopeAnnotation != null && !beanUsesUnsupportedManagedBeanAnnotation(processAnnotatedType))
+ {
+ //noinspection unchecked
+ processAnnotatedType.setAnnotatedType(
+ convertBean(processAnnotatedType.getAnnotatedType(), jsf2ScopeAnnotation));
+ }
+ }
+
+ private AnnotatedType convertBean(AnnotatedType annotatedType, Class<? extends Annotation> jsf2ScopeAnnotation)
+ {
+ logConvertedBean(annotatedType, jsf2ScopeAnnotation);
+
+ return new Jsf2BeanWrapper(annotatedType, this.mappedJsfScopes.get(jsf2ScopeAnnotation), jsf2ScopeAnnotation);
+ }
+
+ private Class<? extends Annotation> getJsf2ScopeAnnotation(ProcessAnnotatedType processAnnotatedType)
+ {
+ for(Class<? extends Annotation> currentJsfScope : this.mappedJsfScopes.keySet())
+ {
+ if(processAnnotatedType.getAnnotatedType().getJavaClass().isAnnotationPresent(currentJsfScope))
+ {
+ return currentJsfScope;
+ }
+ }
+ return null;
+ }
+
+ private void logConvertedBean(AnnotatedType annotatedType, Class<? extends Annotation> jsf2ScopeAnnotation)
+ {
+ ProjectStage projectStage = getCurrentProjectStage();
+
+ if (projectStage == ProjectStage.Development)
+ {
+ logger.info("JSF2 bean was converted to a CDI bean. type: " + annotatedType.getJavaClass().getName() +
+ " original scope: " + jsf2ScopeAnnotation.getName());
+ }
+ }
+
+ private boolean beanUsesUnsupportedManagedBeanAnnotation(ProcessAnnotatedType processAnnotatedType)
+ {
+ Class<?> beanClass = processAnnotatedType.getAnnotatedType().getJavaClass();
+
+ if(!beanClass.isAnnotationPresent(ManagedBean.class))
+ {
+ return false;
+ }
+
+ ManagedBean managedBeanAnnotation = beanClass.getAnnotation(ManagedBean.class);
+
+ if(managedBeanAnnotation.eager())
+ {
+ ProjectStage projectStage = getCurrentProjectStage();
+
+ if (projectStage == ProjectStage.Development)
+ {
+ logger.warning("Bean of type " + beanClass.getName() + " uses @" + ManagedBean.class.getName() +
+ " in combination with #eager = true. That's not supported with CDI. " +
+ "The bean won't get transformed to a CDI bean.");
+ }
+ return true;
+ }
+ return false;
+ }
+
+ private ProjectStage getCurrentProjectStage()
+ {
+ ProjectStageProducer projectStageProducer = null;
+ try
+ {
+ projectStageProducer = ProjectStageProducer.getInstance();
+ }
+ catch (Exception e)
+ {
+ if (!(e instanceof RuntimeException))
+ {
+ throw new RuntimeException(e);
+ }
+ }
+ //noinspection ConstantConditions
+ projectStageProducer.determineProjectStage();
+ ProjectStage projectStage = projectStageProducer.getProjectStage();
+ return projectStage;
+ }
+
+ public boolean isActivated()
+ {
+ return ClassUtils.isClassActivated(getClass());
+ }
+}
Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/META-INF/faces-config.xml
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/META-INF/faces-config.xml?rev=1002705&r1=1002704&r2=1002705&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/META-INF/faces-config.xml (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/META-INF/faces-config.xml Wed Sep 29 16:05:29 2010
@@ -32,7 +32,7 @@
<system-event-listener>
<system-event-class>javax.faces.event.PostConstructApplicationEvent</system-event-class>
- <system-event-listener-class>org.apache.myfaces.extensions.cdi.jsf2.impl.listener.system.CodiStartupListener</system-event-listener-class>
+ <system-event-listener-class>org.apache.myfaces.extensions.cdi.jsf2.impl.listener.system.CodiJsf2SystemEventListener</system-event-listener-class>
</system-event-listener>
</application>
Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension?rev=1002705&r1=1002704&r2=1002705&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension Wed Sep 29 16:05:29 2010
@@ -22,3 +22,5 @@ org.apache.myfaces.extensions.cdi.jsf2.i
# we have to activate this extension in any case!
org.apache.myfaces.extensions.cdi.jsf2.impl.Jsf12AwareFilterExtension
+
+org.apache.myfaces.extensions.cdi.jsf2.impl.scope.mapped.MappedJsf2ScopeExtension