KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > hessian > HessianStubFactory


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

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

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

102   public Object JavaDoc create(Class JavaDoc api, String JavaDoc url)
103     throws Exception JavaDoc
104   {
105     StubGenerator gen = new StubGenerator();
106     gen.setClassDir(getWorkPath().lookup("ejb"));
107       
108     Class JavaDoc cl = gen.createStub(api);
109
110     HessianStub stub = (HessianStub) cl.newInstance();
111
112     stub._hessian_setURLPath(Vfs.lookup(url));
113     stub._hessian_setClientContainer(this);
114
115     return stub;
116   }
117
118   public AbstractHessianInput getHessianInput(InputStream JavaDoc is)
119   {
120     AbstractHessianInput in = new HessianSerializerInput(is);
121     in.setRemoteResolver(_resolver);
122
123     return in;
124   }
125
126   public HessianOutput getHessianOutput(OutputStream JavaDoc os)
127   {
128     return new HessianWriter(os);
129   }
130   
131   /**
132    * Looks up a proxy object.
133    */

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