KickJava   Java API By Example, From Geeks To Geeks.

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


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  * AddAVKSupport.java
21  *
22  * Created on September 19, 2005, 4:21 PM
23  */

24
25 package org.netbeans.modules.j2ee.sun.ide.avk;
26
27 import java.io.File JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.awt.Dialog JavaDoc;
30 import java.util.ResourceBundle JavaDoc;
31 import org.netbeans.api.project.FileOwnerQuery;
32 import org.netbeans.api.project.Project;
33 import org.netbeans.modules.j2ee.sun.ide.avk.actions.StartInstrumentation;
34 import org.openide.nodes.Node;
35
36 import org.openide.util.HelpCtx;
37 import org.openide.DialogDescriptor;
38 import org.openide.DialogDisplayer;
39 import org.openide.NotifyDescriptor;
40 import org.openide.util.Lookup;
41 import org.openide.util.RequestProcessor;
42 import org.openide.filesystems.FileObject;
43 import org.openide.awt.HtmlBrowser.URLDisplayer;
44
45 import org.netbeans.modules.j2ee.dd.api.application.Web;
46 import org.netbeans.modules.j2ee.dd.api.application.Module;
47 import org.netbeans.modules.j2ee.dd.api.application.Application;
48 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
49 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeAppProvider;
50 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
51
52 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
53
54 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface;
55 import org.netbeans.modules.j2ee.sun.ide.j2ee.DeploymentManagerProperties;
56
57 /**
58  *
59  * @author Nitya Doraisamy
60  */

