001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.container.integration.builder;
027:
028: import org.apache.avalon.framework.component.ComponentManager;
029: import org.apache.avalon.framework.service.ServiceManager;
030: import org.jcontainer.dna.ResourceLocator;
031: import org.jicarilla.container.Adapter;
032: import org.jicarilla.container.Container;
033: import org.jicarilla.container.DefaultKeyRelayingContainer;
034: import org.jicarilla.container.KeyAwareAdapter;
035: import org.jicarilla.container.KeyRelayingContainer;
036: import org.jicarilla.container.Resolver;
037: import org.jicarilla.container.ResolverProvider;
038: import org.jicarilla.container.builder.AdapterHelper;
039: import org.jicarilla.container.builder.Builder;
040: import org.jicarilla.container.builder.DefaultBuilder;
041: import org.jicarilla.container.builder.DefaultHelper;
042: import org.jicarilla.container.builder.EntryHelper;
043: import org.jicarilla.container.builder.KeyAwareAdapterHelper;
044: import org.jicarilla.container.builder.ResolverHelper;
045: import org.jicarilla.container.builder.ResolverProviderHelper;
046: import org.jicarilla.container.builder.SingletonType35Helper;
047: import org.jicarilla.lang.Assert;
048: import org.picocontainer.PicoContainer;
049: import org.springframework.beans.factory.BeanFactory;
050:
051: /**
052: * <p>A Builder implementation that's backed by a container itself. In essence,
053: * we're using a container to help build a container. The documentation for the
054: * builder API may be a bit complex, but the usage is actually quite easy.
055: * Just take a look at the following example:</p>
056: *
057: * <pre>
058: * PicoContainer picoContainer = getPicoContainer();
059: * Container fortressContainer = getFortressContainer();
060: * ResourceLocator dnaContainer = getDnaContainer();
061: * Resolver parentContainerResolver = getParentResolver();
062: * Resolver resolver =
063: * JicarillaBuilder.newInstance()
064: * .addComponent( HomerImpl.class )
065: * .addComponent( YoungHomer.class )
066: *
067: * .addComponent( new HomerImpl() )
068: * .addComponent( new BartImpl() )
069: * .addComponent(
070: * new ClassSelector( Marge.class ),
071: * new SingletonAdapter(
072: * new ManualFactory(
073: * new MargeImpl()
074: * )
075: * )
076: * )
077: * .addComponent(
078: * new ClassSelector( Lisa.class ),
079: * new SingletonAdapter(
080: * new DnaFactory(
081: * DnaLisa.class
082: * )
083: * )
084: * )
085: * .addComponent(
086: * new ClassSelector( Maggie.class ),
087: * new SingletonAdapter(
088: * new Type1Factory(
089: * AvalonMaggie.class
090: * )
091: * )
092: * )
093: * .addComponent( parentContainerResolver )
094: * .addComponent( picoContainer )
095: * .addComponent( fortressContainer )
096: * .addComponent( dnaContainer )
097: * .create();
098: * </pre>
099: *
100: * <p>Note a Builder instance is single-use, and you should create a new
101: * instance (using {@link #newInstance()} every time you call
102: * {@link #create()}.</p>
103: *
104: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
105: * @version $Id: JicarillaBuilder.java,v 1.5 2004/04/03 12:59:48 lsimons Exp $
106: */
107: public class JicarillaBuilder extends DefaultBuilder {
108: // ----------------------------------------------------------------------
109: // Constructor and factory method
110: // ----------------------------------------------------------------------
111: /**
112: * Create a new builder that will be used to populate the provided
113: * container.
114: *
115: * @param container the container to populate.
116: */
117: protected JicarillaBuilder(KeyRelayingContainer container) {
118: super (container);
119: }
120:
121: /**
122: * Create a new builder.
123: *
124: * @return a new builder instance to be used in building a single new
125: * container instance.
126: */
127: public static Builder newInstance() {
128: return new JicarillaBuilder(new DefaultKeyRelayingContainer());
129: }
130:
131: /**
132: * Add a new <em>provider</em> to the container. A provider can be lots of
133: * things: another {@link Container container} or a generic
134: * {@link ResolverProvider resolver provider{, a {@link Resolver}, a
135: * different kind of container (a {@link PicoContainer},
136: * {@link BeanFactory}, or {@link ServiceManager} for example), an
137: * {@link Adapter}, {@link KeyAwareAdapter}, {@link Entry entry}, a
138: * {@link Class}, an {@link Object instance}, and several other things.
139: * The builder will figure out what to do with the provider. Subclasses
140: * may add helpers for other kinds of providers.
141: *
142: * @param provider the provider to add to the container.
143: * @return the current instance.
144: */
145: public Builder addComponent(final Object provider) {
146: super .addComponent(provider);
147: Assert.assertNotNull("provider argument may not be null",
148: provider);
149: m_componentProviders.add(provider);
150:
151: return this ;
152: }
153:
154: protected void addHelpers() {
155: addHelperHelpers();
156:
157: // --------------------------------------------------------------------
158: // Jicarilla support
159: // --------------------------------------------------------------------
160: addHelper(Adapter.class, AdapterHelper.class);
161: addHelper(KeyAwareAdapter.class, KeyAwareAdapterHelper.class);
162: addHelper(Resolver.class, ResolverHelper.class);
163: addHelper(ResolverProvider.class, ResolverProviderHelper.class);
164: addHelper(Entry.class, EntryHelper.class);
165:
166: // --------------------------------------------------------------------
167: // Avalon support
168: // --------------------------------------------------------------------
169: addHelper(org.apache.avalon.fortress.Container.class,
170: FortressContainerHelper.class);
171: addHelper(ServiceManager.class, ServiceManagerHelper.class);
172: addHelper(ComponentManager.class, ComponentManagerHelper.class);
173:
174: // --------------------------------------------------------------------
175: // PicoContainer support
176: // --------------------------------------------------------------------
177: addHelper(PicoContainer.class, PicoContainerHelper.class);
178: addHelper(org.picocontainer.ComponentAdapter.class,
179: PicoComponentAdapterHelper.class);
180:
181: // --------------------------------------------------------------------
182: // XWork support
183: // --------------------------------------------------------------------
184: // todo
185:
186: // --------------------------------------------------------------------
187: // Springframework support
188: // --------------------------------------------------------------------
189: addHelper(BeanFactory.class, SpringBeanFactoryHelper.class);
190:
191: // --------------------------------------------------------------------
192: // DNA support
193: // --------------------------------------------------------------------
194: addHelper(ResourceLocator.class, ResourceLocatorHelper.class);
195:
196: // --------------------------------------------------------------------
197: // Loom support
198: // --------------------------------------------------------------------
199: // todo
200:
201: // --------------------------------------------------------------------
202: // Basics
203: // --------------------------------------------------------------------
204:
205: addHelper(Class.class, SingletonType35Helper.class);
206: addHelper(Object.class, DefaultHelper.class);
207: }
208: }
|