Skip to content

Commit

Permalink
Add JMS lifecycle
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@2421 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Apr 15, 2009
1 parent e0ccc2b commit f783c33
Show file tree
Hide file tree
Showing 21 changed files with 769 additions and 61 deletions.
Expand Up @@ -16,6 +16,7 @@
*/
package org.jboss.webbeans.bean.ee;

import java.io.Serializable;
import java.lang.reflect.Method;

import javassist.util.proxy.MethodHandler;
Expand All @@ -30,9 +31,11 @@
* @author Pete Muir
*
*/
public abstract class AbstractJavaEEResourceMethodHandler implements MethodHandler
public abstract class AbstractJavaEEResourceMethodHandler implements MethodHandler, Serializable
{

private static final long serialVersionUID = -3171683636451762591L;

private static final transient Log log = Logging.getLog(AbstractJavaEEResourceMethodHandler.class);

/**
Expand Down Expand Up @@ -63,12 +66,12 @@ public AbstractJavaEEResourceMethodHandler()
*/
public Object invoke(Object self, Method method, Method proceed, Object[] args) throws Throwable
{
Object proxiedInstance = getProxiedInstance();
Object proxiedInstance = getProxiedInstance(null);
Object returnValue = Reflections.invokeAndWrap(method, proxiedInstance, args);
log.trace("Executed {0} on {1} with parameters {2} and got return value {3}", method, proxiedInstance, args, returnValue);
return returnValue;
}

protected abstract Object getProxiedInstance();
protected abstract Object getProxiedInstance(Class<?> declaringClass);

}
Expand Up @@ -23,6 +23,8 @@
public abstract class AbstractResourceMethodHandler extends AbstractJavaEEResourceMethodHandler
{

private static final long serialVersionUID = 8977780996027839558L;

private final String mappedName;
private final String jndiName;

Expand Down

This file was deleted.

Expand Up @@ -27,6 +27,8 @@
*/
public class PersistenceContextMethodHandler extends AbstractJavaEEResourceMethodHandler
{

private static final long serialVersionUID = 6111824732958101382L;

private final String unitName;

Expand All @@ -36,7 +38,7 @@ public PersistenceContextMethodHandler(String unitName)
}

@Override
protected Object getProxiedInstance()
protected Object getProxiedInstance(Class<?> declaringClass)
{
return CurrentManager.rootManager().getServices().get(JpaServices.class).resolvePersistenceContext(unitName);
}
Expand Down
Expand Up @@ -29,6 +29,8 @@
public class PersistenceUnitMethodHandler extends AbstractJavaEEResourceMethodHandler
{

private static final long serialVersionUID = -7936320009903622051L;

private final String unitName;

public PersistenceUnitMethodHandler(String unitName)
Expand All @@ -37,7 +39,7 @@ public PersistenceUnitMethodHandler(String unitName)
}

@Override
protected Object getProxiedInstance()
protected Object getProxiedInstance(Class<?> declaringClass)
{
return CurrentManager.rootManager().getServices().get(JpaServices.class).resolvePersistenceUnit(unitName);
}
Expand Down
Expand Up @@ -28,6 +28,8 @@
*/
public class RemoteEjbMethodHandler extends AbstractResourceMethodHandler
{

private static final long serialVersionUID = 8192691377739747596L;

private final String ejbLink;

Expand All @@ -38,7 +40,7 @@ public RemoteEjbMethodHandler(String jndiName, String mappedName, String ejbLink
}

@Override
protected Object getProxiedInstance()
protected Object getProxiedInstance(Class<?> declaringClass)
{
return CurrentManager.rootManager().getServices().get(EjbServices.class).resolveRemoteEjb(getJndiName(), getMappedName(), ejbLink);
}
Expand Down
Expand Up @@ -29,13 +29,15 @@
public class ResourceMethodHandler extends AbstractResourceMethodHandler
{

private static final long serialVersionUID = -8835529913294253208L;

public ResourceMethodHandler(String jndiName, String mappedName)
{
super(jndiName, mappedName);
}

@Override
protected Object getProxiedInstance()
protected Object getProxiedInstance(Class<?> declaringClass)
{
return CurrentManager.rootManager().getServices().get(ResourceServices.class).resolveResource(getJndiName(), getMappedName());
}
Expand Down
Expand Up @@ -29,13 +29,15 @@
public class WebServiceMethodHandler extends AbstractResourceMethodHandler
{

private static final long serialVersionUID = 6719454070840346045L;

public WebServiceMethodHandler(String jndiName, String mappedName)
{
super(jndiName, mappedName);
}

@Override
protected Object getProxiedInstance()
protected Object getProxiedInstance(Class<?> declaringClass)
{
return CurrentManager.rootManager().getServices().get(WebServices.class).resolveResource(getJndiName(), getMappedName());
}
Expand Down
@@ -0,0 +1,64 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, 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.webbeans.bean.ee.jms;

import java.io.Serializable;

import javax.context.Contextual;
import javax.context.CreationalContext;
import javax.inject.ExecutionException;
import javax.jms.Connection;
import javax.jms.JMSException;

/**
* Contextual in which we can store the connection
*
* @author Pete Muir
*
*/
abstract class ConnectionContextual<T extends Connection> implements Contextual<T>, Serializable
{

private static final long serialVersionUID = -4333311257129016113L;

public T create(CreationalContext<T> creationalContext)
{
try
{
return createConnection();
}
catch (JMSException e)
{
throw new ExecutionException("Error creating connection ", e);
}
}

protected abstract T createConnection() throws JMSException;

public void destroy(T instance)
{
try
{
instance.close();
}
catch (JMSException e)
{
throw new ExecutionException("Error creating connection ", e);
}
}

}
@@ -0,0 +1,140 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, 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.webbeans.bean.ee.jms;



import java.lang.reflect.Method;

import javax.context.CreationalContext;
import javax.jms.Connection;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.jboss.webbeans.bean.ee.AbstractResourceMethodHandler;
import org.jboss.webbeans.context.ApplicationContext;
import org.jboss.webbeans.context.DependentContext;

/**
* JMS method handler that knows how to create a proxy
*
* @author Pete Muir
*
* @param <C> the JMS connection
* @param <S> the JMS session
* @param <MP> the JMS message producer
* @param <MC> the JMS message consumer
*/
abstract class JmsMethodHandler<C extends Connection, S extends Session, MP extends MessageProducer, MC extends MessageConsumer> extends AbstractResourceMethodHandler
{

private static final long serialVersionUID = -2598920314236475437L;

public JmsMethodHandler(String jndiName, String mappedName)
{
super(jndiName, mappedName);
}

@Override
public Object invoke(Object self, Method method, Method proceed, Object[] args) throws Throwable
{
if (method.getName().equals("close"))
{
throw new UnsupportedOperationException("Cannot call close on a Web Beans managed JMS resource");
}
return super.invoke(self, method, proceed, args);
}

@Override
protected Object getProxiedInstance(Class<?> declaringClass)
{
if (Connection.class.isAssignableFrom(declaringClass))
{
return getConnection(getConnectionContextual());
}
else if (Session.class.isAssignableFrom(declaringClass))
{
return getSession(getSessionContextual());
}
else if (MessageConsumer.class.isAssignableFrom(declaringClass))
{
return getQueueReceiver(getMessageConsumerContextual());
}
else if (MessageProducer.class.isAssignableFrom(declaringClass))
{
return getMessageProducer(getMessageProducerContextual());
}
else
{
throw new IllegalStateException("Cannot create proxy for " + declaringClass);
}
}

protected abstract ConnectionContextual<C> getConnectionContextual();

protected abstract SessionContextual<S> getSessionContextual();

protected abstract MessageConsumerContextual<MC> getMessageConsumerContextual();

protected abstract MessageProducerContextual<MP> getMessageProducerContextual();

protected abstract S createSessionFromConnection(ConnectionContextual<C> connectionContexual);

protected C getConnection(ConnectionContextual<C> connectionContexual)
{
return ApplicationContext.instance().get(connectionContexual, new CreationalContext<C>()
{

public void push(C incompleteInstance) {}

});
}

private S getSession(SessionContextual<S> sessionContextual)
{
return DependentContext.instance().get(sessionContextual, new CreationalContext<S>()
{

public void push(S incompleteInstance) {}

});
}

private MP getMessageProducer(MessageProducerContextual<MP> messageProducerContextual)
{
return DependentContext.instance().get(messageProducerContextual, new CreationalContext<MP>()
{

public void push(MP incompleteInstance) {}

});
}

private MC getQueueReceiver(MessageConsumerContextual<MC> messageConsumerContextual)
{
return DependentContext.instance().get(messageConsumerContextual, new CreationalContext<MC>()
{

public void push(MC incompleteInstance) {}

});
}



}

0 comments on commit f783c33

Please sign in to comment.