KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > stateful > StatefulClusteredProxy


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.stateful;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.Proxy JavaDoc;
26 import org.jboss.aop.Dispatcher;
27 import org.jboss.aop.advice.Interceptor;
28 import org.jboss.aop.util.MethodHashing;
29 import org.jboss.aop.util.PayloadKey;
30 import org.jboss.aspects.asynch.AsynchMixin;
31 import org.jboss.aspects.asynch.AsynchProvider;
32 import org.jboss.aspects.remoting.ClusterConstants;
33 import org.jboss.aspects.remoting.FamilyWrapper;
34 import org.jboss.aspects.remoting.InvokeRemoteInterceptor;
35 import org.jboss.ejb3.JBossProxy;
36 import org.jboss.ejb3.ProxyUtils;
37 import org.jboss.ejb3.asynchronous.AsynchronousInterceptor;
38 import org.jboss.ha.framework.interfaces.LoadBalancePolicy;
39 import org.jboss.util.id.GUID;
40
41 /**
42  * Comment
43  *
44  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
45  * @version $Revision: 40600 $
46  */

47 public class StatefulClusteredProxy extends org.jboss.ejb3.remoting.BaseRemoteProxy
48 {
49    private Object JavaDoc id;
50    protected FamilyWrapper family;
51    protected LoadBalancePolicy lbPolicy;
52    AsynchProvider provider;
53
54
55    public StatefulClusteredProxy(Object JavaDoc containerId, Interceptor[] interceptors, FamilyWrapper family, LoadBalancePolicy lb)
56    {
57       super(containerId, interceptors);
58       this.family = family;
59       this.lbPolicy = lb;
60    }
61
62    public StatefulClusteredProxy(AsynchProvider provider, Object JavaDoc containerId, Interceptor[] interceptors, FamilyWrapper family, LoadBalancePolicy lb)
63    {
64       super(containerId, interceptors);
65       this.family = family;
66       this.lbPolicy = lb;
67       this.provider = provider;
68    }
69
70    protected StatefulClusteredProxy()
71    {
72    }
73
74    public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
75            throws Throwable JavaDoc
76    {
77       if (method.getDeclaringClass() == AsynchProvider.class)
78       {
79          return provider.getFuture();
80       }
81
82       long hash = MethodHashing.calculateHash(method);
83       Object JavaDoc ret = ProxyUtils.handleCallLocally(hash, (JBossProxy) proxy, this, method, args);
84       if (ret != null)
85       {
86          return ret;
87       }
88
89       StatefulRemoteInvocation sri = new StatefulRemoteInvocation(interceptors, hash, method, method, null, id);
90       sri.setArguments(args);
91       sri.setInstanceResolver(metadata);
92       sri.getMetaData().addMetaData(Dispatcher.DISPATCHER, Dispatcher.OID, containerId, PayloadKey.AS_IS);
93       sri.getMetaData().addMetaData(ClusterConstants.CLUSTERED_REMOTING, ClusterConstants.CLUSTER_FAMILY_WRAPPER, family, PayloadKey.AS_IS);
94       sri.getMetaData().addMetaData(ClusterConstants.CLUSTERED_REMOTING, ClusterConstants.LOADBALANCE_POLICY, lbPolicy, PayloadKey.AS_IS);
95       sri.getMetaData().addMetaData(InvokeRemoteInterceptor.REMOTING, InvokeRemoteInterceptor.SUBSYSTEM, "AOP", PayloadKey.AS_IS);
96
97       if (provider != null)
98       {
99          sri.getMetaData().addMetaData(AsynchronousInterceptor.ASYNCH, AsynchronousInterceptor.INVOKE_ASYNCH, "YES", PayloadKey.AS_IS);
100       }
101       try
102       {
103          Object JavaDoc rtn = sri.invokeNext();
104          // if this is first invocation then container passes back actual ID
105
if (id == null)
106          {
107             id = sri.getResponseAttachment(StatefulConstants.NEW_ID);
108          }
109          return rtn;
110       }
111       catch (ForwardId forward)
112       {
113          // if this is first invocation then container passes back actual ID
114
// The ForwardId exception is needed if 1st operation throws an exception
115
id = forward.getId();
116          throw forward.getCause();
117       }
118    }
119
120    public Object JavaDoc getAsynchronousProxy(Object JavaDoc proxy)
121    {
122       Class JavaDoc[] infs = proxy.getClass().getInterfaces();
123       if (!ProxyUtils.isAsynchronous(infs))
124       {
125          Class JavaDoc[] interfaces = ProxyUtils.addAsynchProviderInterface(infs);
126          AsynchMixin mixin = new AsynchMixin();
127          Interceptor[] newInterceptors = ProxyUtils.addAsynchProxyInterceptor(mixin, interceptors);
128          StatefulClusteredProxy handler = new StatefulClusteredProxy(mixin, containerId, newInterceptors, family, lbPolicy);
129          return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), interfaces, handler);
130       }
131
132       //I was already asynchronous
133
return proxy;
134    }
135
136    public String JavaDoc toString()
137    {
138       if (id != null)
139       {
140          return containerId.toString() + ":" + id.toString();
141       }
142       else
143       {
144          //If the proxy has not been used yet, create a temporary id
145
GUID guid = new GUID();
146          return containerId.toString() + ":" + guid.toString();
147       }
148    }
149
150 }
151
Popular Tags