KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > broker > SchemaBroker


1 package com.ca.directory.jxplorer.broker;
2
3 import javax.naming.*;
4 import javax.naming.directory.*;
5
6 import com.ca.directory.jxplorer.*;
7 //import com.ca.directory.jxplorer.tree.*;
8
import com.ca.commons.naming.*;
9 import com.ca.commons.jndi.SchemaOps;
10
11 import java.util.*;
12 import java.util.logging.Logger JavaDoc;
13
14 /**
15  * This wraps the schemaOps searching functionality of a
16  * JNDIBroker, and makes it work as a DataSource
17  * so that a SmartTree object can use it to browse the
18  * schemaOps.
19  */

20  
21 public class SchemaBroker extends Broker
22 {
23     JNDIBroker jndiBroker;
24     
25     SchemaOps schemaOps;
26
27     private final static Logger JavaDoc log = Logger.getLogger(SchemaBroker.class.getName());
28
29     public SchemaBroker(JNDIBroker jb)
30     {
31         registerDirectoryConnection(jb);
32     }
33     
34     public void registerDirectoryConnection(JNDIBroker broker)
35     {
36         jndiBroker = broker;
37         schemaOps = jndiBroker.getSchemaOps();
38     }
39
40     /**
41      * Takes a DXAttributes set representing class schemaOps defs,
42      * and translates the oids into human friendly strings...
43      */

44 /*
45     protected Attributes addClassInfo(Attributes classdefs)
46     {
47         if ((classdefs instanceof DXAttributes) && ((DXAttributes)classdefs).hasOIDs() == false) // there are no OIDs requiring translation - we must be using ldap strings instead
48         {
49             return classdefs;
50         }
51         
52         try
53         {
54             Attribute must = classdefs.get("MUST"); // get syntax attribute
55             if (must != null)
56             {
57                 DXNamingEnumeration musts = new DXNamingEnumeration(must.getAll());
58                 BasicAttribute mustDesc = new BasicAttribute("(MUST-DESC)");
59                 while (musts.hasMore())
60                     mustDesc.add(getAttributeDescription(musts.next().toString()));
61                 classdefs.put(mustDesc);
62             }
63             
64             Attribute may = classdefs.get("MAY");
65             if (may != null)
66             {
67                 DXNamingEnumeration mays = new DXNamingEnumeration(may.getAll());
68                 BasicAttribute mayDesc = new BasicAttribute("(MAY-DESC)");
69                 while (mays.hasMore())
70                     mayDesc.add(getAttributeDescription(mays.next().toString()));
71                 classdefs.put(mayDesc);
72             }
73
74             return classdefs;
75         }
76         catch (NamingException e) { return classdefs; }
77         catch (NullPointerException e) { return classdefs; }
78     }
79 */

80 /*
81     public DataQuery getAttributeDefinition(String attributeoid)
82     {
83         return getEntry(new DN("schemaOps="+attributeoid + ",schemaOps=AttributeDefinition"));
84     }
85 */

86      /**
87       * Convenience Class; makes a request for the
88       * passed value, appending 'AttributeDefinition' to the dn.
89       * @param attributeoid the name of the attribute to get info for
90       * @return an Attributes object containing the (server
91       * dependant) available data...
92       */

93 /*
94     public String getAttributeDescription(String attributeoid)
95     {
96          
97         return jndiBroker.getAttributeDescription(attributeoid) ; //+ " (" + attributeoid + ")";
98     }
99 */

100      /**
101       * Convenience Class; makes a request for the
102       * passed syntax definition.
103       * @param syntaxName the name of the syntax to get info for
104       * @return an Attributes object containing the (server
105       * dependant) available data...
106       */

107 /*
108     public DataQuery getSyntaxDefinition (String syntaxName)
109     {
110         if (syntaxName.indexOf('{') > -1)
111             syntaxName = syntaxName.substring(0, syntaxName.indexOf('{'));
112
113         return getEntry(new DN("schemaOps="+syntaxName + ",schemaOps=SyntaxDefinition,cn=schemaOps"));
114     }
115 */

116      /**
117       * Convenience Class; makes a request for the
118       * passed value, appending 'SyntaxDefinition' to the dn.
119       * Looks for a 'DESC' field in the reTranslates the resulting
120       * @param syntaxName the name of the syntax to get info for
121       * @return an Attributes object containing the (server
122       * dependant) available data...
123       */

124 /*
125     public String getSyntaxDescription (String syntaxName)
126     {
127         try
128         {
129             //System.out.println("looking up " + "schemaOps="+syntaxName + ",schemaOps=SyntaxDefinition,cn=schemaOps");
130             
131             // try hard coded list first, from RFC .
132              
133             String oidName = null;
134              
135              oidName = (String)syntaxHash.get(syntaxName); // try quick look up
136              if (oidName == null) // ... fail to a directory query.
137              {
138                  Attributes syntaxdefs = getEntry(new DN("schemaOps="+syntaxName + ",schemaOps=SyntaxDefinition,cn=schemaOps")).getEntry();
139                  oidName = syntaxdefs.get("DESC").get().toString();
140
141
142                  if (oidName != null)
143                      syntaxHash.put(syntaxName, oidName); // and add it to the hash for later... :-)
144              }
145              return oidName;
146          }
147          catch (NullPointerException e) { return null; }
148          catch (NamingException e2) { return null; }
149      }
150 */

151
152      /**
153       * Convenience Class; makes a request for the
154       * passed value, appending 'ClassDefinition' to the dn.
155       * @param className the name of the class to get info for
156       * @return an Attributes object containing the (server
157       * dependant) available data...
158       */

159 /*
160      public DataQuery getClassDefinition (String className)
161      {
162          return getEntry(new DN("schemaOps="+className + ",schemaOps=ClassDefinition"));
163      }
164 */

165
166 /*
167     protected void loadSyntaxHash()
168     {
169          // a quick pick of common syntaxes for Active Directory support
170          // (and other servers that don't publish syntax descriptions)
171          // taken from rfc 2252
172         syntaxHash = new Hashtable(70);
173
174         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.1","ACI Item");
175         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.2","Access Point");
176         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.3","Attribute Type Description");
177         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.4","Audio");
178         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.5","Binary");
179         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.6","Bit String");
180         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.7","Boolean");
181         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.8","Certificate");
182         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.9","Certificate List");
183         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.10","Certificate Pair");
184         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.11","Country String");
185         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.12","DN");
186         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.13","Data Quality Syntax");
187         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.14","Delivery Method");
188         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.15","Directory String");
189         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.16","DIT Content Rule Description");
190         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.17","DIT Structure Rule Description");
191         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.18","DL Submit Permission");
192         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.19","DSA Quality Syntax");
193         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.20","DSE Type");
194         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.21","Enhanced Guide");
195         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.22","Facsimile Telephone Number");
196         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.23","Fax");
197         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.24","Generalized Time");
198         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.25","Guide");
199         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.26","IA5 String");
200         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.27","INTEGER");
201         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.28","JPEG");
202         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.54","LDAP Syntax Description");
203         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.56","LDAP Schema Definition");
204         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.57","LDAP Schema Description");
205         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.29","Master And Shadow Access Points");
206         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.30","Matching Rule Description");
207         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.31","Matching Rule Use Description");
208         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.32","Mail Preference");
209         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.33","MHS OR Address");
210         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.55","Modify Rights");
211         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.34","Name And Optional UID");
212         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.35","Name Form Description");
213         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.36","Numeric String");
214         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.37","Object Class Description");
215         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.40","Octet String");
216         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.38","OID");
217         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.39","Other Mailbox");
218         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.41","Postal Address");
219         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.42","Protocol Information");
220         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.43","Presentation Address");
221         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.44","Printable String");
222         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.58","Substring Assertion");
223         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.45","Subtree Specification");
224         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.46","Supplier Information");
225         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.47","Supplier Or Consumer");
226         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.48","Supplier And Consumer");
227         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.49","Supported Algorithm");
228         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.50","Telephone Number");
229         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.51","Teletex Terminal Identifier");
230         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.52","Telex Number");
231         syntaxHash.put("1.3.6.1.4.1.1466.115.121.1.53","UTC Time");
232      }
233 */

234      //temp
235
/*
236      public static boolean isAttributeBinary(String attID)
237      {
238          return false;
239      }
240 */

241 // public DataSource getDataSource() { return null; } // can't modify schemaOps data...
242

243
244      
245     /**
246      * Operation is not allowed - sets an exception in the request
247      */

248      
249     public DataQuery doExistsQuery(DataQuery request)
250     {
251         return request.setException(new Exception JavaDoc("schemaOps exists not yet implemented")); //XXX
252
}
253
254      
255     protected DataQuery doSearchQuery(DataQuery request)
256     {
257         return request.setException(new Exception JavaDoc("schemaOps search not allowed"));
258     }
259                 
260     /**
261      * Operation is not allowed - sets an exception in the request
262      */

263      
264     protected DataQuery doModifyQuery(DataQuery request)
265     {
266         return request.setException(new Exception JavaDoc("schemaOps modification not allowed"));
267     }
268                 
269     /**
270      * Operation is not allowed - sets an exception in the request
271      */

272      
273     protected DataQuery doCopyQuery(DataQuery request)
274     {
275         return request.setException(new Exception JavaDoc("schemaOps copy not allowed"));
276     }
277                 
278     /**
279      * Operation is not allowed - sets an exception in the request
280      */

281      
282     protected DataQuery doGetAllOCsQuery(DataQuery request)
283     {
284         return request.setException(new Exception JavaDoc("schemaOps object class list not allowed"));
285     }
286                 
287     /**
288      * Operation is not allowed - sets an exception in the request
289      */

290      
291     protected DataQuery doGetRecOCsQuery(DataQuery request)
292     {
293         return request.setException(new Exception JavaDoc("schemaOps rec. object class list not allowed"));
294     }
295     
296     
297     /**
298      * returns the next level of a directory tree, returning
299      * a Enumeration of the results
300      *
301      * @param searchbase the node in the tree to expand
302      * @return list of results (NameClassPair); the next layer of the tree...
303      */

304      
305     public DXNamingEnumeration unthreadedList(DN searchbase)
306     {
307         try
308         {
309             if (schemaOps == null) // not initialised.
310
return null;
311
312             ArrayList nextLevel = schemaOps.listEntryNames(searchbase.toString());
313             int size = nextLevel.size();
314             for (int i=0; i<size; i++)
315             {
316                 nextLevel.set(i, new NameClassPair("schema="+ (String JavaDoc)nextLevel.get(i),"schema", false));
317             }
318             return new DXNamingEnumeration(nextLevel);
319         }
320         catch (NamingException e)
321         {
322             System.out.println("hurm.");
323             return null;
324         }
325     }
326     
327     /**
328      * Not Implemented.
329      *
330      * @param dn the domain name (relative to initial context in ldap) to seach from.
331      * @param filter the non-null filter to use for the search
332      * @param search_level whether to search the base object, the next level or the whole subtree.
333      * @param returnAttributes a vector of string names of attributes to return in the search. (Currently inoperative)
334      * @return list of results ('SearchResult's); the next layer of the tree...
335      */

336      
337     public DXNamingEnumeration unthreadedSearch(DN dn, String JavaDoc filter, int search_level, String JavaDoc[] returnAttributes) { return null; }
338     
339    /**
340      * Not Implemented.
341     *
342     * @param oldNodeDN the original DN of the sub tree root
343     * to be copied (may be a single entry).
344     * @param newNodeDN the target DN for the tree to be moved to.
345     */

346     public void unthreadedCopy(DN oldNodeDN, DN newNodeDN)
347         throws NamingException
348    {
349        throw new NamingException("unable to modify schema");
350    }
351
352     /**
353      * Not Implemented.
354      * @return whether the entry could be found in the directory.
355      */

356      
357     public boolean unthreadedExists(DN checkMe) {return false;}
358     
359     /**
360      * Not Implemented.
361      */

362
363     public Vector unthreadedGetAllOCs() { return null; }
364
365     /**
366      * Reads a 'virtual' schemaOps entry. (i.e. reads a schemaOps attribute value, parsing and
367      * presenting the information as if it were an entry in its own right).
368      * @param entryDN the DN of the object to read.
369      * @param returnAttributes a vector of string names of attributes to return in the search.
370      * (null means 'return all entries', a zero length array means 'return no attributes'.)
371      * @return an entry returning a schemaOps group; null if unreadable.
372      */

373      
374     public DXEntry unthreadedReadEntry(DN entryDN, String JavaDoc[] returnAttributes)
375         throws NamingException
376     {
377             if (schemaOps == null) // not initialised.
378
return null;
379
380             DXEntry returnEntry = new DXEntry(entryDN);
381
382             Attributes atts = schemaOps.getAttributes(entryDN.toString());
383
384             if (atts == null || atts.size() == 0)
385             {
386                 returnEntry = null; // no result
387
}
388             else if (returnAttributes == null)
389             {
390                 returnEntry.put(atts.getAll()); // return the complete schemaOps entry
391
}
392             else if (returnAttributes.length == 0)
393             {
394                                                    // do nothing - just return the named, empty, DXEntry object
395
}
396             else // XXX - may need to be implemented at some stage...
397
{
398                 log.warning("return of partial schemaOps attributes not implemented - returning all");
399                 returnEntry.put(atts.getAll());
400             }
401
402             return returnEntry;
403 /*
404         Attributes temp;
405
406         // add a synthetic schemaOps object class, so that it looks and acts like a normal
407         // directory entry.
408         BasicAttribute oc = new BasicAttribute("objectClass", "top");
409         oc.add("schemaOps");
410
411         temp = jndiBroker.getSchemaData(entryDN, returnAttributes);
412
413         if (temp == null)
414         {
415             System.err.println("unable to read schemaOps; null attributes returned");
416             return null;
417         }
418
419         if (useRawData == false)
420         {
421             // Do some translation work to make things pretty for users.
422             String type = entryDN.getRDNValue(1);
423             oc.add(type);
424
425             if (type.equals("AttributeDefinition"))
426                 temp = addAttributeInfo(temp);
427             else if (type.equals("ClassDefinition"))
428                 temp = addClassInfo(temp);
429     // else if (type.equals("SyntaxDefinition"))
430     // temp = addSyntaxInfo(temp);
431             temp.put(oc);
432
433         }
434
435         return new DXEntry(temp, entryDN);
436 */

437     }
438
439    /**
440      * Not Implemented.
441     * @param oldEntry the old set of attributes of the object.
442     * @param newEntry the replacement set of attributes..
443     */

444     
445     public void unthreadedModify(DXEntry oldEntry, DXEntry newEntry)
446            throws NamingException
447       {
448           throw new NamingException("unable to modify schema");
449       }
450
451     /**
452      * Not Implemented.
453      * @param dn the dn of the parent to determine likely
454      * child object classes for
455      * @return list of recommended object classes...
456      */

457     
458     public ArrayList unthreadedGetRecOCs(DN dn) { return null; }
459     
460     
461     
462     
463     
464     
465     
466     
467     
468     
469     
470     
471     
472     /**
473      * Chains request to jndiBroker to satisfy broker interface
474      */

475      
476     public DirContext getDirContext() { return jndiBroker.getDirContext(); }
477  
478     /**
479      * Whether the schemaOps is modifiable - usually false.
480      */

481      
482     public boolean isModifiable() { return false; }
483     
484     /**
485      * The schemaOps Broker is always active (even if there is no connection)
486      * because it will fall back on hard-coded default values if necessary.
487      */

488      
489     public boolean isActive() { return true; }
490     
491     /**
492      * Chains request to jndiBroker to satisfy broker interface
493      */

494      
495     public SchemaOps getSchemaOps() { return schemaOps; }
496     
497     
498 }
Popular Tags