001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.generator;
021:
022: import java.util.HashMap;
023:
024: import de.schlund.pfixcore.util.FlyWeightChecker;
025:
026: /**
027: * IHandlerFactory.java
028: *
029: *
030: * Created: Sat Oct 13 00:07:21 2001
031: *
032: * @author <a href="mailto:jtl@schlund.de">Jens Lautenbacher</a>
033: *
034: *
035: */
036:
037: public class IHandlerFactory {
038: private static HashMap<String, IHandler> knownhandlers = new HashMap<String, IHandler>();
039: private static HashMap<String, IHandler> wrapper2handlers = new HashMap<String, IHandler>();
040: // private static Logger LOG = Logger.getLogger(IHandlerFactory.class);
041: private static IHandlerFactory instance = new IHandlerFactory();
042:
043: private IHandlerFactory() {
044: // do nothing.
045: }
046:
047: /**
048: * <code>getInstance</code> returns the single Instance of a IHandlerFactory.
049: *
050: * @return an <code>IHandlerFactory</code> value
051: */
052: public static IHandlerFactory getInstance() {
053: return instance;
054: }
055:
056: /**
057: * <code>getIHandler</code> returns the IHandler for the given classname.
058: * (IHandlers are flyweights).
059: *
060: * @param classname a <code>String</code> value
061: * @return a <code>IHandler</code> value
062: */
063: public IHandler getIHandler(String classname) {
064: synchronized (knownhandlers) {
065: IHandler retval = (IHandler) knownhandlers.get(classname);
066: if (retval == null) {
067: try {
068: Class<?> stateclass = Class.forName(classname);
069: retval = (IHandler) stateclass.newInstance();
070: if (!FlyWeightChecker.check(retval)) {
071: throw new IllegalStateException(
072: "You MUST NOT use non-static/non-final fields in flyweight class "
073: + classname);
074: }
075: knownhandlers.put(classname, retval);
076: } catch (InstantiationException e) {
077: throw new IllegalStateException(
078: "unable to instantiate class [" + classname
079: + "] :" + e.getMessage());
080: } catch (IllegalAccessException e) {
081: throw new IllegalStateException(
082: "unable access class [" + classname + "] :"
083: + e.getMessage());
084: } catch (ClassNotFoundException e) {
085: throw new IllegalStateException(
086: "unable to find class [" + classname
087: + "] :" + e.getMessage());
088: } catch (ClassCastException e) {
089: throw new IllegalStateException(
090: "class ["
091: + classname
092: + "] does not implement the interface IHandler. :"
093: + e.getMessage());
094: }
095: }
096: // LOG.debug("Retval is: " + retval);
097: return retval;
098: }
099: }
100:
101: public IHandler getIHandlerForWrapperClass(String classname) {
102: synchronized (wrapper2handlers) {
103: IHandler retval = wrapper2handlers.get(classname);
104: if (retval == null) {
105: try {
106: Class<?> stateclass = Class.forName(classname);
107: IWrapper wrapper = (IWrapper) stateclass
108: .newInstance();
109: retval = wrapper.gimmeIHandler();
110: wrapper2handlers.put(classname, retval);
111: } catch (InstantiationException e) {
112: throw new IllegalStateException(
113: "unable to instantiate class [" + classname
114: + "] :" + e.getMessage());
115: } catch (IllegalAccessException e) {
116: throw new IllegalStateException(
117: "unable access class [" + classname + "] :"
118: + e.getMessage());
119: } catch (ClassNotFoundException e) {
120: throw new IllegalStateException(
121: "unable to find class [" + classname
122: + "] :" + e.getMessage());
123: } catch (ClassCastException e) {
124: throw new IllegalStateException(
125: "class ["
126: + classname
127: + "] does not implement the interface IWrapper :"
128: + e.getMessage());
129: }
130: }
131: return retval;
132: }
133: }
134:
135: }// IHandlerFactory
|