View Javadoc

1   package de.matthias_burbach.mosaique.core;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.FileOutputStream;
6   import java.io.InputStream;
7   import java.util.Iterator;
8   import java.util.Properties;
9   
10  import de.matthias_burbach.mosaique.core.model.DefinitionResolver;
11  import de.matthias_burbach.mosaique.core.model.JspResolver;
12  import de.matthias_burbach.mosaique.core.model.StrutsConfig;
13  import de.matthias_burbach.mosaique.core.model.TilesDefinitions;
14  import de.matthias_burbach.mosaique.core.parser.StrutsConfigParser;
15  import de.matthias_burbach.mosaique.core.parser.TilesDefinitionsParser;
16  import de.matthias_burbach.mosaique.core.util.FileUtils;
17  import de.matthias_burbach.mosaique.core.util.Log;
18  
19  /***
20   * Is the main class of the core application.
21   * <p/>
22   * Is **NOT** used when running Mosaique as Eclipse plug-in!
23   *
24   * @author Matthias Burbach
25   */
26  public class Mosaique {
27      /***
28       * The path and name of the application properties file.
29       */
30      private static final String APP_PROPERTIES_FILE =
31          "mosaique-application.properties";
32  
33      /***
34       * The path and name of the user properties file.
35       */
36      private static final String USER_PROPERTIES_FILE =
37          System.getProperty("user.home")
38              + File.separator
39              + "mosaique.properties";
40  
41      /***
42       * The user properties plus the application properties as defaults.
43       */
44      private Properties properties;
45  
46      /***
47       * The full path of the current Struts configuration file.
48       */
49      private String strutsConfigFile;
50  
51      /***
52       * The internal representation of the current Struts configuration file.
53       */
54      private StrutsConfig strutsConfig;
55  
56      /***
57       * The log.
58       */
59      private Log log;
60  
61      /***
62       * Constructs and starts the application.
63       *
64       * @param log The log for messages.
65       * @throws Exception if anything goes unexpectedly wrong
66       */
67      public Mosaique(final Log log) throws Exception {
68          this.log = log;
69          startApplication();
70      }
71  
72      /***
73       * Initializes the application on start up.
74       */
75      private void startApplication() {
76          /*
77           * Create application properties
78           */
79          Properties applicationProperties = new Properties();
80  
81          /*
82           * Now load application properties from file
83           */
84          try {
85              InputStream in =
86                  getClass().getResourceAsStream(APP_PROPERTIES_FILE);
87              applicationProperties.load(in);
88              in.close();
89          } catch (Exception e) {
90              e.printStackTrace();
91          }
92  
93          /*
94           * Create user properties with application properties as defaults
95           */
96          properties = new Properties(applicationProperties);
97  
98          /*
99           * Now load user properties from file
100          */
101         try {
102             log.log(Log.SEVERITY_INFO, "About to load " + USER_PROPERTIES_FILE);
103             FileInputStream in = new FileInputStream(USER_PROPERTIES_FILE);
104             properties.load(in);
105             in.close();
106         } catch (Exception e) {
107             e.printStackTrace();
108         }
109     }
110 
111     /***
112      * Cleans up the application on exit.
113      */
114     public void exitApplication() {
115         FileOutputStream out;
116         try {
117             out = new FileOutputStream(USER_PROPERTIES_FILE);
118             properties.store(out, "---user properties---");
119             out.close();
120         } catch (Exception e) {
121             e.printStackTrace();
122         }
123         System.exit(0);
124     }
125 
126     /***
127      * @return The Mosaique application version.
128      */
129     public String getVersion() {
130         return properties.getProperty("version");
131     }
132 
133     /***
134      * @return The log Mosaique currently writes messages to.
135      *         Can be <code>null</code>. Can change, don't cache a reference.
136      */
137     public Log getLog() {
138         return log;
139     }
140 
141     /***
142      * @param log The log to write messages to. Can be set by the creator of
143      *            this class to define where to log to.
144      */
145     public void setLog(final Log log) {
146         this.log = log;
147     }
148 
149     /***
150      * @param key The property's key.
151      * @return The property value or <code>null</code> if the property does not
152      *         exist.
153      */
154     public String getProperty(final String key) {
155         String result = properties.getProperty(key);
156         return result;
157     }
158 
159     /***
160      * @param key The property's key. Must not be <code>null</code>.
161      * @param value The property's value. Must not be <code>null</code>.
162      */
163     public void setProperty(final String key, final String value) {
164         properties.setProperty(key, value);
165     }
166 
167     /***
168      * @return The full path of the current Struts configuration file.
169      */
170     public String getStrutsConfigFile() {
171         return strutsConfigFile;
172     }
173 
174     /***
175      * @param strutsConfigFile The full path of the current Struts configuration
176      *                         file.
177      */
178     public void setStrutsConfigFile(final String strutsConfigFile) {
179         this.strutsConfigFile = strutsConfigFile;
180     }
181 
182     /***
183      * @return The internal representation of the current Struts configuration
184      *         file.
185      */
186     public StrutsConfig getStrutsConfig() {
187         return strutsConfig;
188     }
189 
190     /***
191      * Opens and parses the the current Struts configuration file.
192      *
193      * @return The successfully opened Struts configuration.
194      * @throws Exception if anything goes wrong
195      */
196     public StrutsConfig openStrutsConfig() throws Exception {
197         StrutsConfigParser parser = new StrutsConfigParser();
198         strutsConfig =
199             parser.parse(getStrutsConfigFile(), null);
200         parseTilesDefinitions();
201         return strutsConfig;
202     }
203 
204     /***
205      * Parses the Tiles definition files found in the current Struts
206      * configuration.
207      *
208      * @throws Exception if anything goes wrong
209      */
210     private void parseTilesDefinitions() throws Exception {
211         String root = FileUtils.getPathWithoutName(getStrutsConfigFile());
212         root += "/../";
213         JspResolver.getInstance().setRoot(root);
214         DefinitionResolver resolver = DefinitionResolver.getInstance();
215         TilesDefinitionsParser parser = new TilesDefinitionsParser();
216         Iterator iter = strutsConfig.getDefinitionsConfigValues().iterator();
217         while (iter.hasNext()) {
218             String value = (String) iter.next();
219             String path = root + value;
220             TilesDefinitions tilesDefinitions = parser.parse(path, null);
221             resolver.addTilesDefinitions(tilesDefinitions);
222         }
223     }
224 }