Skip to content

Commit

Permalink
Merge branch 'stuart/master'
Browse files Browse the repository at this point in the history
Conflicts:
	impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericProduct.java
  • Loading branch information
pmuir committed Jul 26, 2010
2 parents f1c08ef + 010f4e2 commit efa323b
Show file tree
Hide file tree
Showing 21 changed files with 721 additions and 69 deletions.
@@ -1,19 +1,3 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, 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.extensions.bean.generic;

import static java.lang.annotation.ElementType.FIELD;
Expand All @@ -25,7 +9,6 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.enterprise.util.AnnotationLiteral;
import javax.inject.Qualifier;

/**
Expand All @@ -39,22 +22,13 @@
*
*/
@Retention(RUNTIME)
<<<<<<< HEAD
@Target({ METHOD, FIELD, PARAMETER, TYPE })
=======
@Target({ METHOD, FIELD, PARAMETER, TYPE})
>>>>>>> stuart/master
@Qualifier
public @interface GenericProduct
{

public static final GenericProduct INSTANCE = new GenericProductLiteral();

class GenericProductLiteral extends AnnotationLiteral<GenericProduct> implements GenericProduct
{

private static final long serialVersionUID = 2768505716290514234L;



private GenericProductLiteral() {}

}

}
@@ -0,0 +1,15 @@
package org.jboss.weld.extensions.bean.generic;

import javax.enterprise.util.AnnotationLiteral;


public class GenericProductLiteral extends AnnotationLiteral<GenericProduct> implements GenericProduct
{

public static final GenericProduct INSTANCE = new GenericProductLiteral();

private static final long serialVersionUID = 2768505716290514234L;

public GenericProductLiteral() {}

}
Expand Up @@ -39,6 +39,8 @@
import org.jboss.weld.extensions.annotated.AnnotationRedefiner;
import org.jboss.weld.extensions.annotated.RedefinitionContext;
import org.jboss.weld.extensions.bean.BeanBuilder;
import org.jboss.weld.extensions.literal.InjectLiteral;
import org.jboss.weld.extensions.literal.NamedLiteral;

/**
* Extension to install the "core" extensions. Core extensions are those that
Expand Down
@@ -0,0 +1,69 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, 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.extensions.servicehandler;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* <p>
* Meta annotation that is used to specify an invocation handler for an
* automatically implmented bean.
* </p>
*
* <p>
* If the annotation that is annotated with this meta-annotation is applied to
* an interface or abstract class then the container will automatically provide
* a concrete implementation of the class/interface, and delegate all calls to
* abstract methods through the handler class specified by this annotations.
* </p>
*
* <p>
* The handler class must have a method with the following signature:
* </p>
*
* <pre>
* @AroundInvoke public Object aroundInvoke(final InvocationContext invocation) throws Exception
* </pre>
*
* <p>
* This is the same as an intercepter class. This handler can be injected into
* and use initializer methods, however @PreDestory methods are not available
* </p>
*
* <p>
* The annotation should have:
* </p>
*
* <pre>
* @Retention(RUNTIME)
* @Target({ TYPE })
* </pre>
*
* @author Stuart Douglas <stuart.w.douglas@gmail.com>
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
@Documented
public @interface ServiceHandler
{
Class<?> value();
}
@@ -0,0 +1,98 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, 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.extensions.servicehandler;

import java.lang.reflect.Method;

import javassist.util.proxy.MethodFilter;
import javassist.util.proxy.ProxyFactory;
import javassist.util.proxy.ProxyObject;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;

import org.jboss.weld.extensions.bean.BeanLifecycle;

/**
* Bean lifecycle for ServiceHandler beans
*
* @author Stuart Douglas
*
* @param <T>
* @param <H>
*/
public class ServiceHandlerBeanLifecycle<T, H> implements BeanLifecycle<T>
{
private final ProxyFactory factory;
private final Class<? extends T> proxyClass;
private final ServiceHandlerManager<H> handler;


public ServiceHandlerBeanLifecycle(Class<? extends T> classToImplement, Class<H> handlerClass, BeanManager manager)
{
handler = new ServiceHandlerManager<H>(handlerClass, manager);

// create the proxy
factory = new ProxyFactory();
if (classToImplement.isInterface())
{
Class<?>[] interfaces = new Class[1];
interfaces[0] = classToImplement;
factory.setInterfaces(interfaces);
}
else
{
factory.setSuperclass(classToImplement);
}
factory.setFilter(new MethodFilter()
{
public boolean isHandled(Method m)
{
// ignore finalize()
return !m.getName().equals("finalize");
}
});

this.proxyClass = ((Class<?>) factory.createClass()).asSubclass(classToImplement);
}

public T create(Bean<T> bean, CreationalContext<T> creationalContext)
{
try
{
// Make sure to pass the creational context along, allowing dependents to be cleaned up properly
@SuppressWarnings("unchecked")
H handlerInstance = handler.create((CreationalContext) creationalContext);

ServiceHandlerMethodHandler<T, H> methodHandler = new ServiceHandlerMethodHandler<T, H>(handler, handlerInstance);
T instance = proxyClass.newInstance();
((ProxyObject) instance).setHandler(methodHandler);
return instance;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}

public void destroy(Bean<T> bean, T instance, CreationalContext<T> creationalContext)
{
// handler.dispose(instance);
}

}
@@ -0,0 +1,70 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, 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.extensions.servicehandler;

import static org.jboss.weld.extensions.util.AnnotationInspector.getMetaAnnotation;

import java.util.HashSet;
import java.util.Set;

import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;

import org.jboss.weld.extensions.bean.BeanBuilder;

/**
* This extension automatically implements interfaces and abstract classes.
*
* @author Stuart Douglas
*
*/
class ServiceHandlerExtension implements Extension
{
Set<Bean<?>> beans = new HashSet<Bean<?>>();

<X> void processAnnotatedType(@Observes ProcessAnnotatedType<X> event, BeanManager beanManager)
{
ServiceHandler annotation = getMetaAnnotation(event.getAnnotatedType(), ServiceHandler.class);
if (annotation != null)
{
Class<?> handlerClass = annotation.value();
try
{
BeanBuilder<X> builder = new BeanBuilder<X>(beanManager);
builder.defineBeanFromAnnotatedType(event.getAnnotatedType());
builder.setBeanLifecycle(new ServiceHandlerBeanLifecycle(event.getAnnotatedType().getJavaClass(), handlerClass, beanManager));
beans.add(builder.create());
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
}
}

void afterBeanDiscovery(@Observes AfterBeanDiscovery event)
{
for (Bean<?> bean : beans)
{
event.addBean(bean);
}
}
}

0 comments on commit efa323b

Please sign in to comment.