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: /**
033: * <p>A straightforward implementation of the {@link KeyRelayingContainer}
034: * interface that is backed by a {@link Switch}. This implementation is not
035: * threadsafe, and delegates most of its functionality to
036: * {@link DefaultContainer}.</p>
037: *
038: * <p>You will normally not want to use this implementation directly, as the
039: * {@link org.jicarilla.container.builder.Builder} interface and its
040: * {@link org.jicarilla.container.builder.DefaultBuilder} implementation
041: * provide a more convenient way to create and populate container
042: * instances.</p>
043: *
044: * @author <a href="mail at leosimons dot com">Leo Simons</a>
045: * @version $Id: DefaultKeyRelayingContainer.java,v 1.7 2004/03/23 13:37:51 lsimons Exp $
046: */
047: public class DefaultKeyRelayingContainer extends DefaultContainer
048: implements KeyRelayingContainer {
049: // ----------------------------------------------------------------------
050: // Constructors
051: // ----------------------------------------------------------------------
052:
053: /**
054: * @see DefaultContainer#DefaultContainer()
055: */
056: public DefaultKeyRelayingContainer() {
057: // this is a shame, cause its expensive
058: super ();
059: super .setResolver(new DefaultKeyRelayingResolver(
060: new DefaultKeyRelayingResolverCallback()));
061: }
062:
063: /**
064: * @see DefaultContainer#DefaultContainer(Resolver)
065: *
066: * @param resolver
067: */
068: public DefaultKeyRelayingContainer(final Resolver resolver) {
069: super (resolver);
070: }
071:
072: /**
073: * @see DefaultContainer#DefaultContainer(Resolver,Switch)
074: *
075: * @param resolver
076: * @param switcher
077: */
078: public DefaultKeyRelayingContainer(final Resolver resolver,
079: final Switch switcher) {
080: super (resolver, switcher);
081: }
082:
083: // ----------------------------------------------------------------------
084: // Interface: KeyRelayingContainer
085: // ----------------------------------------------------------------------
086:
087: /**
088: * @see KeyRelayingContainer#registerAdapter(Selector,KeyAwareAdapter)
089: *
090: * @param selector
091: * @param adapter
092: * @return
093: */
094: public KeyRelayingContainer registerAdapter(
095: final Selector selector, final KeyAwareAdapter adapter) {
096: doAddAdapter(selector, adapter);
097: return this ;
098: }
099:
100: /**
101: * @see KeyRelayingContainer#registerAdapter(Object,KeyAwareAdapter)
102: *
103: * @param key
104: * @param adapter
105: * @return
106: */
107: public KeyRelayingContainer registerAdapter(final Object key,
108: final KeyAwareAdapter adapter) {
109: doAddAdapter(key, adapter);
110: return this ;
111: }
112:
113: // ----------------------------------------------------------------------
114: // Inner Class: DefaultKeyRelayingResolverCallback
115: // ----------------------------------------------------------------------
116:
117: /**
118: * Extension of {@link DefaultContainer.DefaultResolverCallback} that
119: * implements handlign of {@link KeyAwareAdapter}s.
120: */
121: protected class DefaultKeyRelayingResolverCallback extends
122: DefaultContainer.DefaultResolverCallback {
123: /**
124: * @see DefaultContainer.DefaultResolverCallback#providedInstance(Object,Object)
125: *
126: * @param instance
127: * @param adapter
128: */
129: public void providedInstance(final Object instance,
130: final Object adapter) {
131: Assert
132: .assertTrue(
133: "DefaultContainer can only handle adapters that "
134: + "implement the Adapter or KeyAwareAdapter interface!",
135: adapter instanceof Adapter
136: || adapter instanceof KeyAwareAdapter);
137:
138: m_container.getInstanceToAdapterMap()
139: .put(instance, adapter);
140: }
141:
142: /**
143: * @see DefaultContainer.DefaultResolverCallback#returnedInstance(Object)
144: *
145: * @param component
146: * @throws Exception
147: */
148: public void returnedInstance(final Object component)
149: throws Exception {
150: if (component == null)
151: return;
152:
153: final Object adapter = m_container
154: .getInstanceToAdapterMap().remove(component);
155: if (adapter != null) {
156: if (adapter instanceof Adapter) {
157: ((Adapter) adapter).releaseInstance(component);
158: } else {
159: final KeyAwareAdapter keyAdapter = (KeyAwareAdapter) adapter;
160: keyAdapter.releaseInstance(component);
161: }
162: }
163: }
164: }
165: }
|