01: /*
02: * This file is part of PFIXCORE.
03: *
04: * PFIXCORE is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU Lesser General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * PFIXCORE is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public License
15: * along with PFIXCORE; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: */
19:
20: package de.schlund.pfixcore.workflow;
21:
22: import java.util.HashMap;
23:
24: import de.schlund.pfixcore.util.FlyWeightChecker;
25:
26: /**
27: * StateFactory.java
28: *
29: *
30: * Created: Sat Oct 13 00:07:21 2001
31: *
32: * @author <a href="mailto:jtl@schlund.de">Jens Lautenbacher</a>
33: */
34:
35: public class StateFactory {
36: private static HashMap<String, State> knownstates = new HashMap<String, State>();
37: private static StateFactory instance = new StateFactory();
38:
39: public static StateFactory getInstance() {
40: return instance;
41: }
42:
43: private StateFactory() {
44: }
45:
46: /**
47: * <code>getState</code> returns the matching State for classname.
48: *
49: * @param classname a <code>String</code> value
50: * @return a <code>State</code> value
51: */
52: public State getState(String classname) {
53: synchronized (knownstates) {
54: State retval = (State) knownstates.get(classname);
55: if (retval == null) {
56: try {
57: Class<?> stateclass = Class.forName(classname);
58: retval = (State) stateclass.newInstance();
59: if (!FlyWeightChecker.check(retval)) {
60: throw new IllegalStateException(
61: "You MUST NOT use non-static/non-final fields in flyweight class "
62: + classname);
63: }
64: knownstates.put(classname, retval);
65: } catch (InstantiationException e) {
66: throw new IllegalStateException(
67: "unable to instantiate class [" + classname
68: + "] :" + e.getMessage());
69: } catch (IllegalAccessException e) {
70: throw new IllegalStateException(
71: "unable access class [" + classname + "] :"
72: + e.getMessage());
73: } catch (ClassNotFoundException e) {
74: throw new IllegalStateException(
75: "unable to find class [" + classname
76: + "] :" + e.getMessage());
77: } catch (ClassCastException e) {
78: throw new IllegalStateException(
79: "class ["
80: + classname
81: + "] does not implement the interface IHandler. :"
82: + e.getMessage());
83: }
84: }
85: return retval;
86: }
87: }
88:
89: }
|