1 package webman.stager; 2 3 import java.io.*; 4 import java.net.*; 5 import java.util.*; 6 import org.apache.log4j.*; 7 8 13 public abstract class Stager 14 { 15 16 protected FileWriter logfile = null; 17 18 19 protected static final String NEWLINE = System.getProperty("line.separator"); 20 21 protected static Category CAT = Category.getInstance(Stager.class); 23 protected PrintStream outStream = null; 24 25 26 static 27 { 28 URL url = Stager.class.getResource("Stager_log4j.properties"); 30 PropertyConfigurator.configure(url); 36 } 37 38 43 protected String markAsFile(String str) 44 { 45 return "\"<i>" + str + "</i>\""; 46 } 47 49 55 protected void printToSocket(Socket s, String message) throws Exception 56 { 57 PrintWriter out = null; 58 try 59 { 60 out = new PrintWriter(s.getOutputStream()); 61 out.println(message); 62 out.flush(); 63 } 64 catch(Exception e) 65 { 66 throw new Exception ("Unable to write to socket: " + e); 67 } 68 } 69 70 76 protected String readFromSocket(Socket s) throws Exception 77 { 78 BufferedReader in = null; 79 String line = null; 80 in = new BufferedReader(new InputStreamReader(s.getInputStream())); 81 line = in.readLine(); 82 return line; 83 } 84 85 87 93 protected FileWriter openLog(String filename, boolean append) 94 { 95 try 96 { 97 return new FileWriter(filename,append); 98 } 99 catch (Exception e) 100 { 101 CAT.error("Unable to open logfile: ",e); 102 } 103 return null; 104 } 105 106 110 protected void log(String str) 111 { 112 CAT.info("log(): " + str); 113 if ( logfile != null ) 114 { 115 if ( !str.endsWith(NEWLINE) ) 116 { 117 str += NEWLINE; 118 } 119 try 120 { 121 logfile.write(str); 122 logfile.flush(); 123 } 124 catch (Exception e) 125 { 126 CAT.error("Could not write to logfile: ",e); 127 } 128 } 129 } 130 131 134 protected void closeLog() 135 { 136 if ( logfile != null ) 137 { 138 try 139 { 140 logfile.flush(); 141 logfile.close(); 142 logfile = null; } 144 catch (Exception e) 145 { 146 CAT.warn("Unable to close logfile",e); 147 } 148 } 149 } 150 151 155 protected String initSSL() 156 { 157 try 158 { 159 java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 161 } 162 catch (Exception e) 163 { 164 return "unable to init ssl: " + e; 165 } 166 return null; 167 } 168 169 177 protected String getAbsoluteName(String docroot, String filename) throws IllegalArgumentException , IOException 178 { 179 if ( new File(filename).isAbsolute() ) 180 { 181 return new File(filename).getCanonicalPath(); 182 } 183 if ( docroot == null ) 184 { 185 throw new IllegalArgumentException ("docroot must be provided if filename is relative"); 186 } 187 return new File(docroot,filename).getCanonicalPath(); 188 } 189 190 194 protected abstract void logAndPrint(String str); 195 196 200 protected void error(String str) 201 { 202 logAndPrint("<FONT color=#ff5555>" + str + "</FONT>"); 203 CAT.error(str); 204 } 205 206 211 protected void error(String str, Throwable t) 212 { 213 logAndPrint("<FONT color=#ff5555>" + str + " [" + t.toString() + "]</FONT>"); 214 CAT.error(str,t); 215 } 216 217 223 boolean methodSucceed(String result, String msg) 224 { 225 if ( result != null ) 226 { 227 error("<B>" + msg + " ... failed: " + result + "</B><BR><BR>"); 228 CAT.error(msg + " failed: " + result); 229 return false; 230 } 231 logAndPrint("<B>" + msg + " ... done" + "</B><BR><BR>"); 232 return true; 233 } 234 235 240 boolean methodSucceed(String result) 241 { 242 return methodSucceed(result,""); 243 } 244 245 252 public static InputStream getResourceInputStream(Class baseClass, String path) throws IOException 253 { 254 if ( !(new File(path).isAbsolute()) ) 255 { 256 String base = baseClass.getName().substring(baseClass.getName().lastIndexOf(".") + 1); 258 base = new File(baseClass.getResource(base + ".class").getFile()).getParent(); 260 path = new File(base,path).getCanonicalPath(); 261 } 262 try 263 { 264 return (new URL("file:" + path)).openStream(); 265 } 266 catch(Exception e) 267 { 268 throw new IOException(e.toString()); 269 } 270 } 271 } 272 | Popular Tags |