001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.deploy;
019:
020: import java.beans.PropertyChangeListener;
021: import java.beans.PropertyChangeSupport;
022: import java.util.HashMap;
023: import java.util.Hashtable;
024: import java.io.Serializable;
025:
026: import org.apache.catalina.ServerFactory;
027:
028: /**
029: * Holds and manages the naming resources defined in the J2EE Enterprise
030: * Naming Context and their associated JNDI context.
031: *
032: * @author Remy Maucherat
033: * @version $Revision: 550048 $ $Date: 2007-06-23 17:00:51 +0200 (sam., 23 juin 2007) $
034: */
035:
036: public class NamingResources implements Serializable {
037:
038: // ----------------------------------------------------------- Constructors
039:
040: /**
041: * Create a new NamingResources instance.
042: */
043: public NamingResources() {
044: }
045:
046: // ----------------------------------------------------- Instance Variables
047:
048: /**
049: * Associated container object.
050: */
051: private Object container = null;
052:
053: /**
054: * List of naming entries, keyed by name. The value is the entry type, as
055: * declared by the user.
056: */
057: private Hashtable entries = new Hashtable();
058:
059: /**
060: * The EJB resource references for this web application, keyed by name.
061: */
062: private HashMap ejbs = new HashMap();
063:
064: /**
065: * The environment entries for this web application, keyed by name.
066: */
067: private HashMap envs = new HashMap();
068:
069: /**
070: * The local EJB resource references for this web application, keyed by
071: * name.
072: */
073: private HashMap localEjbs = new HashMap();
074:
075: /**
076: * The message destination referencess for this web application,
077: * keyed by name.
078: */
079: private HashMap mdrs = new HashMap();
080:
081: /**
082: * The resource environment references for this web application,
083: * keyed by name.
084: */
085: private HashMap resourceEnvRefs = new HashMap();
086:
087: /**
088: * The resource references for this web application, keyed by name.
089: */
090: private HashMap resources = new HashMap();
091:
092: /**
093: * The resource links for this web application, keyed by name.
094: */
095: private HashMap resourceLinks = new HashMap();
096:
097: /**
098: * The web service references for this web application, keyed by name.
099: */
100: private HashMap services = new HashMap();
101:
102: /**
103: * The transaction for this webapp.
104: */
105: private ContextTransaction transaction = null;
106:
107: /**
108: * The property change support for this component.
109: */
110: protected PropertyChangeSupport support = new PropertyChangeSupport(
111: this );
112:
113: // ------------------------------------------------------------- Properties
114:
115: /**
116: * Get the container with which the naming resources are associated.
117: */
118: public Object getContainer() {
119: return container;
120: }
121:
122: /**
123: * Set the container with which the naming resources are associated.
124: */
125: public void setContainer(Object container) {
126: this .container = container;
127: }
128:
129: /**
130: * Set the transaction object.
131: */
132: public void setTransaction(ContextTransaction transaction) {
133: this .transaction = transaction;
134: }
135:
136: /**
137: * Get the transaction object.
138: */
139: public ContextTransaction getTransaction() {
140: return transaction;
141: }
142:
143: /**
144: * Add an EJB resource reference for this web application.
145: *
146: * @param ejb New EJB resource reference
147: */
148: public void addEjb(ContextEjb ejb) {
149:
150: if (entries.containsKey(ejb.getName())) {
151: return;
152: } else {
153: entries.put(ejb.getName(), ejb.getType());
154: }
155:
156: synchronized (ejbs) {
157: ejb.setNamingResources(this );
158: ejbs.put(ejb.getName(), ejb);
159: }
160: support.firePropertyChange("ejb", null, ejb);
161:
162: }
163:
164: /**
165: * Add an environment entry for this web application.
166: *
167: * @param environment New environment entry
168: */
169: public void addEnvironment(ContextEnvironment environment) {
170:
171: if (entries.containsKey(environment.getName())) {
172: ContextEnvironment ce = findEnvironment(environment
173: .getName());
174: ContextResourceLink rl = findResourceLink(environment
175: .getName());
176: if (ce != null) {
177: if (ce.getOverride()) {
178: removeEnvironment(environment.getName());
179: } else {
180: return;
181: }
182: } else if (rl != null) {
183: // Link. Need to look at the global resources
184: NamingResources global = ServerFactory.getServer()
185: .getGlobalNamingResources();
186: if (global.findEnvironment(rl.getGlobal()) != null) {
187: if (global.findEnvironment(rl.getGlobal())
188: .getOverride()) {
189: removeResourceLink(environment.getName());
190: } else {
191: return;
192: }
193: }
194: } else {
195: // It exists but it isn't an env or a res link...
196: return;
197: }
198: }
199:
200: entries.put(environment.getName(), environment.getType());
201:
202: synchronized (envs) {
203: environment.setNamingResources(this );
204: envs.put(environment.getName(), environment);
205: }
206: support.firePropertyChange("environment", null, environment);
207:
208: }
209:
210: /**
211: * Add a local EJB resource reference for this web application.
212: *
213: * @param ejb New EJB resource reference
214: */
215: public void addLocalEjb(ContextLocalEjb ejb) {
216:
217: if (entries.containsKey(ejb.getName())) {
218: return;
219: } else {
220: entries.put(ejb.getName(), ejb.getType());
221: }
222:
223: synchronized (localEjbs) {
224: ejb.setNamingResources(this );
225: localEjbs.put(ejb.getName(), ejb);
226: }
227: support.firePropertyChange("localEjb", null, ejb);
228:
229: }
230:
231: /**
232: * Add a message destination reference for this web application.
233: *
234: * @param mdr New message destination reference
235: */
236: public void addMessageDestinationRef(MessageDestinationRef mdr) {
237:
238: if (entries.containsKey(mdr.getName())) {
239: return;
240: } else {
241: entries.put(mdr.getName(), mdr.getType());
242: }
243:
244: synchronized (mdrs) {
245: mdr.setNamingResources(this );
246: mdrs.put(mdr.getName(), mdr);
247: }
248: support.firePropertyChange("messageDestinationRef", null, mdr);
249:
250: }
251:
252: /**
253: * Add a property change listener to this component.
254: *
255: * @param listener The listener to add
256: */
257: public void addPropertyChangeListener(
258: PropertyChangeListener listener) {
259:
260: support.addPropertyChangeListener(listener);
261:
262: }
263:
264: /**
265: * Add a resource reference for this web application.
266: *
267: * @param resource New resource reference
268: */
269: public void addResource(ContextResource resource) {
270:
271: if (entries.containsKey(resource.getName())) {
272: return;
273: } else {
274: entries.put(resource.getName(), resource.getType());
275: }
276:
277: synchronized (resources) {
278: resource.setNamingResources(this );
279: resources.put(resource.getName(), resource);
280: }
281: support.firePropertyChange("resource", null, resource);
282:
283: }
284:
285: /**
286: * Add a resource environment reference for this web application.
287: *
288: * @param resource The resource
289: */
290: public void addResourceEnvRef(ContextResourceEnvRef resource) {
291:
292: if (entries.containsKey(resource.getName())) {
293: return;
294: } else {
295: entries.put(resource.getName(), resource.getType());
296: }
297:
298: synchronized (localEjbs) {
299: resource.setNamingResources(this );
300: resourceEnvRefs.put(resource.getName(), resource);
301: }
302: support.firePropertyChange("resourceEnvRef", null, resource);
303:
304: }
305:
306: /**
307: * Add a resource link for this web application.
308: *
309: * @param resourceLink New resource link
310: */
311: public void addResourceLink(ContextResourceLink resourceLink) {
312:
313: if (entries.containsKey(resourceLink.getName())) {
314: return;
315: } else {
316: Object value = resourceLink.getType();
317: if (value == null) {
318: value = "";
319: }
320: entries.put(resourceLink.getName(), value);
321: }
322:
323: synchronized (resourceLinks) {
324: resourceLink.setNamingResources(this );
325: resourceLinks.put(resourceLink.getName(), resourceLink);
326: }
327: support.firePropertyChange("resourceLink", null, resourceLink);
328:
329: }
330:
331: /**
332: * Add a web service reference for this web application.
333: *
334: * @param service New web service reference
335: */
336: public void addService(ContextService service) {
337:
338: if (entries.containsKey(service.getName())) {
339: return;
340: } else {
341: Object value = service.getType();
342: if (value == null) {
343: value = "";
344: }
345: entries.put(service.getName(), value);
346: }
347:
348: synchronized (services) {
349: service.setNamingResources(this );
350: services.put(service.getName(), service);
351: }
352: support.firePropertyChange("service", null, service);
353:
354: }
355:
356: /**
357: * Return the EJB resource reference with the specified name, if any;
358: * otherwise, return <code>null</code>.
359: *
360: * @param name Name of the desired EJB resource reference
361: */
362: public ContextEjb findEjb(String name) {
363:
364: synchronized (ejbs) {
365: return ((ContextEjb) ejbs.get(name));
366: }
367:
368: }
369:
370: /**
371: * Return the defined EJB resource references for this application.
372: * If there are none, a zero-length array is returned.
373: */
374: public ContextEjb[] findEjbs() {
375:
376: synchronized (ejbs) {
377: ContextEjb results[] = new ContextEjb[ejbs.size()];
378: return ((ContextEjb[]) ejbs.values().toArray(results));
379: }
380:
381: }
382:
383: /**
384: * Return the environment entry with the specified name, if any;
385: * otherwise, return <code>null</code>.
386: *
387: * @param name Name of the desired environment entry
388: */
389: public ContextEnvironment findEnvironment(String name) {
390:
391: synchronized (envs) {
392: return ((ContextEnvironment) envs.get(name));
393: }
394:
395: }
396:
397: /**
398: * Return the set of defined environment entries for this web
399: * application. If none have been defined, a zero-length array
400: * is returned.
401: */
402: public ContextEnvironment[] findEnvironments() {
403:
404: synchronized (envs) {
405: ContextEnvironment results[] = new ContextEnvironment[envs
406: .size()];
407: return ((ContextEnvironment[]) envs.values().toArray(
408: results));
409: }
410:
411: }
412:
413: /**
414: * Return the local EJB resource reference with the specified name, if any;
415: * otherwise, return <code>null</code>.
416: *
417: * @param name Name of the desired EJB resource reference
418: */
419: public ContextLocalEjb findLocalEjb(String name) {
420:
421: synchronized (localEjbs) {
422: return ((ContextLocalEjb) localEjbs.get(name));
423: }
424:
425: }
426:
427: /**
428: * Return the defined local EJB resource references for this application.
429: * If there are none, a zero-length array is returned.
430: */
431: public ContextLocalEjb[] findLocalEjbs() {
432:
433: synchronized (localEjbs) {
434: ContextLocalEjb results[] = new ContextLocalEjb[localEjbs
435: .size()];
436: return ((ContextLocalEjb[]) localEjbs.values().toArray(
437: results));
438: }
439:
440: }
441:
442: /**
443: * Return the message destination reference with the specified name,
444: * if any; otherwise, return <code>null</code>.
445: *
446: * @param name Name of the desired message destination reference
447: */
448: public MessageDestinationRef findMessageDestinationRef(String name) {
449:
450: synchronized (mdrs) {
451: return ((MessageDestinationRef) mdrs.get(name));
452: }
453:
454: }
455:
456: /**
457: * Return the defined message destination references for this application.
458: * If there are none, a zero-length array is returned.
459: */
460: public MessageDestinationRef[] findMessageDestinationRefs() {
461:
462: synchronized (mdrs) {
463: MessageDestinationRef results[] = new MessageDestinationRef[mdrs
464: .size()];
465: return ((MessageDestinationRef[]) mdrs.values().toArray(
466: results));
467: }
468:
469: }
470:
471: /**
472: * Return the resource reference with the specified name, if any;
473: * otherwise return <code>null</code>.
474: *
475: * @param name Name of the desired resource reference
476: */
477: public ContextResource findResource(String name) {
478:
479: synchronized (resources) {
480: return ((ContextResource) resources.get(name));
481: }
482:
483: }
484:
485: /**
486: * Return the resource link with the specified name, if any;
487: * otherwise return <code>null</code>.
488: *
489: * @param name Name of the desired resource link
490: */
491: public ContextResourceLink findResourceLink(String name) {
492:
493: synchronized (resourceLinks) {
494: return ((ContextResourceLink) resourceLinks.get(name));
495: }
496:
497: }
498:
499: /**
500: * Return the defined resource links for this application. If
501: * none have been defined, a zero-length array is returned.
502: */
503: public ContextResourceLink[] findResourceLinks() {
504:
505: synchronized (resourceLinks) {
506: ContextResourceLink results[] = new ContextResourceLink[resourceLinks
507: .size()];
508: return ((ContextResourceLink[]) resourceLinks.values()
509: .toArray(results));
510: }
511:
512: }
513:
514: /**
515: * Return the defined resource references for this application. If
516: * none have been defined, a zero-length array is returned.
517: */
518: public ContextResource[] findResources() {
519:
520: synchronized (resources) {
521: ContextResource results[] = new ContextResource[resources
522: .size()];
523: return ((ContextResource[]) resources.values().toArray(
524: results));
525: }
526:
527: }
528:
529: /**
530: * Return the resource environment reference type for the specified
531: * name, if any; otherwise return <code>null</code>.
532: *
533: * @param name Name of the desired resource environment reference
534: */
535: public ContextResourceEnvRef findResourceEnvRef(String name) {
536:
537: synchronized (resourceEnvRefs) {
538: return ((ContextResourceEnvRef) resourceEnvRefs.get(name));
539: }
540:
541: }
542:
543: /**
544: * Return the set of resource environment reference names for this
545: * web application. If none have been specified, a zero-length
546: * array is returned.
547: */
548: public ContextResourceEnvRef[] findResourceEnvRefs() {
549:
550: synchronized (resourceEnvRefs) {
551: ContextResourceEnvRef results[] = new ContextResourceEnvRef[resourceEnvRefs
552: .size()];
553: return ((ContextResourceEnvRef[]) resourceEnvRefs.values()
554: .toArray(results));
555: }
556:
557: }
558:
559: /**
560: * Return the web service reference for the specified
561: * name, if any; otherwise return <code>null</code>.
562: *
563: * @param name Name of the desired web service
564: */
565: public ContextService findService(String name) {
566:
567: synchronized (services) {
568: return ((ContextService) services.get(name));
569: }
570:
571: }
572:
573: /**
574: * Return the defined web service references for this application. If
575: * none have been defined, a zero-length array is returned.
576: */
577: public ContextService[] findServices() {
578:
579: synchronized (services) {
580: ContextService results[] = new ContextService[services
581: .size()];
582: return ((ContextService[]) services.values().toArray(
583: results));
584: }
585:
586: }
587:
588: /**
589: * Return true if the name specified already exists.
590: */
591: public boolean exists(String name) {
592:
593: return (entries.containsKey(name));
594:
595: }
596:
597: /**
598: * Remove any EJB resource reference with the specified name.
599: *
600: * @param name Name of the EJB resource reference to remove
601: */
602: public void removeEjb(String name) {
603:
604: entries.remove(name);
605:
606: ContextEjb ejb = null;
607: synchronized (ejbs) {
608: ejb = (ContextEjb) ejbs.remove(name);
609: }
610: if (ejb != null) {
611: support.firePropertyChange("ejb", ejb, null);
612: ejb.setNamingResources(null);
613: }
614:
615: }
616:
617: /**
618: * Remove any environment entry with the specified name.
619: *
620: * @param name Name of the environment entry to remove
621: */
622: public void removeEnvironment(String name) {
623:
624: entries.remove(name);
625:
626: ContextEnvironment environment = null;
627: synchronized (envs) {
628: environment = (ContextEnvironment) envs.remove(name);
629: }
630: if (environment != null) {
631: support
632: .firePropertyChange("environment", environment,
633: null);
634: environment.setNamingResources(null);
635: }
636:
637: }
638:
639: /**
640: * Remove any local EJB resource reference with the specified name.
641: *
642: * @param name Name of the EJB resource reference to remove
643: */
644: public void removeLocalEjb(String name) {
645:
646: entries.remove(name);
647:
648: ContextLocalEjb localEjb = null;
649: synchronized (localEjbs) {
650: localEjb = (ContextLocalEjb) ejbs.remove(name);
651: }
652: if (localEjb != null) {
653: support.firePropertyChange("localEjb", localEjb, null);
654: localEjb.setNamingResources(null);
655: }
656:
657: }
658:
659: /**
660: * Remove any message destination reference with the specified name.
661: *
662: * @param name Name of the message destination resource reference to remove
663: */
664: public void removeMessageDestinationRef(String name) {
665:
666: entries.remove(name);
667:
668: MessageDestinationRef mdr = null;
669: synchronized (mdrs) {
670: mdr = (MessageDestinationRef) mdrs.remove(name);
671: }
672: if (mdr != null) {
673: support.firePropertyChange("messageDestinationRef", mdr,
674: null);
675: mdr.setNamingResources(null);
676: }
677:
678: }
679:
680: /**
681: * Remove a property change listener from this component.
682: *
683: * @param listener The listener to remove
684: */
685: public void removePropertyChangeListener(
686: PropertyChangeListener listener) {
687:
688: support.removePropertyChangeListener(listener);
689:
690: }
691:
692: /**
693: * Remove any resource reference with the specified name.
694: *
695: * @param name Name of the resource reference to remove
696: */
697: public void removeResource(String name) {
698:
699: entries.remove(name);
700:
701: ContextResource resource = null;
702: synchronized (resources) {
703: resource = (ContextResource) resources.remove(name);
704: }
705: if (resource != null) {
706: support.firePropertyChange("resource", resource, null);
707: resource.setNamingResources(null);
708: }
709:
710: }
711:
712: /**
713: * Remove any resource environment reference with the specified name.
714: *
715: * @param name Name of the resource environment reference to remove
716: */
717: public void removeResourceEnvRef(String name) {
718:
719: entries.remove(name);
720:
721: String type = null;
722: synchronized (resourceEnvRefs) {
723: type = (String) resourceEnvRefs.remove(name);
724: }
725: if (type != null) {
726: support.firePropertyChange("resourceEnvRef", name + ":"
727: + type, null);
728: }
729:
730: }
731:
732: /**
733: * Remove any resource link with the specified name.
734: *
735: * @param name Name of the resource link to remove
736: */
737: public void removeResourceLink(String name) {
738:
739: entries.remove(name);
740:
741: ContextResourceLink resourceLink = null;
742: synchronized (resourceLinks) {
743: resourceLink = (ContextResourceLink) resourceLinks
744: .remove(name);
745: }
746: if (resourceLink != null) {
747: support.firePropertyChange("resourceLink", resourceLink,
748: null);
749: resourceLink.setNamingResources(null);
750: }
751:
752: }
753:
754: /**
755: * Remove any web service reference with the specified name.
756: *
757: * @param name Name of the web service reference to remove
758: */
759: public void removeService(String name) {
760:
761: entries.remove(name);
762:
763: ContextService service = null;
764: synchronized (services) {
765: service = (ContextService) services.remove(name);
766: }
767: if (service != null) {
768: support.firePropertyChange("service", service, null);
769: service.setNamingResources(null);
770: }
771:
772: }
773:
774: }
|