1 5 package ve.luz.ica.jackass.daemon; 6 7 import java.io.InputStream ; 8 import java.io.InputStreamReader ; 9 import java.io.Reader ; 10 import java.util.ArrayList ; 11 import java.util.HashSet ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import java.util.Properties ; 15 import java.util.Set ; 16 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 import org.omg.CORBA.ORB ; 20 import org.omg.CORBA.Object ; 21 import org.omg.CORBA.Request ; 22 import org.omg.CORBA.UserException ; 23 import org.omg.CosNaming.NamingContextExt ; 24 import org.omg.CosNaming.NamingContextExtHelper ; 25 import org.omg.CosNaming.NamingContextPackage.AlreadyBound ; 26 import org.omg.PortableServer.POA ; 27 28 import ve.luz.ica.jackass.deploy.DeployerPOA; 29 import ve.luz.ica.jackass.deploy.UnableToDeployException; 30 import ve.luz.ica.jackass.deploy.UnableToUndeployException; 31 import ve.luz.ica.jackass.deploy.daemon.NodeDeployer; 32 import ve.luz.ica.jackass.deploy.daemon.NodeDeployerHelper; 33 import ve.luz.ica.jackass.deploy.daemon.ProxyInfo; 34 import ve.luz.ica.jackass.deploy.daemon.ProxyInfoListHelper; 35 import ve.luz.ica.jackass.deploy.descriptor.ica.IcaJackassApplication; 36 import ve.luz.ica.jackass.deploy.descriptor.standard.JackassApplication; 37 import ve.luz.ica.jackass.deploy.util.Application; 38 import ve.luz.ica.jackass.deploy.util.Component; 39 import ve.luz.ica.jackass.ref.RefUtil; 40 import ve.luz.ica.jackass.solver.ComponentProxyManager; 41 import ve.luz.ica.jackass.util.ConfigurationManager; 42 import ve.luz.ica.remoteio.FileUtil; 43 import ve.luz.ica.remoteio.RemoteFile; 44 import ve.luz.ica.remoteio.RemoteFileHelper; 45 import ve.luz.ica.util.ZipUtil; 46 47 52 public class DeployerImpl extends DeployerPOA 53 { 54 private static final Log LOG = LogFactory.getLog(DeployerImpl.class); 55 56 private static final String UNDEPLOY_OPERATION = "undeploy"; 57 private static final String NAME_SEPARATOR = "/"; 58 private static final String DEPLOY_OPERATION = "deploy"; 59 private static final String DEFAULT_TEMP_DIR = "temp"; 60 61 private ORB orb; 63 private NamingContextExt rootContext; 64 private ComponentProxyManager componentProxyManager; 65 private String tempDirectory = null; 66 private NodeDeployerManager nodeDeployerManager; 67 private ApplicationManager applicationManager; 68 69 79 public DeployerImpl(ORB theOrb, POA thePoa, NamingContextExt rootCtx, ComponentProxyManager proxyManager, 80 NodeDeployerManager nodeDepManager) 81 { 82 Properties cf = ConfigurationManager.getConfigFile(); 83 this.tempDirectory = cf.getProperty(ConfigurationManager.TEMP_PATH_PROPERTY, DEFAULT_TEMP_DIR); 84 85 this.orb = theOrb; 86 this.rootContext = rootCtx; 88 this.componentProxyManager = proxyManager; 89 this.nodeDeployerManager = nodeDepManager; 90 91 this.applicationManager = new SingleHostApplicationManager(); 92 } 93 94 100 public void deploy(RemoteFile remoteFile) throws UnableToDeployException 101 { 102 try 103 { 104 String remoteFileName = remoteFile.getName(); 105 if (LOG.isInfoEnabled()) LOG.info("Deploying " + remoteFileName); 106 107 if (LOG.isDebugEnabled()) LOG.debug("tempDirectory " + tempDirectory); 108 109 String fileName = FileUtil.getRemoteFile(remoteFile, tempDirectory, remoteFileName); 110 InputStream standardIs = ZipUtil.getInputStream(fileName, Application.STANDARD_DESCRIPTOR_NAME); 111 InputStream icaIs = ZipUtil.getInputStream(fileName, Application.ICA_DESCRIPTOR_NAME); 112 113 Reader standardReader = new InputStreamReader (standardIs); 114 Reader icaReader = new InputStreamReader (icaIs); 115 JackassApplication jackApp = JackassApplication.unmarshal(standardReader); 116 IcaJackassApplication icaApp = IcaJackassApplication.unmarshal(icaReader); 117 Application app = new Application(jackApp, icaApp); 118 119 if (LOG.isDebugEnabled()) LOG.debug("Application object created "+app.getName()); 120 121 ApplicationInfo appInfo = applicationManager.get(app.getName()); 122 if (appInfo != null) 123 { 124 throw new Exception ("Application already deployed " + app.getName()); 125 } 126 127 try 129 { 130 this.rootContext.bind_new_context(this.rootContext.to_name(app.getName())); 131 } 132 catch (AlreadyBound e) 133 { 134 LOG.warn("Context for application " + app.getName() + " already bound"); 135 } 136 137 Set deployerSet = this.getDeployerSet(app); 138 List requestList = this.invokeDeployers(deployerSet, app, remoteFile); 139 this.processDeploymentResults(requestList, app); 140 141 if (LOG.isInfoEnabled()) LOG.info("Application " + app.getName() + " deployed"); 142 } 143 catch (Exception e) 144 { 145 LOG.error("Unexpected error deploying application"); 146 if (LOG.isDebugEnabled()) LOG.debug("Stack trace follows", e); 147 throw new UnableToDeployException(e.getMessage()); 148 } 149 } 150 151 159 private Set getDeployerSet(Application app) 160 { 161 Set deployerSet = new HashSet (); 162 163 for (Iterator it = app.getComponents(); it.hasNext();) 164 { 165 Component comp = (Component) it.next(); 166 167 String depGroup = comp.getDeploymentGroup(); 168 NodeDeployer[] nd = this.nodeDeployerManager.getNodeDeployers(depGroup); 169 if (nd != null) 170 { 171 for (int i = 0; i < nd.length; i++) 172 { 173 deployerSet.add(nd[i]); 174 } 175 } 176 } 177 178 return deployerSet; 179 } 180 181 190 private List invokeDeployers(Set deployerSet, Application app, RemoteFile remoteFile) 191 { 192 ArrayList requestList = new ArrayList (); 193 194 ApplicationInfo appInfo = new ApplicationInfo(); 195 applicationManager.put(app.getName(), appInfo); 196 197 for (Iterator deployers = deployerSet.iterator(); deployers.hasNext();) 198 { 199 NodeDeployer nodeDeployer = (NodeDeployer) deployers.next(); 200 appInfo.addNodeDeployer(nodeDeployer); 201 Request request = nodeDeployer._request(DEPLOY_OPERATION); 202 request.add_in_arg().insert_string(app.getName()); 203 RemoteFileHelper.insert(request.add_in_arg(), remoteFile); 204 205 request.set_return_type(ProxyInfoListHelper.type()); 206 207 requestList.add(request); 208 request.send_deferred(); 209 } 210 return requestList; 211 } 212 213 223 private void processDeploymentResults(List requestList, Application app) throws Exception 224 { 225 try 226 { 227 RefUtil refUtil = new RefUtil(orb); 228 List componentList = new ArrayList (); 229 Iterator requests = requestList.iterator(); 230 String appName = app.getName(); 231 232 ApplicationInfo appInfo = applicationManager.get(appName); 233 234 while (requests.hasNext()) 235 { 236 Request request = (Request ) requests.next(); 237 238 Object obj = request.target(); 239 NodeDeployer nodeDeployer = NodeDeployerHelper.narrow(obj); 240 String nodeDeployerRef = orb.object_to_string(nodeDeployer); 241 242 request.get_response(); 243 if (request.env().exception() != null) 244 { 245 throw request.env().exception(); 246 } 247 ProxyInfo[] proxyList = ProxyInfoListHelper.extract(request.return_value()); 248 249 for (int i = 0; i < proxyList.length; ++i) 250 { 251 String compName = proxyList[i].componentName; 252 String componentID = appName + ComponentProxyManager.COMPONENT_ID_SEPARATOR + compName; 253 if (!componentList.contains(compName)) 254 { 255 if (LOG.isDebugEnabled()) LOG.debug("Creating reference for " + componentID); 256 Object componentReference = 257 refUtil.createJackassComponentReference(proxyList[i].proxyReference, componentID); 258 componentList.add(compName); 259 String nsName = this.exportReference(app, app.getComponent(compName), componentReference); 260 appInfo.addComponentNSName(nsName); 261 } 262 componentProxyManager.addProxyToComponent(componentID, nodeDeployerRef, 263 proxyList[i].proxyReference); 264 } 265 } 266 } 267 catch (Exception e) 268 { 269 this.undeploy(app.getName()); 270 throw e; 271 } 272 } 273 274 282 private String exportReference(Application app, Component comp, Object reference) 283 throws UserException 284 { 285 String appName = app.getName(); 286 String compName = comp.getName(); 287 String nameContext = comp.getNameContext(); 288 289 String nameStr = null; 291 if (nameContext == null || nameContext.equals("")) 292 { 293 nameStr = appName + NAME_SEPARATOR + compName; 294 } 296 else 297 { 298 nameStr = nameContext; 299 } 302 303 if (LOG.isDebugEnabled()) 304 { 305 LOG.debug("Exporting reference for " + compName); 306 LOG.debug("name service name: " + nameStr); 307 } 308 309 rootContext.bind(rootContext.to_name(nameStr), reference); 310 return nameStr; 311 } 312 313 319 public void undeploy(String appName) throws UnableToUndeployException 320 { 321 LOG.info("Undeploying application " + appName); 322 323 boolean error = false; 324 325 ApplicationInfo appInfo = applicationManager.get(appName); 327 if (appInfo == null) 328 { 329 if (LOG.isInfoEnabled()) LOG.info("Application not deployed " + appName); 330 throw new UnableToUndeployException("Application not deployed " + appName); 331 } 332 333 List requests = new ArrayList (); 334 for (Iterator i = appInfo.nodeDeployerIterator(); i.hasNext();) 335 { 336 NodeDeployer nodeDeployer = (NodeDeployer) i.next(); 337 Request request = nodeDeployer._request(UNDEPLOY_OPERATION); 338 request.add_in_arg().insert_string(appName); 339 request.send_deferred(); 340 requests.add(request); 341 } 342 343 for (Iterator i = requests.iterator(); i.hasNext();) 346 { 347 try 348 { 349 Request request = (Request ) i.next(); 350 request.get_response(); 351 if (request.env().exception() != null) 352 { 353 throw request.env().exception(); 354 } 355 } 356 catch (Exception e) 357 { 358 error = true; 359 LOG.error("Error undeploying appName ", e); 360 } 361 } 362 363 for (Iterator i = appInfo.componentNSNamesIterator(); i.hasNext();) 365 { 366 String nsName = (String ) i.next(); 367 try 368 { 369 rootContext.unbind(rootContext.to_name(nsName)); 370 } 371 catch (Exception e) 372 { 373 error = true; 374 LOG.error("Error unbinding component reference ", e); 375 } 376 } 377 378 applicationManager.remove(appName); 380 componentProxyManager.removeApplication(appName); 381 382 NamingContextExt appContext; 384 try 385 { 386 appContext = NamingContextExtHelper.narrow(this.rootContext.resolve_str(appName)); 387 appContext.destroy(); 388 rootContext.unbind(rootContext.to_name(appName)); 389 } 390 catch (Exception e) 391 { 392 error = true; 393 LOG.error("Error resolving application reference in the name service ", e); 394 } 395 396 if (error) 397 { 398 throw new UnableToUndeployException(); 399 } 400 } 401 402 } 403 | Popular Tags |