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.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Hashtable ; 25 import java.util.Iterator ; 26 import java.util.List ; 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.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 41 public class ForrestManager { 42 43 private static final String SKIN_PLAIN = "plain-dev"; 44 47 private static final Logger logger = Logger.getLogger(ForrestManager.class); 48 49 50 public static final String COMMAND_BUILD = "site"; 51 52 53 public static final String COMMAND_START = "run"; 54 55 56 public static final String COMMAND_STOP = "stop"; 57 58 59 public static final String 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 74 public static final String FORREST_HOME = ForrestPlugin.getDefault() 75 .getPluginPreferences().getString(ForrestPreferences.FORREST_HOME); 76 77 public static final String FORREST_CORE_LIB = FORREST_HOME 78 + File.separator + "lib" + File.separator + "core" + File.separator; 79 80 public static final String FORREST_ENDORSED_LIB = FORREST_HOME 81 + File.separator + "lib" + File.separator + "endorsed" 82 + File.separator; 83 84 public static final String FORREST_OPTIONAL_LIB = FORREST_HOME 85 + File.separator + "lib" + File.separator + "optional" 86 + File.separator; 87 88 public static final String JETTY_LIB = FORREST_HOME 89 + File.separator + "tools" + File.separator + "jetty" 90 + File.separator; 91 92 public static final String FORREST_CORE_WEBAPP = FORREST_HOME 93 + File.separatorChar + "main" + File.separatorChar + "webapp"; 94 95 public static final String FORREST_JAR = FORREST_HOME 96 + File.separatorChar + "build" + File.separatorChar + "xml-forrest.jar"; 97 98 public static final String FORREST_CLASSES = FORREST_CORE_WEBAPP 99 + File.separator + "WEB-INF" + File.separator + "classes" + File.separator; 100 101 public static final String FORREST_LIB = FORREST_HOME 102 + File.separator + "build" + File.separator + "xml-forrest.jar"; 103 104 public static final String FORREST_ANT = FORREST_HOME 105 + File.separator + "tools" + File.separator + "ant" + File.separator; 106 107 public static final String FORREST_ANTTASK_CLASSES = FORREST_HOME 108 + File.separator + "tools" + File.separator + "anttasks" + File.separator; 109 110 public static final String FORREST_PLUGINS = FORREST_HOME 111 + File.separator + "build" + File.separator + "plugins" + File.separator; 112 113 public static final String FORREST_PLUGINS_LIB = FORREST_HOME 114 + File.separator + "build" + File.separator + "plugins" 115 + File.separator + "lib" + File.separator; 116 117 private static final String FORREST_DEFAULT_PROPERTIES_FILE = FORREST_CORE_WEBAPP 118 + File.separatorChar + "default-forrest.properties"; 119 120 private static ILaunch serverLaunch; 121 122 126 private ForrestManager() { 127 super(); 128 } 129 130 138 public Hashtable getProperties(String workingDir) throws IOException { 139 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 (workingDir + File.separatorChar + "forrest.properties")); 149 props.execute(); 150 151 props.setSrcFile(new File (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 174 public Job getForrestJob(String workingDir, String 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 196 protected static File [] getClasspathFiles() { 197 Vector vctFiles = new Vector (); 198 199 try { 200 vctFiles.add(new File (FORREST_CLASSES)); 203 vctFiles.add(new File (FORREST_JAR)); 204 vctFiles.addAll(getLibFiles(FORREST_CORE_LIB)); 206 vctFiles.addAll(getLibFiles(FORREST_ENDORSED_LIB)); 208 vctFiles.addAll(getLibFiles(FORREST_OPTIONAL_LIB)); 210 vctFiles.addAll(getLibFiles(FORREST_PLUGINS_LIB)); 212 } catch (FileNotFoundException e) { 213 logger.error("getClasspathFiles()", e); 214 215 e.printStackTrace(); 217 } 218 219 File [] files = new File [vctFiles.size()]; 220 File [] returnFileArray = (File []) vctFiles.toArray(files); 221 return returnFileArray; 222 } 223 224 233 static public ArrayList getLibFiles(String dir) throws FileNotFoundException { 234 File directory = new File (dir); 235 ArrayList result = new ArrayList (); 236 List dirs = Arrays.asList(directory.listFiles()); 237 Iterator filesIter = dirs.iterator(); 238 File file = null; 239 240 while (filesIter.hasNext()) { 241 file = (File ) filesIter.next(); 242 243 if (file.isDirectory()) { 244 List 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 262 public static void stopServer(String projectDir) 263 throws DebugException { 264 serverLaunch.terminate(); 265 } 266 267 271 public static void setServerLaunch(ILaunch launch) { 272 serverLaunch = launch; 273 } 274 275 } | Popular Tags |