001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.jms.client.container;
023:
024: import java.lang.reflect.Proxy;
025:
026: import javax.jms.JMSException;
027:
028: import org.jboss.aop.advice.Interceptor;
029: import org.jboss.aop.metadata.SimpleMetaData;
030: import org.jboss.jms.client.BrowserDelegate;
031: import org.jboss.jms.client.ConnectionDelegate;
032: import org.jboss.jms.client.ConsumerDelegate;
033: import org.jboss.jms.client.ImplementationDelegate;
034: import org.jboss.jms.client.ProducerDelegate;
035: import org.jboss.jms.client.SessionDelegate;
036: import org.jboss.jms.container.Container;
037: import org.jboss.jms.container.ContainerObjectOverridesInterceptor;
038: import org.jboss.jms.container.DispatchInterceptor;
039: import org.jboss.jms.container.ForwardInterceptor;
040: import org.jboss.jms.container.LogInterceptor;
041:
042: /**
043: * The in jvm implementation
044: *
045: * @author <a href="mailto:adrian@jboss.org>Adrian Brock</a>
046: * @version $Revision: 57195 $
047: */
048: public class ClientContainerFactory {
049: // Constants -----------------------------------------------------
050:
051: // Attributes ----------------------------------------------------
052:
053: // Static --------------------------------------------------------
054:
055: public static ConnectionDelegate getConnectionContainer(
056: ImplementationDelegate delegate, ConnectionDelegate server,
057: Interceptor[] interceptors, SimpleMetaData metadata)
058: throws JMSException {
059: return (ConnectionDelegate) getContainer(
060: ConnectionDelegate.class, null, server, interceptors,
061: metadata);
062: }
063:
064: public static SessionDelegate getSessionContainer(Container parent,
065: SessionDelegate server, Interceptor[] interceptors,
066: SimpleMetaData metadata) throws JMSException {
067: return (SessionDelegate) getContainer(SessionDelegate.class,
068: parent, server, interceptors, metadata);
069: }
070:
071: public static BrowserDelegate getBrowserContainer(Container parent,
072: BrowserDelegate server, Interceptor[] interceptors,
073: SimpleMetaData metadata) throws JMSException {
074: return (BrowserDelegate) getContainer(BrowserDelegate.class,
075: parent, server, interceptors, metadata);
076: }
077:
078: public static ConsumerDelegate getConsumerContainer(
079: Container parent, ConsumerDelegate server,
080: Interceptor[] interceptors, SimpleMetaData metadata)
081: throws JMSException {
082: return (ConsumerDelegate) getContainer(ConsumerDelegate.class,
083: parent, server, interceptors, metadata);
084: }
085:
086: public static ProducerDelegate getProducerContainer(
087: Container parent, ProducerDelegate server,
088: Interceptor[] interceptors, SimpleMetaData metadata)
089: throws JMSException {
090: return (ProducerDelegate) getContainer(ProducerDelegate.class,
091: parent, server, interceptors, metadata);
092: }
093:
094: public static Object getContainer(Class clazz, Container parent,
095: Object delegate, Interceptor[] interceptors,
096: SimpleMetaData metadata) throws JMSException {
097: Interceptor[] standard = getStandard();
098:
099: int stackSize = standard.length + interceptors.length + 1;
100: Interceptor[] stack = new Interceptor[stackSize];
101: System.arraycopy(standard, 0, stack, 0, standard.length);
102: System.arraycopy(interceptors, 0, stack, standard.length,
103: interceptors.length);
104:
105: if (delegate instanceof Proxy) {
106: try {
107: stack[stackSize - 1] = new ForwardInterceptor(Container
108: .getContainer(delegate));
109: } catch (Throwable ignored) {
110: }
111: }
112: if (stack[stackSize - 1] == null)
113: stack[stackSize - 1] = new DispatchInterceptor(delegate);
114:
115: Container container = new Container(parent, stack, metadata);
116: Object result = Proxy.newProxyInstance(Thread.currentThread()
117: .getContextClassLoader(), new Class[] { clazz },
118: container);
119: container.setProxy(result);
120: return result;
121: }
122:
123: public static Interceptor[] getStandard() {
124: return new Interceptor[] {
125: ContainerObjectOverridesInterceptor.singleton,
126: JMSExceptionInterceptor.singleton,
127: LogInterceptor.singleton, new ClosedInterceptor() };
128: }
129:
130: // Constructors --------------------------------------------------
131:
132: // Public --------------------------------------------------------
133:
134: // Protected ------------------------------------------------------
135:
136: // Package Private ------------------------------------------------
137:
138: // Private --------------------------------------------------------
139:
140: // Inner Classes --------------------------------------------------
141:
142: }
|