001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.core.component;
027:
028: import java.lang.reflect.Constructor;
029:
030: /**
031: * Implement the basics of a BinderFactory. A full implementation
032: * will at least implement the getBinderClass() method and may override getBinder.
033: * We expect many BinderFactory implementations to not use this base
034: * class at all and write full implementations themselves.
035: * <p>
036: * The default implementation does not implement setParameter or request any services.
037: **/
038: public abstract class BinderFactorySupport extends
039: org.cougaar.util.GenericStateModelAdapter implements
040: BinderFactory {
041:
042: private BindingSite parentComponent = null;
043:
044: public void setBindingSite(BindingSite bs) {
045: parentComponent = bs;
046: }
047:
048: protected final BindingSite getBindingSite() {
049: return parentComponent;
050: }
051:
052: //
053: /** Override to choose the class of the Binder to use.
054: * This method should return null if the child is not bindable with
055: * this Factory. The default implementation returns null.
056: **/
057: protected Class getBinderClass(Object child) {
058: return null;
059: }
060:
061: /** Bind the Child component. <p>
062: * The child component will already have been instantiated and any
063: * parameter has been set. Depending on the ComponentFactory (or other
064: * constructor/initializer methods) used, there may have been additional
065: * initialization performed. <p>
066: * Generally all this method does is construct a new instance of
067: * bindingSite for use with the child component.
068: * Various implementations may do additional Binder initialization
069: * such as starting a thread, instructing the binder to provide additional
070: * services, etc.
071: *
072: * By default, it does the equivalent of return new <em>binderClass</em>(ContainerAPI,child);
073: *
074: * @return A Binder instance of class bindingSite which is binding
075: * the child component or null.
076: **/
077: protected Binder bindChild(Class binderClass, Object child) {
078: try {
079: Constructor constructor = binderClass
080: .getConstructor(new Class[] { BinderFactory.class,
081: Object.class });
082: Binder binder = (Binder) constructor
083: .newInstance(new Object[] { this , child });
084: return binder;
085: } catch (Exception e) {
086: throw new RuntimeException("Failed to construct "
087: + binderClass + " to bind " + child, e);
088: }
089: }
090:
091: // implement Component
092: public BinderFactorySupport() {
093: }
094:
095: //public void setParameter(Object parameter) { }
096:
097: // implement BinderFactory
098: /** override to set a higher priority. The default is MIN_PRIORITY (lowest) **/
099: public int getPriority() {
100: return MIN_PRIORITY;
101: }
102:
103: /** standard getBinder implementation essentially calls getBinderClass and
104: * then bindChild.
105: **/
106: public Binder getBinder(Object child) {
107: // figure out which binder to use.
108: Class bc = getBinderClass(child);
109: if (bc == null)
110: return null;
111:
112: return bindChild(bc, child);
113: }
114:
115: /** Provide a default ComponentFactory instance. This implementation
116: * simply returns a default ComponentFactory static instance. It should <em>not</em>
117: * return a new instance each time.
118: **/
119: public ComponentFactory getComponentFactory() {
120: return ComponentFactory.getInstance();
121: }
122: }
|