1 package net.sf.mmapps.modules.config;
2
3 import java.io.*;
4 import java.net.URL;
5
6 import simplexml.XMLElement;
7
8 /***
9 * Util class to read an mmbase application configuration from an url(or the classpath).
10 *
11 * @author keesj
12 */
13 public class URLApplicationConfigurationReader {
14
15 /***
16 * @param url an url to the application configuration file (eg http://www.mmbase.org/applicaitons/config/applications/MyNews.xml)
17 * @return the application configration found at that url/ to load an application from the classpath use something like
18 * <code>YourClass.class.getClassLoader().getResources("config/applications/" + name + ".xml");</code> to get an url to the
19 * application xml and feed it to the method
20 * @throws Exception
21 */
22 public static ApplicationConfiguration getConfiguration(URL url) throws Exception {
23 Configuration config = new Configuration();
24 String data = readURL(url);
25 XMLElement xmle = new XMLElement();
26 xmle.parseString(data);
27 if (xmle.getTagName().equals("application")) {
28 ApplicationConfiguration appConfig = ConfigurationXMLReader.createApplicationConfiguration(config, data.toString());
29 System.err.println("adding application " + appConfig.getName());
30 config.addApplicationConfiguration(appConfig);
31
32
33
34 Strings builders = appConfig.getRequiredNodeManagers();
35 for (int x = 0; x < builders.size(); x++) {
36 String u = url.toString();
37 int index = u.lastIndexOf('/');
38 String base = u.substring(0, index);
39 String builder = builders.getString(x);
40 String path = base + "/" + appConfig.getName() + "/builders/" + builder + ".xml";
41 try {
42 XMLElement builderXML = new XMLElement();
43 String builderData = readURL(new URL(path));
44 builderXML.parseString(builderData);
45 if (builderXML.getTagName().equals("builder")) {
46 NodeManagerConfiguration nodeManagerConfiguration =
47 ConfigurationXMLReader.createNodeManagerConfiguration(config,builderData);
48 System.err.println("adding nodemanager " + nodeManagerConfiguration.getName());
49 appConfig.addNodeManagerConfiguration(nodeManagerConfiguration);
50 }
51
52 } catch (Exception e) {
53 System.err.println("missing " + path);
54 }
55 }
56 return appConfig;
57 }
58 return null;
59 }
60
61 private static String readURL(URL url) throws IOException {
62 StringBuffer data = new StringBuffer();
63 InputStream i = url.openStream();
64 BufferedReader br = new BufferedReader(new InputStreamReader(i));
65 String line = null;
66 while ((line = br.readLine()) != null) {
67 data.append(line);
68 data.append("\n");
69 }
70 return data.toString();
71 }
72
73
74 }