KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > deploy > NamingResources


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.deploy;
20
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.io.Serializable JavaDoc;
27
28
29 /**
30  * Holds and manages the naming resources defined in the J2EE Enterprise
31  * Naming Context and their associated JNDI context.
32  *
33  * @author Remy Maucherat
34  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
35  */

36
37 public class NamingResources implements Serializable JavaDoc {
38
39
40     // ----------------------------------------------------------- Constructors
41

42
43     /**
44      * Create a new NamingResources instance.
45      */

46     public NamingResources() {
47     }
48
49
50     // ----------------------------------------------------- Instance Variables
51

52
53     /**
54      * Associated container object.
55      */

56     private Object JavaDoc container = null;
57
58
59     /**
60      * List of naming entries, keyed by name. The value is the entry type, as
61      * declared by the user.
62      */

63     private Hashtable JavaDoc entries = new Hashtable JavaDoc();
64
65
66     /**
67      * The EJB resource references for this web application, keyed by name.
68      */

69     private HashMap JavaDoc ejbs = new HashMap JavaDoc();
70
71
72     /**
73      * The environment entries for this web application, keyed by name.
74      */

75     private HashMap JavaDoc envs = new HashMap JavaDoc();
76
77
78     /**
79      * The local EJB resource references for this web application, keyed by
80      * name.
81      */

82     private HashMap JavaDoc localEjbs = new HashMap JavaDoc();
83
84
85     /**
86      * The message destination referencess for this web application,
87      * keyed by name.
88      */

89     private HashMap JavaDoc mdrs = new HashMap JavaDoc();
90
91
92     /**
93      * The resource environment references for this web application,
94      * keyed by name.
95      */

96     private HashMap JavaDoc resourceEnvRefs = new HashMap JavaDoc();
97
98
99     /**
100      * The resource references for this web application, keyed by name.
101      */

102     private HashMap JavaDoc resources = new HashMap JavaDoc();
103
104
105     /**
106      * The resource links for this web application, keyed by name.
107      */

108     private HashMap JavaDoc resourceLinks = new HashMap JavaDoc();
109
110
111     /**
112      * The web service references for this web application, keyed by name.
113      */

114     private HashMap JavaDoc services = new HashMap JavaDoc();
115
116
117     /**
118      * The transaction for this webapp.
119      */

120     private ContextTransaction transaction = null;
121
122
123     /**
124      * The property change support for this component.
125      */

126     protected PropertyChangeSupport JavaDoc support = new PropertyChangeSupport JavaDoc(this);
127
128
129     // ------------------------------------------------------------- Properties
130

131
132     /**
133      * Get the container with which the naming resources are associated.
134      */

135     public Object JavaDoc getContainer() {
136         return container;
137     }
138
139
140     /**
141      * Set the container with which the naming resources are associated.
142      */

143     public void setContainer(Object JavaDoc container) {
144         this.container = container;
145     }
146
147     
148     /**
149      * Set the transaction object.
150      */

151     public void setTransaction(ContextTransaction transaction) {
152         this.transaction = transaction;
153     }
154     
155
156     /**
157      * Get the transaction object.
158      */

159     public ContextTransaction getTransaction() {
160         return transaction;
161     }
162     
163
164     /**
165      * Add an EJB resource reference for this web application.
166      *
167      * @param ejb New EJB resource reference
168      */

169     public void addEjb(ContextEjb ejb) {
170
171         if (entries.containsKey(ejb.getName())) {
172             return;
173         } else {
174             entries.put(ejb.getName(), ejb.getType());
175         }
176
177         synchronized (ejbs) {
178             ejb.setNamingResources(this);
179             ejbs.put(ejb.getName(), ejb);
180         }
181         support.firePropertyChange("ejb", null, ejb);
182
183     }
184
185
186     /**
187      * Add an environment entry for this web application.
188      *
189      * @param environment New environment entry
190      */

191     public void addEnvironment(ContextEnvironment environment) {
192
193         if (entries.containsKey(environment.getName())) {
194             if (findEnvironment(environment.getName()).getOverride()) {
195                 removeEnvironment(environment.getName());
196             } else {
197                 return;
198             }
199         }
200         
201         entries.put(environment.getName(), environment.getType());
202
203         synchronized (envs) {
204             environment.setNamingResources(this);
205             envs.put(environment.getName(), environment);
206         }
207         support.firePropertyChange("environment", null, environment);
208
209     }
210
211
212     /**
213      * Add a local EJB resource reference for this web application.
214      *
215      * @param ejb New EJB resource reference
216      */

217     public void addLocalEjb(ContextLocalEjb ejb) {
218
219         if (entries.containsKey(ejb.getName())) {
220             return;
221         } else {
222             entries.put(ejb.getName(), ejb.getType());
223         }
224
225         synchronized (localEjbs) {
226             ejb.setNamingResources(this);
227             localEjbs.put(ejb.getName(), ejb);
228         }
229         support.firePropertyChange("localEjb", null, ejb);
230
231     }
232
233
234     /**
235      * Add a message destination reference for this web application.
236      *
237      * @param mdr New message destination reference
238      */

239     public void addMessageDestinationRef(MessageDestinationRef mdr) {
240
241         if (entries.containsKey(mdr.getName())) {
242             return;
243         } else {
244             entries.put(mdr.getName(), mdr.getType());
245         }
246
247         synchronized (mdrs) {
248             mdr.setNamingResources(this);
249             mdrs.put(mdr.getName(), mdr);
250         }
251         support.firePropertyChange("messageDestinationRef", null, mdr);
252
253     }
254
255
256     /**
257      * Add a property change listener to this component.
258      *
259      * @param listener The listener to add
260      */

261     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
262
263         support.addPropertyChangeListener(listener);
264
265     }
266
267
268     /**
269      * Add a resource reference for this web application.
270      *
271      * @param resource New resource reference
272      */

