1 2 23 package org.enhydra.tool.configure; 24 25 import org.enhydra.tool.ToolBoxInfo; 27 import org.enhydra.tool.common.FileUtil; 28 import org.enhydra.tool.common.PathHandle; 29 import org.enhydra.tool.common.Replacement; 30 import org.enhydra.tool.common.ReplacementSet; 31 import org.enhydra.tool.common.TemplateTool; 32 import org.enhydra.tool.common.ToolException; 33 34 import java.io.BufferedReader ; 36 import java.io.IOException ; 37 import java.io.File ; 38 import java.io.FileFilter ; 39 import java.util.Vector ; 40 import java.util.ResourceBundle ; 41 42 public class ConfigTool extends TemplateTool implements Constants { 44 private static ResourceBundle localRes = 45 ResourceBundle.getBundle("org.enhydra.tool.configure.Res"); 47 private boolean transformPath = true; 49 private String javaPath = ToolBoxInfo.getJavaPath(); 50 private String enhydraRoot = ToolBoxInfo.getEnhydraRoot(); 51 private String appPath = null; 52 private FileFilter fillFilter = null; 53 54 private final String DIR_DIST = "dist"; private final String DIR_LIB = "lib"; 58 private final String FILE_ENHYDRA_JAR = "enhydra.jar"; 64 private final String AT_ENHYDRA_DIR_AT = "@ENHYDRA_DIR@"; private final String AT_JDKDIR_AT = "@JDKDIR@"; private final String AT_OUTPUT_AT = "@OUTPUT@"; private final String AT_ENHYDRA_CLASSES_AT = 69 "@ENHYDRA_CLASSES@"; 71 public static final String PROJECT_PATH = "@PROJECT_PATH@"; public static final String JAVA_PATH = "@JAVA_PATH@"; public static final String DEPLOY_PATH = "@DEPLOY_PATH@"; public static final String ENHYDRA_PATH = "@ENHYDRA_PATH@"; public static final String [] PATHS = { 77 PROJECT_PATH, JAVA_PATH, DEPLOY_PATH, ENHYDRA_PATH 78 }; 79 80 public static Replacement[] createReplacements( 81 String appRoot, 82 String deployPath, 83 String javaHome) throws ConfigException { 84 Replacement[] defRep = new Replacement[4]; 85 86 try { 87 defRep[0] = new Replacement(ConfigTool.PROJECT_PATH, 88 PathHandle.createPathString(appRoot)); 89 defRep[1] = new Replacement(ConfigTool.DEPLOY_PATH, 90 PathHandle.createPathString(deployPath)); 91 defRep[2] = new Replacement(ConfigTool.JAVA_PATH, 92 PathHandle.createPathString(javaHome)); 93 defRep[3] = new Replacement(ConfigTool.ENHYDRA_PATH, 94 ToolBoxInfo.getEnhydraRoot()); 95 } catch (ToolException e) { 96 throw new ConfigException(e, 97 localRes.getString("Unable_to_create")); 98 } 99 return defRep; 100 } 101 102 public static String [] getSuffixArray() { 103 String [] sufs = new String [PATHS.length]; 104 105 for (int i = 0; i < PATHS.length; i++) { 106 sufs[i] = PATHS[i].substring(1); 107 } 108 return sufs; 109 } 110 111 public static Replacement[] createReplacements(String deployPath) 112 throws ConfigException { 113 File f = new File (deployPath); 114 return ConfigTool.createReplacements(f.getParent(), 115 deployPath, 116 ToolBoxInfo.getJavaPath()); 117 } 118 119 public static String [][] createReplacementStringArray(String appRoot, 120 String deployPath, 121 String javaP) 122 throws ConfigException { 123 Replacement[] reps = ConfigTool.createReplacements(appRoot, deployPath, javaP); 124 ReplacementSet set = new ReplacementSet(reps); 125 126 return set.toStringArray(); 127 } 128 129 public ConfigTool() {} 130 131 public File [] createOutput() throws ToolException { 132 if (isTransformPath()) { 133 setReplacements(getTransformedReplacements()); 134 } 135 return super.createOutput(); 136 } 137 138 public boolean isTransformPath() { 139 return transformPath; 140 } 141 142 public void setTransformPath(boolean b) { 143 transformPath = b; 144 } 145 146 public String getAppPath() { 147 return appPath; 148 } 149 150 public void setAppPath(String p) { 151 appPath = p; 152 } 153 154 public String getEnhydraRoot() { 155 if (enhydraRoot == null) { 156 enhydraRoot = ToolBoxInfo.getEnhydraRoot(); 157 } 158 return enhydraRoot; 159 } 160 161 public void setEnhydraRoot(String p) { 162 enhydraRoot = p; 163 } 164 165 public String getJavaPath() { 166 if (javaPath == null) { 167 javaPath = ToolBoxInfo.getJavaPath(); 168 } 169 return javaPath; 170 } 171 172 public void setJavaPath(String p) { 173 javaPath = p; 174 } 175 176 public void configure() throws ConfigException { 177 File dest = null; 178 File source = null; 179 File [] output = new File [0]; 180 String [] types = new String [1]; 181 182 types[0] = TYPE_IN; 183 184 try { 185 if (getAppPath() == null) { 186 source = new File (getEnhydraRoot() + File.separator 187 + DIR_DIST); 188 dest = new File (getEnhydraRoot()); 189 initEnhydraReplacements(); 190 } else { 191 source = new File (getAppPath() + File.separator + DIR_INPUT); 192 dest = new File (getAppPath() + File.separator + DIR_OUTPUT); 193 initAppReplacements(dest); 194 } 195 setOverwrite(!isSwing()); 196 initTemplates(source, dest, types); 197 output = createOutput(); 198 } catch (ToolException e) { 199 throw (new ConfigException(e, 200 localRes.getString("Unable_to_complete"))); 201 } 202 if (output.length == 0 && isEcho()) { 203 System.out.println(localRes.getString("No_templates")); 204 } 205 } 206 207 public void initReplacements(String [][] strings) throws ConfigException { 208 Replacement[] reps = new Replacement[strings.length]; 209 210 try { 211 for (int i = 0; i < reps.length; i++) { 212 reps[i] = new Replacement(strings[i][0], strings[i][1]); 213 } 214 } catch (ToolException e) { 215 throw new ConfigException(e, 216 localRes.getString("Unable_to_initialize")); 217 } 218 setReplacements(reps); 219 } 220 221 private void initAppReplacements(File deployRoot) 222 throws ConfigException { 223 Replacement[] newReps = new Replacement[0]; 224 225 newReps = ConfigTool.createReplacements( 226 deployRoot.getParent(), 227 deployRoot.getAbsolutePath(), 228 getJavaPath()); 229 setReplacements(newReps); 230 } 231 232 private void initEnhydraReplacements() throws ConfigException { 233 Replacement[] newReps = new Replacement[4]; 234 235 try { 236 newReps[0] = new Replacement(AT_ENHYDRA_DIR_AT, getEnhydraRoot()); 237 newReps[1] = new Replacement(AT_JDKDIR_AT, getJavaPath()); 238 newReps[2] = new Replacement(AT_OUTPUT_AT, getEnhydraRoot()); 239 newReps[3] = new Replacement(AT_ENHYDRA_CLASSES_AT, 240 getEnhydraRoot() + '/' + DIR_LIB 241 + '/' + FILE_ENHYDRA_JAR); 242 setReplacements(newReps); 243 } catch (ToolException e) { 244 throw new ConfigException(e, 245 localRes.getString("Unable_to_initialize")); 246 } 247 } 248 249 private Replacement[] getTransformedReplacements() { 250 final String AT = (new String ()) + '@'; 251 final String _PATH = "_PATH@"; final String OS_ = "@OS_"; final String SHELL_ = "@SHELL_"; final String JAVA_ = "@JAVA_"; 256 Vector transVector = new Vector (); 258 Replacement[] trans = new Replacement[0]; 259 String cursorFind = new String (); 260 String newFind = new String (); 261 String path = new String (); 262 263 for (int i = 0; i < getReplacements().length; i++) { 264 transVector.addElement(getReplacements()[i]); 265 cursorFind = getReplacements()[i].getFind(); 266 if (cursorFind.startsWith(AT) && cursorFind.endsWith(_PATH)) { 267 if (!cursorFind.startsWith(OS_)) { 268 path = getReplacements()[i].getReplaceWith()[0]; 269 path = FileUtil.toCurrentPath(path); 270 try { 271 newFind = OS_ + cursorFind.substring(1); 272 Replacement osPath = new Replacement(newFind, path); 273 274 transVector.addElement(osPath); 275 } catch (ToolException e) { 276 echoWrite(e); 277 } 278 } 279 if (!cursorFind.startsWith(SHELL_)) { 280 newFind = SHELL_ + cursorFind.substring(1); 281 path = getReplacements()[i].getReplaceWith()[0]; 282 path = FileUtil.toShellPath(path); 283 try { 284 Replacement shellPath = new Replacement(newFind, 285 path); 286 287 transVector.addElement(shellPath); 288 } catch (ToolException e) { 289 echoWrite(e); 290 } 291 } 292 if ((!cursorFind.startsWith(JAVA_)) || cursorFind.equals(JAVA_PATH)) { 293 newFind = JAVA_ + cursorFind.substring(1); 294 path = getReplacements()[i].getReplaceWith()[0]; 295 path = FileUtil.toJavaPath(path); 296 try { 297 Replacement javaRep = new Replacement(newFind, path); 298 transVector.addElement(javaRep); 299 } catch (ToolException e) { 300 echoWrite(e); 301 } 302 } 303 } 304 } 305 trans = (Replacement[]) transVector.toArray(trans); 306 transVector.removeAllElements(); 307 return trans; 308 } 309 310 } 311 | Popular Tags |