KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > j2ee > ConfigureProfiler


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 package org.netbeans.modules.j2ee.sun.ide.j2ee;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileReader JavaDoc;
25 import java.io.FileWriter JavaDoc;
26 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
27 import org.w3c.dom.Document JavaDoc;
28
29 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface;
30 /**
31  *
32  * @author Ludovic Champenois
33  */

34 public class ConfigureProfiler {
35     
36     
37     private static final String JavaDoc ASENV_INSERTION_POINT_WIN_STRING = "set AS_JAVA";
38     private static final String JavaDoc ASENV_INSERTION_POINT_NOWIN_STRING = "AS_JAVA";
39     
40     // removes any existing 'profiler' element and creates new one using provided parameters (if needed)
41
public static boolean instrumentProfilerInDOmain(DeploymentManager JavaDoc dm, String JavaDoc nativeLibraryPath, String JavaDoc[] jvmOptions) {
42         DomainEditor dEditor = new DomainEditor(dm);
43         
44         // Load domain.xml
45
Document JavaDoc domainDocument = dEditor.getDomainDocument();
46         if (domainDocument == null) {
47             return false;
48         }
49         
50         return dEditor.addProfilerElements(domainDocument, nativeLibraryPath, jvmOptions);
51     }
52     
53     // removes any existing 'profiler' element and creates new one using provided parameters (if needed)
54
public static boolean removeProfilerFromDomain(DeploymentManager JavaDoc dm) {
55         DomainEditor dEditor = new DomainEditor(dm);
56                 
57         // Load domain.xml
58
Document JavaDoc domainDocument = dEditor.getDomainDocument();
59         if (domainDocument == null) {
60             return false;
61         }
62         
63         return dEditor.removeProfilerElements(domainDocument);
64     }
65     
66     // replaces the AS_JAVA item in asenv.bat/conf
67
public static boolean modifyAsEnvScriptFile( SunDeploymentManagerInterface dm, String JavaDoc targetJavaHomePath) {
68         
69             String JavaDoc ext = (isUnix() ? "conf" : "bat");
70         File JavaDoc irf = dm.getPlatformRoot();
71         if (null == irf || !irf.exists()) {
72             return false;
73         }
74         String JavaDoc installRoot = irf.getAbsolutePath(); //System.getProperty("com.sun.aas.installRoot");
75
String JavaDoc asEnvScriptFilePath = installRoot+"/config/asenv." + ext;
76   // System.out.println("asEnvScriptFilePath="+asEnvScriptFilePath);
77
File JavaDoc asEnvScriptFile = new File JavaDoc(asEnvScriptFilePath);
78         String JavaDoc lineBreak = System.getProperty("line.separator");
79         
80         try {
81             
82             String JavaDoc line;
83             FileReader JavaDoc fr = new FileReader JavaDoc(asEnvScriptFile);
84             BufferedReader JavaDoc br = new BufferedReader JavaDoc(fr);
85             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
86             
87             String JavaDoc asJavaString = (isUnix() ? ASENV_INSERTION_POINT_NOWIN_STRING : ASENV_INSERTION_POINT_WIN_STRING);
88             
89             // copy config file from disk into memory buffer and modify line containing AS_JAVA definition
90
while ((line = br.readLine()) != null) {
91                 if (line.trim().startsWith(asJavaString)) {
92                     buffer.append(asJavaString + "=" + targetJavaHomePath);
93                 } else {
94                     buffer.append(line);
95                 }
96                 buffer.append(lineBreak);
97             }
98             br.close();
99             
100             // flush modified config file from memory buffer back to disk
101
FileWriter JavaDoc fw = new FileWriter JavaDoc(asEnvScriptFile);
102             fw.write(buffer.toString());
103             fw.flush();
104             fw.close();
105             
106             if (isUnix()) {
107                 Runtime.getRuntime().exec("chmod a+r " + asEnvScriptFile.getAbsolutePath()); //NOI18N
108
}
109             
110             return true;
111             
112         } catch (Exception JavaDoc ex) {
113             
114             System.err.println("Modifying " + asEnvScriptFilePath + " failed!\n" + ex.getMessage());
115             return false;
116             
117         }
118         
119     }
120     
121     public static boolean isUnix() {
122         return File.separatorChar == '/';
123     }
124     
125
126 }
127
Popular Tags