KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.netbeans.modules.j2ee.sun.ide.avk.actions;
2
3 import java.io.File JavaDoc;
4 import java.net.URL JavaDoc;
5 import java.text.SimpleDateFormat JavaDoc;
6 import java.util.Date JavaDoc;
7 import org.netbeans.api.project.FileOwnerQuery;
8 import org.netbeans.api.project.Project;
9 import org.netbeans.modules.j2ee.dd.api.application.Application;
10 import org.netbeans.modules.j2ee.dd.api.application.Module;
11 import org.netbeans.modules.j2ee.dd.api.application.Web;
12 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
13 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeAppProvider;
14 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
15 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface;
16 import org.netbeans.modules.j2ee.sun.ide.avk.report.model.ReportModel;
17 import org.netbeans.modules.j2ee.sun.ide.avk.report.view.ReportAction;
18 import org.netbeans.spi.project.ActionProvider;
19 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
20 import org.openide.awt.HtmlBrowser.URLDisplayer;
21 import org.openide.filesystems.FileObject;
22 import org.openide.nodes.Node;
23 import org.openide.util.Lookup;
24 import org.openide.util.NbBundle;
25
26 /**
27  * @author bshankar@sun.com
28  *
29  */

30 public final class StartInstrumentation extends CommonNodeAction {
31     
32     private boolean isRunning = false;
33     private static StartInstrumentation instance = null;
34     
35     public static synchronized StartInstrumentation getInstance() {
36         if (instance == null) {
37             instance = new StartInstrumentation();
38         }
39         return instance;
40     }
41     
42     public String JavaDoc getName() {
43         return NbBundle.getMessage(StartInstrumentation.class, "CTL_StartInstrumentation");
44     }
45     
46     // Custom code starts here.
47

48     public void performAction(Node[] nodes) {
49         //start(nodes[0]);
50
}
51     
52     public void start(final Project project) {
53         
54         new Thread JavaDoc() {
55             
56             public synchronized void run() {
57                 
58                 SunDeploymentManagerInterface sdm = getDeploymentManager(project);
59                 final ActionProvider ap = getActionProvider(project);
60                 final Lookup projectLookup = project.getLookup();
61                 AdminTasks adminTasks = new AdminTasks(sdm);
62                 
63                 /**
64                  * If the AVK session is already running, then request the MBean to dump the previous sessions results.
65                  * Else, do the verifier setup, run deploy and start instrumentation
66                  */

67                 if(isRunning && sdm.isRunning()) { //
68
/**
69                      * Step1. If AVK is already running then store the prev session results.
70                      */

71                     AdminTasks.debug("AVK is already running....storing the prev session resuls....");
72                     //adminTasks.storeInstrumentationResults(curResultsDir, true);
73
adminTasks.generateReport(curResultsDir);
74                     
75                     /**
76                      * Step2. Run project's 'deploy' target.
77                      */

78                     AdminTasks.debug("Running redeploy build target...");
79                     
80                     deployProject(project);
81                     
82                     AdminTasks.debug("Redeploy completed...");
83                     
84                     AdminTasks.debug("Deploy Dir = " + getDeployDir(project));
85                     
86                     if(getDeployDir(project) == null) {
87                         showError("CTL_DeploymentFailed");
88                         return;
89                     }
90                 } else {
91                     boolean isVerifierSetup = adminTasks.isSetup();
92                     AdminTasks.debug("isVerifierSetup - " + isVerifierSetup);
93                     /**
94                      * Step1. If AVK is not setup in the target server then do the setup.
95                      */

96                     if(!isVerifierSetup) {
97                         AdminTasks.debug("Doing AVK setup...copying jar...");
98                         showProgress("CTL_PerformingSetup");
99                         adminTasks.copyJar();
100                         hideProgress();
101                     }
102                     /**
103                      * Step2. Run project's 'deploy' target.
104                      */

105                     AdminTasks.debug("Running redeploy build target...");
106                     
107                     deployProject(project);
108                     
109                     AdminTasks.debug("Redeploy completed...");
110                     
111                     AdminTasks.debug("Deploy Dir = " + getDeployDir(project));
112                     
113                     if(getDeployDir(project) == null) {
114                         showError("CTL_DeploymentFailed");
115                         return;
116                     }
117                     
118                     /**
119                      * Step3. If AVK is not setup then create AVK MBean and Rule.
120                      */

121                     if(!isVerifierSetup) {
122                         AdminTasks.debug("Doing AVK setup...creating AVK Mbean and Rule...");
123                         adminTasks.createVerifierMBeanAndRule();
124                         
125                     }
126                     /**
127                      * Step4. Start Instrumentation.
128                      */

129                     AdminTasks.debug("Starting instrumentation...");
130                     adminTasks.startInstrumentation();
131                 }
132                 
133                 /**
134                  * Do introspection.
135                  */

136                 String JavaDoc deployDir = getDeployDir(project);
137                 curResultsDir = getNewResultsDir(project);
138                 
139                 AdminTasks.debug("deployDir = " + deployDir);
140                 AdminTasks.debug("resultDir = " + curResultsDir);
141                 
142                 AdminTasks.debug("Doing introspection...");
143                 adminTasks.storeIntrospectionResults(deployDir, curResultsDir);
144                 
145                 /**
146                  * Run the project.
147                  */

148                 AdminTasks.debug("Running run build target...");
149                 runProject(project);
150                 
151                 //ap.invokeAction("run", projectLookup);
152

153                 startReportUI(project);
154                 
155                 isRunning = true;
156             }
157         }.start();
158     }
159     
160     private String JavaDoc getNewResultsDir(Project project) {
161         String JavaDoc domainDir = getDomainDir(project);
162         SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc("yyyy-MM-dd-kk-mm");
163         String JavaDoc newDirName = domainDir + File.separator +
164                 "logs" + File.separator +
165                 "reporttool" + File.separator +
166                 df.format(new Date JavaDoc());
167         File JavaDoc newDir = new File JavaDoc(newDirName);
168         newDir.mkdirs();
169         return newDirName;
170     }
171     
172     private void runProject(Project project) {
173         try {
174             Lookup projectLookup = project.getLookup();
175             FileObject buildXML = project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
176             /**
177              * Run the application in browser.
178              */

179             SunDeploymentManagerInterface sdm = getDeploymentManager(project);
180             J2eeModuleProvider moduleProvider = (J2eeModuleProvider)projectLookup.lookup(J2eeModuleProvider.class);
181             
182             launchApp(sdm, getContextRoot(moduleProvider));
183             
184             /**
185              * Run application client.
186              */

187             //ActionUtils.runTarget(buildXML, new String[]{"run-ac"}, new Properties());
188
} catch(Exception JavaDoc ex) {
189             ex.printStackTrace();
190         }
191     }
192     
193     private String JavaDoc getContextRoot(J2eeModuleProvider modProvider){
194         AdminTasks.debug("Modprovider = " + modProvider);
195         String JavaDoc url = null;
196         if(modProvider != null){
197             if(modProvider instanceof J2eeAppProvider){
198                 //Get contextRoot of first web module in application.xml
199
Application appXml = (Application)modProvider.getJ2eeModule().getDeploymentDescriptor(J2eeModule.APP_XML);
200                 AdminTasks.debug("appXml = " + appXml);
201                 Module[] mods = appXml.getModule();
202                 AdminTasks.debug("mods.length = " + mods.length);
203                 for(int i=0; i<mods.length; i++){
204                     Web webMod = mods[i].getWeb();
205                     AdminTasks.debug("webMod = " + webMod);
206                     if(webMod != null){
207                         url = webMod.getContextRoot();
208                         AdminTasks.debug("url = " + url);
209                         break;
210                     }
211                 }
212             }else{
213                 url = modProvider.getConfigSupport().getWebContextRoot();
214             }
215         }
216         try {
217             AdminTasks.debug("Config support url - " + modProvider.getConfigSupport().getWebContextRoot());
218         } catch(Exception JavaDoc ex) {
219             ex.printStackTrace();
220         }
221         return url;
222     }
223     
224     private void launchApp(SunDeploymentManagerInterface sdm, String JavaDoc contextRoot){
225         AdminTasks.debug("LaunchApp : contextRoot = " + contextRoot);
226         try {
227             if(contextRoot != null){
228                 String JavaDoc httpport = sdm.getNonAdminPortNumber() == null ? "8080" : sdm.getNonAdminPortNumber();
229                 String JavaDoc start = "http://" + sdm.getHost() + ":" + httpport; //NOI18N
230
if (contextRoot.startsWith("/")) //NOI18N
231
contextRoot = start + contextRoot;
232                 else
233                     contextRoot = start + "/" + contextRoot; //NOI18N
234
AdminTasks.debug("URL = " + contextRoot);
235                 URLDisplayer.getDefault().showURL(new URL JavaDoc(contextRoot));
236             }
237         } catch(Exception JavaDoc ex) {
238             ex.printStackTrace();
239         }
240     }
241     
242     private void startReportUI(Project project) {
243         
244         AdminTasks.debug("Starting UI with curResultDir = " + curResultsDir);
245         
246         final ReportAction reportAction = new ReportAction();
247         SunDeploymentManagerInterface sdm = getDeploymentManager(project);
248         final AdminTasks adminTasks = new AdminTasks(sdm);
249         AdminTasks.debug("Current result dir = " + curResultsDir);
250         adminTasks.startAutoWriteInstrumentationResults(curResultsDir);
251         //adminTasks.storeInstrumentationResults(curResultsDir, false);
252
adminTasks.generateReport(curResultsDir);
253         
254         final ReportModel avkReport = ReportModel.getInstance();
255         avkReport.init(curResultsDir + File.separator + "results" + File.separator + "result.xml");
256         reportAction.actionPerformed(null);
257         
258         final File JavaDoc monitoredFile = new File JavaDoc(curResultsDir + File.separator + "instrumentation" + File.separator + "result.xml");
259         if(!monitoredFile.exists()) {
260             return;
261         }
262         /**
263          * Update the UI whenever a new instrumentation data is available.
264          */

265         new Thread JavaDoc() {
266             public void run() {
267                 long lastModified = 0L;
268                 while(true) {
269                     if(lastModified != monitoredFile.lastModified()) {
270                         lastModified = monitoredFile.lastModified();
271                         adminTasks.generateReport(curResultsDir);
272                         if(reportAction.isUIClosed())
273                             break;
274                         AdminTasks.debug("File modified....New report generated...refreshing UI...");
275                         avkReport.init(curResultsDir + File.separator + "results" + File.separator + "result.xml");
276                         reportAction.repaint();
277                         AdminTasks.debug("Refresing UI done...");
278                         try {
279                             sleep(10000);
280                         } catch(Exception JavaDoc ex) {
281                             AdminTasks.debug("File monitor thread can not sleep. " + ex.getMessage());
282                         }
283                     }
284                 }
285             }
286         }.start();
287     }
288 }
289
Popular Tags