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: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61:
62:
71: public class BeanContextSupport extends BeanContextChildSupport
72: implements BeanContext, Serializable, PropertyChangeListener,
73: VetoableChangeListener
74: {
75: private static final long serialVersionUID = -4879613978649577204L;
76:
77:
78:
79: private void readObject (ObjectInputStream s)
80: throws ClassNotFoundException, IOException, NotImplementedException
81: {
82: throw new Error ("Not implemented");
83: }
84:
85:
86:
87: private void writeObject (ObjectOutputStream s)
88: throws ClassNotFoundException, IOException, NotImplementedException
89: {
90: throw new Error ("Not implemented");
91: }
92:
93: protected class BCSChild implements Serializable
94: {
95: private static final long serialVersionUID = -5815286101609939109L;
96:
97: private Object targetChild;
98: private Object peer;
99:
100: BCSChild(Object targetChild, Object peer)
101: {
102: this.targetChild = targetChild;
103: this.peer = peer;
104: }
105: }
106:
107: protected static final class BCSIterator implements Iterator
108: {
109: private Iterator child;
110:
111: BCSIterator(Iterator child)
112: {
113: this.child = child;
114: }
115:
116: public boolean hasNext ()
117: {
118: return child.hasNext();
119: }
120:
121: public Object next ()
122: {
123: return child.next();
124: }
125:
126: public void remove ()
127: {
128:
129: }
130: }
131:
132: protected transient ArrayList bcmListeners;
133:
134: protected transient HashMap children;
135:
136: protected transient boolean designTime;
137:
138: protected transient Locale locale;
139:
140: protected transient boolean okToUseGui;
141:
142:
145: public BeanContextSupport ()
146: {
147: this (null, null, true, true);
148: }
149:
150:
153: public BeanContextSupport (BeanContext peer)
154: {
155: this (peer, null, true, true);
156: }
157:
158:
161: public BeanContextSupport (BeanContext peer, Locale lcle)
162: {
163: this (peer, lcle, true, true);
164: }
165:
166:
169: public BeanContextSupport (BeanContext peer, Locale lcle, boolean dtime)
170: {
171: this (peer, lcle, dtime, true);
172: }
173:
174:
177: public BeanContextSupport (BeanContext peer, Locale lcle, boolean dtime,
178: boolean visible)
179: {
180: super(peer);
181:
182: locale = lcle == null ? Locale.getDefault() : lcle;
183: designTime = dtime;
184: okToUseGui = visible;
185:
186: initialize ();
187: }
188:
189:
231: public boolean add(Object targetChild)
232: {
233: synchronized (globalHierarchyLock)
234: {
235: if (targetChild == null)
236: throw new IllegalArgumentException();
237:
238: BCSChild child;
239: synchronized (children)
240: {
241: if (children.containsKey(targetChild)
242: || ! validatePendingAdd(targetChild))
243: return false;
244: child = createBCSChild(targetChild, beanContextChildPeer);
245: children.put(targetChild, child);
246: }
247: synchronized (targetChild)
248: {
249: BeanContextChild bcChild = null;
250: if (targetChild instanceof BeanContextChild)
251: bcChild = (BeanContextChild) targetChild;
252: if (targetChild instanceof BeanContextProxy)
253: bcChild = ((BeanContextProxy) targetChild).getBeanContextProxy();
254: if (bcChild != null)
255: try
256: {
257: bcChild.setBeanContext(this);
258: bcChild.addVetoableChangeListener("beanContext", this);
259: bcChild.addPropertyChangeListener("beanContext", this);
260: }
261: catch (PropertyVetoException e)
262: {
263: synchronized (children)
264: {
265: children.remove(targetChild);
266: }
267: throw new IllegalStateException("The child refused to " +
268: "associate itself with " +
269: "this context.", e);
270: }
271: if (targetChild instanceof Visibility)
272: {
273: Visibility visibleChild = (Visibility) targetChild;
274: if (okToUseGui)
275: visibleChild.okToUseGui();
276: else
277: visibleChild.dontUseGui();
278: }
279: childJustAddedHook(targetChild, child);
280: }
281: fireChildrenAdded(new BeanContextMembershipEvent(this,
282: new Object[]{ targetChild }));
283: return true;
284: }
285: }
286:
287: public boolean addAll (Collection c)
288: {
289:
290: throw new UnsupportedOperationException();
291: }
292:
293: public void addBeanContextMembershipListener
294: (BeanContextMembershipListener listener)
295: {
296: synchronized (bcmListeners)
297: {
298: if (! bcmListeners.contains(listener))
299: bcmListeners.add(listener);
300: }
301: }
302:
303:
311: public boolean avoidingGui()
312: throws NotImplementedException
313: {
314: return needsGui() && (!okToUseGui);
315: }
316:
317: protected Iterator bcsChildren ()
318: {
319: synchronized (children)
320: {
321: return new BCSIterator(children.values().iterator());
322: }
323: }
324:
325: protected void bcsPreDeserializationHook (ObjectInputStream ois)
326: throws ClassNotFoundException, IOException, NotImplementedException
327: {
328: throw new Error ("Not implemented");
329: }
330:
331: protected void bcsPreSerializationHook (ObjectOutputStream oos)
332: throws IOException, NotImplementedException
333: {
334: throw new Error ("Not implemented");
335: }
336:
337: protected void childDeserializedHook (Object child, BeanContextSupport.BCSChild bcsc)
338: throws NotImplementedException
339: {
340: throw new Error ("Not implemented");
341: }
342:
343: protected void childJustAddedHook (Object child, BeanContextSupport.BCSChild bcsc)
344: {
345:
346: }
347:
348: protected void childJustRemovedHook (Object child, BeanContextSupport.BCSChild bcsc)
349: {
350:
351: }
352:
353: protected static final boolean classEquals (Class first, Class second)
354: {
355:
356: return (first == second || first.getName().equals(second.getName()));
357: }
358:
359: public void clear ()
360: {
361:
362:
363: throw new UnsupportedOperationException();
364: }
365:
366: public boolean contains (Object o)
367: {
368: synchronized (children)
369: {
370: return children.containsKey(o);
371: }
372: }
373:
374: public boolean containsAll (Collection c)
375: {
376: synchronized (children)
377: {
378: Iterator it = c.iterator();
379: while (it.hasNext())
380: if (! children.containsKey(it.next()))
381: return false;
382: }
383: return true;
384: }
385:
386: public boolean containsKey (Object o)
387: {
388: synchronized (children)
389: {
390: return children.containsKey(o);
391: }
392: }
393:
394: protected final Object[] copyChildren ()
395: {
396: synchronized (children)
397: {
398: return children.keySet().toArray();
399: }
400: }
401:
402: protected BeanContextSupport.BCSChild createBCSChild (Object targetChild, Object peer)
403: {
404: return new BCSChild(targetChild, peer);
405: }
406:
407: protected final void deserialize (ObjectInputStream ois, Collection coll)
408: throws ClassNotFoundException, IOException, NotImplementedException
409: {
410: throw new Error ("Not implemented");
411: }
412:
413:
417: public void dontUseGui()
418: {
419: okToUseGui = false;
420: }
421:
422: protected final void fireChildrenAdded (BeanContextMembershipEvent bcme)
423: {
424: synchronized (bcmListeners)
425: {
426: Iterator it = bcmListeners.iterator();
427: while (it.hasNext())
428: {
429: BeanContextMembershipListener l
430: = (BeanContextMembershipListener) it.next();
431: l.childrenAdded(bcme);
432: }
433: }
434: }
435:
436: protected final void fireChildrenRemoved (BeanContextMembershipEvent bcme)
437: {
438: synchronized (bcmListeners)
439: {
440: Iterator it = bcmListeners.iterator();
441: while (it.hasNext())
442: {
443: BeanContextMembershipListener l
444: = (BeanContextMembershipListener) it.next();
445: l.childrenRemoved(bcme);
446: }
447: }
448: }
449:
450: public BeanContext getBeanContextPeer ()
451: throws NotImplementedException
452: {
453: throw new Error ("Not implemented");
454: }
455:
456: protected static final BeanContextChild getChildBeanContextChild (Object child)
457: throws NotImplementedException
458: {
459: throw new Error ("Not implemented");
460: }
461:
462: protected static final BeanContextMembershipListener getChildBeanContextMembershipListener (Object child)
463: throws NotImplementedException
464: {
465: throw new Error ("Not implemented");
466: }
467:
468: protected static final PropertyChangeListener getChildPropertyChangeListener (Object child)
469: throws NotImplementedException
470: {
471: throw new Error ("Not implemented");
472: }
473:
474: protected static final Serializable getChildSerializable (Object child)
475: throws NotImplementedException
476: {
477: throw new Error ("Not implemented");
478: }
479:
480: protected static final VetoableChangeListener getChildVetoableChangeListener (Object child)
481: throws NotImplementedException
482: {
483: throw new Error ("Not implemented");
484: }
485:
486: protected static final Visibility getChildVisibility (Object child)
487: throws NotImplementedException
488: {
489: throw new Error ("Not implemented");
490: }
491:
492: public Locale getLocale ()
493: {
494: return locale;
495: }
496:
497: public URL getResource (String name, BeanContextChild bcc)
498: {
499: if (! contains(bcc))
500: throw new IllegalArgumentException("argument not a child");
501: ClassLoader loader = bcc.getClass().getClassLoader();
502: return (loader == null ? ClassLoader.getSystemResource(name)
503: : loader.getResource(name));
504: }
505:
506: public InputStream getResourceAsStream (String name, BeanContextChild bcc)
507: {
508: if (! contains(bcc))
509: throw new IllegalArgumentException("argument not a child");
510: ClassLoader loader = bcc.getClass().getClassLoader();
511: return (loader == null ? ClassLoader.getSystemResourceAsStream(name)
512: : loader.getResourceAsStream(name));
513: }
514:
515: protected void initialize ()
516: {
517: bcmListeners = new ArrayList();
518: children = new HashMap();
519: }
520:
521:
531: public Object instantiateChild (String beanName)
532: throws IOException, ClassNotFoundException
533: {
534: return Beans.instantiate(getClass().getClassLoader(), beanName, this);
535: }
536:
537: public boolean isDesignTime ()
538: {
539: return designTime;
540: }
541:
542:
547: public boolean isEmpty ()
548: {
549: synchronized (children)
550: {
551: return children.isEmpty();
552: }
553: }
554:
555: public boolean isSerializing ()
556: throws NotImplementedException
557: {
558: throw new Error ("Not implemented");
559: }
560:
561: public Iterator iterator ()
562: {
563: synchronized (children)
564: {
565: return children.keySet().iterator();
566: }
567: }
568:
569:
575: public boolean needsGui()
576: {
577: return false;
578: }
579:
580:
584: public void okToUseGui ()
585: {
586: okToUseGui = true;
587: }
588:
589:
597: public void propertyChange (PropertyChangeEvent pce)
598: {
599: if (pce.getNewValue() != this)
600: remove(pce.getSource(), false);
601: }
602:
603: public final void readChildren (ObjectInputStream ois)
604: throws IOException, ClassNotFoundException, NotImplementedException
605: {
606: throw new Error ("Not implemented");
607: }
608:
609:
617: public boolean remove (Object targetChild)
618: {
619: return remove(targetChild, true);
620: }
621:
622:
657: protected boolean remove (Object targetChild, boolean callChildSetBC)
658: {
659: synchronized (globalHierarchyLock)
660: {
661: if (targetChild == null)
662: throw new IllegalArgumentException();
663:
664: BCSChild child;
665: synchronized (children)
666: {
667: if (!children.containsKey(targetChild)
668: || !validatePendingRemove(targetChild))
669: return false;
670: child = (BCSChild) children.remove(targetChild);
671: }
672: synchronized (targetChild)
673: {
674: BeanContextChild bcChild = null;
675: if (targetChild instanceof BeanContextChild)
676: bcChild = (BeanContextChild) targetChild;
677: if (targetChild instanceof BeanContextProxy)
678: bcChild = ((BeanContextProxy) targetChild).getBeanContextProxy();
679: if (bcChild != null)
680: try
681: {
682: if (callChildSetBC)
683: bcChild.setBeanContext(null);
684: bcChild.removeVetoableChangeListener("beanContext", this);
685: bcChild.removePropertyChangeListener("beanContext", this);
686: }
687: catch (PropertyVetoException e)
688: {
689: synchronized (children)
690: {
691: children.put(targetChild, child);
692: }
693: throw new IllegalStateException("The child refused to " +
694: "disassociate itself with " +
695: "this context.", e);
696: }
697: childJustRemovedHook(targetChild, child);
698: }
699: fireChildrenRemoved(new BeanContextMembershipEvent(this,
700: new Object[]{ targetChild }));
701: return true;
702: }
703: }
704:
705: public boolean removeAll (Collection c)
706: {
707:
708: throw new UnsupportedOperationException();
709: }
710:
711: public void removeBeanContextMembershipListener (BeanContextMembershipListener bcml)
712: {
713: synchronized (bcmListeners)
714: {
715: bcmListeners.remove(bcml);
716: }
717: }
718:
719: public boolean retainAll (Collection c)
720: {
721:
722: throw new UnsupportedOperationException();
723: }
724:
725: protected final void serialize (ObjectOutputStream oos, Collection coll)
726: throws IOException, NotImplementedException
727: {
728: throw new Error ("Not implemented");
729: }
730:
731: public void setDesignTime (boolean dtime)
732: {
733: boolean save = designTime;
734: designTime = dtime;
735: firePropertyChange(DesignMode.PROPERTYNAME, Boolean.valueOf(save),
736: Boolean.valueOf(dtime));
737: }
738:
739: public void setLocale (Locale newLocale)
740: throws PropertyVetoException
741: {
742: if (newLocale == null || locale == newLocale)
743: return;
744: fireVetoableChange("locale", locale, newLocale);
745: Locale oldLocale = locale;
746: locale = newLocale;
747: firePropertyChange("locale", oldLocale, newLocale);
748: }
749:
750: public int size ()
751: {
752: synchronized (children)
753: {
754: return children.size();
755: }
756: }
757:
758: public Object[] toArray ()
759: {
760: synchronized (children)
761: {
762: return children.keySet().toArray();
763: }
764: }
765:
766: public Object[] toArray(Object[] array)
767: throws NotImplementedException
768: {
769:
770: synchronized (children)
771: {
772: return children.keySet().toArray(array);
773: }
774: }
775:
776: protected boolean validatePendingAdd (Object targetChild)
777: {
778: return true;
779: }
780:
781: protected boolean validatePendingRemove (Object targetChild)
782: {
783: return true;
784: }
785:
786:
792: public void vetoableChange (PropertyChangeEvent pce)
793: throws PropertyVetoException
794: {
795:
796: }
797:
798: public final void writeChildren (ObjectOutputStream oos)
799: throws IOException, NotImplementedException
800: {
801: throw new Error ("Not implemented");
802: }
803: }