1 package net.sf.mmapps.modules.config;
2
3 import java.io.*;
4 import java.util.Enumeration;
5 import java.util.jar.JarFile;
6 import java.util.zip.ZipEntry;
7
8 import simplexml.XMLElement;
9
10 /***
11 * class to read a configuration sorted in a jar file
12 * @author keesj
13 */
14 public class JarConfigurationReader {
15
16 public static String FILE_SEPARATOR = "/";
17 public static String CONFIGURATION_DIR = "config/";
18
19 /***
20 * @param file the jar file
21 * @return a Configuration containing all the application configurations
22 * @throws Exception
23 */
24 public static Configuration getConfiguration(File file) throws Exception {
25 JarFile jarFile = new JarFile(file);
26 Configuration config = new Configuration();
27 Enumeration enumeration = jarFile.entries();
28 while (enumeration.hasMoreElements()) {
29 ZipEntry entry = (ZipEntry) enumeration.nextElement();
30 if (!entry.isDirectory() && entry.getName().startsWith(CONFIGURATION_DIR) && entry.getName().endsWith(".xml")) {
31 String data = readFile(jarFile, entry);
32 XMLElement xmle = new XMLElement();
33 xmle.parseString(data);
34 if (xmle.getTagName().equals("application")) {
35 ApplicationConfiguration appConfig = ConfigurationXMLReader.createApplicationConfiguration(config, data.toString());
36 System.err.println("adding application " + appConfig.getName());
37 config.addApplicationConfiguration(appConfig);
38
39
40
41 Strings builders = appConfig.getRequiredNodeManagers();
42 for (int x = 0; x < builders.size(); x++) {
43 String builder = builders.getString(x);
44 String path = "config/applications/" + appConfig.getName() + "/builders/" + builder + ".xml";
45 ZipEntry en = jarFile.getEntry(path);
46 if (en != null) {
47 XMLElement builderXML = new XMLElement();
48 String builderData = readFile(jarFile, en);
49 builderXML.parseString(builderData);
50 if (builderXML.getTagName().equals("builder")) {
51 NodeManagerConfiguration nodeManagerConfiguration =
52 ConfigurationXMLReader.createNodeManagerConfiguration(config,builderData);
53 System.err.println("adding nodemanager " + nodeManagerConfiguration.getName());
54 appConfig.addNodeManagerConfiguration(nodeManagerConfiguration);
55 }
56 }
57 }
58 }
59 }
60 }
61 return config;
62 }
63
64 private static String readFile(JarFile jarFile, ZipEntry entry) throws IOException {
65 StringBuffer data = new StringBuffer();
66 InputStream i = jarFile.getInputStream(entry);
67 BufferedReader br = new BufferedReader(new InputStreamReader(i));
68 String line = null;
69 while ((line = br.readLine()) != null) {
70 data.append(line);
71 data.append("\n");
72 }
73 return data.toString();
74 }
75 }