Updated Branches: refs/heads/master 38b99026b -> 21a309781
DELTASPIKE-426 scope upgrade via extension Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/21a30978 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/21a30978 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/21a30978 Branch: refs/heads/master Commit: 21a3097818f0f9d6a11390dd4f370d04e688106d Parents: 38b9902 Author: gpetracek <gpetra...@apache.org> Authored: Tue Oct 15 23:03:32 2013 +0200 Committer: gpetracek <gpetra...@apache.org> Committed: Tue Oct 15 23:34:34 2013 +0200 ---------------------------------------------------------------------- .../jsf/api/literal/ViewScopedLiteral.java | 27 ++++ .../jsf/impl/injection/AbstractBeanStorage.java | 54 -------- .../ConverterAndValidatorExtension.java | 122 +++++++++++++++++++ .../jsf/impl/injection/ConverterWrapper.java | 2 + .../jsf/impl/injection/DependentBeanEntry.java | 54 -------- .../impl/injection/ManagedArtifactResolver.java | 47 +------ .../injection/RequestDependentBeanStorage.java | 26 ---- .../jsf/impl/injection/ValidatorWrapper.java | 2 + .../injection/ViewDependentBeanStorage.java | 28 ----- .../javax.enterprise.inject.spi.Extension | 3 +- 10 files changed, 157 insertions(+), 208 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/21a30978/deltaspike/modules/jsf/api/src/main/java/org/apache/deltaspike/jsf/api/literal/ViewScopedLiteral.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/api/src/main/java/org/apache/deltaspike/jsf/api/literal/ViewScopedLiteral.java b/deltaspike/modules/jsf/api/src/main/java/org/apache/deltaspike/jsf/api/literal/ViewScopedLiteral.java new file mode 100644 index 0000000..fadd0dc --- /dev/null +++ b/deltaspike/modules/jsf/api/src/main/java/org/apache/deltaspike/jsf/api/literal/ViewScopedLiteral.java @@ -0,0 +1,27 @@ +/* + * 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.api.literal; + +import javax.enterprise.util.AnnotationLiteral; +import javax.faces.bean.ViewScoped; + +public class ViewScopedLiteral extends AnnotationLiteral<ViewScoped> implements ViewScoped +{ + private static final long serialVersionUID = -6260027440277811767L; +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/21a30978/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/AbstractBeanStorage.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/AbstractBeanStorage.java b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/AbstractBeanStorage.java deleted file mode 100644 index 4f1c79d..0000000 --- a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/AbstractBeanStorage.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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.injection; - -import javax.annotation.PreDestroy; -import java.util.ArrayList; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; - -abstract class AbstractBeanStorage -{ - private static final Logger LOG = Logger.getLogger(AbstractBeanStorage.class.getName()); - - private List<DependentBeanEntry> dependentBeanEntries = new ArrayList<DependentBeanEntry>(); - - void add(DependentBeanEntry dependentBeanEntry) - { - this.dependentBeanEntries.add(dependentBeanEntry); - } - - @PreDestroy - public void cleanup() - { - for (DependentBeanEntry beanEntry : this.dependentBeanEntries) - { - try - { - beanEntry.getBean().destroy(beanEntry.getInstance(), beanEntry.getCreationalContext()); - } - catch (RuntimeException e) - { - LOG.log(Level.SEVERE, e.getMessage(), e); - } - } - this.dependentBeanEntries.clear(); - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/21a30978/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ConverterAndValidatorExtension.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ConverterAndValidatorExtension.java b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ConverterAndValidatorExtension.java new file mode 100644 index 0000000..4d93cc1 --- /dev/null +++ b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ConverterAndValidatorExtension.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.injection; + +import org.apache.deltaspike.core.api.literal.RequestScopedLiteral; +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 org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder; +import org.apache.deltaspike.jsf.api.literal.ViewScopedLiteral; + +import javax.enterprise.context.Dependent; +import javax.enterprise.context.NormalScope; +import javax.enterprise.context.RequestScoped; +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.ViewScoped; +import javax.faces.convert.Converter; +import javax.faces.validator.Validator; +import javax.inject.Scope; +import java.io.Serializable; +import java.lang.annotation.Annotation; +import java.util.Set; +import java.util.logging.Logger; + +/** + * Dependent scoped converters and validators need to have a restricted lifetime. + * With upgrading them automatically, no manual handling is needed. + */ +public class ConverterAndValidatorExtension implements Extension, Deactivatable +{ + private boolean isActivated = true; + + private final Logger logger = Logger.getLogger(ConverterAndValidatorExtension.class.getName()); + + protected void init(@Observes BeforeBeanDiscovery beforeBeanDiscovery) + { + this.isActivated = ClassDeactivationUtils.isActivated(getClass()); + } + + protected void upgradeDependentScopedConvertersAndValidators(@Observes ProcessAnnotatedType processAnnotatedType) + { + if (!isActivated) + { + return; + } + + Class beanClass = processAnnotatedType.getAnnotatedType().getJavaClass(); + + if ((Converter.class.isAssignableFrom(beanClass) || Validator.class.isAssignableFrom(beanClass)) && + isDependentScoped(processAnnotatedType.getAnnotatedType().getAnnotations())) + { + processAnnotatedType.setAnnotatedType(convertBean(processAnnotatedType.getAnnotatedType())); + } + } + + private boolean isDependentScoped(Set<Annotation> annotations) + { + for (Annotation annotation : annotations) + { + //TODO discuss support of jsf scope-annotations (since they get mapped to cdi annotations automatically) + if (annotation.annotationType().isAnnotationPresent(Scope.class) || + annotation.annotationType().isAnnotationPresent(NormalScope.class)) + { + return Dependent.class.equals(annotation.annotationType()); + } + } + + //no scope annotation found -> dependent-scoped per convention + return true; + } + + private AnnotatedType convertBean(AnnotatedType annotatedType) + { + AnnotatedTypeBuilder annotatedTypeBuilder = new AnnotatedTypeBuilder() + .readFromType(annotatedType) + .removeFromClass(Dependent.class); + + if (Serializable.class.isAssignableFrom(annotatedType.getJavaClass())) + { + logConvertedBean(annotatedType, ViewScoped.class); + annotatedTypeBuilder.addToClass(new ViewScopedLiteral()); + } + else + { + logConvertedBean(annotatedType, RequestScoped.class); + annotatedTypeBuilder.addToClass(new RequestScopedLiteral()); + } + + return annotatedTypeBuilder.create(); + } + + private void logConvertedBean(AnnotatedType annotatedType, Class<? extends Annotation> scope) + { + ProjectStage projectStage = ProjectStageProducer.getInstance().getProjectStage(); + + if (projectStage == ProjectStage.Development) + { + logger.info("scope of " + annotatedType.getJavaClass().getName() + " was upgraded to @" + scope.getName()); + } + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/21a30978/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ConverterWrapper.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ConverterWrapper.java b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ConverterWrapper.java index 0a362df..1dcafa6 100644 --- a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ConverterWrapper.java +++ b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ConverterWrapper.java @@ -18,11 +18,13 @@ */ package org.apache.deltaspike.jsf.impl.injection; +import javax.enterprise.inject.Typed; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.faces.convert.ConverterException; +@Typed() public class ConverterWrapper extends AbstractContextualReferenceWrapper<Converter> implements Converter { public ConverterWrapper(Converter wrapped) http://git-wip-us.apache.org/repos/asf/deltaspike/blob/21a30978/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/DependentBeanEntry.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/DependentBeanEntry.java b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/DependentBeanEntry.java deleted file mode 100644 index 5a23e30..0000000 --- a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/DependentBeanEntry.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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.injection; - -import javax.enterprise.context.spi.CreationalContext; -import javax.enterprise.inject.spi.Bean; -import java.io.Serializable; - -class DependentBeanEntry<T> implements Serializable -{ - private static final long serialVersionUID = 7148484695430831322L; - - private final T instance; - private final Bean<?> bean; - private final CreationalContext<T> creationalContext; - - DependentBeanEntry(T instance, Bean<?> bean, CreationalContext<T> creationalContext) - { - this.instance = instance; - this.bean = bean; - this.creationalContext = creationalContext; - } - - T getInstance() - { - return instance; - } - - Bean<?> getBean() - { - return bean; - } - - CreationalContext<T> getCreationalContext() - { - return creationalContext; - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/21a30978/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ManagedArtifactResolver.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ManagedArtifactResolver.java b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ManagedArtifactResolver.java index 417b724..8e9443f 100644 --- a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ManagedArtifactResolver.java +++ b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ManagedArtifactResolver.java @@ -18,18 +18,11 @@ */ package org.apache.deltaspike.jsf.impl.injection; -import org.apache.deltaspike.core.api.provider.BeanManagerProvider; import org.apache.deltaspike.core.api.provider.BeanProvider; -import javax.enterprise.context.Dependent; -import javax.enterprise.context.spi.CreationalContext; import javax.enterprise.inject.Typed; -import javax.enterprise.inject.spi.Bean; -import javax.enterprise.inject.spi.BeanManager; import javax.faces.convert.Converter; import javax.faces.validator.Validator; -import java.io.Serializable; -import java.util.Set; @Typed() public abstract class ManagedArtifactResolver @@ -51,7 +44,7 @@ public abstract class ManagedArtifactResolver return null; } - return getContextualReference(BeanManagerProvider.getInstance().getBeanManager(), converterClass); + return BeanProvider.getContextualReference(converterClass); } public static Validator resolveManagedValidator(Class<? extends Validator> validatorClass) @@ -61,42 +54,6 @@ public abstract class ManagedArtifactResolver return null; } - return getContextualReference(BeanManagerProvider.getInstance().getBeanManager(), validatorClass); - } - - private static <T> T getContextualReference(BeanManager beanManager, Class<T> type) - { - Set<Bean<?>> beans = beanManager.getBeans(type); - - if (beans == null || beans.isEmpty()) - { - return null; - } - - Bean<?> bean = beanManager.resolve(beans); - - CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean); - - @SuppressWarnings({ "unchecked", "UnnecessaryLocalVariable" }) - T result = (T) beanManager.getReference(bean, type, creationalContext); - - if (bean.getScope().equals(Dependent.class)) - { - AbstractBeanStorage beanStorage; - - if (Serializable.class.isAssignableFrom(bean.getBeanClass())) - { - beanStorage = BeanProvider.getContextualReference(ViewDependentBeanStorage.class); - } - else - { - beanStorage = BeanProvider.getContextualReference(RequestDependentBeanStorage.class); - } - - //noinspection unchecked - beanStorage.add(new DependentBeanEntry(result, bean, creationalContext)); - } - - return result; + return BeanProvider.getContextualReference(validatorClass); } } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/21a30978/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/RequestDependentBeanStorage.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/RequestDependentBeanStorage.java b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/RequestDependentBeanStorage.java deleted file mode 100644 index 93a7299..0000000 --- a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/RequestDependentBeanStorage.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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.injection; - -import javax.enterprise.context.RequestScoped; - -@RequestScoped -public class RequestDependentBeanStorage extends AbstractBeanStorage -{ -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/21a30978/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ValidatorWrapper.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ValidatorWrapper.java b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ValidatorWrapper.java index f306f8e..8e0b335 100644 --- a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ValidatorWrapper.java +++ b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ValidatorWrapper.java @@ -18,11 +18,13 @@ */ package org.apache.deltaspike.jsf.impl.injection; +import javax.enterprise.inject.Typed; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.validator.Validator; import javax.faces.validator.ValidatorException; +@Typed() public class ValidatorWrapper extends AbstractContextualReferenceWrapper<Validator> implements Validator { public ValidatorWrapper(Validator wrapped) http://git-wip-us.apache.org/repos/asf/deltaspike/blob/21a30978/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ViewDependentBeanStorage.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ViewDependentBeanStorage.java b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ViewDependentBeanStorage.java deleted file mode 100644 index 1a62f40..0000000 --- a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ViewDependentBeanStorage.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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.injection; - -import javax.faces.bean.ViewScoped; -import java.io.Serializable; - -@ViewScoped -public class ViewDependentBeanStorage extends AbstractBeanStorage implements Serializable -{ - private static final long serialVersionUID = 4500399024267716556L; -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/21a30978/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 a634c99..6b9f3b2 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 @@ -20,4 +20,5 @@ org.apache.deltaspike.jsf.impl.scope.view.ViewScopedExtension org.apache.deltaspike.jsf.impl.scope.mapped.MappedJsf2ScopeExtension -org.apache.deltaspike.jsf.impl.config.view.ViewConfigExtension \ No newline at end of file +org.apache.deltaspike.jsf.impl.config.view.ViewConfigExtension +org.apache.deltaspike.jsf.impl.injection.ConverterAndValidatorExtension \ No newline at end of file