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.naming;
031:
032: import com.caucho.util.L10N;
033:
034: import javax.naming.NamingException;
035: import java.util.ArrayList;
036: import java.util.HashMap;
037: import java.util.List;
038:
039: /**
040: * Environment based model for JNDI.
041: */
042: public class EnvironmentModel extends AbstractModel {
043: private static final L10N L = new L10N(EnvironmentModel.class);
044:
045: private final EnvironmentModelRoot _root;
046: private final String _name;
047:
048: private HashMap<String, Object> _children = new HashMap<String, Object>(
049: 8);
050:
051: /**
052: * Creates a new instance of the environment model.
053: */
054: EnvironmentModel(EnvironmentModelRoot root, String name) {
055: _root = root;
056: _name = name;
057:
058: if ("ejb".equals(name))
059: Thread.dumpStack();
060: }
061:
062: /**
063: * Creates a new instance of EnvironmentModel.
064: */
065: protected AbstractModel create() {
066: return new EnvironmentModel(_root, _name);
067: }
068:
069: /**
070: * Returns the object from looking up a single link.
071: *
072: * @param name the name segment.
073: *
074: * @return the object stored in the map.
075: */
076: public Object lookup(String name) throws NamingException {
077: Object value = _children.get(name);
078:
079: if (value != null)
080: return value;
081:
082: ClassLoader loader = _root.getClassLoader();
083:
084: if (loader == null)
085: return null;
086:
087: EnvironmentModelRoot parentRoot = EnvironmentModelRoot
088: .getLocal(loader.getParent());
089:
090: if (parentRoot != null) {
091: EnvironmentModel parentModel = parentRoot.get(_name);
092:
093: if (parentModel != null) {
094: value = parentModel.lookup(name);
095:
096: if (value instanceof EnvironmentModel) {
097: value = createSubcontext(name);
098: }
099: }
100: }
101:
102: return value;
103: }
104:
105: /**
106: * Rebinds an object as a child to the model.
107: */
108: public void bind(String name, Object obj) throws NamingException {
109: _children.put(name, obj);
110: }
111:
112: /**
113: * Unbinds an object as a child to the model.
114: */
115: public void unbind(String name) throws NamingException {
116: Object oldValue = _children.remove(name);
117:
118: if (oldValue instanceof EnvironmentModel)
119: _root.remove(_name + "/" + name);
120: }
121:
122: /**
123: * Creates a subcontext.
124: */
125: public AbstractModel createSubcontext(String name)
126: throws NamingException {
127: if (_children.get(name) != null) {
128: throw new NamingException(L.l(
129: "can't create subcontext: {0} {1}", name, _children
130: .get(name)));
131: }
132:
133: String childName;
134:
135: if (_name.equals(""))
136: childName = name;
137: else
138: childName = _name + "/" + name;
139:
140: EnvironmentModel model = new EnvironmentModel(_root, childName);
141:
142: _children.put(name, model);
143:
144: _root.put(childName, model);
145:
146: return model;
147: }
148:
149: /**
150: * Lists the child names.
151: */
152: public List list() {
153: ArrayList values = new ArrayList();
154:
155: fillList(values);
156:
157: return values;
158: }
159:
160: protected void fillList(ArrayList values) {
161: for (String key : _children.keySet()) {
162: if (!values.contains(key))
163: values.add(key);
164: }
165:
166: ClassLoader loader = _root.getClassLoader();
167:
168: if (loader == null)
169: return;
170:
171: EnvironmentModelRoot parentRoot = EnvironmentModelRoot
172: .getLocal(loader.getParent());
173:
174: if (parentRoot != null) {
175: EnvironmentModel parentModel = parentRoot.get(_name);
176:
177: if (parentModel != null) {
178: parentModel.fillList(values);
179: }
180: }
181: }
182: }
|