KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > core > NamingContextListener


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.core;
20
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 import javax.management.MalformedObjectNameException JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.naming.NameAlreadyBoundException JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.naming.Reference JavaDoc;
34 import javax.naming.StringRefAddr JavaDoc;
35
36 import org.apache.catalina.Container;
37 import org.apache.catalina.ContainerEvent;
38 import org.apache.catalina.ContainerListener;
39 import org.apache.catalina.Context;
40 import org.apache.catalina.Engine;
41 import org.apache.catalina.Host;
42 import org.apache.catalina.Lifecycle;
43 import org.apache.catalina.LifecycleEvent;
44 import org.apache.catalina.LifecycleListener;
45 import org.apache.catalina.Server;
46 import org.apache.catalina.Service;
47 import org.apache.catalina.deploy.ContextEjb;
48 import org.apache.catalina.deploy.ContextEnvironment;
49 import org.apache.catalina.deploy.ContextLocalEjb;
50 import org.apache.catalina.deploy.ContextResource;
51 import org.apache.catalina.deploy.ContextResourceEnvRef;
52 import org.apache.catalina.deploy.ContextResourceLink;
53 import org.apache.catalina.deploy.ContextTransaction;
54 import org.apache.catalina.deploy.NamingResources;
55 import org.apache.catalina.util.StringManager;
56 import org.apache.commons.logging.Log;
57 import org.apache.commons.logging.LogFactory;
58 import org.apache.naming.ContextAccessController;
59 import org.apache.naming.ContextBindings;
60 import org.apache.naming.EjbRef;
61 import org.apache.naming.NamingContext;
62 import org.apache.naming.ResourceEnvRef;
63 import org.apache.naming.ResourceLinkRef;
64 import org.apache.naming.ResourceRef;
65 import org.apache.naming.TransactionRef;
66 import org.apache.tomcat.util.modeler.Registry;
67
68
69 /**
70  * Helper class used to initialize and populate the JNDI context associated
71  * with each context and server.
72  *
73  * @author Remy Maucherat
74  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
75  */

