KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.ejb.hessian;
31
32 import com.caucho.naming.AbstractModel;
33 import com.caucho.naming.NamingExceptionWrapper;
34 import com.caucho.services.name.NameServerRemote;
35 import com.caucho.util.L10N;
36
37 import javax.ejb.EJBHome JavaDoc;
38 import javax.naming.NamingException JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Hashtable JavaDoc;
41 import java.util.List JavaDoc;
42
43 /**
44  * JNDI context for Hessian home objects.
45  *
46  * <p>For now, only allow single level calls to the EJB.
47  */

48 public class HessianModel extends AbstractModel {
49   private static L10N L = new L10N(HessianModel.class);
50   
51   private String JavaDoc _urlPrefix;
52   private String JavaDoc _namePrefix;
53   private HessianModel _root;
54   private Hashtable JavaDoc _cache;
55   private HessianClientContainer _client;
56   private NameServerRemote _remoteRoot;
57   private NameServerRemote _remote;
58   
59   /**
60    * Creates a new Hessian model
61    */

62   public HessianModel(String JavaDoc prefix)
63   {
64     if (! prefix.endsWith("/"))
65       prefix = prefix + '/';
66     
67     _urlPrefix = prefix;
68     _namePrefix = "/";
69     _root = this;
70     _cache = new Hashtable JavaDoc();
71   }
72   
73   /**
74    * Returns the root Hessian model
75    */

76   public HessianModel(String JavaDoc namePrefix, HessianModel root)
77   {
78     if (! namePrefix.endsWith("/"))
79       namePrefix = namePrefix + '/';
80     
81     _namePrefix = namePrefix;
82     _root = root;
83   }
84
85   void setRemote(NameServerRemote remote)
86   {
87     _remote = remote;
88   }
89
90   void setClientContainer(HessianClientContainer client)
91   {
92     _root._client = client;
93   }
94
95   /**
96    * Creates a new instance of HessianModel.
97    */

98   public AbstractModel copy()
99   {
100     return this;
101   }
102
103   /**
104    * Returns the full url prefix.
105    */

106   String JavaDoc getURLPrefix()
107   {
108     return _root._urlPrefix;
109   }
110
111   /**
112    * Looks up the named bean. Since we're assuming only a single level,
113    * just try to look it up directly.
114    *
115    * <p>Hessian to find all the valid names.
116    *
117    * @param name the segment name to lookup
118    *
119    * @return the home stub.
120    */

121   public Object JavaDoc lookup(String JavaDoc name)
122     throws NamingException JavaDoc
123   {
124     try {
125       String JavaDoc urlPrefix = getURLPrefix();
126       String JavaDoc cacheName = urlPrefix + _namePrefix + name;
127       
128       Object JavaDoc obj = _root._cache.get(cacheName);
129       if (obj != null)
130         return obj;
131
132       if (_root._remoteRoot == null) {
133         if (_root._client == null)
134           _root._client = HessianClientContainer.find(urlPrefix);
135
136         Object JavaDoc stub = _client.createObjectStub(urlPrefix);
137
138         _root._remoteRoot = (NameServerRemote) stub;
139       }
140
141       obj = _root._remoteRoot.lookup(_namePrefix + name);
142
143       if (obj instanceof EJBHome JavaDoc)
144         _root._cache.put(cacheName, obj);
145       else if (obj instanceof NameServerRemote) {
146         HessianModel model = new HessianModel(_namePrefix + name, _root);
147         NameServerRemote remote = (NameServerRemote) obj;
148         model.setRemote(remote);
149         obj = model;
150         _root._cache.put(cacheName, obj);
151       }
152
153       return obj;
154     } catch (Exception JavaDoc e) {
155       throw new NamingExceptionWrapper(e);
156     }
157   }
158
159   /**
160    * Returns a list of children of the named bean.
161    *
162    * @return array of the children.
163    */

164   public List JavaDoc list()
165     throws NamingException JavaDoc
166   {
167     try {
168       if (_remote == null) {
169         if (_root._remoteRoot == null) {
170           if (_root._client == null)
171             _root._client = HessianClientContainer.find(getURLPrefix());
172       
173           _root._remoteRoot =
174             (NameServerRemote) _client.createObjectStub(getURLPrefix());
175         }
176
177         Object JavaDoc obj = _root._remoteRoot.lookup(_namePrefix);
178         if (obj instanceof NameServerRemote)
179           _remote = (NameServerRemote) obj;
180       }
181
182       if (_remote == null)
183         throw new NamingException JavaDoc(L.l("Hessian object `{0}' is not a context.",
184                                       getURLPrefix() + _namePrefix));
185
186       String JavaDoc []list = _remote.list();
187
188       ArrayList JavaDoc value = new ArrayList JavaDoc();
189       for (int i = 0; list != null && i < list.length; i++)
190         value.add(list[i]);
191       
192       return value;
193     } catch (NamingException JavaDoc e) {
194       throw e;
195     } catch (Exception JavaDoc e) {
196       throw new NamingExceptionWrapper(e);
197     }
198   }
199
200   public String JavaDoc toString()
201   {
202     return "HessianModel[url=" + " " + getURLPrefix() + ",name=" + _namePrefix + "]";
203   }
204 }
205
Popular Tags