1 23 24 package com.sun.enterprise.appclient.jws; 25 26 import com.sun.appserv.ClassLoaderUtil; 27 import com.sun.enterprise.deployment.Application; 28 import com.sun.enterprise.deployment.ApplicationClientDescriptor; 29 import com.sun.enterprise.deployment.BundleDescriptor; 30 import com.sun.enterprise.deployment.archivist.AppClientArchivist; 31 import com.sun.enterprise.deployment.archivist.ApplicationArchivist; 32 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive; 33 import com.sun.enterprise.deployment.deploy.shared.ArchiveFactory; 34 import com.sun.enterprise.deployment.util.ModuleDescriptor; 35 import com.sun.enterprise.instance.BaseManager; 36 import java.io.BufferedInputStream ; 37 import java.io.BufferedOutputStream ; 38 import java.io.BufferedReader ; 39 import java.io.BufferedWriter ; 40 import java.io.File ; 41 import java.io.FileNotFoundException ; 42 import java.io.FileOutputStream ; 43 import java.io.FileInputStream ; 44 import java.io.IOException ; 45 import java.io.InputStream ; 46 import java.io.InputStreamReader ; 47 import java.io.OutputStreamWriter ; 48 import java.net.MalformedURLException ; 49 import java.net.URISyntaxException ; 50 import java.net.URL ; 51 import java.net.URLClassLoader ; 52 import java.util.Enumeration ; 53 import java.util.Properties ; 54 import java.util.Vector ; 55 import java.util.jar.JarFile ; 56 import java.util.regex.Matcher ; 57 import java.util.regex.Pattern ; 58 59 64 public class Util { 65 66 72 private static Pattern placeholderPattern = Pattern.compile("\\$\\{(.*?)\\}"); 73 74 75 private static final String CLASS_SUFFIX = ".class"; 76 77 78 private static final int BUFFER_SIZE = 1024; 79 80 81 public Util() { 82 } 83 84 93 public static String replaceTokens(String s, Properties values) { 94 Matcher m = placeholderPattern.matcher(s); 95 96 StringBuffer sb = new StringBuffer (); 97 102 while (m.find()) { 103 String propertyName = m.group(1); 104 String propertyValue = values.getProperty(propertyName); 105 106 if (propertyValue != null) { 107 115 String adjustedPropertyValue = propertyValue.replaceAll("\\$", "\\\\\\$"); 116 String x = s.substring(m.start(),m.end()); 117 try { 118 m.appendReplacement(sb, adjustedPropertyValue); 119 } catch (IllegalArgumentException iae) { 120 System.err.println("**** appendReplacement failed: segment is " + x + "; original replacement was " + propertyValue + " and adj. replacement is " + adjustedPropertyValue + "; exc follows"); 121 throw iae; 122 } 123 } 124 } 125 129 m.appendTail(sb); 130 131 return sb.toString(); 132 } 133 134 139 public static String getMainClassNameForAppClient(ModuleDescriptor moduleDescr) throws IOException , FileNotFoundException , org.xml.sax.SAXParseException { 140 BundleDescriptor bd = moduleDescr.getDescriptor(); 141 ApplicationClientDescriptor acDescr = (ApplicationClientDescriptor) bd; 142 143 String mainClassName = acDescr.getMainClassName(); 144 145 return mainClassName; 146 } 147 148 158 public static File writeTextToTempFile(String content, String prefix, String suffix, boolean retainFile) throws IOException , FileNotFoundException { 159 BufferedWriter wtr = null; 160 try { 161 File result = File.createTempFile(prefix, suffix); 162 if ( ! retainFile) { 163 result.deleteOnExit(); 164 } 165 FileOutputStream fos = new FileOutputStream (result); 166 wtr = new BufferedWriter (new OutputStreamWriter (fos)); 167 wtr.write(content); 168 wtr.close(); 169 return result; 170 } finally { 171 if (wtr != null) { 172 wtr.close(); 173 } 174 } 175 } 176 177 186 public static File writeTextToTempFile(String content, String prefix, String suffix) throws IOException , FileNotFoundException { 187 return writeTextToTempFile(content, prefix, suffix, false); 188 } 189 190 195 public static URL locateClass(Class target) { 196 return target.getProtectionDomain().getCodeSource().getLocation(); 197 } 198 199 210 public static String loadResource(Class contextClass, String resourcePath) throws IOException { 211 String result = null; 212 InputStream is = null; 213 try { 214 is = contextClass.getResourceAsStream(resourcePath); 215 if (is == null) { 216 throw new IOException ("Could not locate the requested resource relative to class " + contextClass.getName()); 217 } 218 219 StringBuilder sb = new StringBuilder (); 220 BufferedReader reader = new BufferedReader (new InputStreamReader (is)); 221 int charsRead; 222 char [] buffer = new char [BUFFER_SIZE]; 223 while ((charsRead = reader.read(buffer)) != -1) { 224 sb.append(buffer, 0, charsRead); 225 } 226 227 result= sb.toString(); 228 return result; 229 } catch (IOException ioe) { 230 IOException wrapperIOE = new IOException ("Error loading resource " + resourcePath); 231 wrapperIOE.initCause(ioe); 232 throw wrapperIOE; 233 } finally { 234 if (is != null) { 235 is.close(); 236 } 237 } 238 } 239 240 245 public static File copyToTempFile(File inputFile, String prefix, String suffix, boolean retainFile) throws IOException , FileNotFoundException { 246 File result = null; 247 BufferedInputStream is = null; 248 BufferedOutputStream os = null; 249 try { 250 result = File.createTempFile(prefix, suffix); 251 if ( ! retainFile) { 252 result.deleteOnExit(); 253 } 254 os = new BufferedOutputStream (new FileOutputStream (result)); 255 is = new BufferedInputStream (new FileInputStream (inputFile)); 256 byte [] buffer = new byte[BUFFER_SIZE]; 257 int bytesRead = 0; 258 while ( (bytesRead = is.read(buffer) ) != -1) { 259 os.write(buffer, 0, bytesRead); 260 } 261 262 return result; 263 } finally { 264 if (is != null) { 265 is.close(); 266 } 267 if (os != null) { 268 os.close(); 269 } 270 } 271 } 272 273 279 public static String URLtoCodeBase(URL classPathElement) throws FileNotFoundException , URISyntaxException { 280 283 File file = new File (classPathElement.toURI()); 284 if (! file.exists()) { 285 291 return null; 293 } 294 295 300 String result; 301 if (file.isDirectory()) { 302 result = classPathElement.getProtocol() + ":" + classPathElement.toURI().getPath() + "-"; 303 } else { 304 result = classPathElement.getProtocol() + ":" + classPathElement.toURI().getPath(); 305 } 306 return result; 307 } 308 } 309 | Popular Tags |