Source Code Cross Referenced for AnyNamingContext.java in  » Web-Framework » anvil » anvil » core » naming » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » anvil » anvil.core.naming 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:/*
002: * $Id: AnyNamingContext.java,v 1.13 2002/09/16 08:05:03 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE 
008: * file, or http://njet.org/license-1.1.txt
009: */
010:package anvil.core.naming; 
011:
012:import anvil.Log;
013:import anvil.database.PooledConnection;
014:import anvil.core.Any;
015:import anvil.core.AnyList;
016:import anvil.core.AnyMap;
017:import anvil.core.AnySequence;
018:import anvil.core.AnyUtils;
019:import anvil.core.AnyAbstractClass;
020:import anvil.core.AnyBindingEnumeration;
021:import anvil.java.util.BindingEnumeration;
022:import javax.naming.NameClassPair;
023:import javax.naming.NamingEnumeration;
024:import javax.naming.Context;
025:import javax.naming.Name;
026:import javax.naming.NamingException;
027:import javax.naming.directory.DirContext;
028:import javax.naming.directory.Attribute;
029:import javax.naming.directory.Attributes;
030:import javax.naming.directory.SearchControls;
031:import javax.naming.directory.ModificationItem;
032:
033:/// @class NamingContext
034:
035:/**
036: * class AnyNamingContext
037: *
038: * @author: Simo Tuokko
039: * @author: Jani Lehtimäki
040: */
041:public class AnyNamingContext extends AnyAbstractClass
042:{
043:
044:  private ModificationItem buildItem(Any item)
045:  {
046:    if (item.isMap()) {
047:      AnyMap map = item.toMap();
048:      int modop = map.getLeft().toInt();
049:      Any right = map.getRight();
050:      if (right instanceof  AnyAttribute) {
051:        Attribute attr = (Attribute)right.toObject();
052:        return new ModificationItem(modop, attr);
053:      }
054:    } else {
055:      if (item instanceof  AnyAttribute) {
056:        Attribute attr = (Attribute)item.toObject();
057:        return new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);
058:      }
059:    }
060:    return null;
061:  }
062:  
063:
064:  public static final Any ADD = Any.create(DirContext.ADD_ATTRIBUTE);
065:  public static final Any REPLACE = Any.create(DirContext.REPLACE_ATTRIBUTE);
066:  public static final Any REMOVE = Any.create(DirContext.REMOVE_ATTRIBUTE);
067:
068:  public static final anvil.script.compiler.NativeClass __class__ = 
069:    new anvil.script.compiler.NativeClass("NamingContext", AnyNamingContext.class,
070:    //DOC{{
071:    ""+
072:      " @class NamingContext\n" +
073:      " @method getName\n" +
074:      " @synopsis string getName()\n" +
075:      " @method list\n" +
076:      " @synopsis list list(string name)\n" +
077:      " @synopsis list list(list name)\n" +
078:      " Return list of SearchResult objects.\n" +
079:      "\n" +
080:      " Example.\n" +
081:      " ctx = anvil.naming.acquire('ldap_pool');\n" +
082:      " foreach (sr; anvil.naming.list('')) {\n" +
083:      "   println \"Found: \", sr.getName();\n" +
084:      " }\n" +
085:      " @method lookup\n" +
086:      " @synopsis object lookup(string name)\n" +
087:      " @synopsis object lookup(list name)\n" +
088:      " @method lookupLink\n" +
089:      " @synopsis object lookupLink(string name)\n" +
090:      " @synopsis object lookupLink(list name)\n" +
091:      " @method getNameParser\n" +
092:      " @synopsis NameParser getNameParser(string name)\n" +
093:      " @synopsis NameParser getNameParser(list name)\n" +
094:      " @method parse\n" +
095:      " @synopsis list parse(string name, string child)\n" +
096:      " @synopsis list parse(list name, string child)\n" +
097:      " @method compose\n" +
098:      " @synopsis list compose(string name, string prefix)\n" +
099:      " @synopsis list compose(list name, list prefix)\n" +
100:      " @method bind\n" +
101:      " @synopsis Context bind(string name, object value [, Attributes attributes] )\n" +
102:      " @synopsis Context bind(list name, object value [, Attributes attributes] )\n" +
103:      " @method rebind\n" +
104:      " @synopsis Context rebind(string name, object value [, Attributes attributes] )\n" +
105:      " @synopsis Context rebind(list name, object value [, Attributes attributes] )\n" +
106:      " @method unbind\n" +
107:      " @synopsis Context unbind(string name)\n" +
108:      " @synopsis Context unbind(list name)\n" +
109:      " @method destroy\n" +
110:      " @synopsis Context destroy(string name)\n" +
111:      " @synopsis Context destroy(list name)\n" +
112:      " @method create\n" +
113:      " @synopsis Context create(string name [, Attributes attributes ])\n" +
114:      " @synopsis Context create(list name [, Attributes attributes ])\n" +
115:      " @method rename\n" +
116:      " @synopsis Context rename(string fromname, string toname)\n" +
117:      " @synopsis Context rename(list fromname, list toname)\n" +
118:      " @method close\n" +
119:      " @synopsis void close()\n" +
120:      " @method release\n" +
121:      " @synopsis void release()\n" +
122:      " @method getAttributes\n" +
123:      " @synopsis Attributes getAttributes()\n" +
124:      " @synopsis Attributes getAttributes(string name)\n" +
125:      " @synopsis Attributes getAttributes(name name)\n" +
126:      " @synopsis Attributes getAttributes(string name, sequence attrsToReturn)\n" +
127:      " @synopsis Attributes getAttributes(name name, sequence attrsToReturn)\n" +
128:      " @method modifyAttributes\n" +
129:      " @synopsis Context modifyAttributes(Attributes attrs)\n" +
130:      " @synopsis Context modifyAttributes(Attributes attrs)\n" +
131:      " @synopsis Context modifyAttributes(map mod, ...)\n" +
132:      " @synopsis Context modifyAttributes(map mod, ...)\n" +
133:      " @synopsis Context modifyAttributes(string name, Attributes attrs)\n" +
134:      " @synopsis Context modifyAttributes(Name name, Attributes attrs)\n" +
135:      " @synopsis Context modifyAttributes(string name, int modop, Attributes attrs)\n" +
136:      " @synopsis Context modifyAttributes(Name name, int modop, Attributes attrs)\n" +
137:      " @synopsis Context modifyAttributes(string name, map modification, ...)\n" +
138:      " @synopsis Context modifyAttributes(Nane name, map modification, ...)\n" +
139:      " @param modification Mapping containing modification operation and attribute.\n" +
140:      "        \"modop=>Attribute\"\n" +
141:      " @method search\n" +
142:      " @synopsis list search(string name, string filter, SearchControls controls)\n" +
143:      " @synopsis list search(list name, string filter, SearchControls controls)\n" 
144:    //}}DOC
145:    );
146:  static {
147:    NamingModule.class.getName();
148:  }
149:
150:
151:  private PooledConnection _connection;
152:  private javax.naming.Context _context;
153:  
154:  
155:  public AnyNamingContext(PooledConnection connection)
156:  {
157:    _connection = connection;
158:  }
159:  
160:  
161:  public AnyNamingContext(Context context)
162:  {
163:    _context = context;
164:  }  
165:  
166:  
167:  public anvil.script.ClassType classOf() 
168:  {
169:    return __class__;
170:  }
171:  
172:
173:  public Object toObject()
174:  {
175:    return getContext();
176:  }
177:  
178:  
179:  public javax.naming.Context getContext()
180:  {
181:    if (_context != null) { 
182:      return _context;
183:    }
184:    if (_connection == null) {
185:      throw new RuntimeException("Context has been released");
186:    }
187:    Context ctx = (Context)_connection.getConnection();
188:    if (ctx == null) {
189:      throw new RuntimeException("Context has been released");
190:    }
191:    return ctx;
192:  }
193:
194:  
195:
196:
197:  public BindingEnumeration enumeration()
198:  {
199:    try {
200:      Context ctx = getContext();
201:      return new ContextEnumeration(ctx.listBindings(""));
202:    } catch (NamingException e) {
203:      throw anvil.script.Context.getInstance().exception(e);
204:    }
205:  }
206:
207:
208:  /// @method getName
209:  /// @synopsis string getName()
210:  public Any m_getName(anvil.script.Context context, Any[] parameters) 
211:  {
212:    Context ctx = getContext();
213:    try {
214:      return Any.create(ctx.getNameInNamespace());
215:    } catch (NamingException e) {
216:      throw context.exception(e);
217:    }
218:  }
219:
220:
221:
222:  /// @method list
223:  /// @synopsis list list(string name)
224:  /// @synopsis list list(list name)
225:  /// Return list of SearchResult objects.
226:  ///
227:  /// Example.
228:  /// ctx = anvil.naming.acquire('ldap_pool');
229:  /// foreach (sr; anvil.naming.list('')) {
230:  ///   println "Found: ", sr.getName();
231:  /// }
232:  public Any m_list(anvil.script.Context context, Any[] parameters) 
233:  {
234:    if (parameters.length < 1) {
235:      throw parametersMissing(context, "list");
236:    }
237:    Context ctx = getContext();
238:    try {
239:      NamingEnumeration enum;
240:      Any param = parameters[0];
241:      if (param instanceof  AnyName) {
242:        enum = ctx.list((Name)param.toObject());
243:      } else {
244:        enum = ctx.list(param.toString());
245:      }
246:      AnyList list = new AnyList();
247:      while(enum.hasMoreElements()) {
248:        list.append(new AnySearchResult((NameClassPair)enum.nextElement()));
249:      }
250:      return list;
251:    } catch (NamingException e) {
252:      throw context.exception(e);
253:    }
254:  }
255:  
256:
257:  /// @method lookup
258:  /// @synopsis object lookup(string name)
259:  /// @synopsis object lookup(list name)
260:  public Any m_lookup(anvil.script.Context context, Any[] parameters) 
261:  {
262:    if (parameters.length < 1) {
263:      throw parametersMissing(context, "lookup");
264:    }
265:    Context ctx = getContext();
266:    try {
267:      Any param = parameters[0];
268:      if (param instanceof  AnyName) {
269:        return Any.create(ctx.lookup((Name)param.toObject()));
270:      } else {
271:        return Any.create(ctx.lookup(param.toString()));
272:      } 
273:    } catch (NamingException e) {
274:      throw context.exception(e);
275:    }
276:  }
277:  
278:
279:  /// @method lookupLink
280:  /// @synopsis object lookupLink(string name)
281:  /// @synopsis object lookupLink(list name)
282:  public Any m_lookupLink(anvil.script.Context context, Any[] parameters) 
283:  {
284:    if (parameters.length < 1) {
285:      throw parametersMissing(context, "lookupLink");
286:    }
287:    Context ctx = getContext();
288:    try {
289:      Any param = parameters[0];
290:      if (param instanceof  AnyName) {
291:        return Any.create(ctx.lookupLink((Name)param.toObject()));
292:      } else {
293:        return Any.create(ctx.lookupLink(param.toString()));
294:      } 
295:    } catch (NamingException e) {
296:      throw context.exception(e);
297:    }
298:  }
299:  
300:
301:  /// @method getNameParser
302:  /// @synopsis NameParser getNameParser(string name)
303:  /// @synopsis NameParser getNameParser(list name)
304:  public Any m_getNameParser(anvil.script.Context context, Any[] parameters) 
305:  {
306:    if (parameters.length < 1) {
307:      throw parametersMissing(context, "getNameParser");
308:    }
309:    Context ctx = getContext();
310:    try {
311:      Any param = parameters[0];
312:      if (param instanceof  AnyName) {
313:        return new AnyNameParser(ctx.getNameParser((Name)param.toObject()));
314:      } else {
315:        return new AnyNameParser(ctx.getNameParser(param.toString()));
316:      } 
317:    } catch (NamingException e) {
318:      throw context.exception(e);
319:    }
320:  }
321:
322:
323:
324:  /// @method parse
325:  /// @synopsis list parse(string name, string child)
326:  /// @synopsis list parse(list name, string child)
327:  public Any m_parse(anvil.script.Context context, Any[] parameters) 
328:  {
329:    if (parameters.length < 2) {
330:      throw parametersMissing(context, "parse");
331:    }
332:    Context ctx = getContext();
333:    try {
334:      Any param = parameters[0];
335:      String child = parameters[1].toString();
336:      AnyList list;
337:      if (param instanceof  AnyName) {
338:        return new AnyName(ctx.getNameParser(
339:          (Name)param.toObject()).parse(child));
340:      } else {
341:        return new AnyName(ctx.getNameParser(
342:          param.toString()).parse(child));
343:      } 
344:    } catch (NamingException e) {
345:      throw context.exception(e);
346:    }
347:  }
348:  
349:
350:  /// @method compose
351:  /// @synopsis list compose(string name, string prefix)
352:  /// @synopsis list compose(list name, list prefix)
353:  public Any m_compose(anvil.script.Context context, Any[] parameters) 
354:  {
355:    if (parameters.length < 2) {
356:      throw parametersMissing(context, "compose");
357:    }
358:    Context ctx = getContext();
359:    try {
360:      String prefix = parameters[1].toString();
361:      Any param1 = parameters[0];
362:      Any param2 = parameters[1];
363:      if (param1 instanceof  AnyName && param2 instanceof  AnyName) {
364:        return new AnyName(ctx.composeName(
365:          (Name)param1.toObject(), (Name)param2.toObject()));
366:      } else {
367:        return Any.create(ctx.composeName(param1.toString(), param2.toString()));
368:      } 
369:    } catch (NamingException e) {
370:      throw context.exception(e);
371:    }
372:  }
373:
374:
375:  /// @method bind
376:  /// @synopsis Context bind(string name, object value [, Attributes attributes] )
377:  /// @synopsis Context bind(list name, object value [, Attributes attributes] )
378:  public Any m_bind(anvil.script.Context context, Any[] parameters) 
379:  {
380:    if (parameters.length < 2) {
381:      throw parametersMissing(context, "bind");
382:    }
383:    Context ctx = getContext();
384:
385:    Any param;
386:    Object value = parameters[1].toObject();
387:    Attributes attrs = null;
388:
389:    if (parameters.length > 2) {
390:      param = parameters[2];
391:      if (param instanceof  AnyAttributes) {
392:        attrs = (Attributes)param.toObject();
393:      }
394:    }
395:
396:    try {
397:      if ((attrs != null) && (ctx instanceof  DirContext)) {
398:        DirContext dirctx = (DirContext)ctx;
399:        param = parameters[0];
400:        if (param instanceof  AnyName) {
401:          dirctx.bind((Name)param.toObject(), value, attrs);
402:        } else {
403:          dirctx.bind(param.toString(), value, attrs);
404:        }
405:        return this ;
406:      }
407:
408:      param = parameters[0];
409:      if (param instanceof  AnyName) {
410:        ctx.bind((Name)param.toObject(), value);
411:      } else {
412:        ctx.bind(param.toString(), value);
413:      }
414:      
415:    } catch (NamingException e) {
416:      throw context.exception(e);
417:    }
418:    return this ;
419:  }  
420:  
421:  /// @method rebind
422:  /// @synopsis Context rebind(string name, object value [, Attributes attributes] )
423:  /// @synopsis Context rebind(list name, object value [, Attributes attributes] )
424:  public Any m_rebind(anvil.script.Context context, Any[] parameters) 
425:  {
426:    if (parameters.length < 2) {
427:      throw parametersMissing(context, "rebind");
428:    }
429:    Context ctx = getContext();
430:
431:    Any param;
432:    Object value = parameters[1].toObject();
433:    Attributes attrs = null;
434:
435:    if (parameters.length > 2) {
436:      param = parameters[2];
437:      if (param instanceof  AnyAttributes) {
438:        attrs = (Attributes)param.toObject();
439:      }
440:    }
441:
442:    try {
443:      if ((attrs != null) && (ctx instanceof  DirContext)) {
444:        DirContext dirctx = (DirContext)ctx;
445:        param = parameters[0];
446:        if (param instanceof  AnyName) {
447:          dirctx.rebind((Name)param.toObject(), value, attrs);
448:        } else {
449:          dirctx.rebind(param.toString(), value, attrs);
450:        }
451:        return this ;
452:      }
453:
454:      param = parameters[0];
455:      if (param instanceof  AnyName) {
456:        ctx.rebind((Name)param.toObject(), value);
457:      } else {
458:        ctx.rebind(param.toString(), value);
459:      }
460:      
461:    } catch (NamingException e) {
462:      throw context.exception(e);
463:    }
464:    return this ;
465:  }  
466:  
467:  
468:  /// @method unbind
469:  /// @synopsis Context unbind(string name)
470:  /// @synopsis Context unbind(list name)
471:  public Any m_unbind(anvil.script.Context context, Any[] parameters) 
472:  {
473:    if (parameters.length < 1) {
474:      throw parametersMissing(context, "unbind");
475:    }
476:    Context ctx = getContext();
477:    try {
478:      Any param = parameters[0];
479:      if (param instanceof  AnyName) {
480:        ctx.unbind((Name)param.toObject());
481:      } else {
482:        ctx.unbind(param.toString());
483:      } 
484:    } catch (NamingException e) {
485:      throw context.exception(e);
486:    }
487:    return this ;
488:  }
489:  
490:
491:  
492:  /// @method destroy
493:  /// @synopsis Context destroy(string name)
494:  /// @synopsis Context destroy(list name)
495:  public Any m_destroy(anvil.script.Context context, Any[] parameters) 
496:  {
497:    if (parameters.length < 1) {
498:      throw parametersMissing(context, "destroy");
499:    }
500:    Context ctx = getContext();
501:    try {
502:      Any param = parameters[0];
503:      if (param instanceof  AnyName) {
504:        ctx.destroySubcontext((Name)param.toObject());
505:      } else {
506:        ctx.destroySubcontext(param.toString());
507:      } 
508:    } catch (NamingException e) {
509:      throw context.exception(e);
510:    }
511:    return this ;
512:  }
513:  
514:
515:  /// @method create
516:  /// @synopsis Context create(string name [, Attributes attributes ])
517:  /// @synopsis Context create(list name [, Attributes attributes ])
518:  public Any m_create(anvil.script.Context context, Any[] parameters) 
519:  {
520:    if (parameters.length < 1) {
521:      throw parametersMissing(context, "create");
522:    }
523:    Context ctx = getContext();
524:
525:    Any param;
526:    Attributes attrs = null;
527:
528:    if (parameters.length > 1) {
529:      param = parameters[1];
530:      if (param instanceof  AnyAttributes) {
531:        attrs = (Attributes)param.toObject();
532:      }
533:    }
534:
535:    try {
536:    
537:      if ((attrs != null) && (ctx instanceof  DirContext)) {
538:        DirContext dirctx = (DirContext)ctx;
539:        param = parameters[0];
540:        if (param instanceof  AnyName) {
541:          return new AnyNamingContext(
542:            dirctx.createSubcontext((Name)param.toObject(), attrs));
543:        } else {
544:          return new AnyNamingContext(
545:            dirctx.createSubcontext(param.toString(), attrs));
546:        }
547:      }
548:
549:      param = parameters[0];
550:      if (param instanceof  AnyName) {
551:        return new AnyNamingContext(
552:          ctx.createSubcontext((Name)param.toObject()));
553:      } else {
554:        return new AnyNamingContext(
555:          ctx.createSubcontext(param.toString()));
556:      }
557:      
558:    } catch (NamingException e) {
559:      throw context.exception(e);
560:    }
561:  }
562:    
563:  
564:  /// @method rename
565:  /// @synopsis Context rename(string fromname, string toname)
566:  /// @synopsis Context rename(list fromname, list toname)
567:  public Any m_rename(anvil.script.Context context, Any[] parameters) 
568:  {
569:    if (parameters.length < 2) {
570:      throw parametersMissing(context, "rename");
571:    }
572:    Context ctx = getContext();
573:    try {
574:      Any param1 = parameters[0];
575:      Any param2 = parameters[1];
576:      if (param1 instanceof  AnyName && param2 instanceof  AnyName) {
577:        ctx.rebind((Name)param1.toObject(), (Name)param2.toObject());
578:      } else if (parameters[0].isString()) {
579:        ctx.rebind(param1.toString(), param2.toString());
580:      } 
581:    } catch (NamingException e) {
582:      throw context.exception(e);
583:    }
584:    return this ;
585:  }  
586:  
587:
588:  /// @method close
589:  /// @synopsis void close()
590:  public Any m_close(anvil.script.Context context, Any[] parameters) 
591:  {
592:    Context ctx = getContext();
593:    try {
594:      ctx.close();
595:    } catch (NamingException e) {
596:      throw context.exception(e);
597:    }
598:    return TRUE;
599:  }
600:
601:
602:  /// @method release
603:  /// @synopsis void release()
604:  public Any m_release(anvil.script.Context context, Any[] parameters) 
605:  {
606:    if (_connection != null) {
607:      _connection.release();
608:    } 
609:    _connection = null;
610:    _context = null;
611:    return TRUE;
612:  }
613:
614:
615:  /// @method getAttributes
616:  /// @synopsis Attributes getAttributes()
617:  /// @synopsis Attributes getAttributes(string name)
618:  /// @synopsis Attributes getAttributes(name name)
619:  /// @synopsis Attributes getAttributes(string name, sequence attrsToReturn)
620:  /// @synopsis Attributes getAttributes(name name, sequence attrsToReturn)
621:  public Any m_getAttributes(anvil.script.Context context, Any[] parameters) 
622:  {
623:    Context ctx = getContext();
624:    if (!(ctx instanceof  DirContext)) {
625:      return UNDEFINED;
626:    }
627:    DirContext dirctx = (DirContext)ctx;
628:    
629:    int nparams = parameters.length;
630:    if (nparams > 2) {
631:      nparams = 2;
632:    }
633:    
634:    try {
635:      switch(nparams) { 
636:      case 2:
637:        {
638:          Any param = parameters[0];
639:          String[] attributes = AnyUtils.toStringArray(parameters[1]);
640:          if (param instanceof  AnyName) {
641:            return new AnyAttributes(dirctx.getAttributes((Name)param.toObject(), attributes));
642:          } else {
643:            return new AnyAttributes(dirctx.getAttributes(parameters[0].toString(), attributes));
644:          } 
645:        }
646:
647:      case 1:
648:        {
649:          Any param = parameters[0];
650:          if (param instanceof  AnyName) {
651:            return new AnyAttributes(dirctx.getAttributes((Name)param.toObject()));
652:          } else {
653:            return new AnyAttributes(dirctx.getAttributes(param.toString()));
654:          }
655:        }
656:        
657:      case 0:
658:        return new AnyAttributes(dirctx.getAttributes(""));
659:      }
660:      throw context.BadParameter("Parameter combination not supported");
661:    } catch (NamingException e) {
662:      throw context.exception(e);
663:    }
664:  }
665:  
666:  
667:
668:
669:  /// @method modifyAttributes
670:  /// @synopsis Context modifyAttributes(Attributes attrs)
671:  /// @synopsis Context modifyAttributes(Attributes attrs)
672:  /// @synopsis Context modifyAttributes(map mod, ...)
673:  /// @synopsis Context modifyAttributes(map mod, ...)
674:  /// @synopsis Context modifyAttributes(string name, Attributes attrs)
675:  /// @synopsis Context modifyAttributes(Name name, Attributes attrs)
676:  /// @synopsis Context modifyAttributes(string name, int modop, Attributes attrs)
677:  /// @synopsis Context modifyAttributes(Name name, int modop, Attributes attrs)
678:  /// @synopsis Context modifyAttributes(string name, map modification, ...)
679:  /// @synopsis Context modifyAttributes(Nane name, map modification, ...)
680:  /// @param modification Mapping containing modification operation and attribute.
681:  ///        "modop=>Attribute"
682:  public Any m_modifyAttributes(anvil.script.Context context, Any[] parameters) 
683:  {
684:    int n = parameters.length;
685:    if (parameters.length < 1) {
686:      throw parametersMissing(context, "modifyAttributes");
687:    }
688:    
689:    int i = 0;
690:    Any param;
691:
692:    Name name = null;
693:    String sname = null;
694:    Attributes attributes = null;
695:    ModificationItem[] modifications = null;
696:    int modop = DirContext.REPLACE_ATTRIBUTE;
697:    
698:    for(; i<n; i++) {
699:      param = parameters[i];
700:      if (param instanceof  AnyName) {
701:        name = (Name)param.toObject();
702:      } else if (param instanceof  AnyAttributes) {
703:        attributes = (Attributes)param.toObject();
704:      } else if (param.isString()) {
705:        sname = param.toString();
706:      } else if (param.isInt()) {
707:        modop = param.toInt();
708:      } else if (param.isMap()) {
709:        break;
710:      } else {
711:        break;
712:      }
713:    }
714:
715:    if (name == null && sname == null) { 
716:      sname = "";
717:    }
718:    
719:    if (i<n) {
720:      int size = n - i;
721:      modifications = new ModificationItem[n - i];
722:      int c = 0;
723:      for(i=0; i<n; i++) {
724:        ModificationItem item = buildItem(parameters[i]);
725:        if (item != null) {
726:          modifications[c++] = item;
727:        }
728:      }
729:      if (c < size) {
730:        ModificationItem[] newmodifications = new ModificationItem[c];
731:        System.arraycopy(modifications, 0, newmodifications, 0, c);
732:        modifications = newmodifications;
733:      }
734:    }
735:
736:    Context ctx = getContext();
737:    if (!(ctx instanceof  DirContext)) {
738:      return this ;
739:    }
740:    DirContext dirctx = (DirContext)ctx;
741:    
742:
743:    try {
744:      if (attributes != null) {
745:        if (name != null) {
746:          dirctx.modifyAttributes(name, modop, attributes);
747:        } else {
748:          dirctx.modifyAttributes(sname, modop, attributes);
749:        }
750:      } else if (modifications != null) {
751:        if (name != null) {
752:          dirctx.modifyAttributes(name, modifications);
753:        } else {
754:          dirctx.modifyAttributes(sname, modifications);
755:        }
756:      } 
757:    } catch (NamingException e) {
758:      throw context.exception(e);
759:    }
760:    
761:    return this ;
762:  }
763:    
764:
765:  /// @method search
766:  /// @synopsis list search(string name, string filter, SearchControls controls)
767:  /// @synopsis list search(list name, string filter, SearchControls controls)
768:  public Any m_search(anvil.script.Context context, Any[] parameters) 
769:
770:  {
771:    if (parameters.length < 3) {
772:      throw parametersMissing(context, "search");
773:    }
774:
775:    Context ctx = getContext();
776:    if (!(ctx instanceof  DirContext)) {
777:      return UNDEFINED;
778:    }
779:    DirContext dirctx = (DirContext)ctx;
780:    
781:    int nparams = parameters.length;
782:    try {
783:      String filter = parameters[1].toString();
784:      SearchControls controls = null;
785:      Any param = parameters[2];
786:      if (param instanceof  AnySearchControls) {
787:        controls = (SearchControls)param.toObject();
788:      } else {
789:        throw context.BadParameter("Excepted SearchControls as third parameter");
790:      } 
791:      NamingEnumeration enum;
792:      param = parameters[0];
793:      if (param instanceof  AnyName) {
794:        Name name = (Name)param.toObject();
795:        enum = dirctx.search(name, filter, controls);
796:      } else {
797:        String name = param.toString();
798:        enum = dirctx.search(name, filter, controls);
799:      }
800:      
801:      AnyList result = new AnyList();
802:      System.err.println("--doing list");
803:      while(enum.hasMoreElements()) {
804:        System.err.println("--found");
805:        result.append(new AnySearchResult((NameClassPair)enum.nextElement()));
806:      }
807:      return result;
808:
809:    } catch (NamingException e) {
810:      throw context.exception(e);
811:    }
812:  }
813:  
814:  
815:
816:  public class ContextEnumeration implements  BindingEnumeration
817:  {
818:    private NamingEnumeration _enum;
819:    private int _index = 0;
820:    
821:    public ContextEnumeration(NamingEnumeration enum) 
822:    {
823:      _enum = enum;
824:    }
825:    
826:    public boolean hasMoreElements()
827:    {
828:      return _enum.hasMoreElements();
829:    }
830:
831:    public Object nextKey()
832:    {
833:      return Any.create(_index);
834:    }
835:
836:    public Object nextElement()
837:    {
838:      try {
839:        NameClassPair pair = (NameClassPair)_enum.nextElement();
840:        _index++;
841:        return new AnySearchResult(pair);
842:      } catch (Exception e) {
843:        throw anvil.script.Context.getInstance().exception(e);
844:      }
845:    }
846:  }
847:
848:}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.