273     public void addResource(ContextResource resource) {
274
275         if (entries.containsKey(resource.getName())) {
276             return;
277         } else {
278             entries.put(resource.getName(), resource.getType());
279         }
280
281         synchronized (resources) {
282             resource.setNamingResources(this);
283             resources.put(resource.getName(), resource);
284         }
285         support.firePropertyChange("resource", null, resource);
286
287     }
288
289
290     /**
291      * Add a resource environment reference for this web application.
292      *
293      * @param resource The resource
294      */

295     public void addResourceEnvRef(ContextResourceEnvRef resource) {
296
297         if (entries.containsKey(resource.getName())) {
298             return;
299         } else {
300             entries.put(resource.getName(), resource.getType());
301         }
302
303         synchronized (localEjbs) {
304             resource.setNamingResources(this);
305             resourceEnvRefs.put(resource.getName(), resource);
306         }
307         support.firePropertyChange("resourceEnvRef", null, resource);
308
309     }
310
311
312     /**
313      * Add a resource link for this web application.
314      *
315      * @param resourceLink New resource link
316      */

317     public void addResourceLink(ContextResourceLink resourceLink) {
318
319         if (entries.containsKey(resourceLink.getName())) {
320             return;
321         } else {
322             Object JavaDoc value = resourceLink.getType();
323             if (value == null) {
324                 value = "";
325             }
326             entries.put(resourceLink.getName(), value);
327         }
328
329         synchronized (resourceLinks) {
330             resourceLink.setNamingResources(this);
331             resourceLinks.put(resourceLink.getName(), resourceLink);
332         }
333         support.firePropertyChange("resourceLink", null, resourceLink);
334
335     }
336
337
338     /**
339      * Add a web service reference for this web application.
340      *
341      * @param service New web service reference
342      */

343     public void addService(ContextService service) {
344
345         if (entries.containsKey(service.getName())) {
346             return;
347         } else {
348             entries.put(service.getName(), service.getServiceinterface());
349         }
350         
351         synchronized (services) {
352             service.setNamingResources(this);
353             services.put(service.getName(), service);
354         }
355         support.firePropertyChange("service", null, service);
356         
357     }
358
359
360     /**
361      * Return the EJB resource reference with the specified name, if any;
362      * otherwise, return <code>null</code>.
363      *
364      * @param name Name of the desired EJB resource reference
365      */

366     public ContextEjb findEjb(String JavaDoc name) {
367
368         synchronized (ejbs) {
369             return ((ContextEjb) ejbs.get(name));
370         }
371
372     }
373
374
375     /**
376      * Return the defined EJB resource references for this application.
377      * If there are none, a zero-length array is returned.
378      */

379     public ContextEjb[] findEjbs() {
380
381         synchronized (ejbs) {
382             ContextEjb results[] = new ContextEjb[ejbs.size()];
383             return ((ContextEjb[]) ejbs.values().toArray(results));
384         }
385
386     }
387
388
389     /**
390      * Return the environment entry with the specified name, if any;
391      * otherwise, return <code>null</code>.
392      *
393      * @param name Name of the desired environment entry
394      */

395     public ContextEnvironment findEnvironment(String JavaDoc name) {
396
397         synchronized (envs) {
398             return ((ContextEnvironment) envs.get(name));
399         }
400
401     }
402
403
404     /**
405      * Return the set of defined environment entries for this web
406      * application. If none have been defined, a zero-length array
407      * is returned.
408      */

409     public ContextEnvironment[] findEnvironments() {
410
411         synchronized (envs) {
412             ContextEnvironment results[] = new ContextEnvironment[envs.size()];
413             return ((ContextEnvironment[]) envs.values().toArray(results));
414         }
415
416     }
417
418
419     /**
420      * Return the local EJB resource reference with the specified name, if any;
421      * otherwise, return <code>null</code>.
422      *
423      * @param name Name of the desired EJB resource reference
424      */

