1 19 20 package com.sslexplorer.applications.server; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.util.Enumeration ; 28 import java.util.Hashtable ; 29 import java.util.Random ; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 import com.sslexplorer.boot.XMLElement; 35 36 42 public class FileReplacement { 43 44 final static Log log = LogFactory.getLog(FileReplacement.class); 45 46 Hashtable tokens = new Hashtable (); 47 File templateFile; 48 File outputFile; 49 File cwd; 50 String parameter; 51 String encoding = "UTF8"; 52 53 public FileReplacement(File cwd) { 54 this.cwd = cwd; 55 } 56 57 public String getId() { 58 return parameter; 59 } 60 61 void processReplacementXML(XMLElement el, ServerLauncher launcher) throws IOException { 62 if (!el.getName().equalsIgnoreCase("replacements")) { 63 throw new IOException ("Error! Element is not <replacements>"); 64 } 65 66 if (el.getAttribute("templateFile") == null) { 67 throw new IOException ("Error! <replacements> element requires 'templateFile' attribute"); 68 } 69 70 templateFile = new File (cwd, el.getAttribute("templateFile").toString()); 71 72 if (log.isDebugEnabled()) 73 log.debug("Template file will be " + templateFile.getAbsolutePath()); 74 75 if (el.getAttribute("outputFile") != null) { 76 outputFile = new File (cwd, el.getAttribute("outputFile").toString()); 77 } else { 78 outputFile = getTempFile(cwd); 79 } 80 81 if (log.isDebugEnabled()) 82 log.debug("Output file will be " + outputFile.getAbsolutePath()); 83 84 if (el.getAttribute("parameter") == null) { 85 throw new IOException ("Error! <replacements> element requires 'parameter' attribute"); 86 } 87 88 parameter = el.getAttribute("parameter").toString(); 89 launcher.addParameter(parameter, outputFile.getAbsolutePath()); 90 91 if (el.getAttribute("encoding") != null) 92 encoding = el.getAttribute("encoding").toString(); 93 94 if (log.isDebugEnabled()) 95 log.debug("Output file will be encoded in " + encoding); 96 97 for (Enumeration e = el.getChildren().elements(); e.hasMoreElements();) { 98 XMLElement child = (XMLElement) e.nextElement(); 99 100 if (!child.getName().equalsIgnoreCase("replace")) { 101 throw new IOException ("Error! <" + child.getName() + "> is not a supported element of <replacements>"); 102 } 103 104 if (child.getAttribute("token") == null || child.getAttribute("value") == null) { 105 throw new IOException ("Error! <replace> element requires 'token' and 'value' attributes"); 106 } 107 108 tokens.put(child.getAttribute("token").toString(), child.getAttribute("value").toString()); 109 } 110 111 } 112 113 void createReplacementsFile(ServerLauncher launcher) throws IOException { 114 115 FileInputStream in = new FileInputStream (templateFile); 116 ByteArrayOutputStream tmp = new ByteArrayOutputStream (); 117 118 byte[] buf = new byte[4096]; 119 int read; 120 121 while ((read = in.read(buf)) > -1) { 122 tmp.write(buf, 0, read); 123 } 124 125 String outputContent = launcher.replaceTokens(new String (tmp.toByteArray(), encoding)); 129 130 for (Enumeration e = tokens.keys(); e.hasMoreElements();) { 131 132 String token = (String ) e.nextElement(); 133 String value = (String ) tokens.get(token); 134 135 if (log.isDebugEnabled()) 136 log.debug("Processing replacement token " + token + "=" + value); 137 138 outputContent = ServerLauncher.replaceAllTokens(outputContent, token, value); 140 141 } 142 143 FileOutputStream out = new FileOutputStream (outputFile); 144 try { 145 out.write(outputContent.getBytes(encoding)); 146 } finally { 147 out.close(); 148 } 149 150 ClientCacheRemover.getInstance().trackFileForRemoval(outputFile); 151 152 } 153 154 static File getTempFile(File near) throws IOException { 155 String path = null; 156 if (near != null) 157 if (near.isFile()) 158 path = near.getParent(); 159 else if (near.isDirectory()) 160 path = near.getPath(); 161 162 Random wheel = new Random (); File tempFile = null; 164 do { 165 int unique = (wheel.nextInt() & Integer.MAX_VALUE) % 90000000 + 10000000; 167 tempFile = new File (path, Integer.toString(unique) + ".tmp"); 168 } while (tempFile.exists()); 169 new FileOutputStream (tempFile).close(); 175 176 if (false) { 178 System.out.println(tempFile.getCanonicalPath()); 179 } 180 return tempFile; 181 } } 183 | Popular Tags |