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.pfixxml.targets;
21:
22: import java.util.HashMap;
23:
24: import org.apache.log4j.Logger;
25:
26: import de.schlund.pfixxml.XMLException;
27: import de.schlund.pfixxml.resources.FileResource;
28:
29: public class TargetGeneratorFactory {
30: private static TargetGeneratorFactory instance = new TargetGeneratorFactory();
31: private static HashMap<String, TargetGenerator> generatormap = new HashMap<String, TargetGenerator>();
32: private final static Logger LOG = Logger
33: .getLogger(TargetGeneratorFactory.class);
34:
35: public static TargetGeneratorFactory getInstance() {
36: return instance;
37: }
38:
39: public synchronized TargetGenerator createGenerator(
40: FileResource cfile) throws Exception {
41: if (cfile.exists() && cfile.isFile() && cfile.canRead()) {
42: String key = genKey(cfile);
43: TargetGenerator generator = (TargetGenerator) generatormap
44: .get(key);
45: if (generator == null) {
46: LOG.debug("-- Init TargetGenerator --");
47: generator = new TargetGenerator(cfile);
48:
49: // Check generator has unique name
50: String tgenName = generator.getName();
51: for (TargetGenerator tgen : generatormap.values()) {
52: if (tgen.getName().equals(tgenName)) {
53: String msg = "Cannot create TargetGenerator for config file \""
54: + generator.getConfigPath().toString()
55: + "\" as it is using the name \""
56: + tgenName
57: + "\" which is already being used by TargetGenerator with config file \""
58: + tgen.getConfigPath().toString()
59: + "\"";
60: LOG.error(msg);
61: throw new Exception(msg);
62: }
63: }
64:
65: generatormap.put(key, generator);
66: } else {
67: generator.tryReinit();
68: }
69: return generator;
70: } else {
71: throw new XMLException("\nConfigfile '" + cfile.toString()
72: + "' isn't a file, can't be read or doesn't exist");
73: }
74: }
75:
76: public void remove(FileResource genfile) {
77: generatormap.remove(genKey(genfile));
78: }
79:
80: private String genKey(FileResource genfile) {
81: return genfile.toString();
82: }
83:
84: public void reset() {
85: generatormap = new HashMap<String, TargetGenerator>();
86: }
87:
88: }
|