Mercurial Hosting > luan
changeset 848:22a4e93ed20e
remove Container
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 19 Sep 2016 16:38:36 -0600 |
parents | 5dfb10ec0ca5 |
children | 89db57b2b9b1 |
files | src/org/eclipse/jetty/server/Server.java src/org/eclipse/jetty/server/handler/AbstractHandler.java src/org/eclipse/jetty/server/handler/ContextHandler.java src/org/eclipse/jetty/server/handler/HandlerCollection.java src/org/eclipse/jetty/server/handler/HandlerWrapper.java src/org/eclipse/jetty/server/handler/HotSwapHandler.java src/org/eclipse/jetty/server/handler/RequestLogHandler.java src/org/eclipse/jetty/util/component/Container.java |
diffstat | 8 files changed, 302 insertions(+), 677 deletions(-) [+] |
line wrap: on
line diff
--- a/src/org/eclipse/jetty/server/Server.java Mon Sep 19 16:11:15 2016 -0600 +++ b/src/org/eclipse/jetty/server/Server.java Mon Sep 19 16:38:36 2016 -0600 @@ -36,7 +36,6 @@ import org.eclipse.jetty.util.MultiException; import org.eclipse.jetty.util.TypeUtil; import org.eclipse.jetty.util.URIUtil; -import org.eclipse.jetty.util.component.Container; import org.eclipse.jetty.util.component.Destroyable; import org.eclipse.jetty.util.component.LifeCycle; import org.slf4j.Logger; @@ -69,7 +68,6 @@ __version=System.getProperty("jetty.version","8.y.z-SNAPSHOT"); } - private final Container _container=new Container(); private final AttributesMap _attributes = new AttributesMap(); private ThreadPool _threadPool; private Connector[] _connectors; @@ -123,15 +121,6 @@ } /* ------------------------------------------------------------ */ - /** - * @return Returns the container. - */ - public Container getContainer() - { - return _container; - } - - /* ------------------------------------------------------------ */ public boolean getStopAtShutdown() { return _stopAtShutdown; @@ -195,7 +184,6 @@ connectors[i].setServer(this); } - _container.update(this, _connectors, connectors, "connector"); _connectors = connectors; } @@ -216,7 +204,6 @@ { if (_threadPool!=null) removeBean(_threadPool); - _container.update(this, _threadPool, threadPool, "threadpool",false); _threadPool = threadPool; if (_threadPool!=null) addBean(_threadPool); @@ -489,7 +476,6 @@ { if (super.addBean(o)) { - _container.addBean(o); return true; } return false; @@ -515,7 +501,6 @@ { if (super.removeBean(o)) { - _container.removeBean(o); return true; } return false;
--- a/src/org/eclipse/jetty/server/handler/AbstractHandler.java Mon Sep 19 16:11:15 2016 -0600 +++ b/src/org/eclipse/jetty/server/handler/AbstractHandler.java Mon Sep 19 16:38:36 2016 -0600 @@ -35,71 +35,64 @@ */ public abstract class AbstractHandler extends AggregateLifeCycle implements Handler { - private static final Logger LOG = LoggerFactory.getLogger(AbstractHandler.class); - - private Server _server; - - /* ------------------------------------------------------------ */ - /** - * - */ - public AbstractHandler() - { - } + private static final Logger LOG = LoggerFactory.getLogger(AbstractHandler.class); - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.thread.LifeCycle#start() - */ - @Override - protected void doStart() throws Exception - { - LOG.debug("starting {}",this); - super.doStart(); - } + private Server _server; + + /* ------------------------------------------------------------ */ + /** + * + */ + public AbstractHandler() + { + } - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.thread.LifeCycle#stop() - */ - @Override - protected void doStop() throws Exception - { - LOG.debug("stopping {}",this); - super.doStop(); - } + /* ------------------------------------------------------------ */ + /* + * @see org.eclipse.thread.LifeCycle#start() + */ + @Override + protected void doStart() throws Exception + { + LOG.debug("starting {}",this); + super.doStart(); + } - /* ------------------------------------------------------------ */ - public void setServer(Server server) - { - Server old_server=_server; - if (old_server!=null && old_server!=server) - old_server.getContainer().removeBean(this); - _server=server; - if (_server!=null && _server!=old_server) - _server.getContainer().addBean(this); - } + /* ------------------------------------------------------------ */ + /* + * @see org.eclipse.thread.LifeCycle#stop() + */ + @Override + protected void doStop() throws Exception + { + LOG.debug("stopping {}",this); + super.doStop(); + } - /* ------------------------------------------------------------ */ - public Server getServer() - { - return _server; - } + /* ------------------------------------------------------------ */ + public void setServer(Server server) + { + _server=server; + } - /* ------------------------------------------------------------ */ - public void destroy() - { - if (!isStopped()) - throw new IllegalStateException("!STOPPED"); - super.destroy(); - if (_server!=null) - _server.getContainer().removeBean(this); - } + /* ------------------------------------------------------------ */ + public Server getServer() + { + return _server; + } - /* ------------------------------------------------------------ */ - public void dumpThis(Appendable out) throws IOException - { - out.append(toString()).append(" - ").append(getState()).append('\n'); - } - + /* ------------------------------------------------------------ */ + public void destroy() + { + if (!isStopped()) + throw new IllegalStateException("!STOPPED"); + super.destroy(); + } + + /* ------------------------------------------------------------ */ + public void dumpThis(Appendable out) throws IOException + { + out.append(toString()).append(" - ").append(getState()).append('\n'); + } + }
--- a/src/org/eclipse/jetty/server/handler/ContextHandler.java Mon Sep 19 16:11:15 2016 -0600 +++ b/src/org/eclipse/jetty/server/handler/ContextHandler.java Mon Sep 19 16:38:36 2016 -0600 @@ -250,12 +250,7 @@ { if (_errorHandler != null) { - Server old_server = getServer(); - if (old_server != null && old_server != server) - old_server.getContainer().update(this,_errorHandler,null,"error",true); super.setServer(server); - if (server != null && server != old_server) - server.getContainer().update(this,null,_errorHandler,"error",true); _errorHandler.setServer(server); } else @@ -1259,7 +1254,6 @@ public void setManagedAttribute(String name, Object value) { Object old = _managedAttributes.put(name,value); - getServer().getContainer().update(this,old,value,name,true); } /* ------------------------------------------------------------ */ @@ -1422,8 +1416,6 @@ { if (errorHandler != null) errorHandler.setServer(getServer()); - if (getServer() != null) - getServer().getContainer().update(this,_errorHandler,errorHandler,"errorHandler",true); _errorHandler = errorHandler; }
--- a/src/org/eclipse/jetty/server/handler/HandlerCollection.java Mon Sep 19 16:11:15 2016 -0600 +++ b/src/org/eclipse/jetty/server/handler/HandlerCollection.java Mon Sep 19 16:38:36 2016 -0600 @@ -44,273 +44,267 @@ */ public class HandlerCollection extends AbstractHandlerContainer { - private final boolean _mutableWhenRunning; - private volatile Handler[] _handlers; - private boolean _parallelStart=false; + private final boolean _mutableWhenRunning; + private volatile Handler[] _handlers; + private boolean _parallelStart=false; - /* ------------------------------------------------------------ */ - public HandlerCollection() - { - _mutableWhenRunning=false; - } - - /* ------------------------------------------------------------ */ - public HandlerCollection(boolean mutableWhenRunning) - { - _mutableWhenRunning=mutableWhenRunning; - } + /* ------------------------------------------------------------ */ + public HandlerCollection() + { + _mutableWhenRunning=false; + } + + /* ------------------------------------------------------------ */ + public HandlerCollection(boolean mutableWhenRunning) + { + _mutableWhenRunning=mutableWhenRunning; + } - /* ------------------------------------------------------------ */ - /** - * @return Returns the handlers. - */ - public Handler[] getHandlers() - { - return _handlers; - } - - /* ------------------------------------------------------------ */ - /** - * - * @param handlers The handlers to set. - */ - public void setHandlers(Handler[] handlers) - { - if (!_mutableWhenRunning && isStarted()) - throw new IllegalStateException(STARTED); - - Handler [] old_handlers = _handlers==null?null:_handlers.clone(); - _handlers = handlers; - - Server server = getServer(); - MultiException mex = new MultiException(); - for (int i=0;handlers!=null && i<handlers.length;i++) - { - if (handlers[i].getServer()!=server) - handlers[i].setServer(server); - } + /* ------------------------------------------------------------ */ + /** + * @return Returns the handlers. + */ + public Handler[] getHandlers() + { + return _handlers; + } + + /* ------------------------------------------------------------ */ + /** + * + * @param handlers The handlers to set. + */ + public void setHandlers(Handler[] handlers) + { + if (!_mutableWhenRunning && isStarted()) + throw new IllegalStateException(STARTED); + + Handler [] old_handlers = _handlers==null?null:_handlers.clone(); + _handlers = handlers; + + Server server = getServer(); + MultiException mex = new MultiException(); + for (int i=0;handlers!=null && i<handlers.length;i++) + { + if (handlers[i].getServer()!=server) + handlers[i].setServer(server); + } - if (getServer()!=null) - getServer().getContainer().update(this, old_handlers, handlers, "handler"); - - // stop old handlers - for (int i=0;old_handlers!=null && i<old_handlers.length;i++) - { - if (old_handlers[i]!=null) - { - try - { - if (old_handlers[i].isStarted()) - old_handlers[i].stop(); - } - catch (Throwable e) - { - mex.add(e); - } - } - } - - mex.ifExceptionThrowRuntime(); - } - + // stop old handlers + for (int i=0;old_handlers!=null && i<old_handlers.length;i++) + { + if (old_handlers[i]!=null) + { + try + { + if (old_handlers[i].isStarted()) + old_handlers[i].stop(); + } + catch (Throwable e) + { + mex.add(e); + } + } + } + + mex.ifExceptionThrowRuntime(); + } + - - /* ------------------------------------------------------------ */ - /** Get the parrallelStart. - * @return true if the contained handlers are started in parallel. - */ - public boolean isParallelStart() - { - return _parallelStart; - } + + /* ------------------------------------------------------------ */ + /** Get the parrallelStart. + * @return true if the contained handlers are started in parallel. + */ + public boolean isParallelStart() + { + return _parallelStart; + } - /* ------------------------------------------------------------ */ - /** Set the parallelStart. - * @param parallelStart If true, contained handlers are started in parallel. - */ - public void setParallelStart(boolean parallelStart) - { - this._parallelStart = parallelStart; - } + /* ------------------------------------------------------------ */ + /** Set the parallelStart. + * @param parallelStart If true, contained handlers are started in parallel. + */ + public void setParallelStart(boolean parallelStart) + { + this._parallelStart = parallelStart; + } - /* ------------------------------------------------------------ */ - /** - * @see Handler#handle(String, Request, HttpServletRequest, HttpServletResponse) - */ - public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) - throws IOException, ServletException - { - if (_handlers!=null && isStarted()) - { - MultiException mex=null; - - for (int i=0;i<_handlers.length;i++) - { - try - { - _handlers[i].handle(target,baseRequest, request, response); - } - catch(IOException e) - { - throw e; - } - catch(RuntimeException e) - { - throw e; - } - catch(Exception e) - { - if (mex==null) - mex=new MultiException(); - mex.add(e); - } - } - if (mex!=null) - { - if (mex.size()==1) - throw new ServletException(mex.getThrowable(0)); - else - throw new ServletException(mex); - } - - } - } + /* ------------------------------------------------------------ */ + /** + * @see Handler#handle(String, Request, HttpServletRequest, HttpServletResponse) + */ + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) + throws IOException, ServletException + { + if (_handlers!=null && isStarted()) + { + MultiException mex=null; + + for (int i=0;i<_handlers.length;i++) + { + try + { + _handlers[i].handle(target,baseRequest, request, response); + } + catch(IOException e) + { + throw e; + } + catch(RuntimeException e) + { + throw e; + } + catch(Exception e) + { + if (mex==null) + mex=new MultiException(); + mex.add(e); + } + } + if (mex!=null) + { + if (mex.size()==1) + throw new ServletException(mex.getThrowable(0)); + else + throw new ServletException(mex); + } + + } + } - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.jetty.server.server.handler.AbstractHandler#doStart() - */ - @Override - protected void doStart() throws Exception - { - final MultiException mex=new MultiException(); - if (_handlers!=null) - { - if (_parallelStart) - { - final CountDownLatch latch = new CountDownLatch(_handlers.length); - final ClassLoader loader = Thread.currentThread().getContextClassLoader(); - for (int i=0;i<_handlers.length;i++) - { - final int h=i; - getServer().getThreadPool().dispatch( - new Runnable() - { - public void run() - { - ClassLoader orig = Thread.currentThread().getContextClassLoader(); - try - { - Thread.currentThread().setContextClassLoader(loader); - _handlers[h].start(); - } - catch(Throwable e) - { - mex.add(e); - } - finally - { - Thread.currentThread().setContextClassLoader(orig); - latch.countDown(); - } - } - } - ); - } - latch.await(); - } - else - { - for (int i=0;i<_handlers.length;i++) - { - try{_handlers[i].start();} - catch(Throwable e){mex.add(e);} - } - } - } - super.doStart(); - mex.ifExceptionThrow(); - } + /* ------------------------------------------------------------ */ + /* + * @see org.eclipse.jetty.server.server.handler.AbstractHandler#doStart() + */ + @Override + protected void doStart() throws Exception + { + final MultiException mex=new MultiException(); + if (_handlers!=null) + { + if (_parallelStart) + { + final CountDownLatch latch = new CountDownLatch(_handlers.length); + final ClassLoader loader = Thread.currentThread().getContextClassLoader(); + for (int i=0;i<_handlers.length;i++) + { + final int h=i; + getServer().getThreadPool().dispatch( + new Runnable() + { + public void run() + { + ClassLoader orig = Thread.currentThread().getContextClassLoader(); + try + { + Thread.currentThread().setContextClassLoader(loader); + _handlers[h].start(); + } + catch(Throwable e) + { + mex.add(e); + } + finally + { + Thread.currentThread().setContextClassLoader(orig); + latch.countDown(); + } + } + } + ); + } + latch.await(); + } + else + { + for (int i=0;i<_handlers.length;i++) + { + try{_handlers[i].start();} + catch(Throwable e){mex.add(e);} + } + } + } + super.doStart(); + mex.ifExceptionThrow(); + } - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.jetty.server.server.handler.AbstractHandler#doStop() - */ - @Override - protected void doStop() throws Exception - { - MultiException mex=new MultiException(); - try { super.doStop(); } catch(Throwable e){mex.add(e);} - if (_handlers!=null) - { - for (int i=_handlers.length;i-->0;) - try{_handlers[i].stop();}catch(Throwable e){mex.add(e);} - } - mex.ifExceptionThrow(); - } - - /* ------------------------------------------------------------ */ - @Override - public void setServer(Server server) - { - if (isStarted()) - throw new IllegalStateException(STARTED); - - Server old_server=getServer(); - - super.setServer(server); + /* ------------------------------------------------------------ */ + /* + * @see org.eclipse.jetty.server.server.handler.AbstractHandler#doStop() + */ + @Override + protected void doStop() throws Exception + { + MultiException mex=new MultiException(); + try { super.doStop(); } catch(Throwable e){mex.add(e);} + if (_handlers!=null) + { + for (int i=_handlers.length;i-->0;) + try{_handlers[i].stop();}catch(Throwable e){mex.add(e);} + } + mex.ifExceptionThrow(); + } + + /* ------------------------------------------------------------ */ + @Override + public void setServer(Server server) + { + if (isStarted()) + throw new IllegalStateException(STARTED); + + Server old_server=getServer(); + + super.setServer(server); - Handler[] h=getHandlers(); - for (int i=0;h!=null && i<h.length;i++) - h[i].setServer(server); - - if (server!=null && server!=old_server) - server.getContainer().update(this, null,_handlers, "handler"); - - } + Handler[] h=getHandlers(); + for (int i=0;h!=null && i<h.length;i++) + h[i].setServer(server); + + } - /* ------------------------------------------------------------ */ - /* Add a handler. - * This implementation adds the passed handler to the end of the existing collection of handlers. - * @see org.eclipse.jetty.server.server.HandlerContainer#addHandler(org.eclipse.jetty.server.server.Handler) - */ - public void addHandler(Handler handler) - { - setHandlers((Handler[])LazyList.addToArray(getHandlers(), handler, Handler.class)); - } - - /* ------------------------------------------------------------ */ - public void removeHandler(Handler handler) - { - Handler[] handlers = getHandlers(); - - if (handlers!=null && handlers.length>0 ) - setHandlers((Handler[])LazyList.removeFromArray(handlers, handler)); - } + /* ------------------------------------------------------------ */ + /* Add a handler. + * This implementation adds the passed handler to the end of the existing collection of handlers. + * @see org.eclipse.jetty.server.server.HandlerContainer#addHandler(org.eclipse.jetty.server.server.Handler) + */ + public void addHandler(Handler handler) + { + setHandlers((Handler[])LazyList.addToArray(getHandlers(), handler, Handler.class)); + } + + /* ------------------------------------------------------------ */ + public void removeHandler(Handler handler) + { + Handler[] handlers = getHandlers(); + + if (handlers!=null && handlers.length>0 ) + setHandlers((Handler[])LazyList.removeFromArray(handlers, handler)); + } - /* ------------------------------------------------------------ */ - @Override - protected Object expandChildren(Object list, Class byClass) - { - Handler[] handlers = getHandlers(); - for (int i=0;handlers!=null && i<handlers.length;i++) - list=expandHandler(handlers[i], list, byClass); - return list; - } + /* ------------------------------------------------------------ */ + @Override + protected Object expandChildren(Object list, Class byClass) + { + Handler[] handlers = getHandlers(); + for (int i=0;handlers!=null && i<handlers.length;i++) + list=expandHandler(handlers[i], list, byClass); + return list; + } - /* ------------------------------------------------------------ */ - @Override - public void destroy() - { - if (!isStopped()) - throw new IllegalStateException("!STOPPED"); - Handler[] children=getChildHandlers(); - setHandlers(null); - for (Handler child: children) - child.destroy(); - super.destroy(); - } + /* ------------------------------------------------------------ */ + @Override + public void destroy() + { + if (!isStopped()) + throw new IllegalStateException("!STOPPED"); + Handler[] children=getChildHandlers(); + setHandlers(null); + for (Handler child: children) + child.destroy(); + super.destroy(); + } }
--- a/src/org/eclipse/jetty/server/handler/HandlerWrapper.java Mon Sep 19 16:11:15 2016 -0600 +++ b/src/org/eclipse/jetty/server/handler/HandlerWrapper.java Mon Sep 19 16:38:36 2016 -0600 @@ -79,9 +79,6 @@ _handler = handler; if (handler!=null) handler.setServer(getServer()); - - if (getServer()!=null) - getServer().getContainer().update(this, old_handler, handler, "handler"); } /* ------------------------------------------------------------ */ @@ -134,9 +131,6 @@ Handler h=getHandler(); if (h!=null) h.setServer(server); - - if (server!=null && server!=old_server) - server.getContainer().update(this, null,_handler, "handler"); }
--- a/src/org/eclipse/jetty/server/handler/HotSwapHandler.java Mon Sep 19 16:11:15 2016 -0600 +++ b/src/org/eclipse/jetty/server/handler/HotSwapHandler.java Mon Sep 19 16:38:36 2016 -0600 @@ -81,9 +81,6 @@ handler.setServer(server); addBean(handler); - if (server != null) - server.getContainer().update(this,old_handler,handler,"handler"); - // if there is an old handler and it was started, stop it if (old_handler != null) { @@ -145,9 +142,6 @@ Handler h = getHandler(); if (h != null) h.setServer(server); - - if (server != null && server != old_server) - server.getContainer().update(this,null,_handler,"handler"); } /* ------------------------------------------------------------ */
--- a/src/org/eclipse/jetty/server/handler/RequestLogHandler.java Mon Sep 19 16:11:15 2016 -0600 +++ b/src/org/eclipse/jetty/server/handler/RequestLogHandler.java Mon Sep 19 16:38:36 2016 -0600 @@ -108,9 +108,6 @@ LOG.warn ("",e); } - if (getServer()!=null) - getServer().getContainer().update(this, _requestLog, requestLog, "logimpl",true); - _requestLog = requestLog; //if we're already started, then start our request log @@ -126,25 +123,6 @@ } /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#setServer(org.eclipse.jetty.server.server.Server) - */ - @Override - public void setServer(Server server) - { - if (_requestLog!=null) - { - if (getServer()!=null && getServer()!=server) - getServer().getContainer().update(this, _requestLog, null, "logimpl",true); - super.setServer(server); - if (server!=null && server!=getServer()) - server.getContainer().update(this, null,_requestLog, "logimpl",true); - } - else - super.setServer(server); - } - - /* ------------------------------------------------------------ */ public RequestLog getRequestLog() { return _requestLog;
--- a/src/org/eclipse/jetty/util/component/Container.java Mon Sep 19 16:11:15 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,305 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.util.component; -import java.lang.ref.WeakReference; -import java.util.EventListener; -import java.util.concurrent.CopyOnWriteArrayList; - -import org.eclipse.jetty.util.LazyList; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/* ------------------------------------------------------------ */ -/** Container. - * This class allows a containment events to be generated from update methods. - * - * The style of usage is: <pre> - * public void setFoo(Foo foo) - * { - * getContainer().update(this,this.foo,foo,"foo"); - * this.foo=foo; - * } - * - * public void setBars(Bar[] bars) - * { - * getContainer().update(this,this.bars,bars,"bar"); - * this.bars=bars; - * } - * </pre> - */ -public class Container -{ - private static final Logger LOG = LoggerFactory.getLogger(Container.class); - private final CopyOnWriteArrayList<Container.Listener> _listeners=new CopyOnWriteArrayList<Container.Listener>(); - - public void addEventListener(Container.Listener listener) - { - _listeners.add(listener); - } - - public void removeEventListener(Container.Listener listener) - { - _listeners.remove(listener); - } - - /* ------------------------------------------------------------ */ - /** Update single parent to child relationship. - * @param parent The parent of the child. - * @param oldChild The previous value of the child. If this is non null and differs from <code>child</code>, then a remove event is generated. - * @param child The current child. If this is non null and differs from <code>oldChild</code>, then an add event is generated. - * @param relationship The name of the relationship - */ - public void update(Object parent, Object oldChild, final Object child, String relationship) - { - if (oldChild!=null && !oldChild.equals(child)) - remove(parent,oldChild,relationship); - if (child!=null && !child.equals(oldChild)) - add(parent,child,relationship); - } - - /* ------------------------------------------------------------ */ - /** Update single parent to child relationship. - * @param parent The parent of the child. - * @param oldChild The previous value of the child. If this is non null and differs from <code>child</code>, then a remove event is generated. - * @param child The current child. If this is non null and differs from <code>oldChild</code>, then an add event is generated. - * @param relationship The name of the relationship - * @param addRemove If true add/remove is called for the new/old children as well as the relationships - */ - public void update(Object parent, Object oldChild, final Object child, String relationship,boolean addRemove) - { - if (oldChild!=null && !oldChild.equals(child)) - { - remove(parent,oldChild,relationship); - if (addRemove) - removeBean(oldChild); - } - - if (child!=null && !child.equals(oldChild)) - { - if (addRemove) - addBean(child); - add(parent,child,relationship); - } - } - - /* ------------------------------------------------------------ */ - /** Update multiple parent to child relationship. - * @param parent The parent of the child. - * @param oldChildren The previous array of children. A remove event is generated for any child in this array but not in the <code>children</code> array. - * This array is modified and children that remain in the new children array are nulled out of the old children array. - * @param children The current array of children. An add event is generated for any child in this array but not in the <code>oldChildren</code> array. - * @param relationship The name of the relationship - */ - public void update(Object parent, Object[] oldChildren, final Object[] children, String relationship) - { - update(parent,oldChildren,children,relationship,false); - } - - /* ------------------------------------------------------------ */ - /** Update multiple parent to child relationship. - * @param parent The parent of the child. - * @param oldChildren The previous array of children. A remove event is generated for any child in this array but not in the <code>children</code> array. - * This array is modified and children that remain in the new children array are nulled out of the old children array. - * @param children The current array of children. An add event is generated for any child in this array but not in the <code>oldChildren</code> array. - * @param relationship The name of the relationship - * @param addRemove If true add/remove is called for the new/old children as well as the relationships - */ - public void update(Object parent, Object[] oldChildren, final Object[] children, String relationship, boolean addRemove) - { - Object[] newChildren = null; - if (children!=null) - { - newChildren = new Object[children.length]; - - for (int i=children.length;i-->0;) - { - boolean new_child=true; - if (oldChildren!=null) - { - for (int j=oldChildren.length;j-->0;) - { - if (children[i]!=null && children[i].equals(oldChildren[j])) - { - oldChildren[j]=null; - new_child=false; - } - } - } - if (new_child) - newChildren[i]=children[i]; - } - } - - if (oldChildren!=null) - { - for (int i=oldChildren.length;i-->0;) - { - if (oldChildren[i]!=null) - { - remove(parent,oldChildren[i],relationship); - if (addRemove) - removeBean(oldChildren[i]); - } - } - } - - if (newChildren!=null) - { - for (int i=0;i<newChildren.length;i++) - if (newChildren[i]!=null) - { - if (addRemove) - addBean(newChildren[i]); - add(parent,newChildren[i],relationship); - } - } - } - - /* ------------------------------------------------------------ */ - public void addBean(Object obj) - { - if (_listeners!=null) - { - for (int i=0; i<LazyList.size(_listeners); i++) - { - Listener listener=(Listener)LazyList.get(_listeners, i); - listener.addBean(obj); - } - } - } - - /* ------------------------------------------------------------ */ - public void removeBean(Object obj) - { - if (_listeners!=null) - { - for (int i=0; i<LazyList.size(_listeners); i++) - ((Listener)LazyList.get(_listeners, i)).removeBean(obj); - } - } - - /* ------------------------------------------------------------ */ - /** Add a parent child relationship - * @param parent - * @param child - * @param relationship - */ - private void add(Object parent, Object child, String relationship) - { - if (LOG.isDebugEnabled()) - LOG.debug("Container "+parent+" + "+child+" as "+relationship); - if (_listeners!=null) - { - Relationship event=new Relationship(this,parent,child,relationship); - for (int i=0; i<LazyList.size(_listeners); i++) - ((Listener)LazyList.get(_listeners, i)).add(event); - } - } - - /* ------------------------------------------------------------ */ - /** remove a parent child relationship - * @param parent - * @param child - * @param relationship - */ - private void remove(Object parent, Object child, String relationship) - { - if (LOG.isDebugEnabled()) - LOG.debug("Container "+parent+" - "+child+" as "+relationship); - if (_listeners!=null) - { - Relationship event=new Relationship(this,parent,child,relationship); - for (int i=0; i<LazyList.size(_listeners); i++) - ((Listener)LazyList.get(_listeners, i)).remove(event); - } - } - - /* ------------------------------------------------------------ */ - /** A Container event. - * @see Listener - */ - public static class Relationship - { - private final WeakReference<Object> _parent; - private final WeakReference<Object> _child; - private String _relationship; - private Container _container; - - private Relationship(Container container, Object parent,Object child, String relationship) - { - _container=container; - _parent=new WeakReference<Object>(parent); - _child=new WeakReference<Object>(child); - _relationship=relationship; - } - - public Container getContainer() - { - return _container; - } - - public Object getChild() - { - return _child.get(); - } - - public Object getParent() - { - return _parent.get(); - } - - public String getRelationship() - { - return _relationship; - } - - @Override - public String toString() - { - return _parent+"---"+_relationship+"-->"+_child; - } - - @Override - public int hashCode() - { - return _parent.hashCode()+_child.hashCode()+_relationship.hashCode(); - } - - @Override - public boolean equals(Object o) - { - if (o==null || !(o instanceof Relationship)) - return false; - Relationship r = (Relationship)o; - return r._parent.get()==_parent.get() && r._child.get()==_child.get() && r._relationship.equals(_relationship); - } - } - - /* ------------------------------------------------------------ */ - /** Listener. - * A listener for Container events. - */ - public interface Listener extends EventListener - { - public void addBean(Object bean); - public void removeBean(Object bean); - public void add(Container.Relationship relationship); - public void remove(Container.Relationship relationship); - } -}