Source for java.beans.beancontext.BeanContextServicesSupport

   1: /* BeanContextServicesSupport.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.io.IOException;
  44: import java.io.ObjectInputStream;
  45: import java.io.ObjectOutputStream;
  46: import java.io.Serializable;
  47: import java.util.ArrayList;
  48: import java.util.HashMap;
  49: import java.util.Iterator;
  50: import java.util.Locale;
  51: import java.util.TooManyListenersException;
  52: 
  53: /**
  54:  * @author Michael Koch
  55:  * @since 1.2
  56:  */
  57: public class BeanContextServicesSupport
  58:   extends BeanContextSupport
  59:   implements BeanContextServices
  60: {
  61:   private static final long serialVersionUID = -8494482757288719206L;
  62:   
  63:   protected class BCSSChild
  64:     extends BeanContextSupport.BCSChild
  65:   {
  66:     private static final long serialVersionUID = -3263851306889194873L;
  67: 
  68:     BCSSChild(Object targetChild, Object peer)
  69:     {
  70:       super(targetChild, peer);
  71:     }
  72:   }
  73: 
  74:   protected class BCSSProxyServiceProvider
  75:     implements BeanContextServiceProvider,
  76:     BeanContextServiceRevokedListener
  77:   {
  78:     private static final long serialVersionUID = 7078212910685744490L;
  79: 
  80:     private BCSSProxyServiceProvider()
  81:     {
  82:     }
  83: 
  84:     public Iterator getCurrentServiceSelectors (BeanContextServices bcs,
  85:                                                 Class serviceClass)
  86:       throws NotImplementedException
  87:     {
  88:       throw new Error ("Not implemented");
  89:     }
  90: 
  91:     public Object getService (BeanContextServices bcs,
  92:                               Object requestor,
  93:                               Class serviceClass,
  94:                               Object serviceSelector)
  95:       throws NotImplementedException
  96:     {
  97:       throw new Error ("Not implemented");
  98:     }
  99: 
 100:     public void releaseService (BeanContextServices bcs,
 101:                                 Object requestor,
 102:                                 Object service)
 103:       throws NotImplementedException
 104:     {
 105:       throw new Error ("Not implemented");
 106:     }
 107: 
 108:     public void serviceRevoked (BeanContextServiceRevokedEvent bcsre)
 109:       throws NotImplementedException
 110:     {
 111:       throw new Error ("Not implemented");
 112:     }
 113:   }
 114: 
 115:   protected static class BCSSServiceProvider
 116:     implements Serializable
 117:   {
 118:     private static final long serialVersionUID = 861278251667444782L;
 119: 
 120:     protected BeanContextServiceProvider serviceProvider;
 121: 
 122:     private BCSSServiceProvider()
 123:     {
 124:     }
 125: 
 126:     protected BeanContextServiceProvider getServiceProvider()
 127:     {
 128:       return serviceProvider;
 129:     }
 130:   }
 131: 
 132:   protected transient ArrayList bcsListeners;
 133: 
 134:   protected transient BCSSProxyServiceProvider proxy;
 135: 
 136:   protected transient int serializable;
 137: 
 138:   protected transient HashMap services;
 139: 
 140:   public BeanContextServicesSupport ()
 141:   {
 142:     super();
 143:   }
 144: 
 145:   public BeanContextServicesSupport (BeanContextServices peer)
 146:   {
 147:     super(peer);
 148:   }
 149: 
 150:   public BeanContextServicesSupport(BeanContextServices peer, Locale locale)
 151:   {
 152:     super(peer, locale);
 153:   }
 154: 
 155:   public BeanContextServicesSupport(BeanContextServices peer, Locale locale,
 156:                                     boolean dtime)
 157:   {
 158:     super(peer, locale, dtime);
 159:   }
 160: 
 161:   public BeanContextServicesSupport(BeanContextServices peer, Locale locale,
 162:                                     boolean dtime, boolean visible)
 163:   {
 164:     super(peer, locale, dtime, visible);
 165:   }
 166: 
 167:   public void addBeanContextServicesListener
 168:     (BeanContextServicesListener listener)
 169:   {
 170:     synchronized (bcsListeners)
 171:       {
 172:         if (! bcsListeners.contains(listener))
 173:           bcsListeners.add(listener);
 174:       }
 175:   }
 176: 
 177:   public boolean addService (Class serviceClass,
 178:                              BeanContextServiceProvider bcsp)
 179:   {
 180:     return addService(serviceClass, bcsp, true);
 181:   }
 182: 
 183:   protected boolean addService (Class serviceClass,
 184:                                 BeanContextServiceProvider bcsp,
 185:                                 boolean fireEvent)
 186:   {
 187:     synchronized (services)
 188:       {
 189:         if (services.containsKey(serviceClass))
 190:           return false;
 191:         services.put(serviceClass, bcsp);
 192:         if (bcsp instanceof Serializable)
 193:           ++serializable;
 194:         fireServiceAdded(serviceClass);
 195:         return true;
 196:       }
 197:   }
 198:   
 199:   protected void bcsPreDeserializationHook (ObjectInputStream ois)
 200:     throws ClassNotFoundException, IOException, NotImplementedException
 201:   {
 202:     throw new Error ("Not implemented");
 203:   }
 204: 
 205:   protected void bcsPreSerializationHook (ObjectOutputStream oos) 
 206:     throws IOException, NotImplementedException
 207:   {
 208:     throw new Error ("Not implemented");
 209:   }
 210:   
 211:   protected void childJustRemovedHook (Object child,
 212:                                        BeanContextSupport.BCSChild bcsc)
 213:     throws NotImplementedException
 214:   {
 215:     throw new Error ("Not implemented");
 216:   }
 217: 
 218:   protected BeanContextSupport.BCSChild createBCSChild (Object targetChild,
 219:                                                         Object peer)
 220:   {
 221:     return new BCSSChild(targetChild, peer);
 222:   }
 223: 
 224:   protected BeanContextServicesSupport.BCSSServiceProvider
 225:   createBCSSServiceProvider (Class sc, BeanContextServiceProvider bcsp)
 226:     throws NotImplementedException
 227:   {
 228:     throw new Error ("Not implemented");
 229:   }
 230: 
 231:   protected final void fireServiceAdded (BeanContextServiceAvailableEvent bcssae)
 232:   {
 233:     synchronized (bcsListeners)
 234:       {
 235:         int size = bcsListeners.size();
 236:         for (int i = 0; i < size; ++i)
 237:           {
 238:             BeanContextServicesListener bcsl
 239:               = (BeanContextServicesListener) bcsListeners.get(i);
 240:             bcsl.serviceAvailable(bcssae);
 241:           }
 242:       }
 243:   }
 244: 
 245:   protected final void fireServiceAdded (Class serviceClass)
 246:   {
 247:     fireServiceAdded(new BeanContextServiceAvailableEvent(this,
 248:                                                           serviceClass));
 249:   }
 250: 
 251:   protected final void fireServiceRevoked(BeanContextServiceRevokedEvent event)
 252:   {
 253:     synchronized (bcsListeners)
 254:       {
 255:         int size = bcsListeners.size();
 256:         for (int i = 0; i < size; ++i)
 257:           {
 258:             BeanContextServicesListener bcsl
 259:               = (BeanContextServicesListener) bcsListeners.get(i);
 260:             bcsl.serviceRevoked(event);
 261:           }
 262:       }
 263:   }
 264: 
 265:   protected final void fireServiceRevoked (Class serviceClass,
 266:                                            boolean revokeNow)
 267:   {
 268:     fireServiceRevoked(new BeanContextServiceRevokedEvent(this, serviceClass,
 269:                                                           revokeNow));
 270:   }
 271: 
 272:   public BeanContextServices getBeanContextServicesPeer ()
 273:     throws NotImplementedException
 274:   {
 275:     throw new Error ("Not implemented");
 276:   }
 277: 
 278:   protected static final BeanContextServicesListener
 279:   getChildBeanContextServicesListener (Object child)
 280:     throws NotImplementedException
 281:   {
 282:     throw new Error ("Not implemented");
 283:   }
 284: 
 285:   public Iterator getCurrentServiceClasses ()
 286:   {
 287:     synchronized (services)
 288:       {
 289:         return services.keySet().iterator();
 290:       }
 291:   }
 292: 
 293:   public Iterator getCurrentServiceSelectors (Class serviceClass)
 294:   {
 295:     synchronized (services)
 296:       {
 297:         // FIXME: what if service does not exist?  Must write a test.
 298:         BeanContextServiceProvider bcsp
 299:           = (BeanContextServiceProvider) services.get(serviceClass);
 300:         return bcsp.getCurrentServiceSelectors(this, serviceClass);
 301:       }
 302:   }
 303: 
 304:   public Object getService (BeanContextChild child, Object requestor,
 305:                             Class serviceClass, Object serviceSelector,
 306:                             BeanContextServiceRevokedListener bcsrl)
 307:     throws TooManyListenersException, NotImplementedException
 308:   {
 309:     throw new Error ("Not implemented");
 310:   }
 311: 
 312:   public boolean hasService (Class serviceClass)
 313:   {
 314:     synchronized (services)
 315:       {
 316:         return services.containsKey(serviceClass);
 317:       }
 318:   }
 319: 
 320:   public void initialize ()
 321:   {
 322:     super.initialize();
 323: 
 324:     bcsListeners = new ArrayList();
 325:     services = new HashMap();
 326:   }
 327: 
 328:   protected  void initializeBeanContextResources ()
 329:     throws NotImplementedException
 330:   {
 331:     throw new Error ("Not implemented");
 332:   }
 333: 
 334:   protected  void releaseBeanContextResources ()
 335:     throws NotImplementedException
 336:   {
 337:     throw new Error ("Not implemented");
 338:   }
 339: 
 340:   public void releaseService (BeanContextChild child, Object requestor,
 341:                               Object service)
 342:     throws NotImplementedException
 343:   {
 344:     throw new Error ("Not implemented");
 345:   }
 346: 
 347:   public void removeBeanContextServicesListener
 348:     (BeanContextServicesListener listener)
 349:   {
 350:     synchronized (bcsListeners)
 351:       {
 352:         int index = bcsListeners.indexOf(listener);
 353:         if (index > -1)
 354:           bcsListeners.remove(index);
 355:       }
 356:   }
 357: 
 358:   public void revokeService (Class serviceClass, BeanContextServiceProvider bcsp,
 359:                              boolean revokeCurrentServicesNow)
 360:     throws NotImplementedException
 361:   {
 362:     throw new Error ("Not implemented");
 363:   }
 364: 
 365:   public void serviceAvailable (BeanContextServiceAvailableEvent bcssae)
 366:   {
 367:     synchronized (services)
 368:       {
 369:         Class klass = bcssae.getServiceClass();
 370:         if (services.containsKey(klass))
 371:           return;
 372:         Iterator it = bcsChildren();
 373:         while (it.hasNext())
 374:           {
 375:             Object obj = it.next();
 376:             if (obj instanceof BeanContextServices)
 377:               ((BeanContextServices) obj).serviceAvailable(bcssae);
 378:           }
 379:       }
 380:   }
 381: 
 382:   public void serviceRevoked (BeanContextServiceRevokedEvent bcssre)
 383:   {
 384:     synchronized (services)
 385:       {
 386:         Class klass = bcssre.getServiceClass();
 387:         if (services.containsKey(klass))
 388:           return;
 389:         Iterator it = bcsChildren();
 390:         while (it.hasNext())
 391:           {
 392:             Object obj = it.next();
 393:             if (obj instanceof BeanContextServices)
 394:               ((BeanContextServices) obj).serviceRevoked(bcssre);
 395:           }
 396:       }
 397:   }
 398: }