001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 2005-2006 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.tcpmon.integration;
021:
022: import java.io.IOException;
023: import java.net.MalformedURLException;
024: import java.net.URL;
025: import java.util.Arrays;
026: import java.util.HashMap;
027: import java.util.LinkedList;
028: import java.util.List;
029: import java.util.Map;
030: import javax.swing.SwingUtilities;
031: import org.apache.tools.ant.module.api.support.ActionUtils;
032: import org.netbeans.api.progress.ProgressHandle;
033: import org.netbeans.api.progress.ProgressHandleFactory;
034: import org.netbeans.api.project.Project;
035: import org.netbeans.modules.j2ee.api.ejbjar.Car;
036: import org.netbeans.modules.j2ee.dd.api.client.AppClient;
037: import org.netbeans.modules.j2ee.dd.api.client.DDProvider;
038: import org.netbeans.modules.j2ee.dd.api.common.ServiceRef;
039: import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException;
040: import org.netbeans.modules.j2ee.dd.api.ejb.Ejb;
041: import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
042: import org.netbeans.modules.j2ee.dd.api.web.WebApp;
043: import org.netbeans.modules.j2ee.metadata.NNMDRListener;
044: import org.netbeans.modules.web.api.webmodule.WebModule;
045: import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport;
046: import org.netbeans.modules.websvc.api.jaxws.project.config.Client;
047: import org.netbeans.modules.websvc.spi.client.WebServicesClientSupportProvider;
048: import org.openide.ErrorManager;
049: import org.openide.filesystems.FileObject;
050: import org.openide.nodes.Node;
051: import org.openide.util.HelpCtx;
052: import org.openide.util.NbBundle;
053: import org.openide.util.actions.CookieAction;
054:
055: /**
056: *
057: * @author Lukas Jungmann
058: */
059: public final class RunMonitor extends CookieAction {
060:
061: private static int port = 10000;
062:
063: protected boolean enable(Node[] activatedNodes) {
064: if (!super .enable(activatedNodes)) {
065: return false;
066: }
067: Project p = (Project) activatedNodes[0].getLookup().lookup(
068: Project.class);
069: WebServicesClientSupportProvider wscsp = (WebServicesClientSupportProvider) p
070: .getLookup().lookup(
071: WebServicesClientSupportProvider.class);
072: if (wscsp == null) {
073: return false;
074: }
075: JAXWSClientSupport wsSupport = wscsp.findJAXWSClientSupport(p
076: .getProjectDirectory());
077: return wsSupport != null
078: && wsSupport.getServiceClients().size() > 0;
079: }
080:
081: protected void performAction(Node[] activatedNodes) {
082: final Project p = (Project) activatedNodes[0].getLookup()
083: .lookup(Project.class);
084: WebServicesClientSupportProvider wscsp = (WebServicesClientSupportProvider) p
085: .getLookup().lookup(
086: WebServicesClientSupportProvider.class);
087: if (wscsp == null) {
088: return;
089: }
090: final JAXWSClientSupport wsSupport = wscsp
091: .findJAXWSClientSupport(p.getProjectDirectory());
092: if (wsSupport != null) {
093: ProgressHandle handle = ProgressHandleFactory
094: .createHandle(NbBundle.getMessage(RunMonitor.class,
095: "CTL_RunMonitorProgress")); //NOI18N
096: handle.start();
097: Map<Client, Integer> monitoredClients = new HashMap<Client, Integer>();
098: //TODO: replace this with some UI component where one will be able
099: // to choose which ws client to monitor.
100: // for now all clients in given project are monitored
101: StringBuilder sb = new StringBuilder();
102: int pp;
103: List<ServiceRef> refs = getServiceRefs(p
104: .getProjectDirectory());
105: for (Object o : wsSupport.getServiceClients()) {
106: Client cc = (Client) o;
107: pp = port++;
108: sb.append(cc.getName());
109: sb.append('$');
110: //TODO: some check wheter port No is in use or not
111: sb.append(pp);
112: sb.append('$');
113: for (ServiceRef ref : refs) {
114: // System.err.println("---");
115: // System.err.println(ref.getWsdlFile().toString());
116: // System.err.println(cc.getWsdlUrl());
117: if (ref.getWsdlFile().toString().equalsIgnoreCase(
118: cc.getWsdlUrl())) {
119: String refName = ref.getServiceRefName();
120: sb.append(refName.substring(0,
121: refName.lastIndexOf('/')).replace('.',
122: '/'));
123: sb.append(".java"); //NOI18N
124: break;
125: }
126: }
127: sb.append('$');
128: monitoredClients.put(cc, new Integer(pp));
129: }
130: //end to replace
131:
132: // System.err.println("passing: " + sb.toString());
133:
134: //generate build script
135: FileObject projectDir = p.getProjectDirectory();
136: BuildScriptGenerator bsg = new BuildScriptGenerator(
137: projectDir);
138: FileObject jaxWsFo = projectDir
139: .getFileObject("nbproject/jax-ws.xml"); //NOI18N
140: try {
141: bsg
142: .generateBuildScriptFromStylesheet(
143: "nbproject/private/monitor.xml", //NOI18N
144: RunMonitor.class
145: .getResource("resources/build-monitor.xsl"), //NOI18N
146: jaxWsFo, sb.toString());
147: } catch (IOException ioe) {
148: ErrorManager.getDefault().notify(ioe);
149: }
150: SwingUtilities.invokeLater(new Displayer(monitoredClients));
151: handle.finish();
152: try {
153: ActionUtils
154: .runTarget(
155: projectDir
156: .getFileObject("nbproject/private/monitor.xml"), //NOI18N
157: new String[] { "run" }, //NOI18N
158: null);
159: } catch (IOException ioe) {
160: ErrorManager.getDefault().notify(ioe);
161: }
162: } else {
163: System.err.println("no test"); //NOI18N
164: }
165: }
166:
167: protected int mode() {
168: return CookieAction.MODE_EXACTLY_ONE;
169: }
170:
171: public String getName() {
172: return NbBundle.getMessage(RunMonitor.class, "CTL_RunMonitor"); //NOI18N
173: }
174:
175: protected Class[] cookieClasses() {
176: return new Class[] { Project.class };
177: }
178:
179: protected String iconResource() {
180: return "org/netbeans/modules/tcpmon/integration/resources/menuitem.gif"; //NOI18N
181: }
182:
183: public HelpCtx getHelpCtx() {
184: return HelpCtx.DEFAULT_HELP;
185: }
186:
187: protected boolean asynchronous() {
188: return true;
189: }
190:
191: private List<ServiceRef> getServiceRefs(FileObject projectDir) {
192: List<ServiceRef> refs = new LinkedList<ServiceRef>();
193: refs.addAll(Arrays.asList(getRefFromAppClient(projectDir)));
194: refs.addAll(getRefFromEjbJar(projectDir));
195: refs.addAll(Arrays.asList(getRefFromWebApp(projectDir)));
196: return refs;
197: }
198:
199: private ServiceRef[] getRefFromAppClient(FileObject fo) {
200: Car appclient = Car.getCar(fo);
201: if (appclient != null) {
202: try {
203: AppClient ac = DDProvider.getDefault().getMergedDDRoot(
204: appclient);
205: if (ac != null) {
206: NNMDRListener.getDefault().waitScanFinished();
207: return ac.getServiceRef();
208: }
209: } catch (IOException ioe) {
210: } catch (VersionNotSupportedException vnse) {
211: }
212: }
213: return new ServiceRef[0];
214: }
215:
216: private List<ServiceRef> getRefFromEjbJar(FileObject fo) {
217: org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbModule = org.netbeans.modules.j2ee.api.ejbjar.EjbJar
218: .getEjbJar(fo);
219: List<ServiceRef> refs = new LinkedList<ServiceRef>();
220: if (ejbModule != null) {
221: try {
222: EjbJar ejbDD = org.netbeans.modules.j2ee.dd.api.ejb.DDProvider
223: .getDefault().getMergedDDRoot(
224: ejbModule.getMetadataUnit());
225: if (ejbDD != null) {
226: NNMDRListener.getDefault().waitScanFinished();
227: for (Ejb ejb : ejbDD.getEnterpriseBeans().getEjbs()) {
228: refs.addAll(Arrays.asList(ejb.getServiceRef()));
229: }
230: }
231: } catch (IOException ioe) {
232: } catch (VersionNotSupportedException vnse) {
233: }
234: }
235: return refs;
236: }
237:
238: private ServiceRef[] getRefFromWebApp(FileObject fo) {
239: WebModule wm = WebModule.getWebModule(fo);
240: if (wm != null) {
241: try {
242: WebApp wa = org.netbeans.modules.j2ee.dd.api.web.DDProvider
243: .getDefault().getMergedDDRoot(wm);
244: if (wa != null) {
245: NNMDRListener.getDefault().waitScanFinished();
246: return wa.getServiceRef();
247: }
248: } catch (IOException ioe) {
249: } catch (VersionNotSupportedException vnse) {
250: }
251: }
252: return new ServiceRef[0];
253: }
254:
255: private static final class Displayer implements Runnable {
256:
257: private Map<Client, Integer> clients;
258:
259: Displayer(Map<Client, Integer> clients) {
260: this .clients = clients;
261: }
262:
263: public void run() {
264: TcpMonitorTopComponent tmtc = TcpMonitorTopComponent
265: .findInstance();
266: if (!tmtc.isOpened()) {
267: tmtc.open();
268: }
269: tmtc.requestActive();
270: for (Client c : clients.keySet()) {
271: int p2 = getPort(c.getWsdlUrl());
272: if (p2 < 0) {
273: p2 = 80;
274: }
275: // System.err.println("args:");
276: // System.err.println(getDomainName(c.getWsdlUrl()));
277: // System.err.println(p2);
278: // System.err.println(monitoredClients.get(c).intValue());
279: // System.err.println(c.getName());
280: // System.err.println("---");
281: tmtc.addNewMonitorTab(getDomainName(c.getWsdlUrl()),
282: p2, clients.get(c).intValue(), true, c
283: .getName());
284: }
285: }
286:
287: private int getPort(String url) {
288: int port = -1;
289: try {
290: port = new URL(url).getPort();
291: } catch (MalformedURLException ex) {
292: }
293: return (port > 0) ? port : 80;
294: }
295:
296: private String getDomainName(String url) {
297: String retVal = null;
298: try {
299: retVal = new URL(url).getHost();
300: } catch (MalformedURLException ex) {
301: int index = url.indexOf("//");
302: if (index < 0) {
303: retVal = url.substring(0, url.indexOf('/'));
304: } else {
305: ErrorManager.getDefault().notify(
306: ErrorManager.INFORMATIONAL, ex);
307: }
308: }
309: return retVal;
310: }
311:
312: }
313: }
|