1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55:
56:
66: public class BeanContextServicesSupport
67: extends BeanContextSupport
68: implements BeanContextServices
69: {
70: private static final long serialVersionUID = -8494482757288719206L;
71:
72: protected class BCSSChild
73: extends BeanContextSupport.BCSChild
74: {
75: private static final long serialVersionUID = -3263851306889194873L;
76:
77: BCSSChild(Object targetChild, Object peer)
78: {
79: super(targetChild, peer);
80: }
81: }
82:
83: protected class BCSSProxyServiceProvider
84: implements BeanContextServiceProvider,
85: BeanContextServiceRevokedListener
86: {
87: private static final long serialVersionUID = 7078212910685744490L;
88:
89: private BeanContextServiceProvider provider;
90:
91: private BCSSProxyServiceProvider(BeanContextServiceProvider p)
92: {
93: provider = p;
94: }
95:
96: public Iterator getCurrentServiceSelectors (BeanContextServices bcs,
97: Class serviceClass)
98: {
99: return provider.getCurrentServiceSelectors(bcs, serviceClass);
100: }
101:
102: public Object getService (BeanContextServices bcs,
103: Object requestor,
104: Class serviceClass,
105: Object serviceSelector)
106: {
107: return provider.getService(bcs, requestor, serviceClass,
108: serviceSelector);
109: }
110:
111: public void releaseService (BeanContextServices bcs,
112: Object requestor,
113: Object service)
114: {
115: provider.releaseService(bcs, requestor, service);
116: }
117:
118: public void serviceRevoked (BeanContextServiceRevokedEvent bcsre)
119: {
120: if (provider instanceof BeanContextServiceRevokedListener)
121: ((BeanContextServiceRevokedListener) provider).serviceRevoked(bcsre);
122: }
123: }
124:
125: protected static class BCSSServiceProvider
126: implements Serializable
127: {
128: private static final long serialVersionUID = 861278251667444782L;
129:
130: protected BeanContextServiceProvider serviceProvider;
131:
132: private Class serviceClass;
133:
134: private BCSSServiceProvider(Class serviceClass,
135: BeanContextServiceProvider provider)
136: {
137: this.serviceClass = serviceClass;
138: serviceProvider = provider;
139: }
140:
141: protected BeanContextServiceProvider getServiceProvider()
142: {
143: return serviceProvider;
144: }
145:
146: private Class getServiceClass()
147: {
148: return serviceClass;
149: }
150:
151: }
152:
153:
160: private static abstract class Request
161: {
162: private Object requestor;
163:
164: public Request(Object requestor)
165: {
166: this.requestor = requestor;
167: }
168:
169: public boolean equals(Object obj)
170: {
171: if (obj instanceof Request)
172: {
173: Request req = (Request) obj;
174: return req.getRequestor().equals(requestor);
175: }
176: return false;
177: }
178:
179: public Object getRequestor()
180: {
181: return requestor;
182: }
183:
184: }
185:
186:
192: private static class ServiceRequest
193: extends Request
194: {
195:
196: private BeanContextServiceRevokedListener listener;
197:
198: public ServiceRequest(Object requestor,
199: BeanContextServiceRevokedListener listener)
200: {
201: super(requestor);
202: this.listener = listener;
203: }
204:
205: public boolean equals(Object obj)
206: {
207: if (obj instanceof ServiceRequest)
208: {
209: ServiceRequest sr = (ServiceRequest) obj;
210: return (super.equals(obj) &&
211: sr.getListener().equals(listener));
212: }
213: return false;
214: }
215:
216: public BeanContextServiceRevokedListener getListener()
217: {
218: return listener;
219: }
220: }
221:
222:
228: private static class ServiceLease
229: extends Request
230: {
231:
232: private Object service;
233:
234: public ServiceLease(Object requestor, Object service)
235: {
236: super(requestor);
237: this.service = service;
238: }
239:
240: public boolean equals(Object obj)
241: {
242: if (obj instanceof ServiceLease)
243: {
244: ServiceLease sl = (ServiceLease) obj;
245: return (super.equals(obj) &&
246: sl.getService().equals(service));
247: }
248: return false;
249: }
250:
251: public Object getService()
252: {
253: return service;
254: }
255: }
256:
257:
261: protected transient ArrayList bcsListeners;
262:
263: protected transient BCSSProxyServiceProvider proxy;
264:
265:
268: protected transient int serializable;
269:
270:
274: protected transient HashMap services;
275:
276:
280: private transient HashMap serviceUsers;
281:
282:
285: private transient HashMap serviceRequests;
286:
287:
290: private transient HashMap serviceLeases;
291:
292:
295: public BeanContextServicesSupport ()
296: {
297: super();
298: }
299:
300:
305: public BeanContextServicesSupport (BeanContextServices peer)
306: {
307: super(peer);
308: }
309:
310:
317: public BeanContextServicesSupport(BeanContextServices peer, Locale locale)
318: {
319: super(peer, locale);
320: }
321:
322:
331: public BeanContextServicesSupport(BeanContextServices peer, Locale locale,
332: boolean dtime)
333: {
334: super(peer, locale, dtime);
335: }
336:
337:
347: public BeanContextServicesSupport(BeanContextServices peer, Locale locale,
348: boolean dtime, boolean visible)
349: {
350: super(peer, locale, dtime, visible);
351: }
352:
353:
359: public void addBeanContextServicesListener
360: (BeanContextServicesListener listener)
361: {
362: synchronized (bcsListeners)
363: {
364: if (! bcsListeners.contains(listener))
365: bcsListeners.add(listener);
366: }
367: }
368:
369:
382: public boolean addService (Class serviceClass,
383: BeanContextServiceProvider bcsp)
384: {
385: return addService(serviceClass, bcsp, true);
386: }
387:
388:
402: protected boolean addService (Class serviceClass,
403: BeanContextServiceProvider bcsp,
404: boolean fireEvent)
405: {
406: synchronized (globalHierarchyLock)
407: {
408: synchronized (services)
409: {
410: if (services.containsKey(serviceClass))
411: return false;
412: services.put(serviceClass,
413: createBCSSServiceProvider(serviceClass, bcsp));
414: if (bcsp instanceof Serializable)
415: ++serializable;
416: if (fireEvent)
417: fireServiceAdded(serviceClass);
418: return true;
419: }
420: }
421: }
422:
423:
435: protected void bcsPreDeserializationHook (ObjectInputStream ois)
436: throws ClassNotFoundException, IOException
437: {
438: serializable = ois.readInt();
439: for (int a = 0; a < serializable; ++a)
440: {
441: BCSSServiceProvider bcsssp = (BCSSServiceProvider) ois.readObject();
442: addService(bcsssp.getServiceClass(), bcsssp.getServiceProvider());
443: }
444: }
445:
446:
456: protected void bcsPreSerializationHook (ObjectOutputStream oos)
457: throws IOException
458: {
459: oos.writeInt(serializable);
460: synchronized (services)
461: {
462: Iterator i = services.values().iterator();
463: while (i.hasNext())
464: {
465: BCSSServiceProvider bcsssp = (BCSSServiceProvider) i.next();
466: if (bcsssp.getServiceProvider() instanceof Serializable)
467: oos.writeObject(bcsssp);
468: }
469: }
470: }
471:
472:
482: protected void childJustRemovedHook (Object child,
483: BeanContextSupport.BCSChild bcsc)
484: {
485: if (child instanceof BeanContextChild)
486: {
487: BeanContextChild bcchild = (BeanContextChild) child;
488: Iterator childServices = ((List) serviceUsers.get(bcchild)).iterator();
489: while (childServices.hasNext())
490: releaseService(bcchild, this, childServices.next());
491: serviceUsers.remove(bcchild);
492: }
493: }
494:
495:
503: protected BeanContextSupport.BCSChild createBCSChild (Object targetChild,
504: Object peer)
505: {
506: return new BCSSChild(targetChild, peer);
507: }
508:
509:
519: protected BeanContextServicesSupport.BCSSServiceProvider
520: createBCSSServiceProvider (Class sc, BeanContextServiceProvider bcsp)
521: {
522: return new BCSSServiceProvider(sc, bcsp);
523: }
524:
525:
531: protected final void fireServiceAdded (BeanContextServiceAvailableEvent bcssae)
532: {
533: synchronized (bcsListeners)
534: {
535: int size = bcsListeners.size();
536: for (int i = 0; i < size; ++i)
537: {
538: BeanContextServicesListener bcsl
539: = (BeanContextServicesListener) bcsListeners.get(i);
540: bcsl.serviceAvailable(bcssae);
541: }
542: }
543: }
544:
545:
552: protected final void fireServiceAdded (Class serviceClass)
553: {
554: fireServiceAdded(new BeanContextServiceAvailableEvent(this,
555: serviceClass));
556: }
557:
558:
564: protected final void fireServiceRevoked(BeanContextServiceRevokedEvent event)
565: {
566: synchronized (bcsListeners)
567: {
568: int size = bcsListeners.size();
569: for (int i = 0; i < size; ++i)
570: {
571: BeanContextServicesListener bcsl
572: = (BeanContextServicesListener) bcsListeners.get(i);
573: bcsl.serviceRevoked(event);
574: }
575: List requests = (List) serviceRequests.get(event.getServiceClass());
576: if (requests != null)
577: {
578: Iterator i = requests.iterator();
579: while (i.hasNext())
580: {
581: ServiceRequest r = (ServiceRequest) i.next();
582: r.getListener().serviceRevoked(event);
583: }
584: }
585: }
586: }
587:
588:
595: protected final void fireServiceRevoked (Class serviceClass,
596: boolean revokeNow)
597: {
598: fireServiceRevoked(new BeanContextServiceRevokedEvent(this, serviceClass,
599: revokeNow));
600: }
601:
602:
608: public BeanContextServices getBeanContextServicesPeer ()
609: {
610: return (BeanContextServices) beanContextChildPeer;
611: }
612:
613:
622: protected static final BeanContextServicesListener
623: getChildBeanContextServicesListener(Object child)
624: {
625: if (child instanceof BeanContextServicesListener)
626: return (BeanContextServicesListener) child;
627: else
628: return null;
629: }
630:
631:
637: public Iterator getCurrentServiceClasses ()
638: {
639: synchronized (globalHierarchyLock)
640: {
641: synchronized (services)
642: {
643: return services.keySet().iterator();
644: }
645: }
646: }
647:
648:
661: public Iterator getCurrentServiceSelectors (Class serviceClass)
662: {
663: synchronized (globalHierarchyLock)
664: {
665: synchronized (services)
666: {
667: BeanContextServiceProvider bcsp
668: = ((BCSSServiceProvider)
669: services.get(serviceClass)).getServiceProvider();
670: if (bcsp == null)
671: return null;
672: else
673: return bcsp.getCurrentServiceSelectors(this, serviceClass);
674: }
675: }
676: }
677:
678:
697: public Object getService (BeanContextChild child, Object requestor,
698: Class serviceClass, Object serviceSelector,
699: BeanContextServiceRevokedListener bcsrl)
700: throws TooManyListenersException
701: {
702: synchronized (globalHierarchyLock)
703: {
704: synchronized (services)
705: {
706: Object service;
707: BeanContextServiceProvider provider = ((BCSSServiceProvider)
708: services.get(serviceClass)).getServiceProvider();
709: if (provider != null)
710: {
711: service = provider.getService(this, requestor, serviceClass,
712: serviceSelector);
713: List childServices = (List) serviceUsers.get(child);
714: if (childServices == null)
715: {
716: childServices = new ArrayList();
717: serviceUsers.put(child, childServices);
718: }
719: childServices.add(serviceClass);
720: }
721: else
722: {
723: BeanContextServices peer = getBeanContextServicesPeer();
724: if (peer != null)
725: service = peer.getService(child, requestor, serviceClass,
726: serviceSelector, bcsrl);
727: else
728: service = null;
729: }
730: if (service != null)
731: {
732: ServiceRequest request = new ServiceRequest(requestor, bcsrl);
733: Set requests = (Set) serviceRequests.get(serviceClass);
734: if (requests == null)
735: {
736: requests = new HashSet();
737: serviceRequests.put(serviceClass, requests);
738: }
739: requests.add(request);
740: ServiceLease lease = new ServiceLease(requestor, service);
741: serviceLeases.put(lease, provider);
742: }
743: return service;
744: }
745: }
746: }
747:
748:
754: public boolean hasService (Class serviceClass)
755: {
756: synchronized (globalHierarchyLock)
757: {
758: synchronized (services)
759: {
760: return services.containsKey(serviceClass);
761: }
762: }
763: }
764:
765: public void initialize ()
766: {
767: super.initialize();
768:
769: bcsListeners = new ArrayList();
770: services = new HashMap();
771: serviceUsers = new HashMap();
772: serviceRequests = new HashMap();
773: serviceLeases = new HashMap();
774: }
775:
776:
780: protected void initializeBeanContextResources()
781: {
782:
783: }
784:
785:
791: protected void releaseBeanContextResources()
792: {
793:
794: }
795:
796:
806: public void releaseService (BeanContextChild child, Object requestor,
807: Object service)
808: {
809: synchronized (globalHierarchyLock)
810: {
811: synchronized (services)
812: {
813: ServiceLease lease = new ServiceLease(requestor, service);
814: BeanContextServiceProvider provider = (BeanContextServiceProvider)
815: serviceLeases.get(lease);
816: if (provider != null)
817: provider.releaseService(this, requestor, service);
818: else
819: {
820: BeanContextServices peer = getBeanContextServicesPeer();
821: if (peer != null)
822: peer.releaseService(child, requestor, service);
823: }
824: serviceLeases.remove(lease);
825: }
826: }
827: }
828:
829: public void removeBeanContextServicesListener
830: (BeanContextServicesListener listener)
831: {
832: synchronized (bcsListeners)
833: {
834: bcsListeners.remove(listener);
835: }
836: }
837:
838:
850: public void revokeService (Class serviceClass, BeanContextServiceProvider bcsp,
851: boolean revokeCurrentServicesNow)
852: {
853: synchronized (globalHierarchyLock)
854: {
855: synchronized (services)
856: {
857: fireServiceRevoked(serviceClass, revokeCurrentServicesNow);
858: services.remove(serviceClass);
859: if (bcsp instanceof Serializable)
860: --serializable;
861: }
862: }
863: }
864:
865: public void serviceAvailable (BeanContextServiceAvailableEvent bcssae)
866: {
867: synchronized (services)
868: {
869: Class klass = bcssae.getServiceClass();
870: if (services.containsKey(klass))
871: return;
872: Iterator it = bcsChildren();
873: while (it.hasNext())
874: {
875: Object obj = it.next();
876: if (obj instanceof BeanContextServices)
877: ((BeanContextServices) obj).serviceAvailable(bcssae);
878: }
879: }
880: }
881:
882: public void serviceRevoked (BeanContextServiceRevokedEvent bcssre)
883: {
884: synchronized (services)
885: {
886: Class klass = bcssre.getServiceClass();
887: if (services.containsKey(klass))
888: return;
889: Iterator it = bcsChildren();
890: while (it.hasNext())
891: {
892: Object obj = it.next();
893: if (obj instanceof BeanContextServices)
894: ((BeanContextServices) obj).serviceRevoked(bcssre);
895: }
896: }
897: }
898: }