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;
027:
028: import org.jicarilla.lang.Assert;
029: import org.jicarilla.lang.Selector;
030: import org.jicarilla.lang.Switch;
031:
032: import java.util.ArrayList;
033: import java.util.Iterator;
034: import java.util.List;
035:
036: /**
037: * <p>A Straightforward implementation of the {@link Resolver} interface that
038: * is backed by a {@link ResolverCallback} instance. This implementation is not
039: * thread safe.</p>
040: *
041: * <p>You will normally not want to create instances of this class directly.
042: * Rather, let the associated {@link DefaultContainer} worry about that.</p>
043: *
044: * @author <a href="mail at leosimons dot com">Leo Simons</a>
045: * @version $Id: DefaultResolver.java,v 1.7 2004/03/23 13:37:51 lsimons Exp $
046: */
047: public class DefaultResolver implements Resolver {
048: // ----------------------------------------------------------------------
049: // Properties
050: // ----------------------------------------------------------------------
051: /** The callback to the container. */
052: protected ResolverCallback m_callback;
053:
054: // ----------------------------------------------------------------------
055: // Constructors
056: // ----------------------------------------------------------------------
057: /**
058: * Create a new instance using the provided callback.
059: *
060: * @param callback value for {@link m_callback}.
061: */
062: public DefaultResolver(final ResolverCallback callback) {
063: setCallback(callback);
064: }
065:
066: // ----------------------------------------------------------------------
067: // Getters and Setters
068: // ----------------------------------------------------------------------
069:
070: /**
071: * Returns {@link m_callback}. Overriding is not recommended.
072: *
073: * @return {@link m_callback}
074: */
075: protected ResolverCallback getCallback() {
076: return m_callback;
077: }
078:
079: /**
080: * Sets {@link m_callback}. Direct usage is not recommended.
081: *
082: * @param callback the new {@link m_callback} value.
083: */
084: public void setCallback(final ResolverCallback callback) {
085: Assert.assertNotNull("callback argument may not be null",
086: callback);
087: m_callback = callback;
088: }
089:
090: // ----------------------------------------------------------------------
091: // Interface: Resolver
092: // ----------------------------------------------------------------------
093:
094: /**
095: * @see {@link Resolver#get(Object)
096: *
097: * @param key
098: * @return
099: */
100: public Object get(final Object key) {
101: Assert.assertNotNull("key argument may not be null", key);
102: if (!contains(key))
103: return null;
104:
105: final Object adapter = getCallback().getSwitch().get(key);
106:
107: return getInstanceFromAdapter(adapter, key);
108: }
109:
110: /**
111: * @see {@link Resolver#getAll(Object)
112: *
113: * @param key
114: * @return
115: */
116: public Object[] getAll(final Object key) {
117: Assert.assertNotNull("key argument may not be null", key);
118:
119: if (!contains(key))
120: return new Object[0];
121:
122: final List instances = getInstanceListFromKey(key);
123: return instances.toArray();
124: }
125:
126: /**
127: * @see {@link Resolver#contains(Object)
128: *
129: * @param key
130: * @return
131: */
132: public boolean contains(final Object key) {
133: if (key == null)
134: return false;
135:
136: if (getCallback().getSwitch().containsKey(key))
137: return true;
138:
139: return false;
140: }
141:
142: /**
143: * @see {@link Resolver#releaseInstance(Object)}
144: *
145: * @param component
146: * @throws Exception
147: */
148: public void releaseInstance(final Object component)
149: throws Exception {
150: getCallback().returnedInstance(component);
151: }
152:
153: // ----------------------------------------------------------------------
154: // Helper Methods
155: // ----------------------------------------------------------------------
156:
157: /**
158: * Helper method that is called from {@link #get(Object)} to retrieve
159: * an instance from an adapter and add that instance to the container
160: * through {@link m_callback}.
161: *
162: * @param adapter
163: * @param key
164: * @return
165: */
166: protected Object getInstanceFromAdapter(final Object adapter,
167: final Object key) {
168: if (adapter == null)
169: throw new JicarillaException(
170: "Internal error: the adapter associated with the key "
171: + key + "could not be found!");
172: if (!(adapter instanceof Adapter))
173: throw new JicarillaException(
174: "Internal error: the adapter associated with the key "
175: + key + "is not of type Adapter!");
176:
177: final Object instance = ((Adapter) adapter).getInstance();
178: getCallback().providedInstance(instance, adapter);
179:
180: return instance;
181: }
182:
183: /**
184: * Helper method that is called form {@link #getAll(Object)} to retrive
185: * a list of instances from the switch provided by {@link m_callback}
186: * and add those instances to to the container.
187: *
188: * @param key
189: * @return
190: */
191: protected List getInstanceListFromKey(final Object key) {
192:
193: final List instances = new ArrayList();
194:
195: final Iterator it = getCallback().getSwitch().entryList()
196: .iterator();
197: while (it.hasNext()) {
198: final Switch.Entry entry = (Switch.Entry) it.next();
199: final Selector selector = entry.getSelector();
200: if (selector.select(key)) {
201: // we catch and log the exception here because we will
202: // ignore it if we find some valid instances.
203: final Object adapter = entry.getValue();
204: final Object instance = getInstanceFromAdapter(adapter,
205: key);
206: instances.add(instance);
207: }
208: }
209:
210: //if( instances.size() == 0 ) // not good, since we're
211: // supposed to contain it...
212: // oh well.
213: return instances;
214: }
215: }
|