1 5 package ve.luz.ica.jackass.daemon; 6 7 import java.io.File ; 8 import java.io.InputStream ; 9 import java.io.InputStreamReader ; 10 import java.io.Reader ; 11 import java.net.MalformedURLException ; 12 import java.net.URL ; 13 import java.net.URLClassLoader ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Properties ; 18 import java.util.zip.ZipFile ; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.omg.CosNaming.NamingContextExt ; 23 import org.omg.PortableServer.POA ; 24 25 import ve.luz.ica.jackass.component.ApplicationContext; 26 import ve.luz.ica.jackass.component.ApplicationContextHelper; 27 import ve.luz.ica.jackass.component.ApplicationContextImpl; 28 import ve.luz.ica.jackass.component.ApplicationContextPOATie; 29 import ve.luz.ica.jackass.component.DeploymentRequirement; 30 import ve.luz.ica.jackass.deploy.UnableToDeployException; 31 import ve.luz.ica.jackass.deploy.UnableToUndeployException; 32 import ve.luz.ica.jackass.deploy.daemon.NodeDeployerPOA; 33 import ve.luz.ica.jackass.deploy.daemon.ProxyInfo; 34 import ve.luz.ica.jackass.deploy.descriptor.ica.IcaJackassApplication; 35 import ve.luz.ica.jackass.deploy.descriptor.standard.JackassApplication; 36 import ve.luz.ica.jackass.deploy.descriptor.standard.Property; 37 import ve.luz.ica.jackass.deploy.util.Application; 38 import ve.luz.ica.jackass.deploy.util.Component; 39 import ve.luz.ica.jackass.instantiator.JInstantiator; 40 import ve.luz.ica.jackass.instantiator.StatelessPoolData; 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.util.ZipUtil; 45 46 51 public class NodeDeployerImpl extends NodeDeployerPOA 52 { 53 private static final Log LOG = LogFactory.getLog(NodeDeployerImpl.class); 54 55 private static final String ZIP_EXTENSION = ".zip"; 56 private static final String POOL_MAX_IDLE = "500"; 57 private static final String POOL_MAX_ACTIVE = "100"; 58 private static final String POOL_CHECK_INTERVAL = "100"; 59 private static final String POOL_MAX_IDLE_PROPERTY = "pool.maxactive"; 60 private static final String POOL_MAX_ACTIVE_PROPERTY = "pool.maxidle"; 61 private static final String POOL_CHECK_INTERVAL_PROPERTY = "pool.checkinterval"; 62 63 private static final String JAR_FILE_PREFIX = "jackass-"; 64 private static final String JAR_EXTENSION = ".jar"; 65 66 private String deploymentPath = null; 67 private int instantiatorCounter = 0; 68 private int totalInstantiators = 0; 69 private POA contextPoa = null; 70 private NamingContextExt rootNamingContext = null; 71 private ComponentDeployer componentDeployer = null; 72 private ArrayList instantiatorList = null; 73 74 82 public NodeDeployerImpl(int numInstantiators, POA ctxPoa, POA prxPoa, NamingContextExt rootNamingCtx) 83 { 84 Properties cf = ConfigurationManager.getConfigFile(); 85 this.deploymentPath = cf.getProperty(ConfigurationManager.DEPLOYMENT_PATH_PROPERTY); 86 87 this.contextPoa = ctxPoa; 88 this.rootNamingContext = rootNamingCtx; 89 90 this.instantiatorList = new ArrayList (); 91 this.totalInstantiators = numInstantiators; 92 93 int poolMaxIdle = Integer.parseInt(cf.getProperty(POOL_MAX_IDLE_PROPERTY, POOL_MAX_IDLE)); 95 int poolMaxActive = Integer.parseInt(cf.getProperty(POOL_MAX_ACTIVE_PROPERTY, POOL_MAX_ACTIVE)); 96 int poolCheckInterval = Integer.parseInt(cf.getProperty(POOL_CHECK_INTERVAL_PROPERTY, POOL_CHECK_INTERVAL)); 97 StatelessPoolData statelessPoolData = new StatelessPoolData(poolMaxActive, poolMaxIdle, poolCheckInterval); 98 99 componentDeployer = new ComponentDeployer(ctxPoa, prxPoa, statelessPoolData); 100 } 101 102 106 public String getDeploymentPath() 107 { 108 return this.deploymentPath; 109 } 110 111 123 public ProxyInfo[] deploy(String applicationName, RemoteFile remoteFile) throws UnableToDeployException 124 { 125 try 126 { 127 if (LOG.isInfoEnabled()) LOG.info("Deploying application " + applicationName); 128 129 String zipFileName = applicationName + ZIP_EXTENSION; 130 String zipPath = FileUtil.getRemoteFile(remoteFile, deploymentPath, zipFileName); 131 132 InputStream stantardIs = ZipUtil.getInputStream(zipPath, Application.STANDARD_DESCRIPTOR_NAME); 133 InputStream icaIs = ZipUtil.getInputStream(zipPath, Application.ICA_DESCRIPTOR_NAME); 134 135 Reader standardReader = new InputStreamReader (stantardIs); 136 Reader icaReader = new InputStreamReader (icaIs); 137 JackassApplication jackApp = JackassApplication.unmarshal(standardReader); 138 IcaJackassApplication icaApp = IcaJackassApplication.unmarshal(icaReader); 139 Application app = new Application(jackApp, icaApp); 140 141 File deploymentDir = new File (deploymentPath, app.getName()); 142 deploymentDir.mkdir(); 143 144 ZipFile zipFile = new ZipFile (zipPath); 145 String depPath = ZipUtil.extractFiles(zipFile, deploymentDir, null); 146 147 if (LOG.isDebugEnabled()) LOG.debug("Extracted archive " + remoteFile.getName()); 148 149 ProxyInfo[] proxyInfoList = this.deployApplication(depPath, app); 150 151 if (LOG.isInfoEnabled()) LOG.info("Application " + applicationName + " deployed"); 152 153 return proxyInfoList; 154 } 155 catch (Exception e) 156 { 157 if (LOG.isErrorEnabled()) LOG.error("Unexpected error deploying", e); 158 throw new UnableToDeployException(e.getMessage()); 159 } 160 } 161 162 170 private ProxyInfo[] deployApplication(String deploymentPath, Application app) throws Exception 171 { 172 URLClassLoader proxyClassLoader = this.createClassLoader(deploymentPath, app); 175 176 if (LOG.isDebugEnabled()) LOG.debug("Creating the application context"); 178 179 Property[] properties = null; 180 ve.luz.ica.jackass.deploy.descriptor.standard.Properties propertyList = app.getProperties(); 181 if (propertyList != null) 182 { 183 properties = propertyList.getProperty(); 184 } 185 186 ApplicationContextImpl contextImpl = new ApplicationContextImpl(rootNamingContext, properties); 187 ApplicationContextPOATie contextServant = new ApplicationContextPOATie(contextImpl); 188 byte[] contextID = app.getName().getBytes(); 189 org.omg.CORBA.Object obj = contextPoa.create_reference_with_id(contextID, ApplicationContextHelper.id()); 190 contextPoa.activate_object_with_id(contextID, contextServant); 191 ApplicationContext appContext = ApplicationContextHelper.narrow(obj); 192 if (LOG.isDebugEnabled()) LOG.debug("Application context created"); 193 194 ProxyInfo[] proxyInfoList = new ProxyInfo[app.getComponentCount()]; 198 List initializedInstantiators = new ArrayList (); 199 Iterator components = app.getComponents(); 200 int n = 0; 201 while (components.hasNext()) 202 { 203 Component comp = (Component) components.next(); 204 205 JInstantiator instantiator = this.findInstantiator(comp); 206 if (instantiator != null) 207 { 208 if (!initializedInstantiators.contains(instantiator)) 209 { 210 instantiator.registerApplication(app.getName(), deploymentPath); 211 initializedInstantiators.add(instantiator); 212 } 213 214 if (LOG.isDebugEnabled()) LOG.debug("Creating component " + comp.getName()); 215 ProxyInfo proxyInfo = componentDeployer.deployComponent(instantiator, app, comp, 216 appContext, proxyClassLoader); 217 proxyInfoList[n++] = proxyInfo; 218 } 219 else 220 { 221 if (LOG.isErrorEnabled()) LOG.error("No instantiator found for component " + comp.getName()); 222 throw new Exception ("No instantiator found for component " + comp.getName()); 223 } 224 } 225 return proxyInfoList; 226 } 227 228 235 private URLClassLoader createClassLoader(String deploymentPath, Application app) 236 throws MalformedURLException 237 { 238 File file = new File (deploymentPath, JAR_FILE_PREFIX + app.getName() + JAR_EXTENSION); 239 240 if (LOG.isDebugEnabled()) 241 LOG.info("loading " + file.getPath()); 242 243 URL [] urls = {file.toURL()}; 244 URLClassLoader loader = new URLClassLoader (urls); 245 return loader; 246 } 247 248 253 private JInstantiator findInstantiator(Component comp) 254 { 255 ve.luz.ica.jackass.deploy.descriptor.ica.Requirement[] compReq = 256 comp.getDeploymentRequirements().getRequirement(); 257 Iterator instantiators = instantiatorList.iterator(); 258 while (instantiators.hasNext()) 259 { 260 JInstantiator instantiator = (JInstantiator) instantiators.next(); 261 DeploymentRequirement[] instantiatorReq = instantiator.getDeployementRequirements(); 262 if (this.meetsRequirements(compReq, instantiatorReq)) 263 { 264 return instantiator; 265 } 266 } 267 return null; 268 } 269 270 277 private boolean meetsRequirements( 278 ve.luz.ica.jackass.deploy.descriptor.ica.Requirement[] componentReq, 279 DeploymentRequirement[] instantiatorReq) 280 { 281 for (int n = 0; n < componentReq.length; ++n) 282 { 283 if (!componentReq[n].getValue().equals(instantiatorReq[n].value) || 284 !componentReq[n].getVersion().equals(instantiatorReq[n].version)) 285 { 286 return false; 287 } 288 } 289 return true; 290 } 291 292 296 public void addInstantiator(JInstantiator instantiator) 297 { 298 if (LOG.isDebugEnabled()) LOG.debug("Registering insntantiator number " + instantiatorCounter); 299 if (instantiatorList.contains(instantiator)) 300 { 301 return; 303 } 304 instantiatorList.add(instantiator); 305 instantiatorCounter++; 306 if (instantiatorCounter >= totalInstantiators) 307 { 308 synchronized (this) 309 { 310 notify(); 311 } 312 } 313 } 314 315 320 public void undeploy(String applicationName) throws UnableToUndeployException 321 { 322 try 323 { 324 LOG.info("Undeploying application " + applicationName); 325 326 boolean error = false; 327 328 File zipFile = new File (deploymentPath, applicationName + ZIP_EXTENSION); 329 String zipPath = zipFile.getPath(); 330 331 InputStream stantardIs = ZipUtil.getInputStream(zipPath, Application.STANDARD_DESCRIPTOR_NAME); 332 InputStream icaIs = ZipUtil.getInputStream(zipPath, Application.ICA_DESCRIPTOR_NAME); 333 334 Reader standardReader = new InputStreamReader (stantardIs); 335 Reader icaReader = new InputStreamReader (icaIs); 336 JackassApplication jackApp = JackassApplication.unmarshal(standardReader); 337 IcaJackassApplication icaApp = IcaJackassApplication.unmarshal(icaReader); 338 Application app = new Application(jackApp, icaApp); 339 340 try 342 { 343 contextPoa.deactivate_object(app.getName().getBytes()); 344 } 345 catch (Exception e) 346 { 347 LOG.warn("The contextPoa has reported this exception " + e.getMessage()); 348 if (LOG.isDebugEnabled()) LOG.debug("Stack trace follows", e); 349 } 350 351 Iterator components = app.getComponents(); 352 while (components.hasNext()) 353 { 354 try 355 { 356 Component comp = (Component) components.next(); 357 componentDeployer.undeployComponent(app, comp); 358 } 359 catch (Exception e) 360 { 361 error = true; 362 LOG.warn(e.getMessage()); 363 if (LOG.isDebugEnabled()) LOG.debug(e); 364 } 365 } 366 367 Iterator instantiators = instantiatorList.iterator(); 369 while (instantiators.hasNext()) 370 { 371 JInstantiator instantiator = (JInstantiator) instantiators.next(); 372 try 373 { 374 instantiator.unregisterApplication(applicationName); 375 } 376 catch (Exception e) 377 { 378 error = true; 379 LOG.warn("Error removing the application from the instantiator " + e.getMessage()); 380 if (LOG.isDebugEnabled()) LOG.debug("Stack trace follows", e); 381 } 382 } 383 384 if (error) 385 { 386 throw new Exception ("Some warnings were issued during undeployment. You should check the log files"); 387 } 388 } 389 catch (Exception e) 390 { 391 LOG.warn("Error undeploying application " + e.getMessage()); 392 if (LOG.isDebugEnabled()) LOG.error("Stack trace follows", e); 393 throw new UnableToUndeployException(e.getMessage()); 394 } 395 finally 396 { 397 File applicationDirectory = new File (deploymentPath, applicationName); 398 File zipFile = new File (deploymentPath, applicationName + ZIP_EXTENSION); 399 if (LOG.isDebugEnabled()) LOG.debug("Deleting application directory " + applicationDirectory); 400 this.deleteFile(applicationDirectory); 401 this.deleteFile(zipFile); 402 403 if (LOG.isInfoEnabled()) LOG.info("Application " + applicationName + " undeployed"); 404 } 405 } 406 407 411 private void deleteFile(File file) 412 { 413 if (file.isDirectory()) 414 { 415 File [] fileList = file.listFiles(); 416 for (int i = 0; i<fileList.length; ++i) 417 { 418 deleteFile(fileList[i]); 419 } 420 } 421 file.delete(); 422 } 423 424 } 425 | Popular Tags |