KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > avk > DomainParser


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * DomainParser.java
21  *
22  * Created on September 8, 2005, 10:14 PM
23  *
24  */

25
26 package org.netbeans.modules.j2ee.sun.ide.avk;
27
28 import java.io.File JavaDoc;
29 import java.io.FileWriter JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.io.StringReader JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34
35 import javax.xml.parsers.DocumentBuilder JavaDoc;
36 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
37 import javax.xml.transform.OutputKeys JavaDoc;
38 import javax.xml.transform.Transformer JavaDoc;
39 import javax.xml.transform.TransformerFactory JavaDoc;
40 import javax.xml.transform.dom.DOMSource JavaDoc;
41 import javax.xml.transform.stream.StreamResult JavaDoc;
42 import org.w3c.dom.Document JavaDoc;
43 import org.w3c.dom.Node JavaDoc;
44 import org.w3c.dom.NodeList JavaDoc;
45 import org.xml.sax.EntityResolver JavaDoc;
46 import org.xml.sax.InputSource JavaDoc;
47 import org.xml.sax.SAXException JavaDoc;
48 import org.w3c.dom.NamedNodeMap JavaDoc;
49 import org.w3c.dom.Attr JavaDoc;
50 import org.w3c.dom.Text JavaDoc;
51 import org.w3c.dom.Element JavaDoc;
52
53 import org.openide.filesystems.FileUtil;
54
55 /**
56  *
57  * @author Nitya Doraisamy
58  */