425     public ContextLocalEjb findLocalEjb(String JavaDoc name) {
426
427         synchronized (localEjbs) {
428             return ((ContextLocalEjb) localEjbs.get(name));
429         }
430
431     }
432
433
434     /**
435      * Return the defined local EJB resource references for this application.
436      * If there are none, a zero-length array is returned.
437      */

438     public ContextLocalEjb[] findLocalEjbs() {
439
440         synchronized (localEjbs) {
441             ContextLocalEjb results[] = new ContextLocalEjb[localEjbs.size()];
442             return ((ContextLocalEjb[]) localEjbs.values().toArray(results));
443         }
444
445     }
446
447
448     /**
449      * Return the message destination reference with the specified name,
450      * if any; otherwise, return <code>null</code>.
451      *
452      * @param name Name of the desired message destination reference
453      */

454     public MessageDestinationRef findMessageDestinationRef(String JavaDoc name) {
455
456         synchronized (mdrs) {
457             return ((MessageDestinationRef) mdrs.get(name));
458         }
459
460     }
461
462
463     /**
464      * Return the defined message destination references for this application.
465      * If there are none, a zero-length array is returned.
466      */

467     public MessageDestinationRef[] findMessageDestinationRefs() {
468
469         synchronized (mdrs) {
470             MessageDestinationRef results[] =
471                 new MessageDestinationRef[mdrs.size()];
472             return ((MessageDestinationRef[]) mdrs.values().toArray(results));
473         }
474
475     }
476
477
478     /**
479      * Return the resource reference with the specified name, if any;
480      * otherwise return <code>null</code>.
481      *
482      * @param name Name of the desired resource reference
483      */

484     public ContextResource findResource(String JavaDoc name) {
485
486         synchronized (resources) {
487             return ((ContextResource) resources.get(name));
488         }
489
490     }
491
492
493     /**
494      * Return the resource link with the specified name, if any;
495      * otherwise return <code>null</code>.
496      *
497      * @param name Name of the desired resource link
498      */

499     public ContextResourceLink findResourceLink(String JavaDoc name) {
500
501         synchronized (resourceLinks) {
502             return ((ContextResourceLink) resourceLinks.get(name));
503         }
504
505     }
506
507
508     /**
509      * Return the defined resource links for this application. If
510      * none have been defined, a zero-length array is returned.
511      */

512     public ContextResourceLink[] findResourceLinks() {
513
514         synchronized (resourceLinks) {
515             ContextResourceLink results[] =
516                 new ContextResourceLink[resourceLinks.size()];
517             return ((ContextResourceLink[]) resourceLinks.values()
518                     .toArray(results));
519         }
520
521     }
522
523
524     /**
525      * Return the defined resource references for this application. If
526      * none have been defined, a zero-length array is returned.
527      */

528     public ContextResource[] findResources() {
529
530         synchronized (resources) {
531             ContextResource results[] = new ContextResource[resources.size()];
532             return ((ContextResource[]) resources.values().toArray(results));
533         }
534
535     }
536
537
538     /**
539      * Return the resource environment reference type for the specified
540      * name, if any; otherwise return <code>null</code>.
541      *
542      * @param name Name of the desired resource environment reference
543      */

544     public ContextResourceEnvRef findResourceEnvRef(String JavaDoc name) {
545
546         synchronized (resourceEnvRefs) {
547             return ((ContextResourceEnvRef) resourceEnvRefs.get(name));
548         }
549
550     }
551
552
553     /**
554      * Return the set of resource environment reference names for this
555      * web application. If none have been specified, a zero-length
556      * array is returned.
557      */

558     public ContextResourceEnvRef[] findResourceEnvRefs() {
559
560         synchronized (resourceEnvRefs) {
561             ContextResourceEnvRef results[] = new ContextResourceEnvRef[resourceEnvRefs.size()];
562             return ((ContextResourceEnvRef[]) resourceEnvRefs.values().toArray(results));
563         }
564
565     }
566
567
568     /**
569      * Return the web service reference for the specified
570      * name, if any; otherwise return <code>null</code>.
571      *
572      * @param name Name of the desired web service
573      */

574     public ContextService findService(String JavaDoc name) {
575
576         synchronized (services) {
577             return ((ContextService) services.get(name));
578         }
579
580     }
581
582
583     /**
584      * Return the defined web service references for this application. If
585      * none have been defined, a zero-length array is returned.
586      */

587     public ContextService[] findServices() {
588         
589         synchronized (services) {
590             ContextService results[] = new ContextService[services.size()];
591             return ((ContextService[]) services.values().toArray(results));
592         }
593         
594     }
595
596
597     /**
598      * Return true if the name specified already exists.
599      */

