📄 beancontextservicessupport.java
字号:
BeanContextServiceProvider bcsp = bcsssp.getServiceProvider(); service = bcsp.getService(bcssp, requestor, serviceClass, serviceSelector); if (service != null) { // do bookkeeping ... try { bcsc.usingService(requestor, service, serviceClass, bcsp, false, bcsrl); } catch (TooManyListenersException tmle) { bcsp.releaseService(bcssp, requestor, service); throw tmle; } catch (UnsupportedOperationException uope) { bcsp.releaseService(bcssp, requestor, service); throw uope; // unchecked rt exception } return service; } } if (proxy != null) { // try to delegate ... service = proxy.getService(bcssp, requestor, serviceClass, serviceSelector); if (service != null) { // do bookkeeping ... try { bcsc.usingService(requestor, service, serviceClass, proxy, true, bcsrl); } catch (TooManyListenersException tmle) { proxy.releaseService(bcssp, requestor, service); throw tmle; } catch (UnsupportedOperationException uope) { proxy.releaseService(bcssp, requestor, service); throw uope; // unchecked rt exception } return service; } } } return null; } /** * release a service */ public void releaseService(BeanContextChild child, Object requestor, Object service) { if (child == null) throw new NullPointerException("child"); if (requestor == null) throw new NullPointerException("requestor"); if (service == null) throw new NullPointerException("service"); BCSSChild bcsc; synchronized(BeanContext.globalHierarchyLock) { synchronized(children) { bcsc = (BCSSChild)children.get(child); } if (bcsc != null) bcsc.releaseService(requestor, service); else throw new IllegalArgumentException("child actual is not a child of this BeanContext"); } } /** * @return an iterator for all the currently registered service classes. */ public Iterator getCurrentServiceClasses() { return new BCSIterator(services.keySet().iterator()); } /** * @return an iterator for all the currently available service selectors * (if any) available for the specified service. */ public Iterator getCurrentServiceSelectors(Class serviceClass) { BCSSServiceProvider bcsssp = (BCSSServiceProvider)services.get(serviceClass); return bcsssp != null ? new BCSIterator(bcsssp.getServiceProvider().getCurrentServiceSelectors(getBeanContextServicesPeer(), serviceClass)) : null; } /** * BeanContextServicesListener callback, propagates event to all * currently registered listeners and BeanContextServices children, * if this BeanContextService does not already implement this service * itself. * * subclasses may override or envelope this method to implement their * own propagation semantics. */ public void serviceAvailable(BeanContextServiceAvailableEvent bcssae) { synchronized(BeanContext.globalHierarchyLock) { if (services.containsKey(bcssae.getServiceClass())) return; fireServiceAdded(bcssae); Iterator i; synchronized(children) { i = children.keySet().iterator(); } while (i.hasNext()) { Object c = i.next(); if (c instanceof BeanContextServices) { ((BeanContextServicesListener)c).serviceAvailable(bcssae); } } } } /** * BeanContextServicesListener callback, propagates event to all * currently registered listeners and BeanContextServices children, * if this BeanContextService does not already implement this service * itself. * * subclasses may override or envelope this method to implement their * own propagation semantics. */ public void serviceRevoked(BeanContextServiceRevokedEvent bcssre) { synchronized(BeanContext.globalHierarchyLock) { if (services.containsKey(bcssre.getServiceClass())) return; fireServiceRevoked(bcssre); Iterator i; synchronized(children) { i = children.keySet().iterator(); } while (i.hasNext()) { Object c = i.next(); if (c instanceof BeanContextServices) { ((BeanContextServicesListener)c).serviceRevoked(bcssre); } } } } /** * Gets the <tt>BeanContextServicesListener</tt> (if any) of the specified * child. * * @param child the specified child * @return the BeanContextServicesListener (if any) of the specified child */ protected static final BeanContextServicesListener getChildBeanContextServicesListener(Object child) { try { return (BeanContextServicesListener)child; } catch (ClassCastException cce) { return null; } } /** * called from superclass child removal operations after a child * has been successfully removed. called with child synchronized. * * This subclass uses this hook to immediately revoke any services * being used by this child if it is a BeanContextChild. * * subclasses may envelope this method in order to implement their * own child removal side-effects. */ protected void childJustRemovedHook(Object child, BCSChild bcsc) { BCSSChild bcssc = (BCSSChild)bcsc; bcssc.cleanupReferences(); } /** * called from setBeanContext to notify a BeanContextChild * to release resources obtained from the nesting BeanContext. * * This method revokes any services obtained from its parent. * * subclasses may envelope this method to implement their own semantics. */ protected synchronized void releaseBeanContextResources() { Object[] bcssc; super.releaseBeanContextResources(); synchronized(children) { if (children.isEmpty()) return; bcssc = children.values().toArray(); } for (int i = 0; i < bcssc.length; i++) { ((BCSSChild)bcssc[i]).revokeAllDelegatedServicesNow(); } proxy = null; } /** * called from setBeanContext to notify a BeanContextChild * to allocate resources obtained from the nesting BeanContext. * * subclasses may envelope this method to implement their own semantics. */ protected synchronized void initializeBeanContextResources() { super.initializeBeanContextResources(); BeanContext nbc = getBeanContext(); if (nbc == null) return; try { BeanContextServices bcs = (BeanContextServices)nbc; proxy = new BCSSProxyServiceProvider(bcs); } catch (ClassCastException cce) { // do nothing ... } } /** * Fires a <tt>BeanContextServiceEvent</tt> notifying of a new service. */ protected final void fireServiceAdded(Class serviceClass) { BeanContextServiceAvailableEvent bcssae = new BeanContextServiceAvailableEvent(getBeanContextServicesPeer(), serviceClass); fireServiceAdded(bcssae); } /** * Fires a <tt>BeanContextServiceAvailableEvent</tt> indicating that a new * service has become available. * * @param bcssae the <tt>BeanContextServiceAvailableEvent</tt> */ protected final void fireServiceAdded(BeanContextServiceAvailableEvent bcssae) { Object[] copy; synchronized (bcsListeners) { copy = bcsListeners.toArray(); } for (int i = 0; i < copy.length; i++) { ((BeanContextServicesListener)copy[i]).serviceAvailable(bcssae); } } /** * Fires a <tt>BeanContextServiceEvent</tt> notifying of a service being revoked. * * @param bcsre the <tt>BeanContextServiceRevokedEvent</tt> */ protected final void fireServiceRevoked(BeanContextServiceRevokedEvent bcsre) { Object[] copy; synchronized (bcsListeners) { copy = bcsListeners.toArray(); } for (int i = 0; i < copy.length; i++) { ((BeanContextServiceRevokedListener)copy[i]).serviceRevoked(bcsre); } } /** * Fires a <tt>BeanContextServiceRevokedEvent</tt> * indicating that a particular service is * no longer available. */ protected final void fireServiceRevoked(Class serviceClass, boolean revokeNow) { Object[] copy; BeanContextServiceRevokedEvent bcsre = new BeanContextServiceRevokedEvent(getBeanContextServicesPeer(), serviceClass, revokeNow); synchronized (bcsListeners) { copy = bcsListeners.toArray(); } for (int i = 0; i < copy.length; i++) { ((BeanContextServicesListener)copy[i]).serviceRevoked(bcsre); } } /** * called from BeanContextSupport writeObject before it serializes the * children ... * * This class will serialize any Serializable BeanContextServiceProviders * herein. * * subclasses may envelope this method to insert their own serialization * processing that has to occur prior to serialization of the children */ protected synchronized void bcsPreSerializationHook(ObjectOutputStream oos) throws IOException { oos.writeInt(serializable); if (serializable <= 0) return; int count = 0; Iterator i = services.entrySet().iterator(); while (i.hasNext() && count < serializable) { Map.Entry entry = (Map.Entry)i.next(); BCSSServiceProvider bcsp = null; try { bcsp = (BCSSServiceProvider)entry.getValue(); } catch (ClassCastException cce) { continue; } if (bcsp.getServiceProvider() instanceof Serializable) { oos.writeObject(entry.getKey()); oos.writeObject(bcsp); count++; } } if (count != serializable) throw new IOException("wrote different number of service providers than expected"); } /** * called from BeanContextSupport readObject before it deserializes the * children ... * * This class will deserialize any Serializable BeanContextServiceProviders * serialized earlier thus making them available to the children when they * deserialized. * * subclasses may envelope this method to insert their own serialization * processing that has to occur prior to serialization of the children */ protected synchronized void bcsPreDeserializationHook(ObjectInputStream ois) throws IOException, ClassNotFoundException { serializable = ois.readInt(); int count = serializable; while (count > 0) { services.put(ois.readObject(), ois.readObject()); count--; } } /** * serialize the instance */ private synchronized void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); serialize(oos, (Collection)bcsListeners); } /** * deserialize the instance */ private synchronized void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException { ois.defaultReadObject(); deserialize(ois, (Collection)bcsListeners); } /* * fields */ /** * all accesses to the <code> protected transient HashMap services </code> * field should be synchronized on that object */ protected transient HashMap services; /** * The number of instances of a serializable <tt>BeanContextServceProvider</tt>. */ protected transient int serializable = 0; /** * Delegate for the <tt>BeanContextServiceProvider</tt>. */ protected transient BCSSProxyServiceProvider proxy; /** * List of <tt>BeanContextServicesListener</tt> objects. */ protected transient ArrayList bcsListeners;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -