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.proxy;
023:
024: import java.io.Externalizable;
025:
026: import java.io.IOException;
027: import java.io.ObjectInput;
028: import java.io.ObjectOutput;
029:
030: import java.lang.reflect.Method;
031: import java.lang.reflect.InvocationHandler;
032: import java.util.ArrayList;
033:
034: import org.jboss.invocation.Invocation;
035: import org.jboss.invocation.InvocationContext;
036: import org.jboss.invocation.InvocationKey;
037: import org.jboss.invocation.PayloadKey;
038:
039: /**
040: * An invocation handler whichs sets up the client invocation and
041: * starts the invocation interceptor call chain.
042: *
043: * @author <a href="mailto:marc.fleury@jboss.org">Marc Fleury</a>
044: * @version $Revision: 57209 $
045: */
046: public class ClientContainer implements Externalizable,
047: InvocationHandler {
048: /** The serialVersionUID. @since 1.5 */
049: private static final long serialVersionUID = -4061374432170701306L;
050:
051: /** An empty method parameter list. */
052: protected static final Object[] EMPTY_ARGS = {};
053:
054: /**
055: * The <em>static</em> information that gets attached to every invocation.
056: */
057: public InvocationContext context;
058:
059: /** The first interceptor in the chain. */
060: public Interceptor next;
061:
062: /**
063: * Exposed for externalization.
064: */
065: public ClientContainer() {
066: super ();
067: }
068:
069: public ClientContainer(final InvocationContext context) {
070: this .context = context;
071: }
072:
073: public Object invoke(final Object proxy, final Method m,
074: Object[] args) throws Throwable {
075: // Normalize args to always be an array
076: // Isn't this a bug in the proxy call??
077: if (args == null)
078: args = EMPTY_ARGS;
079:
080: // Create the invocation object
081: Invocation invocation = new Invocation();
082:
083: // Contextual information for the interceptors
084: invocation.setInvocationContext(context);
085: invocation.setId(context.getCacheId());
086: invocation.setObjectName(context.getObjectName());
087: invocation.setMethod(m);
088: invocation.setArguments(args);
089: invocation.setValue(InvocationKey.INVOKER_PROXY_BINDING,
090: context.getInvokerProxyBinding(), PayloadKey.AS_IS);
091:
092: // send the invocation down the client interceptor chain
093: Object obj = next.invoke(invocation);
094: return obj;
095: }
096:
097: public InvocationContext getInvocationContext() {
098: return this .context;
099: }
100:
101: public ArrayList getInterceptors() {
102: ArrayList tmp = new ArrayList();
103: Interceptor inext = next;
104: while (inext != null) {
105: tmp.add(inext);
106: inext = inext.nextInterceptor;
107: }
108: return tmp;
109: }
110:
111: public void setInterceptors(ArrayList interceptors) {
112: if (interceptors.size() == 0)
113: return;
114: next = (Interceptor) interceptors.get(0);
115: Interceptor i = next;
116: for (int n = 1; n < interceptors.size(); n++) {
117: Interceptor inext = (Interceptor) interceptors.get(n);
118: i.setNext(inext);
119: i = inext;
120: }
121: }
122:
123: public Interceptor setNext(Interceptor interceptor) {
124: next = interceptor;
125:
126: return interceptor;
127: }
128:
129: /**
130: * Externalization support.
131: */
132: public void writeExternal(final ObjectOutput out)
133: throws IOException {
134: out.writeObject(next);
135: out.writeObject(context);
136: }
137:
138: /**
139: * Externalization support.
140: */
141: public void readExternal(final ObjectInput in) throws IOException,
142: ClassNotFoundException {
143: next = (Interceptor) in.readObject();
144: context = (InvocationContext) in.readObject();
145: }
146: }
|