KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > burlap > BurlapStubFactory


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @@author Scott Ferguson
27  */

28
29 package com.caucho.ejb.burlap;
30
31 import com.caucho.burlap.io.BurlapInput;
32 import com.caucho.hessian.io.AbstractHessianInput;
33 import com.caucho.hessian.io.AbstractHessianOutput;
34 import com.caucho.hessian.io.HessianRemoteResolver;
35 import com.caucho.server.util.CauchoSystem;
36 import com.caucho.vfs.Path;
37 import com.caucho.vfs.Vfs;
38
39 import java.io.IOException JavaDoc;
40 import java.io.InputStream JavaDoc;
41 import java.io.OutputStream JavaDoc;
42
43 /**
44  * Factory for creating Burlap client stubs. The returned stub will
45  * call the remote object for all methods.
46  *
47  * <pre>
48  * String url = "http://localhost:8080/ejb/hello";
49  * HelloHome hello = (HelloHome) factory.create(HelloHome.class, url);
50  * </pre>
51  *
52  * After creation, the stub can be like a regular Java class. Because
53  * it makes remote calls, it can throw more exceptions than a Java class.
54  * In particular, it may throw protocol exceptions.
55  */

56
57 public class BurlapStubFactory implements HessianRemoteResolver {
58   private HessianRemoteResolver _resolver;
59   private Path _workPath;
60   
61   public BurlapStubFactory()
62   {
63     _resolver = this;
64   }
65
66   /**
67    * Returns the remote resolver.
68    */

69   public HessianRemoteResolver getRemoteResolver()
70   {
71     return _resolver;
72   }
73
74   public void setWorkPath(Path path)
75   {
76     _workPath = path;
77   }
78
79   public Path getWorkPath()
80   {
81     return _workPath;
82   }
83
84   /**
85    * Creates a new proxy with the specified URL. The returned object
86    * is a proxy with the interface specified by api.
87    *
88    * <pre>
89    * String url = "http://localhost:8080/ejb/hello");
90    * HelloHome hello = (HelloHome) factory.create(HelloHome.class, url);
91    * </pre>
92    *
93    * @@param api the interface the proxy class needs to implement
94    * @@param url the URL where the client object is located.
95    *
96    * @@return a proxy to the object with the specified interface.
97    */

98   public Object JavaDoc create(Class JavaDoc api, String JavaDoc url)
99     throws Exception JavaDoc
100   {
101     StubGenerator gen = new StubGenerator();
102     gen.setClassDir(getWorkPath());
103       
104     Class JavaDoc cl = gen.createStub(api);
105
106     BurlapStub stub = (BurlapStub) cl.newInstance();
107
108     stub._burlap_setURLPath(Vfs.lookup(url));
109     stub._burlap_setClientContainer(this);
110
111     return stub;
112   }
113
114   public AbstractHessianInput getBurlapInput(InputStream JavaDoc is)
115   {
116     AbstractHessianInput in = new BurlapInput(is);
117     in.setRemoteResolver(_resolver);
118
119     return in;
120   }
121
122   public AbstractHessianOutput getBurlapOutput(OutputStream JavaDoc os)
123   {
124     return new BurlapWriter(os);
125   }
126   
127   /**
128    * Looks up a proxy object.
129    */

130   public Object JavaDoc lookup(String JavaDoc type, String JavaDoc url)
131     throws IOException JavaDoc
132   {
133     try {
134       Class JavaDoc api = CauchoSystem.loadClass(type);
135
136       return create(api, url);
137     } catch (RuntimeException JavaDoc e) {
138       throw e;
139     } catch (Exception JavaDoc e) {
140       IOException JavaDoc e1 = new IOException JavaDoc(e.toString());
141       e1.initCause(e);
142
143       throw e1;
144     }
145   }
146 }
147
Popular Tags