1 16 17 package org.apache.axis2.deployment; 18 19 import org.apache.axis2.context.ConfigurationContextFactory; 20 import org.apache.axis2.deployment.listener.RepositoryListenerImpl; 21 import org.apache.axis2.deployment.repository.util.ArchiveFileData; 22 import org.apache.axis2.deployment.repository.util.ArchiveReader; 23 import org.apache.axis2.deployment.repository.util.WSInfo; 24 import org.apache.axis2.deployment.scheduler.DeploymentIterator; 25 import org.apache.axis2.deployment.scheduler.Scheduler; 26 import org.apache.axis2.deployment.scheduler.SchedulerTask; 27 import org.apache.axis2.deployment.util.DeploymentData; 28 import org.apache.axis2.description.*; 29 import org.apache.axis2.engine.AxisConfiguration; 30 import org.apache.axis2.engine.AxisConfigurationImpl; 31 import org.apache.axis2.engine.AxisFault; 32 import org.apache.axis2.engine.Handler; 33 import org.apache.axis2.modules.Module; 34 import org.apache.axis2.phaseresolver.PhaseException; 35 import org.apache.axis2.phaseresolver.PhaseMetadata; 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 import javax.xml.namespace.QName ; 40 import javax.xml.stream.XMLStreamException; 41 import java.io.*; 42 import java.util.*; 43 44 45 public class DeploymentEngine implements DeploymentConstants { 46 47 private Log log = LogFactory.getLog(getClass()); 48 private static Scheduler scheduler; 49 50 public static String axis2repository = null; 51 52 53 private boolean hotDeployment = true; private boolean hotUpdate = true; 56 57 60 private List wsToDeploy = new ArrayList(); 61 64 private List wsToUnDeploy = new ArrayList(); 65 66 71 private AxisConfiguration axisConfig; 72 73 76 77 private String folderName; 78 79 private String engineConfigName; 80 81 84 86 private ArchiveFileData currentArchiveFile; 87 88 private ConfigurationContextFactory factory; 90 91 94 public DeploymentEngine() { 95 } 96 97 104 105 public DeploymentEngine(String RepositaryName) throws DeploymentException { 106 this(RepositaryName, "axis2.xml"); 107 } 108 109 public DeploymentEngine(String RepositaryName, String serverXMLFile) throws DeploymentException { 110 if(RepositaryName == null || RepositaryName.trim().equals("")){ 111 throw new DeploymentException("Axis2 repositary can not be null"); 112 } 113 this.folderName = RepositaryName; 114 axis2repository = RepositaryName; 115 File repository = new File(RepositaryName); 116 if (!repository.exists()) { 117 repository.mkdirs(); 118 File servcies = new File(repository, "services"); 119 File modules = new File(repository, "modules"); 120 modules.mkdirs(); 121 servcies.mkdirs(); 122 } 123 File serverConf = new File(repository, serverXMLFile); 124 if (!serverConf.exists()) { 125 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 126 InputStream in = cl.getResourceAsStream("org/apache/axis2/deployment/axis2.xml"); 127 if (in != null) { 128 try { 129 serverConf.createNewFile(); 130 FileOutputStream out = new FileOutputStream(serverConf); 131 int BUFSIZE = 512; byte[] buf = new byte[BUFSIZE]; 133 int read; 134 while ((read = in.read(buf)) > 0) { 135 out.write(buf, 0, read); 136 } 137 in.close(); 138 out.close(); 139 } catch (IOException e) { 140 throw new DeploymentException(e); 141 } 142 143 144 } else { 145 throw new DeploymentException("can not found org/apache/axis2/deployment/axis2.xml"); 146 147 } 148 } 149 factory = new ConfigurationContextFactory(); 150 this.engineConfigName = RepositaryName + '/' + serverXMLFile; 151 } 152 153 public ArchiveFileData getCurrentFileItem() { 154 return currentArchiveFile; 155 } 156 157 158 163 public AxisConfiguration getAxisConfig() { 164 return axisConfig; 165 } 166 167 170 private void setDeploymentFeatures() { 171 String value; 172 Parameter parahotdeployment = ((AxisConfigurationImpl) axisConfig).getParameter(HOTDEPLOYMENT); 173 Parameter parahotupdate = ((AxisConfigurationImpl) axisConfig).getParameter(HOTUPDATE); 174 if (parahotdeployment != null) { 175 value = (String ) parahotdeployment.getValue(); 176 if ("false".equals(value)) 177 hotDeployment = false; 178 } 179 if (parahotupdate != null) { 180 value = (String ) parahotupdate.getValue(); 181 if ("false".equals(value)) 182 hotUpdate = false; 183 184 } 185 } 186 187 public AxisConfiguration load() throws DeploymentException { 188 if (engineConfigName == null) { 189 throw new DeploymentException("path to axis2.xml can not be NUll"); 190 } 191 File tempfile = new File(engineConfigName); 192 try { 193 InputStream in = new FileInputStream(tempfile); 194 axisConfig = createEngineConfig(); 195 DeploymentParser parser = new DeploymentParser(in, this); 196 parser.processGlobalConfig(((AxisConfigurationImpl) axisConfig), AXIS2CONFIG); 197 } catch (FileNotFoundException e) { 198 throw new DeploymentException("Exception at deployment", e); 199 } catch (XMLStreamException e) { 200 throw new DeploymentException(e); 201 } 202 setDeploymentFeatures(); 203 if (hotDeployment) { 204 startSearch(this); 205 } else { 206 new RepositoryListenerImpl(folderName, this); 207 } 208 try { 209 engagdeModules(); 210 validateSystemPredefinedPhases(); 211 } catch (AxisFault axisFault) { 212 log.info("Module validation failed" + axisFault.getMessage()); 213 throw new DeploymentException(axisFault); 214 } 215 return axisConfig; 216 } 217 218 219 public AxisConfiguration loadClient(String clientHome) throws DeploymentException { 220 InputStream in = null; 221 axis2repository = clientHome; 222 boolean isRepositoryExist = false; 223 if (!(clientHome == null ||clientHome.trim().equals(""))) { 224 checkClientHome(clientHome); 225 isRepositoryExist = true; 226 try { 227 File tempfile = new File(engineConfigName); 228 in = new FileInputStream(tempfile); 229 } catch (FileNotFoundException e) { 230 throw new DeploymentException("Exception at deployment", e); 231 } 232 } else { 233 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 234 in = cl.getResourceAsStream("org/apache/axis2/deployment/axis2.xml"); 235 } 236 try { 237 axisConfig = createEngineConfig(); 238 DeploymentParser parser = new DeploymentParser(in, this); 239 parser.processGlobalConfig(((AxisConfigurationImpl) axisConfig), AXIS2CONFIG); 240 } catch (XMLStreamException e) { 241 throw new DeploymentException(e); 242 } 243 if (isRepositoryExist) { 244 hotDeployment = false; 245 hotUpdate = false; 246 new RepositoryListenerImpl(folderName, this); 247 try { 248 engagdeModules(); 249 } catch (AxisFault axisFault) { 250 log.info("Module validation failed" + axisFault.getMessage()); 251 throw new DeploymentException(axisFault); 252 } 253 } 254 return axisConfig; 255 } 256 257 258 private void checkClientHome(String clientHome) throws DeploymentException { 259 String clientXML = "axis2.xml"; 260 this.folderName = clientHome; 261 File repository = new File(clientHome); 262 if (!repository.exists()) { 263 repository.mkdirs(); 264 File servcies = new File(repository, "services"); 265 File modules = new File(repository, "modules"); 266 modules.mkdirs(); 267 servcies.mkdirs(); 268 } 269 File serverConf = new File(repository, clientXML); 270 if (!serverConf.exists()) { 271 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 272 InputStream in = cl.getResourceAsStream("org/apache/axis2/deployment/axis2.xml"); 273 if (in != null) { 274 try { 275 serverConf.createNewFile(); 276 FileOutputStream out = new FileOutputStream(serverConf); 277 int BUFSIZE = 512; byte[] buf = new byte[BUFSIZE]; 279 int read; 280 while ((read = in.read(buf)) > 0) { 281 out.write(buf, 0, read); 282 } 283 in.close(); 284 out.close(); 285 } catch (IOException e) { 286 throw new DeploymentException(e); 287 } 288 289 290 } else { 291 throw new DeploymentException("can not found org/apache/axis2/deployment/axis2.xml"); 292 293 } 294 } 295 factory = new ConfigurationContextFactory(); 296 this.engineConfigName = clientHome + '/' + clientXML; 297 } 298 299 303 private void engagdeModules() throws AxisFault { 304 ArrayList modules = DeploymentData.getInstance().getModules(); 305 for (Iterator iterator = modules.iterator(); iterator.hasNext();) { 307 QName name = (QName ) iterator.next(); 308 ((AxisConfigurationImpl) axisConfig).engageModule(name); 309 } 310 } 311 312 318 private void validateSystemPredefinedPhases() throws DeploymentException { 319 DeploymentData tempdata = DeploymentData.getInstance(); 320 ArrayList inPhases = tempdata.getINPhases(); 321 if (!(((String ) inPhases.get(0)).equals(PhaseMetadata.PHASE_TRANSPORTIN) && 323 ((String ) inPhases.get(1)).equals(PhaseMetadata.PHASE_PRE_DISPATCH) && 324 ((String ) inPhases.get(2)).equals(PhaseMetadata.PHASE_DISPATCH) && 325 ((String ) inPhases.get(3)).equals(PhaseMetadata.PHASE_POST_DISPATCH))) { 326 throw new DeploymentException("Invalid System predefined inphases , phase order dose not" + 327 " support\n recheck axis2.xml"); 328 } 329 } 333 334 public ModuleDescription getModule(QName moduleName) throws AxisFault { 335 ModuleDescription axisModule = axisConfig.getModule(moduleName); 336 return axisModule; 337 } 338 339 343 private void startSearch(DeploymentEngine engine) { 344 scheduler = new Scheduler(); 345 scheduler.schedule(new SchedulerTask(engine, folderName), new DeploymentIterator()); 346 } 347 348 private AxisConfiguration createEngineConfig() { 349 AxisConfiguration newEngineConfig = new AxisConfigurationImpl(); 350 return newEngineConfig; 351 } 352 353 354 private void addnewService(ServiceDescription serviceMetaData) throws AxisFault { 355 try { 356 loadServiceProperties(serviceMetaData); 357 axisConfig.addService(serviceMetaData); 358 359 ArrayList list = currentArchiveFile.getModules(); 360 for(int i = 0;i<list.size();i++){ 361 ModuleDescription module = axisConfig.getModule((QName )list.get(i)); 362 if (module != null) { 363 serviceMetaData.engageModule(module); 364 } else { 365 throw new DeploymentException("Service " + serviceMetaData.getName().getLocalPart() + 366 " Refer to invalide module " + ((QName )list.get(i)).getLocalPart()); 367 } 368 } 369 370 HashMap opeartions = serviceMetaData.getOperations(); 371 Collection opCol = opeartions.values(); 372 for (Iterator iterator = opCol.iterator(); iterator.hasNext();) { 373 OperationDescription opDesc = (OperationDescription) iterator.next(); 374 ArrayList modules = opDesc.getModuleRefs(); 375 for (int i = 0; i < modules.size(); i++) { 376 QName moduleName = (QName ) modules.get(i); 377 ModuleDescription module = axisConfig.getModule(moduleName); 378 if(module != null) { 379 opDesc.engageModule(module); 380 } else { 381 throw new DeploymentException("Operation " + opDesc.getName().getLocalPart() + 382 " Refer to invalide module " + moduleName.getLocalPart()); 383 } 384 } 385 386 } 387 System.out.println("service Description : " + serviceMetaData.getServiceDescription()); 389 System.out.println("adding new service : " + serviceMetaData.getName().getLocalPart()); 390 } catch (PhaseException e) { 391 throw new AxisFault(e); 392 } 393 394 } 395 396 403 private void loadServiceProperties(ServiceDescription axisService) throws AxisFault { 404 Flow inflow = axisService.getInFlow(); 405 if (inflow != null) { 406 addFlowHandlers(inflow); 407 } 408 409 Flow outFlow = axisService.getOutFlow(); 410 if (outFlow != null) { 411 addFlowHandlers(outFlow); 412 } 413 414 Flow faultInFlow = axisService.getFaultInFlow(); 415 if (faultInFlow != null) { 416 addFlowHandlers(faultInFlow); 417 } 418 419 Flow faultOutFlow = axisService.getFaultOutFlow(); 420 if (faultOutFlow != null) { 421 addFlowHandlers(faultOutFlow); 422 } 423 axisService.setClassLoader(currentArchiveFile.getClassLoader()); 424 } 425 426 427 private void loadModuleClass(ModuleDescription module) throws AxisFault { 428 Class moduleClass = null; 429 try { 430 String readInClass = currentArchiveFile.getModuleClass(); 431 if (readInClass != null && !"".equals(readInClass)) { 432 moduleClass = Class.forName(readInClass, true, currentArchiveFile.getClassLoader()); 433 module.setModule((Module) moduleClass.newInstance()); 434 } 435 } catch (Exception e) { 436 throw new AxisFault(e.getMessage(), e); 437 } 438 439 } 440 441 442 private void addFlowHandlers(Flow flow) throws AxisFault { 443 int count = flow.getHandlerCount(); 444 ClassLoader loader1 = currentArchiveFile.getClassLoader(); 445 for (int j = 0; j < count; j++) { 446 HandlerDescription handlermd = flow.getHandler(j); 447 Class handlerClass = null; 448 Handler handler; 449 handlerClass = getHandlerClass(handlermd.getClassName(), loader1); 450 try { 451 handler = (Handler) handlerClass.newInstance(); 452 handler.init(handlermd); 453 handlermd.setHandler(handler); 454 455 } catch (InstantiationException e) { 456 throw new AxisFault(e.getMessage()); 457 } catch (IllegalAccessException e) { 458 throw new AxisFault(e.getMessage()); 459 } 460 461 } 462 } 463 464 465 public Class getHandlerClass(String className, ClassLoader loader1) throws AxisFault { 466 Class handlerClass = null; 467 468 try { 469 handlerClass = Class.forName(className, true, loader1); 470 } catch (ClassNotFoundException e) { 471 throw new AxisFault(e.getMessage()); 472 } 473 return handlerClass; 474 } 475 476 477 private void addNewModule(ModuleDescription moduelmetada) throws AxisFault { 478 Flow inflow = moduelmetada.getInFlow(); 480 if (inflow != null) { 481 addFlowHandlers(inflow); 482 } 483 Flow outFlow = moduelmetada.getOutFlow(); 484 if (outFlow != null) { 485 addFlowHandlers(outFlow); 486 } 487 Flow faultInFlow = moduelmetada.getFaultInFlow(); 488 if (faultInFlow != null) { 489 addFlowHandlers(faultInFlow); 490 } 491 492 Flow faultOutFlow = moduelmetada.getFaultOutFlow(); 493 if (faultOutFlow != null) { 494 addFlowHandlers(faultOutFlow); 495 } 496 loadModuleClass(moduelmetada); 497 axisConfig.addMdoule(moduelmetada); 498 System.out.println("adding new module"); 499 } 500 501 502 505 public void addtowsToDeploy(ArchiveFileData file) { 506 wsToDeploy.add(file); 507 } 508 509 512 public void addtowstoUnDeploy(WSInfo file) { 513 wsToUnDeploy.add(file); 514 } 515 516 public void doDeploy() { 517 if (wsToDeploy.size() > 0) { 518 for (int i = 0; i < wsToDeploy.size(); i++) { 519 currentArchiveFile = (ArchiveFileData) wsToDeploy.get(i); 520 int type = currentArchiveFile.getType(); 521 try { 522 currentArchiveFile.setClassLoader(); 523 } catch (AxisFault axisFault) { 524 log.info("Setting Class Loader " +axisFault); 525 continue; 526 } 527 ArchiveReader archiveReader = new ArchiveReader(); 528 String serviceStatus = ""; 529 StringWriter errorWriter= new StringWriter(); 530 switch (type) { 531 case SERVICE: 532 try { 533 ServiceDescription service = archiveReader.createService(currentArchiveFile); 535 archiveReader.readServiceArchive(currentArchiveFile.getAbsolutePath(), this, service); 536 addnewService(service); 537 log.info("Deployement WS Name " + currentArchiveFile.getName()); 538 } catch (DeploymentException de) { 539 log.info("Invalid service" + currentArchiveFile.getName()); 540 log.info("DeploymentException " + de); 541 PrintWriter error_ptintWriter = new PrintWriter(errorWriter); 542 de.printStackTrace(error_ptintWriter); 543 serviceStatus = "Error:\n" + errorWriter.toString(); 544 } catch (AxisFault axisFault) { 545 log.info("Invalid service" + currentArchiveFile.getName()); 546 log.info("AxisFault " + axisFault); 547 PrintWriter error_ptintWriter = new PrintWriter(errorWriter); 548 axisFault.printStackTrace(error_ptintWriter); 549 serviceStatus = "Error:\n" + errorWriter.toString(); 550 } catch (Exception e) { 551 log.info("Invalid service" + currentArchiveFile.getName()); 552 log.info("Exception " + e); 553 PrintWriter error_ptintWriter = new PrintWriter(errorWriter); 554 e.printStackTrace(error_ptintWriter); 555 serviceStatus = "Error:\n" + errorWriter.toString(); 556 } finally { 557 if (serviceStatus.startsWith("Error:")) { 558 axisConfig.getFaulytServices().put(getAxisServiceName(currentArchiveFile.getName()), serviceStatus); 559 } 560 currentArchiveFile = null; 561 } 562 break; 563 case MODULE: 564 String moduleStatus = ""; 565 try { 566 ModuleDescription metaData = new ModuleDescription(); 567 archiveReader.readModuleArchive(currentArchiveFile.getAbsolutePath(), this, metaData); 568 addNewModule(metaData); 569 log.info("Moduel WS Name " + currentArchiveFile.getName() + " modulename :" + metaData.getName()); 570 } catch (DeploymentException e) { 571 log.info("Invalid module" + currentArchiveFile.getName()); 572 log.info("DeploymentException " + e); 573 PrintWriter error_ptintWriter = new PrintWriter(errorWriter); 574 e.printStackTrace(error_ptintWriter); 575 moduleStatus = "Error:\n" + errorWriter.toString(); 576 } catch (AxisFault axisFault) { 577 log.info("Invalid module" + currentArchiveFile.getName()); 578 log.info("AxisFault " + axisFault); 579 PrintWriter error_ptintWriter = new PrintWriter(errorWriter); 580 axisFault.printStackTrace(error_ptintWriter); 581 moduleStatus = "Error:\n" + errorWriter.toString(); 582 } finally { 583 if (moduleStatus.startsWith("Error:")) { 584 axisConfig.getFaulytModules().put(getAxisServiceName(currentArchiveFile.getName()), moduleStatus); 585 } 586 currentArchiveFile = null; 587 } 588 break; 589 590 } 591 } 592 } 593 wsToDeploy.clear(); 594 } 595 596 public void unDeploy() { 597 String serviceName = ""; 598 try { 599 if (wsToUnDeploy.size() > 0) { 600 for (int i = 0; i < wsToUnDeploy.size(); i++) { 601 WSInfo wsInfo = (WSInfo) wsToUnDeploy.get(i); 602 if (wsInfo.getType() == SERVICE) { 603 serviceName = getAxisServiceName(wsInfo.getFilename()); 604 axisConfig.removeService(new QName (serviceName)); 605 log.info("UnDeployement WS Name " + wsInfo.getFilename()); 606 } 607 axisConfig.getFaulytServices().remove(serviceName); 608 } 609 610 } 611 } catch (AxisFault e) { 612 log.info("AxisFault " + e); 613 } 614 wsToUnDeploy.clear(); 615 } 616 617 public boolean isHotUpdate() { 618 return hotUpdate; 619 } 620 621 628 private String getAxisServiceName(String fileName) { 629 char seperator = '.'; 630 String value = null; 631 int index = fileName.indexOf(seperator); 632 if (index > 0) { 633 value = fileName.substring(0, index); 634 return value; 635 } 636 return fileName; 637 } 638 639 658 659 671 public ServiceDescription buildService(ServiceDescription axisService, InputStream serviceInputStream, ClassLoader classLoader) throws DeploymentException { 672 try { 673 currentArchiveFile = new ArchiveFileData(SERVICE, ""); 674 currentArchiveFile.setClassLoader(classLoader); 675 DeploymentParser schme = new DeploymentParser(serviceInputStream, this); 676 schme.parseServiceXML(axisService); 677 loadServiceProperties(axisService); 678 axisConfig.addService(axisService); 679 } catch (XMLStreamException e) { 680 throw new DeploymentException(e); 681 } catch (AxisFault axisFault) { 682 throw new DeploymentException(axisFault); 683 } 684 return axisService; 685 } 686 687 694 695 public ModuleDescription buildModule(File modulearchive) throws DeploymentException { 696 ModuleDescription axismodule = null; 697 try { 698 currentArchiveFile = new ArchiveFileData(modulearchive, MODULE); 699 axismodule = new ModuleDescription(); 700 ArchiveReader archiveReader = new ArchiveReader(); 701 archiveReader.readModuleArchive(currentArchiveFile.getAbsolutePath(), this, axismodule); 702 currentArchiveFile.setClassLoader(); 703 Flow inflow = axismodule.getInFlow(); 704 if (inflow != null) { 705 addFlowHandlers(inflow); 706 } 707 Flow outFlow = axismodule.getOutFlow(); 708 if (outFlow != null) { 709 addFlowHandlers(outFlow); 710 } 711 Flow faultInFlow = axismodule.getFaultInFlow(); 712 if (faultInFlow != null) { 713 addFlowHandlers(faultInFlow); 714 } 715 Flow faultOutFlow = axismodule.getFaultOutFlow(); 716 if (faultOutFlow != null) { 717 addFlowHandlers(faultOutFlow); 718 } 719 loadModuleClass(axismodule); 720 } catch (AxisFault axisFault) { 721 throw new DeploymentException(axisFault); 722 } 723 return axismodule; 724 } 725 726 } 727 | Popular Tags |