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.factories;
027:
028: import org.jicarilla.container.CyclicDependencyException;
029: import org.jicarilla.container.Factory;
030: import org.jicarilla.container.JicarillaClassNotFoundException;
031: import org.jicarilla.container.JicarillaException;
032: import org.jicarilla.container.JicarillaIllegalAccessException;
033: import org.jicarilla.container.JicarillaInstantiationException;
034: import org.jicarilla.container.JicarillaInvocationTargetException;
035: import org.jicarilla.container.Resolver;
036: import org.jicarilla.container.util.ReflectionUtil;
037: import org.jicarilla.lang.Assert;
038: import org.jicarilla.lang.LifecycleUtil;
039:
040: import java.lang.reflect.InvocationTargetException;
041:
042: /**
043: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
044: * @version $Id: AbstractFactory.java,v 1.2 2004/03/23 13:37:52 lsimons Exp $
045: */
046: public abstract class AbstractFactory implements Factory {
047: // ----------------------------------------------------------------------
048: // Properties
049: // ----------------------------------------------------------------------
050: protected final Resolver m_resolver;
051: protected final String m_className;
052: protected Class m_clazz;
053: protected boolean m_inTheProcessOfConstructingAnInstance = false;
054:
055: // ----------------------------------------------------------------------
056: // Constructors
057: // ----------------------------------------------------------------------
058: public AbstractFactory(final Resolver resolver,
059: final String className) {
060: Assert.assertNotNull("resolver argument may not be null",
061: resolver);
062: Assert.assertNotNull("className argument may not be null",
063: className);
064:
065: m_resolver = resolver;
066: m_className = className;
067: }
068:
069: // ----------------------------------------------------------------------
070: // Interface: Factory
071: // ----------------------------------------------------------------------
072:
073: public Object newInstance() {
074: lazyInitialization();
075: Object instance = null;
076:
077: try {
078: try {
079: instance = doNewInstance();
080: } catch (InstantiationException e) {
081: throw new JicarillaInstantiationException(e);
082: } catch (IllegalAccessException e) {
083: throw new JicarillaIllegalAccessException(e);
084: } catch (InvocationTargetException e) {
085: throw new JicarillaInvocationTargetException(e);
086: }
087: } catch (CyclicDependencyException cde) {
088: handleCyclicDependencyException(cde);
089: } finally {
090: disableCyclicDependencyChecking();
091: }
092: initialize(instance);
093: return instance;
094: }
095:
096: public void releaseInstance(final Object component)
097: throws Exception {
098: doReleaseInstance(component);
099: }
100:
101: protected void doReleaseInstance(final Object component)
102: throws Exception {
103: dispose(component);
104: }
105:
106: protected abstract Object doNewInstance()
107: throws IllegalAccessException, InvocationTargetException,
108: InstantiationException;
109:
110: // ----------------------------------------------------------------------
111: // Getters/Setters
112: // ----------------------------------------------------------------------
113: protected Resolver getResolver() {
114: return m_resolver;
115: }
116:
117: protected String getClassName() {
118: return m_className;
119: }
120:
121: protected Class getClazz() {
122: return m_clazz;
123: }
124:
125: // ----------------------------------------------------------------------
126: // Helper Methods
127: // ----------------------------------------------------------------------
128: protected void enableCyclicDependencyChecking() {
129: m_inTheProcessOfConstructingAnInstance = true;
130: }
131:
132: protected void checkCyclicDependency(final Class[] parameterTypes) {
133: if (m_inTheProcessOfConstructingAnInstance)
134: throw new CyclicDependencyException(parameterTypes);
135: }
136:
137: protected void disableCyclicDependencyChecking() {
138: m_inTheProcessOfConstructingAnInstance = false;
139: }
140:
141: protected void handleCyclicDependencyException(
142: final CyclicDependencyException cde) {
143: throw cde;
144: }
145:
146: protected void lazyInitialization() {
147: if (m_clazz == null)
148: try {
149: m_clazz = ReflectionUtil.loadClass(getClassName());
150: } catch (ClassNotFoundException e) {
151: throw new JicarillaClassNotFoundException(e);
152: }
153: }
154:
155: protected void initialize(Object instance) {
156: try {
157: LifecycleUtil.initialize(instance);
158: } catch (Throwable t) {
159: throw new JicarillaInstantiationException(t);
160: }
161: }
162:
163: protected void dispose(Object instance) throws Exception {
164: try {
165: LifecycleUtil.dispose(instance);
166: } catch (Throwable t) {
167: if (t instanceof Exception)
168: throw (Exception) t;
169: if (t instanceof RuntimeException)
170: throw (RuntimeException) t;
171: if (t instanceof Error)
172: throw (Error) t;
173:
174: throw new JicarillaException(t);
175: }
176:
177: }
178: }
|