1 19 25 26 package org.netbeans.modules.j2ee.sun.ide.avk; 27 28 import java.io.File ; 29 import java.io.FileWriter ; 30 import java.io.IOException ; 31 import java.util.ArrayList ; 32 import java.io.StringReader ; 33 import java.util.StringTokenizer ; 34 35 import javax.xml.parsers.DocumentBuilder ; 36 import javax.xml.parsers.DocumentBuilderFactory ; 37 import javax.xml.transform.OutputKeys ; 38 import javax.xml.transform.Transformer ; 39 import javax.xml.transform.TransformerFactory ; 40 import javax.xml.transform.dom.DOMSource ; 41 import javax.xml.transform.stream.StreamResult ; 42 import org.w3c.dom.Document ; 43 import org.w3c.dom.Node ; 44 import org.w3c.dom.NodeList ; 45 import org.xml.sax.EntityResolver ; 46 import org.xml.sax.InputSource ; 47 import org.xml.sax.SAXException ; 48 import org.w3c.dom.NamedNodeMap ; 49 import org.w3c.dom.Attr ; 50 import org.w3c.dom.Text ; 51 import org.w3c.dom.Element ; 52 53 import org.openide.filesystems.FileUtil; 54 55 59 public class DomainParser { 60 61 private static String CLASSPATH_PREFIX = "classpath-prefix"; private static String JVMOPTION_PREFIX = "-Dj2ee.appverification.home="; private static final String FILE_BACKUP_EXTENSION = "backup"; 64 private static final String FILE_BACKUP_NAME = "policybackup"; 65 66 67 public DomainParser() { 68 } 69 70 public static boolean editSupportInDomain(String domainScriptFilePath, String avkJarLoc, String avkHome, boolean onOff) { 71 File domainScriptFile = new File (domainScriptFilePath); 72 73 Document domainScriptDocument = loadDomainScriptFile(domainScriptFilePath); 75 if (domainScriptDocument == null) 76 return false; 77 78 NodeList javaConfigNodeList = domainScriptDocument.getElementsByTagName("java-config"); 79 if (javaConfigNodeList == null || javaConfigNodeList.getLength() == 0) { 80 return false; 81 } 82 Node javaConfigNode = javaConfigNodeList.item(0); 83 if (avkJarLoc != null && avkHome != null) { 84 if(onOff){ 85 addClassPathPrefix(domainScriptDocument, javaConfigNode, avkJarLoc); 86 addJVMOption(domainScriptDocument, javaConfigNode, avkHome); 87 }else{ 88 removeClassPathPrefix(domainScriptDocument, javaConfigNode, avkJarLoc); 89 removeJVMOption(domainScriptDocument, javaConfigNode, avkHome); 90 } 91 } 92 93 return saveDomainScriptFile(domainScriptDocument, domainScriptFilePath); 95 } 96 97 98 private static Document loadDomainScriptFile(String domainScriptFilePath) { 100 101 Document document = null; 102 103 try { 104 105 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 106 dbFactory.setValidating(false); 107 108 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 109 110 dBuilder.setEntityResolver(new EntityResolver () { 111 public InputSource resolveEntity(String publicId, String systemId) throws SAXException , IOException { 112 StringReader reader = new StringReader ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); InputSource source = new InputSource (reader); 114 source.setPublicId(publicId); 115 source.setSystemId(systemId); 116 return source; 117 } 118 }); 119 120 return dBuilder.parse(new File (domainScriptFilePath)); 121 122 } catch (Exception e) { 123 return null; 124 } 125 126 } 127 128 private static boolean saveDomainScriptFile(Document domainScriptDocument, String domainScriptFilePath) { 130 boolean result = false; 131 132 FileWriter domainScriptFileWriter = null; 133 134 try { 135 136 domainScriptFileWriter = new FileWriter (domainScriptFilePath); 137 138 try { 139 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 140 Transformer transformer = transformerFactory.newTransformer(); 141 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 142 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); 143 transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, domainScriptDocument.getDoctype().getPublicId()); 144 transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, domainScriptDocument.getDoctype().getSystemId()); 145 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); 146 147 DOMSource domSource = new DOMSource (domainScriptDocument); 148 StreamResult streamResult = new StreamResult (domainScriptFileWriter); 149 150 transformer.transform(domSource, streamResult); 151 result = true; 152 } catch (Exception e) { 153 result = false; 154 } 155 } catch (IOException ioex) { 156 result = false; 157 } finally { 158 try { 159 if (domainScriptFileWriter != null) 160 domainScriptFileWriter.close(); 161 } catch (IOException ioex2) { 162 }; 164 } 165 166 return result; 167 } 168 169 private static void addClassPathPrefix(Document domainScriptDocument, Node javaConfigNode, String avkJarLoc){ 170 NamedNodeMap map = javaConfigNode.getAttributes(); 171 Node cpPrefix = map.getNamedItem(CLASSPATH_PREFIX); 172 if(cpPrefix == null){ 173 map = setAttribute(domainScriptDocument, CLASSPATH_PREFIX, avkJarLoc, map); 174 }else{ 175 String prefixNodeValue = cpPrefix.getNodeValue(); 176 if(prefixNodeValue.indexOf(avkJarLoc) == -1){ 177 prefixNodeValue = removeDuplicateClassPath(avkJarLoc + File.pathSeparatorChar + prefixNodeValue); 178 map = setAttribute(domainScriptDocument, CLASSPATH_PREFIX, prefixNodeValue, map); 179 } 180 } 181 } 182 183 private static void removeClassPathPrefix(Document domainScriptDocument, Node javaConfigNode, String avkJarLoc){ 184 NamedNodeMap map = javaConfigNode.getAttributes(); 185 Node cpPrefix = map.getNamedItem(CLASSPATH_PREFIX); 186 if(cpPrefix != null){ 187 String prefixNodeValue = cpPrefix.getNodeValue(); 188 if(prefixNodeValue.indexOf(avkJarLoc) != -1){ 189 prefixNodeValue = removeClassPath(prefixNodeValue, avkJarLoc); 190 map = setAttribute(domainScriptDocument, CLASSPATH_PREFIX, prefixNodeValue, map); 191 } 192 } 193 } 194 195 private static void addJVMOption(Document domainScriptDocument, Node javaConfigNode, String avkHome){ 196 avkHome = JVMOPTION_PREFIX + avkHome; 197 removeJVMOption(domainScriptDocument, javaConfigNode, avkHome); 198 Element jvmOptionsElement = domainScriptDocument.createElement("jvm-options"); 199 Text avkOption = domainScriptDocument.createTextNode(avkHome); 200 jvmOptionsElement.appendChild(avkOption); 201 javaConfigNode.appendChild(jvmOptionsElement); 202 } 203 204 private static void removeJVMOption(Document domainScriptDocument, Node javaConfigNode, String avkHome){ 205 avkHome = JVMOPTION_PREFIX + avkHome; 206 NodeList jvmOptionNodeList = domainScriptDocument.getElementsByTagName("jvm-options"); 207 boolean removeNode = false; 208 for(int i=0; i<jvmOptionNodeList.getLength(); i++){ 209 Node nd = jvmOptionNodeList.item(i); 210 if(nd.hasChildNodes()) { 211 Node childNode = nd.getFirstChild(); 212 if(childNode.getNodeValue().indexOf(JVMOPTION_PREFIX) != -1) 213 removeNode = true; 214 } 215 if(removeNode){ 216 javaConfigNode.removeChild(nd); 217 removeNode = false; 218 } 219 } 220 } 221 222 private static NamedNodeMap setAttribute(Document domainScriptDocument, String attrName, String attrVal, NamedNodeMap map){ 223 Attr cpPrefixAttr = domainScriptDocument.createAttribute(attrName); 224 cpPrefixAttr.setValue(attrVal); 225 map.setNamedItem(cpPrefixAttr); 226 return map; 227 } 228 229 private static String removeClassPath(String cp, String removeValue){ 230 int begin = cp.lastIndexOf(removeValue); 231 if(begin !=- 1){ 232 cp = removeDuplicateClassPath(cp.substring(0, begin) + cp.substring(begin + removeValue.length())); 233 } 234 return cp; 235 } 236 237 private static String removeDuplicateClassPath(String cp){ 238 ArrayList tokens = getTokenArray(cp); 239 StringBuffer result = new StringBuffer (); 240 for(int j=0; j<tokens.size(); ++j){ 241 if(j != 0){ 242 result.append(File.pathSeparator); 243 } 244 result.append(tokens.get(j)); 245 } 246 return result.toString(); 247 } 248 249 private static ArrayList getTokenArray(String value){ 250 ArrayList tokens = new ArrayList (); 251 for(StringTokenizer st = new StringTokenizer (value, File.pathSeparator);st.hasMoreTokens();){ 252 String next = st.nextToken(); 253 if(!tokens.contains(next)) 254 tokens.add(next); 255 } 256 return tokens; 257 } 258 259 public static boolean backupFile(String filename) { 260 File source = new File (filename); 261 String target = source.getAbsolutePath(); 262 263 File parentObj = source.getParentFile(); 264 File targetFile = new File (parentObj.getAbsolutePath() + File.separator + source.getName() + "." + FILE_BACKUP_EXTENSION); 265 266 if (!source.exists()) { 267 return false; 268 } 269 270 if (targetFile.exists()) if (!targetFile.delete()) { 271 return false; 272 } 273 274 try { 275 FileUtil.copyFile(FileUtil.toFileObject(source), FileUtil.toFileObject(parentObj), source.getName(), FILE_BACKUP_EXTENSION); 276 return true; 277 } catch (Exception ex) { 278 return false; 279 } 280 } 281 282 public static boolean restoreFile(String filename) { 283 File target = new File (filename); 284 File source = new File (filename + "." + FILE_BACKUP_EXTENSION); 286 if (!source.exists()) { 287 return false; 288 } 289 290 if (target.exists()) if (!target.delete()) { 291 return false; 292 } 293 294 if (!source.renameTo(target)) { 295 return false; 296 } 297 return true; 298 } 299 300 301 } 302 | Popular Tags |