KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.Externalizable JavaDoc;
27 import java.io.ObjectInput JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.ObjectOutput JavaDoc;
30 import org.jboss.aspects.asynch.AsynchMixin;
31 import org.jboss.aspects.asynch.AsynchProvider;
32 import org.jboss.aspects.asynch.FutureHolder;
33 import org.jboss.ejb3.Container;
34 import org.jboss.ejb3.LocalProxy;
35 import org.jboss.ejb3.ProxyUtils;
36 import org.jboss.util.id.GUID;
37
38 /**
39  * Comment
40  *
41  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
42  * @version $Revision: 43261 $
43  */

44 public class StatefulLocalProxy extends LocalProxy implements Externalizable JavaDoc
45 {
46    protected Object JavaDoc id;
47    AsynchProvider provider;
48
49
50
51    public StatefulLocalProxy(Container container, Object JavaDoc id)
52    {
53       super(container);
54       this.id = id;
55    }
56
57    public StatefulLocalProxy(AsynchProvider provider, Container container, Object JavaDoc id)
58    {
59       super(container);
60       this.provider = provider;
61       this.id = id;
62    }
63
64    public StatefulLocalProxy()
65    {
66    }
67
68    //@Override
69
public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
70    {
71       super.readExternal(in);
72       id = in.readObject();
73    }
74
75    //@Override
76
public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
77    {
78       super.writeExternal(out);
79       out.writeObject(id);
80    }
81
82    public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
83            throws Throwable JavaDoc
84    {
85       if (method.getDeclaringClass() == AsynchProvider.class)
86       {
87          return provider.getFuture();
88       }
89
90       //Make sure we get the cache id before getting the asynchronous interface
91
StatefulContainer sfsb = (StatefulContainer) container;
92       Object JavaDoc ret = ProxyUtils.handleCallLocally(proxy, this, method, args);
93       if (ret != null)
94       {
95          return ret;
96       }
97
98       return sfsb.localInvoke(id, method, args, (FutureHolder) provider);
99    }
100
101    public Object JavaDoc getAsynchronousProxy(Object JavaDoc proxy)
102    {
103       Class JavaDoc[] infs = proxy.getClass().getInterfaces();
104       if (!ProxyUtils.isAsynchronous(infs))
105       {
106          Class JavaDoc[] interfaces = ProxyUtils.addAsynchProviderInterface(infs);
107          AsynchMixin mixin = new AsynchMixin();
108          StatefulLocalProxy handler = new StatefulLocalProxy(mixin, container, id);
109          return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), interfaces, handler);
110       }
111
112       //I was already asynchronous
113
return proxy;
114    }
115
116    public String JavaDoc toString()
117    {
118       if (id != null)
119       {
120          return container.getEjbName().toString() + ":" + id.toString();
121       }
122       else
123       {
124          //If the proxy has not been used yet, create a temporary id
125
GUID guid = new GUID();
126          return container.getEjbName().toString() + ":" + guid.toString();
127       }
128    }
129
130 }
131
Popular Tags