001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.base;
019:
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.List;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import org.xml.sax.helpers.AttributesImpl;
027:
028: import de.finix.contelligent.client.ContelligentClient;
029: import de.finix.contelligent.client.event.ConfigurationEvent;
030: import de.finix.contelligent.client.event.ConfigurationEventListener;
031: import de.finix.contelligent.client.gui.ContelligentAction;
032: import de.finix.contelligent.client.i18n.Resources;
033: import de.finix.contelligent.client.modules.ModuleDescription;
034: import de.finix.contelligent.client.modules.ModuleRegistry;
035: import de.finix.contelligent.client.remote.ActionResult;
036: import de.finix.contelligent.client.remote.Actions;
037: import de.finix.contelligent.client.remote.RemoteActionException;
038: import de.zeigermann.xml.simpleImporter.ConversionHelpers;
039: import de.zeigermann.xml.simpleImporter.DefaultSimpleImportHandler;
040: import de.zeigermann.xml.simpleImporter.SimplePath;
041:
042: public final class ModuleManager implements ConfigurationEventListener,
043: SessionListener {
044: private static Logger logger = Logger.getLogger(ModuleManager.class
045: .getName());
046:
047: private final static String DOMAIN = "module";
048:
049: private static ModuleManager moduleManager = null;
050:
051: private List<ModuleDescription> modules;
052:
053: private ModuleManager() {
054: ConfigurationManager.getInstance()
055: .addConfigurationEventListener(this , false);
056: }
057:
058: public static ModuleManager getInstance() {
059: if (moduleManager == null) {
060: moduleManager = new ModuleManager();
061: try {
062: moduleManager.load();
063: } catch (RemoteActionException rae) {
064: logger.log(Level.SEVERE,
065: "Could not load ModuleManager", rae);
066: }
067: Session.getInstance().addSessionListener(moduleManager);
068: }
069: return moduleManager;
070: }
071:
072: public void sessionChanged(Session session) {
073: // Dont reload here; that should be handled by Session, since we
074: // cant know when the correct user info is available from the
075: // Session object.
076: }
077:
078: public synchronized void load() throws RemoteActionException {
079: modules = new ArrayList<ModuleDescription>();
080: ModuleDescription sessionModule = new ModuleDescription(
081: ModuleRegistry.getInstance().getImpl("Session"),
082: Resources.getLocalString("session"),
083: ContelligentAction.FILE_ACTION, "user", false, false,
084: false, true, null,
085: ContelligentClient.defaultClientEnvironment);
086: addModule(sessionModule);
087: ModuleHandler handler = new ModuleHandler();
088: ActionResult actionResult = Actions.loadConfiguration(DOMAIN,
089: handler);
090: }
091:
092: public synchronized List getModules() {
093: return Collections.unmodifiableList(modules);
094: }
095:
096: public void onConfigurationAdded(ConfigurationEvent event) {
097: }
098:
099: public void onConfigurationRemoved(ConfigurationEvent event) {
100: if (event.getDomain().equals(DOMAIN)) {
101: try {
102: load();
103: } catch (RemoteActionException rae) {
104: logger.log(Level.SEVERE,
105: "Could not load PreviewManager", rae);
106: }
107: }
108: }
109:
110: public void onConfigurationChanged(ConfigurationEvent event) {
111: if (event.getDomain().equals(DOMAIN)) {
112: try {
113: load();
114: } catch (RemoteActionException rae) {
115: logger.log(Level.SEVERE,
116: "Could not load PreviewManager", rae);
117: }
118: }
119: }
120:
121: public synchronized void addModule(
122: ModuleDescription moduleDescription) {
123: if (moduleDescription != null) {
124: modules.add(moduleDescription);
125: }
126: }
127:
128: public final class ModuleHandler extends DefaultSimpleImportHandler {
129: String configPath = ContelligentClient.defaultClientEnvironment;
130:
131: public void startElement(SimplePath path, String name,
132: AttributesImpl attrs, String leadingCData) {
133: try {
134: if (path.matches("config")) {
135: configPath = attrs.getValue("path");
136: } else if (path.matches("client-module")) {
137: String moduleName = attrs.getValue("name");
138: String moduleImpl = attrs.getValue("moduleImpl");
139: String category = attrs.getValue("category");
140: String tooltip = attrs.getValue("tooltip");
141: boolean createMenuEntry = ConversionHelpers
142: .getBoolean(attrs
143: .getValue("createMenuEntry"), false);
144: boolean createToolbarEntry = ConversionHelpers
145: .getBoolean(attrs
146: .getValue("createToolbarEntry"),
147: false);
148: boolean autoOpenModule = ConversionHelpers
149: .getBoolean(attrs
150: .getValue("autoOpenModule"), false);
151: boolean createTab = ConversionHelpers.getBoolean(
152: attrs.getValue("createTab"), false);
153: String configuration = leadingCData;
154:
155: logger.log(Level.FINE,
156: "parse module config: try to generate module '"
157: + moduleName + "'");
158: logger.log(Level.FINE, "configuration: "
159: + configuration);
160:
161: ModuleDescription moduleDescription = new ModuleDescription(
162: moduleImpl, moduleName, category, tooltip,
163: createMenuEntry, createToolbarEntry,
164: autoOpenModule, createTab, configuration,
165: configPath);
166: ModuleManager.getInstance().addModule(
167: moduleDescription);
168: }
169: } catch (Throwable t) {
170: logger.log(Level.SEVERE,
171: "Caught exception while preview import", t);
172: }
173: }
174: }
175: }
|