001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ejb.hessian;
031:
032: import com.caucho.naming.AbstractModel;
033: import com.caucho.naming.NamingExceptionWrapper;
034: import com.caucho.services.name.NameServerRemote;
035: import com.caucho.util.L10N;
036:
037: import javax.ejb.EJBHome;
038: import javax.naming.NamingException;
039: import java.util.ArrayList;
040: import java.util.Hashtable;
041: import java.util.List;
042:
043: /**
044: * JNDI context for Hessian home objects.
045: *
046: * <p>For now, only allow single level calls to the EJB.
047: */
048: public class HessianModel extends AbstractModel {
049: private static L10N L = new L10N(HessianModel.class);
050:
051: private String _urlPrefix;
052: private String _namePrefix;
053: private HessianModel _root;
054: private Hashtable _cache;
055: private HessianClientContainer _client;
056: private NameServerRemote _remoteRoot;
057: private NameServerRemote _remote;
058:
059: /**
060: * Creates a new Hessian model
061: */
062: public HessianModel(String prefix) {
063: if (!prefix.endsWith("/"))
064: prefix = prefix + '/';
065:
066: _urlPrefix = prefix;
067: _namePrefix = "/";
068: _root = this ;
069: _cache = new Hashtable();
070: }
071:
072: /**
073: * Returns the root Hessian model
074: */
075: public HessianModel(String namePrefix, HessianModel root) {
076: if (!namePrefix.endsWith("/"))
077: namePrefix = namePrefix + '/';
078:
079: _namePrefix = namePrefix;
080: _root = root;
081: }
082:
083: void setRemote(NameServerRemote remote) {
084: _remote = remote;
085: }
086:
087: void setClientContainer(HessianClientContainer client) {
088: _root._client = client;
089: }
090:
091: /**
092: * Creates a new instance of HessianModel.
093: */
094: public AbstractModel copy() {
095: return this ;
096: }
097:
098: /**
099: * Returns the full url prefix.
100: */
101: String getURLPrefix() {
102: return _root._urlPrefix;
103: }
104:
105: /**
106: * Looks up the named bean. Since we're assuming only a single level,
107: * just try to look it up directly.
108: *
109: * <p>Hessian to find all the valid names.
110: *
111: * @param name the segment name to lookup
112: *
113: * @return the home stub.
114: */
115: public Object lookup(String name) throws NamingException {
116: try {
117: String urlPrefix = getURLPrefix();
118: String cacheName = urlPrefix + _namePrefix + name;
119:
120: Object obj = _root._cache.get(cacheName);
121: if (obj != null)
122: return obj;
123:
124: if (_root._remoteRoot == null) {
125: if (_root._client == null)
126: _root._client = HessianClientContainer
127: .find(urlPrefix);
128:
129: Object stub = _client.createObjectStub(urlPrefix);
130:
131: _root._remoteRoot = (NameServerRemote) stub;
132: }
133:
134: obj = _root._remoteRoot.lookup(_namePrefix + name);
135:
136: if (obj instanceof EJBHome)
137: _root._cache.put(cacheName, obj);
138: else if (obj instanceof NameServerRemote) {
139: HessianModel model = new HessianModel(_namePrefix
140: + name, _root);
141: NameServerRemote remote = (NameServerRemote) obj;
142: model.setRemote(remote);
143: obj = model;
144: _root._cache.put(cacheName, obj);
145: }
146:
147: return obj;
148: } catch (Exception e) {
149: throw new NamingExceptionWrapper(e);
150: }
151: }
152:
153: /**
154: * Returns a list of children of the named bean.
155: *
156: * @return array of the children.
157: */
158: public List list() throws NamingException {
159: try {
160: if (_remote == null) {
161: if (_root._remoteRoot == null) {
162: if (_root._client == null)
163: _root._client = HessianClientContainer
164: .find(getURLPrefix());
165:
166: _root._remoteRoot = (NameServerRemote) _client
167: .createObjectStub(getURLPrefix());
168: }
169:
170: Object obj = _root._remoteRoot.lookup(_namePrefix);
171: if (obj instanceof NameServerRemote)
172: _remote = (NameServerRemote) obj;
173: }
174:
175: if (_remote == null)
176: throw new NamingException(L.l(
177: "Hessian object `{0}' is not a context.",
178: getURLPrefix() + _namePrefix));
179:
180: String[] list = _remote.list();
181:
182: ArrayList value = new ArrayList();
183: for (int i = 0; list != null && i < list.length; i++)
184: value.add(list[i]);
185:
186: return value;
187: } catch (NamingException e) {
188: throw e;
189: } catch (Exception e) {
190: throw new NamingExceptionWrapper(e);
191: }
192: }
193:
194: public String toString() {
195: return "HessianModel[url=" + " " + getURLPrefix() + ",name="
196: + _namePrefix + "]";
197: }
198: }
|