1 25 26 27 package org.objectweb.jonas.resource; 28 29 import java.io.File ; 30 import java.io.IOException ; 31 import java.net.URL ; 32 import java.rmi.RemoteException ; 33 import java.util.ArrayList ; 34 import java.util.Enumeration ; 35 import java.util.HashSet ; 36 import java.util.List ; 37 import java.util.Set ; 38 import java.util.StringTokenizer ; 39 import java.util.Vector ; 40 41 import javax.management.InstanceAlreadyExistsException ; 42 import javax.management.InstanceNotFoundException ; 43 import javax.management.MBeanRegistrationException ; 44 import javax.management.MBeanServer ; 45 import javax.naming.Context ; 46 import javax.naming.NamingException ; 47 import javax.resource.spi.XATerminator ; 48 import javax.resource.spi.work.WorkManager ; 49 50 import org.objectweb.transaction.jta.TransactionManager; 51 52 import org.objectweb.jonas_lib.JWorkManager; 53 import org.objectweb.jonas_rar.deployment.lib.wrapper.RarManagerWrapper; 54 55 import org.objectweb.jonas.common.JModule; 56 import org.objectweb.jonas.common.JProp; 57 import org.objectweb.jonas.common.Log; 58 import org.objectweb.jonas.container.EJBService; 59 import org.objectweb.jonas.jmx.JmxService; 60 import org.objectweb.jonas.jmx.JonasObjectName; 61 import org.objectweb.jonas.jtm.TransactionService; 62 import org.objectweb.jonas.naming.CompNamingContext; 63 import org.objectweb.jonas.server.LoaderManager; 64 import org.objectweb.jonas.service.AbsServiceImpl; 65 import org.objectweb.jonas.service.ServiceException; 66 import org.objectweb.jonas.service.ServiceManager; 67 68 import org.objectweb.util.monolog.api.BasicLevel; 69 import org.objectweb.util.monolog.api.Logger; 70 71 81 public class ResourceServiceImpl extends AbsServiceImpl implements 82 ResourceService, 83 ResourceServiceImplMBean { 84 85 89 private static Logger logger = null; 90 93 private static Logger poolLogger = null; 94 97 private static Logger setterLogger = null; 98 101 private static Logger manageLogger = null; 102 103 105 109 public static final String AUTOLOADDIR = "jonas.service.resource.autoloaddir"; 110 113 public static final String CLASS = "jonas.service.resource.class"; 114 117 public static final String JNDI_NAME = "jndiname"; 118 121 public static final String RAR_OBJNAME = "rarobjname"; 122 125 public static final String FACTORY_OFFSET = "factoryoffset"; 126 129 public static final String FACTORY_TYPE = "factorytype"; 130 133 public static final String RAR_FILENAME = "rarfilename"; 134 137 public static final String LNK_JNDI_NAME = "lnkjndiname"; 138 141 public static final String LNK_RAR_FILENAME = "lnkrarfilename"; 142 145 public static final String JONAS_RA_XML = "jonasraxml"; 146 149 public static final String RA_XML = "raxml"; 150 153 public static final String PARSINGWITHVALIDATION = "jonas.service.resource.parsingwithvalidation"; 154 157 public static final String RESOURCE_LIST = "jonas.service.resource.resources"; 158 161 public static final String THREADWAITTIMEOUT = "jonas.service.resource.threadwaittimeout"; 162 165 public static final String MINWORKTHREADS = "jonas.service.resource.minworkthreads"; 166 169 public static final String MAXWORKTHREADS = "jonas.service.resource.maxworkthreads"; 170 173 public static final String EXECTIMEOUT = "jonas.service.resource.worktimeout"; 174 175 178 public static final int DEF_WRK_THREADWAITTIMEOUT = 60; 179 182 public static final int DEF_WRK_THREADS = 5; 183 186 public static final int DEF_MAX_WRK_THREADS = 80; 187 190 public static final int DEF_EXEC_TIME = 0; 191 192 195 199 203 205 210 private Vector delayedRAs = new Vector (); 211 214 private boolean processingDelayed = false; 215 216 219 private TransactionService ts = null; 220 223 private TransactionManager tm = null; 224 225 228 private MBeanServer mbeanServer = null; 229 230 233 private Vector autoNames = null; 234 237 private Vector resourceNames = null; 238 239 240 244 private WorkManager workMgr = null; 245 248 private ResourceBootstrapContext bootCtx = null; 249 250 253 public static final String JONAS_BASE = JProp.getJonasBase(); 254 255 258 public static final String WORK_RARS_DIR = JProp.getWorkDir() + File.separator + "rars"; 259 260 263 public static final String RARSDIR = JProp.getJonasBase() + File.separator + "rars"; 264 265 268 private ClassLoader appsClassLoader; 269 270 273 public ResourceServiceImpl() { 274 } 275 276 286 public void doInit(Context ctx) { 287 if (logger == null) { 288 logger = Log.getLogger(Log.JONAS_JCA_PREFIX + ".process"); 289 } 290 if (poolLogger == null) { 291 poolLogger = Log.getLogger(Log.JONAS_JCA_PREFIX + ".pool"); 292 } 293 if (setterLogger == null) { 294 setterLogger = Log.getLogger(Log.JONAS_JCA_PREFIX + ".setters"); 295 } 296 if (manageLogger == null) { 297 manageLogger = Log.getLogger(Log.JONAS_JCA_PREFIX + ".management"); 298 } 299 300 try { 301 LoaderManager lm = LoaderManager.getInstance(); 302 appsClassLoader = lm.getAppsLoader(); 303 } catch (Exception e) { 304 logger.log(BasicLevel.ERROR, "Cannot get the Applications ClassLoader from RAR Container Service: " + e); 305 throw new ServiceException("Cannot get the Applications ClassLoader from RAR Container Service", e); 306 } 307 308 resourceNames = new Vector (); 309 autoNames = new Vector (); 310 311 String dirValue = null; 313 try { 314 dirValue = (String ) ctx.lookup(AUTOLOADDIR); 315 if (logger.isLoggable(BasicLevel.DEBUG)) { 316 logger.log(BasicLevel.DEBUG, "autoloaddir= " + dirValue); 317 } 318 } catch (NamingException e) { 319 if (logger.isLoggable(BasicLevel.DEBUG)) { 320 logger.log(BasicLevel.DEBUG, "No autoloaddir value specified in context, usually for client container"); 321 } 322 } 323 if (dirValue != null) { 324 StringTokenizer st = new StringTokenizer (dirValue, ","); 325 String dirName = null; 326 while (st.hasMoreTokens()) { 327 dirName = normalizePath(st.nextToken().trim()); 328 addRars(dirName); 329 } 330 } 331 332 try { 334 String rs = (String ) ctx.lookup(RESOURCE_LIST); 335 if (logger.isLoggable(BasicLevel.DEBUG)) { 336 logger.log(BasicLevel.DEBUG, "resource list= " + rs); 337 } 338 if (rs != null) { 339 StringTokenizer st = new StringTokenizer (rs, ","); 340 String resFilename = null; 341 while (st.hasMoreTokens()) { 342 resFilename = normalizePath(st.nextToken().trim()); 343 resourceNames.add(resFilename); 344 } 345 } 346 } catch (NamingException e) { 347 logger.log(BasicLevel.ERROR, "Cannot lookup the configuration context at Resource service starting"); 348 } 349 350 try { 352 ServiceManager sm = ServiceManager.getInstance(); 353 ts = (TransactionService) sm.getTransactionService(); 354 tm = ts.getTransactionManager(); 355 } catch (Exception e) { 356 logger.log(BasicLevel.ERROR, "Cannot get the Transaction service: " + e); 357 throw new ServiceException("Cannot get the Transaction service: ", e); 358 } 359 360 try { 362 mbeanServer = 363 ((JmxService) ServiceManager.getInstance().getJmxService()).getJmxServer(); 364 } catch (Exception e) { 365 mbeanServer = null; 367 } 368 369 String parsingMode = "false"; 371 try { 372 parsingMode = (String ) ctx.lookup(PARSINGWITHVALIDATION); 373 } catch (NamingException e) { 374 if (logger.isLoggable(BasicLevel.DEBUG)) { 376 logger.log(BasicLevel.DEBUG, "No parsingwithvalidation value specified in context"); 377 } 378 } 379 if ("false".equalsIgnoreCase(parsingMode)) { 380 RarManagerWrapper.setParsingWithValidation(false); 381 if (logger.isLoggable(BasicLevel.DEBUG)) { 382 logger.log(BasicLevel.DEBUG, "XML parsing without validation"); 383 } 384 } else { 385 if (logger.isLoggable(BasicLevel.DEBUG)) { 386 logger.log(BasicLevel.DEBUG, "XML parsing with validation"); 387 } 388 } 389 390 EJBService ejbService = null; 392 try { 393 ejbService = (EJBService) ServiceManager.getInstance().getEjbService(); 394 workMgr = ejbService.getWorkManager(); 395 } catch (Exception e) { 396 workMgr = null; 398 } 399 400 int execTime = 0; 402 try { 403 String etime = (String ) ctx.lookup(EXECTIMEOUT); 404 execTime = (new Integer (etime)).intValue(); 405 } catch (NamingException e) { 406 if (logger.isLoggable(BasicLevel.DEBUG)) { 408 logger.log(BasicLevel.DEBUG, "No exectimeout value specified in context, usually for client container"); 409 } 410 } 411 412 int threadWaitTimeout = DEF_WRK_THREADWAITTIMEOUT; 414 try { 415 String tTimeout = (String ) ctx.lookup(THREADWAITTIMEOUT); 416 threadWaitTimeout = (new Integer (tTimeout)).intValue(); 417 if (threadWaitTimeout <= 0) { 418 threadWaitTimeout = DEF_WRK_THREADWAITTIMEOUT; 419 if (logger.isLoggable(BasicLevel.DEBUG)) { 420 logger.log(BasicLevel.DEBUG, "Resetting thread wait timeout to " + DEF_WRK_THREADWAITTIMEOUT); 421 } 422 } 423 } catch (Exception e) { 424 if (logger.isLoggable(BasicLevel.DEBUG)) { 426 logger.log(BasicLevel.DEBUG, "No workthread wait timeout value specified in context"); 427 } 428 } 429 430 int minWorkThreads = DEF_WRK_THREADS; 432 try { 433 String wThreads = (String ) ctx.lookup(MINWORKTHREADS); 434 minWorkThreads = (new Integer (wThreads)).intValue(); 435 if (minWorkThreads <= 0) { 436 minWorkThreads = DEF_WRK_THREADS; 437 if (logger.isLoggable(BasicLevel.DEBUG)) { 438 logger.log(BasicLevel.DEBUG, "Resetting min threads to " + DEF_WRK_THREADS); 439 } 440 } 441 } catch (Exception e) { 442 if (logger.isLoggable(BasicLevel.DEBUG)) { 444 logger.log(BasicLevel.DEBUG, "No min workthreads value specified in context"); 445 } 446 } 447 448 int maxWorkThreads = DEF_MAX_WRK_THREADS; 450 try { 451 String wThreads = (String ) ctx.lookup(MAXWORKTHREADS); 452 maxWorkThreads = (new Integer (wThreads)).intValue(); 453 } catch (Exception e) { 454 if (logger.isLoggable(BasicLevel.DEBUG)) { 456 logger.log(BasicLevel.DEBUG, "No max workthreads value specified in context"); 457 } 458 } 459 460 if (maxWorkThreads > 0 || workMgr == null) { 462 logger.log(BasicLevel.DEBUG, "Create a WorkManager for Resources"); 463 if (maxWorkThreads <= 0) { 464 maxWorkThreads = DEF_MAX_WRK_THREADS; 465 if (logger.isLoggable(BasicLevel.DEBUG)) { 466 logger.log(BasicLevel.DEBUG, "Resetting max threads to " + DEF_WRK_THREADS); 467 } 468 } 469 workMgr = new JWorkManager(minWorkThreads, maxWorkThreads, tm, threadWaitTimeout); 470 } 471 472 XATerminator xat = null; 474 try { 475 xat = ts.getCurrent().getXATerminator(); 476 } catch (Exception ex) { 477 logger.log(BasicLevel.ERROR, "Unable to get an XATerminator from the TransactionService"); 478 throw new ServiceException("Unable to get an XATerminator from the TransactionService", ex); 479 } 480 bootCtx = new ResourceBootstrapContext(workMgr, xat); 481 482 if (logger.isLoggable(BasicLevel.DEBUG)) { 483 logger.log(BasicLevel.DEBUG, "ResourceService initialized"); 484 } 485 } 486 487 491 public void doStart() throws ServiceException { 492 String rarFileName = null; 494 CompNamingContext ctx = null; 495 for (int i = 0; i < resourceNames.size(); i++) { 496 rarFileName = (String ) resourceNames.elementAt(i); 497 if (logger.isLoggable(BasicLevel.DEBUG)) { 498 logger.log(BasicLevel.DEBUG, "rarFileName=" + rarFileName); 499 } 500 try { 501 ctx = new CompNamingContext(rarFileName); 502 ctx.rebind("rarFileName", rarFileName); 503 ctx.rebind("isInEar", new Boolean (false)); 504 ctx.rebind("classloader", appsClassLoader); 505 createResourceAdapter(ctx); 506 } catch (Exception e) { 507 logger.log(BasicLevel.ERROR, "JOnAS: Cannot create resource: " + rarFileName + " exception: " + e); 508 e.printStackTrace(); 509 } 510 } 511 512 if (!delayedRAs.isEmpty()) { 514 try { 515 processingDelayed = true; 516 Object [] rList = null; 517 rList = delayedRAs.toArray(); 518 for (int i = 0; i < rList.length; i++) { 519 try { 520 createResourceAdapter((CompNamingContext) rList[i]); 521 } catch (Exception e) { 522 e.printStackTrace(); 523 String rFile = (String ) ((CompNamingContext) rList[i]).lookup("rarFileName"); 524 logger.log(BasicLevel.ERROR, "JOnAS: Cannot create resource: " + rFile + " exception: " + e); 525 logger.log(BasicLevel.ERROR, "Please verify that the rarlink is correct/deployed"); 526 } 527 } 528 } catch (Exception e) { 529 e.printStackTrace(); 530 logger.log(BasicLevel.ERROR, "ResourceService: Error with delayed RAR file deployment\n" + e); 531 throw new ServiceException("ResourceService: Error with delayed RAR file deployment", e); 532 } 533 } 534 535 if (mbeanServer != null) { 537 try { 538 mbeanServer.registerMBean(this, JonasObjectName.resourceService()); 539 } catch (InstanceAlreadyExistsException iae) { 540 logger.log(BasicLevel.ERROR, "Cannot start the Resource Service Already Exists:\n" + iae); 541 throw new ServiceException("Cannot start the Resource Service Already Exists", iae); 542 } catch (Exception e) { 543 logger.log(BasicLevel.ERROR, "ResourceService: Cannot start the Resource service:\n" + e); 544 throw new ServiceException("ResourceService: Cannot start the Resource service", e); 545 } 546 } 547 548 } 549 550 554 public void doStop() throws ServiceException { 555 556 ServiceException se = null; 557 synchronized (Rar.fileName2RA) { 558 Enumeration keys = Rar.fileName2RA.elements(); 559 while (keys.hasMoreElements()) { 560 Rar ra = (Rar) keys.nextElement(); 561 try { 562 ra.unRegister(); 563 Rar.fileName2RA.remove(ra); 564 } catch (Exception ex) { 565 logger.log(BasicLevel.ERROR, "ResourceService: Received the following:" + ex); 566 ex.printStackTrace(); 567 if (se == null) { 568 se = new ServiceException(ex.getMessage()); 569 } 570 } 571 } 572 } 573 if (mbeanServer != null) { 575 try { 576 mbeanServer.unregisterMBean(JonasObjectName.resourceService()); 578 } catch (MBeanRegistrationException mr) { 579 logger.log(BasicLevel.ERROR, "Cannot cleanly stop the ResourceService: " 580 + mr.getMessage()); 581 } catch (InstanceNotFoundException infe) { 582 logger.log(BasicLevel.ERROR, "Cannot cleanly stop the ResourceService: " 583 + infe.getMessage()); 584 } catch (Exception e) { 585 logger.log(BasicLevel.ERROR, "ResourceService: Cannot stop the Resource service:\n" + e); 586 throw new ServiceException("ResourceService: Cannot stop the Resource service", e); 587 } 588 } 589 590 if (se != null) { 591 throw se; 592 } 593 if (logger.isLoggable(BasicLevel.DEBUG)) { 594 logger.log(BasicLevel.DEBUG, "ResourceService stopped"); 595 } 596 } 597 598 599 601 608 public String createResourceAdapter(Context ctx) throws Exception { 609 610 String rarFileName; 613 try { 614 rarFileName = (String ) ctx.lookup("rarFileName"); 615 ctx.rebind("deployed", new Boolean (false)); 616 } catch (Exception ex) { 617 String err = "Error while getting parameter from context param."; 618 logger.log(BasicLevel.ERROR, err + ex.getMessage()); 619 throw new ResourceServiceException(err, ex); 620 } 621 622 if (logger.isLoggable(BasicLevel.DEBUG)) { 623 logger.log(BasicLevel.DEBUG, rarFileName); 624 } 625 if (!rarFileName.endsWith(".rar")) { 626 rarFileName += ".rar"; 627 ctx.rebind("rarFileName", rarFileName); 628 } 629 630 File f = new File (rarFileName); 632 if (!f.exists()) { 633 boolean found = false; 634 String resFileName = null; 635 resFileName = RARSDIR + File.separator + rarFileName; 638 f = new File (resFileName); 639 found = f.exists(); 640 if (found) { 641 rarFileName = resFileName; 642 ctx.rebind("rarFileName", rarFileName); 643 } else { 644 logger.log(BasicLevel.ERROR, "createResourceAdapter: " + resFileName + " not found"); 645 Exception e = new NamingException (resFileName + " not found"); 646 throw e; 647 } 648 } 649 650 URL rarUrl = f.toURL(); 651 652 Rar rar = new Rar(ctx, getDomainName(), getJonasServerName(), workMgr, bootCtx); 653 654 try { 655 Context ctxRar = rar.processRar(); 656 } catch (Exception ex) { 657 String err = "Error processing Rar: " + ex.getMessage(); 659 try { 660 rar.unRegister(); 661 } catch (Exception exc) { 662 err = err + " Unregister also failed with " + exc.getMessage(); 663 } 664 logger.log(BasicLevel.ERROR, err); 665 throw new ResourceServiceException(err, ex); 666 } 667 668 boolean isDeployed = false; 669 try { 670 isDeployed = ((Boolean ) ctx.lookup("deployed")).booleanValue(); 671 } catch (Exception ex) { 672 String err = "Error while getting parameter(isDeployed) from context param."; 673 logger.log(BasicLevel.ERROR, err + ex.getMessage()); 674 throw new ResourceServiceException(err, ex); 675 } 676 677 if (!isDeployed) { 678 if (processingDelayed) { 682 logger.log(BasicLevel.ERROR, "ResourceService.createRA: Resource (" + rarFileName + ") contains an invalid rarlink."); 683 throw new ResourceServiceException("resource input file incorrect: invalid rarlink"); 684 } 685 delayedRAs.add(ctx); 686 return null; 687 } 688 689 Vector jNames = rar.getJndinames(); 690 if (jNames != null) { 691 for (int i = 0; i < jNames.size(); i++) { 692 Rar.jndiName2RA.put(jNames.get(i), rar); 693 } 694 } 695 696 Rar.fileName2RA.put(rarUrl.getPath(), rar); 698 699 String onRar = null; 700 try { 701 onRar = (String ) ctx.lookup("onRar"); 702 } catch (Exception ex) { 703 String err = "Error while getting parameter(onRar) from context param."; 704 logger.log(BasicLevel.ERROR, err + ex.getMessage()); 705 throw new ResourceServiceException(err, ex); 706 } 707 708 return onRar.toString(); 709 } 710 711 712 726 public void deployRars(Context ctx) throws ResourceServiceException { 727 728 URL [] urls = null; 734 URL earUrl = null; 735 ClassLoader earClassLoader = null; 736 URL [] altDDs = null; 737 try { 738 urls = (URL []) ctx.lookup("urls"); 739 earUrl = (URL ) ctx.lookup("earUrl"); 740 earClassLoader = (ClassLoader ) ctx.lookup("earClassLoader"); 741 altDDs = (URL []) ctx.lookup("altDDs"); 742 } catch (NamingException e) { 743 String err = "Error while getting parameter from context param "; 744 logger.log(BasicLevel.ERROR, err + e.getMessage()); 745 throw new ResourceServiceException(err, e); 746 } 747 748 delayedRAs.clear(); for (int i = 0; i < urls.length; i++) { 751 String fileName = urls[i].getFile(); 753 if (logger.isLoggable(BasicLevel.DEBUG)) { 754 logger.log(BasicLevel.DEBUG, "Deploy rar '" + fileName + "' for the ear service"); 755 } 756 757 Context contctx = null; 760 try { 761 contctx = new CompNamingContext(fileName); 762 contctx.rebind("rarFileName", fileName); 763 contctx.rebind("isInEar", new Boolean (true)); 764 contctx.rebind("earUrl", earUrl); 765 if (altDDs[i] != null) { 766 contctx.rebind("altDD", altDDs[i]); 767 } 768 contctx.rebind("classloader", earClassLoader); 769 createResourceAdapter(contctx); 770 } catch (Exception e) { 771 logger.log(BasicLevel.ERROR, "Error when deploying '" + fileName + "'"); 774 logger.log(BasicLevel.ERROR, e.getMessage()); 775 logger.log(BasicLevel.ERROR, "Undeploy rar of the ear application"); 776 777 for (int j = 0; j < i; j++) { 778 String rarFileName = urls[j].getFile(); 779 try { 780 CompNamingContext compctx = new CompNamingContext(rarFileName); 782 compctx.rebind("rarFileName", rarFileName); 783 compctx.rebind("isInEar", new Boolean (true)); 784 contctx.rebind("earUrl", earUrl); 785 unRegisterRar(compctx); 786 } catch (Exception ex) { 787 logger.log(BasicLevel.ERROR, "Error when undeploying '" + rarFileName + "'"); 790 logger.log(BasicLevel.ERROR, ex.getMessage()); 791 logger.log(BasicLevel.ERROR, "Cannot undeploy rar of the ear application"); 792 } 793 794 } 795 throw new ResourceServiceException("Error during the deployment", e); 796 } 797 } 798 if (!delayedRAs.isEmpty()) { 800 try { 801 processingDelayed = true; 802 Object [] rList = null; 803 rList = delayedRAs.toArray(); 804 for (int i = 0; i < rList.length; i++) { 805 try { 806 createResourceAdapter((CompNamingContext) rList[i]); 807 } catch (Exception e) { 808 logger.log(BasicLevel.ERROR, "JOnAS: Cannot create resource: " + (String ) rList[i] + " exception: " + e); 809 logger.log(BasicLevel.ERROR, "Please verify that the rarlink is correct"); 810 e.printStackTrace(); 811 } 812 } 813 } catch (Exception e) { 814 logger.log(BasicLevel.ERROR, "ResourceService: Error with delayed RAR file deployment\n" + e); 815 throw new ServiceException("ResourceService: Error with delayed RAR file deployment", e); 816 } 817 } 818 } 819 820 821 827 public void unDeployRars(URL [] urls, URL earUrl) { 828 for (int i = 0; i < urls.length; i++) { 829 String fileName = urls[i].getFile(); 830 if (Rar.fileName2RA.containsKey(fileName)) { 831 try { 832 CompNamingContext compctx = new CompNamingContext(fileName); 834 compctx.rebind("rarFileName", fileName); 835 compctx.rebind("isInEar", new Boolean (true)); 836 compctx.rebind("earUrl", earUrl); 837 unRegisterRar(compctx); 838 } catch (Exception ex) { 839 logger.log(BasicLevel.ERROR, "Cannot undeploy resource: " + fileName + " " + ex); 840 } 841 } else { 842 logger.log(BasicLevel.ERROR, "Cannot remove the non-existant rar '" + fileName + "'"); 843 } 844 } 845 } 846 847 848 853 public void unRegisterRar(Context ctx) throws Exception { 854 String rarFileName; 855 try { 856 rarFileName = (String ) ctx.lookup("rarFileName"); 857 } catch (NamingException e) { 858 String err = "Error while getting parameter from context param."; 859 logger.log(BasicLevel.ERROR, err + e.getMessage()); 860 throw new ResourceServiceException(err, e); 861 } catch (Exception ex) { 862 String err = "Error while getting parameter from context param."; 863 logger.log(BasicLevel.ERROR, err + ex.getMessage()); 864 throw new ResourceServiceException(err, ex); 865 } 866 867 if (manageLogger.isLoggable(BasicLevel.DEBUG)) { 868 manageLogger.log(BasicLevel.DEBUG, "TEST Unregister MBeans for RAR in file: " + rarFileName); 869 } 870 871 String rarPath = (new File (rarFileName)).toURL().getPath(); 872 Rar rar = (Rar) Rar.fileName2RA.get(rarPath); 873 Vector jNames = rar.getJndinames(); 874 rar.unRegister(); 875 876 for (int i = 0; i < jNames.size(); i++) { 878 Rar.jndiName2RA.remove(jNames.get(i)); 879 } 880 Rar.fileName2RA.remove(rarPath); 881 resourceNames.remove(normalizePath(rarFileName)); 882 883 logger.log(BasicLevel.INFO, 884 "ResourceService: unRegisterRar: " + rarFileName); 885 } 886 887 891 894 public Integer getCurrentNumberOfResource() { 895 return getCurrentNumberOfRars(); 896 } 897 898 899 902 public Integer getCurrentNumberOfRars() { 903 return new Integer (Rar.fileName2RA.size()); 904 } 905 906 907 910 public List getDeployedRars() { 911 ArrayList al = new ArrayList (); 912 String rarFileName = null; 913 Enumeration keys = Rar.fileName2RA.keys(); 914 while (keys.hasMoreElements()) { 915 rarFileName = keys.nextElement().toString(); 916 try { 917 al.add((new File (rarFileName)).toURL().getPath()); 918 } catch (Exception e) { 919 if (logger.isLoggable(BasicLevel.DEBUG)) { 920 logger.log(BasicLevel.DEBUG, "Unable to add rarfile " + rarFileName + " to arraylist"); 921 } 922 } 923 } 924 return al; 925 } 926 927 934 public List getInstalledRars() throws Exception { 935 ArrayList al = JModule.getInstalledContainersInDir(RARSDIR, JModule.RAR_EXTENSION 937 , JModule.RAR_CHILD_DIR, JModule.RAR_CONFIRM_FILE); 938 for (int i = 0; i < autoNames.size(); i++) { 940 al.addAll(JModule.getInstalledContainersInDir(autoNames.get(i).toString(), 941 JModule.RAR_EXTENSION, JModule.RAR_CHILD_DIR, JModule.RAR_CONFIRM_FILE)); 942 } 943 return al; 944 } 945 946 951 public Set getRarNames() { 952 HashSet names = new HashSet (); 953 String rarFileName = null; 954 for (int i = 0; i < resourceNames.size(); i++) { 955 rarFileName = (String ) resourceNames.elementAt(i); 956 names.add(rarFileName); 957 } 958 return names; 959 } 960 961 962 970 public String deployRarMBean(String fileName) throws RemoteException , ResourceServiceException { 971 Context ctx = null; 972 String onRar = null; 973 try { 974 ctx = new CompNamingContext(fileName); 975 ctx.rebind("rarFileName", fileName); 976 ctx.rebind("isInEar", new Boolean (false)); 977 ctx.rebind("classloader", appsClassLoader); 978 onRar = createResourceAdapter(ctx); 979 } catch (Exception e) { 980 String err = "Error when deploying the rar file: " + fileName; 981 logger.log(BasicLevel.ERROR, err + e.getMessage()); 982 e.printStackTrace(); 983 throw new ResourceServiceException(err, e); 984 } 985 986 return onRar; 987 } 988 989 995 public String deployRar(String fileName) throws Exception { 996 return deployRarMBean(fileName); 997 } 998 999 1004 public Boolean isRarDeployed(String fileName) { 1005 return new Boolean (isRarLoaded(fileName)); 1006 } 1007 1008 1014 public boolean isRarDeployedByUnpackName(String unpackName) { 1015 if (logger.isLoggable(BasicLevel.DEBUG)) { 1016 logger.log(BasicLevel.DEBUG, "entering for unpackName= " + unpackName); 1017 } 1018 1034 return false; 1035 } 1036 1037 1044 public void unDeployRarMBean(String fileName) throws RemoteException , ResourceServiceException { 1045 1048 1049 boolean found = true; 1050 if (logger.isLoggable(BasicLevel.DEBUG)) { 1051 logger.log(BasicLevel.DEBUG, "Trying to undeploy: " + fileName 1052 + " with the following deployed:" + Rar.fileName2RA); 1053 } 1054 1055 try { 1056 File f = new File (fileName); 1058 if (!Rar.fileName2RA.containsKey(f.toURL().getPath())) { 1059 found = false; 1060 String resFileName = RARSDIR + File.separator + fileName; 1062 f = new File (resFileName); 1063 if (Rar.fileName2RA.containsKey(f.toURL().getPath())) { 1064 fileName = resFileName; 1065 found = true; 1066 } 1067 } 1068 1069 if (!found) { 1070 String err = "Cannot undeploy the rar '" + fileName + "', it is not deployed."; 1071 logger.log(BasicLevel.ERROR, err); 1072 throw new ResourceServiceException(err); 1073 } 1074 } catch (Exception ex) { 1075 String err = "Error trying to undeployRarMBean " + fileName; 1076 logger.log(BasicLevel.ERROR, err + ex.getMessage()); 1077 throw new ResourceServiceException(err, ex); 1078 } 1079 1080 CompNamingContext compctx = null; 1082 try { 1083 compctx = new CompNamingContext(fileName); 1084 compctx.rebind("rarFileName", fileName); 1085 compctx.rebind("isInEar", new Boolean (false)); 1086 unRegisterRar(compctx); 1088 } catch (NamingException e) { 1089 String err = "Error when binding parameters"; 1090 logger.log(BasicLevel.ERROR, err + e.getMessage()); 1091 throw new ResourceServiceException(err, e); 1092 } catch (Exception ex) { 1093 String err = "Error when unRegistering rar " + fileName; 1094 logger.log(BasicLevel.ERROR, err + ex.getMessage()); 1095 ex.printStackTrace(); 1096 throw new ResourceServiceException(err, ex); 1097 } 1098 1099 } 1100 1101 1106 public void unDeployRar(String fileName) throws Exception { 1107 unDeployRarMBean(fileName); 1108 } 1109 1110 1115 public boolean isRarLoaded(String fileName) { 1116 1117 String updateName = null; 1118 boolean isLoaded = false; 1119 try { 1120 if (Rar.fileName2RA.containsKey(fileName)) { 1121 isLoaded = true; 1122 } 1123 if (!isLoaded) { 1125 updateName = RARSDIR + File.separator + fileName; 1127 1128 if (Rar.fileName2RA.containsKey(updateName)) { 1130 isLoaded = true; 1131 } 1132 } 1133 } catch (Exception e) { 1134 String err = "Cannot determine if the rar is deployed or not"; 1135 logger.log(BasicLevel.ERROR, err); 1136 return false; 1137 } 1138 1139 return isLoaded; 1140 } 1141 1142 1143 1149 public List getDeployableRars() throws Exception { 1150 List al = getInstalledRars(); 1151 al.removeAll(getDeployedRars()); 1152 return al; 1153 } 1154 1155 1159 public List getAutoloadDirectories() { 1160 ArrayList al = new ArrayList (); 1161 for (int i = 0; i < autoNames.size(); i++) { 1162 try { 1163 al.add((new File (autoNames.get(i).toString())).toURL().getPath()); 1164 } catch (Exception e) { 1165 if (logger.isLoggable(BasicLevel.DEBUG)) { 1166 logger.log(BasicLevel.DEBUG, "Can't get autoload directories : " + e.getMessage()); 1167 } 1168 } 1169 } 1170 return al; 1171 } 1172 1173 1177 public String getRarsDirectory() { 1178 String sRet = null; 1179 try { 1180 sRet = (new File (RARSDIR)).toURL().getPath(); 1181 } catch (Exception e) { 1182 throw new RuntimeException ("Cannot get the RARS directory", e); 1183 } 1184 return sRet; 1185 } 1186 1187 1191 1199 private void addRars(String dirPath) { 1200 boolean found = false; 1201 1202 File dir = new File (RARSDIR + File.separator + dirPath); 1204 found = dir.isDirectory(); 1205 1206 if (found) { 1207 autoNames.add(dir); 1208 addRarsFrom(dir); 1209 } else { 1210 String err = "Warning: Cannot load dir: '" + dirPath + "' "; 1211 err += "is not a directory or directory doesn't exist"; 1212 logger.log(BasicLevel.WARN, err); 1213 } 1214 } 1215 1216 1221 private void addRarsFrom(File dir) throws ResourceServiceException { 1222 try { 1223 if (dir.isDirectory()) { 1224 File [] files = dir.listFiles(); 1225 for (int i = 0; i < files.length; i++) { 1226 if (files[i].isFile()) { 1227 if (files[i].getPath().toLowerCase().endsWith(".rar")) { 1228 resourceNames.add(files[i].getCanonicalPath()); 1229 } 1230 } else { 1231 autoNames.add(files[i]); 1232 addRarsFrom(files[i]); 1233 } 1234 } 1235 } else { 1236 String err = "Cannot load dir: '" + dir.getPath(); 1237 err += "' is not a directory"; 1238 logger.log(BasicLevel.ERROR, err); 1239 throw new ResourceServiceException(err); 1240 } 1241 } catch (IOException e) { 1242 String err = "Invalid file name '" + dir.getPath(); 1243 logger.log(BasicLevel.ERROR, err); 1244 throw new ResourceServiceException(err, e); 1245 } 1246 } 1247 1248 1253 private String normalizePath(String path) { 1254 String ret = null; 1255 if (File.separatorChar == '/') { 1256 ret = path.replace('\\', File.separatorChar); 1257 } else { 1258 ret = path.replace('/', File.separatorChar); 1259 } 1260 return ret; 1261 } 1262} 1263 1264 1265 | Popular Tags |