You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by gp...@apache.org on 2012/10/07 15:12:16 UTC
git commit: DELTASPIKE-279 initial import of the jsf2 to cdi scope
mapping
Updated Branches:
refs/heads/master de286e000 -> 39c369bc9
DELTASPIKE-279 initial import of the jsf2 to cdi scope mapping
Project: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/commit/39c369bc
Tree: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/tree/39c369bc
Diff: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/diff/39c369bc
Branch: refs/heads/master
Commit: 39c369bc9b3e4bcd715779b82b9e301e1302622d
Parents: de286e0
Author: gpetracek <gp...@apache.org>
Authored: Sun Oct 7 14:33:12 2012 +0200
Committer: gpetracek <gp...@apache.org>
Committed: Sun Oct 7 15:12:03 2012 +0200
----------------------------------------------------------------------
.../jsf/impl/scope/mapped/Jsf2BeanWrapper.java | 116 ++++++++++++++
.../scope/mapped/MappedJsf2ScopeExtension.java | 122 +++++++++++++++
.../services/javax.enterprise.inject.spi.Extension | 2 +
3 files changed, 240 insertions(+), 0 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/39c369bc/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/mapped/Jsf2BeanWrapper.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/mapped/Jsf2BeanWrapper.java b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/mapped/Jsf2BeanWrapper.java
new file mode 100644
index 0000000..da3b61b
--- /dev/null
+++ b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/mapped/Jsf2BeanWrapper.java
@@ -0,0 +1,116 @@
+/*
+ * 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.deltaspike.jsf.impl.scope.mapped;
+
+import org.apache.deltaspike.core.util.metadata.AnnotationInstanceProvider;
+
+import javax.enterprise.inject.spi.AnnotatedType;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Helper used by {@link MappedJsf2ScopeExtension}
+ */
+//TODO refactor it
+class Jsf2BeanWrapper implements AnnotatedType<Object>
+{
+ private final AnnotatedType wrapped;
+ private Map<Class<? extends Annotation>, Annotation> annotations;
+ private Set<Annotation> annotationSet;
+
+ 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(jsf2ScopeAnnotation))
+ {
+ this.annotations.put(originalAnnotation.annotationType(), originalAnnotation);
+ }
+ }
+
+ this.annotations.put(cdiScopeAnnotation, AnnotationInstanceProvider.of(cdiScopeAnnotation));
+
+ this.annotationSet = new HashSet<Annotation>(this.annotations.size());
+ this.annotationSet.addAll(this.annotations.values());
+ }
+
+ @Override
+ public Class getJavaClass()
+ {
+ return wrapped.getJavaClass();
+ }
+
+ @Override
+ public Set getConstructors()
+ {
+ return wrapped.getConstructors();
+ }
+
+ @Override
+ public Set getMethods()
+ {
+ return wrapped.getMethods();
+ }
+
+ @Override
+ public Set getFields()
+ {
+ return wrapped.getFields();
+ }
+
+ @Override
+ public Type getBaseType()
+ {
+ return wrapped.getBaseType();
+ }
+
+ @Override
+ public Set<Type> getTypeClosure()
+ {
+ return wrapped.getTypeClosure();
+ }
+
+ @Override
+ public <T extends Annotation> T getAnnotation(Class<T> targetClass)
+ {
+ return (T) this.annotations.get(targetClass);
+ }
+
+ @Override
+ public Set<Annotation> getAnnotations()
+ {
+ return this.annotationSet;
+ }
+
+ @Override
+ public boolean isAnnotationPresent(Class<? extends Annotation> targetClass)
+ {
+ return this.annotations.containsKey(targetClass);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/39c369bc/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/mapped/MappedJsf2ScopeExtension.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/mapped/MappedJsf2ScopeExtension.java b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/mapped/MappedJsf2ScopeExtension.java
new file mode 100644
index 0000000..960eaa5
--- /dev/null
+++ b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/scope/mapped/MappedJsf2ScopeExtension.java
@@ -0,0 +1,122 @@
+/*
+ * 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.deltaspike.jsf.impl.scope.mapped;
+
+import org.apache.deltaspike.core.api.projectstage.ProjectStage;
+import org.apache.deltaspike.core.spi.activation.Deactivatable;
+import org.apache.deltaspike.core.util.ClassDeactivationUtils;
+import org.apache.deltaspike.core.util.ProjectStageProducer;
+
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.AnnotatedType;
+import javax.enterprise.inject.spi.BeforeBeanDiscovery;
+import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.ProcessAnnotatedType;
+import javax.faces.bean.ManagedBean;
+import java.lang.annotation.Annotation;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.logging.Logger;
+
+/**
+ * Maps JSF2 scopes to CDI scopes
+ */
+public class MappedJsf2ScopeExtension implements Extension, Deactivatable
+{
+ private boolean isActivated = true;
+
+ 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>>();
+
+ /**
+ * Default constructor which initializes the scope mapping
+ */
+ 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 init(@Observes BeforeBeanDiscovery beforeBeanDiscovery)
+ {
+ this.isActivated = ClassDeactivationUtils.isActivated(getClass());
+ }
+
+ protected void convertJsf2Scopes(@Observes ProcessAnnotatedType processAnnotatedType)
+ {
+ if (!isActivated)
+ {
+ return;
+ }
+
+ //TODO
+ //CodiStartupBroadcaster.broadcastStartup();
+
+ Class<? extends Annotation> jsf2ScopeAnnotation = getJsf2ScopeAnnotation(processAnnotatedType);
+
+ if (jsf2ScopeAnnotation != null && !isBeanWithManagedBeanAnnotation(processAnnotatedType))
+ {
+ processAnnotatedType.setAnnotatedType(
+ convertBean(processAnnotatedType.getAnnotatedType(), 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 boolean isBeanWithManagedBeanAnnotation(ProcessAnnotatedType processAnnotatedType)
+ {
+ Class<?> beanClass = processAnnotatedType.getAnnotatedType().getJavaClass();
+
+ return beanClass.isAnnotationPresent(ManagedBean.class);
+ }
+
+ private AnnotatedType convertBean(AnnotatedType annotatedType, Class<? extends Annotation> jsf2ScopeAnnotation)
+ {
+ logConvertedBean(annotatedType, jsf2ScopeAnnotation);
+
+ return new Jsf2BeanWrapper(annotatedType, this.mappedJsfScopes.get(jsf2ScopeAnnotation), jsf2ScopeAnnotation);
+ }
+
+ private void logConvertedBean(AnnotatedType annotatedType, Class<? extends Annotation> jsf2ScopeAnnotation)
+ {
+ ProjectStage projectStage = ProjectStageProducer.getInstance().getProjectStage();
+
+ if (projectStage == ProjectStage.Development)
+ {
+ logger.info("JSF2 bean was converted to a CDI bean. Type: " + annotatedType.getJavaClass().getName() +
+ " original scope: " + jsf2ScopeAnnotation.getName());
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/39c369bc/deltaspike/modules/jsf/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jsf/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension b/deltaspike/modules/jsf/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
index d6aeefb..34ad3a2 100644
--- a/deltaspike/modules/jsf/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
+++ b/deltaspike/modules/jsf/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
@@ -18,3 +18,5 @@
#####################################################################################
org.apache.deltaspike.jsf.impl.scope.view.ViewScopedExtension
+
+org.apache.deltaspike.jsf.impl.scope.mapped.MappedJsf2ScopeExtension
\ No newline at end of file