KickJava   Java API By Example, From Geeks To Geeks.

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


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  * AVKSupport.java
21  *
22  * Created on September 8, 2005, 9:46 AM
23  *
24  */

25
26 package org.netbeans.modules.j2ee.sun.ide.avk;
27
28 import java.io.File JavaDoc;
29 import java.io.FileReader JavaDoc;
30 import java.io.FileWriter JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.BufferedWriter JavaDoc;
34 import java.io.BufferedReader JavaDoc;
35 import java.text.MessageFormat JavaDoc;
36 import java.util.ResourceBundle JavaDoc;
37 import java.lang.reflect.Method JavaDoc;
38 import java.io.InputStreamReader JavaDoc;
39
40 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
41
42 import org.openide.ErrorManager;
43 import org.openide.NotifyDescriptor;
44 import org.openide.awt.StatusDisplayer;
45 import org.openide.modules.InstalledFileLocator;
46 import org.openide.awt.HtmlBrowser.URLDisplayer;
47 import org.netbeans.modules.j2ee.sun.ide.Installer;
48 import org.netbeans.modules.j2ee.sun.ide.j2ee.ui.Util;
49 import org.netbeans.modules.j2ee.sun.api.InstrumentAVK;
50 import org.netbeans.modules.j2ee.sun.api.ExtendedClassLoader;
51 import org.netbeans.modules.j2ee.sun.ide.j2ee.PluginProperties;
52 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface;
53 import org.netbeans.modules.j2ee.sun.ide.j2ee.DeploymentManagerProperties;
54 import org.netbeans.modules.j2ee.sun.ide.j2ee.runtime.actions.ViewLogAction;
55 import org.netbeans.modules.j2ee.sun.api.ServerLocationManager;
56
57 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
58
59 /**
60  *
61  * @author Nitya Doraisamy
62  */

