KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > client > DeploymentClientUtils


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.deployment.client;
25
26 import com.sun.appserv.management.base.UploadDownloadMgr;
27 import com.sun.appserv.management.base.XTypes;
28 import com.sun.appserv.management.client.ConnectionSource;
29 import com.sun.appserv.management.client.ProxyFactory;
30 import com.sun.appserv.management.config.*;
31 import com.sun.appserv.management.deploy.DeploymentSupport;
32 import com.sun.enterprise.deployapi.SunTarget;
33 import com.sun.enterprise.deployment.backend.DeploymentStatus;
34 import com.sun.enterprise.deployment.util.DeploymentProperties;
35 import com.sun.enterprise.deployment.util.DOLUtils;
36 import com.sun.enterprise.deployment.util.XModuleType;
37
38 import java.io.File JavaDoc;
39 import java.io.FileOutputStream JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.net.URL JavaDoc;
42 import java.util.HashMap JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.Map JavaDoc;
46 import java.util.Map.Entry;
47 import java.util.Properties JavaDoc;
48
49 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
50 import javax.management.MBeanServerConnection JavaDoc;
51 import javax.management.ObjectName JavaDoc;
52
53 public class DeploymentClientUtils {
54
55     private static final String JavaDoc APPS_CONFIG_MBEAN =
56         "com.sun.appserv:type=applications,category=config";
57
58     public static final String JavaDoc mapModuleTypeToXType(ModuleType moduleType) {
59         if (ModuleType.EAR.equals(moduleType)) {
60             return XTypes.J2EE_APPLICATION_CONFIG;
61         } else if (ModuleType.EJB.equals(moduleType)) {
62             return XTypes.EJB_MODULE_CONFIG;
63         } else if (ModuleType.RAR.equals(moduleType)) {
64             return XTypes.RAR_MODULE_CONFIG;
65         } else if (ModuleType.WAR.equals(moduleType)) {
66             return XTypes.WEB_MODULE_CONFIG;
67         } else if (ModuleType.CAR.equals(moduleType)) {
68             return XTypes.APP_CLIENT_MODULE_CONFIG;
69         }
70         return null;
71     }
72
73     public static final ModuleType getModuleType(
74         MBeanServerConnection JavaDoc mbsc, String JavaDoc moduleID) throws Exception JavaDoc {
75         if (mbsc!=null) {
76             ObjectName JavaDoc applicationsMBean = new ObjectName JavaDoc(APPS_CONFIG_MBEAN);
77             String JavaDoc[] signature = new String JavaDoc[]{"java.lang.String"};
78             Object JavaDoc[] params = new Object JavaDoc[]{moduleID};
79             Integer JavaDoc modType = (Integer JavaDoc) mbsc.invoke(applicationsMBean, "getModuleType", params, signature);
80             if(modType != null) {
81                 return XModuleType.getModuleType(modType.intValue());
82             }
83         }
84         return null;
85     }
86
87     public static final DeploymentStatus deleteApplicationReference(MBeanServerConnection JavaDoc mbsc, String JavaDoc moduleID,
88                                                             SunTarget target, Map JavaDoc options) throws Exception JavaDoc {
89         //System.out.println("DBG_PRINT : del-app-ref for target = " + target.getName());
90
if (mbsc!=null) {
91             ObjectName JavaDoc applicationsMBean = new ObjectName JavaDoc(APPS_CONFIG_MBEAN);
92             String JavaDoc[] signature = new String JavaDoc[]{"java.lang.String", "java.lang.String", "java.util.Map"};
93             Object JavaDoc[] params = new Object JavaDoc[]{target.getName(), moduleID, options};
94             return (DeploymentStatus) (mbsc.invoke(
95                     applicationsMBean, "deleteApplicationReference", params, signature));
96         }
97         return null;
98     }
99     
100     public static final DeploymentStatus stopApplication(MBeanServerConnection JavaDoc mbsc, String JavaDoc moduleID,
101                                                             SunTarget target, Map JavaDoc options) throws Exception JavaDoc {
102         //System.out.println("DBG_PRINT : app stop for target = " + target.getName());
103
if (mbsc!=null) {
104             ObjectName JavaDoc applicationsMBean = new ObjectName JavaDoc(APPS_CONFIG_MBEAN);
105             String JavaDoc[] signature = new String JavaDoc[]{"java.lang.String", "java.lang.String", "java.util.Map"};
106             Object JavaDoc[] params = new Object JavaDoc[]{moduleID, target.getName(), options};
107             return (DeploymentStatus) (mbsc.invoke(
108                     applicationsMBean, "stop", params, signature));
109         }
110         return null;
111     }
112     
113     public static final DeploymentStatus createApplicationReference(MBeanServerConnection JavaDoc mbsc, String JavaDoc moduleID,
114                                         SunTarget target, Map JavaDoc options) throws Exception JavaDoc {
115         //System.out.println("DBG_PRINT : create-app-ref for target = " + target.getName());
116
if (mbsc!=null) {
117             ObjectName JavaDoc applicationsMBean = new ObjectName JavaDoc(APPS_CONFIG_MBEAN);
118             Object JavaDoc[] params = new Object JavaDoc[]{target.getName(), moduleID, options};
119             String JavaDoc[] signature = new String JavaDoc[]{"java.lang.String", "java.lang.String", "java.util.Map"};
120             return (DeploymentStatus) (mbsc.invoke(
121                     applicationsMBean, "createApplicationReference", params, signature));
122         }
123         return null;
124     }
125     
126     public static final DeploymentStatus startApplication(MBeanServerConnection JavaDoc mbsc, String JavaDoc moduleID,
127                                                             SunTarget target, Map JavaDoc options) throws Exception JavaDoc {
128         //System.out.println("DBG_PRINT : app start for target = " + target.getName());
129
if (mbsc!=null) {
130             ObjectName JavaDoc applicationsMBean = new ObjectName JavaDoc(APPS_CONFIG_MBEAN);
131             String JavaDoc[] signature = new String JavaDoc[]{"java.lang.String", "java.lang.String", "java.util.Map"};
132             Object JavaDoc[] params = new Object JavaDoc[]{moduleID, target.getName(), options};
133             return (DeploymentStatus) (mbsc.invoke(
134                     applicationsMBean, "start", params, signature));
135         }
136         return null;
137     }
138     
139     public static boolean isModuleDeployed (MBeanServerConnection JavaDoc mbsc,
140         String JavaDoc moduleID) throws Exception JavaDoc {
141         if (moduleID == null) {
142             return false;
143         }
144     
145         if (mbsc!=null) {
146             ObjectName JavaDoc applicationsMBean = new ObjectName JavaDoc(APPS_CONFIG_MBEAN);
147             String JavaDoc[] signature = new String JavaDoc[] {"java.lang.String"};
148             Object JavaDoc[] params = new Object JavaDoc[] {moduleID};
149             Object JavaDoc result = mbsc.invoke(applicationsMBean, "getModuleType",
150                 params, signature);
151             if (result != null) {
152                 return true;
153             }
154         }
155         return false;
156     }
157
158     public static boolean isLifecycleModule(MBeanServerConnection JavaDoc mbsc, String JavaDoc moduleID) throws Exception JavaDoc {
159         if (mbsc!=null) {
160             ObjectName JavaDoc applicationsMBean = new ObjectName JavaDoc(APPS_CONFIG_MBEAN);
161             String JavaDoc[] signature = new String JavaDoc[] {"java.lang.String"};
162             Object JavaDoc[] params = new Object JavaDoc[] {moduleID};
163             Boolean JavaDoc ret = (Boolean JavaDoc)mbsc.invoke(applicationsMBean, "isLifecycleModuleRegistered", params, signature);
164             if(Boolean.TRUE.equals(ret)) {
165                 return true;
166             }
167         }
168         return false;
169     }
170     
171     public static DeploymentStatus createLifecycleModuleReference(MBeanServerConnection JavaDoc mbsc,
172         String JavaDoc moduleID, String JavaDoc target, Map JavaDoc options) throws Exception JavaDoc {
173         if (mbsc!=null) {
174             ObjectName JavaDoc applicationsMBean = new ObjectName JavaDoc(APPS_CONFIG_MBEAN);
175             String JavaDoc[] signature = new String JavaDoc[] {"java.lang.String", "java.lang.String", "java.util.Map"};
176             Object JavaDoc[] params = new Object JavaDoc[] {moduleID, target, options};
177             return((DeploymentStatus)mbsc.invoke(applicationsMBean, "createLifecycleModuleReference", params, signature));
178         }
179         return null;
180     }
181     
182     public static DeploymentStatus removeLifecycleModuleReference(MBeanServerConnection JavaDoc mbsc,
183         String JavaDoc moduleID, String JavaDoc target) throws Exception JavaDoc {
184         if (mbsc!=null) {
185             ObjectName JavaDoc applicationsMBean = new ObjectName JavaDoc(APPS_CONFIG_MBEAN);
186             String JavaDoc[] signature = new String JavaDoc[] {"java.lang.String", "java.lang.String"};
187             Object JavaDoc[] params = new Object JavaDoc[] {moduleID, target};
188             return((DeploymentStatus)mbsc.invoke(applicationsMBean, "removeLifecycleModuleReference", params, signature));
189         }
190         return null;
191     }
192     
193     public static final void changeStateOfModule(MBeanServerConnection JavaDoc mbsc, String JavaDoc moduleID, String JavaDoc type,
194                                                         SunTarget target, boolean enable) throws Exception JavaDoc {
195         //System.out.println("DBG_PRINT : Changing state in target " + target.getName() + " to " + enable);
196
if (mbsc!=null) {
197             ObjectName JavaDoc applicationsMBean = new ObjectName JavaDoc(APPS_CONFIG_MBEAN);
198             Object JavaDoc[] params = new Object JavaDoc[]{moduleID, type, target.getName()};
199             String JavaDoc[] signature = new String JavaDoc[]{"java.lang.String", "java.lang.String", "java.lang.String"};
200
201             if (enable) {
202                 mbsc.invoke(applicationsMBean, "enable", params, signature);
203             } else {
204                 mbsc.invoke(applicationsMBean, "disable", params, signature);
205             }
206         }
207         return;
208     }
209     
210     public static boolean isTargetListComplete(Map JavaDoc deployedTargets, SunTarget[] targetList) throws IOException JavaDoc {
211         Map JavaDoc tmpMap = new HashMap JavaDoc();
212         tmpMap.putAll(deployedTargets);
213         for(int i=0; i<targetList.length; i++) {
214             if(tmpMap.get(targetList[i].getName()) != null) {
215                 tmpMap.remove(targetList[i].getName());
216             }
217         }
218         if(tmpMap.size() != 0) {
219             return false;
220         }
221         return true;
222     }
223     
224     public static boolean isNewTarget(Map JavaDoc deployedTargets, SunTarget[] targetList) throws IOException JavaDoc {
225         // During redeploy, if only one target is given and it is "domain", then
226
// there is no need to check if this is a new target
227
if( (targetList.length == 1) && (TargetType.DOMAIN.equals(targetList[0].getName())) ) {
228             return false;
229         }
230         if(deployedTargets.size() != targetList.length) {
231             return true;
232         }
233         Map JavaDoc tmpMap = new HashMap JavaDoc();
234         tmpMap.putAll(deployedTargets);
235         for(int i=0; i<targetList.length; i++) {
236             if(tmpMap.get(targetList[i].getName()) == null) {
237                 return true;
238             }
239         }
240         return false;
241     }
242     
243     public static Map JavaDoc getDeployedTargetList(ConnectionSource dasConnection, String JavaDoc id) throws IOException JavaDoc {
244
245         // Create a Map of target names
246
Map JavaDoc deployedTargets = new HashMap JavaDoc();
247
248         // Get all targets from AMX
249
DomainConfig domainCfg = ProxyFactory.getInstance(dasConnection).getDomainRoot().getDomainConfig();
250         final Map JavaDoc serverProxies = domainCfg.getStandaloneServerConfigMap();
251         final Map JavaDoc clusterProxies = domainCfg.getClusterConfigMap();
252         String JavaDoc[] serverNames = (String JavaDoc[])serverProxies.keySet().toArray(new String JavaDoc[0]);
253         String JavaDoc[] clusterNames = (String JavaDoc[])clusterProxies.keySet().toArray(new String JavaDoc[0]);
254         
255         // Now for each stand alone server targets, check if this id is deployed on that target
256
// If so, add this target to the map
257
for(int i=0; i<serverNames.length; i++) {
258             StandaloneServerConfig tgtProxy = (StandaloneServerConfig)
259                     serverProxies.get(serverNames[i]);
260             DeployedItemRefConfig refProxy = (DeployedItemRefConfig)
261                     tgtProxy.getContainee(
262                         XTypes.DEPLOYED_ITEM_REF_CONFIG, id);
263             if(refProxy != null) {
264                 deployedTargets.put(serverNames[i], new Boolean JavaDoc(refProxy.getEnabled()));
265             }
266         }
267         
268         // Now for each cluster targets, check if this id is deployed on that target
269
// If so, add this target to the map
270
for(int i=0; i<clusterNames.length; i++) {
271             ClusterConfig tgtProxy = (ClusterConfig)clusterProxies.get(
272                     clusterNames[i]);
273             DeployedItemRefConfig refProxy = (DeployedItemRefConfig)
274                     tgtProxy.getContainee(
275                         XTypes.DEPLOYED_ITEM_REF_CONFIG, id);
276             if(refProxy != null) {
277                 deployedTargets.put(clusterNames[i], new Boolean JavaDoc(refProxy.getEnabled()));
278             }
279         }
280         return deployedTargets;
281     }
282     
283     public static DeploymentStatus
284                 getDeploymentStatusFromAdminStatus(com.sun.appserv.management.deploy.DeploymentStatus stat) {
285         DeploymentStatus tmp = new DeploymentStatus();
286         tmp.setStageStatus(stat.getStageStatus());
287         if(stat.getThrowable() != null) {
288             tmp.setStageException(stat.getThrowable());
289         }
290         tmp.setStageDescription(stat.getStageDescription());
291         tmp.setStageStatusMessage(stat.getStageStatusMessage());
292         Iterator JavaDoc it = stat.getSubStages();
293         while(it.hasNext()) {
294             com.sun.appserv.management.deploy.DeploymentStatus stageStatus =
295                             DeploymentSupport.mapToDeploymentStatus((Map JavaDoc)it.next());
296             tmp.addSubStage(getDeploymentStatusFromAdminStatus(stageStatus));
297         }
298
299         if (stat.getAdditionalStatus() != null) {
300             it = stat.getAdditionalStatus().entrySet().iterator();
301             while (it.hasNext()) {
302                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
303                 tmp.addProperty((String JavaDoc)entry.getKey(), (String JavaDoc)entry.getValue());
304             }
305         }
306         return tmp;
307     }
308
309     public static void doWsdlFilePublishing(DeploymentStatus status,
310                                       ConnectionSource conn)
311             throws IOException JavaDoc {
312         if (status == null) { return; }
313         String JavaDoc sep = DeploymentStatus.KEY_SEPARATOR;
314
315         String JavaDoc key = DeploymentStatus.MODULE_ID;
316         String JavaDoc moduleID = status.getProperty(key);
317
318         key = moduleID + sep + DeploymentStatus.SUBMODULE_COUNT;
319         if (status.getProperty(key) == null) { //standalone module
320
doWsdlFilePublishing(moduleID, status, conn);
321         } else {
322             int counter = (Integer.valueOf(status.getProperty(key))).intValue();
323             for (int i = 0; i < counter; i++) { //for each submodule
324
key = moduleID + sep + DeploymentStatus.MODULE_ID + sep +
325                         String.valueOf(i);
326                 String JavaDoc subModuleID = status.getProperty(key);
327                 doWsdlFilePublishing(subModuleID, status, conn);
328             }
329         }
330     }
331
332     private static void doWsdlFilePublishing(String JavaDoc keyPrefix,
333             DeploymentStatus status, ConnectionSource conn)
334                 throws IOException JavaDoc {
335         String JavaDoc sep = DeploymentStatus.KEY_SEPARATOR;
336         String JavaDoc key = keyPrefix + sep + DeploymentStatus.MODULE_TYPE;
337         ModuleType moduleType = ModuleType.getModuleType((new Integer JavaDoc(status.getProperty(key))).intValue());
338
339         //only EAR, WAR and EJB archives could contain wsdl files for publish
340
if (!(ModuleType.EAR.equals(moduleType) ||
341               ModuleType.WAR.equals(moduleType) ||
342               ModuleType.EJB.equals(moduleType))) {
343             return;
344         }
345
346         key = keyPrefix + sep + DeploymentStatus.WSDL_PUBLISH_URL;
347         String JavaDoc clientPublishURL = status.getProperty(key);
348
349         if (clientPublishURL == null) { // No file publishing
350
return;
351         }
352
353         URL JavaDoc u = new URL JavaDoc(clientPublishURL);
354         String JavaDoc destinationDir = (new File JavaDoc(u.getFile())).getAbsolutePath();
355
356         key = keyPrefix + sep + DeploymentStatus.WSDL_FILE_ENTRIES + sep +
357                 DeploymentStatus.COUNT;
358         int counter = (Integer.valueOf(status.getProperty(key))).intValue();
359         for (int i = 0; i < counter; i++) {
360             key = keyPrefix + sep + DeploymentStatus.WSDL_FILE_ENTRIES + sep +
361                     String.valueOf(i);
362             String JavaDoc entryName =
363                     (status.getProperty(key)).replace('/', File.separatorChar);
364
365             key = key + sep + DeploymentStatus.WSDL_LOCATION;
366             String JavaDoc wsdlFileLocation = status.getProperty(key);
367
368             // publish file, preserving its location relative to the module
369
// wsdl directory.
370
File JavaDoc outputFile = new File JavaDoc(destinationDir, entryName);
371             File JavaDoc parentDir = outputFile.getParentFile();
372             if( !parentDir.exists() ) {
373                 boolean madeDirs = parentDir.mkdirs();
374                 if( !madeDirs ) {
375                     throw new IOException JavaDoc("Error creating "+outputFile);
376                 }
377             }
378             
379             downloadFile(new File JavaDoc(wsdlFileLocation), outputFile, conn);
380             //@@@ should log the location
381
}
382     }
383
384     public static String JavaDoc downloadClientStubs (String JavaDoc moduleID, String JavaDoc destDir,
385                                        ConnectionSource dasConnection)
386             throws IOException JavaDoc {
387         Object JavaDoc[] params = new Object JavaDoc[] {moduleID};
388         String JavaDoc[] signature = new String JavaDoc[] {"java.lang.String"};
389         MBeanServerConnection JavaDoc mbsc =
390                         dasConnection.getExistingMBeanServerConnection();
391         try {
392             ModuleType moduleType = getModuleType(mbsc, moduleID);
393
394             // only ejb, appclient, application are applicable for
395
// retrieving client stubs
396
if (!(ModuleType.EJB.equals(moduleType) ||
397                 ModuleType.CAR.equals(moduleType) ||
398                 ModuleType.EAR.equals(moduleType))) {
399                 DOLUtils.getDefaultLogger().log(
400                     Level.WARNING, "retrieve_client_stub_not_applicable",
401                         new Object JavaDoc[] {moduleID, moduleType});
402                return null;
403             }
404
405             //Note that we still rely on the SystemsServiceMBean to retrieve
406
//the client jar location from the server. This should only
407
//happen on DAS.
408
ObjectName JavaDoc ssMBean =
409                 new ObjectName JavaDoc("ias:type=system-services,server=server");
410             
411             String JavaDoc filePath = (String JavaDoc) mbsc.invoke(
412                 ssMBean, "getClientStubJarLocation", params, signature);
413             File JavaDoc srcFile = new File JavaDoc(filePath);
414             downloadFile(srcFile, new File JavaDoc(destDir, srcFile.getName()), dasConnection);
415             String JavaDoc exportedFileLocation =
416                 new File JavaDoc(destDir, srcFile.getName()).getAbsolutePath();
417             return exportedFileLocation;
418         }catch(Exception JavaDoc e) {
419             throw (IOException JavaDoc)(new IOException JavaDoc(e.getLocalizedMessage()).initCause(e));
420         }
421     }
422
423     public static void downloadFile(File JavaDoc srcFile, File JavaDoc destFile,
424                                     ConnectionSource dasConnection)
425             throws IOException JavaDoc {
426
427         File JavaDoc destDir = destFile.getParentFile();
428         if (!destDir.exists() || !destDir.isDirectory() || !destDir.canWrite()) {
429             //@@@ i18n
430
throw new IOException JavaDoc(
431                 "Problem accessing directory " + destDir.getPath());
432         }
433
434         UploadDownloadMgr downloadMgr =
435             ProxyFactory.getInstance(dasConnection).getDomainRoot().getUploadDownloadMgr();
436         int chunkSize = 32 * 1024;
437         FileOutputStream JavaDoc fos = null;
438         try {
439             Object JavaDoc downloadID = downloadMgr.initiateDownload(srcFile, false);
440             fos = new FileOutputStream JavaDoc(destFile);
441             boolean done = false;
442             while (!done)
443             {
444                 byte[] bytes = downloadMgr.downloadBytes(downloadID, chunkSize);
445                 fos.write(bytes, 0, bytes.length);
446                 if (bytes.length < chunkSize) { done = true; }
447             }
448         } finally {
449             if (fos != null) {
450                 try { fos.close(); }
451                 catch (Exception JavaDoc e) {}
452             }
453         }
454     }
455     
456     public static void setResourceOptions (Map JavaDoc options, String JavaDoc rAction,
457         SunTarget[] targetList) {
458         options.put(DeploymentProperties.RESOURCE_ACTION, rAction);
459         options.put(DeploymentProperties.RESOURCE_TARGET_LIST,
460             getTargetStringFromTargetList(targetList));
461     }
462
463     public static void setResourceOptions (Map JavaDoc options, String JavaDoc rAction,
464         String JavaDoc target) {
465         options.put(DeploymentProperties.RESOURCE_ACTION, rAction);
466         options.put(DeploymentProperties.RESOURCE_TARGET_LIST,
467             target);
468     }
469
470     public static String JavaDoc getTargetStringFromTargetList (
471         SunTarget[] targetList) {
472         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
473         for (int i = 0; i < targetList.length; i++) {
474             sb.append(targetList[i].getName());
475             if (i != targetList.length -1) {
476                 sb.append(",");
477             }
478         }
479         return sb.toString();
480     }
481
482 }
483
Popular Tags