59 public class DomainParser {
60     
61     private static String JavaDoc CLASSPATH_PREFIX = "classpath-prefix"; //NOI18N
62
private static String JavaDoc JVMOPTION_PREFIX = "-Dj2ee.appverification.home="; //NOI18N
63
private static final String JavaDoc FILE_BACKUP_EXTENSION = "backup";
64     private static final String JavaDoc FILE_BACKUP_NAME = "policybackup";
65     
66     /** Creates a new instance of DomainParser */
67     public DomainParser() {
68     }
69  
70     public static boolean editSupportInDomain(String JavaDoc domainScriptFilePath, String JavaDoc avkJarLoc, String JavaDoc avkHome, boolean onOff) {
71         File JavaDoc domainScriptFile = new File JavaDoc(domainScriptFilePath);
72         
73         // Load domain.xml
74
Document JavaDoc domainScriptDocument = loadDomainScriptFile(domainScriptFilePath);
75         if (domainScriptDocument == null)
76             return false;
77         
78         NodeList JavaDoc javaConfigNodeList = domainScriptDocument.getElementsByTagName("java-config");
79         if (javaConfigNodeList == null || javaConfigNodeList.getLength() == 0) {
80             return false;
81         }
82         Node JavaDoc 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         // Save domain.xml
94
return saveDomainScriptFile(domainScriptDocument, domainScriptFilePath);
95     }
96     
97     
98     // creates Document instance from domain.xml
99
private static Document JavaDoc loadDomainScriptFile(String JavaDoc domainScriptFilePath) {
100         
101         Document JavaDoc document = null;
102         
103         try {
104             
105             DocumentBuilderFactory JavaDoc dbFactory = DocumentBuilderFactory.newInstance();
106             dbFactory.setValidating(false);
107             
108             DocumentBuilder JavaDoc dBuilder = dbFactory.newDocumentBuilder();
109             
110             dBuilder.setEntityResolver(new EntityResolver JavaDoc() {
111                 public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc {
112                     StringReader JavaDoc reader = new StringReader JavaDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); // NOI18N
113
InputSource JavaDoc source = new InputSource JavaDoc(reader);
114                     source.setPublicId(publicId);
115                     source.setSystemId(systemId);
116                     return source;
117                 }
118             });
119             
120             return dBuilder.parse(new File JavaDoc(domainScriptFilePath));
121             
122         } catch (Exception JavaDoc e) {
123             return null;
124         }
125         
126     }
127     
128     // saves Document to domain.xml
129
private static boolean saveDomainScriptFile(Document JavaDoc domainScriptDocument, String JavaDoc domainScriptFilePath) {
130         boolean result = false;
131         
132         FileWriter JavaDoc domainScriptFileWriter = null;
133         
134         try {
135             
136             domainScriptFileWriter = new FileWriter JavaDoc(domainScriptFilePath);
137             
138             try {
139                 TransformerFactory JavaDoc transformerFactory = TransformerFactory.newInstance();
140                 Transformer JavaDoc 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 JavaDoc domSource = new DOMSource JavaDoc(domainScriptDocument);
148                 StreamResult JavaDoc streamResult = new StreamResult JavaDoc(domainScriptFileWriter);
149                 
150                 transformer.transform(domSource, streamResult);
151                 result = true;
152             } catch (Exception JavaDoc e) {
153                 result = false;
154             }
155         } catch (IOException JavaDoc ioex) {
156             result = false;
157         } finally {
158             try {
159                 if (domainScriptFileWriter != null)
160                     domainScriptFileWriter.close();
161             } catch (IOException JavaDoc ioex2) {
162                 //System.err.println("SunAS8IntegrationProvider: cannot close output stream for " + domainScriptFilePath);
163
};
164         }
165         
166         return result;
167     }
168         
169     private static void addClassPathPrefix(Document JavaDoc domainScriptDocument, Node JavaDoc javaConfigNode, String JavaDoc avkJarLoc){
170         NamedNodeMap JavaDoc map = javaConfigNode.getAttributes();
171         Node JavaDoc cpPrefix = map.getNamedItem(CLASSPATH_PREFIX);
172         if(cpPrefix == null){
173             map = setAttribute(domainScriptDocument, CLASSPATH_PREFIX, avkJarLoc, map);
174         }else{
175             String JavaDoc 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 JavaDoc domainScriptDocument, Node JavaDoc javaConfigNode, String JavaDoc avkJarLoc){
184         NamedNodeMap JavaDoc map = javaConfigNode.getAttributes();
185         Node JavaDoc cpPrefix = map.getNamedItem(CLASSPATH_PREFIX);
186         if(cpPrefix != null){
187             String JavaDoc 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 JavaDoc domainScriptDocument, Node JavaDoc javaConfigNode, String JavaDoc avkHome){
196         avkHome = JVMOPTION_PREFIX + avkHome;
197         removeJVMOption(domainScriptDocument, javaConfigNode, avkHome);
198         Element JavaDoc jvmOptionsElement = domainScriptDocument.createElement("jvm-options");
199         Text JavaDoc avkOption = domainScriptDocument.createTextNode(avkHome);
200         jvmOptionsElement.appendChild(avkOption);
201         javaConfigNode.appendChild(jvmOptionsElement);
202     }
203     
204     private static void removeJVMOption(Document JavaDoc domainScriptDocument, Node JavaDoc javaConfigNode, String JavaDoc avkHome){
205         avkHome = JVMOPTION_PREFIX + avkHome;
206         NodeList JavaDoc jvmOptionNodeList = domainScriptDocument.getElementsByTagName("jvm-options");
207         boolean removeNode = false;
208         for(int i=0; i<jvmOptionNodeList.getLength(); i++){
209             Node JavaDoc nd = jvmOptionNodeList.item(i);
210             if(nd.hasChildNodes()) {
211                 Node JavaDoc 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 JavaDoc setAttribute(Document JavaDoc domainScriptDocument, String JavaDoc attrName, String JavaDoc attrVal, NamedNodeMap JavaDoc map){
223         Attr JavaDoc cpPrefixAttr = domainScriptDocument.createAttribute(attrName);
224         cpPrefixAttr.setValue(attrVal);
225         map.setNamedItem(cpPrefixAttr);
226         return map;
227     }
228     
229     private static String JavaDoc removeClassPath(String JavaDoc cp, String JavaDoc 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 JavaDoc removeDuplicateClassPath(String JavaDoc cp){
238         ArrayList JavaDoc tokens = getTokenArray(cp);
239         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
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 JavaDoc getTokenArray(String JavaDoc value){
250         ArrayList JavaDoc tokens = new ArrayList JavaDoc();
251         for(StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(value, File.pathSeparator);st.hasMoreTokens();){
252             String JavaDoc next = st.nextToken();
253             if(!tokens.contains(next))
254                 tokens.add(next);
255         }
256         return tokens;
257     }
258     
259     public static boolean backupFile(String JavaDoc filename) {
260         File JavaDoc source = new File JavaDoc(filename);
261         String JavaDoc target = source.getAbsolutePath();
262         
263         File JavaDoc parentObj = source.getParentFile();
264         File JavaDoc targetFile = new File JavaDoc(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 JavaDoc ex) {
278             return false;
279         }
280     }
281     
282     public static boolean restoreFile(String JavaDoc filename) {
283         File JavaDoc target = new File JavaDoc(filename);
284         File JavaDoc source = new File JavaDoc(filename + "." + FILE_BACKUP_EXTENSION); //NOI18N
285

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