1 2 24 package com.sun.enterprise.repository; 25 26 import java.util.*; 27 import java.io.*; 28 import com.sun.enterprise.ServerConfiguration; 29 import com.sun.enterprise.util.FileUtil; 30 import javax.rmi.PortableRemoteObject ; 31 import javax.naming.*; 32 33 import com.sun.enterprise.server.ServerContext; 34 import com.sun.enterprise.server.ApplicationServer; 35 36 import com.sun.enterprise.config.ConfigContext; 37 import com.sun.enterprise.config.ConfigFactory; 38 import com.sun.enterprise.config.ConfigException; 39 import com.sun.enterprise.config.serverbeans.Resources; 40 import com.sun.enterprise.config.ConfigBean; 41 import com.sun.enterprise.config.serverbeans.Domain; 42 import com.sun.enterprise.config.serverbeans.Servers; 43 import com.sun.enterprise.config.serverbeans.Server; 44 import com.sun.enterprise.config.serverbeans.ResourceRef; 45 import com.sun.enterprise.config.serverbeans.ServerHelper; 46 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 47 import com.sun.enterprise.config.serverbeans.ElementProperty; 48 import java.util.logging.*; 50 import com.sun.logging.*; 51 53 59 public class IASJ2EEResourceFactoryImpl implements J2EEResourceFactory { 60 61 private static Logger _logger=null; 63 static{ 64 _logger=LogDomains.getLogger(LogDomains.ROOT_LOGGER); 65 } 66 static final String JMS_QUEUE = "javax.jms.Queue"; 68 static final String JMS_TOPIC = "javax.jms.Topic"; 69 static final String JMS_QUEUE_CONNECTION_FACTORY = "javax.jms.QueueConnectionFactory"; 70 static final String JMS_TOPIC_CONNECTION_FACTORY = "javax.jms.TopicConnectionFactory"; 71 72 74 public static final String CUSTOM_RES_TYPE = "custom-resource"; 75 76 77 public static final String EXT_JNDI_RES_TYPE = "external-jndi-resource"; 78 79 80 public static final String JDBC_RES_TYPE = "jdbc-resource"; 81 82 83 public static final String MAIL_RES_TYPE = "mail-resource"; 84 85 86 public static final String JMS_RES_TYPE = "jms-resource"; 87 88 89 public static final String PMF_RES_TYPE="persistence-manager-factory-resource"; 90 91 public static final String JDBC_CONN_POOL_RES_TYPE = "jdbc-connection-pool"; 92 93 public static final String CONNECTOR_CONN_POOL_TYPE = "connector-connection-pool"; 94 96 97 private ConfigContext ctx = null; 98 private Resources rBean = null; 99 100 private String serverName = null; 101 private static J2EEResourceCollection resources = null; 102 103 public J2EEResourceCollection loadDefaultResourceCollection() 104 throws J2EEResourceException { 105 if (resources != null) { 106 return resources; 107 } else { 108 return loadResourceCollection(null); 109 } 110 } 111 112 public J2EEResourceCollection loadResourceCollection(String resourcesURL) 113 throws J2EEResourceException { 114 115 resources = new J2EEResourceCollectionImpl(); 116 117 ServerContext sc = ApplicationServer.getServerContext(); 118 if(sc == null) { 119 _logger.log(Level.FINE,"Not Running in server. Returning empty resources..."); 122 return resources; 124 } else { 125 serverName = sc.getInstanceName(); 127 } 128 129 try { 130 ctx = sc.getConfigContext(); 131 rBean = ServerBeansFactory.getDomainBean(ctx).getResources(); 134 if (rBean == null) 136 { 137 rBean = new Resources(); 138 } 139 } catch (ConfigException ce) { 140 145 _logger.log(Level.SEVERE,"enterprise.empty_resource",ce); 147 _logger.log(Level.FINE,"Error initializing context. Returning empty resources..."); 148 return resources; 150 } 151 152 try { 153 172 _logger.log(Level.FINE,"Loading Jdbc Connection Pool=========="); 174 loadJdbcConnectionPool(resources); 176 _logger.log(Level.FINE,"Loading Jdbc Resource=========="); 177 loadJdbcResource(resources); 178 _logger.log(Level.FINE,"Loading PMF Resource=========="); 181 loadPMFResource(resources); 182 _logger.log(Level.FINE,"Loading Custom Resource=========="); 183 loadCustomResource(resources); 184 _logger.log(Level.FINE,"Loading External Jndi Resource=========="); 185 loadExternalJndiResource(resources); 186 _logger.log(Level.FINE,"Loading Mail Resource=========="); 188 loadMailResource(resources); 189 } catch (ConfigException ce) { 192 _logger.log(Level.SEVERE,"enterprise.config_exception",ce); 196 _logger.log(Level.FINE,"Error Loading resources: "+ce.getMessage()); 197 throw new J2EEResourceException(ce); 199 } 200 return resources; 201 202 } 203 204 public void storeDefaultResourceCollection(J2EEResourceCollection resources) 205 throws J2EEResourceException { 206 storeResourceCollection(resources, null); 207 } 208 209 public void storeResourceCollection(J2EEResourceCollection resources, String resourcesURL) 210 throws J2EEResourceException { 211 273 } 274 275 public J2EEResource createResource(int type, String name) { 276 277 J2EEResource resource = null; 278 279 switch(type) { 280 case J2EEResource.JMS_DESTINATION : 281 resource = new JmsDestinationResource(name); 282 break; 283 case J2EEResource.JMS_CNX_FACTORY : 284 resource = new JmsCnxFactoryResource(name); 285 break; 286 case J2EEResource.JDBC_RESOURCE : 287 resource = new JdbcResource(name); 288 break; 289 case J2EEResource.JDBC_XA_RESOURCE : 290 resource = new JdbcXAResource(name); 291 break; 292 case J2EEResource.JDBC_DRIVER : 293 resource = new JdbcDriver(name); 294 break; 295 case J2EEResource.JDBC_CONNECTION_POOL : 296 resource = new JdbcConnectionPool(name); 297 break; 298 case J2EEResource.PMF_RESOURCE : 299 resource = new PMFResource(name); 300 break; 301 case J2EEResource.CUSTOM_RESOURCE : 302 resource = new CustomResource(name); 303 break; 304 case J2EEResource.EXTERNAL_JNDI_RESOURCE : 305 resource = new ExternalJndiResource(name); 306 break; 307 case J2EEResource.MAIL_RESOURCE : 309 resource = new MailResource(name); 310 break; 311 default : 313 throw new java.lang.IllegalArgumentException (); 314 } 315 316 return resource; 317 } 318 319 public ResourceProperty createProperty(String name) { 320 return new ResourcePropertyImpl(name); 321 } 322 323 332 private boolean isJdbcPoolReferenced(String poolName) 333 throws ConfigException { 334 335 com.sun.enterprise.config.serverbeans.JdbcResource[] jdbcBeans = 336 rBean.getJdbcResource(); 337 338 if (jdbcBeans == null) { 341 return false; 342 } 343 344 for (int i = 0; i <jdbcBeans.length; i++) { 345 346 com.sun.enterprise.config.serverbeans.JdbcResource next = 347 (com.sun.enterprise.config.serverbeans.JdbcResource) jdbcBeans[i]; 348 349 if ( !isReferenced(next.getJndiName()) ) { 351 continue; 352 } else { 353 String pool = next.getPoolName(); 354 if ( (pool != null) && pool.equals(poolName) ) { 355 return true; 357 } 358 } 359 } 360 361 return false; 363 } 364 365 373 private boolean isReferenced(String resourceName) throws ConfigException { 374 return ServerHelper.serverReferencesResource(ctx, serverName, resourceName); 375 } 376 377 private void loadJdbcResource(J2EEResourceCollection resources) throws ConfigException { 378 com.sun.enterprise.config.serverbeans.JdbcResource[] jBeanSet = rBean.getJdbcResource(); 379 if(jBeanSet == null) return; 380 381 for(int i = 0; i <jBeanSet.length; i++) { 382 com.sun.enterprise.config.serverbeans.JdbcResource next = 383 (com.sun.enterprise.config.serverbeans.JdbcResource) jBeanSet[i]; 384 385 if ( !isReferenced(next.getJndiName()) ) { 387 _logger.fine("Skipping Jdbc Resource: " + next.getJndiName() 388 + ". It is not used by server: " + serverName); 389 continue; 390 } 391 392 if (!next.isEnabled()) { 394 logMessageIfNotEnabled(next.getJndiName(), JDBC_RES_TYPE); 395 continue; 396 } 397 399 String poolName = next.getPoolName(); 400 JdbcConnectionPool jcp = (JdbcConnectionPool)resources.getResourceByName( 401 J2EEResource.JDBC_CONNECTION_POOL, poolName); 402 403 if(jcp == null) { 405 _logger.log(Level.WARNING, "enterprise.connection_pool_does_not_exist", poolName); 407 _logger.log(Level.WARNING, "enterprise.error_loading_jdbc_resource", next.getJndiName()); 408 _logger.info("Error loading Jdbc Resource:" + next.getJndiName() + ". skipping..."); 409 continue; 410 } 411 413 boolean isXA = jcp.isXA(); 414 J2EEResourceBase jr = null; 415 if(isXA) { 416 jr = new IASJdbcXAResource(next.getJndiName()); 417 ((IASJdbcXAResource)jr).setJdbcConnectionPool(jcp); 418 } else { 419 jr = new IASJdbcResource(next.getJndiName()); 420 ((IASJdbcResource)jr).setJdbcConnectionPool(jcp); 421 } 422 jr.setEnabled(next.isEnabled()); 424 resources.addResource(jr); 425 } 426 } 427 428 493 494 499 private void loadJdbcConnectionPool(J2EEResourceCollection resources) throws ConfigException{ 500 com.sun.enterprise.config.serverbeans.JdbcConnectionPool[] jBeanSet = rBean.getJdbcConnectionPool(); 501 if(jBeanSet == null) return; 502 503 for(int i = 0; i <jBeanSet.length; i++) { 504 com.sun.enterprise.config.serverbeans.JdbcConnectionPool next = 505 (com.sun.enterprise.config.serverbeans.JdbcConnectionPool) jBeanSet[i]; 506 if ( !isJdbcPoolReferenced(next.getName()) ) { 508 _logger.fine("Skipping Jdbc Pool:" + next.getName() 509 + ". It is not used by server: " + serverName); 510 continue; 511 } 512 513 JdbcConnectionPool jr = loadJdbcConnectionPool(next); 514 resources.addResource(jr); 515 } 516 } 517 518 519 public static JdbcConnectionPool loadJdbcConnectionPool(com.sun.enterprise.config.serverbeans.JdbcConnectionPool jcp) throws ConfigException{ 520 JdbcConnectionPool jr = new JdbcConnectionPool(jcp.getName()); 521 jr.setDatasourceClassname(jcp.getDatasourceClassname()); 525 jr.setMaxPoolSize(jcp.getMaxPoolSize()); 527 jr.setSteadyPoolSize(jcp.getSteadyPoolSize()); 528 jr.setMaxWaitTimeInMillis(jcp.getMaxWaitTimeInMillis()); 529 jr.setPoolResizeQuantity(jcp.getPoolResizeQuantity()); 531 jr.setIdleTimeoutInSeconds(jcp.getIdleTimeoutInSeconds()); 533 jr.setIsConnectionValidationRequired(jcp.isIsConnectionValidationRequired()); 534 jr.setConnectionValidationMethod(jcp.getConnectionValidationMethod()); 535 jr.setValidationTableName(jcp.getValidationTableName()); 536 jr.setFailAllConnections(jcp.isFailAllConnections()); 537 538 jr.setResType(jcp.getResType()); 540 jr.setTransactionIsolationLevel(jcp.getTransactionIsolationLevel()); 541 jr.setIsIsolationLevelGuaranteed(jcp.isIsIsolationLevelGuaranteed()); 542 544 545 ElementProperty[] s = jcp.getElementProperty(); 546 if(s!= null) { 547 for(int j = 0; j <s.length; j++) { 548 com.sun.enterprise.config.serverbeans.ElementProperty ep = 549 (com.sun.enterprise.config.serverbeans.ElementProperty) s[j]; 550 ResourceProperty rp = new ResourcePropertyImpl(ep.getName(), 551 ep.getValue()); 552 jr.addProperty(rp); 553 } 554 } 555 return jr; 556 } 557 558 560 561 567 private void loadCustomResource(J2EEResourceCollection resources) 568 throws ConfigException { 569 570 com.sun.enterprise.config.serverbeans.CustomResource[] jBeanSet = 571 rBean.getCustomResource(); 572 573 if (jBeanSet == null) { 574 return; 575 } 576 577 for (int i=0; i < jBeanSet.length; i++) { 578 com.sun.enterprise.config.serverbeans.CustomResource next = 579 (com.sun.enterprise.config.serverbeans.CustomResource) jBeanSet[i]; 580 581 if ( !isReferenced(next.getJndiName()) ) { 583 _logger.fine("Skipping Custom Resource:" + next.getJndiName() 584 + ". It is not used by server: " + serverName); 585 continue; 586 } 587 588 if (!next.isEnabled()) { 590 logMessageIfNotEnabled(next.getJndiName(), CUSTOM_RES_TYPE); 591 continue; 592 } 593 595 J2EEResource jr = toCustomJ2EEResource(next); 597 598 resources.addResource(jr); 599 } 600 } 601 602 615 public static J2EEResource toCustomJ2EEResource( 616 com.sun.enterprise.config.serverbeans.CustomResource rbean) 617 throws ConfigException { 618 619 CustomResource jr = new CustomResource( rbean.getJndiName() ); 620 621 623 jr.setEnabled( rbean.isEnabled() ); 625 626 jr.setResType( rbean.getResType() ); 628 629 jr.setFactoryClass( rbean.getFactoryClass() ); 631 632 ElementProperty[] s = rbean.getElementProperty(); 634 if (s!= null) { 635 for(int j = 0; j <s.length; j++) { 636 637 com.sun.enterprise.config.serverbeans.ElementProperty next = 638 (com.sun.enterprise.config.serverbeans.ElementProperty) s[j]; 639 640 ResourceProperty rp = 641 new ResourcePropertyImpl(next.getName(), next.getValue()); 642 643 jr.addProperty(rp); 644 } 645 } 646 647 return jr; 648 } 649 650 664 public static J2EEResource toExternalJndiJ2EEResource( 665 com.sun.enterprise.config.serverbeans.ExternalJndiResource rbean) 666 throws ConfigException { 667 668 ExternalJndiResource jr = new ExternalJndiResource(rbean.getJndiName()); 669 670 672 jr.setEnabled( rbean.isEnabled() ); 674 675 jr.setJndiLookupName( rbean.getJndiLookupName() ); 677 678 jr.setResType( rbean.getResType() ); 680 681 jr.setFactoryClass( rbean.getFactoryClass() ); 683 684 ElementProperty[] s = rbean.getElementProperty(); 686 if (s!= null) { 687 for (int j=0; j<s.length; j++) { 688 com.sun.enterprise.config.serverbeans.ElementProperty next = 689 (com.sun.enterprise.config.serverbeans.ElementProperty) s[j]; 690 691 ResourceProperty rp = 692 new ResourcePropertyImpl(next.getName(), next.getValue()); 693 jr.addProperty(rp); 694 } 695 } 696 697 return jr; 698 } 699 700 708 private void loadExternalJndiResource(J2EEResourceCollection resources) 709 throws ConfigException { 710 711 com.sun.enterprise.config.serverbeans.ExternalJndiResource[] jBeanSet = 712 rBean.getExternalJndiResource(); 713 714 if (jBeanSet == null) { 715 return; 716 } 717 718 for (int i=0; i<jBeanSet.length; i++) { 719 com.sun.enterprise.config.serverbeans.ExternalJndiResource next = 720 (com.sun.enterprise.config.serverbeans.ExternalJndiResource) jBeanSet[i]; 721 722 if ( !isReferenced(next.getJndiName()) ) { 724 _logger.fine("Skipping External JNDI Resource:" 725 + next.getJndiName() 726 + ". It is not used by server: " + serverName); 727 continue; 728 } 729 730 if (!next.isEnabled()) { 732 logMessageIfNotEnabled(next.getJndiName(), EXT_JNDI_RES_TYPE); 733 continue; 734 } 735 737 J2EEResource jr = toExternalJndiJ2EEResource(next); 739 740 resources.addResource(jr); 741 } 742 } 743 744 private void loadPMFResource(J2EEResourceCollection resources) throws ConfigException { 745 com.sun.enterprise.config.serverbeans.PersistenceManagerFactoryResource[] jBeanSet = rBean.getPersistenceManagerFactoryResource(); 746 if(jBeanSet == null) return; 747 748 for(int i = 0; i <jBeanSet.length; i++) { 749 com.sun.enterprise.config.serverbeans.PersistenceManagerFactoryResource next = 750 (com.sun.enterprise.config.serverbeans.PersistenceManagerFactoryResource) jBeanSet[i]; 751 752 if ( !isReferenced(next.getJndiName()) ) { 754 _logger.fine("Skipping PMF Resource:" + next.getJndiName() 755 + ". It is not used by server: " + serverName); 756 continue; 757 } 758 759 if (!next.isEnabled()) { 761 logMessageIfNotEnabled(next.getJndiName(), PMF_RES_TYPE); 762 continue; 763 } 764 766 resources.addResource(toPMFJ2EEResource(next)); 767 } 768 } 769 770 783 public static J2EEResource toPMFJ2EEResource( 784 com.sun.enterprise.config.serverbeans.PersistenceManagerFactoryResource 785 rbean) throws ConfigException { 786 PMFResource jr = new PMFResource(rbean.getJndiName()); 787 jr.setEnabled(rbean.isEnabled()); 788 jr.setFactoryClass(rbean.getFactoryClass()); 789 jr.setJdbcResourceJndiName(rbean.getJdbcResourceJndiName()); 790 791 ElementProperty[] s = rbean.getElementProperty(); 792 if (s!= null) { 793 for (int j = 0; j <s.length; j++) { 794 com.sun.enterprise.config.serverbeans.ElementProperty next1 = 795 (com.sun.enterprise.config.serverbeans.ElementProperty) s[j]; 796 ResourceProperty rp = new ResourcePropertyImpl(next1.getName(), 797 next1.getValue()); 798 jr.addProperty(rp); 799 } 800 } 801 803 return jr; 804 } 805 806 819 public static J2EEResource toMailJ2EEResource( 820 com.sun.enterprise.config.serverbeans.MailResource rbean) 821 throws ConfigException { 822 823 MailResource jr = new MailResource(rbean.getJndiName()); 824 825 jr.setEnabled(rbean.isEnabled()); 827 jr.setStoreProtocol(rbean.getStoreProtocol()); 828 jr.setStoreProtocolClass(rbean.getStoreProtocolClass()); 829 jr.setTransportProtocol(rbean.getTransportProtocol()); 830 jr.setTransportProtocolClass(rbean.getTransportProtocolClass()); 831 jr.setMailHost(rbean.getHost()); 832 jr.setUsername(rbean.getUser()); 833 jr.setMailFrom(rbean.getFrom()); 834 jr.setDebug(rbean.isDebug()); 835 836 ElementProperty[] s = rbean.getElementProperty(); 838 if (s != null) { 839 for (int j = 0; j < s.length; j++) { 840 com.sun.enterprise.config.serverbeans.ElementProperty next = 841 (com.sun.enterprise.config.serverbeans.ElementProperty)s[j]; 842 843 ResourceProperty rp = 844 new ResourcePropertyImpl(next.getName(), next.getValue()); 845 jr.addProperty(rp); 846 } 847 } 848 849 return jr; 850 } 851 852 859 private void loadMailResource(J2EEResourceCollection resources) 860 throws ConfigException { 861 862 com.sun.enterprise.config.serverbeans.MailResource[] jBeanSet = 863 rBean.getMailResource(); 864 865 if (jBeanSet == null) { 866 return; 867 } 868 869 for (int i = 0; i < jBeanSet.length; i++) { 870 com.sun.enterprise.config.serverbeans.MailResource next = 871 (com.sun.enterprise.config.serverbeans.MailResource)jBeanSet[i]; 872 873 if ( !isReferenced(next.getJndiName()) ) { 875 _logger.fine("Skipping Mail Resource:" + next.getJndiName() 876 + ". It is not used by server: " + serverName); 877 continue; 878 } 879 880 if (!next.isEnabled()) { 882 logMessageIfNotEnabled(next.getJndiName(), MAIL_RES_TYPE); 883 continue; 884 } 885 887 J2EEResource jr = toMailJ2EEResource(next); 889 890 resources.addResource(jr); 891 } 892 } 893 894 private void logMessageIfNotEnabled(String name, String type) { 896 _logger.log(Level.INFO, "enterprise.resource_disabled", new Object [] {name, type}); 897 } 898 900 901 903 1134} 1135 | Popular Tags |