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;
21:
22: import java.util.HashMap;
23: import java.util.Map;
24: import java.util.WeakHashMap;
25:
26: import org.apache.log4j.Logger;
27:
28: /**
29: * This class manages shared objects, which are build from properties.
30: *
31: * @author mleidig
32: */
33: public class PropertyObjectManager {
34:
35: private Logger LOG = Logger.getLogger(this .getClass());
36: private static PropertyObjectManager instance = new PropertyObjectManager();
37: private Map<Object, HashMap<Class<? extends ConfigurableObject>, ConfigurableObject>> confMaps;
38:
39: /**Returns PropertyObjectManager instance.*/
40: public static PropertyObjectManager getInstance() {
41: return instance;
42: }
43:
44: /**Constructor.*/
45: PropertyObjectManager() {
46: confMaps = new WeakHashMap<Object, HashMap<Class<? extends ConfigurableObject>, ConfigurableObject>>();
47: }
48:
49: public ConfigurableObject getConfigurableObject(Object config,
50: String className) throws Exception {
51: return getConfigurableObject(config, Class.forName(className)
52: .asSubclass(ConfigurableObject.class));
53: }
54:
55: public ConfigurableObject getConfigurableObject(Object config,
56: Class<? extends ConfigurableObject> objClass)
57: throws Exception {
58: HashMap<Class<? extends ConfigurableObject>, ConfigurableObject> confObjs = null;
59: ConfigurableObject confObj = null;
60:
61: confObjs = confMaps.get(config);
62: if (confObjs == null) {
63: synchronized (confMaps) {
64: confObjs = confMaps.get(config);
65: if (confObjs == null) {
66: confObjs = new HashMap<Class<? extends ConfigurableObject>, ConfigurableObject>();
67: confMaps.put(config, confObjs);
68: }
69: }
70: }
71:
72: confObj = confObjs.get(objClass);
73: if (confObj == null) {
74: synchronized (confObjs) {
75: confObj = confObjs.get(objClass);
76: if (confObj == null) {
77: LOG.warn("******* Creating new ConfigurableObject "
78: + objClass.getName());
79: confObj = objClass.newInstance();
80: confObj.init(config);
81: confObjs.put(objClass, confObj);
82: }
83: }
84: }
85: return confObj;
86: }
87: }
|