61 public class AddAVKSupport {
62     static ResourceBundle JavaDoc bundle = ResourceBundle.getBundle("org.netbeans.modules.j2ee.sun.ide.avk.Bundle");// NOI18N
63
boolean instrumentServer = false;
64     boolean verify = false;
65      
66     private RequestProcessor processor = new RequestProcessor("instrument"); //NOI18N
67

68     public static final String JavaDoc APPSERVER_VERSION_UNKNOWN = "unknown"; // NOI18N
69
public static final String JavaDoc APPSERVER_VERSION_9 = "9.0"; // NOI18N
70
public static final String JavaDoc APPSERVER_VERSION_8_x = "8.x"; // NOI18N
71

72     /** Creates new AddAVKSupport */
73     public AddAVKSupport(){
74     }
75     
76     public boolean createAVKSupport(final DeploymentManager JavaDoc dm, final J2eeModuleProvider modProvider) {
77         AddAVKFrame frame = new AddAVKFrame();
78         DialogDescriptor dd = new DialogDescriptor(frame, bundle.getString("LBL_ChooseVerification"), true, null);
79         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(dd);
80         dialog.setLocation(320, 325);
81         dialog.pack();
82         dialog.setVisible(true);
83         if (dd.getValue() == DialogDescriptor.OK_OPTION) {
84            instrumentServer = frame.instrumentServer();
85            verify = frame.verify();
86            if(instrumentServer) {
87                instrument(dm, modProvider);
88                return false;
89            } else
90                return verify;
91         }else{
92             instrumentServer = false;
93             verify = false;
94             return false;
95         }
96     }
97         
98     public boolean instrumentServer(){
99         return instrumentServer;
100     }
101
102     public boolean verify(){
103         return verify;
104     }
105     
106     public boolean instrumentAndVerify(){
107         return (verify && instrumentServer);
108     }
109     
110     static class AddAVKFrame extends javax.swing.JPanel JavaDoc {
111                         
112             public AddAVKFrame(){
113                 initComponents();
114                 staticRadioButton.setSelected(true);
115                 HelpCtx.setHelpIDString(this, "AVKVerifyDialog");//NOI18N
116
}
117             /** This method is called from within the constructor to
118              * initialize the form.
119              * WARNING: Do NOT modify this code. The content of this method is
120              * always regenerated by the Form Editor.
121              */

122     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
123
private void initComponents() {
124         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
125
126         jPanel1 = new javax.swing.JPanel JavaDoc();
127         jTextPane1 = new javax.swing.JTextPane JavaDoc();
128         jPanel2 = new javax.swing.JPanel JavaDoc();
129         staticRadioButton = new javax.swing.JRadioButton JavaDoc();
130         dynamicRadioButton = new javax.swing.JRadioButton JavaDoc();
131         staticTextArea = new javax.swing.JTextArea JavaDoc();
132         dynamicTextArea = new javax.swing.JTextArea JavaDoc();
133
134         setLayout(new java.awt.GridBagLayout JavaDoc());
135
136         setEnabled(false);
137         setFocusable(false);
138         setMaximumSize(new java.awt.Dimension JavaDoc(500, 200));
139         setMinimumSize(new java.awt.Dimension JavaDoc(500, 200));
140         setPreferredSize(new java.awt.Dimension JavaDoc(500, 200));
141         setRequestFocusEnabled(false);
142         setVerifyInputWhenFocusTarget(false);
143         getAccessibleContext().setAccessibleName(bundle.getString("LBL_ChooseVerification"));
144         getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/sun/ide/avk/Bundle").getString("LBL_ChooseVerification"));
145         jPanel1.setLayout(new java.awt.GridBagLayout JavaDoc());
146
147         jTextPane1.setEditable(false);
148         jTextPane1.setText(bundle.getString("MSG_ChooseVerification"));
149         jTextPane1.setFocusCycleRoot(false);
150         jTextPane1.setFocusable(false);
151         jTextPane1.setOpaque(false);
152         jTextPane1.setRequestFocusEnabled(false);
153         jTextPane1.setVerifyInputWhenFocusTarget(false);
154         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
155         gridBagConstraints.gridx = 0;
156         gridBagConstraints.gridy = 0;
157         gridBagConstraints.gridwidth = 15;
158         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
159         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
160         gridBagConstraints.weightx = 1.0;
161         gridBagConstraints.weighty = 1.0;
162         jPanel1.add(jTextPane1, gridBagConstraints);
163         jTextPane1.getAccessibleContext().setAccessibleName(bundle.getString("LBL_ChooseVerification"));
164         jTextPane1.getAccessibleContext().setAccessibleDescription(bundle.getString("MSG_ChooseVerification"));
165
166         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
167         gridBagConstraints.gridx = 0;
168         gridBagConstraints.gridy = 0;
169         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
170         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
171         gridBagConstraints.weightx = 1.0;
172         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 12);
173         add(jPanel1, gridBagConstraints);
174
175         jPanel2.setLayout(new java.awt.GridBagLayout JavaDoc());
176
177         staticRadioButton.setText(bundle.getString("LBL_StaticVerification"));
178         staticRadioButton.addItemListener(new java.awt.event.ItemListener JavaDoc() {
179             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
180                 staticRadioButtonItemStateChanged(evt);
181             }
182         });
183
184         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
185         gridBagConstraints.gridx = 0;
186         gridBagConstraints.gridy = 0;
187         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
188         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
189         gridBagConstraints.weightx = 1.0;
190         gridBagConstraints.weighty = 1.0;
191         jPanel2.add(staticRadioButton, gridBagConstraints);
192         staticRadioButton.getAccessibleContext().setAccessibleName(bundle.getString("LBL_StaticVerification"));
193         staticRadioButton.getAccessibleContext().setAccessibleDescription(bundle.getString("LBL_StaticVerification"));
194
195         dynamicRadioButton.setText(bundle.getString("LBL_DynamicVerification"));
196         dynamicRadioButton.addItemListener(new java.awt.event.ItemListener JavaDoc() {
197             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
198                 dynamicRadioButtonItemStateChanged(evt);
199             }
200         });
201
202         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
203         gridBagConstraints.gridx = 0;
204         gridBagConstraints.gridy = 2;
205         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
206         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
207         gridBagConstraints.weightx = 1.0;
208         gridBagConstraints.weighty = 1.0;
209         jPanel2.add(dynamicRadioButton, gridBagConstraints);
210         dynamicRadioButton.getAccessibleContext().setAccessibleName(bundle.getString("LBL_DynamicVerification"));
211         dynamicRadioButton.getAccessibleContext().setAccessibleDescription(bundle.getString("LBL_DynamicVerification"));
212
213         staticTextArea.setFont(new java.awt.Font JavaDoc("Microsoft Sans Serif", 0, 11));
214         staticTextArea.setLineWrap(true);
215         staticTextArea.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/sun/ide/avk/Bundle").getString("Msg_InfoVerifier"));
216         staticTextArea.setWrapStyleWord(true);
217         staticTextArea.setOpaque(false);
218         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
219         gridBagConstraints.gridx = 0;
220         gridBagConstraints.gridy = 1;
221         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
222         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
223         gridBagConstraints.weightx = 1.0;
224         gridBagConstraints.weighty = 1.0;
225         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 19, 0, 0);
226         jPanel2.add(staticTextArea, gridBagConstraints);
227         staticTextArea.getAccessibleContext().setAccessibleName(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/sun/ide/avk/Bundle").getString("LBL_StaticVerification"));
228         staticTextArea.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/sun/ide/avk/Bundle").getString("Msg_InfoVerifier"));
229
230         dynamicTextArea.setFont(new java.awt.Font JavaDoc("Microsoft Sans Serif", 0, 11));
231         dynamicTextArea.setLineWrap(true);
232         dynamicTextArea.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/sun/ide/avk/Bundle").getString("Msg_InfoAVK"));
233         dynamicTextArea.setWrapStyleWord(true);
234         dynamicTextArea.setOpaque(false);
235         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
236         gridBagConstraints.gridx = 0;
237         gridBagConstraints.gridy = 3;
238         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
239         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
240         gridBagConstraints.weightx = 1.0;
241         gridBagConstraints.weighty = 1.0;
242         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 19, 0, 0);
243         jPanel2.add(dynamicTextArea, gridBagConstraints);
244         dynamicTextArea.getAccessibleContext().setAccessibleName(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/sun/ide/avk/Bundle").getString("LBL_DynamicVerification"));
245         dynamicTextArea.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/sun/ide/avk/Bundle").getString("Msg_InfoAVK"));
246
247         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
248         gridBagConstraints.gridx = 0;
249         gridBagConstraints.gridy = 1;
250         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
251         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
252         gridBagConstraints.weightx = 1.0;
253         gridBagConstraints.weighty = 1.0;
254         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 12, 12);
255         add(jPanel2, gridBagConstraints);
256
257     }
258     // </editor-fold>//GEN-END:initComponents
259

