View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2009 Ordina and committers to Mod4j
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *     Ordina - initial implementation
10   *******************************************************************************/
11  package org.mod4j.common.generator.admin;
12  
13  import java.io.File;
14  import java.util.Collection;
15  import java.util.Collections;
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import org.mod4j.dslcommon.generator.helpers.ProjectProperties;
20  
21  public class Mod4jTracker {
22  
23      static private Mod4jTracker mod4jTracker = null;
24  
25      private Map<String, ProjectTrack> projects = null;
26  
27      private ProjectTrack currentProject = null;
28  
29      /**
30       * @return the currentProject
31       */
32      public ProjectTrack getCurrentProject() {
33          return currentProject;
34      }
35  
36      /**
37       * @return The collection of all Project Tracks
38       */
39      public Collection<ProjectTrack> getProjects() {
40          return Collections.unmodifiableCollection(projects.values());
41      }
42  
43      public void clear() {
44          projects = new HashMap<String, ProjectTrack>();
45          currentProject = null;
46      }
47  
48      /**
49       * @return the currentTrack
50       */
51      public FileTrack getCurrentTrack() {
52          return currentProject.getCurrentFileTrack();
53      }
54  
55      /**
56       * @return Return the singleton instance of this class
57       */
58      static public Mod4jTracker getFileTracker() {
59          if (mod4jTracker == null) {
60              mod4jTracker = new Mod4jTracker();
61          }
62          return mod4jTracker;
63      }
64  
65      private Mod4jTracker() {
66          projects = new HashMap<String, ProjectTrack>();
67      }
68  
69      /**
70       * Notify the ExtensionPointtracker that 'resource'is started This used by the Maven plugin
71       * 
72       * @param resource
73       */
74      public void initResource(String resource) {
75          currentProject = findOrCreateProject(resource);
76          currentProject.setApplicationPath(resource);
77          currentProject.setCurrentFileTrack(currentProject.getTrack(resource));
78      }
79  
80      /**
81       * Notify the ExtensionPointtracker that code will be generated for 'resource' This is used by the Eclipse Mod4j
82       * Builder and set the current project such that the Xpand templates will refer to the correct template.
83       * 
84       * @param resource
85       */
86      public void initResource(String resource, String applicationPath, String projectPath) {
87          System.err.println("Mod4jTracker initResource [" + resource + "] [" + applicationPath + "] [" + projectPath
88                  + "]");
89          currentProject = findOrCreateProject(projectPath);
90          currentProject.setApplicationPath(applicationPath);
91          currentProject.setCurrentFileTrack(currentProject.getTrack(resource));
92      }
93  
94      /**
95       * Notify the ExtensionPointtracker that 'resource'is finished
96       * 
97       * @param resource
98       */
99      public void finishResource(String resource, String applicationPath, String projectPath) {
100         // System.err.println("FINISH FileTracker");
101         for (GeneratedFile gen : getCurrentTrack().getExtensionFiles()) {
102             if (gen.isChanged()) {
103                 // System.err.println("GENERATED : " + gen.getSourcePath());
104                 String name = gen.getSourcePath();
105                 name = gen.getOwner().getProject().getApplicationPath() + "/" + name;
106                 File file = new File(name);
107                 // Date modified = new Date(file.lastModified());
108                 // System.err.println(DateFormat.getInstance().format(modified)
109                 // + " [" + name + "]");
110                 gen.setModifiedDate(file.lastModified());
111                 gen.setChanged(false);
112             } else if (gen.isRetained()) {
113                 // System.err.println("KEEPING : " + gen.getSourcePath());
114                 gen.setRetained(false);
115             } else {
116                 // System.err.println("REMOVED : " + gen.getSourcePath());
117             }
118         }
119         for (GeneratedFile gen : getCurrentTrack().getGeneratedFiles()) {
120             if (gen.isChanged()) {
121                 // System.err.println("! GENERATED : " + gen.getSourcePath());
122                 String name = gen.getSourcePath();
123                 name = gen.getOwner().getProject().getApplicationPath() + "/" + name;
124                 File file = new File(name);
125                 // Date modified = new Date(file.lastModified());
126                 // System.err.println(DateFormat.getInstance().format(modified)
127                 // + " [" + name + "]");
128                 gen.setModifiedDate(file.lastModified());
129                 gen.setChanged(false);
130             } else if (gen.isRetained()) {
131                 // System.err.println("! KEEPING : " + gen.getSourcePath());
132                 gen.setRetained(false);
133             } else {
134                 // System.err.println("! REMOVED : " + gen.getSourcePath());
135             }
136         }
137     }
138 
139     /**
140      * Add the project track to the list of managed projects. Overwrite any project with the same path that is already
141      * in the managed collection
142      * 
143      * @param projectTrack
144      *            The new projectTrack to add
145      */
146     public void addProjectTrack(ProjectTrack projectTrack) {
147         projects.put(projectTrack.getProjectPath(), projectTrack);
148     }
149 
150     /**
151      * Find the ProjectTrack with the given projectPath. If it does not exist, create a new one.
152      * 
153      * @param projectPath
154      * @return
155      */
156     private ProjectTrack findOrCreateProject(String projectPath) {
157         ProjectTrack result = projects.get(projectPath);
158         if (result == null) {
159             result = new ProjectTrack(projectPath);
160             projects.put(projectPath, result);
161         }
162         return result;
163     }
164 
165     /**
166      * The file 'filename' is to be generated within 'moduleName'. the name of the resulting file will be returned and
167      * stored in the file tracker. Static, because this is used in Xpand templates.
168      * 
169      * @param moduleName
170      * @param filename
171      * @return
172      */
173     static public String generate(String moduleName, String filename) {
174         // System.err.println("Generate [" + filename + "]");
175         String result = null;
176         if (filename.endsWith(".java")) {
177             result = moduleName + "/" + ProjectProperties.getSrcGenPath() + "/" + filename;
178         } else if (filename.equals("pom.xml")) { // always in root of module
179             result = moduleName + "/" + filename;
180         } else {
181             result = moduleName + "/" + ProjectProperties.getResourceGenPath() + "/" + filename;
182         }
183         GeneratedFile file = getFileTracker().getCurrentTrack().generatedFile(result);
184         file.setChanged(true);
185         file.setRetained(false);
186         return result;
187     }
188 
189     /**
190      * Static, because this is used in Xpand templates.
191      * 
192      * @param moduleName
193      * @param filename
194      * @return
195      */
196     static public String extend(String moduleName, String filename) {
197         // System.err.println("Extend [" + filename + "]");
198         String result = getDefaultModuleManFilePath(moduleName, filename);
199         GeneratedFile file = getFileTracker().getCurrentTrack().extensionFile(result);
200         file.setChanged(true);
201         file.setRetained(false);
202         return result;
203     }
204     
205     /**
206      * Static, because this is used in Xpand templates.
207      * 
208      * @param moduleName
209      * @param subFolderPath
210      * @param fileName
211      * @return
212      */
213     static public String extend(final String moduleName, final String subFolderPath, final String fileName) {
214         
215         String result = getFilePath(moduleName, subFolderPath, fileName);
216         GeneratedFile file = getFileTracker().getCurrentTrack().extensionFile(result);
217         file.setChanged(true);
218         file.setRetained(false);
219         return result;
220     }
221 
222     /**
223      * Static, because this is used in Xpand templates.
224      * 
225      * @param moduleName
226      * @param filename
227      * @return
228      */
229     static public String retain(String moduleName, String filename) {
230         // System.err.println("Retain [" + filename + "]");
231         String result = getDefaultModuleManFilePath(moduleName, filename);
232         GeneratedFile file = getFileTracker().getCurrentTrack().extensionFile(result);
233         file.setChanged(false);
234         file.setRetained(true);
235         return result;
236     }
237 
238     /**
239      * Static, because this is used in Xpand templates.
240      * 
241      * @param moduleName
242      * @param subFolderPath
243      * @param filename
244      * @return
245      */
246     static public String retain(final String moduleName, final String subFolderPath, final String filename) {
247         // System.err.println("Retain [" + filename + "]");
248         String result = getFilePath(moduleName, subFolderPath, filename);
249         GeneratedFile file = getFileTracker().getCurrentTrack().extensionFile(result);
250         file.setChanged(false);
251         file.setRetained(true);
252         return result;
253     }
254     
255     /**
256      * Static, because this is used in Xpand templates.
257      * 
258      * @param moduleName
259      * @param filename
260      * @return
261      */
262     static public String fullExtendPath(String moduleName, String filename) {
263         String result = getDefaultModuleManFilePath(moduleName, filename);
264         return ProjectProperties.getApplicationPath() + "/" + result;
265     }
266     
267     /**
268      * Static, because this is used in Xpand templates.
269      * 
270      * @param moduleName
271      * @param filename
272      * @return
273      */
274     static public String fullExtendPath(String moduleName, String subFolderPath, String filename) {
275         String result = getFilePath(moduleName, subFolderPath, filename);
276         return ProjectProperties.getApplicationPath() + "/" + result;
277     }
278 
279     /**
280      * Static, because this is used in Xpand templates.
281      * 
282      * @param moduleName
283      * @param filename
284      * @return
285      */
286     static public String fullGeneratePath(String moduleName, String filename) {
287         String result = moduleName + "/" + ProjectProperties.getSrcGenPath() + "/" + filename;
288         return ProjectProperties.getApplicationPath() + "/" + result;
289     }
290 
291     /**
292      * Static, because this is used in Xpand templates.
293      * 
294      * @param moduleName
295      * @param filename
296      * @return
297      */
298     static public String fullGenerateResourcePath(String moduleName, String filename) {
299         String result = moduleName + "/" + ProjectProperties.getResourceGenPath() + "/" + filename;
300         return ProjectProperties.getApplicationPath() + "/" + result;
301     }
302 
303     /**
304      * Determine the default file-path for manual maintained sources or resources, based on the given fileName and moduleName.
305      * E.g. <br>
306      * <code>MyApp-domain/src/main/resources/filenname.xml</code>. Or <br>
307      * <code>MyApp-domain/src/main/java/filename.java</code>
308      * 
309      * @param moduleName
310      * @param fileName
311      * @return The path to the given fileName.
312      */
313     static public String getDefaultModuleManFilePath(String moduleName, String fileName) {
314         String result = null;
315         String prefix;
316         if ((moduleName == null) || (moduleName.length() == 0)) {
317             prefix = "";
318         } else {
319             prefix = moduleName + "/";
320         }
321         if (fileName.endsWith(".java")) {
322             result = prefix + ProjectProperties.getSrcManPath() + "/" + fileName;
323         } else if (fileName.equals("pom.xml")) { // always in root of module
324             result = prefix + fileName;
325         } else {
326             result = prefix + ProjectProperties.getResourceManPath() + "/" + fileName;
327         }
328         return result;
329     }
330 
331     /**
332      * Returns the file-path, based on the given moduleName, subFolderPath and fileName.
333      * E.g. <br>
334      * <code>MyApp-domain/src/main/webapp/WEB-INF/applicationContext.xml</code>.
335      * 
336      * @param moduleName
337      * @param subFolderPath
338      * @param fileName
339      * @return The path to the given fileName.
340      */
341     static public String getFilePath(String moduleName, String subFolderPath, String fileName) {
342         String result = "";
343         
344         if ((moduleName != null) && (moduleName.length() > 0)) {
345             result = result + moduleName;
346         }
347         if ((subFolderPath != null) && (subFolderPath.length() > 0)) {
348             result = result + "/" + subFolderPath;
349         }
350         if (fileName != null && fileName.length() > 0) {
351             result = result +  "/" + fileName;
352         } 
353         return result;
354     }
355 }