KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > applications > server > FileReplacement


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.applications.server;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Random JavaDoc;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 import com.sslexplorer.boot.XMLElement;
35
36 /**
37  * This utility will take an input file and replace any number of tokens before
38  * outputing it to an output file, which may or may not be temporary.
39  *
40  * @author Lee David Painter
41  */

42 public class FileReplacement {
43     
44     final static Log log = LogFactory.getLog(FileReplacement.class);
45
46     Hashtable JavaDoc tokens = new Hashtable JavaDoc();
47     File JavaDoc templateFile;
48     File JavaDoc outputFile;
49     File JavaDoc cwd;
50     String JavaDoc parameter;
51     String JavaDoc encoding = "UTF8";
52
53     public FileReplacement(File JavaDoc cwd) {
54         this.cwd = cwd;
55     }
56
57     public String JavaDoc getId() {
58         return parameter;
59     }
60
61     void processReplacementXML(XMLElement el, ServerLauncher launcher) throws IOException JavaDoc {
62         if (!el.getName().equalsIgnoreCase("replacements")) {
63             throw new IOException JavaDoc("Error! Element is not <replacements>");
64         }
65
66         if (el.getAttribute("templateFile") == null) {
67             throw new IOException JavaDoc("Error! <replacements> element requires 'templateFile' attribute");
68         }
69
70         templateFile = new File JavaDoc(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 JavaDoc(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 JavaDoc("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 JavaDoc e = el.getChildren().elements(); e.hasMoreElements();) {
98             XMLElement child = (XMLElement) e.nextElement();
99
100             if (!child.getName().equalsIgnoreCase("replace")) {
101                 throw new IOException JavaDoc("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 JavaDoc("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 JavaDoc {
114
115         FileInputStream JavaDoc in = new FileInputStream JavaDoc(templateFile);
116         ByteArrayOutputStream JavaDoc tmp = new ByteArrayOutputStream JavaDoc();
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         // First replace all standard parameters etc
126
// String outputContent = null /*launcher.replaceTokens(new
127
// String(tmp.toByteArray(), encoding))*/;
128
String JavaDoc outputContent = launcher.replaceTokens(new String JavaDoc(tmp.toByteArray(), encoding));
129
130         for (Enumeration JavaDoc e = tokens.keys(); e.hasMoreElements();) {
131
132             String JavaDoc token = (String JavaDoc) e.nextElement();
133             String JavaDoc value = (String JavaDoc) tokens.get(token);
134
135             if (log.isDebugEnabled())
136                 log.debug("Processing replacement token " + token + "=" + value);
137
138             // Perform replacement in String
139
outputContent = ServerLauncher.replaceAllTokens(outputContent, token, value);
140
141         }
142
143         FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(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 JavaDoc getTempFile(File JavaDoc near) throws IOException JavaDoc {
155         String JavaDoc 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 JavaDoc wheel = new Random JavaDoc(); // seeded from the clock
163
File JavaDoc tempFile = null;
164         do {
165             // generate random a number 10,000,000 .. 99,999,999
166
int unique = (wheel.nextInt() & Integer.MAX_VALUE) % 90000000 + 10000000;
167             tempFile = new File JavaDoc(path, Integer.toString(unique) + ".tmp");
168         } while (tempFile.exists());
169         // We "finally" found a name not already used. Nearly always the first
170
// time.
171
// Quickly stake our claim to it by opening/closing it to create it.
172
// In theory somebody could have grabbed it in that tiny window since
173
// we checked if it exists, but that is highly unlikely.
174
new FileOutputStream JavaDoc(tempFile).close();
175
176         // debugging peek at the name generated.
177
if (false) {
178             System.out.println(tempFile.getCanonicalPath());
179         }
180         return tempFile;
181     } // end getTempFile
182
}
183
Popular Tags