KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > bridge > AppServerBridge


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
21 package org.netbeans.modules.j2ee.sun.bridge;
22
23 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration;
24 import com.sun.enterprise.admin.jmx.remote.SunOneHttpJmxConnectorFactory;
25 import com.sun.enterprise.config.serverbeans.Config;
26 import com.sun.enterprise.config.serverbeans.Domain;
27 import com.sun.enterprise.config.serverbeans.HttpListener;
28 import com.sun.enterprise.deployapi.SunDeploymentManager;
29 import com.sun.enterprise.deployapi.SunTargetModuleID;
30 import com.sun.enterprise.deployment.client.ServerConnectionEnvironment;
31 import java.io.BufferedInputStream JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.io.FileNotFoundException JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.util.logging.Level JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
40 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
41 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
42 import javax.management.MBeanServerConnection JavaDoc;
43 import javax.management.ObjectName JavaDoc;
44 import javax.management.remote.JMXConnector JavaDoc;
45 import javax.management.remote.JMXServiceURL JavaDoc;
46
47 /*
48  * wrap some App Server internal APIS calls
49  * for the netbeans plugin
50  * to compile this class, you need some App Server jar files like appsrv-rt.jar and appsrv-admin.jar
51  * @author: Ludovic Champenois
52  *
53  **/

54
55
56 public class AppServerBridge {
57     
58     public static java.io.File JavaDoc getDirLocation( TargetModuleID tmid){
59         java.io.File JavaDoc dirLocation=null;
60         try{
61             SunTargetModuleID ddd = (SunTargetModuleID)tmid;
62             
63             if (ddd.getModuleType().equals(ModuleType.WAR)){
64                 ObjectName JavaDoc aaaa = new ObjectName JavaDoc("com.sun.appserv:type=web-module,name="+ddd.getModuleID()+",category=config");
65                 dirLocation =new java.io.File JavaDoc(""+ddd.getMBeanServerConnection().getAttribute(aaaa,"location"));
66                 // System.out.println("dirLocation"+dirLocation);
67
} else if (ddd.getModuleType().equals(ModuleType.EJB)){
68                 ObjectName JavaDoc aaaa = new ObjectName JavaDoc("com.sun.appserv:type=ejb-module,name="+ddd.getModuleID()+",category=config");
69                 dirLocation =new java.io.File JavaDoc(""+ddd.getMBeanServerConnection().getAttribute(aaaa,"location"));
70             }
71             
72             else if (ddd.getModuleType().equals(ModuleType.EAR)){
73                 ObjectName JavaDoc aaaa = new ObjectName JavaDoc("com.sun.appserv:type=application,name="+ddd.getModuleID()+",category=config");
74                 dirLocation =new java.io.File JavaDoc(""+ddd.getMBeanServerConnection().getAttribute(aaaa,"location"));
75             } else{
76                 System.out.println("Still Some Work to do in ModuleRestartActikon is AS 8.1 plugin code");
77             }
78             
79         } catch(Exception JavaDoc e) {
80             e.printStackTrace();
81             IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc(e.getMessage());
82             ise.initCause(e);
83             throw ise;
84         }
85         return dirLocation;
86     }
87     
88     public static boolean isApp(TargetModuleID tmid) {
89         ModuleType JavaDoc mt = ((SunTargetModuleID)tmid).getModuleType();
90         return mt.equals(ModuleType.EAR);
91     }
92     
93     public static boolean isWar(TargetModuleID tmid) {
94         ModuleType JavaDoc mt = ((SunTargetModuleID)tmid).getModuleType();
95         return mt.equals(ModuleType.WAR);
96     }
97     
98     public static Boolean JavaDoc isCar(TargetModuleID tmid) {
99         ModuleType JavaDoc mt = ((SunTargetModuleID)tmid).getModuleType();
100         return Boolean.valueOf(mt.equals(ModuleType.CAR));
101     }
102     
103     /**
104      * Get the URI pointing to location of child module inside a application archive.
105      * For a root module, service provider does not need to override this method.
106      *
107      * @param module TargetModuleID of the child module
108      * @return its relative path within application archive, returns null by
109      * default (for standalone module)
110      */

111     public static String JavaDoc getModuleUrl(TargetModuleID module){
112         ModuleType JavaDoc mt = ((SunTargetModuleID)module).getModuleType();
113         String JavaDoc suffix = ".jar";
114         if (mt.equals(ModuleType.EAR)){
115             suffix = ".ear";
116         }
117         if (mt.equals(ModuleType.WAR)){
118             suffix = ".war";
119         }
120         if (mt.equals(ModuleType.RAR)){
121             suffix = ".rar";
122         }
123         
124         String JavaDoc moduleID = module.getModuleID();
125         int i = moduleID.indexOf('#');
126         if (i > -1) {
127             moduleID = moduleID.substring(i+1);
128         }
129         if (moduleID.endsWith(suffix) || moduleID.endsWith(suffix.toUpperCase())) {
130             return moduleID;
131         }
132         return moduleID + suffix;
133     }
134     
135     
136     public static String JavaDoc getHostPort(File JavaDoc domainXml, File JavaDoc platformDir){
137         String JavaDoc hostPort = null;
138         InputStream JavaDoc inFile = null;
139         try {
140             inFile = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(domainXml));
141             Domain domain = Domain.createGraph(inFile);
142             String JavaDoc domainSysID = domain.graphManager().getXmlDocument().getDoctype().getSystemId();
143             
144             // make sure the platform will support this domain..
145

146             // unknown domain.xml content.. we don't support that
147
if (null == domainSysID) {
148                 return null;
149             }
150             
151             // the sys ID doesn't contain the content we expect... we don't support that
152
int dtdsDex = domainSysID.indexOf("dtds/"); // NOI18N
153
if (-1 == dtdsDex) {
154                 return null;
155             }
156             
157             File JavaDoc domainDtd = new File JavaDoc(platformDir, "lib/"+ // NOI18N
158
domainSysID.substring(dtdsDex));
159             
160             // the installation doesn't have the dtd where we expect it... we don't support that
161
if (!domainDtd.exists()) {
162                 return null;
163             }
164             
165             Config conf = domain.getConfigs().getConfigByName("server-config"); //NOI18N
166
HttpListener list = conf.getHttpService().getHttpListenerById("admin-listener"); //NOI18N
167
hostPort = "localhost:" + list.getPort(); //NOI18N
168
} catch (FileNotFoundException JavaDoc ex) {
169             Logger.getLogger("org.netbeans.modules.j2ee.sun.bridge").log(Level.WARNING,"",ex); // NOI18N
170
} catch (IOException JavaDoc ex) {
171             Logger.getLogger("org.netbeans.modules.j2ee.sun.bridge").log(Level.WARNING,"",ex); // NOI18N
172
} catch (RuntimeException JavaDoc re) {
173             Logger.getLogger("org.netbeans.modules.j2ee.sun.bridge").log(Level.WARNING,"",re); // NOI18N
174
} finally {
175             if (null!=inFile)
176                 try {
177                     inFile.close();
178                 } catch(IOException JavaDoc ioe) {
179                     // what about this???
180
Logger.getLogger("org.netbeans.modules.j2ee.sun.bridge").log(Level.FINE,"",ioe); // NOI18N
181
}
182         }
183         return hostPort;
184     }
185     
186     /* return the port number used bu the server instance (usually, it is the 8080...
187      * This is not the admin port number which is usally 4848
188      **/

