Source Code Cross Referenced for CosContext.java in  » Net » Coadunation_1.0.1 » com » rift » coad » lib » naming » cos » 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 » Net » Coadunation_1.0.1 » com.rift.coad.lib.naming.cos 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * CoadunationLib: The coaduntion implementation library.
003:         * Copyright (C) 2006  Rift IT Contracting
004:         * 
005:         * This library is free software; you can redistribute it and/or
006:         * modify it under the terms of the GNU Lesser General Public
007:         * License as published by the Free Software Foundation; either
008:         * version 2.1 of the License, or (at your option) any later version.
009:         * 
010:         * This library is distributed in the hope that it will be useful,
011:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
012:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013:         * Lesser General Public License for more details.
014:         * 
015:         * You should have received a copy of the GNU Lesser General Public
016:         * License along with this library; if not, write to the Free Software
017:         * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
018:         *
019:         * CosContext.java
020:         *
021:         * This object wrapps the complexity of speaking to the Cos Naming Server.
022:         */
023:
024:        // package path
025:        package com.rift.coad.lib.naming.cos;
026:
027:        // java imports
028:        import java.io.Serializable;
029:        import java.rmi.Remote;
030:        import java.util.Hashtable;
031:        import javax.naming.Context;
032:        import javax.naming.InitialContext;
033:        import javax.naming.Name;
034:        import javax.naming.NameParser;
035:        import javax.naming.NamingEnumeration;
036:        import javax.naming.NamingException;
037:        import javax.naming.OperationNotSupportedException;
038:        import javax.naming.Reference;
039:        import javax.naming.Referenceable;
040:        import javax.rmi.PortableRemoteObject;
041:        import org.omg.CORBA.Any;
042:        import org.omg.CORBA.TypeCode;
043:        import org.omg.CORBA.portable.ObjectImpl;
044:        import org.omg.PortableServer.POA;
045:        import org.omg.DynamicAny.DynAny;
046:        import org.omg.DynamicAny.DynAnyFactory;
047:        import org.omg.DynamicAny.DynAnyFactoryHelper;
048:        import org.omg.CosNaming.NamingContext;
049:        import org.omg.CosNaming.NamingContextHelper;
050:        import org.omg.PortableServer.Servant;
051:        import org.omg.PortableServer.ForwardRequest;
052:        import org.omg.PortableServer.IdAssignmentPolicyValue;
053:        import org.omg.PortableServer.LifespanPolicyValue;
054:        import org.omg.PortableServer.RequestProcessingPolicyValue;
055:        import javax.rmi.CORBA.Util;
056:
057:        // logging import
058:        import org.apache.log4j.Logger;
059:
060:        // carol imports
061:        // We use the carol objects to store none org.omg.CORBA.Object values in the
062:        // Cos naming server. It is not recommened that information other than corba
063:        // objects get stored in the cos naming server but this gives us a way to do it
064:        // if someone requires this functionality.
065:        import org.objectweb.carol.jndi.wrapping.JNDIRemoteResource;
066:        import org.objectweb.carol.jndi.wrapping.JNDIReferenceWrapper;
067:        import org.objectweb.carol.jndi.wrapping.JNDIResourceWrapper;
068:
069:        // coadunation imports
070:        import com.rift.coad.lib.thread.CoadunationThreadGroup;
071:        import com.rift.coad.lib.configuration.Configuration;
072:        import com.rift.coad.lib.configuration.ConfigurationFactory;
073:        import com.rift.coad.lib.naming.NamingConstants;
074:        import com.rift.coad.lib.naming.OrbManager;
075:        import com.rift.coad.lib.thread.BasicThread;
076:        import com.rift.coad.lib.thread.ThreadStateMonitor;
077:        import com.rift.coad.lib.naming.NamingDirector;
078:
079:        /**
080:         * This object wrapps the complexity of speaking to the Cos Naming Server.
081:         *
082:         * @author Brett Chaldecott
083:         */
084:        public class CosContext implements  Context {
085:
086:            /**
087:             * The class responsible for binding this nameserver context to the master
088:             * nameserver context.
089:             */
090:            public class InstanceBinder extends BasicThread {
091:
092:                // the classes private member variables
093:                private ThreadStateMonitor stateMonitor = new ThreadStateMonitor(
094:                        60 * 1000);
095:
096:                /**
097:                 * The constructor of the instance binder thread.
098:                 *
099:                 * @exception Exception
100:                 */
101:                public InstanceBinder() throws Exception {
102:
103:                }
104:
105:                /**
106:                 * This method is responsible for performing the processing in the cos
107:                 * context.
108:                 */
109:                public void process() {
110:                    while (!stateMonitor.isTerminated()) {
111:                        try {
112:                            log
113:                                    .info("Making a connection to the name server context");
114:                            NamingContext namingContext = NamingContextHelper
115:                                    .narrow(orbManager.getORB()
116:                                            .string_to_object(instanceCosURL));
117:                            log.info("Binding to the primary context ["
118:                                    + masterCosURL + "]");
119:                            Context context = getInitialContext(instanceURL);
120:
121:                            log.info("Loop through the instance url and add ["
122:                                    + instanceURL.toString() + "]");
123:                            for (int index = 0; index < instanceURL.size() - 1; index++) {
124:                                try {
125:                                    context = (Context) context
126:                                            .lookup(instanceURL.get(index));
127:                                } catch (javax.naming.NameNotFoundException ex) {
128:                                    context = context
129:                                            .createSubcontext(instanceURL
130:                                                    .get(index));
131:                                }
132:                            }
133:                            log.info("Add entry to the context");
134:                            context
135:                                    .rebind(instanceURL
136:                                            .get(instanceURL.size() - 1),
137:                                            namingContext);
138:                            log
139:                                    .info("Registered with master Cos Naming service");
140:                            break;
141:                        } catch (Exception ex) {
142:                            log.error(
143:                                    "Failed to bind to the mater cos name server : "
144:                                            + ex.getMessage(), ex);
145:                        }
146:                        stateMonitor.monitor();
147:                    }
148:                }
149:
150:                /**
151:                 * The terminate method.
152:                 */
153:                public void terminate() {
154:                    stateMonitor.terminate(true);
155:                }
156:            }
157:
158:            // the class log variable
159:            protected Logger log = Logger.getLogger(CosContext.class.getName());
160:
161:            // class constants
162:            private final static String SUN_COS_CONTEXT_FACTORY = "com.sun.jndi.cosnaming.CNCtxFactory";
163:            private final static String PRIMARY = "primary";
164:            private final static String INSTANCE_COS_URL = "instance_cos_url";
165:            private final static String MASTER_COS_URL = "master_cos_url";
166:            private final static String USER = "cos_user";
167:
168:            // class private member variables
169:            private Hashtable env = null;
170:            private CoadunationThreadGroup threadGroup = null;
171:            private OrbManager orbManager = null;
172:            private Name base = null;
173:            private Name instanceURL = null;
174:            private String instanceCosURL = null;
175:            private Context instanceContext = null;
176:            private boolean primary = false;
177:            private String masterCosURL = null;
178:            private Context masterContext = null;
179:            private InstanceBinder instanceBinder = null;
180:
181:            /**
182:             * Creates a new instance of CosContext
183:             * 
184:             * @param env The environment of the cos context.
185:             * @param threadGroup The reference to the coadunation thread group.
186:             * @param instanceId The id of the coadunation instance.
187:             * @exception NamingException
188:             */
189:            public CosContext(Hashtable env,
190:                    CoadunationThreadGroup threadGroup, OrbManager orbManager,
191:                    String instanceId) throws NamingException {
192:                try {
193:                    this .env = env;
194:                    this .threadGroup = threadGroup.createThreadGroup();
195:                    this .orbManager = orbManager;
196:                    Configuration config = ConfigurationFactory.getInstance()
197:                            .getConfig(this .getClass());
198:
199:                    instanceCosURL = config.getString(INSTANCE_COS_URL);
200:                    if (config.getBoolean(PRIMARY)) {
201:                        instanceURL = new NamingParser().parse(config
202:                                .getString(NamingDirector.PRIMARY_URL));
203:                        base = instanceURL;
204:                        primary = true;
205:                        log.info("Running as primary");
206:                    } else {
207:                        instanceURL = new NamingParser().parse(
208:                                config.getString(NamingDirector.PRIMARY_URL))
209:                                .add(NamingConstants.SUBCONTEXT)
210:                                .add(instanceId);
211:
212:                        base = new NamingParser().parse("");
213:                        masterCosURL = config.getString(MASTER_COS_URL);
214:
215:                        // instanciate the new instance binder thread thread.
216:                        instanceBinder = new InstanceBinder();
217:                        threadGroup.addThread(instanceBinder, config
218:                                .getString(USER));
219:                        instanceBinder.start();
220:                        log.info("Running as secondary");
221:
222:                    }
223:
224:                } catch (Exception ex) {
225:                    log.error("Failed to create a new URL context : "
226:                            + ex.getMessage());
227:                    throw new NamingException(
228:                            "Failed to create a new URL context : "
229:                                    + ex.getMessage());
230:                }
231:            }
232:
233:            /**
234:             * Adds a new environment property to the environment of this context.
235:             *
236:             * @return The previous value of the property or null.
237:             * @param propName The property to replace or add.
238:             * @param propValue The new property value.
239:             */
240:            public Object addToEnvironment(String propName, Object propVal)
241:                    throws NamingException {
242:                return null;
243:            }
244:
245:            /**
246:             * Binds a name to an object.
247:             *
248:             * @param name The name of the object to bind.
249:             * @param obj The object to bind.
250:             * @exception NamingException
251:             */
252:            public void bind(Name name, Object obj) throws NamingException {
253:                if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) {
254:                    throw new NamingException(
255:                            "Not allowed to bind to the base context. "
256:                                    + "Must use relative binding");
257:                }
258:                Context context = getInitialContext(name);
259:                try {
260:                    Name composedName = composeName(name, base);
261:                    for (int index = 0; index < (composedName.size() - 1); index++) {
262:                        try {
263:                            context = (Context) context.lookup(composedName
264:                                    .get(index));
265:                        } catch (javax.naming.NameNotFoundException ex) {
266:                            context = context.createSubcontext(composedName
267:                                    .get(index));
268:                        }
269:                    }
270:                    log.info("Binding object [" + composedName.toString()
271:                            + "] [" + name.get(0) + "]["
272:                            + composedName.get(composedName.size() - 1)
273:                            + "] object [" + obj.getClass().getName() + "]");
274:                    context.bind(composedName.get(composedName.size() - 1),
275:                            wrapBindingValue(obj));
276:                } catch (NamingException ex) {
277:                    resetContext(context);
278:                    log.error("Failed to bind the object to the context :"
279:                            + ex.getMessage(), ex);
280:                    throw ex;
281:                } catch (Exception ex) {
282:                    resetContext(context);
283:                    log.error("Failed to bind the object to the context :"
284:                            + ex.getMessage(), ex);
285:                    throw new NamingException(
286:                            "Failed to bind the object to the context :"
287:                                    + ex.getMessage());
288:                }
289:            }
290:
291:            /**
292:             * Binds a name to an object.
293:             */
294:            public void bind(String name, Object obj) throws NamingException {
295:                bind(new NamingParser().parse(name), obj);
296:            }
297:
298:            /**
299:             * Closes this context.
300:             */
301:            public void close() throws NamingException {
302:                if (instanceContext != null) {
303:                    instanceContext.close();
304:                }
305:                if (masterContext != null) {
306:                    masterContext.close();
307:                }
308:            }
309:
310:            /**
311:             * Composes the name of this context with a name relative to this context.
312:             * 
313:             * @return The compisit name.
314:             * @param name The name to add to the prefix.
315:             * @param prefix The prefix of the current context.
316:             * @exception NamingException
317:             */
318:            public Name composeName(Name name, Name prefix)
319:                    throws NamingException {
320:                Name tmp = (Name) prefix.clone();
321:                tmp.addAll(name);
322:                return tmp;
323:            }
324:
325:            /**
326:             * Composes the name of this context with a name relative to this context.
327:             *
328:             * @return The string version of the composed name.
329:             * @param name The name to add to the preffix.
330:             * @param prefix The prefix for this context.
331:             */
332:            public String composeName(String name, String prefix)
333:                    throws NamingException {
334:                return composeName(new NamingParser().parse(name),
335:                        new NamingParser().parse(prefix)).toString();
336:            }
337:
338:            /**
339:             * Creates and binds a new context to this context.
340:             *
341:             * @return The newly created context.
342:             * @param name The name of the new sub context.
343:             * @exception NamingException
344:             */
345:            public Context createSubcontext(Name name) throws NamingException {
346:                if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) {
347:                    throw new NamingException("Must use relative bindings.");
348:                }
349:                Context context = getInitialContext(name);
350:                try {
351:                    Name composedName = composeName(name, base);
352:                    for (int index = 0; index < (composedName.size()); index++) {
353:                        try {
354:                            context = (Context) context.lookup(composedName
355:                                    .get(index));
356:                        } catch (javax.naming.NameNotFoundException ex) {
357:                            context = context.createSubcontext(composedName
358:                                    .get(index));
359:                        }
360:                    }
361:                    return context;
362:                } catch (NamingException ex) {
363:                    resetContext(context);
364:                    log.error("Failed to create the sub context :"
365:                            + ex.getMessage(), ex);
366:                    throw ex;
367:                } catch (Exception ex) {
368:                    resetContext(context);
369:                    log.error("Failed to create the sub context :"
370:                            + ex.getMessage(), ex);
371:                    throw new NamingException(
372:                            "Failed to create the sub context :"
373:                                    + ex.getMessage());
374:                }
375:            }
376:
377:            /**
378:             * Creates and binds a new context.
379:             *
380:             * @return The newly create sub context.
381:             * @exception name The name of the new sub context.
382:             * @exception NamingException
383:             */
384:            public Context createSubcontext(String name) throws NamingException {
385:                return createSubcontext(new NamingParser().parse(name));
386:            }
387:
388:            /**
389:             * Destroys the named context and removes it from the namespace.
390:             *
391:             * @param name The name of the sub context to remove.
392:             * @exception NamingException
393:             */
394:            public void destroySubcontext(Name name) throws NamingException {
395:                if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) {
396:                    throw new NamingException("Must use relative bindings.");
397:                }
398:                Context context = getInitialContext(name);
399:                try {
400:                    Name composedName = composeName(name, base);
401:                    context.destroySubcontext(composedName);
402:                } catch (NamingException ex) {
403:                    resetContext(context);
404:                    log.error("Failed to destroy the sub context :"
405:                            + ex.getMessage(), ex);
406:                    throw ex;
407:                } catch (Exception ex) {
408:                    resetContext(context);
409:                    log.error("Failed to destroy the sub context :"
410:                            + ex.getMessage(), ex);
411:                    throw new NamingException(
412:                            "Failed to destroy the sub context :"
413:                                    + ex.getMessage());
414:                }
415:            }
416:
417:            /**
418:             * Destroys the named context and removes it from the namespace.
419:             *
420:             * @param name The name of the context to destroy.
421:             */
422:            public void destroySubcontext(String name) throws NamingException {
423:                destroySubcontext(new NamingParser().parse(name));
424:            }
425:
426:            /**
427:             * Retrieves the environment in effect for this context.
428:             *
429:             * @return The reference to the hash table.
430:             * @exception NamingException
431:             */
432:            public Hashtable getEnvironment() throws NamingException {
433:                return null;
434:            }
435:
436:            /**
437:             * Retrieves the full name of this context within its own namespace.
438:             */
439:            public String getNameInNamespace() throws NamingException {
440:                return this .instanceURL.toString();
441:            }
442:
443:            /**
444:             * Retrieves the parser associated with the named context.
445:             *
446:             * @return The reference to the name parser.
447:             * @param name The name to return the parser for.
448:             * @exception NamingException
449:             */
450:            public NameParser getNameParser(Name name) throws NamingException {
451:                return new NamingParser();
452:            }
453:
454:            /**
455:             * Retrieves the parser associated with the named context.
456:             */
457:            public NameParser getNameParser(String name) throws NamingException {
458:                return new NamingParser();
459:            }
460:
461:            /**
462:             * Enumerates the names bound in the named context, along with the class 
463:             * names of objects bound to them.
464:             */
465:            public NamingEnumeration list(Name name) throws NamingException {
466:                throw new OperationNotSupportedException(
467:                        "This operation is currently not supported");
468:            }
469:
470:            /**
471:             * Enumerates the names bound in the named context, along with the class 
472:             * names of objects bound to them.
473:             *
474:             * @return The list of names bound to this context.
475:             * @param name The list of names.
476:             * @exception NamingException
477:             */
478:            public NamingEnumeration list(String name) throws NamingException {
479:                throw new OperationNotSupportedException(
480:                        "This operation is currently not supported");
481:            }
482:
483:            /**
484:             * Enumerates the names bound in the named context, along with the objects 
485:             * bound to them.
486:             *
487:             * @return The list of bindings for the name
488:             * @param name The name to perform the search below.
489:             * @exception NamingException
490:             */
491:            public NamingEnumeration listBindings(Name name)
492:                    throws NamingException {
493:                throw new OperationNotSupportedException(
494:                        "This operation is currently not supported");
495:            }
496:
497:            /**
498:             * Enumerates the names bound in the named context, along with the objects 
499:             * bound to them.
500:             *
501:             * @return The list of binding for the name.
502:             * @param name The name to perform the search for.
503:             * @exception NamingException
504:             */
505:            public NamingEnumeration listBindings(String name)
506:                    throws NamingException {
507:                throw new OperationNotSupportedException(
508:                        "This operation is currently not supported");
509:            }
510:
511:            /**
512:             * Retrieves the named object.
513:             *
514:             * @return The named object.
515:             * @param name The name to retrieve the object for.
516:             * @exception NamingException
517:             */
518:            public Object lookup(Name name) throws NamingException {
519:                Context context = getInitialContext(name);
520:                String currentName = null;
521:                try {
522:                    Name composedName = null;
523:                    if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) {
524:                        composedName = name;
525:                    } else {
526:                        composedName = composeName(name, base);
527:                    }
528:                    for (int index = 0; index < (composedName.size() - 1); index++) {
529:                        currentName = composedName.get(index);
530:                        context = (Context) context.lookup(currentName);
531:                    }
532:                    currentName = composedName.get(composedName.size() - 1);
533:                    return unwrapBindingValue(context.lookup(currentName));
534:                } catch (NamingException ex) {
535:                    resetContext(context);
536:                    log.error("Failed to lookup the object [" + currentName
537:                            + "] :" + ex.getMessage(), ex);
538:                    throw ex;
539:                } catch (Exception ex) {
540:                    resetContext(context);
541:                    log.error("Failed to lookup the object [" + currentName
542:                            + "] :" + ex.getMessage(), ex);
543:                    throw new NamingException("Failed to lookup the object ["
544:                            + currentName + "] :" + ex.getMessage());
545:                }
546:            }
547:
548:            /**
549:             * Retrieves the named object.
550:             *
551:             * @return The object to retrieve by name.
552:             * @param name The name of the object to retrieve.
553:             * @exception NamingException
554:             */
555:            public Object lookup(String name) throws NamingException {
556:                return lookup(new NamingParser().parse(name));
557:            }
558:
559:            /**
560:             * Retrieves the named object, following links except for the terminal 
561:             * atomic component of the name.
562:             *
563:             * @return The object to retrieve.
564:             * @param name The name of the object to lookup.
565:             * @exception NamingException
566:             */
567:            public Object lookupLink(Name name) throws NamingException {
568:                throw new OperationNotSupportedException(
569:                        "This method is not currently supported");
570:            }
571:
572:            /**
573:             * Retrieves the named object, following links except for the terminal 
574:             * atomic component of the name.
575:             *
576:             * @return The results of the lookup link.
577:             * @param name The name of the object to lookup.
578:             * @exception NamingException
579:             */
580:            public Object lookupLink(String name) throws NamingException {
581:                return lookupLink(new NamingParser().parse(name));
582:            }
583:
584:            /**
585:             * Binds a name to an object, overwriting any existing binding.
586:             *
587:             * @param name The name to rebind.
588:             * @param obj The object to rebind.
589:             * @exception NamingException
590:             */
591:            public void rebind(Name name, Object obj) throws NamingException {
592:                if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) {
593:                    throw new NamingException(
594:                            "Not allowed to bind to the base context. "
595:                                    + "Must use relative binding");
596:                }
597:                Context context = getInitialContext(name);
598:                try {
599:                    Name composedName = composeName(name, base);
600:                    for (int index = 0; index < (composedName.size() - 1); index++) {
601:                        try {
602:                            context = (Context) context.lookup(composedName
603:                                    .get(index));
604:                        } catch (javax.naming.NameNotFoundException ex) {
605:                            context = context.createSubcontext(composedName
606:                                    .get(index));
607:                        }
608:                    }
609:                    log.info("Rebinding object [" + composedName.toString()
610:                            + "] [" + name.get(0) + "]["
611:                            + composedName.get(composedName.size() - 1)
612:                            + "] object [" + obj.getClass().getName() + "]");
613:                    context.rebind(composedName.get(composedName.size() - 1),
614:                            wrapBindingValue(obj));
615:                } catch (NamingException ex) {
616:                    resetContext(context);
617:                    log.error("Failed to rebind the object to the context :"
618:                            + ex.getMessage(), ex);
619:                    throw ex;
620:                } catch (Exception ex) {
621:                    resetContext(context);
622:                    log.error("Failed to rebind the object to the context :"
623:                            + ex.getMessage(), ex);
624:                    throw new NamingException(
625:                            "Failed to rebind the object to the context :"
626:                                    + ex.getMessage());
627:                }
628:            }
629:
630:            /**
631:             * Binds a name to an object, overwriting any existing binding.
632:             *
633:             * @param name The name to rebind.
634:             * @param obj The object to rebind.
635:             * @exception NamingException
636:             */
637:            public void rebind(String name, Object obj) throws NamingException {
638:                rebind(new NamingParser().parse(name), obj);
639:            }
640:
641:            /**
642:             * Removes an environment property from the environment of this context.
643:             *
644:             * @param propName The name of the entry to remove from the environment.
645:             * @exception NamingException
646:             */
647:            public Object removeFromEnvironment(String propName)
648:                    throws NamingException {
649:                return null;
650:            }
651:
652:            /**
653:             * Binds a new name to the object bound to an old name, and unbinds the old 
654:             * name.
655:             *
656:             * @param oldName The old name to rename.
657:             * @param newName The name to replace it with.
658:             * @exception NamingException
659:             */
660:            public void rename(Name oldName, Name newName)
661:                    throws NamingException {
662:                if (oldName.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)
663:                        || newName.get(0).equals(
664:                                NamingConstants.JNDI_NETWORK_PREFIX)) {
665:                    throw new NamingException("Must use relative binding");
666:                }
667:                Context context = getInitialContext(oldName);
668:                try {
669:                    Object value = lookup(oldName);
670:                    bind(newName, value);
671:                    unbind(oldName);
672:                } catch (NamingException ex) {
673:                    resetContext(context);
674:                    log.error("Failed to rename the object in the context :"
675:                            + ex.getMessage(), ex);
676:                    throw ex;
677:                } catch (Exception ex) {
678:                    resetContext(context);
679:                    log.error("Failed to rename the object in the context :"
680:                            + ex.getMessage(), ex);
681:                    throw new NamingException(
682:                            "Failed to rename the object in the context :"
683:                                    + ex.getMessage());
684:                }
685:            }
686:
687:            /**
688:             * Binds a new name to the object bound to an old name, and unbinds the old 
689:             * name.
690:             *
691:             * @param oldName The old name to rename.
692:             * @param newName The name to replace it with.
693:             * @exception NamingException
694:             */
695:            public void rename(String oldName, String newName)
696:                    throws NamingException {
697:                rename(new NamingParser().parse(oldName), new NamingParser()
698:                        .parse(newName));
699:            }
700:
701:            /**
702:             * Unbinds the named object.
703:             *
704:             * @param name The name to unbind.
705:             * @exception NamingException
706:             */
707:            public void unbind(Name name) throws NamingException {
708:                if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) {
709:                    throw new NamingException(
710:                            "Not allowed to use the base context. "
711:                                    + "Must use relative binding");
712:                }
713:                Context context = getInitialContext(name);
714:                try {
715:                    Name composedName = composeName(name, base);
716:                    context.unbind(composedName);
717:                } catch (NamingException ex) {
718:                    resetContext(context);
719:                    log.error("Failed to unbind the object to the context :"
720:                            + ex.getMessage(), ex);
721:                    throw ex;
722:                } catch (Exception ex) {
723:                    resetContext(context);
724:                    log.error("Failed to unbind the object to the context :"
725:                            + ex.getMessage(), ex);
726:                    throw new NamingException(
727:                            "Failed to unbind the object to the context :"
728:                                    + ex.getMessage());
729:                }
730:            }
731:
732:            /**
733:             * Unbinds the named object.
734:             *
735:             * @param name The name to unbind.
736:             * @exception NamingException
737:             */
738:            public void unbind(String name) throws NamingException {
739:                unbind(new NamingParser().parse(name));
740:            }
741:
742:            /**
743:             * This method is responsible for terminating the processing of this cos
744:             * context.
745:             */
746:            public void terminate() {
747:                threadGroup.terminate();
748:                try {
749:                    close();
750:                } catch (Exception ex) {
751:                    log.error("Failed to close the contexts : "
752:                            + ex.getMessage(), ex);
753:                }
754:                if (instanceBinder != null) {
755:                    instanceBinder.terminate();
756:                }
757:            }
758:
759:            /**
760:             * This method returns the initial context that matches the given name.
761:             *
762:             * @return The context that matches the name.
763:             * @param name The name to retrieve the context for.
764:             * @exception NamingException
765:             */
766:            private Context getInitialContext(Name name) throws NamingException {
767:                try {
768:                    if (primary
769:                            || !(name.get(0).equals(this .instanceURL.get(0)))) {
770:                        synchronized (this ) {
771:                            if (instanceContext != null) {
772:                                return instanceContext;
773:                            }
774:                            Hashtable env = new Hashtable();
775:                            env.put(Context.INITIAL_CONTEXT_FACTORY,
776:                                    SUN_COS_CONTEXT_FACTORY);
777:                            env.put(Context.PROVIDER_URL, instanceCosURL);
778:                            env.put("java.naming.corba.orb", orbManager
779:                                    .getORB());
780:                            return instanceContext = new InitialContext(env);
781:                        }
782:                    }
783:                    synchronized (this ) {
784:                        if (masterContext != null) {
785:                            return masterContext;
786:                        }
787:                        Hashtable env = new Hashtable();
788:                        env.put(Context.INITIAL_CONTEXT_FACTORY,
789:                                SUN_COS_CONTEXT_FACTORY);
790:                        env.put(Context.PROVIDER_URL, masterCosURL);
791:                        env.put("java.naming.corba.orb", orbManager.getORB());
792:                        log.info("Retrieve the master context url");
793:                        return masterContext = new InitialContext(env);
794:                    }
795:                } catch (Exception ex) {
796:                    log.error(
797:                            "Failed to retrieve an initial context for the name ["
798:                                    + name.toString() + "] because : "
799:                                    + ex.getMessage(), ex);
800:                    throw new NamingException(
801:                            "Failed to retrieve an initial context "
802:                                    + "for the name [" + name.toString()
803:                                    + "] because : " + ex.getMessage());
804:                }
805:            }
806:
807:            /**
808:             * This method is used to reset contexts that are not working correctly.
809:             *
810:             * @param context The reference to the context that needs resetting
811:             */
812:            private void resetContext(Context context) {
813:                if (context == instanceContext) {
814:                    instanceContext = null;
815:                }
816:                if (context == masterContext) {
817:                    masterContext = null;
818:                }
819:            }
820:
821:            /**
822:             * This method is responsible for wrapping the binding value. It uses the
823:             * dynamic any value stub code to perform this task.
824:             *
825:             * @return The wrapped object or Remote.
826:             * @param value The object to wrap if remote it will just be returned.
827:             * @exception NamingException
828:             */
829:            public Object wrapBindingValue(Object value) throws NamingException {
830:                if (value instanceof  Remote) {
831:                    return value;
832:                } else if (!(value instanceof  Serializable)) {
833:                    throw new NamingException(
834:                            "binding value is not instance of "
835:                                    + "Remote or Serializable cannot be stored in Cos Naming");
836:                }
837:                JNDIResourceWrapper resourceWrapper = null;
838:                if (value instanceof  Referenceable) {
839:                    resourceWrapper = new JNDIResourceWrapper(
840:                            (Serializable) ((Referenceable) value)
841:                                    .getReference());
842:                } else {
843:                    resourceWrapper = new JNDIResourceWrapper(
844:                            (Serializable) value);
845:                }
846:                try {
847:                    PortableRemoteObject.exportObject(resourceWrapper);
848:                } catch (Exception ex) {
849:                    log.error("Failed to export wrapper :" + ex.getMessage(),
850:                            ex);
851:                    throw new NamingException("Failed to export wrapper :"
852:                            + ex.getMessage());
853:                }
854:                return resourceWrapper;
855:            }
856:
857:            /**
858:             * This method unwrapps the retrieved value. If it is an instance of remote
859:             * than the value just gets returned.
860:             *
861:             * @return The unwrapped object.
862:             * @param value The value to unwrapp.
863:             * @param name The name of the bound object used for Referenceble objects.
864:             * @exception NamingException
865:             */
866:            public Object unwrapBindingValue(Object value)
867:                    throws NamingException {
868:                try {
869:                    ObjectImpl objectImpl = (ObjectImpl) PortableRemoteObject
870:                            .narrow(value, ObjectImpl.class);
871:                    String[] ids = objectImpl._ids();
872:                    String itf = ids[0];
873:
874:                    if (itf.indexOf(":org.objectweb.carol.jndi.wrapping."
875:                            + "JNDIRemoteResource:") != -1) {
876:                        JNDIRemoteResource jndiRemoteResource = (JNDIRemoteResource) PortableRemoteObject
877:                                .narrow(value, JNDIRemoteResource.class);
878:                        return jndiRemoteResource.getResource();
879:                    }
880:
881:                    return value;
882:                } catch (Exception ex) {
883:                    log.error("Failed to wrapp the object :" + ex.getMessage(),
884:                            ex);
885:                    throw new NamingException("Failed to wrapp the object :"
886:                            + ex.getMessage());
887:                }
888:            }
889:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.