01: /* ====================================================================
02: The Jicarilla Software License
03:
04: Copyright (c) 2003 Leo Simons.
05: All rights reserved.
06:
07: Permission is hereby granted, free of charge, to any person obtaining
08: a copy of this software and associated documentation files (the
09: "Software"), to deal in the Software without restriction, including
10: without limitation the rights to use, copy, modify, merge, publish,
11: distribute, sublicense, and/or sell copies of the Software, and to
12: permit persons to whom the Software is furnished to do so, subject to
13: the following conditions:
14:
15: The above copyright notice and this permission notice shall be
16: included in all copies or substantial portions of the Software.
17:
18: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25: ==================================================================== */
26: package org.jicarilla.container.factories;
27:
28: import org.jicarilla.container.Resolver;
29: import org.jicarilla.container.Factory;
30: import org.jicarilla.lang.Assert;
31:
32: /**
33: * <p>A small utility class for instantiation of and disposing of instances
34: * that need to be created using dependencies from a resolver, but that
35: * should not live inside the container. Example usage:</p>
36: *
37: * <pre>
38: * Resolver resolver =
39: * DefaultBuilder.newInstance()
40: * .addComponent( HomerImpl.class )
41: * .addComponent( MargeImpl.class )
42: * .create();
43: * Script unmangedScript = (Script)FactoryUtil.newInstance( resolver,
44: * SomeScriptImpl.class );
45: * // ...
46: * FactoryUtil.releaseInstance( unmanagedScript );
47: * </pre>
48: *
49: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
50: * @version $Id: FactoryUtil.java,v 1.2 2004/03/23 13:37:52 lsimons Exp $
51: */
52: public class FactoryUtil {
53: public static Object newInstance(Resolver resolver, Class clazz) {
54: final Factory f = new Type35Factory(resolver, clazz.getName());
55: return f.newInstance();
56: }
57:
58: public static void releaseInstance(Object instance)
59: throws Exception {
60: Assert.assertNotNull("instance argument may not be null",
61: instance);
62:
63: final Resolver r = new Resolver() {
64: public Object get(Object key) {
65: return null;
66: }
67:
68: public Object[] getAll(Object key) {
69: return new Object[0];
70: }
71:
72: public boolean contains(Object key) {
73: return false;
74: }
75:
76: public void releaseInstance(Object instance)
77: throws Exception {
78: }
79: };
80: final Factory f = new Type35Factory(r, instance.getClass()
81: .getName());
82:
83: f.releaseInstance(instance);
84: }
85: }
|