KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * Copyright 2002-2005 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.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.ListIterator 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.log4j.xml.DOMConfigurator;
33 import org.apache.tools.ant.Project;
34 import org.eclipse.ant.core.AntRunner;
35 import org.eclipse.core.runtime.CoreException;
36 import org.eclipse.core.runtime.IProgressMonitor;
37 import org.eclipse.core.runtime.IStatus;
38 import org.eclipse.core.runtime.Path;
39 import org.eclipse.core.runtime.Platform;
40 import org.eclipse.core.runtime.Status;
41 import org.eclipse.core.runtime.jobs.Job;
42 import org.osgi.framework.Bundle;
43
44 /**
45  * An abstract Forrest job that provides utility methods for handling Eclipse based Forrest Jobs.
46  */

47 public abstract class ForrestJob extends Job {
48     private static final String JavaDoc CONCURRENT_ANT_BUILDS = "Concurrent Ant builds are possible if you specify to build in a separate JRE.";
49
50     private static final String JavaDoc VALIDATION_ERROR_MESSAGE = "Could not validate document";
51     
52     private static final String JavaDoc PLUGIN_INSTALL_FAILURE = "Unable to install required plugins";
53
54     /**
55      * Logger for this class
56      */

57     protected static final Logger logger = Logger.getLogger(ForrestJob.class);
58
59     public static final int EXCEPTION_UNIDENTIFIED = 1001;
60
61     public static final int EXCEPTION_VALIDATION = 1010;
62
63     public static final int EXCEPTION_ANT_RUNNING = 1020;
64     
65     protected String JavaDoc workingDir;
66
67     /**
68      * Create a new Forrest Job.
69      * @param name of the job
70      */

71     public ForrestJob(String JavaDoc name) {
72         super(name);
73
74         ForrestPlugin plugin = ForrestPlugin.getDefault();
75         URL JavaDoc urlPluginDir = plugin.getBundle().getEntry("/");
76         Bundle bundle = Platform.getBundle(ForrestPlugin.ID);
77         URL JavaDoc log4jConf = Platform.find(bundle, new Path("conf/log4j.xml"));
78         DOMConfigurator.configure(log4jConf);
79     }
80
81     /**
82      * Run ant as a normal executable, that is wait for completion and retuen a
83      * status.
84      *
85      * @param monitor -
86      * the monitor to report execution progress
87      * @param cmdString -
88      * the command string to pass to ant
89      * @return Status of the execution.
90      */

91     protected IStatus runAnt(IProgressMonitor monitor, String JavaDoc cmdString) {
92         if (logger.isDebugEnabled()) {
93             logger.debug("runAnt(IProgressMonitor, String) - start");
94         }
95
96         IStatus status = Status.OK_STATUS;
97
98         if (cmdString != null) {
99             String JavaDoc fhome = ForrestPlugin.getDefault().getPluginPreferences()
100                     .getString(ForrestPreferences.FORREST_HOME);
101             String JavaDoc antFile = fhome + File.separatorChar + "main"
102                     + File.separatorChar + "forrest.build.xml";
103             AntRunner runner = new AntRunner();
104             runner.setCustomClasspath(getAntClasspath());
105             runner.addBuildListener(AntBuildListener.class.getName());
106             runner.setMessageOutputLevel(Project.MSG_INFO);
107             try {
108                 runner.setBuildFileLocation(antFile);
109                 runner.setArguments(cmdString);
110                 logger.info("Running ANT with command string = " + cmdString);
111                 runner.run(monitor);
112             } catch (CoreException e) {
113                 String JavaDoc userMsg;
114                 String JavaDoc errMsg = e.getMessage();
115                 if (errMsg.indexOf(VALIDATION_ERROR_MESSAGE) > 0) {
116                     String JavaDoc file = errMsg.substring(errMsg
117                             .indexOf(VALIDATION_ERROR_MESSAGE));
118                     userMsg = "Invalid XML Document: " + file;
119                     status = new Status(Status.ERROR, ForrestPlugin.ID,
120                             ForrestRunner.EXCEPTION_VALIDATION, userMsg, e);
121                 } else if (errMsg.endsWith(CONCURRENT_ANT_BUILDS)) {
122                     userMsg = "Can only run one Site operation at a time";
123                     status = new Status(Status.ERROR, ForrestPlugin.ID,
124                             EXCEPTION_ANT_RUNNING, userMsg, e);
125                 } else if (errMsg.indexOf(PLUGIN_INSTALL_FAILURE) >= 0) {
126                     userMsg = "Unable to install required server plugins";
127                     status = new Status(Status.ERROR, ForrestPlugin.ID,
128                             EXCEPTION_ANT_RUNNING, userMsg, e);
129                 } else {
130                     userMsg = "Forrest Server Exception";
131                     status = new Status(Status.ERROR, ForrestPlugin.ID,
132                             ForrestRunner.EXCEPTION_UNIDENTIFIED, userMsg, e);
133                 }
134                 logger.error("run(IProgressMonitor) - " + userMsg, e);
135             }
136         }
137
138         if (logger.isDebugEnabled()) {
139             logger.debug("runAnt(IProgressMonitor, String) - end");
140         }
141         return status;
142     }
143     
144     private URL JavaDoc[] getAntClasspath() {
145         Vector JavaDoc vctURLs = new Vector JavaDoc();
146         String JavaDoc forrestHome = ForrestManager.FORREST_HOME;
147
148         try {
149             // FIXME: cache the classpath
150
// add Forrest ANT jars
151
vctURLs.add(new File JavaDoc(ForrestManager.FORREST_ANTTASK_CLASSES).toURL());
152             ArrayList JavaDoc fileList = ForrestManager.getLibFiles(ForrestManager.FORREST_ANT);
153             ListIterator JavaDoc itr = fileList.listIterator();
154             while (itr.hasNext()) {
155                 vctURLs.add(((File JavaDoc)itr.next()).toURL());
156             }
157             
158             // Add the path to the Plugin classes
159
String JavaDoc className = this.getClass().getName();
160             if (!className.startsWith("/")) {
161                 className = "/" + className;
162             }
163             className = className.replace('.', '/');
164             String JavaDoc classLocation = this.getClass().getClassLoader().getResource(className + ".class").toExternalForm();
165             URL JavaDoc classURL = Platform.resolve(new URL JavaDoc(classLocation.substring(0, classLocation.indexOf(className))));
166             vctURLs.add(classURL);
167             
168             // Add Plugin jars
169
URL JavaDoc installURL = Platform.getBundle(ForrestPlugin.ID).getEntry("/");
170             String JavaDoc location = Platform.resolve(installURL).getFile();
171             fileList = ForrestManager.getLibFiles(location);
172             itr = fileList.listIterator();
173             while (itr.hasNext()) {
174                 vctURLs.add(((File JavaDoc)itr.next()).toURL());
175             }
176             
177             // add Forrest jars
178
File JavaDoc[] files = ForrestManager.getClasspathFiles();
179             URL JavaDoc[] urls = new URL JavaDoc[files.length];
180             for (int i = 0; i < files.length; i++) {
181                 urls[i] = files[i].toURL();
182             }
183             vctURLs.addAll(Arrays.asList(urls));
184         } catch (FileNotFoundException JavaDoc e) {
185             logger.error("getClasspathURLS()", e);
186
187             // TODO Auto-generated catch block
188
e.printStackTrace();
189         } catch (MalformedURLException JavaDoc e) {
190             logger.error("getClasspathURLS()", e);
191
192             // TODO Auto-generated catch block
193
e.printStackTrace();
194         } catch (IOException JavaDoc e) {
195             logger.error("getClasspathURLS(), cannot resolve URL", e);
196             
197             // TODO Auto-generated catch block
198
e.printStackTrace();
199         }
200
201         URL JavaDoc[] urls = new URL JavaDoc[vctURLs.size()];
202         URL JavaDoc[] returnURLArray = (URL JavaDoc[]) vctURLs.toArray(urls);
203         return returnURLArray;
204     }
205 }
Popular Tags