600     public boolean exists(String JavaDoc name) {
601
602         return (entries.containsKey(name));
603
604     }
605
606
607     /**
608      * Remove any EJB resource reference with the specified name.
609      *
610      * @param name Name of the EJB resource reference to remove
611      */

612     public void removeEjb(String JavaDoc name) {
613
614         entries.remove(name);
615
616         ContextEjb ejb = null;
617         synchronized (ejbs) {
618             ejb = (ContextEjb) ejbs.remove(name);
619         }
620         if (ejb != null) {
621             support.firePropertyChange("ejb", ejb, null);
622             ejb.setNamingResources(null);
623         }
624
625     }
626
627
628     /**
629      * Remove any environment entry with the specified name.
630      *
631      * @param name Name of the environment entry to remove
632      */

633     public void removeEnvironment(String JavaDoc name) {
634
635         entries.remove(name);
636
637         ContextEnvironment environment = null;
638         synchronized (envs) {
639             environment = (ContextEnvironment) envs.remove(name);
640         }
641         if (environment != null) {
642             support.firePropertyChange("environment", environment, null);
643             environment.setNamingResources(null);
644         }
645
646     }
647
648
649     /**
650      * Remove any local EJB resource reference with the specified name.
651      *
652      * @param name Name of the EJB resource reference to remove
653      */

654     public void removeLocalEjb(String JavaDoc name) {
655
656         entries.remove(name);
657
658         ContextLocalEjb localEjb = null;
659         synchronized (localEjbs) {
660             localEjb = (ContextLocalEjb) ejbs.remove(name);
661         }
662         if (localEjb != null) {
663             support.firePropertyChange("localEjb", localEjb, null);
664             localEjb.setNamingResources(null);
665         }
666
667     }
668
669
670     /**
671      * Remove any message destination reference with the specified name.
672      *
673      * @param name Name of the message destination resource reference to remove
674      */

675     public void removeMessageDestinationRef(String JavaDoc name) {
676
677         entries.remove(name);
678
679         MessageDestinationRef mdr = null;
680         synchronized (mdrs) {
681             mdr = (MessageDestinationRef) mdrs.remove(name);
682         }
683         if (mdr != null) {
684             support.firePropertyChange("messageDestinationRef",
685                                        mdr, null);
686             mdr.setNamingResources(null);
687         }
688
689     }
690
691
692     /**
693      * Remove a property change listener from this component.
694      *
695      * @param listener The listener to remove
696      */

697     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
698
699         support.removePropertyChangeListener(listener);
700
701     }
702
703
704     /**
705      * Remove any resource reference with the specified name.
706      *
707      * @param name Name of the resource reference to remove
708      */

709     public void removeResource(String JavaDoc name) {
710
711         entries.remove(name);
712
713         ContextResource resource = null;
714         synchronized (resources) {
715             resource = (ContextResource) resources.remove(name);
716         }
717         if (resource != null) {
718             support.firePropertyChange("resource", resource, null);
719             resource.setNamingResources(null);
720         }
721
722     }
723
724
725     /**
726      * Remove any resource environment reference with the specified name.
727      *
728      * @param name Name of the resource environment reference to remove
729      */

730     public void removeResourceEnvRef(String JavaDoc name) {
731
732         entries.remove(name);
733
734         String JavaDoc type = null;
735         synchronized (resourceEnvRefs) {
736             type = (String JavaDoc) resourceEnvRefs.remove(name);
737         }
738         if (type != null) {
739             support.firePropertyChange("resourceEnvRef",
740                                        name + ":" + type, null);
741         }
742
743     }
744
745
746     /**
747      * Remove any resource link with the specified name.
748      *
749      * @param name Name of the resource link to remove
750      */

751     public void removeResourceLink(String JavaDoc name) {
752
753         entries.remove(name);
754
755         ContextResourceLink resourceLink = null;
756         synchronized (resourceLinks) {
757             resourceLink = (ContextResourceLink) resourceLinks.remove(name);
758         }
759         if (resourceLink != null) {
760             support.firePropertyChange("resourceLink", resourceLink, null);
761             resourceLink.setNamingResources(null);
762         }
763
764     }
765
766
767     /**
768      * Remove any web service reference with the specified name.
769      *
770      * @param name Name of the web service reference to remove
771      */

772     public void removeService(String JavaDoc name) {
773         
774         entries.remove(name);
775         
776         ContextService service = null;
777         synchronized (services) {
778             service = (ContextService) services.remove(name);
779         }
780         if (service != null) {
781             support.firePropertyChange("service", service, null);
782             service.setNamingResources(null);
783         }
784         
785     }
786
787
788 }
789
Popular Tags