Source for java.beans.beancontext.BeanContextSupport

   1: /* BeanContextSupport.java --
   2:    Copyright (C) 2003, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.beans.beancontext;
  40: 
  41: import gnu.classpath.NotImplementedException;
  42: 
  43: import java.beans.DesignMode;
  44: import java.beans.PropertyChangeEvent;
  45: import java.beans.PropertyChangeListener;
  46: import java.beans.PropertyVetoException;
  47: import java.beans.VetoableChangeListener;
  48: import java.beans.Visibility;
  49: import java.io.IOException;
  50: import java.io.InputStream;
  51: import java.io.ObjectInputStream;
  52: import java.io.ObjectOutputStream;
  53: import java.io.Serializable;
  54: import java.net.URL;
  55: import java.util.ArrayList;
  56: import java.util.Collection;
  57: import java.util.HashMap;
  58: import java.util.Iterator;
  59: import java.util.Locale;
  60: 
  61: /**
  62:  * @author Michael Koch
  63:  * @since 1.2
  64:  */
  65: public class BeanContextSupport extends BeanContextChildSupport
  66:   implements BeanContext, Serializable, PropertyChangeListener,
  67:   VetoableChangeListener
  68: {
  69:   private static final long serialVersionUID = -4879613978649577204L;
  70: 
  71:   // This won't show up in japi, but we mark it as a stub anyway,
  72:   // so that searches for NotImplementedException will find it.
  73:   private void readObject (ObjectInputStream s)
  74:     throws ClassNotFoundException, IOException, NotImplementedException
  75:   {
  76:     throw new Error ("Not implemented");
  77:   }
  78: 
  79:   // This won't show up in japi, but we mark it as a stub anyway,
  80:   // so that searches for NotImplementedException will find it.
  81:   private void writeObject (ObjectOutputStream s)
  82:     throws ClassNotFoundException, IOException, NotImplementedException
  83:   {
  84:     throw new Error ("Not implemented");
  85:   }
  86: 
  87:   protected class BCSChild implements Serializable
  88:   {
  89:     private static final long serialVersionUID = -5815286101609939109L;
  90: 
  91:     private Object targetChild;
  92:     private Object peer;
  93: 
  94:     BCSChild(Object targetChild, Object peer)
  95:     {
  96:       this.targetChild = targetChild;
  97:       this.peer = peer;
  98:     }
  99:   }
 100: 
 101:   protected static final class BCSIterator implements Iterator
 102:   {
 103:     private Iterator child;
 104: 
 105:     BCSIterator(Iterator child)
 106:     {
 107:       this.child = child;
 108:     }
 109: 
 110:     public boolean hasNext ()
 111:     {
 112:       return child.hasNext();
 113:     }
 114: 
 115:     public Object next ()
 116:     {
 117:       return child.next();
 118:     }
 119: 
 120:     public void remove ()
 121:     {
 122:       // This must be a noop remove operation.
 123:     }
 124:   }
 125: 
 126:   protected transient ArrayList bcmListeners;
 127: 
 128:   protected transient HashMap children;
 129: 
 130:   protected transient boolean designTime;
 131: 
 132:   protected transient Locale locale;
 133: 
 134:   protected transient boolean okToUseGui;
 135: 
 136:   /**
 137:    * Construct a BeanContextSupport instance.
 138:    */
 139:   public BeanContextSupport ()
 140:   {
 141:     this (null, null, true, true);
 142:   }
 143: 
 144:   /**
 145:    * Construct a BeanContextSupport instance.
 146:    */
 147:   public BeanContextSupport (BeanContext peer)
 148:   {
 149:     this (peer, null, true, true);
 150:   }
 151: 
 152:   /**
 153:    * Construct a BeanContextSupport instance.
 154:    */
 155:   public BeanContextSupport (BeanContext peer, Locale lcle)
 156:   {
 157:     this (peer, lcle, true, true);
 158:   }
 159: 
 160:   /**
 161:    * Construct a BeanContextSupport instance.
 162:    */
 163:   public BeanContextSupport (BeanContext peer, Locale lcle, boolean dtime)
 164:   {
 165:     this (peer, lcle, dtime, true);
 166:   }
 167: 
 168:   /**
 169:    * Construct a BeanContextSupport instance.
 170:    */
 171:   public BeanContextSupport (BeanContext peer, Locale lcle, boolean dtime,
 172:                              boolean visible)
 173:   {
 174:     super(peer);
 175: 
 176:     locale = lcle == null ? Locale.getDefault() : lcle;
 177:     designTime = dtime;
 178:     okToUseGui = visible;
 179: 
 180:     initialize ();
 181:   }
 182: 
 183:   public boolean add (Object targetChild)
 184:   {
 185:     if (targetChild == null)
 186:       throw new IllegalArgumentException();
 187: 
 188:     BCSChild child;
 189:     synchronized (children)
 190:       {
 191:         if (children.containsKey(targetChild)
 192:             || ! validatePendingAdd(targetChild))
 193:           return false;
 194:         child = createBCSChild(targetChild, beanContextChildPeer);
 195:         children.put(targetChild, child);
 196:       }
 197:     synchronized (targetChild)
 198:       {
 199:         childJustAddedHook(targetChild, child);
 200:       }
 201:     fireChildrenAdded(new BeanContextMembershipEvent(this,
 202:                                                      new Object[] { targetChild }));
 203:     return true;
 204:   }
 205: 
 206:   public boolean addAll (Collection c)
 207:   {
 208:     // Intentionally throws an exception.
 209:     throw new UnsupportedOperationException();
 210:   }
 211: 
 212:   public void addBeanContextMembershipListener
 213:     (BeanContextMembershipListener listener)
 214:   {
 215:     synchronized (bcmListeners)
 216:       {
 217:         if (! bcmListeners.contains(listener))
 218:           bcmListeners.add(listener);
 219:       }
 220:   }
 221: 
 222:   public boolean avoidingGui ()
 223:     throws NotImplementedException
 224:   {
 225:     throw new Error ("Not implemented");
 226:   }
 227: 
 228:   protected Iterator bcsChildren ()
 229:   {
 230:     synchronized (children)
 231:       {
 232:         return new BCSIterator(children.values().iterator());
 233:       }
 234:   }
 235: 
 236:   protected void bcsPreDeserializationHook (ObjectInputStream ois)
 237:     throws ClassNotFoundException, IOException, NotImplementedException
 238:   {
 239:     throw new Error ("Not implemented");
 240:   }
 241: 
 242:   protected void bcsPreSerializationHook (ObjectOutputStream oos)
 243:     throws IOException, NotImplementedException
 244:   {
 245:     throw new Error ("Not implemented");
 246:   }
 247: 
 248:   protected void childDeserializedHook (Object child, BeanContextSupport.BCSChild bcsc)
 249:     throws NotImplementedException
 250:   {
 251:     throw new Error ("Not implemented");
 252:   }
 253: 
 254:   protected void childJustAddedHook (Object child, BeanContextSupport.BCSChild bcsc)
 255:   {
 256:     // Do nothing in the base class.
 257:   }
 258: 
 259:   protected void childJustRemovedHook (Object child, BeanContextSupport.BCSChild bcsc)
 260:   {
 261:     // Do nothing in the base class.
 262:   }
 263: 
 264:   protected static final boolean classEquals (Class first, Class second)
 265:   {
 266:     // Lame function!
 267:     return (first == second || first.getName().equals(second.getName()));
 268:   }
 269: 
 270:   public void clear ()
 271:   {
 272:     // This is the right thing to do.
 273:     // The JDK docs are really bad here.
 274:     throw new UnsupportedOperationException();
 275:   }
 276: 
 277:   public boolean contains (Object o)
 278:   {
 279:     synchronized (children)
 280:       {
 281:         return children.containsKey(o);
 282:       }
 283:   }
 284: 
 285:   public boolean containsAll (Collection c)
 286:   {
 287:     synchronized (children)
 288:       {
 289:         Iterator it = c.iterator();
 290:         while (it.hasNext())
 291:           if (! children.containsKey(it.next()))
 292:             return false;
 293:       }
 294:     return true;
 295:   }
 296: 
 297:   public boolean containsKey (Object o)
 298:   {
 299:     synchronized (children)
 300:       {
 301:         return children.containsKey(o);
 302:       }
 303:   }
 304: 
 305:   protected final Object[] copyChildren ()
 306:   {
 307:     synchronized (children)
 308:       {
 309:         return children.keySet().toArray();
 310:       }
 311:   }
 312: 
 313:   protected BeanContextSupport.BCSChild createBCSChild (Object targetChild, Object peer)
 314:   {
 315:     return new BCSChild(targetChild, peer);
 316:   }
 317: 
 318:   protected final void deserialize (ObjectInputStream ois, Collection coll)
 319:     throws ClassNotFoundException, IOException, NotImplementedException
 320:   {
 321:     throw new Error ("Not implemented");
 322:   }
 323: 
 324:   public void dontUseGui ()
 325:     throws NotImplementedException
 326:   {
 327:     throw new Error ("Not implemented");
 328:   }
 329: 
 330:   protected final void fireChildrenAdded (BeanContextMembershipEvent bcme)
 331:   {
 332:     synchronized (bcmListeners)
 333:       {
 334:         Iterator it = bcmListeners.iterator();
 335:         while (it.hasNext())
 336:           {
 337:             BeanContextMembershipListener l
 338:               = (BeanContextMembershipListener) it.next();
 339:             l.childrenAdded(bcme);
 340:           }
 341:       }
 342:   }
 343: 
 344:   protected final void fireChildrenRemoved (BeanContextMembershipEvent bcme)
 345:   {
 346:     synchronized (bcmListeners)
 347:       {
 348:         Iterator it = bcmListeners.iterator();
 349:         while (it.hasNext())
 350:           {
 351:             BeanContextMembershipListener l
 352:             = (BeanContextMembershipListener) it.next();
 353:             l.childrenRemoved(bcme);
 354:           }
 355:       }
 356:   }
 357: 
 358:   public BeanContext getBeanContextPeer ()
 359:     throws NotImplementedException
 360:   {
 361:     throw new Error ("Not implemented");
 362:   }
 363: 
 364:   protected static final BeanContextChild getChildBeanContextChild (Object child)
 365:     throws NotImplementedException
 366:   {
 367:     throw new Error ("Not implemented");
 368:   }
 369: 
 370:   protected static final BeanContextMembershipListener getChildBeanContextMembershipListener (Object child)
 371:     throws NotImplementedException
 372:   {
 373:     throw new Error ("Not implemented");
 374:   }
 375: 
 376:   protected static final PropertyChangeListener getChildPropertyChangeListener (Object child)
 377:     throws NotImplementedException
 378:   {
 379:     throw new Error ("Not implemented");
 380:   }
 381: 
 382:   protected static final Serializable getChildSerializable (Object child)
 383:     throws NotImplementedException
 384:   {
 385:     throw new Error ("Not implemented");
 386:   }
 387: 
 388:   protected static final VetoableChangeListener getChildVetoableChangeListener (Object child)
 389:     throws NotImplementedException
 390:   {
 391:     throw new Error ("Not implemented");
 392:   }
 393: 
 394:   protected static final Visibility getChildVisibility (Object child)
 395:     throws NotImplementedException
 396:   {
 397:     throw new Error ("Not implemented");
 398:   }
 399: 
 400:   public Locale getLocale ()
 401:   {
 402:     return locale;
 403:   }
 404: 
 405:   public URL getResource (String name, BeanContextChild bcc)
 406:   {
 407:     if (! contains(bcc))
 408:       throw new IllegalArgumentException("argument not a child");
 409:     ClassLoader loader = bcc.getClass().getClassLoader();
 410:     return (loader == null ? ClassLoader.getSystemResource(name)
 411:             : loader.getResource(name));
 412:   }
 413: 
 414:   public InputStream getResourceAsStream (String name, BeanContextChild bcc)
 415:   {
 416:     if (! contains(bcc))
 417:       throw new IllegalArgumentException("argument not a child");
 418:     ClassLoader loader = bcc.getClass().getClassLoader();
 419:     return (loader == null ? ClassLoader.getSystemResourceAsStream(name)
 420:             : loader.getResourceAsStream(name));
 421:   }
 422: 
 423:   protected void initialize ()
 424:   {
 425:     bcmListeners = new ArrayList();
 426:     children = new HashMap();
 427:   }
 428: 
 429:   public Object instantiateChild (String beanName)
 430:     throws IOException, ClassNotFoundException, NotImplementedException
 431:   {
 432:     throw new Error ("Not implemented");
 433:   }
 434: 
 435:   public boolean isDesignTime ()
 436:   {
 437:     return designTime;
 438:   }
 439: 
 440:   public boolean isEmpty ()
 441:   {
 442:     synchronized (children)
 443:       {
 444:         return children.isEmpty();
 445:       }
 446:   }
 447: 
 448:   public boolean isSerializing ()
 449:     throws NotImplementedException
 450:   {
 451:     throw new Error ("Not implemented");
 452:   }
 453: 
 454:   public Iterator iterator ()
 455:   {
 456:     synchronized (children)
 457:       {
 458:         return children.keySet().iterator();
 459:       }
 460:   }
 461: 
 462:   public boolean needsGui ()
 463:     throws NotImplementedException
 464:   {
 465:     throw new Error ("Not implemented");
 466:   }
 467: 
 468:   public void okToUseGui ()
 469:     throws NotImplementedException
 470:   {
 471:     throw new Error ("Not implemented");
 472:   }
 473: 
 474:   public void propertyChange (PropertyChangeEvent pce)
 475:     throws NotImplementedException
 476:   {
 477:     throw new Error ("Not implemented");
 478:   }
 479: 
 480:   public final void readChildren (ObjectInputStream ois)
 481:     throws IOException, ClassNotFoundException, NotImplementedException
 482:   {
 483:     throw new Error ("Not implemented");
 484:   }
 485: 
 486:   public boolean remove (Object targetChild)
 487:   {
 488:     return remove(targetChild, true);
 489:   }
 490: 
 491:   protected boolean remove (Object targetChild, boolean callChildSetBC)
 492:     throws NotImplementedException
 493:   {
 494:     if (targetChild == null)
 495:       throw new IllegalArgumentException();
 496:     
 497:     throw new Error ("Not implemented");
 498:   }
 499: 
 500:   public boolean removeAll (Collection c)
 501:   {
 502:     // Intentionally throws an exception.
 503:     throw new UnsupportedOperationException();
 504:   }
 505: 
 506:   public void removeBeanContextMembershipListener (BeanContextMembershipListener bcml)
 507:   {
 508:     synchronized (bcmListeners)
 509:       {
 510:         bcmListeners.remove(bcml);
 511:       }
 512:   }
 513: 
 514:   public boolean retainAll (Collection c)
 515:   {
 516:     // Intentionally throws an exception.
 517:     throw new UnsupportedOperationException();
 518:   }
 519: 
 520:   protected final void serialize (ObjectOutputStream oos, Collection coll)
 521:     throws IOException, NotImplementedException
 522:   {
 523:     throw new Error ("Not implemented");
 524:   }
 525: 
 526:   public void setDesignTime (boolean dtime)
 527:   {
 528:     boolean save = designTime;
 529:     designTime = dtime;
 530:     firePropertyChange(DesignMode.PROPERTYNAME, Boolean.valueOf(save),
 531:                        Boolean.valueOf(dtime));
 532:   }
 533: 
 534:   public void setLocale (Locale newLocale)
 535:     throws PropertyVetoException
 536:   {
 537:     if (newLocale == null || locale == newLocale)
 538:       return;
 539:     fireVetoableChange("locale", locale, newLocale);
 540:     Locale oldLocale = locale;
 541:     locale = newLocale;
 542:     firePropertyChange("locale", oldLocale, newLocale);
 543:   }
 544: 
 545:   public int size ()
 546:   {
 547:     synchronized (children)
 548:       {
 549:         return children.size();
 550:       }
 551:   }
 552: 
 553:   public Object[] toArray ()
 554:   {
 555:     synchronized (children)
 556:       {
 557:         return children.keySet().toArray();
 558:       }
 559:   }
 560: 
 561:   public Object[] toArray(Object[] array)
 562:     throws NotImplementedException
 563:   {
 564:     // This implementation is incorrect, I think.
 565:     synchronized (children)
 566:       {
 567:         return children.keySet().toArray(array);
 568:       }
 569:   }
 570: 
 571:   protected boolean validatePendingAdd (Object targetChild)
 572:   {
 573:     return true;
 574:   }
 575: 
 576:   protected boolean validatePendingRemove (Object targetChild)
 577:   {
 578:     return true;
 579:   }
 580: 
 581:   public void vetoableChange (PropertyChangeEvent pce)
 582:     throws PropertyVetoException, NotImplementedException
 583:   {
 584:     throw new Error ("Not implemented");
 585:   }
 586: 
 587:   public final void writeChildren (ObjectOutputStream oos)
 588:     throws IOException, NotImplementedException
 589:   {
 590:     throw new Error ("Not implemented");
 591:   }
 592: }