1 17 package org.apache.forrest.eclipse.job; 18 19 import java.io.File ; 20 import java.io.FileNotFoundException ; 21 import java.io.IOException ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.ListIterator ; 27 import java.util.Vector ; 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 47 public abstract class ForrestJob extends Job { 48 private static final String CONCURRENT_ANT_BUILDS = "Concurrent Ant builds are possible if you specify to build in a separate JRE."; 49 50 private static final String VALIDATION_ERROR_MESSAGE = "Could not validate document"; 51 52 private static final String PLUGIN_INSTALL_FAILURE = "Unable to install required plugins"; 53 54 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 workingDir; 66 67 71 public ForrestJob(String name) { 72 super(name); 73 74 ForrestPlugin plugin = ForrestPlugin.getDefault(); 75 URL urlPluginDir = plugin.getBundle().getEntry("/"); 76 Bundle bundle = Platform.getBundle(ForrestPlugin.ID); 77 URL log4jConf = Platform.find(bundle, new Path("conf/log4j.xml")); 78 DOMConfigurator.configure(log4jConf); 79 } 80 81 91 protected IStatus runAnt(IProgressMonitor monitor, String 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 fhome = ForrestPlugin.getDefault().getPluginPreferences() 100 .getString(ForrestPreferences.FORREST_HOME); 101 String 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 userMsg; 114 String errMsg = e.getMessage(); 115 if (errMsg.indexOf(VALIDATION_ERROR_MESSAGE) > 0) { 116 String 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 [] getAntClasspath() { 145 Vector vctURLs = new Vector (); 146 String forrestHome = ForrestManager.FORREST_HOME; 147 148 try { 149 vctURLs.add(new File (ForrestManager.FORREST_ANTTASK_CLASSES).toURL()); 152 ArrayList fileList = ForrestManager.getLibFiles(ForrestManager.FORREST_ANT); 153 ListIterator itr = fileList.listIterator(); 154 while (itr.hasNext()) { 155 vctURLs.add(((File )itr.next()).toURL()); 156 } 157 158 String className = this.getClass().getName(); 160 if (!className.startsWith("/")) { 161 className = "/" + className; 162 } 163 className = className.replace('.', '/'); 164 String classLocation = this.getClass().getClassLoader().getResource(className + ".class").toExternalForm(); 165 URL classURL = Platform.resolve(new URL (classLocation.substring(0, classLocation.indexOf(className)))); 166 vctURLs.add(classURL); 167 168 URL installURL = Platform.getBundle(ForrestPlugin.ID).getEntry("/"); 170 String location = Platform.resolve(installURL).getFile(); 171 fileList = ForrestManager.getLibFiles(location); 172 itr = fileList.listIterator(); 173 while (itr.hasNext()) { 174 vctURLs.add(((File )itr.next()).toURL()); 175 } 176 177 File [] files = ForrestManager.getClasspathFiles(); 179 URL [] urls = new URL [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 e) { 185 logger.error("getClasspathURLS()", e); 186 187 e.printStackTrace(); 189 } catch (MalformedURLException e) { 190 logger.error("getClasspathURLS()", e); 191 192 e.printStackTrace(); 194 } catch (IOException e) { 195 logger.error("getClasspathURLS(), cannot resolve URL", e); 196 197 e.printStackTrace(); 199 } 200 201 URL [] urls = new URL [vctURLs.size()]; 202 URL [] returnURLArray = (URL []) vctURLs.toArray(urls); 203 return returnURLArray; 204 } 205 } | Popular Tags |