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.container;
023:
024: import java.lang.reflect.InvocationHandler;
025: import java.lang.reflect.Method;
026: import java.lang.reflect.Proxy;
027: import java.util.Collections;
028: import java.util.HashSet;
029: import java.util.Set;
030:
031: import javax.jms.JMSException;
032:
033: import org.jboss.aop.Advised;
034: import org.jboss.aop.InstanceAdvisor;
035: import org.jboss.aop.MethodInfo;
036: import org.jboss.aop.advice.Interceptor;
037: import org.jboss.aop.joinpoint.Invocation;
038: import org.jboss.aop.joinpoint.MethodInvocation;
039: import org.jboss.aop.metadata.MetaDataResolver;
040: import org.jboss.aop.metadata.SimpleMetaData;
041:
042: /**
043: * A JMS container
044: *
045: * @author <a href="mailto:adrian@jboss.org>Adrian Brock</a>
046: * @version $Revision: 57195 $
047: */
048: public class Container implements InvocationHandler {
049: // Constants -----------------------------------------------------
050:
051: // Attributes ----------------------------------------------------
052:
053: protected Interceptor[] interceptors;
054:
055: protected SimpleMetaData metadata = new SimpleMetaData();
056:
057: /**
058: * The frontend proxy
059: */
060: private Object proxy;
061:
062: /**
063: * The parent
064: */
065: private Container parent;
066:
067: /**
068: * The children
069: */
070: private Set children = Collections.synchronizedSet(new HashSet());
071:
072: // Static --------------------------------------------------------
073:
074: /**
075: * Get the container from a proxy
076: *
077: * @param object the proxy
078: * @returns the container
079: * @throws Throwable for any error
080: */
081: public static Container getContainer(Object object)
082: throws Throwable {
083: Proxy proxy = (Proxy) object;
084: return (Container) Proxy.getInvocationHandler(proxy);
085: }
086:
087: /**
088: * Get the container from an invocation
089: *
090: * @param invocation the conatiner
091: * @returns the container
092: */
093: public static Container getContainer(Invocation invocation) {
094: return (Container) invocation.getMetaData("JMS", "Container");
095: }
096:
097: /**
098: * Get the proxy from an invocation
099: *
100: * @param invocation the conatiner
101: * @returns the proxy
102: */
103: public static Object getProxy(Invocation invocation) {
104: return getContainer(invocation).getProxy();
105: }
106:
107: // Constructors --------------------------------------------------
108:
109: /**
110: * Create a new container
111: *
112: * @param interceptors the interceptors
113: * @param metadata the meta data
114: * @throws JMSException for any error
115: */
116: public Container(Container parent, Interceptor[] interceptors,
117: SimpleMetaData metadata) throws JMSException {
118: this .interceptors = interceptors;
119: this .parent = parent;
120: if (metadata != null)
121: this .metadata = metadata;
122: this .metadata.addMetaData("JMS", "Container", this );
123:
124: if (parent != null)
125: parent.children.add(this );
126: }
127:
128: // Public --------------------------------------------------------
129:
130: public Object getProxy() {
131: return proxy;
132: }
133:
134: public void setProxy(Object proxy) {
135: this .proxy = proxy;
136: }
137:
138: public Container getParent() {
139: return parent;
140: }
141:
142: public Set getChildren() {
143: return children;
144: }
145:
146: public void addChild(Container child) {
147: children.add(child);
148: }
149:
150: public void removeChild(Container child) {
151: children.remove(child);
152: }
153:
154: public Object invoke(Invocation invocation) throws Throwable {
155: //MetaDataResolver oldMetaData = invocation.instanceResolver;
156: //invocation.instanceResolver = getMetaData();
157: try {
158: return invocation.invokeNext(interceptors);
159: } finally {
160: //invocation.instanceResolver = oldMetaData;
161: }
162: }
163:
164: public Object invoke(Object proxy, Method method, Object[] args)
165: throws Throwable {
166: MethodInfo info = new MethodInfo();
167: MethodInvocation invocation = new MethodInvocation(info,
168: interceptors);
169: //invocation.instanceResolver = getMetaData();
170: //invocation.method = m;
171: //invocation.arguments = args;
172: return invocation.invokeNext();
173: }
174:
175: // Protected ------------------------------------------------------
176:
177: // Package Private ------------------------------------------------
178:
179: // Private --------------------------------------------------------
180:
181: // Inner Classes --------------------------------------------------
182: }
|