63 public class AVKSupport implements InstrumentAVK {
64     
65     private static final String JavaDoc FILE_BACKUP_EXTENSION = "backup";
66     private static SunDeploymentManagerInterface sunDm;
67     private static boolean serverRunning = true;
68            
69     protected static final ResourceBundle JavaDoc bundle = ResourceBundle.getBundle("org.netbeans.modules.j2ee.sun.ide.avk.actions.Bundle");// NOI18N
70

71     /** Creates a new instance of AVKSupport */
72     public AVKSupport() {
73     }
74     
75     public AVKSupport(SunDeploymentManagerInterface sdm) {
76         setDeploymentManager(sdm);
77     }
78     
79     public void setDeploymentManager(SunDeploymentManagerInterface sdm){
80         this.sunDm = sdm;
81     }
82     
83     public void setAVK(boolean onOff){
84         if(onOff){
85             //Turn On AVK
86
instrumentAVK();
87         }else{
88             //Turn Off AVK
89
uninstrumentAVK();
90         }
91     }
92     
93     public void generateReport() {
94         File JavaDoc report = null;
95         try{
96             boolean success = runReportTool();
97             if(success){
98                 report = new File JavaDoc(getAVKReportLocation() + File.separator + "results" + File.separator + "suiteSummary.html"); //NOI18N
99
URLDisplayer.getDefault().showURL(report.toURI().toURL());
100             }
101         }catch(Exception JavaDoc ex){
102             if(report != null){
103                 String JavaDoc message = MessageFormat.format(bundle.getString("Err_ShowReport"), new Object JavaDoc[]{report.getAbsolutePath()}); //NOI18N
104
Util.showError(message);
105             }
106             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
107             return;
108         }
109     }
110     
111     private void instrumentAVK() {
112         if(this.sunDm.isLocal() == false){
113             return;
114         }
115         try{
116             DeploymentManagerProperties dmProps = new DeploymentManagerProperties((DeploymentManager JavaDoc) this.sunDm);
117             if(!dmProps.getAVKOn()){
118                 setTopManagerStatus(bundle.getString("MSG_Instrument")); //NOI18N
119
FileUtil.clearResults(this.sunDm, dmProps);
120                 if(this.sunDm.isRunning()){
121                     stopStartForInstrument(dmProps);
122                 }else{
123                     instrument(dmProps);
124                     startAfterInstrument(dmProps);
125                 }
126             }else{
127                 if(! this.sunDm.isRunning()){
128                     startAfterInstrument(dmProps);
129                 }
130             }
131         }catch(Exception JavaDoc ex){
132             Util.showError(bundle.getString("Err_Instrument")); //NOI18N
133
ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getLocalizedMessage());
134         }
135     }
136     
137     private void stopStartForInstrument(DeploymentManagerProperties dmProps) throws Exception JavaDoc {
138         try{
139             setTopManagerStatus(bundle.getString("MSG_Stop")); //NOI18N
140
stopServer(dmProps, this.sunDm);
141             dmProps.getInstanceProperties().refreshServerInstance();
142             instrument(dmProps);
143             startAfterInstrument(dmProps);
144         }catch(Exception JavaDoc ex){
145             throw ex;
146         }
147     }
148     
149     private void startAfterInstrument(DeploymentManagerProperties dmProps) throws Exception JavaDoc {
150         try{
151             setTopManagerStatus(bundle.getString("MSG_Start")); //NOI18N
152
startServer(dmProps, this.sunDm);
153             dmProps.getInstanceProperties().refreshServerInstance();
154             setTopManagerStatus(bundle.getString("MSG_AVK_Running")); //NOI18N
155
}catch(Exception JavaDoc ex){
156             throw ex;
157         }
158     }
159     
160     private void instrument(DeploymentManagerProperties dmProps){
161         editPolicyFile(dmProps);
162         editStoppedDomainConfig(dmProps, true);
163         dmProps.setAVKOn(true);
164         setTopManagerStatus(bundle.getString("MSG_AVK_Stopped")); //NOI18N
165
}
166     
167     public void uninstrumentAVK() {
168         DeploymentManagerProperties dmProps = new DeploymentManagerProperties((DeploymentManager JavaDoc) this.sunDm);
169         try{
170             setTopManagerStatus(bundle.getString("MSG_Generating")); //NOI18N
171
//Parse Domain Config to remove elements
172
editStoppedDomainConfig(dmProps, false);
173         }catch(Exception JavaDoc ex){
174             Util.showError(bundle.getString("Err_UnInstrument")); //NOI18N
175
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
176             return;
177         }
178     }
179     
180     public Object JavaDoc loadClass(String JavaDoc methodName, SunDeploymentManagerInterface sdm) throws Exception JavaDoc{
181         Object JavaDoc result = null;
182         File JavaDoc avkHome = getAVKHome();
183         DeploymentManager JavaDoc deployMgr = (DeploymentManager JavaDoc)sdm;
184         ClassLoader JavaDoc origClassLoader = Thread.currentThread().getContextClassLoader();
185         try{
186             Class JavaDoc[] argClass = new Class JavaDoc[2];
187             argClass[0] = javax.enterprise.deploy.spi.DeploymentManager JavaDoc.class;
188             argClass[1] = java.io.File JavaDoc.class;
189             Object JavaDoc[] argObject = new Object JavaDoc[2];
190             argObject[0] = deployMgr;
191             argObject[1] = avkHome;
192             Class JavaDoc controllerUtilClass = ServerLocationManager.getNetBeansAndServerClassLoader(sdm.getPlatformRoot()).
193                     loadClass("org.netbeans.modules.j2ee.sun.util.InstrumentAVK"); //NOI18N
194

195             
196             Method JavaDoc method = controllerUtilClass.getMethod(methodName, argClass);
197             
198             Thread.currentThread().setContextClassLoader(
199                     ServerLocationManager.getNetBeansAndServerClassLoader(sdm.getPlatformRoot()));
200             
201             result = method.invoke(controllerUtilClass.newInstance(), argObject);
202             
203         } catch (Exception JavaDoc e){
204             throw e;
205         } finally {
206             Thread.currentThread().setContextClassLoader(origClassLoader);
207         }
208         return result;
209     }
210     
211     public boolean runReportTool(){
212         try{
213             System.setProperty("j2ee.appverification.home", getAVKHome().getAbsolutePath());
214             System.setProperty("com.sun.aas.installRoot", sunDm.getPlatformRoot().getAbsolutePath());
215             DeploymentManagerProperties dmProps = new DeploymentManagerProperties((DeploymentManager JavaDoc) this.sunDm);
216         ExtendedClassLoader loader = (ExtendedClassLoader) ServerLocationManager.getServerOnlyClassLoader(sunDm.getPlatformRoot());
217             if(loader != null){
218                 File JavaDoc f = getAVKJarLocation();
219                 loader.addURL(f);
220             }
221
222             if(loader != null){
223                 String JavaDoc domainDir = dmProps.getLocation() + File.separator + dmProps.getDomainName();
224                 String JavaDoc args[] = {"-result", "-domainDir", domainDir, "-resultsDir", getAVKReportLocation()};
225                 Class JavaDoc cc = loader.loadClass("com.sun.enterprise.appverification.tools.ReportTool");
226                 
227                 Method JavaDoc method = cc.getMethod("main", new Class JavaDoc[] {args.getClass()});
228                 method.invoke(null, new Object JavaDoc[] { args });
229             }
230             return true;
231         }catch(Exception JavaDoc ex){
232             return false;
233         }
234     }
235     
236     public File JavaDoc getAVKHome(){
237         File JavaDoc f = getAVKJarLocation();
238         if(f != null)
239             f = f.getParentFile().getParentFile();
240         return f;
241     }
242     
243     private File JavaDoc getAVKJarLocation(){
244         File JavaDoc f = InstalledFileLocator.getDefault().locate("javke142/lib/javke.jar", null, true);
245         return f;
246     }
247     
248     private String JavaDoc getAVKReportLocation(){
249         String JavaDoc resultsDir = System.getProperty("netbeans.user") + File.separator + "avkreport"; //NOI18N
250
return resultsDir;
251     }
252     
253     public void stopServer(DeploymentManagerProperties dmProps, SunDeploymentManagerInterface sdm) throws Exception JavaDoc{
254         String JavaDoc asadminCmd = sdm.getPlatformRoot() + File.separator +
255                 "bin" +
256                 File.separator +
257                 "asadmin"; //NOI18N
258

259         if (File.separator.equals("\\")) {
260             asadminCmd = asadminCmd + ".bat"; //NOI18N
261
}
262         String JavaDoc args[]={asadminCmd, "stop-domain",
263                 dmProps.getDomainName()};
264         ViewLogAction.viewLog(sdm);
265         exec(args);
266         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
267         for(int i=0;i<args.length;++i){
268             sb.append(args[i]+ " ");
269         }
270     }
271     
272     public void startServer(DeploymentManagerProperties dmProps, SunDeploymentManagerInterface sdm) throws Exception JavaDoc{
273         String JavaDoc asadminCmd = sdm.getPlatformRoot() + File.separator +
274                 "bin" +
275                 File.separator +
276                 "asadmin"; //NOI18N
277

278         if (File.separator.equals("\\")) {
279             asadminCmd = asadminCmd + ".bat"; //NOI18N
280
}
281         String JavaDoc args[]={asadminCmd, "start-domain",
282                 dmProps.getDomainName()};
283         ViewLogAction.viewLog(sdm);
284         exec(args);
285         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
286         for(int i=0;i<args.length;++i){
287             sb.append(args[i]+ " ");
288         }
289     }
290     
291     private int exec(String JavaDoc[] arr) throws Exception JavaDoc{
292         final Process JavaDoc subProcess = Runtime.getRuntime().exec(arr);
293         //start a new thread that reads from the subprocess's output stream and prints it in deploytool log file.
294
new Thread JavaDoc(){ //an inner class
295
public void run(){
296                 try{
297                     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(subProcess.getInputStream()));
298                     String JavaDoc line=null;
299                     while ( (line = br.readLine()) != null){
300                         //logger.info(line);
301
}
302                 } catch (IOException JavaDoc ioe) {
303                     //logger.log(Level.SEVERE, "Exception" ,ioe);
304
}
305             }//run
306
}.start();
307         //start a new thread that reads from the subprocess's error stream and prints it in deploytool log file.
308
new Thread JavaDoc(){ //an inner class
309
public void run(){
310                 try{
311                     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(subProcess.getErrorStream()));
312                     String JavaDoc line=null;
313                     while ( (line = br.readLine()) != null) {
314                         //logger.info(line);
315
}
316                 } catch (IOException JavaDoc ioe) {
317                     //logger.log(Level.SEVERE, "Exception" ,ioe);
318
}
319             }//run
320
}.start();
321         while(true){
322             try{
323                 int exitValue=subProcess.waitFor();
324                 //logger.log(Level.FINER, "Process exited with exit value " + exitValue);
325
return exitValue;
326             }catch(InterruptedException JavaDoc e){
327             }
328         }
329     }
330     
331     public void editPolicyFile(DeploymentManagerProperties dmProps){
332         System.setProperty("j2ee.appverification.home", getAVKHome().getAbsolutePath()); //NOI18N
333
String JavaDoc policyFile = dmProps.getLocation() + File.separator + dmProps.getDomainName() + File.separator + "config" + File.separator + "server.policy";
334         if(policyFile == null)
335             return;
336         try{
337             DomainParser.backupFile(policyFile);
338             BufferedReader JavaDoc textIn = new BufferedReader JavaDoc(new FileReader JavaDoc(policyFile));
339             String JavaDoc line;
340             boolean edited = false;
341             while ((line = textIn.readLine()) != null){
342                 if(line.indexOf("j2ee.appverification.home") != -1){ //NOI18N
343
edited = true;
344                     break;
345                 }
346                 
347             }
348             textIn.close();
349             if(! edited){
350                 PrintWriter JavaDoc textOut = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new FileWriter JavaDoc(policyFile, true)));
351                 textOut.println("// permissions for avkit classes"); //NOI18N
352
textOut.println("grant codeBase \"file:${j2ee.appverification.home}/-\" {"); //NOI18N
353
textOut.println("permission java.security.AllPermission;"); //NOI18N
354
textOut.println("};"); //NOI18N
355
textOut.flush();
356                 textOut.close();
357             }
358         }catch(Exception JavaDoc ex){
359             DomainParser.restoreFile(policyFile);
360         }
361     }
362     
363     public void editStoppedDomainConfig(DeploymentManagerProperties dmProps, boolean onOff){
364         File JavaDoc avkHome = getAVKHome();
365         String JavaDoc domainXmlLoc = getDomainConfigLoc(dmProps) + File.separator + "domain.xml";
366         DomainParser.backupFile(domainXmlLoc);
367         boolean success = DomainParser.editSupportInDomain(domainXmlLoc, getAVKJarLocation().getAbsolutePath(), avkHome.getAbsolutePath(), onOff);
368         if(!success){
369             DomainParser.restoreFile(domainXmlLoc);
370         }
371     }
372     
373     private String JavaDoc getDomainConfigLoc(DeploymentManagerProperties dmProps){
374         String JavaDoc configLoc = dmProps.getLocation() + File.separator + dmProps.getDomainName() + File.separator + "config"; //NOI18N
375
return configLoc;
376     }
377     
378     private void setTopManagerStatus(String JavaDoc msg){
379         StatusDisplayer.getDefault().setStatusText(msg);
380     }
381     
382     public boolean createAVKSupport(DeploymentManager JavaDoc dm, J2eeModuleProvider modProvider){
383         AddAVKSupport support = new AddAVKSupport();
384         return support.createAVKSupport(dm, modProvider);
385     }
386     
387 }
388
Popular Tags