1 24 package org.ofbiz.appservers; 25 26 import java.util.Map ; 27 import java.util.List ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.io.*; 32 33 import org.ofbiz.base.container.Container; 34 import org.ofbiz.base.container.ContainerException; 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.base.util.template.FreeMarkerWorker; 37 import org.ofbiz.base.start.Classpath; 38 import org.ofbiz.base.component.ComponentConfig; 39 40 49 public class GenerateContainer implements Container { 50 51 public static final String module = GenerateContainer.class.getName(); 52 public static final String source = "/framework/appservers/templates/"; 53 public static final String target = "/setup/"; 54 55 protected String configFile = null; 56 protected String ofbizHome = null; 57 protected String args[] = null; 58 59 60 63 public void init(String [] args, String configFile) { 64 this.ofbizHome = System.getProperty("ofbiz.home"); 65 this.configFile = configFile; 66 this.args = args; 67 } 68 69 72 public boolean start() throws ContainerException { 73 this.generateFiles(); 74 System.exit(1); 75 return true; 76 } 77 78 84 public void stop() throws ContainerException { 85 } 86 87 private void generateFiles() throws ContainerException { 88 File files[] = getTemplates(); 89 Map dataMap = buildDataMap(); 90 91 for (int i = 0; i < files.length; i++) { 93 if (!files[i].isDirectory() && !files[i].isHidden()) { 94 parseTemplate(files[i], dataMap); 95 } 96 } 97 } 98 99 private File[] getTemplates() throws ContainerException { 100 if (args == null) { 101 throw new ContainerException("Invalid application server type argument passed"); 102 } 103 104 String templateLocation = args[0]; 105 if (templateLocation == null) { 106 throw new ContainerException("Unable to locate Application Server template directory"); 107 } 108 109 File parentDir = new File(ofbizHome + source + templateLocation); 110 if (!parentDir.exists() || !parentDir.isDirectory()) { 111 throw new ContainerException("Template location - " + templateLocation + " does not exist!"); 112 } 113 114 return parentDir.listFiles(); 115 } 116 117 private Map buildDataMap() { 118 Map dataMap = new HashMap (); 119 List c[] = getClasspath(); 120 dataMap.put("classpathJars", c[0]); 121 dataMap.put("classpathDirs", c[1]); 122 dataMap.put("env", System.getProperties()); 123 dataMap.put("webApps", ComponentConfig.getAllWebappResourceInfos()); 124 return dataMap; 125 } 126 127 private List [] getClasspath() { 128 Classpath classPath = new Classpath(System.getProperty("java.class.path")); 129 List elements = classPath.getElements(); 130 List jar = new ArrayList (); 131 List dir = new ArrayList (); 132 133 Iterator i = elements.iterator(); 134 while (i.hasNext()) { 135 File f = (File) i.next(); 136 if (f.exists()) { 137 if (f.isDirectory()) { 138 dir.add(f.getAbsolutePath()); 139 } else { 140 jar.add(f.getAbsolutePath()); 141 } 142 } 143 } 144 145 List [] lists = { jar, dir }; 146 return lists; 147 } 148 149 private void parseTemplate(File templateFile, Map dataMap) throws ContainerException { 150 Debug.log("Parsing template : " + templateFile.getAbsolutePath(), module); 151 Reader reader = null; 152 try { 153 reader = new InputStreamReader(new FileInputStream(templateFile)); 154 } catch (FileNotFoundException e) { 155 throw new ContainerException(e); 156 } 157 158 String targetDirectoryName = args.length > 1 ? args[1] : null; 160 if (targetDirectoryName == null) { 161 targetDirectoryName = target; 162 } 163 String targetDirectory = ofbizHome + targetDirectoryName + args[0]; 164 File targetDir = new File(targetDirectory); 165 if (!targetDir.exists()) { 166 boolean created = targetDir.mkdirs(); 167 if (!created) { 168 throw new ContainerException("Unable to create target directory - " + targetDirectory); 169 } 170 } 171 172 if (!targetDirectory.endsWith("/")) { 173 targetDirectory = targetDirectory + "/"; 174 } 175 176 Writer writer = null; 178 try { 179 writer = new FileWriter(targetDirectory + templateFile.getName()); 180 } catch (IOException e) { 181 throw new ContainerException(e); 182 } 183 try { 184 FreeMarkerWorker.renderTemplate(templateFile.getAbsolutePath(), reader, dataMap, writer); 185 } catch (Exception e) { 186 throw new ContainerException(e); 187 } 188 189 try { 190 writer.flush(); 191 writer.close(); 192 } catch (IOException e) { 193 throw new ContainerException(e); 194 } 195 } 196 } 197 | Popular Tags |