Skip to content

Commit

Permalink
WELD-1355 Interceptor and decorators are applied globally (not only w…
Browse files Browse the repository at this point in the history
…ithin accessible closure)
  • Loading branch information
jharting committed Mar 10, 2013
1 parent af8f629 commit aecedf7
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 6 deletions.
37 changes: 31 additions & 6 deletions impl/src/main/java/org/jboss/weld/manager/BeanManagerImpl.java
Expand Up @@ -256,6 +256,12 @@ public class BeanManagerImpl implements WeldManager, Serializable {
*/
private final transient Set<Bean<?>> beanSet = Collections.synchronizedSet(new HashSet<Bean<?>>());

/*
* Data structure representing all managers *accessible* from this bean
* deployment archive activity
*/
private final transient Set<BeanManagerImpl> managers;

/*
* These data structures represent the managers *accessible* from this bean
* deployment archive activity
Expand Down Expand Up @@ -302,7 +308,8 @@ public static BeanManagerImpl newRootManager(String id, ServiceRegistry serviceR
new CopyOnWriteArraySet<CurrentActivity>(),
ModuleEnablement.EMPTY_ENABLEMENT,
id,
new AtomicInteger());
new AtomicInteger(),
new HashSet<BeanManagerImpl>());
}

public static BeanManagerImpl newManager(BeanManagerImpl rootManager, String id, ServiceRegistry services) {
Expand All @@ -320,7 +327,8 @@ public static BeanManagerImpl newManager(BeanManagerImpl rootManager, String id,
new CopyOnWriteArraySet<CurrentActivity>(),
ModuleEnablement.EMPTY_ENABLEMENT,
id,
new AtomicInteger());
new AtomicInteger(),
rootManager.managers);
}

/**
Expand Down Expand Up @@ -354,7 +362,8 @@ public static BeanManagerImpl newChildActivityManager(BeanManagerImpl parentMana
parentManager.getCurrentActivities(),
parentManager.getEnabled(),
new StringBuilder().append(parentManager.getChildIds().incrementAndGet()).toString(),
parentManager.getChildIds());
parentManager.getChildIds(),
parentManager.managers);
}

private BeanManagerImpl(
Expand All @@ -371,7 +380,8 @@ private BeanManagerImpl(
Set<CurrentActivity> currentActivities,
ModuleEnablement enabled,
String id,
AtomicInteger childIds) {
AtomicInteger childIds,
Set<BeanManagerImpl> managers) {
this.services = serviceRegistry;
this.beans = beans;
this.transitiveBeans = transitiveBeans;
Expand All @@ -386,15 +396,18 @@ private BeanManagerImpl(
this.namespaces = namespaces;
this.id = id;
this.childIds = new AtomicInteger();
this.managers = managers;

managers.add(this);

// Set up the structure to store accessible managers in
this.accessibleManagers = new HashSet<BeanManagerImpl>();

// TODO Currently we build the accessible bean list on the fly, we need to set it in stone once bootstrap is finished...
Transform<Bean<?>> beanTransform = new BeanTransform(this);
this.beanResolver = new TypeSafeBeanResolver(this, createDynamicAccessibleIterable(beanTransform));
this.decoratorResolver = new TypeSafeDecoratorResolver(this, createDynamicAccessibleIterable(DecoratorTransform.INSTANCE));
this.interceptorResolver = new TypeSafeInterceptorResolver(this, createDynamicAccessibleIterable(InterceptorTransform.INSTANCE));
this.decoratorResolver = new TypeSafeDecoratorResolver(this, createDynamicGlobalIterable(DecoratorTransform.INSTANCE));
this.interceptorResolver = new TypeSafeInterceptorResolver(this, createDynamicGlobalIterable(InterceptorTransform.INSTANCE));
this.nameBasedResolver = new NameBasedResolver(this, createDynamicAccessibleIterable(beanTransform));
this.weldELResolver = new WeldELResolver(this);
this.childActivities = new CopyOnWriteArraySet<BeanManagerImpl>();
Expand All @@ -408,6 +421,17 @@ private BeanManagerImpl(
this.containerLifecycleEvents = serviceRegistry.get(ContainerLifecycleEvents.class);
}

private <T> Iterable<T> createDynamicGlobalIterable(final Transform<T> transform) {
return new Iterable<T>() {
public Iterator<T> iterator() {
Set<Iterable<T>> result = new HashSet<Iterable<T>>();
for (BeanManagerImpl manager : managers) {
result.add(transform.transform(manager));
}
return Iterators.concat(Iterators.transform(result.iterator(), IterableToIteratorFunction.<T>instance()));
}
};
}

private <T> Iterable<T> createDynamicAccessibleIterable(final Transform<T> transform) {
return new Iterable<T>() {
Expand Down Expand Up @@ -1142,6 +1166,7 @@ public <T> SessionBean<T> getBean(EjbDescriptor<T> descriptor) {
public void cleanup() {
services.cleanup();
this.accessibleManagers.clear();
this.managers.clear();
this.beanResolver.clear();
this.beans.clear();
this.childActivities.clear();
Expand Down
@@ -0,0 +1,25 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.jboss.weld.tests.interceptors.global.accessibility;

@FooBinding
public class Foo {

public String ping() {
return "pong";
}
}
@@ -0,0 +1,36 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.jboss.weld.tests.interceptors.global.accessibility;

import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.interceptor.InterceptorBinding;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@InterceptorBinding
@Inherited
@Target({ TYPE, METHOD })
@Retention(RUNTIME)
public @interface FooBinding {

}
@@ -0,0 +1,34 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.jboss.weld.tests.interceptors.global.accessibility;

import javax.annotation.Priority;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

@Interceptor
@FooBinding
@Priority(Interceptor.Priority.APPLICATION + 100)
public class FooInterceptor {

@AroundInvoke
Object intercept(InvocationContext ctx) throws Exception {
ctx.proceed();
return "intercepted";
}
}
@@ -0,0 +1,48 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.jboss.weld.tests.interceptors.global.accessibility;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.BeanArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.weld.tests.category.Integration;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
@Category(Integration.class)
public class GlobalInterceptorAccessibilityTest {

@Deployment(testable = false)
public static Archive<?> getDeployment() {
WebArchive war = ShrinkWrap.create(WebArchive.class).addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml").addClasses(FooInterceptor.class, VerifyingListener.class);
JavaArchive jar = ShrinkWrap.create(BeanArchive.class).addClasses(Foo.class, FooBinding.class);
return ShrinkWrap.create(EnterpriseArchive.class).addAsModule(war).addAsLibrary(jar);
}

@Test
public void test() {
// noop, the deployment is verified within VerifyingListener
}
}
@@ -0,0 +1,40 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.jboss.weld.tests.interceptors.global.accessibility;

import javax.inject.Inject;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class VerifyingListener implements ServletContextListener {

@Inject
private Foo foo;

@Override
public void contextInitialized(ServletContextEvent sce) {
if (!"intercepted".equals(foo.ping())) {
throw new IllegalStateException();
}
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}

0 comments on commit aecedf7

Please sign in to comment.