001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * URLContext.java
020: *
021: * This object is responsible for implementing the JNDI context within
022: * Coadunation.
023: */
024:
025: // package path
026: package com.rift.coad.lib.naming.cos;
027:
028: // logging import
029: import org.apache.log4j.Logger;
030:
031: // imports
032: import java.util.Hashtable;
033: import javax.naming.Context;
034: import javax.naming.Name;
035: import javax.naming.NameParser;
036: import javax.naming.NamingEnumeration;
037: import javax.naming.NamingException;
038:
039: /**
040: * This object is responsible for implementing the JNDI context within
041: * Coadunation.
042: *
043: * @author Brett Chaldecott
044: */
045: public class URLContext implements Context {
046:
047: // the class log variable
048: protected Logger log = Logger.getLogger(URLContext.class.getName());
049:
050: // class private member variables
051: private Hashtable env = null;
052: private MasterContext masterContext = null;
053: private Name prefix = null;
054:
055: /**
056: * Creates a new instance of URLContext
057: *
058: * @param env The environment of the cos context.
059: */
060: public URLContext(Hashtable env) throws NamingException {
061: try {
062: this .env = env;
063: masterContext = CosNamingContextManager.getInstance()
064: .getMasterContext();
065: prefix = new NamingParser().parse("");
066: } catch (Exception ex) {
067: throw new NamingException(
068: "Failed to create a new URL context");
069: }
070: }
071:
072: /**
073: * Creates a new instance of URLContext
074: *
075: * @param name The prefix name of this context.
076: * @param env The environment of the cos context.
077: * @exception NamingException
078: */
079: public URLContext(Name name, Hashtable env) throws NamingException {
080: try {
081: this .env = env;
082: masterContext = CosNamingContextManager.getInstance()
083: .getMasterContext();
084: this .prefix = name;
085: } catch (Exception ex) {
086: throw new NamingException(
087: "Failed to create a new URL context");
088: }
089: }
090:
091: /**
092: *
093: * Creates a new instance of URLContext
094: *
095: *
096: * @param env The environment of the cos context.
097: */
098: public URLContext(Hashtable env, MasterContext masterContext,
099: Name prefix) throws NamingException {
100: this .env = env;
101: this .masterContext = masterContext;
102: this .prefix = prefix;
103: }
104:
105: /**
106: * Adds a new environment property to the environment of this context.
107: *
108: * @return The previous value of the property or null.
109: * @param propName The property to replace or add.
110: * @param propValue The new property value.
111: */
112: public Object addToEnvironment(String propName, Object propVal)
113: throws NamingException {
114: return masterContext.addToEnvironment(propName, propVal);
115: }
116:
117: /**
118: * Binds a name to an object.
119: *
120: * @param name The name of the object to bind.
121: * @param obj The object to bind.
122: * @exception NamingException
123: */
124: public void bind(Name name, Object obj) throws NamingException {
125: Name composedName = masterContext.composeName(name, prefix);
126: masterContext.bind(composedName, obj);
127: }
128:
129: /**
130: * Binds a name to an object.
131: */
132: public void bind(String name, Object obj) throws NamingException {
133: Name composedName = masterContext.composeName(
134: new NamingParser().parse(name), prefix);
135: masterContext.bind(composedName, obj);
136: }
137:
138: /**
139: * Closes this context.
140: */
141: public void close() throws NamingException {
142: masterContext.close();
143: }
144:
145: /**
146: * Composes the name of this context with a name relative to this context.
147: *
148: * @return The compisit name.
149: * @param name The name to add to the prefix.
150: * @param prefix The prefix of the current context.
151: * @exception NamingException
152: */
153: public Name composeName(Name name, Name prefix)
154: throws NamingException {
155: return masterContext.composeName(name, prefix);
156: }
157:
158: /**
159: * Composes the name of this context with a name relative to this context.
160: *
161: * @return The string version of the composed name.
162: * @param name The name to add to the preffix.
163: * @param prefix The prefix for this context.
164: */
165: public String composeName(String name, String prefix)
166: throws NamingException {
167: return masterContext.composeName(name, prefix);
168: }
169:
170: /**
171: * Creates and binds a new context to this context.
172: *
173: * @return The newly created context.
174: * @param name The name of the new sub context.
175: * @exception NamingException
176: */
177: public Context createSubcontext(Name name) throws NamingException {
178: Name composedName = masterContext.composeName(name, prefix);
179: masterContext.createSubcontext(composedName);
180: return new URLContext(env, masterContext, composedName);
181: }
182:
183: /**
184: * Creates and binds a new context.
185: *
186: * @return The newly create sub context.
187: * @exception name The name of the new sub context.
188: * @exception NamingException
189: */
190: public Context createSubcontext(String name) throws NamingException {
191: Name composedName = masterContext.composeName(
192: new NamingParser().parse(name), prefix);
193: masterContext.createSubcontext(composedName);
194: return new URLContext(env, masterContext, composedName);
195: }
196:
197: /**
198: * Destroys the named context and removes it from the namespace.
199: *
200: * @param name The name of the sub context to remove.
201: * @exception NamingException
202: */
203: public void destroySubcontext(Name name) throws NamingException {
204: Name composedName = masterContext.composeName(name, prefix);
205: masterContext.destroySubcontext(composedName);
206: }
207:
208: /**
209: * Destroys the named context and removes it from the namespace.
210: *
211: * @param name The name of the context to destroy.
212: */
213: public void destroySubcontext(String name) throws NamingException {
214: Name composedName = masterContext.composeName(
215: new NamingParser().parse(name), prefix);
216: masterContext.destroySubcontext(composedName);
217: }
218:
219: /**
220: * Retrieves the environment in effect for this context.
221: *
222: * @return The reference to the hash table.
223: * @exception NamingException
224: */
225: public Hashtable getEnvironment() throws NamingException {
226: return masterContext.getEnvironment();
227: }
228:
229: /**
230: * Retrieves the full name of this context within its own namespace.
231: */
232: public String getNameInNamespace() throws NamingException {
233: return masterContext.getNameInNamespace();
234: }
235:
236: /**
237: * Retrieves the parser associated with the named context.
238: *
239: * @return The reference to the name parser.
240: * @param name The name to return the parser for.
241: * @exception NamingException
242: */
243: public NameParser getNameParser(Name name) throws NamingException {
244: return masterContext.getNameParser(name);
245: }
246:
247: /**
248: * Retrieves the parser associated with the named context.
249: */
250: public NameParser getNameParser(String name) throws NamingException {
251: return masterContext.getNameParser(name);
252: }
253:
254: /**
255: * Enumerates the names bound in the named context, along with the class
256: * names of objects bound to them.
257: */
258: public NamingEnumeration list(Name name) throws NamingException {
259: Name composedName = masterContext.composeName(name, prefix);
260: return masterContext.list(composedName);
261: }
262:
263: /**
264: * Enumerates the names bound in the named context, along with the class
265: * names of objects bound to them.
266: *
267: * @return The list of names bound to this context.
268: * @param name The list of names.
269: * @exception NamingException
270: */
271: public NamingEnumeration list(String name) throws NamingException {
272: Name composedName = masterContext.composeName(
273: new NamingParser().parse(name), prefix);
274: return masterContext.list(composedName);
275: }
276:
277: /**
278: * Enumerates the names bound in the named context, along with the objects
279: * bound to them.
280: *
281: * @return The list of bindings for the name
282: * @param name The name to perform the search below.
283: * @exception NamingException
284: */
285: public NamingEnumeration listBindings(Name name)
286: throws NamingException {
287: Name composedName = masterContext.composeName(name, prefix);
288: return masterContext.listBindings(composedName);
289: }
290:
291: /**
292: * Enumerates the names bound in the named context, along with the objects
293: * bound to them.
294: *
295: * @return The list of binding for the name.
296: * @param name The name to perform the search for.
297: * @exception NamingException
298: */
299: public NamingEnumeration listBindings(String name)
300: throws NamingException {
301: Name composedName = masterContext.composeName(
302: new NamingParser().parse(name), prefix);
303: return masterContext.listBindings(composedName);
304: }
305:
306: /**
307: * Retrieves the named object.
308: *
309: * @return The named object.
310: * @param name The name to retrieve the object for.
311: * @exception NamingException
312: */
313: public Object lookup(Name name) throws NamingException {
314: Name composedName = masterContext.composeName(name, prefix);
315: Object result = masterContext.lookup(composedName);
316: if (result instanceof Context) {
317: return new URLContext(env, masterContext, composedName);
318: }
319: return result;
320: }
321:
322: /**
323: * Retrieves the named object.
324: *
325: * @return The object to retrieve by name.
326: * @param name The name of the object to retrieve.
327: * @exception NamingException
328: */
329: public Object lookup(String name) throws NamingException {
330: Name composedName = masterContext.composeName(
331: new NamingParser().parse(name), prefix);
332: Object result = masterContext.lookup(composedName);
333: if (result instanceof Context) {
334: return new URLContext(env, masterContext, composedName);
335: }
336: return result;
337: }
338:
339: /**
340: * Retrieves the named object, following links except for the terminal
341: * atomic component of the name.
342: *
343: * @return The object to retrieve.
344: * @param name The name of the object to lookup.
345: * @exception NamingException
346: */
347: public Object lookupLink(Name name) throws NamingException {
348: Name composedName = masterContext.composeName(name, prefix);
349: Object result = masterContext.lookupLink(composedName);
350: if (result instanceof Context) {
351: return new URLContext(env, masterContext, composedName);
352: }
353: return result;
354: }
355:
356: /**
357: * Retrieves the named object, following links except for the terminal
358: * atomic component of the name.
359: *
360: * @return The results of the lookup link.
361: * @param name The name of the object to lookup.
362: * @exception NamingException
363: */
364: public Object lookupLink(String name) throws NamingException {
365: Name composedName = masterContext.composeName(
366: new NamingParser().parse(name), prefix);
367: Object result = masterContext.lookupLink(composedName);
368: if (result instanceof Context) {
369: return new URLContext(env, masterContext, composedName);
370: }
371: return result;
372: }
373:
374: /**
375: * Binds a name to an object, overwriting any existing binding.
376: *
377: * @param name The name to rebind.
378: * @param obj The object to rebind.
379: * @exception NamingException
380: */
381: public void rebind(Name name, Object obj) throws NamingException {
382: Name composedName = masterContext.composeName(name, prefix);
383: masterContext.rebind(composedName, obj);
384: }
385:
386: /**
387: * Binds a name to an object, overwriting any existing binding.
388: *
389: * @param name The name to rebind.
390: * @param obj The object to rebind.
391: * @exception NamingException
392: */
393: public void rebind(String name, Object obj) throws NamingException {
394: Name composedName = masterContext.composeName(
395: new NamingParser().parse(name), prefix);
396: masterContext.rebind(composedName, obj);
397: }
398:
399: /**
400: * Removes an environment property from the environment of this context.
401: *
402: * @param propName The name of the entry to remove from the environment.
403: * @exception NamingException
404: */
405: public Object removeFromEnvironment(String propName)
406: throws NamingException {
407: return masterContext.removeFromEnvironment(propName);
408: }
409:
410: /**
411: * Binds a new name to the object bound to an old name, and unbinds the old
412: * name.
413: *
414: * @param oldName The old name to rename.
415: * @param newName The name to replace it with.
416: * @exception NamingException
417: */
418: public void rename(Name oldName, Name newName)
419: throws NamingException {
420: Name oldComposedName = masterContext.composeName(oldName,
421: prefix);
422: Name newComposedName = masterContext.composeName(newName,
423: prefix);
424: masterContext.rename(oldComposedName, newComposedName);
425: }
426:
427: /**
428: * Binds a new name to the object bound to an old name, and unbinds the old
429: * name.
430: *
431: * @param oldName The old name to rename.
432: * @param newName The name to replace it with.
433: * @exception NamingException
434: */
435: public void rename(String oldName, String newName)
436: throws NamingException {
437: Name oldComposedName = masterContext.composeName(
438: new NamingParser().parse(oldName), prefix);
439: Name newComposedName = masterContext.composeName(
440: new NamingParser().parse(newName), prefix);
441: masterContext.rename(oldComposedName, newComposedName);
442: }
443:
444: /**
445: * Unbinds the named object.
446: *
447: * @param name The name to unbind.
448: * @exception NamingException
449: */
450: public void unbind(Name name) throws NamingException {
451: Name composedName = masterContext.composeName(name, prefix);
452: masterContext.unbind(composedName);
453: }
454:
455: /**
456: * Unbinds the named objec.
457: *
458: * @param name The name to unbind.
459: * @exception NamingException
460: */
461: public void unbind(String name) throws NamingException {
462: Name composedName = masterContext.composeName(
463: new NamingParser().parse(name), prefix);
464: masterContext.unbind(composedName);
465: }
466:
467: }
|