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;
23 import java.beans.PropertyChangeSupport;
24 import java.util.HashMap;
25 import java.util.Hashtable;
26 import java.io.Serializable;
27
28 import org.apache.catalina.ServerFactory;
29
30
31 /**
32 * Holds and manages the naming resources defined in the J2EE Enterprise
33 * Naming Context and their associated JNDI context.
34 *
35 * @author Remy Maucherat
36 * @version $Revision: 550048 $ $Date: 2007-06-23 17:00:51 +0200 (sam., 23 juin 2007) $
37 */
38
39 public class NamingResources implements Serializable {
40
41
42 // ----------------------------------------------------------- Constructors
43
44
45 /**
46 * Create a new NamingResources instance.
47 */
48 public NamingResources() {
49 }
50
51
52 // ----------------------------------------------------- Instance Variables
53
54
55 /**
56 * Associated container object.
57 */
58 private Object container = null;
59
60
61 /**
62 * List of naming entries, keyed by name. The value is the entry type, as
63 * declared by the user.
64 */
65 private Hashtable entries = new Hashtable();
66
67
68 /**
69 * The EJB resource references for this web application, keyed by name.
70 */
71 private HashMap ejbs = new HashMap();
72
73
74 /**
75 * The environment entries for this web application, keyed by name.
76 */
77 private HashMap envs = new HashMap();
78
79
80 /**
81 * The local EJB resource references for this web application, keyed by
82 * name.
83 */
84 private HashMap localEjbs = new HashMap();
85
86
87 /**
88 * The message destination referencess for this web application,
89 * keyed by name.
90 */
91 private HashMap mdrs = new HashMap();
92
93
94 /**
95 * The resource environment references for this web application,
96 * keyed by name.
97 */
98 private HashMap resourceEnvRefs = new HashMap();
99
100
101 /**
102 * The resource references for this web application, keyed by name.
103 */
104 private HashMap resources = new HashMap();
105
106
107 /**
108 * The resource links for this web application, keyed by name.
109 */
110 private HashMap resourceLinks = new HashMap();
111
112
113 /**
114 * The web service references for this web application, keyed by name.
115 */
116 private HashMap services = new HashMap();
117
118
119 /**
120 * The transaction for this webapp.
121 */
122 private ContextTransaction transaction = null;
123
124
125 /**
126 * The property change support for this component.
127 */
128 protected PropertyChangeSupport support = new PropertyChangeSupport(this);
129
130
131 // ------------------------------------------------------------- Properties
132
133
134 /**
135 * Get the container with which the naming resources are associated.
136 */
137 public Object getContainer() {
138 return container;
139 }
140
141
142 /**
143 * Set the container with which the naming resources are associated.
144 */
145 public void setContainer(Object container) {
146 this.container = container;
147 }
148
149
150 /**
151 * Set the transaction object.
152 */
153 public void setTransaction(ContextTransaction transaction) {
154 this.transaction = transaction;
155 }
156
157
158 /**
159 * Get the transaction object.
160 */
161 public ContextTransaction getTransaction() {
162 return transaction;
163 }
164
165
166 /**
167 * Add an EJB resource reference for this web application.
168 *
169 * @param ejb New EJB resource reference
170 */
171 public void addEjb(ContextEjb ejb) {
172
173 if (entries.containsKey(ejb.getName())) {
174 return;
175 } else {
176 entries.put(ejb.getName(), ejb.getType());
177 }
178
179 synchronized (ejbs) {
180 ejb.setNamingResources(this);
181 ejbs.put(ejb.getName(), ejb);
182 }
183 support.firePropertyChange("ejb", null, ejb);
184
185 }
186
187
188 /**
189 * Add an environment entry for this web application.
190 *
191 * @param environment New environment entry
192 */
193 public void addEnvironment(ContextEnvironment environment) {
194
195 if (entries.containsKey(environment.getName())) {
196 ContextEnvironment ce = findEnvironment(environment.getName());
197 ContextResourceLink rl = findResourceLink(environment.getName());
198 if (ce != null) {
199 if (ce.getOverride()) {
200 removeEnvironment(environment.getName());
201 } else {
202 return;
203 }
204 } else if (rl != null) {
205 // Link. Need to look at the global resources
206 NamingResources global =
207 ServerFactory.getServer().getGlobalNamingResources();
208 if (global.findEnvironment(rl.getGlobal()) != null) {
209 if (global.findEnvironment(rl.getGlobal()).getOverride()) {
210 removeResourceLink(environment.getName());
211 } else {
212 return;
213 }
214 }
215 } else {
216 // It exists but it isn't an env or a res link...
217 return;
218 }
219 }
220
221 entries.put(environment.getName(), environment.getType());
222
223 synchronized (envs) {
224 environment.setNamingResources(this);
225 envs.put(environment.getName(), environment);
226 }
227 support.firePropertyChange("environment", null, environment);
228
229 }
230
231
232 /**
233 * Add a local EJB resource reference for this web application.
234 *
235 * @param ejb New EJB resource reference
236 */
237 public void addLocalEjb(ContextLocalEjb ejb) {
238
239 if (entries.containsKey(ejb.getName())) {
240 return;
241 } else {
242 entries.put(ejb.getName(), ejb.getType());
243 }
244
245 synchronized (localEjbs) {
246 ejb.setNamingResources(this);
247 localEjbs.put(ejb.getName(), ejb);
248 }
249 support.firePropertyChange("localEjb", null, ejb);
250
251 }
252
253
254 /**
255 * Add a message destination reference for this web application.
256 *
257 * @param mdr New message destination reference
258 */
259 public void addMessageDestinationRef(MessageDestinationRef mdr) {
260
261 if (entries.containsKey(mdr.getName())) {
262 return;
263 } else {
264 entries.put(mdr.getName(), mdr.getType());
265 }
266
267 synchronized (mdrs) {
268 mdr.setNamingResources(this);
269 mdrs.put(mdr.getName(), mdr);
270 }
271 support.firePropertyChange("messageDestinationRef", null, mdr);
272
273 }
274
275
276 /**
277 * Add a property change listener to this component.
278 *
279 * @param listener The listener to add
280 */
281 public void addPropertyChangeListener(PropertyChangeListener listener) {
282
283 support.addPropertyChangeListener(listener);
284
285 }
286
287
288 /**
289 * Add a resource reference for this web application.
290 *
291 * @param resource New resource reference
292 */
293 public void addResource(ContextResource resource) {
294
295 if (entries.containsKey(resource.getName())) {
296 return;
297 } else {
298 entries.put(resource.getName(), resource.getType());
299 }
300
301 synchronized (resources) {
302 resource.setNamingResources(this);
303 resources.put(resource.getName(), resource);
304 }
305 support.firePropertyChange("resource", null, resource);
306
307 }
308
309
310 /**
311 * Add a resource environment reference for this web application.
312 *
313 * @param resource The resource
314 */
315 public void addResourceEnvRef(ContextResourceEnvRef resource) {
316
317 if (entries.containsKey(resource.getName())) {
318 return;
319 } else {
320 entries.put(resource.getName(), resource.getType());
321 }
322
323 synchronized (localEjbs) {
324 resource.setNamingResources(this);
325 resourceEnvRefs.put(resource.getName(), resource);
326 }
327 support.firePropertyChange("resourceEnvRef", null, resource);
328
329 }
330
331
332 /**
333 * Add a resource link for this web application.
334 *
335 * @param resourceLink New resource link
336 */
337 public void addResourceLink(ContextResourceLink resourceLink) {
338
339 if (entries.containsKey(resourceLink.getName())) {
340 return;
341 } else {
342 Object value = resourceLink.getType();
343 if (value == null) {
344 value = "";
345 }
346 entries.put(resourceLink.getName(), value);
347 }
348
349 synchronized (resourceLinks) {
350 resourceLink.setNamingResources(this);
351 resourceLinks.put(resourceLink.getName(), resourceLink);
352 }
353 support.firePropertyChange("resourceLink", null, resourceLink);
354
355 }
356
357
358 /**
359 * Add a web service reference for this web application.
360 *
361 * @param service New web service reference
362 */
363 public void addService(ContextService service) {
364
365 if (entries.containsKey(service.getName())) {
366 return;
367 } else {
368 Object value = service.getType();
369 if (value == null) {
370 value = "";
371 }
372 entries.put(service.getName(), value);
373 }
374
375 synchronized (services) {
376 service.setNamingResources(this);
377 services.put(service.getName(), service);
378 }
379 support.firePropertyChange("service", null, service);
380
381 }
382
383
384 /**
385 * Return the EJB resource reference with the specified name, if any;
386 * otherwise, return <code>null</code>.
387 *
388 * @param name Name of the desired EJB resource reference
389 */
390 public ContextEjb findEjb(String name) {
391
392 synchronized (ejbs) {
393 return ((ContextEjb) ejbs.get(name));
394 }
395
396 }
397
398
399 /**
400 * Return the defined EJB resource references for this application.
401 * If there are none, a zero-length array is returned.
402 */
403 public ContextEjb[] findEjbs() {
404
405 synchronized (ejbs) {
406 ContextEjb results[] = new ContextEjb[ejbs.size()];
407 return ((ContextEjb[]) ejbs.values().toArray(results));
408 }
409
410 }
411
412
413 /**
414 * Return the environment entry with the specified name, if any;
415 * otherwise, return <code>null</code>.
416 *
417 * @param name Name of the desired environment entry
418 */
419 public ContextEnvironment findEnvironment(String name) {
420
421 synchronized (envs) {
422 return ((ContextEnvironment) envs.get(name));
423 }
424
425 }
426
427
428 /**
429 * Return the set of defined environment entries for this web
430 * application. If none have been defined, a zero-length array
431 * is returned.
432 */
433 public ContextEnvironment[] findEnvironments() {
434
435 synchronized (envs) {
436 ContextEnvironment results[] = new ContextEnvironment[envs.size()];
437 return ((ContextEnvironment[]) envs.values().toArray(results));
438 }
439
440 }
441
442
443 /**
444 * Return the local EJB resource reference with the specified name, if any;
445 * otherwise, return <code>null</code>.
446 *
447 * @param name Name of the desired EJB resource reference
448 */
449 public ContextLocalEjb findLocalEjb(String name) {
450
451 synchronized (localEjbs) {
452 return ((ContextLocalEjb) localEjbs.get(name));
453 }
454
455 }
456
457
458 /**
459 * Return the defined local EJB resource references for this application.
460 * If there are none, a zero-length array is returned.
461 */
462 public ContextLocalEjb[] findLocalEjbs() {
463
464 synchronized (localEjbs) {
465 ContextLocalEjb results[] = new ContextLocalEjb[localEjbs.size()];
466 return ((ContextLocalEjb[]) localEjbs.values().toArray(results));
467 }
468
469 }
470
471
472 /**
473 * Return the message destination reference with the specified name,
474 * if any; otherwise, return <code>null</code>.
475 *
476 * @param name Name of the desired message destination reference
477 */
478 public MessageDestinationRef findMessageDestinationRef(String name) {
479
480 synchronized (mdrs) {
481 return ((MessageDestinationRef) mdrs.get(name));
482 }
483
484 }
485
486
487 /**
488 * Return the defined message destination references for this application.
489 * If there are none, a zero-length array is returned.
490 */
491 public MessageDestinationRef[] findMessageDestinationRefs() {
492
493 synchronized (mdrs) {
494 MessageDestinationRef results[] =
495 new MessageDestinationRef[mdrs.size()];
496 return ((MessageDestinationRef[]) mdrs.values().toArray(results));
497 }
498
499 }
500
501
502 /**
503 * Return the resource reference with the specified name, if any;
504 * otherwise return <code>null</code>.
505 *
506 * @param name Name of the desired resource reference
507 */
508 public ContextResource findResource(String name) {
509
510 synchronized (resources) {
511 return ((ContextResource) resources.get(name));
512 }
513
514 }
515
516
517 /**
518 * Return the resource link with the specified name, if any;
519 * otherwise return <code>null</code>.
520 *
521 * @param name Name of the desired resource link
522 */
523 public ContextResourceLink findResourceLink(String name) {
524
525 synchronized (resourceLinks) {
526 return ((ContextResourceLink) resourceLinks.get(name));
527 }
528
529 }
530
531
532 /**
533 * Return the defined resource links for this application. If
534 * none have been defined, a zero-length array is returned.
535 */
536 public ContextResourceLink[] findResourceLinks() {
537
538 synchronized (resourceLinks) {
539 ContextResourceLink results[] =
540 new ContextResourceLink[resourceLinks.size()];
541 return ((ContextResourceLink[]) resourceLinks.values()
542 .toArray(results));
543 }
544
545 }
546
547
548 /**
549 * Return the defined resource references for this application. If
550 * none have been defined, a zero-length array is returned.
551 */
552 public ContextResource[] findResources() {
553
554 synchronized (resources) {
555 ContextResource results[] = new ContextResource[resources.size()];
556 return ((ContextResource[]) resources.values().toArray(results));
557 }
558
559 }
560
561
562 /**
563 * Return the resource environment reference type for the specified
564 * name, if any; otherwise return <code>null</code>.
565 *
566 * @param name Name of the desired resource environment reference
567 */
568 public ContextResourceEnvRef findResourceEnvRef(String name) {
569
570 synchronized (resourceEnvRefs) {
571 return ((ContextResourceEnvRef) resourceEnvRefs.get(name));
572 }
573
574 }
575
576
577 /**
578 * Return the set of resource environment reference names for this
579 * web application. If none have been specified, a zero-length
580 * array is returned.
581 */
582 public ContextResourceEnvRef[] findResourceEnvRefs() {
583
584 synchronized (resourceEnvRefs) {
585 ContextResourceEnvRef results[] = new ContextResourceEnvRef[resourceEnvRefs.size()];
586 return ((ContextResourceEnvRef[]) resourceEnvRefs.values().toArray(results));
587 }
588
589 }
590
591
592 /**
593 * Return the web service reference for the specified
594 * name, if any; otherwise return <code>null</code>.
595 *
596 * @param name Name of the desired web service
597 */
598 public ContextService findService(String name) {
599
600 synchronized (services) {
601 return ((ContextService) services.get(name));
602 }
603
604 }
605
606
607 /**
608 * Return the defined web service references for this application. If
609 * none have been defined, a zero-length array is returned.
610 */
611 public ContextService[] findServices() {
612
613 synchronized (services) {
614 ContextService results[] = new ContextService[services.size()];
615 return ((ContextService[]) services.values().toArray(results));
616 }
617
618 }
619
620
621 /**
622 * Return true if the name specified already exists.
623 */
624 public boolean exists(String name) {
625
626 return (entries.containsKey(name));
627
628 }
629
630
631 /**
632 * Remove any EJB resource reference with the specified name.
633 *
634 * @param name Name of the EJB resource reference to remove
635 */
636 public void removeEjb(String name) {
637
638 entries.remove(name);
639
640 ContextEjb ejb = null;
641 synchronized (ejbs) {
642 ejb = (ContextEjb) ejbs.remove(name);
643 }
644 if (ejb != null) {
645 support.firePropertyChange("ejb", ejb, null);
646 ejb.setNamingResources(null);
647 }
648
649 }
650
651
652 /**
653 * Remove any environment entry with the specified name.
654 *
655 * @param name Name of the environment entry to remove
656 */
657 public void removeEnvironment(String name) {
658
659 entries.remove(name);
660
661 ContextEnvironment environment = null;
662 synchronized (envs) {
663 environment = (ContextEnvironment) envs.remove(name);
664 }
665 if (environment != null) {
666 support.firePropertyChange("environment", environment, null);
667 environment.setNamingResources(null);
668 }
669
670 }
671
672
673 /**
674 * Remove any local EJB resource reference with the specified name.
675 *
676 * @param name Name of the EJB resource reference to remove
677 */
678 public void removeLocalEjb(String name) {
679
680 entries.remove(name);
681
682 ContextLocalEjb localEjb = null;
683 synchronized (localEjbs) {
684 localEjb = (ContextLocalEjb) ejbs.remove(name);
685 }
686 if (localEjb != null) {
687 support.firePropertyChange("localEjb", localEjb, null);
688 localEjb.setNamingResources(null);
689 }
690
691 }
692
693
694 /**
695 * Remove any message destination reference with the specified name.
696 *
697 * @param name Name of the message destination resource reference to remove
698 */
699 public void removeMessageDestinationRef(String name) {
700
701 entries.remove(name);
702
703 MessageDestinationRef mdr = null;
704 synchronized (mdrs) {
705 mdr = (MessageDestinationRef) mdrs.remove(name);
706 }
707 if (mdr != null) {
708 support.firePropertyChange("messageDestinationRef",
709 mdr, null);
710 mdr.setNamingResources(null);
711 }
712
713 }
714
715
716 /**
717 * Remove a property change listener from this component.
718 *
719 * @param listener The listener to remove
720 */
721 public void removePropertyChangeListener(PropertyChangeListener listener) {
722
723 support.removePropertyChangeListener(listener);
724
725 }
726
727
728 /**
729 * Remove any resource reference with the specified name.
730 *
731 * @param name Name of the resource reference to remove
732 */
733 public void removeResource(String name) {
734
735 entries.remove(name);
736
737 ContextResource resource = null;
738 synchronized (resources) {
739 resource = (ContextResource) resources.remove(name);
740 }
741 if (resource != null) {
742 support.firePropertyChange("resource", resource, null);
743 resource.setNamingResources(null);
744 }
745
746 }
747
748
749 /**
750 * Remove any resource environment reference with the specified name.
751 *
752 * @param name Name of the resource environment reference to remove
753 */
754 public void removeResourceEnvRef(String name) {
755
756 entries.remove(name);
757
758 String type = null;
759 synchronized (resourceEnvRefs) {
760 type = (String) resourceEnvRefs.remove(name);
761 }
762 if (type != null) {
763 support.firePropertyChange("resourceEnvRef",
764 name + ":" + type, null);
765 }
766
767 }
768
769
770 /**
771 * Remove any resource link with the specified name.
772 *
773 * @param name Name of the resource link to remove
774 */
775 public void removeResourceLink(String name) {
776
777 entries.remove(name);
778
779 ContextResourceLink resourceLink = null;
780 synchronized (resourceLinks) {
781 resourceLink = (ContextResourceLink) resourceLinks.remove(name);
782 }
783 if (resourceLink != null) {
784 support.firePropertyChange("resourceLink", resourceLink, null);
785 resourceLink.setNamingResources(null);
786 }
787
788 }
789
790
791 /**
792 * Remove any web service reference with the specified name.
793 *
794 * @param name Name of the web service reference to remove
795 */
796 public void removeService(String name) {
797
798 entries.remove(name);
799
800 ContextService service = null;
801 synchronized (services) {
802 service = (ContextService) services.remove(name);
803 }
804 if (service != null) {
805 support.firePropertyChange("service", service, null);
806 service.setNamingResources(null);
807 }
808
809 }
810
811
812 }