189     public String JavaDoc getNonAdminPortNumber(File JavaDoc domainXml){
190         String JavaDoc port = null;
191         try{
192             InputStream JavaDoc inFile = new FileInputStream JavaDoc(domainXml);
193             Domain domain = Domain.createGraph(inFile);
194             Config conf = domain.getConfigs().getConfigByName("server-config"); //NOI18N
195
HttpListener list = conf.getHttpService().getHttpListenerById("http-listener-1"); //NOI18N
196
port = list.getPort(); //NOI18N
197
inFile.close();
198         }catch(Exception JavaDoc ex){
199             return null;
200             //Suppressing exception while trying to get admin port.
201
//Null port value is handled in AddServerChoiceVisualPanel
202
}
203         return port;
204     }
205     public static MBeanServerConnection JavaDoc getJMXConnector(String JavaDoc host, int port, String JavaDoc username, String JavaDoc password,boolean secure) throws java.net.MalformedURLException JavaDoc, java.io.IOException JavaDoc{
206         MBeanServerConnection JavaDoc serverConn = null;
207         String JavaDoc mode=null;
208         if (secure)
209             mode="s1ashttps";
210         else
211             mode = "s1ashttp";
212         JMXServiceURL JavaDoc serverUrl = new JMXServiceURL JavaDoc(mode, host, port); //NOI18N
213
final JMXConnector JavaDoc connector = SunOneHttpJmxConnectorFactory.connect(serverUrl, username, password);
214         serverConn = connector.getMBeanServerConnection();
215         
216         return serverConn;
217     }
218     /**
219      * This method initializes a newly DeploymentManager by creating and
220      * setting a ServerConnectionEnvironement with Deploytool's
221      * X509TrustManager. This method will case the given DeploymentManager
222      * to a SunDeploymentManager in order to make
223      * invoke the appropriate setter for the ServerConnectionEnvironment.
224      */

225     public static void setServerConnectionEnvironment(DeploymentManager dm) {
226     ServerConnectionEnvironment env = new ServerConnectionEnvironment();
227     env.put(DefaultConfiguration.TRUST_MANAGER_PROPERTY_NAME, new X509TrustManager());
228     if (dm instanceof SunDeploymentManager) {
229         ((SunDeploymentManager)dm).setServerConnectionEnvironment(env);
230     }// else {
231
// Print.dprintStackTrace(null, new IllegalArgumentException(
232
// "Unsupported DeploymentManager type: '"+
233
// dm.getClass().getName()+"'."));
234
// }
235
}
236
237
238 }
239
Popular Tags