KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > framework > amoda > generic > core > GenericApplicationModule


1 /*
2  * Project: AMODA - Abstract Modeled Application
3  * Class: de.gulden.framework.amoda.generic.core.GenericApplicationModule
4  * Version: snapshot-beautyj-1.1
5  *
6  * Date: 2004-09-29
7  *
8  * This is a snapshot version of the AMODA 0.2 development branch,
9  * it is not released as a seperate version.
10  * For AMODA, see http://amoda.berlios.de/.
11  *
12  * This is licensed under the GNU Lesser General Public License (LGPL)
13  * and comes with NO WARRANTY.
14  *
15  * Author: Jens Gulden
16  * Email: amoda@jensgulden.de
17  */

18
19 package de.gulden.framework.amoda.generic.core;
20
21 import de.gulden.framework.amoda.generic.metadata.*;
22 import de.gulden.framework.amoda.generic.option.*;
23 import de.gulden.framework.amoda.model.core.*;
24 import de.gulden.framework.amoda.model.core.ApplicationModule;
25 import de.gulden.framework.amoda.model.data.*;
26 import java.lang.*;
27 import java.net.*;
28 import java.util.*;
29 import org.w3c.dom.*;
30
31 /**
32  * Class GenericApplicationModule.
33  *
34  * @author Jens Gulden
35  * @version snapshot-beautyj-1.1
36  */

37 public class GenericApplicationModule extends GenericFeatureGroup implements ApplicationModule {
38
39     // ------------------------------------------------------------------------
40
// --- fields ---
41
// ------------------------------------------------------------------------
42

43     protected Document configurationDocument;
44
45     protected boolean configurationDocumentSet = false;
46
47
48     // ------------------------------------------------------------------------
49
// --- methods ---
50
// ------------------------------------------------------------------------
51

52     public void init() {
53         // your code here
54
}
55
56     public void start() {
57         for (Iterator it = getModules().iterator(); it.hasNext(); ) {
58             ApplicationModule m = (ApplicationModule)it.next();
59             m.start();
60         }
61     }
62
63     public void stop() {
64         for (Iterator it = getModules().iterator(); it.hasNext(); ) {
65             ApplicationModule m = (ApplicationModule)it.next();
66             m.stop();
67         }
68     }
69
70     public void destroy() {
71         for (Iterator it = getModules().iterator(); it.hasNext(); ) {
72             ApplicationModule m = (ApplicationModule)it.next();
73             m.destroy();
74         }
75     }
76
77     public Collection getModules() {
78         return getAll(ApplicationModule.class,false).values();
79     }
80
81     public ApplicationModule getModule(String JavaDoc id) {
82         return (ApplicationModule)get(id);
83     }
84
85     public void loadModule(String JavaDoc id, String JavaDoc classname) {
86         // create instance of module
87
Class JavaDoc cl;
88         try {
89             cl = Class.forName(classname);
90         } catch (ClassNotFoundException JavaDoc cnfe) {
91             getApplication().error("cannot load application module '"+classname+"'",cnfe);
92             return;
93         }
94         GenericApplicationModule module;
95         try {
96             module = (GenericApplicationModule)cl.newInstance();
97         } catch (Exception JavaDoc e) {
98             getApplication().error("cannot create instance of module '"+classname+"' as GenericApplicationModule", e);
99             return;
100         }
101         module.setId(id);
102         this.add(module); // sets module.parent, too
103
((GenericApplication)getApplication()).initModule(module);
104     }
105
106     public Document getConfigurationDocument() {
107         if (!configurationDocumentSet) { // has not been initialized by calling setConfiguration(..)
108
// attempt to load from a file named as the class + ".xml" from the classpath
109
URL res = getDefaultConfigurationDocumentResource();
110             if (res==null) {
111                 getApplication().fatalError("cannot initialize application XML configuration for class "+this.getClass().getName(), null);
112             }
113             setConfigurationDocument(res);
114             configurationDocumentSet = true; // always set even if load failed, to avoid reload attempt and return null
115
}
116         return configurationDocument;
117     }
118
119     public void setConfigurationDocument(Document _configurationDocument) {
120         configurationDocumentSet = true;
121         configurationDocument = _configurationDocument;
122     }
123
124     public void setConfigurationDocument(URL url) {
125         try {
126             String JavaDoc u=url.toExternalForm();
127             getApplication().log("loading configuration XML document '"+u+"'");
128             Document d=((GenericApplication)getApplication()).getGenericApplicationEnvironment().getFactory().getDocumentBuilder().parse(u);
129             setConfigurationDocument(d);
130         } catch (Exception JavaDoc e) {
131             getApplication().fatalError("cannot initialize application XML configuration",e);
132         }
133     }
134
135     public void setConfigurationDocument(String JavaDoc resource) {
136         try {
137             setConfigurationDocument(de.gulden.util.Toolbox.getResourceURL(resource));
138         } catch (java.net.MalformedURLException JavaDoc mue) {
139             getApplication().error(mue);
140         }
141     }
142
143     public CompositeMember get(String JavaDoc id) {
144         CompositeMember m = super.get(id);
145         if ((m.getParent()==null) && this.all.containsValue(m)) {
146             // if direct child, setParent() might not have yet been invoked (XML-parser couldn't know)
147
m.setParent(this);
148         }
149         return m;
150     }
151
152     protected URL getDefaultConfigurationDocumentResource() {
153         return GenericApplicationEnvironment.getXMLResourceForClass(this.getClass());
154     }
155
156 } // end GenericApplicationModule
157
Popular Tags