001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.naming.core;
018:
019: import java.util.Hashtable;
020:
021: import javax.naming.Name;
022: import javax.naming.NameAlreadyBoundException;
023: import javax.naming.NamingEnumeration;
024: import javax.naming.NamingException;
025: import javax.naming.OperationNotSupportedException;
026: import javax.naming.directory.Attribute;
027: import javax.naming.directory.Attributes;
028: import javax.naming.directory.DirContext;
029: import javax.naming.directory.ModificationItem;
030: import javax.naming.directory.SearchControls;
031:
032: //import org.apache.naming.core.NameParserImpl;
033:
034: // Based on a merge of various catalina naming contexts
035: // Name is used - it provide better oportunities for reuse and optimizations
036:
037: /**
038: * Base Directory Context implementation. All j-t-c/naming contexts should
039: * extend it.
040: *
041: * Implements all JNDI methods - if you just extend it you'll get UnsuportedOperation.
042: * XXX Should it also act as introspector proxy or should we use a separate context ?
043: * The intention is to allow use 'introspection magic' and bean-like DirContexts.
044: *
045: * IMPORTANT: all contexts should use setters/getters for configuration, instead
046: * of the Hashtable. The default constructor will use introspection to configure
047: * and may provide ( via a hook ? ) JMX management on all contexts.
048: *
049: * You must extend and override few methods. Of course, you can also override any other
050: * method and provide a more optimal implementation, but in most cases you only
051: * need the minimal set.
052: *
053: * All methods use Name variant. They should expect an arbitrary implementation, but
054: * it's recommended to check if ServerName is used - and take advantage of the
055: * specific features ( MessageBytes, etc ).
056: *
057: * <ul>
058: * <li>
059: * </ul>
060: *
061: * @author Remy Maucherat
062: * @author Costin Manolache
063: */
064: public class BaseDirContext extends BaseContext implements DirContext {
065:
066: public BaseDirContext() {
067: super ();
068: }
069:
070: public BaseDirContext(Hashtable env) {
071: super (env);
072: }
073:
074: // ----------------------------------------------------- DirContext Methods
075:
076: /**
077: * Retrieves all of the attributes associated with a named object.
078: *
079: * @return the set of attributes associated with name.
080: * Returns an empty attribute set if name has no attributes; never null.
081: * @param name the name of the object from which to retrieve attributes
082: * @exception javax.naming.NamingException if a naming exception is encountered
083: */
084: public Attributes getAttributes(Name name) throws NamingException {
085: return getAttributes(name, null);
086: }
087:
088: /**
089: * Retrieves all of the attributes associated with a named object.
090: *
091: * @return the set of attributes associated with name
092: * @param name the name of the object from which to retrieve attributes
093: * @exception javax.naming.NamingException if a naming exception is encountered
094: */
095: public Attributes getAttributes(String name) throws NamingException {
096: return getAttributes(string2Name(name));
097: }
098:
099: /**
100: * Retrieves selected attributes associated with a named object.
101: * See the class description regarding attribute models, attribute type
102: * names, and operational attributes.
103: *
104: * @return the requested attributes; never null
105: * @param name the name of the object from which to retrieve attributes
106: * @param attrIds the identifiers of the attributes to retrieve. null
107: * indicates that all attributes should be retrieved; an empty array
108: * indicates that none should be retrieved
109: * @exception javax.naming.NamingException if a naming exception is encountered
110: */
111: public Attributes getAttributes(String name, String[] attrIds)
112: throws NamingException {
113: return getAttributes(string2Name(name), attrIds);
114: }
115:
116: public Attributes getAttributes(Name name, String[] attrIds)
117: throws NamingException {
118: if (attrIds == null) {
119: attrIds = super .getAttributeNames(name);
120: }
121: Attributes res = new ServerAttributes();
122: if (attrIds == null)
123: return res;
124:
125: for (int i = 0; i < attrIds.length; i++) {
126: Object val = super .getAttribute(name, attrIds[i]);
127: res.put(attrIds[i], val);
128: }
129: return res;
130: }
131:
132: /**
133: * Modifies the attributes associated with a named object. The order of
134: * the modifications is not specified. Where possible, the modifications
135: * are performed atomically.
136: *
137: * @param name the name of the object whose attributes will be updated
138: * @param mod_op the modification operation, one of: ADD_ATTRIBUTE,
139: * REPLACE_ATTRIBUTE, REMOVE_ATTRIBUTE
140: * @param attrs the attributes to be used for the modification; may not
141: * be null
142: * @exception AttributeModificationException if the modification cannot be
143: * completed successfully
144: * @exception javax.naming.NamingException if a naming exception is encountered
145: */
146: public void modifyAttributes(Name name, int mod_op, Attributes attrs)
147: throws NamingException
148: {
149: NamingEnumeration enum=attrs.getAll();
150: while( enum.hasMoreElements() ) {
151: Attribute att=(Attribute)enum.nextElement();
152: switch( mod_op ) {
153: case ADD_ATTRIBUTE:
154: case REPLACE_ATTRIBUTE:
155: for( int i=0; i< att.size(); i++ ) {
156: super .setAttribute(name, att.getID(), att.get(i));
157: }
158: break;
159: case REMOVE_ATTRIBUTE:
160: break;
161: }
162: }
163: }
164:
165: public void modifyAttributes(String name, int mod_op,
166: Attributes attrs) throws NamingException {
167: modifyAttributes(string2Name(name), mod_op, attrs);
168: }
169:
170: /**
171: * Modifies the attributes associated with a named object using an an
172: * ordered list of modifications. The modifications are performed in the
173: * order specified. Each modification specifies a modification operation
174: * code and an attribute on which to operate. Where possible, the
175: * modifications are performed atomically.
176: *
177: * @param name the name of the object whose attributes will be updated
178: * @param mods an ordered sequence of modifications to be performed; may
179: * not be null
180: * @exception AttributeModificationException if the modification cannot be
181: * completed successfully
182: * @exception javax.naming.NamingException if a naming exception is encountered
183: */
184: public void modifyAttributes(Name name, ModificationItem[] mods)
185: throws NamingException {
186: if (mods == null)
187: return;
188: for (int i = 0; i < mods.length; i++) {
189:
190: switch (mods[i].getModificationOp()) {
191: case ADD_ATTRIBUTE:
192: case REPLACE_ATTRIBUTE:
193: case REMOVE_ATTRIBUTE:
194: }
195: ;
196: }
197: }
198:
199: public void modifyAttributes(String name, ModificationItem[] mods)
200: throws NamingException {
201: modifyAttributes(string2Name(name), mods);
202: }
203:
204: /**
205: * Binds a name to an object, along with associated attributes. If attrs
206: * is null, the resulting binding will have the attributes associated
207: * with obj if obj is a DirContext, and no attributes otherwise. If attrs
208: * is non-null, the resulting binding will have attrs as its attributes;
209: * any attributes associated with obj are ignored.
210: *
211: * @param name the name to bind; may not be empty
212: * @param obj the object to bind; possibly null
213: * @param attrs the attributes to associate with the binding
214: * @exception javax.naming.NameAlreadyBoundException if name is already bound
215: * @exception InvalidAttributesException if some "mandatory" attributes
216: * of the binding are not supplied
217: * @exception javax.naming.NamingException if a naming exception is encountered
218: */
219: public void bind(Name name, Object obj, Attributes attrs)
220: throws NamingException
221: {
222: super .bind( name, obj );
223:
224: NamingEnumeration enum=attrs.getAll();
225: while( enum.hasMoreElements() ) {
226: Attribute att=(Attribute)enum.nextElement();
227:
228: Object val=getAttribute(name, att.getID() );
229: if( val != null ) {
230: throw new NameAlreadyBoundException(name.toString() + " : " +
231: att.getID());
232: }
233:
234: int size=att.size();
235: for( int i=0; i<size; i++ ) {
236: // probably need some addAttribute
237: setAttribute( name, att.getID(), att.get(i));
238: }
239: }
240: }
241:
242: public void bind(String name, Object obj, Attributes attrs)
243: throws NamingException {
244: bind(string2Name(name), obj, attrs);
245: }
246:
247: /**
248: * Binds a name to an object, along with associated attributes,
249: * overwriting any existing binding. If attrs is null and obj is a
250: * DirContext, the attributes from obj are used. If attrs is null and obj
251: * is not a DirContext, any existing attributes associated with the object
252: * already bound in the directory remain unchanged. If attrs is non-null,
253: * any existing attributes associated with the object already bound in
254: * the directory are removed and attrs is associated with the named
255: * object. If obj is a DirContext and attrs is non-null, the attributes
256: * of obj are ignored.
257: *
258: * @param name the name to bind; may not be empty
259: * @param obj the object to bind; possibly null
260: * @param attrs the attributes to associate with the binding
261: * @exception InvalidAttributesException if some "mandatory" attributes
262: * of the binding are not supplied
263: * @exception javax.naming.NamingException if a naming exception is encountered
264: */
265: public void rebind(Name name, Object obj, Attributes attrs)
266: throws NamingException {
267: bind(name, obj, attrs, true);
268: }
269:
270: public void rebind(String name, Object obj, Attributes attrs)
271: throws NamingException {
272: bind(string2Name(name), obj, attrs, true);
273: }
274:
275: /**
276: * Creates and binds a new context, along with associated attributes.
277: * This method creates a new subcontext with the given name, binds it in
278: * the target context (that named by all but terminal atomic component of
279: * the name), and associates the supplied attributes with the newly
280: * created object. All intermediate and target contexts must already
281: * exist. If attrs is null, this method is equivalent to
282: * Context.createSubcontext().
283: *
284: * @param name the name of the context to create; may not be empty
285: * @param attrs the attributes to associate with the newly created context
286: * @return the newly created context
287: * @exception javax.naming.NameAlreadyBoundException if the name is already bound
288: * @exception InvalidAttributesException if attrs does not contain all
289: * the mandatory attributes required for creation
290: * @exception javax.naming.NamingException if a naming exception is encountered
291: */
292: public DirContext createSubcontext(String name, Attributes attrs)
293: throws NamingException {
294: return createSubcontext(string2Name(name), attrs);
295: }
296:
297: /**
298: * Retrieves the schema associated with the named object. The schema
299: * describes rules regarding the structure of the namespace and the
300: * attributes stored within it. The schema specifies what types of
301: * objects can be added to the directory and where they can be added;
302: * what mandatory and optional attributes an object can have. The range
303: * of support for schemas is directory-specific.
304: *
305: * @param name the name of the object whose schema is to be retrieved
306: * @return the schema associated with the context; never null
307: * @exception javax.naming.OperationNotSupportedException if schema not supported
308: * @exception javax.naming.NamingException if a naming exception is encountered
309: */
310: public DirContext getSchema(Name name) throws NamingException {
311: return getSchema(name.toString());
312: }
313:
314: /**
315: * Retrieves the schema associated with the named object.
316: *
317: * @param name the name of the object whose schema is to be retrieved
318: * @return the schema associated with the context; never null
319: * @exception javax.naming.OperationNotSupportedException if schema not supported
320: * @exception javax.naming.NamingException if a naming exception is encountered
321: */
322: public DirContext getSchema(String name) throws NamingException {
323: throw new OperationNotSupportedException();
324: }
325:
326: /**
327: * Retrieves a context containing the schema objects of the named
328: * object's class definitions.
329: *
330: * @param name the name of the object whose object class definition is to
331: * be retrieved
332: * @return the DirContext containing the named object's class
333: * definitions; never null
334: * @exception javax.naming.OperationNotSupportedException if schema not supported
335: * @exception javax.naming.NamingException if a naming exception is encountered
336: */
337: public DirContext getSchemaClassDefinition(Name name)
338: throws NamingException {
339: return getSchemaClassDefinition(name.toString());
340: }
341:
342: /**
343: * Retrieves a context containing the schema objects of the named
344: * object's class definitions.
345: *
346: * @param name the name of the object whose object class definition is to
347: * be retrieved
348: * @return the DirContext containing the named object's class
349: * definitions; never null
350: * @exception javax.naming.OperationNotSupportedException if schema not supported
351: * @exception javax.naming.NamingException if a naming exception is encountered
352: */
353: public DirContext getSchemaClassDefinition(String name)
354: throws NamingException {
355: throw new OperationNotSupportedException();
356: }
357:
358: /**
359: * Searches in a single context for objects that contain a specified set
360: * of attributes, and retrieves selected attributes. The search is
361: * performed using the default SearchControls settings.
362: *
363: * @param name the name of the context to search
364: * @param matchingAttributes the attributes to search for. If empty or
365: * null, all objects in the target context are returned.
366: * @param attributesToReturn the attributes to return. null indicates
367: * that all attributes are to be returned; an empty array indicates that
368: * none are to be returned.
369: * @return a non-null enumeration of SearchResult objects. Each
370: * SearchResult contains the attributes identified by attributesToReturn
371: * and the name of the corresponding object, named relative to the
372: * context named by name.
373: * @exception javax.naming.NamingException if a naming exception is encountered
374: */
375: public NamingEnumeration search(Name name,
376: Attributes matchingAttributes, String[] attributesToReturn)
377: throws NamingException {
378: return search(name.toString(), matchingAttributes,
379: attributesToReturn);
380: }
381:
382: /**
383: * Searches in a single context for objects that contain a specified set
384: * of attributes, and retrieves selected attributes.
385: *
386: * @param name the name of the context to search
387: * @param matchingAttributes the attributes to search for. If empty or
388: * null, all objects in the target context are returned.
389: * @param attributesToReturn the attributes to return. null indicates
390: * that all attributes are to be returned; an empty array indicates that
391: * none are to be returned.
392: * @return a non-null enumeration of SearchResult objects. Each
393: * SearchResult contains the attributes identified by attributesToReturn
394: * and the name of the corresponding object, named relative to the
395: * context named by name.
396: * @exception javax.naming.NamingException if a naming exception is encountered
397: */
398: public NamingEnumeration search(String name,
399: Attributes matchingAttributes, String[] attributesToReturn)
400: throws NamingException {
401: throw new OperationNotSupportedException();
402: }
403:
404: /**
405: * Searches in a single context for objects that contain a specified set
406: * of attributes. This method returns all the attributes of such objects.
407: * It is equivalent to supplying null as the atributesToReturn parameter
408: * to the method search(Name, Attributes, String[]).
409: *
410: * @param name the name of the context to search
411: * @param matchingAttributes the attributes to search for. If empty or
412: * null, all objects in the target context are returned.
413: * @return a non-null enumeration of SearchResult objects. Each
414: * SearchResult contains the attributes identified by attributesToReturn
415: * and the name of the corresponding object, named relative to the
416: * context named by name.
417: * @exception javax.naming.NamingException if a naming exception is encountered
418: */
419: public NamingEnumeration search(Name name,
420: Attributes matchingAttributes) throws NamingException {
421: return search(name.toString(), matchingAttributes);
422: }
423:
424: /**
425: * Searches in a single context for objects that contain a specified set
426: * of attributes.
427: *
428: * @param name the name of the context to search
429: * @param matchingAttributes the attributes to search for. If empty or
430: * null, all objects in the target context are returned.
431: * @return a non-null enumeration of SearchResult objects. Each
432: * SearchResult contains the attributes identified by attributesToReturn
433: * and the name of the corresponding object, named relative to the
434: * context named by name.
435: * @exception javax.naming.NamingException if a naming exception is encountered
436: */
437: public NamingEnumeration search(String name,
438: Attributes matchingAttributes) throws NamingException {
439: throw new OperationNotSupportedException();
440: }
441:
442: /**
443: * Searches in the named context or object for entries that satisfy the
444: * given search filter. Performs the search as specified by the search
445: * controls.
446: *
447: * @param name the name of the context or object to search
448: * @param filter the filter expression to use for the search; may not be
449: * null
450: * @param cons the search controls that control the search. If null,
451: * the default search controls are used (equivalent to
452: * (new SearchControls())).
453: * @return an enumeration of SearchResults of the objects that satisfy
454: * the filter; never null
455: * @exception InvalidSearchFilterException if the search filter specified
456: * is not supported or understood by the underlying directory
457: * @exception InvalidSearchControlsException if the search controls
458: * contain invalid settings
459: * @exception javax.naming.NamingException if a naming exception is encountered
460: */
461: public NamingEnumeration search(Name name, String filter,
462: SearchControls cons) throws NamingException {
463: return search(name.toString(), filter, cons);
464: }
465:
466: /**
467: * Searches in the named context or object for entries that satisfy the
468: * given search filter. Performs the search as specified by the search
469: * controls.
470: *
471: * @param name the name of the context or object to search
472: * @param filter the filter expression to use for the search; may not be
473: * null
474: * @param cons the search controls that control the search. If null,
475: * the default search controls are used (equivalent to
476: * (new SearchControls())).
477: * @return an enumeration of SearchResults of the objects that satisfy
478: * the filter; never null
479: * @exception InvalidSearchFilterException if the search filter
480: * specified is not supported or understood by the underlying directory
481: * @exception InvalidSearchControlsException if the search controls
482: * contain invalid settings
483: * @exception javax.naming.NamingException if a naming exception is encountered
484: */
485: public NamingEnumeration search(String name, String filter,
486: SearchControls cons) throws NamingException {
487: throw new OperationNotSupportedException();
488: }
489:
490: /**
491: * Searches in the named context or object for entries that satisfy the
492: * given search filter. Performs the search as specified by the search
493: * controls.
494: *
495: * @param name the name of the context or object to search
496: * @param filterExpr the filter expression to use for the search.
497: * The expression may contain variables of the form "{i}" where i is a
498: * nonnegative integer. May not be null.
499: * @param filterArgs the array of arguments to substitute for the
500: * variables in filterExpr. The value of filterArgs[i] will replace each
501: * occurrence of "{i}". If null, equivalent to an empty array.
502: * @param cons the search controls that control the search. If null, the
503: * default search controls are used (equivalent to (new SearchControls())).
504: * @return an enumeration of SearchResults of the objects that satisy the
505: * filter; never null
506: * @exception java.lang.ArrayIndexOutOfBoundsException if filterExpr contains {i}
507: * expressions where i is outside the bounds of the array filterArgs
508: * @exception InvalidSearchControlsException if cons contains invalid
509: * settings
510: * @exception InvalidSearchFilterException if filterExpr with filterArgs
511: * represents an invalid search filter
512: * @exception javax.naming.NamingException if a naming exception is encountered
513: */
514: public NamingEnumeration search(Name name, String filterExpr,
515: Object[] filterArgs, SearchControls cons)
516: throws NamingException {
517: return search(name.toString(), filterExpr, filterArgs, cons);
518: }
519:
520: /**
521: * Searches in the named context or object for entries that satisfy the
522: * given search filter. Performs the search as specified by the search
523: * controls.
524: *
525: * @param name the name of the context or object to search
526: * @param filterExpr the filter expression to use for the search.
527: * The expression may contain variables of the form "{i}" where i is a
528: * nonnegative integer. May not be null.
529: * @param filterArgs the array of arguments to substitute for the
530: * variables in filterExpr. The value of filterArgs[i] will replace each
531: * occurrence of "{i}". If null, equivalent to an empty array.
532: * @param cons the search controls that control the search. If null, the
533: * default search controls are used (equivalent to (new SearchControls())).
534: * @return an enumeration of SearchResults of the objects that satisy the
535: * filter; never null
536: * @exception java.lang.ArrayIndexOutOfBoundsException if filterExpr contains {i}
537: * expressions where i is outside the bounds of the array filterArgs
538: * @exception InvalidSearchControlsException if cons contains invalid
539: * settings
540: * @exception InvalidSearchFilterException if filterExpr with filterArgs
541: * represents an invalid search filter
542: * @exception javax.naming.NamingException if a naming exception is encountered
543: */
544: public NamingEnumeration search(String name, String filterExpr,
545: Object[] filterArgs, SearchControls cons)
546: throws NamingException {
547: throw new OperationNotSupportedException();
548: }
549:
550: }
|