260     private void dynamicRadioButtonItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_dynamicRadioButtonItemStateChanged
261
if(dynamicRadioButton.isSelected()){
262             staticRadioButton.setSelected(false);
263         }
264     }//GEN-LAST:event_dynamicRadioButtonItemStateChanged
265

266     private void staticRadioButtonItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_staticRadioButtonItemStateChanged
267
if(staticRadioButton.isSelected()){
268             dynamicRadioButton.setSelected(false);
269         }
270     }//GEN-LAST:event_staticRadioButtonItemStateChanged
271

272     public boolean instrumentServer() {
273         return dynamicRadioButton.isSelected();
274     }
275     
276     public boolean verify() {
277         return staticRadioButton.isSelected();
278     }
279     
280     // Variables declaration - do not modify//GEN-BEGIN:variables
281
private javax.swing.JRadioButton JavaDoc dynamicRadioButton;
282     private javax.swing.JTextArea JavaDoc dynamicTextArea;
283     private javax.swing.JPanel JavaDoc jPanel1;
284     private javax.swing.JPanel JavaDoc jPanel2;
285     private javax.swing.JTextPane JavaDoc jTextPane1;
286     private javax.swing.JRadioButton JavaDoc staticRadioButton;
287     private javax.swing.JTextArea JavaDoc staticTextArea;
288     // End of variables declaration//GEN-END:variables
289
}
290     
291     private void instrument(final DeploymentManager JavaDoc dm, final J2eeModuleProvider modProvider){
292         processor.post(new Runnable JavaDoc() {
293             public void run() {
294                 SunDeploymentManagerInterface sdm = (SunDeploymentManagerInterface)dm;
295                 String JavaDoc appServerVersion = getAppServerVersion(sdm.getPlatformRoot());
296                 if(APPSERVER_VERSION_9.equals(appServerVersion)) {
297                    StartInstrumentation.getInstance().start(getProject(modProvider));
298                 } else {
299                     
300                     AVKSupport avkSupport = new AVKSupport((SunDeploymentManagerInterface)dm);
301                     avkSupport.setAVK(true);
302                     launchApp(dm, getContextRoot(modProvider));
303                 }
304             }
305         });
306     }
307     
308     private String JavaDoc getContextRoot(J2eeModuleProvider modProvider){
309         String JavaDoc url = null;
310         if(modProvider != null){
311             if(modProvider instanceof J2eeAppProvider){
312                 //Get contextRoot of first web module in application.xml
313
Application appXml = (Application)modProvider.getJ2eeModule().getDeploymentDescriptor(J2eeModule.APP_XML);
314                 Module[] mods = appXml.getModule();
315                 for(int i=0; i<mods.length; i++){
316                     Web webMod = mods[i].getWeb();
317                     if(webMod != null){
318                         url = webMod.getContextRoot();
319                         break;
320                     }
321                 }
322             }else{
323                 url = modProvider.getConfigSupport().getWebContextRoot();
324             }
325         }
326         return url;
327     }
328     
329     private void launchApp(DeploymentManager JavaDoc dm, String JavaDoc contextRoot){
330         try{
331             if(contextRoot != null){
332                 SunDeploymentManagerInterface sdm = (SunDeploymentManagerInterface)dm;
333                 DeploymentManagerProperties dmProps = new DeploymentManagerProperties(dm);
334                 String JavaDoc start = "http://" + sdm.getHost() + ":" + dmProps.getHttpPortNumber(); //NOI18N
335
if (contextRoot.startsWith("/")) //NOI18N
336
contextRoot = start + contextRoot;
337                 else
338                     contextRoot = start + "/" + contextRoot; //NOI18N
339

340                 URLDisplayer.getDefault().showURL(new URL JavaDoc(contextRoot));
341             }
342         }catch(Exception JavaDoc ex){}
343     }
344     
345     /**
346      * This code is copied from serverplugins/sun/appsrv81/src/org/netbeans/modules/j2ee/sun/ide/j2ee/PlatformImpl.java
347      */

