summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/process/module/ModuleManager.java
blob: e78c25d66d989d89074e4dee7d4f21da57705d6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.c2kernel.process.module;

import java.io.IOException;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;

import com.c2kernel.common.CannotManageException;
import com.c2kernel.common.InvalidDataException;
import com.c2kernel.common.ObjectAlreadyExistsException;
import com.c2kernel.common.ObjectCannotBeUpdated;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.proxy.ItemProxy;
import com.c2kernel.lookup.DomainPath;
import com.c2kernel.persistency.outcome.OutcomeValidator;
import com.c2kernel.persistency.outcome.Schema;
import com.c2kernel.process.Gateway;
import com.c2kernel.scripting.ScriptingEngineException;
import com.c2kernel.utils.FileStringUtility;
import com.c2kernel.utils.Logger;
import com.c2kernel.utils.Resource;

public class ModuleManager {
	ArrayList<Module> modules = new ArrayList<Module>();
	HashMap<String, String> modulesXML = new HashMap<String, String>();
	Properties props = new Properties();
	boolean isServer;
	OutcomeValidator moduleValidator;

	public ModuleManager(Enumeration<URL> moduleEnum, boolean isServer) throws ModuleException {
		this.isServer = isServer;
		try {
			Schema moduleSchema = new Schema("Module", 0, false, 
					FileStringUtility.url2String(Resource.getKernelResourceURL("boot/OD/Module.xsd")));
			moduleValidator = new OutcomeValidator(moduleSchema);
		} catch (InvalidDataException ex) {
			Logger.error(ex);
			throw new ModuleException("Module Schema is not valid");
		} catch (IOException ex) {
			throw new ModuleException("Could not load Module Schema from kernel resources");
		}
		ArrayList<String> loadedModules = new ArrayList<String>();
		ArrayList<String> moduleNs = new ArrayList<String>();
		while(moduleEnum.hasMoreElements()) {
			URL newModuleURL = moduleEnum.nextElement();
			try {
				String moduleXML = FileStringUtility.url2String(newModuleURL);
				String errors = moduleValidator.validate(moduleXML);
				if (errors.length() > 0)
					throw new ModuleException("Module XML found at "+newModuleURL+" was not valid: "+errors);
				Module newModule = (Module)Gateway.getMarshaller().unmarshall(moduleXML);
				Resource.addModuleBaseURL(newModule.ns, newModule.resURL);
				modules.add(newModule);
				modulesXML.put(newModule.ns, moduleXML);
				if (loadedModules.contains(newModule.getName())) throw new ModuleException("Module name clash: "+newModule.getName());
				if (moduleNs.contains(newModule.getNs())) throw new ModuleException("Module namespace clash: "+newModule.getNs());
				Logger.debug(4, "Module found: "+newModule.getNs()+" - "+newModule.getName());
				loadedModules.add(newModule.getName()); moduleNs.add(newModule.getNs());
				Properties modProp = newModule.getProperties(isServer);
		        for (Enumeration<?> e = modProp.propertyNames(); e.hasMoreElements();) {
		            String propName = (String)e.nextElement();
		            props.put(propName, modProp.get(propName));
		        }
			} catch (ModuleException e) {
				Logger.error("Could not load module description from "+newModuleURL);
				throw e;
			} catch (Exception e) {
				Logger.error(e);
				throw new ModuleException("Could not load module.xml from "+newModuleURL);
			}
		}
		
		Logger.debug(5, "Checking dependencies");
		//TODO: order the importing so each modules dependencies are imported ahead of it
		boolean depFailed = false;
		for (Module thisMod : modules) {
			ArrayList<String> deps = thisMod.getDependencies();
			for (String dep : deps) {
				if (!loadedModules.contains(dep)) {
					Logger.error("UNMET MODULE DEPENDENCY: "+thisMod.getName()+" requires "+dep);
					depFailed = true;
				}
			}
		}
		if (depFailed) Logger.die("Unmet module dependencies. Cannot continue");
	}

	public String getModuleVersions() {
		StringBuffer ver = new StringBuffer();
		for (Module thisMod : modules) {
			if (ver.length()>0) ver.append("; ");
			ver.append(thisMod.getName()+" ("+thisMod.getVersion()+")");
		}
		return ver.toString();
	}
	
	
	public Properties getAllModuleProperties() {
		return props;
	}
	
	public void runScripts(String event) {
		for (Module thisMod : modules) {
			try {
				thisMod.runScript(event, isServer);
			} catch (ScriptingEngineException e) {
				Logger.error(e);
				Logger.die(e.getMessage());
			}
		}
	}
	
	public void registerModules() throws ObjectNotFoundException, ObjectCannotBeUpdated, CannotManageException, ObjectAlreadyExistsException, NoSuchAlgorithmException {
		ItemProxy serverEntity = (ItemProxy)Gateway.getProxyManager().getProxy(new DomainPath("/servers/"+Gateway.getProperty("ItemServer.name")));
		Logger.debug(3, "Registering modules");
		for (Module thisMod : modules) {
			Logger.msg("Registering module "+thisMod.getName());
			thisMod.importAll(serverEntity, modulesXML.get(thisMod.ns));
			Logger.msg("Module "+thisMod.getName()+" registered");
		}
	}

	public void dumpModules() {
		for (Module thisMod : modules) {
			try {
				FileStringUtility.string2File(thisMod.getName()+".xml", Gateway.getMarshaller().marshall(thisMod));
			} catch (Exception e) {
				Logger.error(e);
			}
		}
		
	}
}