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.builder;
027:
028: import org.jicarilla.container.CustomizableResolver;
029: import org.jicarilla.container.DefaultCustomizableResolver;
030: import org.jicarilla.container.Resolver;
031: import org.jicarilla.lang.Assert;
032: import org.jicarilla.lang.Selector;
033:
034: import java.util.ArrayList;
035: import java.util.Iterator;
036: import java.util.List;
037:
038: /**
039: * Use to override parts of the default autowiring policy for a particular
040: * component.
041: *
042: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
043: * @version $Id: CustomComponent.java,v 1.1 2004/03/23 15:59:52 lsimons Exp $
044: */
045: public class CustomComponent {
046: // ----------------------------------------------------------------------
047: // Properties
048: // ----------------------------------------------------------------------
049: protected Class m_class;
050: protected List m_overrides;
051:
052: // ----------------------------------------------------------------------
053: // Constructor
054: // ----------------------------------------------------------------------
055: public CustomComponent(Class clazz) {
056: Assert.assertNotNull("clazz argument may not be null", clazz);
057: m_class = clazz;
058:
059: m_overrides = new ArrayList();
060: }
061:
062: // ----------------------------------------------------------------------
063: // Public API
064: // ----------------------------------------------------------------------
065: public CustomComponent overrideKey(Object key, Object instance) {
066: m_overrides.add(new KeyOverride(key, instance));
067: return this ;
068: }
069:
070: public CustomComponent override(Selector selector, Object instance) {
071: m_overrides.add(new SelectorOverride(selector, instance));
072: return this ;
073: }
074:
075: /**
076: *
077: * @param callNumber index starts at 0
078: * @param instance
079: */
080: public CustomComponent overrideCall(int callNumber, Object instance) {
081: m_overrides.add(new CallOverride(callNumber, instance));
082: return this ;
083: }
084:
085: public CustomComponent redirectKey(Object oldKey, Object newKey) {
086: m_overrides.add(new KeyRedirect(oldKey, newKey));
087: return this ;
088: }
089:
090: public CustomComponent redirectCall(int callNumber, Object newKey) {
091: m_overrides.add(new CallRedirect(callNumber, newKey));
092: return this ;
093: }
094:
095: // ----------------------------------------------------------------------
096: // Internal API
097: // ----------------------------------------------------------------------
098: Class getClazz() {
099: return m_class;
100: }
101:
102: Resolver getResolver(Resolver delegate) {
103: CustomizableResolver resolver = new DefaultCustomizableResolver(
104: delegate);
105:
106: final Iterator it = m_overrides.iterator();
107: while (it.hasNext()) {
108: Object o = it.next();
109: if (o instanceof KeyOverride) {
110: KeyOverride ko = (KeyOverride) o;
111: resolver.overrideKey(ko.key, ko.instance);
112: } else if (o instanceof SelectorOverride) {
113: SelectorOverride so = (SelectorOverride) o;
114: resolver.overrideKey(so.selector, so.instance);
115: } else if (o instanceof CallOverride) {
116: CallOverride co = (CallOverride) o;
117: resolver.overrideCall(co.call, co.instance);
118: } else if (o instanceof KeyRedirect) {
119: KeyRedirect kr = (KeyRedirect) o;
120: resolver.redirectKey(kr.key, kr.newKey);
121: } else if (o instanceof CallRedirect) {
122: CallRedirect cr = (CallRedirect) o;
123: resolver.redirectCall(cr.call, cr.newKey);
124: }
125: }
126: return resolver;
127: }
128:
129: protected class KeyOverride {
130: public Object key;
131: public Object instance;
132:
133: public KeyOverride(Object key, Object instance) {
134: this .key = key;
135: this .instance = instance;
136: }
137: };
138:
139: protected class SelectorOverride {
140: public Selector selector;
141: public Object instance;
142:
143: public SelectorOverride(Selector selector, Object instance) {
144: this .selector = selector;
145: this .instance = instance;
146: }
147: };
148:
149: protected class CallOverride {
150: public int call;
151: public Object instance;
152:
153: public CallOverride(int call, Object instance) {
154: this .call = call;
155: this .instance = instance;
156: }
157: };
158:
159: protected class KeyRedirect {
160: public Object key;
161: public Object newKey;
162:
163: public KeyRedirect(Object key, Object newKey) {
164: this .key = key;
165: this .newKey = newKey;
166: }
167: };
168:
169: protected class CallRedirect {
170: public int call;
171: public Object newKey;
172:
173: public CallRedirect(int call, Object newKey) {
174: this.call = call;
175: this.newKey = newKey;
176: }
177: };
178: }
|