348     public String JavaDoc getAppServerVersion(File JavaDoc asInstallRoot) {
349         String JavaDoc version = APPSERVER_VERSION_UNKNOWN; // NOI18N
350

351         if(asInstallRoot != null && asInstallRoot.exists()) {
352             File JavaDoc sunDomain11Dtd = new File JavaDoc(asInstallRoot, "lib/dtds/sun-domain_1_1.dtd"); // NOI18N
353
//now test for AS 9 (J2EE 5.0) which should work for this plugin
354
File JavaDoc as9 = new File JavaDoc((asInstallRoot)+"/lib/dtds/sun-web-app_2_5-0.dtd");
355             if(as9.exists()){
356                 version = APPSERVER_VERSION_9;
357
358             } else if(sunDomain11Dtd.exists()) {
359                 version = APPSERVER_VERSION_8_x;
360             }
361         }
362         return version;
363     }
364     
365     private Project getProject(J2eeModuleProvider modProvider) {
366         Project project = null;
367         try {
368             FileObject archive = modProvider.getJ2eeModule().getArchive();
369             project = FileOwnerQuery.getOwner(archive);
370         } catch(Exception JavaDoc ex) {
371             ex.printStackTrace();
372         }
373         return project;
374     }
375     
376 }
377
Popular Tags