Updated Branches: refs/heads/master 21a309781 -> 58f6d5463
DELTASPIKE-426 switch back to the initial approach (to keep one instance per component) Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/58f6d546 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/58f6d546 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/58f6d546 Branch: refs/heads/master Commit: 58f6d546310a78f3445e35d6b97b584ee6c7395b Parents: 21a3097 Author: gpetracek <gpetra...@apache.org> Authored: Wed Oct 16 10:24:41 2013 +0200 Committer: gpetracek <gpetra...@apache.org> Committed: Wed Oct 16 10:29:33 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, 208 insertions(+), 157 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/58f6d546/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 deleted file mode 100644 index fadd0dc..0000000 --- a/deltaspike/modules/jsf/api/src/main/java/org/apache/deltaspike/jsf/api/literal/ViewScopedLiteral.java +++ /dev/null @@ -1,27 +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.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/58f6d546/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 new file mode 100644 index 0000000..4f1c79d --- /dev/null +++ b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/AbstractBeanStorage.java @@ -0,0 +1,54 @@ +/* + * 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/58f6d546/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 deleted file mode 100644 index 4d93cc1..0000000 --- a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ConverterAndValidatorExtension.java +++ /dev/null @@ -1,122 +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 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/58f6d546/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 1dcafa6..0a362df 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,13 +18,11 @@ */ 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/58f6d546/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 new file mode 100644 index 0000000..5a23e30 --- /dev/null +++ b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/DependentBeanEntry.java @@ -0,0 +1,54 @@ +/* + * 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/58f6d546/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 8e9443f..417b724 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,11 +18,18 @@ */ 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 @@ -44,7 +51,7 @@ public abstract class ManagedArtifactResolver return null; } - return BeanProvider.getContextualReference(converterClass); + return getContextualReference(BeanManagerProvider.getInstance().getBeanManager(), converterClass); } public static Validator resolveManagedValidator(Class<? extends Validator> validatorClass) @@ -54,6 +61,42 @@ public abstract class ManagedArtifactResolver return null; } - return BeanProvider.getContextualReference(validatorClass); + 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; } } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/58f6d546/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 new file mode 100644 index 0000000..93a7299 --- /dev/null +++ b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/RequestDependentBeanStorage.java @@ -0,0 +1,26 @@ +/* + * 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/58f6d546/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 8e0b335..f306f8e 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,13 +18,11 @@ */ 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/58f6d546/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 new file mode 100644 index 0000000..1a62f40 --- /dev/null +++ b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/injection/ViewDependentBeanStorage.java @@ -0,0 +1,28 @@ +/* + * 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/58f6d546/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 6b9f3b2..a634c99 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,5 +20,4 @@ org.apache.deltaspike.jsf.impl.scope.view.ViewScopedExtension org.apache.deltaspike.jsf.impl.scope.mapped.MappedJsf2ScopeExtension -org.apache.deltaspike.jsf.impl.config.view.ViewConfigExtension -org.apache.deltaspike.jsf.impl.injection.ConverterAndValidatorExtension \ No newline at end of file +org.apache.deltaspike.jsf.impl.config.view.ViewConfigExtension \ No newline at end of file