KickJava   Java API By Example, From Geeks To Geeks.

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


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.naming.AbstractModel;
32 import com.caucho.naming.NamingExceptionWrapper;
33 import com.caucho.services.name.NameServerRemote;
34 import com.caucho.util.L10N;
35
36 import javax.ejb.EJBHome JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Hashtable JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 /**
45  * JNDI context for Burlap home objects.
46  *
47  * <p>For now, only allow single level calls to the EJB.
48  */

49 public class BurlapModel extends AbstractModel {
50   private static final L10N L = new L10N(BurlapModel.class);
51   private static final Logger JavaDoc log
52     = Logger.getLogger(BurlapModel.class.getName());
53   
54   private String JavaDoc _urlPrefix;
55   private String JavaDoc _namePrefix;
56   private BurlapModel _root;
57   private Hashtable JavaDoc _cache;
58   private BurlapClientContainer _client;
59   private NameServerRemote _remoteRoot;
60   private NameServerRemote _remote;
61   
62   /**
63    * Creates a new Burlap model
64    */

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

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

101   public AbstractModel copy()
102   {
103     return this;
104   }
105
106   /**
107    * Returns the full url prefix.
108    */

109   String JavaDoc getURLPrefix()
110   {
111     return _root._urlPrefix;
112   }
113
114   /**
115    * Returns the full name.
116    */

117   String JavaDoc getURL()
118   {
119     return getURLPrefix() + _namePrefix;
120   }
121
122   /**
123    * Looks up the named bean. Since we're assuming only a single level,
124    * just try to look it up directly.
125    *
126    * <p>Burlap to find all the valid names.
127    *
128    * @param name the segment name to lookup
129    *
130    * @return the home stub.
131    */

132   public Object JavaDoc lookup(String JavaDoc name)
133     throws NamingException JavaDoc
134   {
135     try {
136       Object JavaDoc obj = _root._cache.get(getURLPrefix() + _namePrefix + name);
137       if (obj != null)
138         return obj;
139
140       if (_root._remoteRoot == null) {
141         if (_root._client == null)
142           _root._client = BurlapClientContainer.find(getURLPrefix());
143       
144         _root._remoteRoot =
145           (NameServerRemote) _client.createObjectStub(getURLPrefix());
146       }
147
148       obj = _root._remoteRoot.lookup(_namePrefix + name);
149
150       if (obj instanceof EJBHome JavaDoc)
151         _root._cache.put(name, obj);
152       else if (obj instanceof NameServerRemote) {
153         BurlapModel model = new BurlapModel(_namePrefix + name, _root);
154         _remote = (NameServerRemote) obj;
155         model.setRemote(_remote);
156         obj = model;
157         _root._cache.put(name, obj);
158       }
159
160       if (log.isLoggable(Level.FINE))
161     log.fine(this + " lookup(" + name + ")->" + obj);
162
163       return obj;
164     } catch (Exception JavaDoc e) {
165       throw new NamingExceptionWrapper(e);
166     }
167   }
168
169   /**
170    * Returns a list of children of the named bean.
171    *
172    * @return array of the children.
173    */

174   public List JavaDoc list()
175     throws NamingException JavaDoc
176   {
177     try {
178       if (_remote == null) {
179         if (_root._remoteRoot == null) {
180           if (_root._client == null)
181             _root._client = BurlapClientContainer.find(getURLPrefix());
182       
183           _root._remoteRoot =
184             (NameServerRemote) _client.createObjectStub(getURLPrefix());
185         }
186
187         Object JavaDoc obj = _root._remoteRoot.lookup(_namePrefix);
188         if (obj instanceof NameServerRemote)
189           _remote = (NameServerRemote) obj;
190       }
191
192       if (_remote == null)
193         throw new NamingException JavaDoc(L.l("Burlap object `{0}' is not a context.",
194                                       getURLPrefix() + _namePrefix));
195
196       String JavaDoc []list = _remote.list();
197
198       ArrayList JavaDoc value = new ArrayList JavaDoc();
199       for (int i = 0; list != null && i < list.length; i++)
200         value.add(list[i]);
201       
202       return value;
203     } catch (NamingException JavaDoc e) {
204       throw e;
205     } catch (Exception JavaDoc e) {
206       throw new NamingExceptionWrapper(e);
207     }
208   }
209
210   public String JavaDoc toString()
211   {
212     return "BurlapModel[url=" + " " + getURLPrefix() + ",name=" + _namePrefix + "]";
213   }
214 }
215
Popular Tags