76
77 public class NamingContextListener
78     implements LifecycleListener, ContainerListener, PropertyChangeListener JavaDoc {
79
80     private static Log log = LogFactory.getLog(NamingContextListener.class);
81
82
83     // ----------------------------------------------------- Instance Variables
84

85
86     protected Log logger = log;
87     
88     
89     /**
90      * Name of the associated naming context.
91      */

92     protected String JavaDoc name = "/";
93
94
95     /**
96      * Associated container.
97      */

98     protected Object JavaDoc container = null;
99
100
101     /**
102      * Initialized flag.
103      */

104     protected boolean initialized = false;
105
106
107     /**
108      * Associated naming resources.
109      */

110     protected NamingResources namingResources = null;
111
112
113     /**
114      * Associated JNDI context.
115      */

116     protected NamingContext namingContext = null;
117
118
119     /**
120      * Comp context.
121      */

122     protected javax.naming.Context JavaDoc compCtx = null;
123
124
125     /**
126      * Env context.
127      */

128     protected javax.naming.Context JavaDoc envCtx = null;
129
130     
131     /**
132      * Objectnames hashtable.
133      */

134     protected HashMap JavaDoc objectNames = new HashMap JavaDoc();
135     
136
137     /**
138      * The string manager for this package.
139      */

140     protected static StringManager sm =
141         StringManager.getManager(Constants.Package);
142
143
144     // ------------------------------------------------------------- Properties
145

146
147     /**
148      * Return the "name" property.
149      */

150     public String JavaDoc getName() {
151         return (this.name);
152     }
153
154
155     /**
156      * Set the "name" property.
157      *
158      * @param name The new name
159      */

160     public void setName(String JavaDoc name) {
161         this.name = name;
162     }
163
164     
165     /**
166      * Return the comp context.
167      */

168     public javax.naming.Context JavaDoc getCompContext() {
169         return this.compCtx;
170     }
171     
172
173     /**
174      * Return the env context.
175      */

176     public javax.naming.Context JavaDoc getEnvContext() {
177         return this.envCtx;
178     }
179     
180
181     /**
182      * Return the associated naming context.
183      */

184     public NamingContext getNamingContext() {
185         return (this.namingContext);
186     }
187
188
189     // ---------------------------------------------- LifecycleListener Methods
190

191
192     /**
193      * Acknowledge the occurrence of the specified event.
194      *
195      * @param event LifecycleEvent that has occurred
196      */

197     public void lifecycleEvent(LifecycleEvent event) {
198
199         container = event.getLifecycle();
200
201         if (container instanceof Context) {
202             namingResources = ((Context) container).getNamingResources();
203             logger = log;
204         } else if (container instanceof Server) {
205             namingResources = ((Server) container).getGlobalNamingResources();
206         } else {
207             return;
208         }
209
210         if (event.getType() == Lifecycle.START_EVENT) {
211
212             if (initialized)
213                 return;
214
215             Hashtable JavaDoc contextEnv = new Hashtable JavaDoc();
216             try {
217                 namingContext = new NamingContext(contextEnv, getName());
218             } catch (NamingException JavaDoc e) {
219                 // Never happens
220
}
221             ContextAccessController.setSecurityToken(getName(), container);
222             ContextBindings.bindContext(container, namingContext, container);
223             if( log.isDebugEnabled() ) {
224                 log.debug("Bound " + container );
225             }
226
227             // Setting the context in read/write mode
228
ContextAccessController.setWritable(getName(), container);
229
230             try {
231                 createNamingContext();
232             } catch (NamingException JavaDoc e) {
233                 logger.error
234                     (sm.getString("naming.namingContextCreationFailed", e));
235             }
236
237             // Binding the naming context to the class loader
238
if (container instanceof Context) {
239                 // Setting the context in read only mode
240
ContextAccessController.setReadOnly(getName());
241                 try {
242                     ContextBindings.bindClassLoader
243                         (container, container,
244                          ((Container) container).getLoader().getClassLoader());
245                 } catch (NamingException JavaDoc e) {
246                     logger.error(sm.getString("naming.bindFailed", e));
247                 }
248             }
249
250             if (container instanceof Server) {
251                 namingResources.addPropertyChangeListener(this);
252                 org.apache.naming.factory.ResourceLinkFactory.setGlobalContext
253                     (namingContext);
254                 try {
255                     ContextBindings.bindClassLoader
256                         (container, container,
257                          this.getClass().getClassLoader());
258                 } catch (NamingException JavaDoc e) {
259                     logger.error(sm.getString("naming.bindFailed", e));
260                 }
261                 if (container instanceof StandardServer) {
262                     ((StandardServer) container).setGlobalNamingContext
263                         (namingContext);
264                 }
265             }
266
267             initialized = true;
268
269         } else if (event.getType() == Lifecycle.STOP_EVENT) {
270
271             if (!initialized)
272                 return;
273
274             // Setting the context in read/write mode
275
ContextAccessController.setWritable(getName(), container);
276             ContextBindings.unbindContext(container, container);
277
278             if (container instanceof Context) {
279                 ContextBindings.unbindClassLoader
280                     (container, container,
281                      ((Container) container).getLoader().getClassLoader());
282             }
283
284             if (container instanceof Server) {
285                 namingResources.removePropertyChangeListener(this);
286                 ContextBindings.unbindClassLoader
287                     (container, container,
288                      this.getClass().getClassLoader());
289             }
290
291             ContextAccessController.unsetSecurityToken(getName(), container);
292
293             namingContext = null;
294             envCtx = null;
295             compCtx = null;
296             initialized = false;
297
298         }
299
300     }
301
302
303     // ---------------------------------------------- ContainerListener Methods
304

305
306     /**
307      * Acknowledge the occurrence of the specified event.
308      * Note: Will never be called when the listener is associated to a Server,
309      * since it is not a Container.
310      *
311      * @param event ContainerEvent that has occurred
312      */

313     public void containerEvent(ContainerEvent event) {
314
315         if (!initialized)
316             return;
317
318         // Setting the context in read/write mode
319
ContextAccessController.setWritable(getName(), container);
320
321         String JavaDoc type = event.getType();
322
323         if (type.equals("addEjb")) {
324
325             String JavaDoc ejbName = (String JavaDoc) event.getData();
326             if (ejbName != null) {
327                 ContextEjb ejb = namingResources.findEjb(ejbName);
328                 addEjb(ejb);
329             }
330
331         } else if (type.equals("addEnvironment")) {
332
333             String JavaDoc environmentName = (String JavaDoc) event.getData();
334             if (environmentName != null) {
335                 ContextEnvironment env =
336                     namingResources.findEnvironment(environmentName);
337                 addEnvironment(env);
338             }
339
340         } else if (type.equals("addLocalEjb")) {
341
342             String JavaDoc localEjbName = (String JavaDoc) event.getData();
343             if (localEjbName != null) {
344                 ContextLocalEjb localEjb =
345                     namingResources.findLocalEjb(localEjbName);
346                 addLocalEjb(localEjb);
347             }
348
349         } else if (type.equals("addResource")) {
350
351             String JavaDoc resourceName = (String JavaDoc) event.getData();
352             if (resourceName != null) {
353                 ContextResource resource =
354                     namingResources.findResource(resourceName);
355                 addResource(resource);
356             }
357
358         } else if (type.equals("addResourceLink")) {
359
360             String JavaDoc resourceLinkName = (String JavaDoc) event.getData();
361             if (resourceLinkName != null) {
362                 ContextResourceLink resourceLink =
363                     namingResources.findResourceLink(resourceLinkName);
364                 addResourceLink(resourceLink);
365             }
366
367         } else if (type.equals("addResourceEnvRef")) {
368
369             String JavaDoc resourceEnvRefName = (String JavaDoc) event.getData();
370             if (resourceEnvRefName != null) {
371                 ContextResourceEnvRef resourceEnvRef =
372                     namingResources.findResourceEnvRef(resourceEnvRefName);
373                 addResourceEnvRef(resourceEnvRef);
374             }
375
376         } else if (type.equals("removeEjb")) {
377
378             String JavaDoc ejbName = (String JavaDoc) event.getData();
379             if (ejbName != null) {
380                 removeEjb(ejbName);
381             }
382
383         } else if (type.equals("removeEnvironment")) {
384
385             String JavaDoc environmentName = (String JavaDoc) event.getData();
386             if (environmentName != null) {
387                 removeEnvironment(environmentName);
388             }
389
390         } else if (type.equals("removeLocalEjb")) {
391
392             String JavaDoc localEjbName = (String JavaDoc) event.getData();
393             if (localEjbName != null) {
394                 removeLocalEjb(localEjbName);
395             }
396
397         } else if (type.equals("removeResource")) {
398
399             String JavaDoc resourceName = (String JavaDoc) event.getData();
400             if (resourceName != null) {
401                 removeResource(resourceName);
402             }
403
404         } else if (type.equals("removeResourceLink")) {
405
406             String JavaDoc resourceLinkName = (String JavaDoc) event.getData();
407             if (resourceLinkName != null) {
408                 removeResourceLink(resourceLinkName);
409             }
410
411         } else if (type.equals("removeResourceEnvRef")) {
412
413             String JavaDoc resourceEnvRefName = (String JavaDoc) event.getData();
414             if (resourceEnvRefName != null) {
415                 removeResourceEnvRef(resourceEnvRefName);
416             }
417
418         }
419
420         // Setting the context in read only mode
421
ContextAccessController.setReadOnly(getName());
422
423     }
424
425
426     // ----------------------------------------- PropertyChangeListener Methods
427

428
429     /**
430      * Process property change events. Currently, only listens to such events
431      * on the <code>NamingResources</code> instance for the global naming
432      * resources.
433      *
434      * @param event The property change event that has occurred
435      */

436     public void propertyChange(PropertyChangeEvent JavaDoc event) {
437
438         if (!initialized)
439             return;
440
441         Object JavaDoc source = event.getSource();
442         if (source == namingResources) {
443
444             // Setting the context in read/write mode
445
ContextAccessController.setWritable(getName(), container);
446
447             processGlobalResourcesChange(event.getPropertyName(),
448                                          event.getOldValue(),
449                                          event.getNewValue());
450
451             // Setting the context in read only mode
452
ContextAccessController.setReadOnly(getName());
453
454         }
455
456     }
457
458
459     // -------------------------------------------------------- Private Methods
460

461
462     /**
463      * Process a property change on the global naming resources, by making the
464      * corresponding addition or removal to the associated JNDI context.
465      *
466      * @param name Property name of the change to be processed
467      * @param oldValue The old value (or <code>null</code> if adding)
468      * @param newValue The new value (or <code>null</code> if removing)
469      */

470     private void processGlobalResourcesChange(String JavaDoc name,
471                                               Object JavaDoc oldValue,
472                                               Object JavaDoc newValue) {
473
474         // NOTE - It seems that the Context for global JNDI resources
475
// is left in read-write mode, so we do not have to change it here
476

477         if (name.equals("ejb")) {
478             if (oldValue != null) {
479                 ContextEjb ejb = (ContextEjb) oldValue;
480                 if (ejb.getName() != null) {
481                     removeEjb(ejb.getName());
482                 }
483             }
484             if (newValue != null) {
485                 ContextEjb ejb = (ContextEjb) newValue;
486                 if (ejb.getName() != null) {
487                     addEjb(ejb);
488                 }
489             }
490         } else if (name.equals("environment")) {
491             if (oldValue != null) {
492                 ContextEnvironment env = (ContextEnvironment) oldValue;
493                 if (env.getName() != null) {
494                     removeEnvironment(env.getName());
495                 }
496             }
497             if (newValue != null) {
498                 ContextEnvironment env = (ContextEnvironment) newValue;
499                 if (env.getName() != null) {
500                     addEnvironment(env);
501                 }
502             }
503         } else if (name.equals("localEjb")) {
504             if (oldValue != null) {
505                 ContextLocalEjb ejb = (ContextLocalEjb) oldValue;
506                 if (ejb.getName() != null) {
507                     removeLocalEjb(ejb.getName());
508                 }
509             }
510             if (newValue != null) {
511                 ContextLocalEjb ejb = (ContextLocalEjb) newValue;
512                 if (ejb.getName() != null) {
513                     addLocalEjb(ejb);
514                 }
515             }
516         } else if (name.equals("resource")) {
517             if (oldValue != null) {
518                 ContextResource resource = (ContextResource) oldValue;
519                 if (resource.getName() != null) {
520                     removeResource(resource.getName());
521                 }
522             }
523             if (newValue != null) {
524                 ContextResource resource = (ContextResource) newValue;
525                 if (resource.getName() != null) {
526                     addResource(resource);
527                 }
528             }
529         } else if (name.equals("resourceEnvRef")) {
530             if (oldValue != null) {
531                 ContextResourceEnvRef resourceEnvRef =
532                     (ContextResourceEnvRef) oldValue;
533                 if (resourceEnvRef.getName() != null) {
534                     removeResourceEnvRef(resourceEnvRef.getName());
535                 }
536             }
537             if (newValue != null) {
538                 ContextResourceEnvRef resourceEnvRef =
539                     (ContextResourceEnvRef) newValue;
540                 if (resourceEnvRef.getName() != null) {
541                     addResourceEnvRef(resourceEnvRef);
542                 }
543             }
544         } else if (name.equals("resourceLink")) {
545             if (oldValue != null) {
546                 ContextResourceLink rl = (ContextResourceLink) oldValue;
547                 if (rl.getName() != null) {
548                     removeResourceLink(rl.getName());
549                 }
550             }
551             if (newValue != null) {
552                 ContextResourceLink rl = (ContextResourceLink) newValue;
553                 if (rl.getName() != null) {
554                     addResourceLink(rl);
555                 }
556             }
557         }
558
559
560     }
561
562
563     /**
564      * Create and initialize the JNDI naming context.
565      */

566     private void createNamingContext()
567         throws NamingException JavaDoc {
568
569         // Creating the comp subcontext
570
if (container instanceof Server) {
571             compCtx = namingContext;
572             envCtx = namingContext;
573         } else {
574             compCtx = namingContext.createSubcontext("comp");
575             envCtx = compCtx.createSubcontext("env");
576         }
577
578         int i;
579
580         if (log.isDebugEnabled())
581             log.debug("Creating JNDI naming context");
582
583         if (namingResources == null) {
584             namingResources = new NamingResources();
585             namingResources.setContainer(container);
586         }
587
588         // Resource links
589
ContextResourceLink[] resourceLinks =
590             namingResources.findResourceLinks();
591         for (i = 0; i < resourceLinks.length; i++) {
592             addResourceLink(resourceLinks[i]);
593         }
594
595         // Resources
596
ContextResource[] resources = namingResources.findResources();
597         for (i = 0; i < resources.length; i++) {
598             addResource(resources[i]);
599         }
600
601         // Resources Env
602
ContextResourceEnvRef[] resourceEnvRefs = namingResources.findResourceEnvRefs();
603         for (i = 0; i < resourceEnvRefs.length; i++) {
604             addResourceEnvRef(resourceEnvRefs[i]);
605         }
606
607         // Environment entries
608
ContextEnvironment[] contextEnvironments =
609             namingResources.findEnvironments();
610         for (i = 0; i < contextEnvironments.length; i++) {
611             addEnvironment(contextEnvironments[i]);
612         }
613
614         // EJB references
615
ContextEjb[] ejbs = namingResources.findEjbs();
616         for (i = 0; i < ejbs.length; i++) {
617             addEjb(ejbs[i]);
618         }
619
620         // Binding a User Transaction reference
621
if (container instanceof Context) {
622             try {
623                 Reference JavaDoc ref = new TransactionRef();
624                 compCtx.bind("UserTransaction", ref);
625                 ContextTransaction transaction = namingResources.getTransaction();
626                 if (transaction != null) {
627                     Iterator JavaDoc params = transaction.listProperties();
628                     while (params.hasNext()) {
629                         String JavaDoc paramName = (String JavaDoc) params.next();
630                         String JavaDoc paramValue = (String JavaDoc) transaction.getProperty(paramName);
631                         StringRefAddr JavaDoc refAddr = new StringRefAddr JavaDoc(paramName, paramValue);
632                         ref.add(refAddr);
633                     }
634                 }
635             } catch (NameAlreadyBoundException JavaDoc e) {
636                 // Ignore because UserTransaction was obviously
637
// added via ResourceLink
638
} catch (NamingException JavaDoc e) {
639                 logger.error(sm.getString("naming.bindFailed", e));
640             }
641         }
642
643         // Binding the resources directory context
644
if (container instanceof Context) {
645             try {
646                 compCtx.bind("Resources",
647                              ((Container) container).getResources());
648             } catch (NamingException JavaDoc e) {
649                 logger.error(sm.getString("naming.bindFailed", e));
650             }
651         }
652
653     }
654
655
656     /**
657      * Create an <code>ObjectName</code> for this
658      * <code>ContextResource</code> object.
659      *
660      * @param resource The resource
661      * @return ObjectName The object name
662      * @exception MalformedObjectNameException if a name cannot be created
663      */

664     protected ObjectName JavaDoc createObjectName(ContextResource resource)
665         throws MalformedObjectNameException JavaDoc {
666
667         String JavaDoc domain = null;
668         if (container instanceof StandardServer) {
669             domain = ((StandardServer) container).getDomain();
670         } else if (container instanceof ContainerBase) {
671             domain = ((ContainerBase) container).getDomain();
672         }
673         if (domain == null) {
674             domain = "Catalina";
675         }
676         
677         ObjectName JavaDoc name = null;
678         String JavaDoc quotedResourceName = ObjectName.quote(resource.getName());
679         if (container instanceof Server) {
680             name = new ObjectName JavaDoc(domain + ":type=DataSource" +
681                         ",class=" + resource.getType() +
682                         ",name=" + quotedResourceName);
683         } else if (container instanceof Context) {
684             String JavaDoc path = ((Context)container).getPath();
685             if (path.length() < 1)
686                 path = "/";
687             Host host = (Host) ((Context)container).getParent();
688             Engine engine = (Engine) host.getParent();
689             Service service = engine.getService();
690             name = new ObjectName JavaDoc(domain + ":type=DataSource" +
691                         ",path=" + path +
692                         ",host=" + host.getName() +
693                         ",class=" + resource.getType() +
694                         ",name=" + quotedResourceName);
695         }
696         
697         return (name);
698
699     }
700
701     
702     /**
703      * Set the specified EJBs in the naming context.
704      */

705     public void addEjb(ContextEjb ejb) {
706
707         // Create a reference to the EJB.
708
Reference JavaDoc ref = new EjbRef
709             (ejb.getType(), ejb.getHome(), ejb.getRemote(), ejb.getLink());
710         // Adding the additional parameters, if any
711
Iterator JavaDoc params = ejb.listProperties();
712         while (params.hasNext()) {
713             String JavaDoc paramName = (String JavaDoc) params.next();
714             String JavaDoc paramValue = (String JavaDoc) ejb.getProperty(paramName);
715             StringRefAddr JavaDoc refAddr = new StringRefAddr JavaDoc(paramName, paramValue);
716             ref.add(refAddr);
717         }
718         try {
719             createSubcontexts(envCtx, ejb.getName());
720             envCtx.bind(ejb.getName(), ref);
721         } catch (NamingException JavaDoc e) {
722             logger.error(sm.getString("naming.bindFailed", e));
723         }
724
725     }
726
727
728     /**
729      * Set the specified environment entries in the naming context.
730      */

731     public void addEnvironment(ContextEnvironment env) {
732
733         Object JavaDoc value = null;
734         // Instantiating a new instance of the correct object type, and
735
// initializing it.
736
String JavaDoc type = env.getType();
737         try {
738             if (type.equals("java.lang.String")) {
739                 value = env.getValue();
740             } else if (type.equals("java.lang.Byte")) {
741                 if (env.getValue() == null) {
742                     value = new Byte JavaDoc((byte) 0);
743                 } else {
744                     value = Byte.decode(env.getValue());
745                 }
746             } else if (type.equals("java.lang.Short")) {
747                 if (env.getValue() == null) {
748                     value = new Short JavaDoc((short) 0);
749                 } else {
750                     value = Short.decode(env.getValue());
751                 }
752             } else if (type.equals("java.lang.Integer")) {
753                 if (env.getValue() == null) {
754                     value = new Integer JavaDoc(0);
755                 } else {
756                     value = Integer.decode(env.getValue());
757                 }
758             } else if (type.equals("java.lang.Long")) {
759                 if (env.getValue() == null) {
760                     value = new Long JavaDoc(0);
761                 } else {
762                     value = Long.decode(env.getValue());
763                 }
764             } else if (type.equals("java.lang.Boolean")) {
765                 value = Boolean.valueOf(env.getValue());
766             } else if (type.equals("java.lang.Double")) {
767                 if (env.getValue() == null) {
768                     value = new Double JavaDoc(0);
769                 } else {
770                     value = Double.valueOf(env.getValue());
771                 }
772             } else if (type.equals("java.lang.Float")) {
773                 if (env.getValue() == null) {
774                     value = new Float JavaDoc(0);
775                 } else {
776                     value = Float.valueOf(env.getValue());
777                 }
778             } else if (type.equals("java.lang.Character")) {
779                 if (env.getValue() == null) {
780                     value = new Character JavaDoc((char) 0);
781                 } else {
782                     if (env.getValue().length() == 1) {
783                         value = new Character JavaDoc(env.getValue().charAt(0));
784                     } else {
785                         throw new IllegalArgumentException JavaDoc();
786                     }
787                 }
788             } else {
789                 logger.error(sm.getString("naming.invalidEnvEntryType", env.getName()));
790             }
791         } catch (NumberFormatException JavaDoc e) {
792             logger.error(sm.getString("naming.invalidEnvEntryValue", env.getName()));
793         } catch (IllegalArgumentException JavaDoc e) {
794             logger.error(sm.getString("naming.invalidEnvEntryValue", env.getName()));
795         }
796
797         // Binding the object to the appropriate name
798
if (value != null) {
799             try {
800                 if (logger.isDebugEnabled())
801                     logger.debug(" Adding environment entry " + env.getName());
802                 createSubcontexts(envCtx, env.getName());
803                 envCtx.bind(env.getName(), value);
804             } catch (NamingException JavaDoc e) {
805                 logger.error(sm.getString("naming.invalidEnvEntryValue", e));
806             }
807         }
808
809     }
810
811
812     /**
813      * Set the specified local EJBs in the naming context.
814      */

815     public void addLocalEjb(ContextLocalEjb localEjb) {
816
817
818
819     }
820
821
822     /**
823      * Set the specified resources in the naming context.
824      */

825     public void addResource(ContextResource resource) {
826
827         // Create a reference to the resource.
828
Reference JavaDoc ref = new ResourceRef
829             (resource.getType(), resource.getDescription(),
830              resource.getScope(), resource.getAuth());
831         // Adding the additional parameters, if any
832
Iterator JavaDoc params = resource.listProperties();
833         while (params.hasNext()) {
834             String JavaDoc paramName = (String JavaDoc) params.next();
835             String JavaDoc paramValue = (String JavaDoc) resource.getProperty(paramName);
836             StringRefAddr JavaDoc refAddr = new StringRefAddr JavaDoc(paramName, paramValue);
837             ref.add(refAddr);
838         }
839         try {
840             if (logger.isDebugEnabled()) {
841                 logger.debug(" Adding resource ref "
842                              + resource.getName() + " " + ref);
843             }
844             createSubcontexts(envCtx, resource.getName());
845             envCtx.bind(resource.getName(), ref);
846         } catch (NamingException JavaDoc e) {
847             logger.error(sm.getString("naming.bindFailed", e));
848         }
849
850         if ("javax.sql.DataSource".equals(ref.getClassName())) {
851             try {
852                 ObjectName JavaDoc on = createObjectName(resource);
853                 Object JavaDoc actualResource = envCtx.lookup(resource.getName());
854                 Registry.getRegistry(null, null).registerComponent(actualResource, on, null);
855                 objectNames.put(resource.getName(), on);
856             } catch (Exception JavaDoc e) {
857                 logger.warn(sm.getString("naming.jmxRegistrationFailed", e));
858             }
859         }
860         
861     }
862
863
864     /**
865      * Set the specified resources in the naming context.
866      */

867     public void addResourceEnvRef(ContextResourceEnvRef resourceEnvRef) {
868
869         // Create a reference to the resource env.
870
Reference JavaDoc ref = new ResourceEnvRef(resourceEnvRef.getType());
871         // Adding the additional parameters, if any
872
Iterator JavaDoc params = resourceEnvRef.listProperties();
873         while (params.hasNext()) {
874             String JavaDoc paramName = (String JavaDoc) params.next();
875             String JavaDoc paramValue = (String JavaDoc) resourceEnvRef.getProperty(paramName);
876             StringRefAddr JavaDoc refAddr = new StringRefAddr JavaDoc(paramName, paramValue);
877             ref.add(refAddr);
878         }
879         try {
880             if (logger.isDebugEnabled())
881                 log.debug(" Adding resource env ref " + resourceEnvRef.getName());
882             createSubcontexts(envCtx, resourceEnvRef.getName());
883             envCtx.bind(resourceEnvRef.getName(), ref);
884         } catch (NamingException JavaDoc e) {
885             logger.error(sm.getString("naming.bindFailed", e));
886         }
887
888     }
889
890
891     /**
892      * Set the specified resource link in the naming context.
893      */

894     public void addResourceLink(ContextResourceLink resourceLink) {
895
896         // Create a reference to the resource.
897
Reference JavaDoc ref = new ResourceLinkRef
898             (resourceLink.getType(), resourceLink.getGlobal());
899         javax.naming.Context JavaDoc ctx =
900             "UserTransaction".equals(resourceLink.getName())
901             ? compCtx : envCtx;
902         try {
903             if (logger.isDebugEnabled())
904                 log.debug(" Adding resource link " + resourceLink.getName());
905             createSubcontexts(envCtx, resourceLink.getName());
906             ctx.bind(resourceLink.getName(), ref);
907         } catch (NamingException JavaDoc e) {
908             logger.error(sm.getString("naming.bindFailed", e));
909         }
910
911     }
912
913
914     /**
915      * Set the specified EJBs in the naming context.
916      */

917     public void removeEjb(String JavaDoc name) {
918
919         try {
920             envCtx.unbind(name);
921         } catch (NamingException JavaDoc e) {
922             logger.error(sm.getString("naming.unbindFailed", e));
923         }
924
925     }
926
927
928     /**
929      * Set the specified environment entries in the naming context.
930      */

931     public void removeEnvironment(String JavaDoc name) {
932
933         try {
934             envCtx.unbind(name);
935         } catch (NamingException JavaDoc e) {
936             logger.error(sm.getString("naming.unbindFailed", e));
937         }
938
939     }
940
941
942     /**
943      * Set the specified local EJBs in the naming context.
944      */

945     public void removeLocalEjb(String JavaDoc name) {
946
947         try {
948             envCtx.unbind(name);
949         } catch (NamingException JavaDoc e) {
950             logger.error(sm.getString("naming.unbindFailed", e));
951         }
952
953     }
954
955
956     /**
957      * Set the specified resources in the naming context.
958      */

959     public void removeResource(String JavaDoc name) {
960
961         try {
962             envCtx.unbind(name);
963         } catch (NamingException JavaDoc e) {
964             logger.error(sm.getString("naming.unbindFailed", e));
965         }
966
967         ObjectName JavaDoc on = (ObjectName JavaDoc) objectNames.get(name);
968         if (on != null) {
969             Registry.getRegistry(null, null).unregisterComponent(on);
970         }
971
972     }
973
974
975     /**
976      * Set the specified resources in the naming context.
977      */

978     public void removeResourceEnvRef(String JavaDoc name) {
979
980         try {
981             envCtx.unbind(name);
982         } catch (NamingException JavaDoc e) {
983             logger.error(sm.getString("naming.unbindFailed", e));
984         }
985
986     }
987
988
989     /**
990      * Set the specified resources in the naming context.
991      */

992     public void removeResourceLink(String JavaDoc name) {
993
994         try {
995             envCtx.unbind(name);
996         } catch (NamingException JavaDoc e) {
997             logger.error(sm.getString("naming.unbindFailed", e));
998         }
999
1000    }
1001
1002
1003    /**
1004     * Create all intermediate subcontexts.
1005     */

1006    private void createSubcontexts(javax.naming.Context JavaDoc ctx, String JavaDoc name)
1007        throws NamingException JavaDoc {
1008        javax.naming.Context JavaDoc currentContext = ctx;
1009        StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(name, "/");
1010        while (tokenizer.hasMoreTokens()) {
1011            String JavaDoc token = tokenizer.nextToken();
1012            if ((!token.equals("")) && (tokenizer.hasMoreTokens())) {
1013                try {
1014                    currentContext = currentContext.createSubcontext(token);
1015                } catch (NamingException JavaDoc e) {
1016                    // Silent catch. Probably an object is already bound in
1017
// the context.
1018
currentContext =
1019                        (javax.naming.Context JavaDoc) currentContext.lookup(token);
1020                }
1021            }
1022        }
1023    }
1024
1025
1026}
1027
Popular Tags