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.pfixxml.targets;
021:
022: import java.lang.reflect.Constructor;
023: import java.util.TreeMap;
024:
025: import org.apache.log4j.Logger;
026:
027: /**
028: * TargetFactory.java
029: *
030: *
031: * Created: Mon Jul 23 15:38:41 2001
032: *
033: * @author <a href="mailto:jtl@schlund.de">Jens Lautenbacher</a>
034: *
035: *
036: */
037:
038: public class TargetFactory {
039: private final static Logger LOG = Logger
040: .getLogger(TargetFactory.class);
041: private static TargetFactory instance = new TargetFactory();
042:
043: private TreeMap<String, TargetRW> targetmap = new TreeMap<String, TargetRW>();
044:
045: private TargetFactory() {
046: }
047:
048: public static TargetFactory getInstance() {
049: return instance;
050: }
051:
052: public boolean exists(TargetType type, TargetGenerator gen,
053: String targetkey) {
054: String key = createKey(type, gen, targetkey);
055: Target ret = (Target) targetmap.get(key);
056: if (ret != null) {
057: return true;
058: }
059: return false;
060: }
061:
062: public synchronized TargetRW getTarget(TargetType type,
063: TargetGenerator gen, String targetkey, Themes themes) {
064: String key = createKey(type, gen, targetkey);
065: TargetRW ret = (TargetRW) targetmap.get(key);
066: if (ret == null) {
067: ret = createTargetForType(type, gen, targetkey, themes);
068: targetmap.put(key, ret);
069: }
070: return ret;
071: }
072:
073: private String createKey(TargetType type, TargetGenerator gen,
074: String targetkey) {
075: return (type.getTag() + "@" + gen.getName() + "@" + targetkey);
076: }
077:
078: private TargetRW createTargetForType(TargetType type,
079: TargetGenerator gen, String targetkey, Themes themes) {
080: TargetRW target;
081: LOG.debug("===> Creating target '" + targetkey + "' " + type
082: + " [" + gen.getName() + "]");
083: try {
084: Class<? extends TargetRW> theclass = type.getTargetClass();
085: Constructor<? extends TargetRW> constructor = theclass
086: .getConstructor(new Class[] { type.getClass(),
087: gen.getClass(), targetkey.getClass(),
088: Themes.class });
089: target = constructor.newInstance(new Object[] { type, gen,
090: targetkey, themes });
091: } catch (Exception e) {
092: throw new RuntimeException("error creating target '"
093: + targetkey + "' " + type + " [" + gen.getName()
094: + "]: " + e.toString(), e);
095: }
096: return target;
097: }
098:
099: public void reset() {
100: targetmap.clear();
101: }
102:
103: }// TargetFactory
|