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


001:        /*
002:         *  Copyright 1999-2004 The Apache Software Foundation
003:         *
004:         *  Licensed under the Apache License, Version 2.0 (the "License");
005:         *  you may not use this file except in compliance with the License.
006:         *  You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         *  Unless required by applicable law or agreed to in writing, software
011:         *  distributed under the License is distributed on an "AS IS" BASIS,
012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         *  See the License for the specific language governing permissions and
014:         *  limitations under the License.
015:         */
016:
017:        package org.apache.naming.modules.memory;
018:
019:        import java.util.Hashtable;
020:
021:        import javax.naming.Context;
022:        import javax.naming.InitialContext;
023:        import javax.naming.LinkRef;
024:        import javax.naming.Name;
025:        import javax.naming.NameNotFoundException;
026:        import javax.naming.NamingEnumeration;
027:        import javax.naming.NamingException;
028:        import javax.naming.NotContextException;
029:        import javax.naming.Reference;
030:        import javax.naming.Referenceable;
031:        import javax.naming.directory.Attributes;
032:        import javax.naming.directory.DirContext;
033:        import javax.naming.spi.NamingManager;
034:
035:        import org.apache.naming.core.BaseDirContext;
036:        import org.apache.naming.core.NamingContextEnumeration;
037:        import org.apache.naming.core.NamingEntry;
038:        import org.apache.tomcat.util.res.StringManager;
039:
040:        /**
041:         * In-memory context.
042:         *
043:         * IMPLEMENTATION. We use NamingEntry stored in a tree of hashtables. 
044:         *
045:         * @author Remy Maucherat
046:         * @author Costin Manolache
047:         */
048:        public class MemoryNamingContext extends BaseDirContext {
049:
050:            public MemoryNamingContext() throws NamingException {
051:                super ();
052:            }
053:
054:            /**
055:             * Builds a naming context using the given environment.
056:             */
057:            public MemoryNamingContext(Hashtable env) throws NamingException {
058:                super (env);
059:                this .bindings = new Hashtable();
060:            }
061:
062:            // ----------------------------------------------------- Instance Variables
063:
064:            /**
065:             * The string manager for this package.
066:             */
067:            protected static StringManager sm = StringManager
068:                    .getManager("org.apache.naming.res");
069:
070:            /**
071:             * Bindings in this Context.
072:             */
073:            protected Hashtable bindings;
074:
075:            public void setBindings(Hashtable bindings) {
076:                this .bindings = bindings;
077:            }
078:
079:            // -------------------------------------------------------- Context Methods
080:
081:            /**
082:             * Unbinds the named object. Removes the terminal atomic name in name 
083:             * from the target context--that named by all but the terminal atomic 
084:             * part of name.
085:             * <p>
086:             * This method is idempotent. It succeeds even if the terminal atomic 
087:             * name is not bound in the target context, but throws 
088:             * NameNotFoundException if any of the intermediate contexts do not exist. 
089:             * 
090:             * @param name the name to bind; may not be empty
091:             * @exception NameNotFoundException if an intermediate context does not 
092:             * exist
093:             * @exception NamingException if a naming exception is encountered
094:             */
095:            public void unbind(Name name, boolean isContext)
096:                    throws NamingException {
097:                checkWritable(name);
098:
099:                while ((!name.isEmpty()) && (name.get(0).length() == 0))
100:                    name = name.getSuffix(1);
101:
102:                if (name.isEmpty())
103:                    throw new NamingException(sm
104:                            .getString("namingContext.invalidName"));
105:
106:                NamingEntry entry = (NamingEntry) bindings.get(name.get(0));
107:
108:                if (entry == null) {
109:                    throw new NameNotFoundException(sm.getString(
110:                            "namingContext.nameNotBound", name.get(0)));
111:                }
112:
113:                if (name.size() > 1) {
114:                    if (entry.type == NamingEntry.CONTEXT) {
115:                        ((Context) entry.value).unbind(name.getSuffix(1));
116:                    } else {
117:                        throw new NamingException(sm
118:                                .getString("namingContext.contextExpected"));
119:                    }
120:                } else {
121:                    if (entry.type == NamingEntry.CONTEXT) {
122:                        ((Context) entry.value).close();
123:                        bindings.remove(name.get(0));
124:                    } else {
125:                        if (isContext) {
126:                            throw new NotContextException(sm
127:                                    .getString("namingContext.contextExpected"));
128:                        } else {
129:                            bindings.remove(name.get(0));
130:                        }
131:                    }
132:                }
133:            }
134:
135:            /**
136:             * Enumerates the names bound in the named context, along with the class 
137:             * names of objects bound to them. The contents of any subcontexts are 
138:             * not included.
139:             * <p>
140:             * If a binding is added to or removed from this context, its effect on 
141:             * an enumeration previously returned is undefined.
142:             * 
143:             * @param name the name of the context to list
144:             * @return an enumeration of the names and class names of the bindings in 
145:             * this context. Each element of the enumeration is of type NameClassPair.
146:             * @exception NamingException if a naming exception is encountered
147:             */
148:            public NamingEnumeration list(Name name) throws NamingException {
149:                // Removing empty parts
150:                while ((!name.isEmpty()) && (name.get(0).length() == 0))
151:                    name = name.getSuffix(1);
152:
153:                if (name.isEmpty()) {
154:                    return new NamingContextEnumeration(bindings.elements(),
155:                            this , false);
156:                }
157:
158:                NamingEntry entry = (NamingEntry) bindings.get(name.get(0));
159:
160:                if (entry == null) {
161:                    throw new NameNotFoundException(sm.getString(
162:                            "namingContext.nameNotBound", name.get(0)));
163:                }
164:
165:                if (entry.type != NamingEntry.CONTEXT) {
166:                    throw new NamingException(sm
167:                            .getString("namingContext.contextExpected"));
168:                }
169:                return ((Context) entry.value).list(name.getSuffix(1));
170:            }
171:
172:            private Name removeEmptyPrefix(Name name) {
173:                while ((!name.isEmpty()) && (name.get(0).length() == 0))
174:                    name = name.getSuffix(1);
175:                return name;
176:            }
177:
178:            /**
179:             * Enumerates the names bound in the named context, along with the 
180:             * objects bound to them. The contents of any subcontexts are not 
181:             * included.
182:             * <p>
183:             * If a binding is added to or removed from this context, its effect on 
184:             * an enumeration previously returned is undefined.
185:             * 
186:             * @param name the name of the context to list
187:             * @return an enumeration of the bindings in this context. 
188:             * Each element of the enumeration is of type Binding.
189:             * @exception NamingException if a naming exception is encountered
190:             */
191:            public NamingEnumeration listBindings(Name name)
192:                    throws NamingException {
193:                // Removing empty parts
194:                while ((!name.isEmpty()) && (name.get(0).length() == 0))
195:                    name = name.getSuffix(1);
196:
197:                if (name.isEmpty()) {
198:                    return new NamingContextEnumeration(bindings.elements(),
199:                            this , true);
200:                }
201:
202:                NamingEntry entry = (NamingEntry) bindings.get(name.get(0));
203:
204:                if (entry == null) {
205:                    throw new NameNotFoundException(sm.getString(
206:                            "namingContext.nameNotBound", name.get(0)));
207:                }
208:
209:                if (entry.type != NamingEntry.CONTEXT) {
210:                    throw new NamingException(sm
211:                            .getString("namingContext.contextExpected"));
212:                }
213:                return ((Context) entry.value).listBindings(name.getSuffix(1));
214:            }
215:
216:            /**
217:             * Creates and binds a new context. Creates a new context with the given 
218:             * name and binds it in the target context (that named by all but 
219:             * terminal atomic component of the name). All intermediate contexts and 
220:             * the target context must already exist.
221:             * 
222:             * @param name the name of the context to create; may not be empty
223:             * @return the newly created context
224:             * @exception NameAlreadyBoundException if name is already bound
225:             * @exception InvalidAttributesException if creation of the subcontext 
226:             * requires specification of mandatory attributes
227:             * @exception NamingException if a naming exception is encountered
228:             */
229:            public DirContext createSubcontext(Name name, Attributes attrs)
230:                    throws NamingException {
231:                checkWritable(name);
232:
233:                DirContext newContext = new MemoryNamingContext(env);
234:                bind(name, newContext);
235:                return newContext;
236:            }
237:
238:            // XXX Make it iterative, less objects
239:            private NamingEntry findNamingEntry(Name name, boolean resolveLinks)
240:                    throws NamingException {
241:                if (name.isEmpty()) {
242:                    //             // If name is empty, a newly allocated naming context is returned
243:                    //             MemoryNamingContext mmc= new MemoryNamingContext(env);
244:                    //             mmc.setBindings( bindings );
245:                    //             return mmc;
246:                    return null;
247:                }
248:
249:                NamingEntry entry = (NamingEntry) bindings.get(name.get(0));
250:
251:                if (entry == null) {
252:                    throw new NameNotFoundException(sm.getString(
253:                            "namingContext.nameNotBound", name.get(0)));
254:                }
255:
256:                if (name.size() > 1) {
257:                    // If the size of the name is greater that 1, then we go through a
258:                    // number of subcontexts.
259:                    if (entry.type != NamingEntry.CONTEXT) {
260:                        throw new NamingException(sm
261:                                .getString("namingContext.contextExpected"));
262:                    }
263:                    return entry;
264:                } else {
265:                    return entry;
266:                }
267:            }
268:
269:            public Object lookup(Name name, boolean resolveLinks)
270:                    throws NamingException {
271:                // Removing empty parts
272:                while ((!name.isEmpty()) && (name.get(0).length() == 0))
273:                    name = name.getSuffix(1);
274:
275:                NamingEntry entry = findNamingEntry(name, resolveLinks);
276:
277:                if (entry.type == NamingEntry.CONTEXT) {
278:                    return ((BaseDirContext) entry.value).lookup(name
279:                            .getSuffix(1), resolveLinks);
280:                }
281:
282:                if ((resolveLinks) && (entry.type == NamingEntry.LINK_REF)) {
283:                    String link = ((LinkRef) entry.value).getLinkName();
284:                    if (link.startsWith(".")) {
285:                        // Link relative to this context
286:                        return lookup(link.substring(1));
287:                    } else {
288:                        return (new InitialContext(env)).lookup(link);
289:                    }
290:                } else if (entry.type == NamingEntry.REFERENCE) {
291:                    try {
292:                        Object obj = NamingManager.getObjectInstance(
293:                                entry.value, name, this , env);
294:                        if (obj != null) {
295:                            entry.value = obj;
296:                            entry.type = NamingEntry.ENTRY;
297:                        }
298:                        return obj;
299:                    } catch (NamingException e) {
300:                        throw e;
301:                    } catch (Exception e) {
302:                        throw new NamingException(e.getMessage());
303:                    }
304:                } else {
305:                    return entry.value;
306:                }
307:            }
308:
309:            /**
310:             * Binds a name to an object. All intermediate contexts and the target 
311:             * context (that named by all but terminal atomic component of the name) 
312:             * must already exist.
313:             * 
314:             * @param name the name to bind; may not be empty
315:             * @param object the object to bind; possibly null
316:             * @param rebind if true, then perform a rebind (ie, overwrite)
317:             * @exception NameAlreadyBoundException if name is already bound
318:             * @exception InvalidAttributesException if object did not supply all 
319:             * mandatory attributes
320:             * @exception NamingException if a naming exception is encountered
321:             */
322:            public void bind(Name name, Object obj, Attributes attrs,
323:                    boolean rebind) throws NamingException {
324:                checkWritable(name);
325:
326:                while ((!name.isEmpty()) && (name.get(0).length() == 0))
327:                    name = name.getSuffix(1);
328:
329:                if (name.isEmpty())
330:                    throw new NamingException(sm
331:                            .getString("namingContext.invalidName"));
332:
333:                NamingEntry entry = (NamingEntry) bindings.get(name.get(0));
334:
335:                if (name.size() > 1) {
336:                    if (entry == null) {
337:                        throw new NameNotFoundException(sm.getString(
338:                                "namingContext.nameNotBound", name.get(0)));
339:                    }
340:                    if (entry.type == NamingEntry.CONTEXT) {
341:                        if (rebind) {
342:                            ((Context) entry.value).rebind(name.getSuffix(1),
343:                                    obj);
344:                        } else {
345:                            ((Context) entry.value)
346:                                    .bind(name.getSuffix(1), obj);
347:                        }
348:                    } else {
349:                        throw new NamingException(sm
350:                                .getString("namingContext.contextExpected"));
351:                    }
352:                } else {
353:                    if ((!rebind) && (entry != null)) {
354:                        throw new NamingException(sm.getString(
355:                                "namingContext.alreadyBound", name.get(0)));
356:                    } else {
357:                        // Getting the type of the object and wrapping it within a new
358:                        // NamingEntry
359:                        Object toBind = NamingManager.getStateToBind(obj, name,
360:                                this , env);
361:                        if (toBind instanceof  Context) {
362:                            entry = new NamingEntry(name.get(0), toBind, attrs,
363:                                    NamingEntry.CONTEXT);
364:                        } else if (toBind instanceof  LinkRef) {
365:                            entry = new NamingEntry(name.get(0), toBind, attrs,
366:                                    NamingEntry.LINK_REF);
367:                        } else if (toBind instanceof  Reference) {
368:                            entry = new NamingEntry(name.get(0), toBind, attrs,
369:                                    NamingEntry.REFERENCE);
370:                        } else if (toBind instanceof  Referenceable) {
371:                            toBind = ((Referenceable) toBind).getReference();
372:                            entry = new NamingEntry(name.get(0), toBind, attrs,
373:                                    NamingEntry.REFERENCE);
374:                        } else {
375:                            entry = new NamingEntry(name.get(0), toBind, attrs,
376:                                    NamingEntry.ENTRY);
377:                        }
378:                        bindings.put(name.get(0), entry);
379:                    }
380:                }
381:            }
382:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.