001: package org.enhydra.jawe.base.componentmanager;
002:
003: import java.lang.reflect.Constructor;
004: import java.util.ArrayList;
005: import java.util.HashMap;
006: import java.util.List;
007: import java.util.Map;
008: import java.util.Properties;
009:
010: import org.enhydra.jawe.JaWE;
011: import org.enhydra.jawe.JaWEComponent;
012: import org.enhydra.jawe.JaWEComponentSettings;
013: import org.enhydra.jawe.JaWEConstants;
014: import org.enhydra.jawe.JaWEManager;
015: import org.enhydra.jawe.PropertyMgr;
016: import org.enhydra.jawe.ResourceManager;
017: import org.enhydra.jawe.Utils;
018: import org.enhydra.jawe.components.graph.GraphController;
019: import org.enhydra.jawe.components.graph.GraphSettings;
020: import org.enhydra.jawe.components.simplenavigator.SimpleNavigator;
021: import org.enhydra.jawe.components.simplenavigator.SimpleNavigatorSettings;
022: import org.enhydra.jawe.components.xpdlview.XPDLViewController;
023: import org.enhydra.jawe.components.xpdlview.XPDLViewSettings;
024:
025: /**
026: * Used to manage JaWE components.
027: *
028: * @author Sasa Bojanic
029: */
030: public class ComponentManager {
031: public static final String PROPERTYFILE_PATH = "org/enhydra/jawe/base/componentmanager/properties/";
032:
033: public static final String PROPERTYFILE_NAME = "componentmanager.properties";
034:
035: protected Map componentMap = new HashMap();
036:
037: protected PropertyMgr propertyMgr;
038:
039: protected Properties properties = new Properties();
040:
041: public ComponentManager() {
042: }
043:
044: public void setPropertyMgr(PropertyMgr pm) {
045: this .propertyMgr = pm;
046: }
047:
048: protected void registerComponents(JaWEComponent comp) {
049: JaWEManager.getInstance().getJaWEController()
050: .registerJaWEComponent(comp);
051: }
052:
053: public JaWEComponent getComponent(String name) {
054: return (JaWEComponent) componentMap.get(name);
055: }
056:
057: public List getComponents() {
058: return new ArrayList(componentMap.values());
059: }
060:
061: public void addComponent(JaWEComponent comp) {
062: componentMap.put(comp.getName(), comp);
063: }
064:
065: public void init() {
066: if (propertyMgr == null) {
067: if (Utils.checkFileExistence(PROPERTYFILE_NAME)
068: || Utils.checkResourceExistence(PROPERTYFILE_PATH,
069: PROPERTYFILE_NAME)) {
070: try {
071: Utils.manageProperties(properties,
072: PROPERTYFILE_PATH, PROPERTYFILE_NAME);
073: } catch (Exception e) {
074: }
075: } else {
076: try {
077: Utils.manageProperties(properties,
078: JaWEConstants.JAWE_BASIC_PROPERTYFILE_PATH,
079: JaWEConstants.JAWE_BASIC_PROPERTYFILE_NAME);
080: } catch (Exception e) {
081: }
082: }
083: } else {
084: if (Utils.checkFileExistence(PROPERTYFILE_NAME)
085: || Utils.checkResourceExistence(PROPERTYFILE_PATH,
086: PROPERTYFILE_NAME)) {
087: properties = propertyMgr.loadProperties(
088: PROPERTYFILE_PATH, PROPERTYFILE_NAME);
089: } else {
090: properties = propertyMgr.loadProperties(
091: JaWEConstants.JAWE_BASIC_PROPERTYFILE_PATH,
092: JaWEConstants.JAWE_BASIC_PROPERTYFILE_NAME);
093: }
094: }
095:
096: boolean treeComponentAdded = false;
097: if (JaWE.getJaWEVersion() != JaWE.COMMUNITY_VERSION
098: || JaWE.addOnsAvailable()) {
099:
100: ClassLoader cl = getClass().getClassLoader();
101:
102: List compToAdd = ResourceManager.getResourceStrings(
103: properties, "Component.Add.", false);
104:
105: // main components
106: String mcomp = ResourceManager.getResourceString(
107: properties, "Main.ComponentOrder");
108: String[] mcomps = Utils.tokenize(mcomp, " ");
109: for (int i = 0; i < mcomps.length; i++) {
110: int pos = shoudBeAdded(compToAdd, mcomps[i]);
111: if (pos != -1) {
112: compToAdd.remove(pos);
113: String component = ResourceManager
114: .getResourceString(properties,
115: "Component.Add." + mcomps[i]);
116: String settings = ResourceManager
117: .getResourceString(properties, "Settings."
118: + mcomps[i]);
119: try {
120: JaWEComponentSettings set = (JaWEComponentSettings) cl
121: .loadClass(settings).newInstance();
122: set.setPropertyMgr(propertyMgr);
123:
124: Constructor c = Class
125: .forName(component)
126: .getConstructor(
127: new Class[] { JaWEComponentSettings.class });
128: JaWEComponent jc = (JaWEComponent) c
129: .newInstance(new Object[] { set });
130: jc.setType(JaWEComponent.MAIN_COMPONENT);
131: registerComponents(jc);
132: JaWEManager.getInstance().getLoggingManager()
133: .debug(
134: "ComponentManager -> component "
135: + jc.getName()
136: + " added to JaWE");
137: componentMap.put(jc.getName(), jc);
138: } catch (Throwable thr) {
139: if (JaWE.getJaWEVersion() != JaWE.COMMUNITY_VERSION) {
140: JaWEManager.getInstance()
141: .getLoggingManager().error(
142: "ComponentManager -> error while adding JaWE component "
143: + component + "!",
144: thr);
145: }
146: }
147: }
148: }
149:
150: // special components
151: String scomp = ResourceManager.getResourceString(
152: properties, "Special.ComponentOrder");
153: String[] scomps = Utils.tokenize(scomp, " ");
154: for (int i = 0; i < scomps.length; i++) {
155: int pos = shoudBeAdded(compToAdd, scomps[i]);
156: if (pos != -1) {
157: compToAdd.remove(pos);
158: String component = ResourceManager
159: .getResourceString(properties,
160: "Component.Add." + scomps[i]);
161: String settings = ResourceManager
162: .getResourceString(properties, "Settings."
163: + scomps[i]);
164: try {
165: JaWEComponentSettings set = (JaWEComponentSettings) cl
166: .loadClass(settings).newInstance();
167: set.setPropertyMgr(propertyMgr);
168:
169: Constructor c = Class
170: .forName(component)
171: .getConstructor(
172: new Class[] { JaWEComponentSettings.class });
173: JaWEComponent jc = (JaWEComponent) c
174: .newInstance(new Object[] { set });
175: jc.setType(JaWEComponent.SPECIAL_COMPONENT);
176: registerComponents(jc);
177: JaWEManager.getInstance().getLoggingManager()
178: .debug(
179: "ComponentManager -> component "
180: + jc.getName()
181: + " added to JaWE");
182: componentMap.put(jc.getName(), jc);
183: } catch (Throwable thr) {
184: if (JaWE.getJaWEVersion() != JaWE.COMMUNITY_VERSION) {
185: JaWEManager.getInstance()
186: .getLoggingManager().error(
187: "ComponentManager -> error while adding JaWE component "
188: + component + "!",
189: thr);
190: }
191: }
192: }
193: }
194:
195: // tree components
196: String tcomp = ResourceManager.getResourceString(
197: properties, "Tree.ComponentOrder");
198: String[] tcomps = Utils.tokenize(tcomp, " ");
199: for (int i = 0; i < tcomps.length; i++) {
200: int pos = shoudBeAdded(compToAdd, tcomps[i]);
201: if (pos != -1) {
202: compToAdd.remove(pos);
203: String component = ResourceManager
204: .getResourceString(properties,
205: "Component.Add." + tcomps[i]);
206: String settings = ResourceManager
207: .getResourceString(properties, "Settings."
208: + tcomps[i]);
209: try {
210: JaWEComponentSettings set = (JaWEComponentSettings) cl
211: .loadClass(settings).newInstance();
212: set.setPropertyMgr(propertyMgr);
213:
214: Constructor c = Class
215: .forName(component)
216: .getConstructor(
217: new Class[] { JaWEComponentSettings.class });
218: JaWEComponent jc = (JaWEComponent) c
219: .newInstance(new Object[] { set });
220: jc.setType(JaWEComponent.TREE_COMPONENT);
221: registerComponents(jc);
222: JaWEManager.getInstance().getLoggingManager()
223: .debug(
224: "ComponentManager -> component "
225: + jc.getName()
226: + " added to JaWE");
227: componentMap.put(jc.getName(), jc);
228: treeComponentAdded = true;
229: } catch (Throwable thr) {
230: if (JaWE.getJaWEVersion() != JaWE.COMMUNITY_VERSION) {
231: JaWEManager.getInstance()
232: .getLoggingManager().error(
233: "ComponentManager -> error while adding JaWE component "
234: + component + "!",
235: thr);
236: }
237: }
238: }
239: }
240:
241: // other components
242: String ocomp = ResourceManager.getResourceString(
243: properties, "Other.ComponentOrder");
244: String[] ocomps = Utils.tokenize(ocomp, " ");
245: for (int i = 0; i < ocomps.length; i++) {
246: int pos = shoudBeAdded(compToAdd, ocomps[i]);
247: if (pos != -1) {
248: compToAdd.remove(pos);
249: String component = ResourceManager
250: .getResourceString(properties,
251: "Component.Add." + ocomps[i]);
252: String settings = ResourceManager
253: .getResourceString(properties, "Settings."
254: + ocomps[i]);
255: try {
256: JaWEComponentSettings set = (JaWEComponentSettings) cl
257: .loadClass(settings).newInstance();
258: set.setPropertyMgr(propertyMgr);
259:
260: Constructor c = Class
261: .forName(component)
262: .getConstructor(
263: new Class[] { JaWEComponentSettings.class });
264: JaWEComponent jc = (JaWEComponent) c
265: .newInstance(new Object[] { set });
266: jc.setType(JaWEComponent.OTHER_COMPONENT);
267: registerComponents(jc);
268: JaWEManager.getInstance().getLoggingManager()
269: .debug(
270: "ComponentManager -> component "
271: + jc.getName()
272: + " added to JaWE");
273: componentMap.put(jc.getName(), jc);
274: } catch (Throwable thr) {
275: if (JaWE.getJaWEVersion() != JaWE.COMMUNITY_VERSION) {
276: JaWEManager.getInstance()
277: .getLoggingManager().error(
278: "ComponentManager -> error while adding JaWE component "
279: + component + "!",
280: thr);
281: }
282: }
283: }
284: }
285:
286: for (int i = 0; i < compToAdd.size(); i++) {
287: String component = ResourceManager
288: .getResourceString(properties, "Component.Add."
289: + compToAdd.get(i));
290: String settings = ResourceManager.getResourceString(
291: properties, "Settings." + compToAdd.get(i));
292: try {
293: JaWEComponentSettings set = (JaWEComponentSettings) cl
294: .loadClass(settings).newInstance();
295: set.setPropertyMgr(propertyMgr);
296:
297: Constructor c = Class
298: .forName(component)
299: .getConstructor(
300: new Class[] { JaWEComponentSettings.class });
301: JaWEComponent jc = (JaWEComponent) c
302: .newInstance(new Object[] { set });
303: registerComponents(jc);
304: JaWEManager.getInstance().getLoggingManager()
305: .debug(
306: "ComponentManager -> component "
307: + jc.getName()
308: + " added to JaWE");
309: componentMap.put(jc.getName(), jc);
310: } catch (Throwable thr) {
311: if (JaWE.getJaWEVersion() != JaWE.COMMUNITY_VERSION) {
312: JaWEManager.getInstance().getLoggingManager()
313: .error(
314: "ComponentManager -> error while adding JaWE component "
315: + component + "!", thr);
316: }
317: }
318: }
319:
320: String upperComponent = ResourceManager.getResourceString(
321: properties, "UpperStatus");
322: String upperSettings = ResourceManager.getResourceString(
323: properties, "UpperStatus.Settings");
324: if (null != upperComponent && !"".equals(upperComponent)) {
325: try {
326: JaWEComponentSettings set = (JaWEComponentSettings) cl
327: .loadClass(upperSettings).newInstance();
328: set.setPropertyMgr(propertyMgr);
329:
330: Constructor c = Class
331: .forName(upperComponent)
332: .getConstructor(
333: new Class[] { JaWEComponentSettings.class });
334: JaWEComponent jc = (JaWEComponent) c
335: .newInstance(new Object[] { set });
336: jc.setType(JaWEComponent.UPPER_STATUS_COMPONENT);
337: registerComponents(jc);
338: JaWEManager.getInstance().getLoggingManager()
339: .debug(
340: "ComponentManager -> component "
341: + jc.getName()
342: + " added to JaWE");
343: componentMap.put(jc.getName(), jc);
344: } catch (Throwable thr) {
345: if (JaWE.getJaWEVersion() != JaWE.COMMUNITY_VERSION) {
346: JaWEManager.getInstance().getLoggingManager()
347: .error(
348: "ComponentManager -> error while adding JaWE component "
349: + upperComponent + "!",
350: thr);
351: }
352: }
353: }
354:
355: String lowerComponent = ResourceManager.getResourceString(
356: properties, "LowerStatus");
357: String lowerSettings = ResourceManager.getResourceString(
358: properties, "LowerStatus.Settings");
359: if (null != lowerComponent && !"".equals(lowerComponent)) {
360: try {
361: JaWEComponentSettings set = (JaWEComponentSettings) cl
362: .loadClass(lowerSettings).newInstance();
363: set.setPropertyMgr(propertyMgr);
364:
365: Constructor c = Class
366: .forName(lowerComponent)
367: .getConstructor(
368: new Class[] { JaWEComponentSettings.class });
369: JaWEComponent jc = (JaWEComponent) c
370: .newInstance(new Object[] { set });
371: jc.setType(JaWEComponent.LOWER_STATUS_COMPONENT);
372: registerComponents(jc);
373: JaWEManager.getInstance().getLoggingManager()
374: .debug(
375: "ComponentManager -> component "
376: + jc.getName()
377: + " added to JaWE");
378: componentMap.put(jc.getName(), jc);
379: } catch (Throwable thr) {
380: if (JaWE.getJaWEVersion() != JaWE.COMMUNITY_VERSION) {
381: JaWEManager.getInstance().getLoggingManager()
382: .error(
383: "ComponentManager -> error while adding JaWE component "
384: + lowerComponent + "!",
385: thr);
386: }
387: }
388: }
389: } else {
390: initDefault();
391: }
392: if (!treeComponentAdded
393: && JaWE.getJaWEVersion() == JaWE.COMMUNITY_VERSION) {
394: try {
395: SimpleNavigator snc = new SimpleNavigator(
396: new SimpleNavigatorSettings());
397: addComponent(snc);
398: registerComponents(snc);
399: } catch (Exception ex) {
400: String msg = "ComponentManager --> Problems while instantiating SimpleNavigator - working without it!";
401: JaWEManager.getInstance().getLoggingManager().error(
402: msg, ex);
403: }
404: }
405: JaWEManager.getInstance().getJaWEController().getJaWEFrame()
406: .rearrangeFrame();
407:
408: }
409:
410: protected int shoudBeAdded(List compList, String compName) {
411: if (compName == null)
412: return -1;
413: for (int i = 0; i < compList.size(); i++) {
414: String name = (String) compList.get(i);
415: if (compName.equals(name))
416: return i;
417: }
418:
419: return -1;
420: }
421:
422: protected void initDefault() {
423: try {
424: GraphController gc = new GraphController(
425: new GraphSettings());
426: addComponent(gc);
427: registerComponents(gc);
428: JaWEManager.getInstance().getLoggingManager().debug(
429: "ComponentManager -> component " + gc.getName()
430: + " added to JaWE");
431: } catch (Exception ex) {
432: String msg = "ComponentManager --> Problems while instantiating GraphController - working without it!";
433: JaWEManager.getInstance().getLoggingManager()
434: .error(msg, ex);
435: }
436: try {
437: XPDLViewController xpdlc = new XPDLViewController(
438: new XPDLViewSettings());
439: addComponent(xpdlc);
440: registerComponents(xpdlc);
441: JaWEManager.getInstance().getLoggingManager().debug(
442: "ComponentManager -> component " + xpdlc.getName()
443: + " added to JaWE");
444: } catch (Exception ex) {
445: String msg = "ComponentManager --> Problems while instantiating XPDLViewController - working without it!";
446: JaWEManager.getInstance().getLoggingManager()
447: .error(msg, ex);
448: }
449: }
450:
451: }
|