KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > eclipse > job > ForrestManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation or its licensors,
3  * as applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.forrest.eclipse.job;
18
19 import java.io.File JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.apache.forrest.eclipse.ForrestPlugin;
30 import org.apache.forrest.eclipse.preference.ForrestPreferences;
31 import org.apache.log4j.Logger;
32 import org.apache.tools.ant.Project;
33 import org.apache.tools.ant.taskdefs.LoadProperties;
34 import org.eclipse.core.runtime.jobs.Job;
35 import org.eclipse.debug.core.DebugException;
36 import org.eclipse.debug.core.ILaunch;
37
38 /**
39  * Manages instances of Forrest that are running on the local server.
40  */

41 public class ForrestManager {
42     /** Name of the skin to use for building the site without navigation */
43     private static final String JavaDoc SKIN_PLAIN = "plain-dev";
44     /**
45      * Logger for this class
46      */

47     private static final Logger logger = Logger.getLogger(ForrestManager.class);
48
49     /** The command for building the static site */
50     public static final String JavaDoc COMMAND_BUILD = "site";
51
52     /** The command for running the site server */
53     public static final String JavaDoc COMMAND_START = "run";
54
55     /** The command for stopping the site server */
56     public static final String JavaDoc COMMAND_STOP = "stop";
57
58     /** The command for building the site in HTML with no navigation */
59     public static final String JavaDoc COMMAND_BUILD_PLAIN = "buildPlain";
60     
61     private static final int UNKOWN_COMMAND = 0;
62
63     private static final int BUILD = 1;
64
65     private static final int START = 2;
66
67     private static final int STOP = 3;
68
69     private static final ForrestManager INSTANCE = new ForrestManager();
70
71     // FIXME: dir configuration should be in preferences and should be set by
72
// reading forrest.properties and forrest.build.xml (where some defaults are set)
73

74     public static final String JavaDoc FORREST_HOME = ForrestPlugin.getDefault()
75             .getPluginPreferences().getString(ForrestPreferences.FORREST_HOME);
76     
77     public static final String JavaDoc FORREST_CORE_LIB = FORREST_HOME
78             + File.separator + "lib" + File.separator + "core" + File.separator;
79
80     public static final String JavaDoc FORREST_ENDORSED_LIB = FORREST_HOME
81             + File.separator + "lib" + File.separator + "endorsed"
82             + File.separator;
83
84     public static final String JavaDoc FORREST_OPTIONAL_LIB = FORREST_HOME
85             + File.separator + "lib" + File.separator + "optional"
86             + File.separator;
87
88     public static final String JavaDoc JETTY_LIB = FORREST_HOME
89             + File.separator + "tools" + File.separator + "jetty"
90             + File.separator;
91     
92     public static final String JavaDoc FORREST_CORE_WEBAPP = FORREST_HOME
93             + File.separatorChar + "main" + File.separatorChar + "webapp";
94     
95     public static final String JavaDoc FORREST_JAR = FORREST_HOME
96     + File.separatorChar + "build" + File.separatorChar + "xml-forrest.jar";
97
98     public static final String JavaDoc FORREST_CLASSES = FORREST_CORE_WEBAPP
99             + File.separator + "WEB-INF" + File.separator + "classes" + File.separator;
100
101     public static final String JavaDoc FORREST_LIB = FORREST_HOME
102             + File.separator + "build" + File.separator + "xml-forrest.jar";
103
104     public static final String JavaDoc FORREST_ANT = FORREST_HOME
105             + File.separator + "tools" + File.separator + "ant" + File.separator;
106     
107     public static final String JavaDoc FORREST_ANTTASK_CLASSES = FORREST_HOME
108             + File.separator + "tools" + File.separator + "anttasks" + File.separator;
109     
110     public static final String JavaDoc FORREST_PLUGINS = FORREST_HOME
111             + File.separator + "build" + File.separator + "plugins" + File.separator;
112
113     public static final String JavaDoc FORREST_PLUGINS_LIB = FORREST_HOME
114             + File.separator + "build" + File.separator + "plugins"
115             + File.separator + "lib" + File.separator;
116
117     private static final String JavaDoc FORREST_DEFAULT_PROPERTIES_FILE = FORREST_CORE_WEBAPP
118     + File.separatorChar + "default-forrest.properties";
119     
120     private static ILaunch serverLaunch;
121     
122     /**
123      * Create a manager, this is not intended to be called from your code. Use
124      * Manager.getInstance() instead.
125      */

126     private ForrestManager() {
127         super();
128     }
129
130     /**
131      * Get the properties for a working directory
132      * @param workingDir the working dir of the project
133      * @return the properties for this project
134      * @throws IOException if unable to read either the defaults file or the project file
135      * @TODO Cache the project properties file
136      * @refactor Move project creation code to own method
137      */

138     public Hashtable JavaDoc getProperties(String JavaDoc workingDir) throws IOException JavaDoc {
139         // TODO: keep a record of the projects created, this is how we will cache the properties
140
Project project = new Project();
141         project.setName(workingDir);
142         
143         LoadProperties props = new LoadProperties();
144         props.setProject(project);
145         
146         project.setProperty("project.home", workingDir);
147         
148         props.setSrcFile(new File JavaDoc(workingDir + File.separatorChar + "forrest.properties"));
149         props.execute();
150         
151         props.setSrcFile(new File JavaDoc(FORREST_DEFAULT_PROPERTIES_FILE));
152         props.execute();
153         
154         project.setProperty("forrest.home", FORREST_HOME);
155         project.setProperty("forrest.plugins", FORREST_PLUGINS);
156         
157         return project.getProperties();
158     }
159     
160
161     public static ForrestManager getInstance() {
162         return ForrestManager.INSTANCE;
163     }
164
165     /**
166      * Create a Forrest Job.
167      *
168      * @param workingDir -
169      * the directory on which the job is to work
170      * @param cmd -
171      * the command the job is to carry out
172      * @return
173      */

174     public Job getForrestJob(String JavaDoc workingDir, String JavaDoc cmd) {
175         Job theJob;
176         if (cmd.equals(COMMAND_STOP)) {
177             theJob = new ForrestStopper(workingDir);
178         } else if (cmd.equals(COMMAND_START)) {
179             theJob = new ForrestRunner(workingDir);
180         } else if (cmd.equals(COMMAND_BUILD)) {
181             theJob = new ForrestBuilder(workingDir);
182         } else if (cmd.equals(COMMAND_BUILD_PLAIN)) {
183             theJob = new ForrestBuilder(workingDir, SKIN_PLAIN);
184         } else {
185             theJob = null;
186         }
187         return theJob;
188     }
189
190     /**
191      * Get an array of Files to be placed in the
192      * classpath for Forrest.
193      *
194      * @return an array of Files
195      */

196     protected static File JavaDoc[] getClasspathFiles() {
197         Vector JavaDoc vctFiles = new Vector JavaDoc();
198
199         try {
200             // FIXME: cache the classpath
201
// add Forrest classes
202
vctFiles.add(new File JavaDoc(FORREST_CLASSES));
203             vctFiles.add(new File JavaDoc(FORREST_JAR));
204             // add core libs
205
vctFiles.addAll(getLibFiles(FORREST_CORE_LIB));
206             // add endorsed libs
207
vctFiles.addAll(getLibFiles(FORREST_ENDORSED_LIB));
208             // add optional libs
209
vctFiles.addAll(getLibFiles(FORREST_OPTIONAL_LIB));
210             // add plugin libs
211
vctFiles.addAll(getLibFiles(FORREST_PLUGINS_LIB));
212         } catch (FileNotFoundException JavaDoc e) {
213             logger.error("getClasspathFiles()", e);
214
215             // TODO Auto-generated catch block
216
e.printStackTrace();
217         }
218
219         File JavaDoc[] files = new File JavaDoc[vctFiles.size()];
220         File JavaDoc[] returnFileArray = (File JavaDoc[]) vctFiles.toArray(files);
221         return returnFileArray;
222     }
223
224     /**
225      * Return an array of URLs that point to lib files.
226      *
227      * @param dir -
228      * directory in which to look for libs
229      * @return ArrayList of URLs
230      * @throws FileNotFoundException -
231      * if the directory doesn't exist
232      */

233     static public ArrayList JavaDoc getLibFiles(String JavaDoc dir) throws FileNotFoundException JavaDoc {
234         File JavaDoc directory = new File JavaDoc(dir);
235         ArrayList JavaDoc result = new ArrayList JavaDoc();
236         List JavaDoc dirs = Arrays.asList(directory.listFiles());
237         Iterator JavaDoc filesIter = dirs.iterator();
238         File JavaDoc file = null;
239
240         while (filesIter.hasNext()) {
241             file = (File JavaDoc) filesIter.next();
242
243             if (file.isDirectory()) {
244                 List JavaDoc deeperList = getLibFiles(file.toString());
245                 result.addAll(deeperList);
246             } else if (file.toString().endsWith(".jar")
247                     || file.toString().endsWith(".zip")) {
248                 result.add(file);
249             }
250         }
251         
252         return result;
253     }
254
255     /**
256      * Stop the server for the given project
257      *
258      * @param projectDir
259      * @throws InterruptedException
260      * @throws DebugException
261      */

262     public static void stopServer(String JavaDoc projectDir)
263             throws DebugException {
264         serverLaunch.terminate();
265     }
266
267     /**
268      * Set the server launch.
269      * @param launch - the launch
270      */

271     public static void setServerLaunch(ILaunch launch) {
272         serverLaunch = launch;
273     }
274     
275 }
Popular Tags