1 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 ; 39 import java.io.FileOutputStream ; 40 import java.io.IOException ; 41 import java.net.URL ; 42 import java.util.HashMap ; 43 import java.util.Iterator ; 44 import java.util.logging.Level ; 45 import java.util.Map ; 46 import java.util.Map.Entry; 47 import java.util.Properties ; 48 49 import javax.enterprise.deploy.shared.ModuleType ; 50 import javax.management.MBeanServerConnection ; 51 import javax.management.ObjectName ; 52 53 public class DeploymentClientUtils { 54 55 private static final String APPS_CONFIG_MBEAN = 56 "com.sun.appserv:type=applications,category=config"; 57 58 public static final String 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 mbsc, String moduleID) throws Exception { 75 if (mbsc!=null) { 76 ObjectName applicationsMBean = new ObjectName (APPS_CONFIG_MBEAN); 77 String [] signature = new String []{"java.lang.String"}; 78 Object [] params = new Object []{moduleID}; 79 Integer modType = (Integer ) 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 mbsc, String moduleID, 88 SunTarget target, Map options) throws Exception { 89 if (mbsc!=null) { 91 ObjectName applicationsMBean = new ObjectName (APPS_CONFIG_MBEAN); 92 String [] signature = new String []{"java.lang.String", "java.lang.String", "java.util.Map"}; 93 Object [] params = new Object []{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 mbsc, String moduleID, 101 SunTarget target, Map options) throws Exception { 102 if (mbsc!=null) { 104 ObjectName applicationsMBean = new ObjectName (APPS_CONFIG_MBEAN); 105 String [] signature = new String []{"java.lang.String", "java.lang.String", "java.util.Map"}; 106 Object [] params = new Object []{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 mbsc, String moduleID, 114 SunTarget target, Map options) throws Exception { 115 if (mbsc!=null) { 117 ObjectName applicationsMBean = new ObjectName (APPS_CONFIG_MBEAN); 118 Object [] params = new Object []{target.getName(), moduleID, options}; 119 String [] signature = new String []{"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 mbsc, String moduleID, 127 SunTarget target, Map options) throws Exception { 128 if (mbsc!=null) { 130 ObjectName applicationsMBean = new ObjectName (APPS_CONFIG_MBEAN); 131 String [] signature = new String []{"java.lang.String", "java.lang.String", "java.util.Map"}; 132 Object [] params = new Object []{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 mbsc, 140 String moduleID) throws Exception { 141 if (moduleID == null) { 142 return false; 143 } 144 145 if (mbsc!=null) { 146 ObjectName applicationsMBean = new ObjectName (APPS_CONFIG_MBEAN); 147 String [] signature = new String [] {"java.lang.String"}; 148 Object [] params = new Object [] {moduleID}; 149 Object 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 mbsc, String moduleID) throws Exception { 159 if (mbsc!=null) { 160 ObjectName applicationsMBean = new ObjectName (APPS_CONFIG_MBEAN); 161 String [] signature = new String [] {"java.lang.String"}; 162 Object [] params = new Object [] {moduleID}; 163 Boolean ret = (Boolean )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 mbsc, 172 String moduleID, String target, Map options) throws Exception { 173 if (mbsc!=null) { 174 ObjectName applicationsMBean = new ObjectName (APPS_CONFIG_MBEAN); 175 String [] signature = new String [] {"java.lang.String", "java.lang.String", "java.util.Map"}; 176 Object [] params = new Object [] {moduleID, target, options}; 177 return((DeploymentStatus)mbsc.invoke(applicationsMBean, "createLifecycleModuleReference", params, signature)); 178 } 179 return null; 180 } 181 182 public static DeploymentStatus removeLifecycleModuleReference(MBeanServerConnection mbsc, 183 String moduleID, String target) throws Exception { 184 if (mbsc!=null) { 185 ObjectName applicationsMBean = new ObjectName (APPS_CONFIG_MBEAN); 186 String [] signature = new String [] {"java.lang.String", "java.lang.String"}; 187 Object [] params = new Object [] {moduleID, target}; 188 return((DeploymentStatus)mbsc.invoke(applicationsMBean, "removeLifecycleModuleReference", params, signature)); 189 } 190 return null; 191 } 192 193 public static final void changeStateOfModule(MBeanServerConnection mbsc, String moduleID, String type, 194 SunTarget target, boolean enable) throws Exception { 195 if (mbsc!=null) { 197 ObjectName applicationsMBean = new ObjectName (APPS_CONFIG_MBEAN); 198 Object [] params = new Object []{moduleID, type, target.getName()}; 199 String [] signature = new String []{"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 deployedTargets, SunTarget[] targetList) throws IOException { 211 Map tmpMap = new HashMap (); 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 deployedTargets, SunTarget[] targetList) throws IOException { 225 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 tmpMap = new HashMap (); 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 getDeployedTargetList(ConnectionSource dasConnection, String id) throws IOException { 244 245 Map deployedTargets = new HashMap (); 247 248 DomainConfig domainCfg = ProxyFactory.getInstance(dasConnection).getDomainRoot().getDomainConfig(); 250 final Map serverProxies = domainCfg.getStandaloneServerConfigMap(); 251 final Map clusterProxies = domainCfg.getClusterConfigMap(); 252 String [] serverNames = (String [])serverProxies.keySet().toArray(new String [0]); 253 String [] clusterNames = (String [])clusterProxies.keySet().toArray(new String [0]); 254 255 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 (refProxy.getEnabled())); 265 } 266 } 267 268 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 (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 it = stat.getSubStages(); 293 while(it.hasNext()) { 294 com.sun.appserv.management.deploy.DeploymentStatus stageStatus = 295 DeploymentSupport.mapToDeploymentStatus((Map )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 entry = (Map.Entry ) it.next(); 303 tmp.addProperty((String )entry.getKey(), (String )entry.getValue()); 304 } 305 } 306 return tmp; 307 } 308 309 public static void doWsdlFilePublishing(DeploymentStatus status, 310 ConnectionSource conn) 311 throws IOException { 312 if (status == null) { return; } 313 String sep = DeploymentStatus.KEY_SEPARATOR; 314 315 String key = DeploymentStatus.MODULE_ID; 316 String moduleID = status.getProperty(key); 317 318 key = moduleID + sep + DeploymentStatus.SUBMODULE_COUNT; 319 if (status.getProperty(key) == null) { doWsdlFilePublishing(moduleID, status, conn); 321 } else { 322 int counter = (Integer.valueOf(status.getProperty(key))).intValue(); 323 for (int i = 0; i < counter; i++) { key = moduleID + sep + DeploymentStatus.MODULE_ID + sep + 325 String.valueOf(i); 326 String subModuleID = status.getProperty(key); 327 doWsdlFilePublishing(subModuleID, status, conn); 328 } 329 } 330 } 331 332 private static void doWsdlFilePublishing(String keyPrefix, 333 DeploymentStatus status, ConnectionSource conn) 334 throws IOException { 335 String sep = DeploymentStatus.KEY_SEPARATOR; 336 String key = keyPrefix + sep + DeploymentStatus.MODULE_TYPE; 337 ModuleType moduleType = ModuleType.getModuleType((new Integer (status.getProperty(key))).intValue()); 338 339 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 clientPublishURL = status.getProperty(key); 348 349 if (clientPublishURL == null) { return; 351 } 352 353 URL u = new URL (clientPublishURL); 354 String destinationDir = (new File (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 entryName = 363 (status.getProperty(key)).replace('/', File.separatorChar); 364 365 key = key + sep + DeploymentStatus.WSDL_LOCATION; 366 String wsdlFileLocation = status.getProperty(key); 367 368 File outputFile = new File (destinationDir, entryName); 371 File parentDir = outputFile.getParentFile(); 372 if( !parentDir.exists() ) { 373 boolean madeDirs = parentDir.mkdirs(); 374 if( !madeDirs ) { 375 throw new IOException ("Error creating "+outputFile); 376 } 377 } 378 379 downloadFile(new File (wsdlFileLocation), outputFile, conn); 380 } 382 } 383 384 public static String downloadClientStubs (String moduleID, String destDir, 385 ConnectionSource dasConnection) 386 throws IOException { 387 Object [] params = new Object [] {moduleID}; 388 String [] signature = new String [] {"java.lang.String"}; 389 MBeanServerConnection mbsc = 390 dasConnection.getExistingMBeanServerConnection(); 391 try { 392 ModuleType moduleType = getModuleType(mbsc, moduleID); 393 394 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 [] {moduleID, moduleType}); 402 return null; 403 } 404 405 ObjectName ssMBean = 409 new ObjectName ("ias:type=system-services,server=server"); 410 411 String filePath = (String ) mbsc.invoke( 412 ssMBean, "getClientStubJarLocation", params, signature); 413 File srcFile = new File (filePath); 414 downloadFile(srcFile, new File (destDir, srcFile.getName()), dasConnection); 415 String exportedFileLocation = 416 new File (destDir, srcFile.getName()).getAbsolutePath(); 417 return exportedFileLocation; 418 }catch(Exception e) { 419 throw (IOException )(new IOException (e.getLocalizedMessage()).initCause(e)); 420 } 421 } 422 423 public static void downloadFile(File srcFile, File destFile, 424 ConnectionSource dasConnection) 425 throws IOException { 426 427 File destDir = destFile.getParentFile(); 428 if (!destDir.exists() || !destDir.isDirectory() || !destDir.canWrite()) { 429 throw new IOException ( 431 "Problem accessing directory " + destDir.getPath()); 432 } 433 434 UploadDownloadMgr downloadMgr = 435 ProxyFactory.getInstance(dasConnection).getDomainRoot().getUploadDownloadMgr(); 436 int chunkSize = 32 * 1024; 437 FileOutputStream fos = null; 438 try { 439 Object downloadID = downloadMgr.initiateDownload(srcFile, false); 440 fos = new FileOutputStream (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 e) {} 452 } 453 } 454 } 455 456 public static void setResourceOptions (Map options, String 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 options, String rAction, 464 String target) { 465 options.put(DeploymentProperties.RESOURCE_ACTION, rAction); 466 options.put(DeploymentProperties.RESOURCE_TARGET_LIST, 467 target); 468 } 469 470 public static String getTargetStringFromTargetList ( 471 SunTarget[] targetList) { 472 StringBuffer sb = new StringBuffer (); 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 |