1 2 24 package org.enhydra.tool.archive; 25 26 import org.enhydra.tool.common.PathHandle; 28 29 import java.io.BufferedInputStream ; 31 import java.io.IOException ; 32 import java.io.File ; 33 import java.io.FileInputStream ; 34 import java.util.ArrayList ; 35 import java.util.Properties ; 36 37 public class PropertyReader implements PropertyKeys { 39 private Properties props = new Properties (); 40 private File file = null; 41 private boolean validate = true; 42 43 public PropertyReader() {} 44 45 public boolean isValidate() { 46 return validate; 47 } 48 49 public void setValidate(boolean v) { 50 validate = v; 51 } 52 53 public JarPlan[] read(File in) throws IOException { 54 FileInputStream fileStream = null; 55 BufferedInputStream bufStream = null; 56 57 file = in; 58 fileStream = new FileInputStream (file); 59 bufStream = new BufferedInputStream (fileStream); 60 props.load(bufStream); 61 bufStream.close(); 62 fileStream.close(); 63 return read(props); 64 } 65 66 public JarPlan[] read(Properties p) { 67 ArrayList list = null; 68 JarPlan[] plans = new JarPlan[0]; 69 70 props = p; 71 list = new ArrayList (); 72 for (int i = 0; i < 100; i++) { 73 JarPlan cursor = null; 74 String value = null; 75 76 value = pathValue(props.getProperty(PATH + i)); 77 if (isEmpty(value)) { 78 break; 79 } else { 80 try { 81 cursor = createPlan(i); 82 83 cursor.setArchivePath(value); 85 } catch (ArchiveException e) { 86 e.printStackTrace(System.err); 87 cursor = null; 88 } 89 } 90 if (cursor != null) { 91 list.add(cursor); 92 } 93 } 94 list.trimToSize(); 95 plans = new JarPlan[list.size()]; 96 plans = (JarPlan[]) list.toArray(plans); 97 list.clear(); 98 return plans; 99 } 100 101 private JarPlan createPlan(int count) throws ArchiveException { 102 String warValue = null; 103 String earValue = null; 104 String ejbValue = null; 105 String jarValue = null; 106 String serviceValue = null; 107 JarPlan plan = null; 108 109 earValue = props.getProperty(APP_NAME_PROP + count); 110 warValue = props.getProperty(CONTENT_ROOT_PROP + count); 111 ejbValue = props.getProperty(CREATE_CLIENT_PROP + count); 112 serviceValue = props.getProperty(Descriptor.ENHYDRA_SERVICE + '.' 113 + count); 114 jarValue = props.getProperty(CLASS_ROOT_PROP + count); 115 if (!isEmpty(earValue)) { 116 plan = createEarPlan(count); 117 } else if (!isEmpty(warValue)) { 118 plan = createWarPlan(count); 119 } else if (!isEmpty(ejbValue)) { 120 plan = createEjbPlan(count); 121 } else if (!isEmpty(serviceValue)) { 122 plan = createServicePlan(count); 123 } else if (!isEmpty(jarValue)) { 124 plan = createJarPlan(count); 125 } 126 return plan; 127 } 128 129 private JarPlan createJarPlan(int count) throws ArchiveException { 130 JarPlan plan = null; 131 132 plan = new JarPlan(); 133 readJarProperties(plan, count); 134 return plan; 135 } 136 137 private ServicePlan createServicePlan(int count) throws ArchiveException { 138 ServicePlan plan = null; 139 140 plan = new ServicePlan(); 141 readJarProperties(plan, count); 142 return plan; 143 } 144 145 private void readJarProperties(JarPlan plan, 146 int count) throws ArchiveException { 147 Descriptor[] dd = new Descriptor[0]; 148 String value = null; 149 String [] files = new String [0]; 150 151 plan.setValidate(isValidate()); 152 153 value = pathValue(props.getProperty(CLASS_ROOT_PROP + count)); 155 plan.setClassRoot(value); 156 files = readFiles(CLASS, count); 157 if (files.length > 0) { 158 plan.setClassFiles(files); 159 } 160 161 dd = plan.getDescriptors(); 163 for (int i = 0; i < dd.length; i++) { 164 value = pathValue(props.getProperty(dd[i].getType() + "." 165 + count)); 166 dd[i].setPath(value); 167 } 168 plan.setDescriptors(dd); 169 } 170 171 private EjbPlan createEjbPlan(int count) throws ArchiveException { 172 EjbPlan plan = null; 173 Boolean b = null; 174 String value = null; 175 176 plan = new EjbPlan(); 177 readJarProperties(plan, count); 178 179 value = props.getProperty(CREATE_CLIENT_PROP + count); 181 b = Boolean.valueOf(value); 182 plan.setCreateClient(b.booleanValue()); 183 return plan; 184 } 185 186 private EarPlan createEarPlan(int count) throws ArchiveException { 187 EarPlan plan = null; 188 String value = null; 189 Boolean b = null; 190 String [] files = new String [0]; 191 WebApplication[] webApps = new WebApplication[0]; 192 Descriptor[] dd = new Descriptor[0]; 193 194 plan = new EarPlan(); 195 plan.setValidate(isValidate()); 196 197 value = props.getProperty(APP_NAME_PROP + count); 199 plan.setAppName(value); 200 201 value = props.getProperty(APP_DESC_PROP + count); 203 plan.setDescription(value); 204 205 value = props.getProperty(ALT_DD_PROP + count); 207 b = Boolean.valueOf(value); 208 plan.setAlt(b.booleanValue()); 209 210 value = props.getProperty(COPY_CLIENT_PROP + count); 212 b = Boolean.valueOf(value); 213 plan.setCopyClient(b.booleanValue()); 214 215 files = readFiles(ENTERPRISE_MODULE_PROP, count); 217 if (files.length > 0) { 218 plan.setEnterpriseModules(files); 219 } 220 221 webApps = readWebApps(count); 223 if (webApps.length > 0) { 224 plan.setWebApplications(webApps); 225 } 226 227 files = readFiles(LIBRARY_PROP, count); 229 if (files.length > 0) { 230 plan.setLibFiles(files); 231 } 232 233 files = readFiles(META_DD_PROP, count); 235 if (files.length > 0) { 236 dd = new Descriptor[files.length]; 237 for (int i = 0; i < dd.length; i++) { 238 dd[i] = new Descriptor(files[i]); 239 } 240 plan.setDescriptors(dd); 241 } 242 return plan; 243 } 244 245 private WarPlan createWarPlan(int count) throws ArchiveException { 246 WarPlan plan = null; 247 String value = null; 248 String [] files = new String [0]; 249 Descriptor[] dd = new Descriptor[0]; 250 251 plan = new WarPlan(); 252 readJarProperties(plan, count); 253 254 value = pathValue(props.getProperty(CONTENT_ROOT_PROP + count)); 256 plan.setContentRoot(value); 257 files = readFiles(CONTENT, count); 258 if (files.length > 0) { 259 plan.setContentFiles(files); 260 } 261 262 files = readFiles(LIBRARY_PROP, count); 264 if (files.length > 0) { 265 plan.setLibFiles(files); 266 } 267 268 return plan; 270 } 271 272 private String [] readFiles(String property, int count) { 273 ArrayList list = new ArrayList (); 274 String [] files = new String [0]; 275 String value = null; 276 277 for (int i = 0; i < 2500; i++) { 278 value = pathValue(props.getProperty(property + count + "." + i)); 279 if (isEmpty(value)) { 280 break; 281 } else { 282 list.add(value); 283 } 284 } 285 list.trimToSize(); 286 files = new String [list.size()]; 287 files = (String []) list.toArray(files); 288 return files; 289 } 290 291 private WebApplication[] readWebApps(int count) { 292 ArrayList list = new ArrayList (); 293 WebApplication[] webApps = new WebApplication[0]; 294 String value = null; 295 296 for (int i = 0; i < 2500; i++) { 297 value = pathValue(props.getProperty(WEBAPP_MODULE_PROP + count 298 + "." + i)); 299 if (isEmpty(value)) { 300 break; 301 } else { 302 WebApplication webApp = new WebApplication(value); 303 304 value = props.getProperty(WEBAPP_CONTEXT_PROP + count + "." 305 + i); 306 if (!isEmpty(value)) { 307 webApp.setContextRoot(value); 308 } 309 list.add(webApp); 310 } 311 } 312 list.trimToSize(); 313 webApps = new WebApplication[list.size()]; 314 webApps = (WebApplication[]) list.toArray(webApps); 315 return webApps; 316 } 317 318 private boolean isEmpty(String value) { 319 boolean empty = false; 320 321 if (value == null) { 322 empty = true; 323 } else if (value.trim().length() == 0) { 324 empty = true; 325 } 326 return empty; 327 } 328 329 private String pathValue(String in) { 330 String out = null; 331 332 if (file == null) { 333 out = in; 334 } else if (isEmpty(in)) { 335 out = in; 336 } else { 337 PathHandle propPh = null; 338 PathHandle outPh = null; 339 340 propPh = PathHandle.createPathHandle(file); 341 outPh = propPh.expandRelativePath(in); 342 out = outPh.getPath(); 343 } 344 return out; 345 } 346 347 } 348 | Popular Tags |