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