Source Code Cross Referenced for ProxyDirContext.java in  » Sevlet-Container » tomcat-connectors » org » apache » naming » modules » cache » 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 » Sevlet Container » tomcat connectors » org.apache.naming.modules.cache 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         *  Copyright 1999-2004 The Apache Software Foundation
0003:         *
0004:         *  Licensed under the Apache License, Version 2.0 (the "License");
0005:         *  you may not use this file except in compliance with the License.
0006:         *  You may obtain a copy of the License at
0007:         *
0008:         *      http://www.apache.org/licenses/LICENSE-2.0
0009:         *
0010:         *  Unless required by applicable law or agreed to in writing, software
0011:         *  distributed under the License is distributed on an "AS IS" BASIS,
0012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013:         *  See the License for the specific language governing permissions and
0014:         *  limitations under the License.
0015:         */
0016:
0017:        package org.apache.naming.modules.cache;
0018:
0019:        import java.util.Collections;
0020:        import java.util.Hashtable;
0021:        import java.util.Map;
0022:
0023:        import javax.naming.Context;
0024:        import javax.naming.Name;
0025:        import javax.naming.NameParser;
0026:        import javax.naming.NamingEnumeration;
0027:        import javax.naming.NamingException;
0028:        import javax.naming.directory.Attributes;
0029:        import javax.naming.directory.DirContext;
0030:        import javax.naming.directory.ModificationItem;
0031:        import javax.naming.directory.SearchControls;
0032:
0033:        import org.apache.commons.collections.LRUMap;
0034:        import org.apache.naming.core.BaseDirContext;
0035:        import org.apache.naming.util.AttributeHelper;
0036:        import org.apache.tomcat.util.res.StringManager;
0037:
0038:        /* Changes( costin ): The goal is to make it a generic JNDI cache, not specific
0039:         to file system. 
0040:
0041:         - no more wrapping in Resource and ResourceAttributes.
0042:         We just cache, and the caller can use tools to do the wrapping or operate on atts.
0043:         - we use only lastModified ( not contentLength ).
0044:         -
0045:
0046:         TODO:
0047:         - add a special CacheInputStream - that will save the byte[] in the cache entry.
0048:         - 2 TTL: one will prevent accessing the dir ( even for lastModified ), one will
0049:         expire the entry regardless. The first should be very short ( .1 sec ? ), for
0050:         to avoid very frequent accesses to the same entry.
0051:         Alternative ( probably the best ): use a background thread to check 'lastModified',
0052:         like we do in 3.3 for class reloading.
0053:         */
0054:
0055:        /**
0056:         * Proxy Directory Context implementation.
0057:         *
0058:         * Will cache directory entries - attributes and content. This can be used
0059:         * to eliminate expensive dir access and to avoid keeping large directories in memory.
0060:         *
0061:         * @author Remy Maucherat
0062:         * @author Costin Manolache
0063:         */
0064:        public class ProxyDirContext implements  DirContext {
0065:            private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
0066:                    .getLog(ProxyDirContext.class);
0067:
0068:            // ----------------------------------------------------------- Constructors
0069:
0070:            /**
0071:             * Builds a proxy directory context using the given environment.
0072:             */
0073:            public ProxyDirContext(Hashtable env, DirContext dirContext) {
0074:                this .env = env;
0075:                this .dirContext = dirContext;
0076:                if (dirContext instanceof  BaseDirContext) {
0077:                    // Initialize parameters based on the associated dir context, like
0078:                    // the caching policy.
0079:                    if (((BaseDirContext) dirContext).isCached()) {
0080:                        cache = Collections.synchronizedMap(new LRUMap(
0081:                                cacheSize));
0082:                        cacheTTL = ((BaseDirContext) dirContext).getCacheTTL();
0083:                        cacheObjectMaxSize = ((BaseDirContext) dirContext)
0084:                                .getCacheObjectMaxSize();
0085:                    }
0086:                }
0087:            }
0088:
0089:            /**
0090:             * Builds a clone of this proxy dir context, wrapping the given directory
0091:             * context, and sharing the same cache.
0092:             */
0093:            protected ProxyDirContext(ProxyDirContext proxyDirContext,
0094:                    DirContext dirContext) {
0095:                this .env = proxyDirContext.env;
0096:                this .dirContext = dirContext;
0097:                this .cache = proxyDirContext.cache;
0098:                this .cacheSize = proxyDirContext.cacheSize;
0099:                this .cacheTTL = proxyDirContext.cacheTTL;
0100:                this .cacheObjectMaxSize = proxyDirContext.cacheObjectMaxSize;
0101:            }
0102:
0103:            // ----------------------------------------------------- Instance Variables
0104:
0105:            /**
0106:             * Environment.
0107:             */
0108:            protected Hashtable env;
0109:
0110:            /**
0111:             * The string manager for this package.
0112:             */
0113:            protected StringManager sm = StringManager
0114:                    .getManager("org.apache.naming.res");
0115:
0116:            /**
0117:             * Associated DirContext.
0118:             */
0119:            protected DirContext dirContext;
0120:
0121:            /**
0122:             * Cache.
0123:             * Path -> Cache entry.
0124:             */
0125:            protected Map cache = null;
0126:
0127:            /**
0128:             * Cache size
0129:             */
0130:            protected int cacheSize = 1000;
0131:
0132:            /**
0133:             * Cache TTL.
0134:             */
0135:            protected int cacheTTL = 5000; // 5s
0136:
0137:            /**
0138:             * Max size of resources which will have their content cached.
0139:             */
0140:            protected int cacheObjectMaxSize = 32768; // 32 KB
0141:
0142:            // --------------------------------------------------------- Public Methods
0143:
0144:            /**
0145:             * Return the actual directory context we are wrapping.
0146:             */
0147:            public DirContext getDirContext() {
0148:                return this .dirContext;
0149:            }
0150:
0151:            // -------------------------------------------------------- Context Methods
0152:
0153:            /**
0154:             * Retrieves the named object. If name is empty, returns a new instance 
0155:             * of this context (which represents the same naming context as this 
0156:             * context, but its environment may be modified independently and it may 
0157:             * be accessed concurrently).
0158:             * 
0159:             * @param name the name of the object to look up
0160:             * @return the object bound to name
0161:             * @exception NamingException if a naming exception is encountered
0162:             */
0163:            public Object lookup(Name name) throws NamingException {
0164:                CacheEntry entry = cacheLookupAndLoad(name.toString());
0165:                if (entry != null) {
0166:                    if (entry.resource != null) {
0167:                        // Check content caching.
0168:
0169:                        return entry.resource;
0170:                    } else {
0171:                        return entry.context;
0172:                    }
0173:                }
0174:                log.info("Strange, entry was no loadeded " + name);
0175:                Object object = dirContext.lookup(parseName(name));
0176:                //         if (object instanceof InputStream)
0177:                //             return new Resource((InputStream) object);
0178:                //         else
0179:                return object;
0180:            }
0181:
0182:            /**
0183:             * Retrieves the named object.
0184:             * 
0185:             * @param name the name of the object to look up
0186:             * @return the object bound to name
0187:             * @exception NamingException if a naming exception is encountered
0188:             */
0189:            public Object lookup(String name) throws NamingException {
0190:                CacheEntry entry = cacheLookupAndLoad(name);
0191:                if (entry != null) {
0192:                    if (entry.resource != null) {
0193:                        return entry.resource;
0194:                    } else {
0195:                        return entry.context;
0196:                    }
0197:                }
0198:                log.info("Strange, entry was no loadeded " + name);
0199:
0200:                Object object = dirContext.lookup(parseName(name));
0201:                //         if (object instanceof InputStream) {
0202:                //             return new Resource((InputStream) object);
0203:                //         } else if (object instanceof DirContext) {
0204:                //             return object;
0205:                //         } else if (object instanceof Resource) {
0206:                //             return object;
0207:                //         } else {
0208:                //             return new Resource(new ByteArrayInputStream
0209:                //                 (object.toString().getBytes()));
0210:                //         }
0211:                return object;
0212:            }
0213:
0214:            /**
0215:             * Binds a name to an object. All intermediate contexts and the target 
0216:             * context (that named by all but terminal atomic component of the name) 
0217:             * must already exist.
0218:             * 
0219:             * @param name the name to bind; may not be empty
0220:             * @param obj the object to bind; possibly null
0221:             * @exception NameAlreadyBoundException if name is already bound
0222:             * @exception InvalidAttributesException if object did not supply all 
0223:             * mandatory attributes
0224:             * @exception NamingException if a naming exception is encountered
0225:             */
0226:            public void bind(Name name, Object obj) throws NamingException {
0227:                dirContext.bind(parseName(name), obj);
0228:                cacheUnload(name.toString());
0229:            }
0230:
0231:            /**
0232:             * Binds a name to an object.
0233:             * 
0234:             * @param name the name to bind; may not be empty
0235:             * @param obj the object to bind; possibly null
0236:             * @exception NameAlreadyBoundException if name is already bound
0237:             * @exception InvalidAttributesException if object did not supply all 
0238:             * mandatory attributes
0239:             * @exception NamingException if a naming exception is encountered
0240:             */
0241:            public void bind(String name, Object obj) throws NamingException {
0242:                dirContext.bind(parseName(name), obj);
0243:                cacheUnload(name);
0244:            }
0245:
0246:            /**
0247:             * Binds a name to an object, overwriting any existing binding. All 
0248:             * intermediate contexts and the target context (that named by all but 
0249:             * terminal atomic component of the name) must already exist.
0250:             * <p>
0251:             * If the object is a DirContext, any existing attributes associated with 
0252:             * the name are replaced with those of the object. Otherwise, any 
0253:             * existing attributes associated with the name remain unchanged.
0254:             * 
0255:             * @param name the name to bind; may not be empty
0256:             * @param obj the object to bind; possibly null
0257:             * @exception InvalidAttributesException if object did not supply all 
0258:             * mandatory attributes
0259:             * @exception NamingException if a naming exception is encountered
0260:             */
0261:            public void rebind(Name name, Object obj) throws NamingException {
0262:                dirContext.rebind(parseName(name), obj);
0263:                cacheUnload(name.toString());
0264:            }
0265:
0266:            /**
0267:             * Binds a name to an object, overwriting any existing binding.
0268:             * 
0269:             * @param name the name to bind; may not be empty
0270:             * @param obj the object to bind; possibly null
0271:             * @exception InvalidAttributesException if object did not supply all 
0272:             * mandatory attributes
0273:             * @exception NamingException if a naming exception is encountered
0274:             */
0275:            public void rebind(String name, Object obj) throws NamingException {
0276:                dirContext.rebind(parseName(name), obj);
0277:                cacheUnload(name);
0278:            }
0279:
0280:            /**
0281:             * Unbinds the named object. Removes the terminal atomic name in name 
0282:             * from the target context--that named by all but the terminal atomic 
0283:             * part of name.
0284:             * <p>
0285:             * This method is idempotent. It succeeds even if the terminal atomic 
0286:             * name is not bound in the target context, but throws 
0287:             * NameNotFoundException if any of the intermediate contexts do not exist. 
0288:             * 
0289:             * @param name the name to bind; may not be empty
0290:             * @exception NameNotFoundException if an intermediate context does not 
0291:             * exist
0292:             * @exception NamingException if a naming exception is encountered
0293:             */
0294:            public void unbind(Name name) throws NamingException {
0295:                dirContext.unbind(parseName(name));
0296:                cacheUnload(name.toString());
0297:            }
0298:
0299:            /**
0300:             * Unbinds the named object.
0301:             * 
0302:             * @param name the name to bind; may not be empty
0303:             * @exception NameNotFoundException if an intermediate context does not 
0304:             * exist
0305:             * @exception NamingException if a naming exception is encountered
0306:             */
0307:            public void unbind(String name) throws NamingException {
0308:                dirContext.unbind(parseName(name));
0309:                cacheUnload(name);
0310:            }
0311:
0312:            /**
0313:             * Binds a new name to the object bound to an old name, and unbinds the 
0314:             * old name. Both names are relative to this context. Any attributes 
0315:             * associated with the old name become associated with the new name. 
0316:             * Intermediate contexts of the old name are not changed.
0317:             * 
0318:             * @param oldName the name of the existing binding; may not be empty
0319:             * @param newName the name of the new binding; may not be empty
0320:             * @exception NameAlreadyBoundException if newName is already bound
0321:             * @exception NamingException if a naming exception is encountered
0322:             */
0323:            public void rename(Name oldName, Name newName)
0324:                    throws NamingException {
0325:                dirContext.rename(parseName(oldName), parseName(newName));
0326:                cacheUnload(oldName.toString());
0327:            }
0328:
0329:            /**
0330:             * Binds a new name to the object bound to an old name, and unbinds the 
0331:             * old name.
0332:             * 
0333:             * @param oldName the name of the existing binding; may not be empty
0334:             * @param newName the name of the new binding; may not be empty
0335:             * @exception NameAlreadyBoundException if newName is already bound
0336:             * @exception NamingException if a naming exception is encountered
0337:             */
0338:            public void rename(String oldName, String newName)
0339:                    throws NamingException {
0340:                dirContext.rename(parseName(oldName), parseName(newName));
0341:                cacheUnload(oldName);
0342:            }
0343:
0344:            /**
0345:             * Enumerates the names bound in the named context, along with the class 
0346:             * names of objects bound to them. The contents of any subcontexts are 
0347:             * not included.
0348:             * <p>
0349:             * If a binding is added to or removed from this context, its effect on 
0350:             * an enumeration previously returned is undefined.
0351:             * 
0352:             * @param name the name of the context to list
0353:             * @return an enumeration of the names and class names of the bindings in 
0354:             * this context. Each element of the enumeration is of type NameClassPair.
0355:             * @exception NamingException if a naming exception is encountered
0356:             */
0357:            public NamingEnumeration list(Name name) throws NamingException {
0358:                return dirContext.list(parseName(name));
0359:            }
0360:
0361:            /**
0362:             * Enumerates the names bound in the named context, along with the class 
0363:             * names of objects bound to them.
0364:             * 
0365:             * @param name the name of the context to list
0366:             * @return an enumeration of the names and class names of the bindings in 
0367:             * this context. Each element of the enumeration is of type NameClassPair.
0368:             * @exception NamingException if a naming exception is encountered
0369:             */
0370:            public NamingEnumeration list(String name) throws NamingException {
0371:                return dirContext.list(parseName(name));
0372:            }
0373:
0374:            /**
0375:             * Enumerates the names bound in the named context, along with the 
0376:             * objects bound to them. The contents of any subcontexts are not 
0377:             * included.
0378:             * <p>
0379:             * If a binding is added to or removed from this context, its effect on 
0380:             * an enumeration previously returned is undefined.
0381:             * 
0382:             * @param name the name of the context to list
0383:             * @return an enumeration of the bindings in this context. 
0384:             * Each element of the enumeration is of type Binding.
0385:             * @exception NamingException if a naming exception is encountered
0386:             */
0387:            public NamingEnumeration listBindings(Name name)
0388:                    throws NamingException {
0389:                return dirContext.listBindings(parseName(name));
0390:            }
0391:
0392:            /**
0393:             * Enumerates the names bound in the named context, along with the 
0394:             * objects bound to them.
0395:             * 
0396:             * @param name the name of the context to list
0397:             * @return an enumeration of the bindings in this context. 
0398:             * Each element of the enumeration is of type Binding.
0399:             * @exception NamingException if a naming exception is encountered
0400:             */
0401:            public NamingEnumeration listBindings(String name)
0402:                    throws NamingException {
0403:                return dirContext.listBindings(parseName(name));
0404:            }
0405:
0406:            /**
0407:             * Destroys the named context and removes it from the namespace. Any 
0408:             * attributes associated with the name are also removed. Intermediate 
0409:             * contexts are not destroyed.
0410:             * <p>
0411:             * This method is idempotent. It succeeds even if the terminal atomic 
0412:             * name is not bound in the target context, but throws 
0413:             * NameNotFoundException if any of the intermediate contexts do not exist. 
0414:             * 
0415:             * In a federated naming system, a context from one naming system may be 
0416:             * bound to a name in another. One can subsequently look up and perform 
0417:             * operations on the foreign context using a composite name. However, an 
0418:             * attempt destroy the context using this composite name will fail with 
0419:             * NotContextException, because the foreign context is not a "subcontext" 
0420:             * of the context in which it is bound. Instead, use unbind() to remove 
0421:             * the binding of the foreign context. Destroying the foreign context 
0422:             * requires that the destroySubcontext() be performed on a context from 
0423:             * the foreign context's "native" naming system.
0424:             * 
0425:             * @param name the name of the context to be destroyed; may not be empty
0426:             * @exception NameNotFoundException if an intermediate context does not 
0427:             * exist
0428:             * @exception NotContextException if the name is bound but does not name 
0429:             * a context, or does not name a context of the appropriate type
0430:             */
0431:            public void destroySubcontext(Name name) throws NamingException {
0432:                dirContext.destroySubcontext(parseName(name));
0433:                cacheUnload(name.toString());
0434:            }
0435:
0436:            /**
0437:             * Destroys the named context and removes it from the namespace.
0438:             * 
0439:             * @param name the name of the context to be destroyed; may not be empty
0440:             * @exception NameNotFoundException if an intermediate context does not 
0441:             * exist
0442:             * @exception NotContextException if the name is bound but does not name 
0443:             * a context, or does not name a context of the appropriate type
0444:             */
0445:            public void destroySubcontext(String name) throws NamingException {
0446:                dirContext.destroySubcontext(parseName(name));
0447:                cacheUnload(name);
0448:            }
0449:
0450:            /**
0451:             * Creates and binds a new context. Creates a new context with the given 
0452:             * name and binds it in the target context (that named by all but 
0453:             * terminal atomic component of the name). All intermediate contexts and 
0454:             * the target context must already exist.
0455:             * 
0456:             * @param name the name of the context to create; may not be empty
0457:             * @return the newly created context
0458:             * @exception NameAlreadyBoundException if name is already bound
0459:             * @exception InvalidAttributesException if creation of the subcontext 
0460:             * requires specification of mandatory attributes
0461:             * @exception NamingException if a naming exception is encountered
0462:             */
0463:            public Context createSubcontext(Name name) throws NamingException {
0464:                return dirContext.createSubcontext(parseName(name));
0465:            }
0466:
0467:            /**
0468:             * Creates and binds a new context.
0469:             * 
0470:             * @param name the name of the context to create; may not be empty
0471:             * @return the newly created context
0472:             * @exception NameAlreadyBoundException if name is already bound
0473:             * @exception InvalidAttributesException if creation of the subcontext 
0474:             * requires specification of mandatory attributes
0475:             * @exception NamingException if a naming exception is encountered
0476:             */
0477:            public Context createSubcontext(String name) throws NamingException {
0478:                return dirContext.createSubcontext(parseName(name));
0479:            }
0480:
0481:            /**
0482:             * Retrieves the named object, following links except for the terminal 
0483:             * atomic component of the name. If the object bound to name is not a 
0484:             * link, returns the object itself.
0485:             * 
0486:             * @param name the name of the object to look up
0487:             * @return the object bound to name, not following the terminal link 
0488:             * (if any).
0489:             * @exception NamingException if a naming exception is encountered
0490:             */
0491:            public Object lookupLink(Name name) throws NamingException {
0492:                return dirContext.lookupLink(parseName(name));
0493:            }
0494:
0495:            /**
0496:             * Retrieves the named object, following links except for the terminal 
0497:             * atomic component of the name.
0498:             * 
0499:             * @param name the name of the object to look up
0500:             * @return the object bound to name, not following the terminal link 
0501:             * (if any).
0502:             * @exception NamingException if a naming exception is encountered
0503:             */
0504:            public Object lookupLink(String name) throws NamingException {
0505:                return dirContext.lookupLink(parseName(name));
0506:            }
0507:
0508:            /**
0509:             * Retrieves the parser associated with the named context. In a 
0510:             * federation of namespaces, different naming systems will parse names 
0511:             * differently. This method allows an application to get a parser for 
0512:             * parsing names into their atomic components using the naming convention 
0513:             * of a particular naming system. Within any single naming system, 
0514:             * NameParser objects returned by this method must be equal (using the 
0515:             * equals() test).
0516:             * 
0517:             * @param name the name of the context from which to get the parser
0518:             * @return a name parser that can parse compound names into their atomic 
0519:             * components
0520:             * @exception NamingException if a naming exception is encountered
0521:             */
0522:            public NameParser getNameParser(Name name) throws NamingException {
0523:                return dirContext.getNameParser(parseName(name));
0524:            }
0525:
0526:            /**
0527:             * Retrieves the parser associated with the named context.
0528:             * 
0529:             * @param name the name of the context from which to get the parser
0530:             * @return a name parser that can parse compound names into their atomic 
0531:             * components
0532:             * @exception NamingException if a naming exception is encountered
0533:             */
0534:            public NameParser getNameParser(String name) throws NamingException {
0535:                return dirContext.getNameParser(parseName(name));
0536:            }
0537:
0538:            /**
0539:             * Composes the name of this context with a name relative to this context.
0540:             * <p>
0541:             * Given a name (name) relative to this context, and the name (prefix) 
0542:             * of this context relative to one of its ancestors, this method returns 
0543:             * the composition of the two names using the syntax appropriate for the 
0544:             * naming system(s) involved. That is, if name names an object relative 
0545:             * to this context, the result is the name of the same object, but 
0546:             * relative to the ancestor context. None of the names may be null.
0547:             * 
0548:             * @param name a name relative to this context
0549:             * @param prefix the name of this context relative to one of its ancestors
0550:             * @return the composition of prefix and name
0551:             * @exception NamingException if a naming exception is encountered
0552:             */
0553:            public Name composeName(Name name, Name prefix)
0554:                    throws NamingException {
0555:                prefix = (Name) name.clone();
0556:                return prefix.addAll(name);
0557:            }
0558:
0559:            /**
0560:             * Composes the name of this context with a name relative to this context.
0561:             * 
0562:             * @param name a name relative to this context
0563:             * @param prefix the name of this context relative to one of its ancestors
0564:             * @return the composition of prefix and name
0565:             * @exception NamingException if a naming exception is encountered
0566:             */
0567:            public String composeName(String name, String prefix)
0568:                    throws NamingException {
0569:                return prefix + "/" + name;
0570:            }
0571:
0572:            /**
0573:             * Adds a new environment property to the environment of this context. If 
0574:             * the property already exists, its value is overwritten.
0575:             * 
0576:             * @param propName the name of the environment property to add; may not 
0577:             * be null
0578:             * @param propVal the value of the property to add; may not be null
0579:             * @exception NamingException if a naming exception is encountered
0580:             */
0581:            public Object addToEnvironment(String propName, Object propVal)
0582:                    throws NamingException {
0583:                return dirContext.addToEnvironment(propName, propVal);
0584:            }
0585:
0586:            /**
0587:             * Removes an environment property from the environment of this context. 
0588:             * 
0589:             * @param propName the name of the environment property to remove; 
0590:             * may not be null
0591:             * @exception NamingException if a naming exception is encountered
0592:             */
0593:            public Object removeFromEnvironment(String propName)
0594:                    throws NamingException {
0595:                return dirContext.removeFromEnvironment(propName);
0596:            }
0597:
0598:            /**
0599:             * Retrieves the environment in effect for this context. See class 
0600:             * description for more details on environment properties. 
0601:             * The caller should not make any changes to the object returned: their 
0602:             * effect on the context is undefined. The environment of this context 
0603:             * may be changed using addToEnvironment() and removeFromEnvironment().
0604:             * 
0605:             * @return the environment of this context; never null
0606:             * @exception NamingException if a naming exception is encountered
0607:             */
0608:            public Hashtable getEnvironment() throws NamingException {
0609:                return dirContext.getEnvironment();
0610:            }
0611:
0612:            /**
0613:             * Closes this context. This method releases this context's resources 
0614:             * immediately, instead of waiting for them to be released automatically 
0615:             * by the garbage collector.
0616:             * This method is idempotent: invoking it on a context that has already 
0617:             * been closed has no effect. Invoking any other method on a closed 
0618:             * context is not allowed, and results in undefined behaviour.
0619:             * 
0620:             * @exception NamingException if a naming exception is encountered
0621:             */
0622:            public void close() throws NamingException {
0623:                dirContext.close();
0624:            }
0625:
0626:            /**
0627:             * Retrieves the full name of this context within its own namespace.
0628:             * <p>
0629:             * Many naming services have a notion of a "full name" for objects in 
0630:             * their respective namespaces. For example, an LDAP entry has a 
0631:             * distinguished name, and a DNS record has a fully qualified name. This 
0632:             * method allows the client application to retrieve this name. The string 
0633:             * returned by this method is not a JNDI composite name and should not be 
0634:             * passed directly to context methods. In naming systems for which the 
0635:             * notion of full name does not make sense, 
0636:             * OperationNotSupportedException is thrown.
0637:             * 
0638:             * @return this context's name in its own namespace; never null
0639:             * @exception OperationNotSupportedException if the naming system does 
0640:             * not have the notion of a full name
0641:             * @exception NamingException if a naming exception is encountered
0642:             */
0643:            public String getNameInNamespace() throws NamingException {
0644:                return dirContext.getNameInNamespace();
0645:            }
0646:
0647:            // ----------------------------------------------------- DirContext Methods
0648:
0649:            /**
0650:             * Retrieves all of the attributes associated with a named object. 
0651:             * 
0652:             * @return the set of attributes associated with name. 
0653:             * Returns an empty attribute set if name has no attributes; never null.
0654:             * @param name the name of the object from which to retrieve attributes
0655:             * @exception NamingException if a naming exception is encountered
0656:             */
0657:            public Attributes getAttributes(Name name) throws NamingException {
0658:                CacheEntry entry = cacheLookupAndLoad(name.toString());
0659:                if (entry != null) {
0660:                    return entry.attributes;
0661:                }
0662:                Attributes attributes = dirContext
0663:                        .getAttributes(parseName(name));
0664:
0665:                // TODO(costin): Why do we need to wrap it ? It may be better to use decorator
0666:                //         if (!(attributes instanceof ResourceAttributes)) {
0667:                //             attributes = new ResourceAttributes(attributes);
0668:                //         }
0669:                return attributes;
0670:            }
0671:
0672:            /**
0673:             * Retrieves all of the attributes associated with a named object.
0674:             * 
0675:             * @return the set of attributes associated with name
0676:             * @param name the name of the object from which to retrieve attributes
0677:             * @exception NamingException if a naming exception is encountered
0678:             */
0679:            public Attributes getAttributes(String name) throws NamingException {
0680:                CacheEntry entry = cacheLookupAndLoad(name);
0681:                if (entry != null) {
0682:                    return entry.attributes;
0683:                }
0684:                Attributes attributes = dirContext
0685:                        .getAttributes(parseName(name));
0686:                //         if (!(attributes instanceof ResourceAttributes)) {
0687:                //             attributes = new ResourceAttributes(attributes);
0688:                //         }
0689:                return attributes;
0690:            }
0691:
0692:            /**
0693:             * Retrieves selected attributes associated with a named object. 
0694:             * See the class description regarding attribute models, attribute type 
0695:             * names, and operational attributes.
0696:             * 
0697:             * @return the requested attributes; never null
0698:             * @param name the name of the object from which to retrieve attributes
0699:             * @param attrIds the identifiers of the attributes to retrieve. null 
0700:             * indicates that all attributes should be retrieved; an empty array 
0701:             * indicates that none should be retrieved
0702:             * @exception NamingException if a naming exception is encountered
0703:             */
0704:            public Attributes getAttributes(Name name, String[] attrIds)
0705:                    throws NamingException {
0706:                Attributes attributes = dirContext.getAttributes(
0707:                        parseName(name), attrIds);
0708:                //         if (!(attributes instanceof ResourceAttributes)) {
0709:                //             attributes = new ResourceAttributes(attributes);
0710:                //         }
0711:                return attributes;
0712:            }
0713:
0714:            /**
0715:             * Retrieves selected attributes associated with a named object.
0716:             * 
0717:             * @return the requested attributes; never null
0718:             * @param name the name of the object from which to retrieve attributes
0719:             * @param attrIds the identifiers of the attributes to retrieve. null 
0720:             * indicates that all attributes should be retrieved; an empty array 
0721:             * indicates that none should be retrieved
0722:             * @exception NamingException if a naming exception is encountered
0723:             */
0724:            public Attributes getAttributes(String name, String[] attrIds)
0725:                    throws NamingException {
0726:                Attributes attributes = dirContext.getAttributes(
0727:                        parseName(name), attrIds);
0728:                //         if (!(attributes instanceof ResourceAttributes)) {
0729:                //             attributes = new ResourceAttributes(attributes);
0730:                //         }
0731:                return attributes;
0732:            }
0733:
0734:            /**
0735:             * Modifies the attributes associated with a named object. The order of 
0736:             * the modifications is not specified. Where possible, the modifications 
0737:             * are performed atomically.
0738:             * 
0739:             * @param name the name of the object whose attributes will be updated
0740:             * @param mod_op the modification operation, one of: ADD_ATTRIBUTE, 
0741:             * REPLACE_ATTRIBUTE, REMOVE_ATTRIBUTE
0742:             * @param attrs the attributes to be used for the modification; may not 
0743:             * be null
0744:             * @exception AttributeModificationException if the modification cannot be
0745:             * completed successfully
0746:             * @exception NamingException if a naming exception is encountered
0747:             */
0748:            public void modifyAttributes(Name name, int mod_op, Attributes attrs)
0749:                    throws NamingException {
0750:                dirContext.modifyAttributes(parseName(name), mod_op, attrs);
0751:            }
0752:
0753:            /**
0754:             * Modifies the attributes associated with a named object.
0755:             * 
0756:             * @param name the name of the object whose attributes will be updated
0757:             * @param mod_op the modification operation, one of: ADD_ATTRIBUTE, 
0758:             * REPLACE_ATTRIBUTE, REMOVE_ATTRIBUTE
0759:             * @param attrs the attributes to be used for the modification; may not 
0760:             * be null
0761:             * @exception AttributeModificationException if the modification cannot be
0762:             * completed successfully
0763:             * @exception NamingException if a naming exception is encountered
0764:             */
0765:            public void modifyAttributes(String name, int mod_op,
0766:                    Attributes attrs) throws NamingException {
0767:                dirContext.modifyAttributes(parseName(name), mod_op, attrs);
0768:            }
0769:
0770:            /**
0771:             * Modifies the attributes associated with a named object using an an 
0772:             * ordered list of modifications. The modifications are performed in the 
0773:             * order specified. Each modification specifies a modification operation 
0774:             * code and an attribute on which to operate. Where possible, the 
0775:             * modifications are performed atomically.
0776:             * 
0777:             * @param name the name of the object whose attributes will be updated
0778:             * @param mods an ordered sequence of modifications to be performed; may 
0779:             * not be null
0780:             * @exception AttributeModificationException if the modification cannot be
0781:             * completed successfully
0782:             * @exception NamingException if a naming exception is encountered
0783:             */
0784:            public void modifyAttributes(Name name, ModificationItem[] mods)
0785:                    throws NamingException {
0786:                dirContext.modifyAttributes(parseName(name), mods);
0787:            }
0788:
0789:            /**
0790:             * Modifies the attributes associated with a named object using an an 
0791:             * ordered list of modifications.
0792:             * 
0793:             * @param name the name of the object whose attributes will be updated
0794:             * @param mods an ordered sequence of modifications to be performed; may 
0795:             * not be null
0796:             * @exception AttributeModificationException if the modification cannot be
0797:             * completed successfully
0798:             * @exception NamingException if a naming exception is encountered
0799:             */
0800:            public void modifyAttributes(String name, ModificationItem[] mods)
0801:                    throws NamingException {
0802:                dirContext.modifyAttributes(parseName(name), mods);
0803:            }
0804:
0805:            /**
0806:             * Binds a name to an object, along with associated attributes. If attrs 
0807:             * is null, the resulting binding will have the attributes associated 
0808:             * with obj if obj is a DirContext, and no attributes otherwise. If attrs 
0809:             * is non-null, the resulting binding will have attrs as its attributes; 
0810:             * any attributes associated with obj are ignored.
0811:             * 
0812:             * @param name the name to bind; may not be empty
0813:             * @param obj the object to bind; possibly null
0814:             * @param attrs the attributes to associate with the binding
0815:             * @exception NameAlreadyBoundException if name is already bound
0816:             * @exception InvalidAttributesException if some "mandatory" attributes 
0817:             * of the binding are not supplied
0818:             * @exception NamingException if a naming exception is encountered
0819:             */
0820:            public void bind(Name name, Object obj, Attributes attrs)
0821:                    throws NamingException {
0822:                dirContext.bind(parseName(name), obj, attrs);
0823:            }
0824:
0825:            /**
0826:             * Binds a name to an object, along with associated attributes.
0827:             * 
0828:             * @param name the name to bind; may not be empty
0829:             * @param obj the object to bind; possibly null
0830:             * @param attrs the attributes to associate with the binding
0831:             * @exception NameAlreadyBoundException if name is already bound
0832:             * @exception InvalidAttributesException if some "mandatory" attributes 
0833:             * of the binding are not supplied
0834:             * @exception NamingException if a naming exception is encountered
0835:             */
0836:            public void bind(String name, Object obj, Attributes attrs)
0837:                    throws NamingException {
0838:                dirContext.bind(parseName(name), obj, attrs);
0839:            }
0840:
0841:            /**
0842:             * Binds a name to an object, along with associated attributes, 
0843:             * overwriting any existing binding. If attrs is null and obj is a 
0844:             * DirContext, the attributes from obj are used. If attrs is null and obj 
0845:             * is not a DirContext, any existing attributes associated with the object
0846:             * already bound in the directory remain unchanged. If attrs is non-null, 
0847:             * any existing attributes associated with the object already bound in 
0848:             * the directory are removed and attrs is associated with the named 
0849:             * object. If obj is a DirContext and attrs is non-null, the attributes 
0850:             * of obj are ignored.
0851:             * 
0852:             * @param name the name to bind; may not be empty
0853:             * @param obj the object to bind; possibly null
0854:             * @param attrs the attributes to associate with the binding
0855:             * @exception InvalidAttributesException if some "mandatory" attributes 
0856:             * of the binding are not supplied
0857:             * @exception NamingException if a naming exception is encountered
0858:             */
0859:            public void rebind(Name name, Object obj, Attributes attrs)
0860:                    throws NamingException {
0861:                dirContext.rebind(parseName(name), obj, attrs);
0862:            }
0863:
0864:            /**
0865:             * Binds a name to an object, along with associated attributes, 
0866:             * overwriting any existing binding.
0867:             * 
0868:             * @param name the name to bind; may not be empty
0869:             * @param obj the object to bind; possibly null
0870:             * @param attrs the attributes to associate with the binding
0871:             * @exception InvalidAttributesException if some "mandatory" attributes 
0872:             * of the binding are not supplied
0873:             * @exception NamingException if a naming exception is encountered
0874:             */
0875:            public void rebind(String name, Object obj, Attributes attrs)
0876:                    throws NamingException {
0877:                dirContext.rebind(parseName(name), obj, attrs);
0878:            }
0879:
0880:            /**
0881:             * Creates and binds a new context, along with associated attributes. 
0882:             * This method creates a new subcontext with the given name, binds it in 
0883:             * the target context (that named by all but terminal atomic component of 
0884:             * the name), and associates the supplied attributes with the newly 
0885:             * created object. All intermediate and target contexts must already 
0886:             * exist. If attrs is null, this method is equivalent to 
0887:             * Context.createSubcontext().
0888:             * 
0889:             * @param name the name of the context to create; may not be empty
0890:             * @param attrs the attributes to associate with the newly created context
0891:             * @return the newly created context
0892:             * @exception NameAlreadyBoundException if the name is already bound
0893:             * @exception InvalidAttributesException if attrs does not contain all 
0894:             * the mandatory attributes required for creation
0895:             * @exception NamingException if a naming exception is encountered
0896:             */
0897:            public DirContext createSubcontext(Name name, Attributes attrs)
0898:                    throws NamingException {
0899:                return dirContext.createSubcontext(parseName(name), attrs);
0900:            }
0901:
0902:            /**
0903:             * Creates and binds a new context, along with associated attributes.
0904:             * 
0905:             * @param name the name of the context to create; may not be empty
0906:             * @param attrs the attributes to associate with the newly created context
0907:             * @return the newly created context
0908:             * @exception NameAlreadyBoundException if the name is already bound
0909:             * @exception InvalidAttributesException if attrs does not contain all 
0910:             * the mandatory attributes required for creation
0911:             * @exception NamingException if a naming exception is encountered
0912:             */
0913:            public DirContext createSubcontext(String name, Attributes attrs)
0914:                    throws NamingException {
0915:                return dirContext.createSubcontext(parseName(name), attrs);
0916:            }
0917:
0918:            /**
0919:             * Retrieves the schema associated with the named object. The schema 
0920:             * describes rules regarding the structure of the namespace and the 
0921:             * attributes stored within it. The schema specifies what types of 
0922:             * objects can be added to the directory and where they can be added; 
0923:             * what mandatory and optional attributes an object can have. The range 
0924:             * of support for schemas is directory-specific.
0925:             * 
0926:             * @param name the name of the object whose schema is to be retrieved
0927:             * @return the schema associated with the context; never null
0928:             * @exception OperationNotSupportedException if schema not supported
0929:             * @exception NamingException if a naming exception is encountered
0930:             */
0931:            public DirContext getSchema(Name name) throws NamingException {
0932:                return dirContext.getSchema(parseName(name));
0933:            }
0934:
0935:            /**
0936:             * Retrieves the schema associated with the named object.
0937:             * 
0938:             * @param name the name of the object whose schema is to be retrieved
0939:             * @return the schema associated with the context; never null
0940:             * @exception OperationNotSupportedException if schema not supported
0941:             * @exception NamingException if a naming exception is encountered
0942:             */
0943:            public DirContext getSchema(String name) throws NamingException {
0944:                return dirContext.getSchema(parseName(name));
0945:            }
0946:
0947:            /**
0948:             * Retrieves a context containing the schema objects of the named 
0949:             * object's class definitions.
0950:             * 
0951:             * @param name the name of the object whose object class definition is to 
0952:             * be retrieved
0953:             * @return the DirContext containing the named object's class 
0954:             * definitions; never null
0955:             * @exception OperationNotSupportedException if schema not supported
0956:             * @exception NamingException if a naming exception is encountered
0957:             */
0958:            public DirContext getSchemaClassDefinition(Name name)
0959:                    throws NamingException {
0960:                return dirContext.getSchemaClassDefinition(parseName(name));
0961:            }
0962:
0963:            /**
0964:             * Retrieves a context containing the schema objects of the named 
0965:             * object's class definitions.
0966:             * 
0967:             * @param name the name of the object whose object class definition is to 
0968:             * be retrieved
0969:             * @return the DirContext containing the named object's class 
0970:             * definitions; never null
0971:             * @exception OperationNotSupportedException if schema not supported
0972:             * @exception NamingException if a naming exception is encountered
0973:             */
0974:            public DirContext getSchemaClassDefinition(String name)
0975:                    throws NamingException {
0976:                return dirContext.getSchemaClassDefinition(parseName(name));
0977:            }
0978:
0979:            /**
0980:             * Searches in a single context for objects that contain a specified set 
0981:             * of attributes, and retrieves selected attributes. The search is 
0982:             * performed using the default SearchControls settings.
0983:             * 
0984:             * @param name the name of the context to search
0985:             * @param matchingAttributes the attributes to search for. If empty or 
0986:             * null, all objects in the target context are returned.
0987:             * @param attributesToReturn the attributes to return. null indicates 
0988:             * that all attributes are to be returned; an empty array indicates that 
0989:             * none are to be returned.
0990:             * @return a non-null enumeration of SearchResult objects. Each 
0991:             * SearchResult contains the attributes identified by attributesToReturn 
0992:             * and the name of the corresponding object, named relative to the 
0993:             * context named by name.
0994:             * @exception NamingException if a naming exception is encountered
0995:             */
0996:            public NamingEnumeration search(Name name,
0997:                    Attributes matchingAttributes, String[] attributesToReturn)
0998:                    throws NamingException {
0999:                return dirContext.search(parseName(name), matchingAttributes,
1000:                        attributesToReturn);
1001:            }
1002:
1003:            /**
1004:             * Searches in a single context for objects that contain a specified set 
1005:             * of attributes, and retrieves selected attributes.
1006:             * 
1007:             * @param name the name of the context to search
1008:             * @param matchingAttributes the attributes to search for. If empty or 
1009:             * null, all objects in the target context are returned.
1010:             * @param attributesToReturn the attributes to return. null indicates 
1011:             * that all attributes are to be returned; an empty array indicates that 
1012:             * none are to be returned.
1013:             * @return a non-null enumeration of SearchResult objects. Each 
1014:             * SearchResult contains the attributes identified by attributesToReturn 
1015:             * and the name of the corresponding object, named relative to the 
1016:             * context named by name.
1017:             * @exception NamingException if a naming exception is encountered
1018:             */
1019:            public NamingEnumeration search(String name,
1020:                    Attributes matchingAttributes, String[] attributesToReturn)
1021:                    throws NamingException {
1022:                return dirContext.search(parseName(name), matchingAttributes,
1023:                        attributesToReturn);
1024:            }
1025:
1026:            /**
1027:             * Searches in a single context for objects that contain a specified set 
1028:             * of attributes. This method returns all the attributes of such objects. 
1029:             * It is equivalent to supplying null as the atributesToReturn parameter 
1030:             * to the method search(Name, Attributes, String[]).
1031:             * 
1032:             * @param name the name of the context to search
1033:             * @param matchingAttributes the attributes to search for. If empty or 
1034:             * null, all objects in the target context are returned.
1035:             * @return a non-null enumeration of SearchResult objects. Each 
1036:             * SearchResult contains the attributes identified by attributesToReturn 
1037:             * and the name of the corresponding object, named relative to the 
1038:             * context named by name.
1039:             * @exception NamingException if a naming exception is encountered
1040:             */
1041:            public NamingEnumeration search(Name name,
1042:                    Attributes matchingAttributes) throws NamingException {
1043:                return dirContext.search(parseName(name), matchingAttributes);
1044:            }
1045:
1046:            /**
1047:             * Searches in a single context for objects that contain a specified set 
1048:             * of attributes.
1049:             * 
1050:             * @param name the name of the context to search
1051:             * @param matchingAttributes the attributes to search for. If empty or 
1052:             * null, all objects in the target context are returned.
1053:             * @return a non-null enumeration of SearchResult objects. Each 
1054:             * SearchResult contains the attributes identified by attributesToReturn 
1055:             * and the name of the corresponding object, named relative to the 
1056:             * context named by name.
1057:             * @exception NamingException if a naming exception is encountered
1058:             */
1059:            public NamingEnumeration search(String name,
1060:                    Attributes matchingAttributes) throws NamingException {
1061:                return dirContext.search(parseName(name), matchingAttributes);
1062:            }
1063:
1064:            /**
1065:             * Searches in the named context or object for entries that satisfy the 
1066:             * given search filter. Performs the search as specified by the search 
1067:             * controls.
1068:             * 
1069:             * @param name the name of the context or object to search
1070:             * @param filter the filter expression to use for the search; may not be 
1071:             * null
1072:             * @param cons the search controls that control the search. If null, 
1073:             * the default search controls are used (equivalent to 
1074:             * (new SearchControls())).
1075:             * @return an enumeration of SearchResults of the objects that satisfy 
1076:             * the filter; never null
1077:             * @exception InvalidSearchFilterException if the search filter specified 
1078:             * is not supported or understood by the underlying directory
1079:             * @exception InvalidSearchControlsException if the search controls 
1080:             * contain invalid settings
1081:             * @exception NamingException if a naming exception is encountered
1082:             */
1083:            public NamingEnumeration search(Name name, String filter,
1084:                    SearchControls cons) throws NamingException {
1085:                return dirContext.search(parseName(name), filter, cons);
1086:            }
1087:
1088:            /**
1089:             * Searches in the named context or object for entries that satisfy the 
1090:             * given search filter. Performs the search as specified by the search 
1091:             * controls.
1092:             * 
1093:             * @param name the name of the context or object to search
1094:             * @param filter the filter expression to use for the search; may not be 
1095:             * null
1096:             * @param cons the search controls that control the search. If null, 
1097:             * the default search controls are used (equivalent to 
1098:             * (new SearchControls())).
1099:             * @return an enumeration of SearchResults of the objects that satisfy 
1100:             * the filter; never null
1101:             * @exception InvalidSearchFilterException if the search filter 
1102:             * specified is not supported or understood by the underlying directory
1103:             * @exception InvalidSearchControlsException if the search controls 
1104:             * contain invalid settings
1105:             * @exception NamingException if a naming exception is encountered
1106:             */
1107:            public NamingEnumeration search(String name, String filter,
1108:                    SearchControls cons) throws NamingException {
1109:                return dirContext.search(parseName(name), filter, cons);
1110:            }
1111:
1112:            /**
1113:             * Searches in the named context or object for entries that satisfy the 
1114:             * given search filter. Performs the search as specified by the search 
1115:             * controls.
1116:             * 
1117:             * @param name the name of the context or object to search
1118:             * @param filterExpr the filter expression to use for the search. 
1119:             * The expression may contain variables of the form "{i}" where i is a 
1120:             * nonnegative integer. May not be null.
1121:             * @param filterArgs the array of arguments to substitute for the 
1122:             * variables in filterExpr. The value of filterArgs[i] will replace each 
1123:             * occurrence of "{i}". If null, equivalent to an empty array.
1124:             * @param cons the search controls that control the search. If null, the 
1125:             * default search controls are used (equivalent to (new SearchControls())).
1126:             * @return an enumeration of SearchResults of the objects that satisy the 
1127:             * filter; never null
1128:             * @exception ArrayIndexOutOfBoundsException if filterExpr contains {i} 
1129:             * expressions where i is outside the bounds of the array filterArgs
1130:             * @exception InvalidSearchControlsException if cons contains invalid 
1131:             * settings
1132:             * @exception InvalidSearchFilterException if filterExpr with filterArgs 
1133:             * represents an invalid search filter
1134:             * @exception NamingException if a naming exception is encountered
1135:             */
1136:            public NamingEnumeration search(Name name, String filterExpr,
1137:                    Object[] filterArgs, SearchControls cons)
1138:                    throws NamingException {
1139:                return dirContext.search(parseName(name), filterExpr,
1140:                        filterArgs, cons);
1141:            }
1142:
1143:            /**
1144:             * Searches in the named context or object for entries that satisfy the 
1145:             * given search filter. Performs the search as specified by the search 
1146:             * controls.
1147:             * 
1148:             * @param name the name of the context or object to search
1149:             * @param filterExpr the filter expression to use for the search. 
1150:             * The expression may contain variables of the form "{i}" where i is a 
1151:             * nonnegative integer. May not be null.
1152:             * @param filterArgs the array of arguments to substitute for the 
1153:             * variables in filterExpr. The value of filterArgs[i] will replace each 
1154:             * occurrence of "{i}". If null, equivalent to an empty array.
1155:             * @param cons the search controls that control the search. If null, the 
1156:             * default search controls are used (equivalent to (new SearchControls())).
1157:             * @return an enumeration of SearchResults of the objects that satisy the 
1158:             * filter; never null
1159:             * @exception ArrayIndexOutOfBoundsException if filterExpr contains {i} 
1160:             * expressions where i is outside the bounds of the array filterArgs
1161:             * @exception InvalidSearchControlsException if cons contains invalid 
1162:             * settings
1163:             * @exception InvalidSearchFilterException if filterExpr with filterArgs 
1164:             * represents an invalid search filter
1165:             * @exception NamingException if a naming exception is encountered
1166:             */
1167:            public NamingEnumeration search(String name, String filterExpr,
1168:                    Object[] filterArgs, SearchControls cons)
1169:                    throws NamingException {
1170:                return dirContext.search(parseName(name), filterExpr,
1171:                        filterArgs, cons);
1172:            }
1173:
1174:            // ------------------------------------------------------ Protected Methods
1175:
1176:            /**
1177:             * Parses a name.
1178:             * 
1179:             * @return the parsed name
1180:             */
1181:            protected String parseName(String name) throws NamingException {
1182:                return name;
1183:            }
1184:
1185:            /**
1186:             * Parses a name.
1187:             * 
1188:             * @return the parsed name
1189:             */
1190:            protected Name parseName(Name name) throws NamingException {
1191:                return name;
1192:            }
1193:
1194:            /**
1195:             * Lookup in cache.
1196:             */
1197:            protected CacheEntry cacheLookupAndLoad(String name) {
1198:                if (cache == null)
1199:                    return (null);
1200:                CacheEntry cacheEntry = (CacheEntry) cache.get(name);
1201:                if (cacheEntry == null) {
1202:                    cacheEntry = new CacheEntry();
1203:                    cacheEntry.name = name;
1204:                    // Load entry
1205:                    if (!cacheLoad(cacheEntry))
1206:                        return null;
1207:                    return (cacheEntry);
1208:                } else {
1209:                    if (!validate(cacheEntry)) {
1210:                        if (!revalidate(cacheEntry)) {
1211:                            cacheUnload(cacheEntry.name);
1212:                            return (null);
1213:                        } else {
1214:                            cacheEntry.timestamp = System.currentTimeMillis()
1215:                                    + cacheTTL;
1216:                        }
1217:                    }
1218:                    return (cacheEntry);
1219:                }
1220:            }
1221:
1222:            /**
1223:             * Validate entry.
1224:             */
1225:            protected boolean validate(CacheEntry entry) {
1226:                if ((entry.resource != null)
1227:                //            && (entry.resource.getContent() != null) 
1228:                        && (System.currentTimeMillis() < entry.timestamp)) {
1229:                    return true;
1230:                }
1231:                return false;
1232:            }
1233:
1234:            /**
1235:             * Revalidate entry.
1236:             */
1237:            protected boolean revalidate(CacheEntry entry) {
1238:                // Get the attributes at the given path, and check the last 
1239:                // modification date
1240:                if (entry.attributes == null)
1241:                    return false;
1242:                long lastModified = AttributeHelper
1243:                        .getLastModified(entry.attributes);
1244:                //        long contentLength = entry.attributes.getContentLength();
1245:                if (lastModified <= 0)
1246:                    return false;
1247:                try {
1248:                    //             Attributes tempAttributes = dirContext.getAttributes(entry.name);
1249:                    Attributes attributes = dirContext
1250:                            .getAttributes(entry.name);
1251:                    //             ResourceAttributes attributes = null;
1252:                    //             if (!(tempAttributes instanceof ResourceAttributes)) {
1253:                    //                 attributes = new ResourceAttributes(tempAttributes);
1254:                    //             } else {
1255:                    //                 attributes = (ResourceAttributes) tempAttributes;
1256:                    //             }
1257:                    long lastModified2 = AttributeHelper
1258:                            .getLastModified(attributes);
1259:                    //            long contentLength2 = attributes.getContentLength();
1260:                    return (lastModified == lastModified2);
1261:                    //  && (contentLength == contentLength2);
1262:                } catch (NamingException e) {
1263:                    return false;
1264:                }
1265:            }
1266:
1267:            /**
1268:             * Load entry into cache.
1269:             */
1270:            protected boolean cacheLoad(CacheEntry entry) {
1271:
1272:                if (cache == null)
1273:                    return false;
1274:
1275:                String name = entry.name;
1276:
1277:                // Retrieve missing info
1278:
1279:                // Retrieving attributes
1280:                if (entry.attributes == null) {
1281:                    try {
1282:                        Attributes attributes = dirContext
1283:                                .getAttributes(entry.name);
1284:                        //                 if (!(attributes instanceof ResourceAttributes)) {
1285:                        //                     entry.attributes = 
1286:                        //                         new ResourceAttributes(attributes);
1287:                        //                 } else {
1288:                        //                     entry.attributes = (ResourceAttributes) attributes;
1289:                        //                 }
1290:                    } catch (NamingException e) {
1291:                        return false;
1292:                    }
1293:                }
1294:
1295:                // Retriving object
1296:                if ((entry.resource == null) && (entry.context == null)) {
1297:                    try {
1298:                        Object object = dirContext.lookup(name);
1299:                        //                 if (object instanceof InputStream) {
1300:                        //                     entry.resource = new Resource((InputStream) object);
1301:                        //                 } else if (object instanceof DirContext) {
1302:                        //                     entry.context = (DirContext) object;
1303:                        //                 } else if (object instanceof Resource) {
1304:                        //                     entry.resource = (Resource) object;
1305:                        //                 } else {
1306:                        //                     entry.resource = new Resource(new ByteArrayInputStream
1307:                        //                         (object.toString().getBytes()));
1308:                        //                 }
1309:                        entry.resource = object;
1310:                    } catch (NamingException e) {
1311:                        return false;
1312:                    }
1313:                }
1314:
1315:                // TODO: lazy loading. We may list a dir, there's no reason to load
1316:                // all entries ( we may just look at attributes )
1317:
1318:                // Load object content. We cache entries without content ( users, etc ) 
1319:                /*
1320:                  if ((entry.resource != null) && (entry.resource.getContent() == null) 
1321:                    //            && (entry.attributes.getContentLength() >= 0)
1322:                    && (entry.attributes.getContentLength() < cacheObjectMaxSize)) {
1323:                    int length = (int) entry.attributes.getContentLength();
1324:                    InputStream is = null;
1325:                    try {
1326:                        is = entry.resource.streamContent();
1327:                        int pos = 0;
1328:                        byte[] b = new byte[length];
1329:                        while (pos < length) {
1330:                            int n = is.read(b, pos, length - pos);
1331:                            if (n < 0)
1332:                                break;
1333:                            pos = pos + n;
1334:                        }
1335:                        entry.resource.setContent(b);
1336:                    } catch (IOException e) {
1337:                        ; // Ignore
1338:                    } finally {
1339:                        try {
1340:                            if (is != null)
1341:                                is.close();
1342:                        } catch (IOException e) {
1343:                            ; // Ignore
1344:                        }
1345:                    }
1346:                }
1347:                 */
1348:                // Set timestamp
1349:                entry.timestamp = System.currentTimeMillis() + cacheTTL;
1350:
1351:                // Add new entry to cache
1352:                cache.put(name, entry);
1353:
1354:                return true;
1355:
1356:            }
1357:
1358:            /**
1359:             * Remove entry from cache.
1360:             */
1361:            protected boolean cacheUnload(String name) {
1362:                if (cache == null)
1363:                    return false;
1364:                return (cache.remove(name) != null);
1365:            }
1366:
1367:            // ------------------------------------------------- CacheEntry Inner Class
1368:
1369:            protected class CacheEntry {
1370:
1371:                // ------------------------------------------------- Instance Variables
1372:
1373:                long timestamp = -1;
1374:                String name = null;
1375:                Attributes attributes = null;
1376:                //ResourceAttributes attributes = null;
1377:                Object resource = null;
1378:                DirContext context = null;
1379:
1380:                // ----------------------------------------------------- Public Methods
1381:
1382:                public void recycle() {
1383:                    timestamp = -1;
1384:                    name = null;
1385:                    attributes = null;
1386:                    resource = null;
1387:                    context = null;
1388:                }
1389:
1390:                public String toString() {
1391:                    return ("Cache entry: " + name + "\n" + "Attributes: "
1392:                            + attributes + "\n" + "Resource: " + resource
1393:                            + "\n" + "Context: " + context);
1394:                }
1395:
1396:            }
1397:
1398:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.