1 package org.netbeans.modules.j2ee.sun.ide.avk.actions; 2 3 import java.io.File ; 4 import java.net.URL ; 5 import java.text.SimpleDateFormat ; 6 import java.util.Date ; 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 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 getName() { 43 return NbBundle.getMessage(StartInstrumentation.class, "CTL_StartInstrumentation"); 44 } 45 46 48 public void performAction(Node[] nodes) { 49 } 51 52 public void start(final Project project) { 53 54 new Thread () { 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 67 if(isRunning && sdm.isRunning()) { 71 AdminTasks.debug("AVK is already running....storing the prev session resuls...."); 72 adminTasks.generateReport(curResultsDir); 74 75 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 96 if(!isVerifierSetup) { 97 AdminTasks.debug("Doing AVK setup...copying jar..."); 98 showProgress("CTL_PerformingSetup"); 99 adminTasks.copyJar(); 100 hideProgress(); 101 } 102 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 121 if(!isVerifierSetup) { 122 AdminTasks.debug("Doing AVK setup...creating AVK Mbean and Rule..."); 123 adminTasks.createVerifierMBeanAndRule(); 124 125 } 126 129 AdminTasks.debug("Starting instrumentation..."); 130 adminTasks.startInstrumentation(); 131 } 132 133 136 String 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 148 AdminTasks.debug("Running run build target..."); 149 runProject(project); 150 151 153 startReportUI(project); 154 155 isRunning = true; 156 } 157 }.start(); 158 } 159 160 private String getNewResultsDir(Project project) { 161 String domainDir = getDomainDir(project); 162 SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd-kk-mm"); 163 String newDirName = domainDir + File.separator + 164 "logs" + File.separator + 165 "reporttool" + File.separator + 166 df.format(new Date ()); 167 File newDir = new File (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 179 SunDeploymentManagerInterface sdm = getDeploymentManager(project); 180 J2eeModuleProvider moduleProvider = (J2eeModuleProvider)projectLookup.lookup(J2eeModuleProvider.class); 181 182 launchApp(sdm, getContextRoot(moduleProvider)); 183 184 187 } catch(Exception ex) { 189 ex.printStackTrace(); 190 } 191 } 192 193 private String getContextRoot(J2eeModuleProvider modProvider){ 194 AdminTasks.debug("Modprovider = " + modProvider); 195 String url = null; 196 if(modProvider != null){ 197 if(modProvider instanceof J2eeAppProvider){ 198 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 ex) { 219 ex.printStackTrace(); 220 } 221 return url; 222 } 223 224 private void launchApp(SunDeploymentManagerInterface sdm, String contextRoot){ 225 AdminTasks.debug("LaunchApp : contextRoot = " + contextRoot); 226 try { 227 if(contextRoot != null){ 228 String httpport = sdm.getNonAdminPortNumber() == null ? "8080" : sdm.getNonAdminPortNumber(); 229 String start = "http://" + sdm.getHost() + ":" + httpport; if (contextRoot.startsWith("/")) contextRoot = start + contextRoot; 232 else 233 contextRoot = start + "/" + contextRoot; AdminTasks.debug("URL = " + contextRoot); 235 URLDisplayer.getDefault().showURL(new URL (contextRoot)); 236 } 237 } catch(Exception 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.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 monitoredFile = new File (curResultsDir + File.separator + "instrumentation" + File.separator + "result.xml"); 259 if(!monitoredFile.exists()) { 260 return; 261 } 262 265 new Thread () { 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 ex) { 281 AdminTasks.debug("File monitor thread can not sleep. " + ex.getMessage()); 282 } 283 } 284 } 285 } 286 }.start(); 287 } 288 } 289 | Popular Tags |