FileDocCategorySizeDatePackage
IASJ2EEResourceFactoryImpl.javaAPI DocGlassfish v2 API24448Fri May 04 22:35:10 BST 2007com.sun.enterprise.repository

IASJ2EEResourceFactoryImpl

public class IASJ2EEResourceFactoryImpl extends Object implements J2EEResourceFactory
Implementation of J2EEResourceFactory. Reads and writes XML Resource properties and converts them to J2EEResources. IASRI #4626188

NOT THREAD SAFE: mutable instance variables

author
Sridatta Viswanath

Fields Summary
private static final Logger
_logger
static final String
JMS_QUEUE
static final String
JMS_TOPIC
static final String
JMS_QUEUE_CONNECTION_FACTORY
static final String
JMS_TOPIC_CONNECTION_FACTORY
public static final String
CUSTOM_RES_TYPE
generic resource type for jndi
public static final String
EXT_JNDI_RES_TYPE
resource type residing in an external JNDI repository
public static final String
JDBC_RES_TYPE
JDBC javax.sql.(XA)DataSource resource type
public static final String
MAIL_RES_TYPE
java mail resource type
public static final String
JMS_RES_TYPE
jms resource type
public static final String
PMF_RES_TYPE
persistence manager runtime configuration resource type
public static final String
JDBC_CONN_POOL_RES_TYPE
jdbc connection pool resource type
public static final String
CONNECTOR_CONN_POOL_TYPE
Connector resource type
private com.sun.enterprise.config.ConfigContext
ctx
private com.sun.enterprise.config.serverbeans.Resources
rBean
private String
serverName
private volatile J2EEResourceCollection
resources
Constructors Summary
Methods Summary
public ResourcePropertycreateProperty(java.lang.String name)

        return new ResourcePropertyImpl(name);
    
public J2EEResourcecreateResource(int type, java.lang.String name)


        J2EEResource resource = null;

        switch(type) {
            case J2EEResource.PMF_RESOURCE :
                resource = new PMFResource(name);
                break;
            case J2EEResource.CUSTOM_RESOURCE :
                resource = new CustomResource(name);
                break;
            case J2EEResource.EXTERNAL_JNDI_RESOURCE :
                resource = new ExternalJndiResource(name);
                break;
// START OF IASRI 4650786
            case J2EEResource.MAIL_RESOURCE :
                resource = new MailResource(name);
                break;
// END OF IASRI 4650786
            default :
                throw new java.lang.IllegalArgumentException();
        }

        return resource;
    
private booleanisReferenced(java.lang.String resourceName)
Returns true if the given resource is referenced by this server.

param
resourceName name (jndi) of the resource
return
true if the named resource is used/referred by this server
throws
ConfigException if an error while parsing domain.xml

        return ServerHelper.serverReferencesResource(ctx, serverName, resourceName);
    
private voidloadCustomResource(J2EEResourceCollection resources)
Loads all the available custom resources into the resource collection.

param
resources j2ee resource collection
throws
ConfigException if an error while loading data into beans


        com.sun.enterprise.config.serverbeans.CustomResource[] jBeanSet =
            rBean.getCustomResource();

        if (jBeanSet == null) {
            return;
        }

        for (int i=0; i < jBeanSet.length; i++) {
            com.sun.enterprise.config.serverbeans.CustomResource next =
                (com.sun.enterprise.config.serverbeans.CustomResource) jBeanSet[i];

            // custom resource is not referenced by this server
            if ( !isReferenced(next.getJndiName()) ) {
                _logger.fine("Skipping Custom Resource:" + next.getJndiName() 
                            + ". It is not used by server: " + serverName);
                continue;
            }           

			// Start Bug Fix: 4693395
            if (!next.isEnabled()) {
				logMessageIfNotEnabled(next.getJndiName(), CUSTOM_RES_TYPE);
				continue;
			}
			// End Bug Fix: 4693395

            // convets the config bean to j2ee resource
            J2EEResource jr = toCustomJ2EEResource(next);

            resources.addResource(jr);
        }
    
public J2EEResourceCollectionloadDefaultResourceCollection()

    

      
                  
        if (resources != null) {
            return resources;
        }

        return loadResourceCollection(null);
    
private voidloadExternalJndiResource(J2EEResourceCollection resources)
Loads all available external jndi resources into the given resource collection.

param
resources j2ee resource collection
throws
ConfigException if an error while loading data into beans


        com.sun.enterprise.config.serverbeans.ExternalJndiResource[] jBeanSet =
            rBean.getExternalJndiResource();

        if (jBeanSet == null) {
            return;
        }

        for (int i=0; i<jBeanSet.length; i++) {
            com.sun.enterprise.config.serverbeans.ExternalJndiResource next =
                (com.sun.enterprise.config.serverbeans.ExternalJndiResource) jBeanSet[i];
            
            // external jndi resource is not referenced by this server
            if ( !isReferenced(next.getJndiName()) ) {
                _logger.fine("Skipping External JNDI Resource:" 
                            + next.getJndiName() 
                            + ". It is not used by server: " + serverName);
                continue;
            }

			// Start Bug Fix: 4693395
            if (!next.isEnabled()) {
				logMessageIfNotEnabled(next.getJndiName(), EXT_JNDI_RES_TYPE);
				continue;
			}
			// End Bug Fix: 4693395

            // convets the config bean to j2ee resource
            J2EEResource jr = toExternalJndiJ2EEResource(next);

            resources.addResource(jr);
        }
    
private voidloadMailResource(J2EEResourceCollection resources)
Loads all available mail resources into the given resource collection.

param
resources j2ee resource collection
throws
ConfigException if an error while loading data into beans


        com.sun.enterprise.config.serverbeans.MailResource[] jBeanSet =
            rBean.getMailResource();

        if (jBeanSet == null) {
            return;
        }

        for (int i = 0; i < jBeanSet.length; i++) {
            com.sun.enterprise.config.serverbeans.MailResource next =
                (com.sun.enterprise.config.serverbeans.MailResource)jBeanSet[i];
            
            // mail resource is not referenced by this server
            if ( !isReferenced(next.getJndiName()) ) {
                _logger.fine("Skipping Mail Resource:" + next.getJndiName() 
                            + ". It is not used by server: " + serverName);
                continue;
            }            

			// Start Bug Fix: 4693395
            if (!next.isEnabled()) {
				logMessageIfNotEnabled(next.getJndiName(), MAIL_RES_TYPE);
				continue;
			}
			// End Bug Fix: 4693395

            // convets the config bean to j2ee resource
            J2EEResource jr = toMailJ2EEResource(next);

            resources.addResource(jr);
        }
    
private voidloadPMFResource(J2EEResourceCollection resources)

        com.sun.enterprise.config.serverbeans.PersistenceManagerFactoryResource[] jBeanSet = rBean.getPersistenceManagerFactoryResource();
        if(jBeanSet == null) return;

        for(int i = 0; i <jBeanSet.length; i++) {
            com.sun.enterprise.config.serverbeans.PersistenceManagerFactoryResource next =
            (com.sun.enterprise.config.serverbeans.PersistenceManagerFactoryResource) jBeanSet[i];
            
            // PMF resource is not referenced by this server
            if ( !isReferenced(next.getJndiName()) ) {
                _logger.fine("Skipping PMF Resource:" + next.getJndiName() 
                            + ". It is not used by server: " + serverName);
                continue;
            }            

			// Start Bug Fix: 4693395
            if (!next.isEnabled()) {
				logMessageIfNotEnabled(next.getJndiName(), PMF_RES_TYPE);
				continue;
			}
			// End Bug Fix: 4693395

            resources.addResource(toPMFJ2EEResource(next));
        }
    
public synchronized J2EEResourceCollectionloadResourceCollection(java.lang.String resourcesURL)


        resources = new J2EEResourceCollectionImpl();

           ServerContext sc = ApplicationServer.getServerContext();
           if(sc == null) {
//IASRI 4660742               System.out.println("Not Running in server. Returning empty resources...");
// START OF IASRI 4660742
	_logger.log(Level.FINE,"Not Running in server. Returning empty resources...");
// END OF IASRI 4660742
               return resources;
           } else {
                // name of this server instance
                serverName = sc.getInstanceName();
           }

           try {
               ctx = sc.getConfigContext();
               //ROB: config changes
               //rBean = ServerBeansFactory.getServerBean(ctx).getResources();
               rBean = ServerBeansFactory.getDomainBean(ctx).getResources();
               //RAMAKANTH
               if (rBean == null)
               {
                   rBean = new Resources();
               }
           } catch (ConfigException ce) {
/** IASRI 4660742
               ce.printStackTrace();
               System.out.println("Error initializing context");
               System.out.println("Returning empty resources...");
*/
// START OF IASRI 4660742
		 _logger.log(Level.SEVERE,"enterprise.empty_resource",ce);
		_logger.log(Level.FINE,"Error initializing context. Returning empty resources...");
// END OF IASRI 4660742
               return resources;
           }

        try {
            _logger.log(Level.FINE,"Loading PMF Resource==========");
            loadPMFResource(resources);
            _logger.log(Level.FINE,"Loading Custom Resource==========");
            loadCustomResource(resources);
            _logger.log(Level.FINE,"Loading External Jndi Resource==========");
            loadExternalJndiResource(resources);
// START OF IASRI 4650786
            _logger.log(Level.FINE,"Loading Mail Resource==========");
            loadMailResource(resources);
// END OF IASRI 4650786
// END OF IASRI 4660742
        } catch (ConfigException ce) {
// IASRI 4660742            ce.printStackTrace();
// IASRI 4660742            System.out.println("Error Loading resources: "+ce.getMessage());
// START OF IASRI 4660742
		 _logger.log(Level.SEVERE,"enterprise.config_exception",ce);
		_logger.log(Level.FINE,"Error Loading resources: "+ce.getMessage());
// END OF IASRI 4660742
            throw new J2EEResourceException(ce);
        }
        return resources;

    
private voidlogMessageIfNotEnabled(java.lang.String name, java.lang.String type)

		_logger.log(Level.INFO, "enterprise.resource_disabled", new Object[] {name, type});
	
public voidstoreDefaultResourceCollection(J2EEResourceCollection resources)

        storeResourceCollection(resources, null);
    
public voidstoreResourceCollection(J2EEResourceCollection resources, java.lang.String resourcesURL)

 /*               ServerContext sc = ApplicationServer.getServerContext();

        if(sc == null) {
            throw new J2EEResourceException("Server Not Initialized");
        }

        try {
            ctx = sc.getConfigContext();
            rBean = ConfigBean.getServerObject(ctx).getResources();
        } catch (ConfigException ce) {
// IASRI 4660742            ce.printStackTrace();
// IASRI 4660742            System.out.println("Error initializing context");
// START OF IASRI 4660742
		 _logger.log(Level.SEVERE,"enterprise.config_exception",ce);
		_logger.log(Level.FINE,"Error initializing context ");
// END OF IASRI 4660742
            throw new J2EEResourceException(ce);
        }

        try {
            rBean.removeAllChildObjects();

// IASRI 4660742            System.out.println("Saving Jdbc Connection Pool==========");
// IASRI 4660742            saveJdbcConnectionPool(resources);
// IASRI 4660742            System.out.println("Saving Jdbc Resource==========");
// IASRI 4660742            saveJdbcResource(resources);
// IASRI 4660742            System.out.println("Saving Jms Resource==========");
// IASRI 4660742            saveJmsResource(resources);
// IASRI 4660742            System.out.println("Saving PMF Resource==========");
// IASRI 4660742            savePMFResource(resources);
// IASRI 4660742            System.out.println("Saving Custom Resource==========");
// IASRI 4660742            saveCustomResource(resources);
// IASRI 4660742            System.out.println("Saving External Jndi Resource==========");
// START OF IASRI 4660742
            _logger.log(Level.FINE,"Saving Jdbc Connection Pool==========");
            saveJdbcConnectionPool(resources);
            _logger.log(Level.FINE,"Saving Jdbc Resource==========");
            saveJdbcResource(resources);
            _logger.log(Level.FINE,"Saving Jms Resource==========");
            saveJmsResource(resources);
            _logger.log(Level.FINE,"Saving PMF Resource==========");
            savePMFResource(resources);
            _logger.log(Level.FINE,"Saving Custom Resource==========");
            saveCustomResource(resources);
            _logger.log(Level.FINE,"Saving External Jndi Resource==========");
// END OF IASRI 4660742
            saveExternalJndiResource(resources);
// START OF IASRI 4650786
            _logger.log(Level.FINE,"Saving Mail Resource==========");
            saveMailResource(resources);
// END OF IASRI 4650786
            rBean.save();
        } catch (ConfigException ce) {
//IASRI 4660742            ce.printStackTrace();
//IASRI 4660742            System.out.println("Error Loading resources");
// START OF IASRI 4660742
		 _logger.log(Level.SEVERE,"enterprise.config_exception",ce);
		_logger.log(Level.FINE,"Error Loading resources ");
// END OF IASRI 4660742
            throw new J2EEResourceException(ce);
        }
  */
    
public static J2EEResourcetoCustomJ2EEResource(com.sun.enterprise.config.serverbeans.CustomResource rbean)
Returns a new instance of j2ee custom resource from the given config bean. This method gets called from the custom resource deployer to convert custom-resource config bean into custom j2ee resource.

param
rbean custom-resource config bean
return
new instance of j2ee custom resource
throws
ConfigException if an error while loading data into beans


        CustomResource jr = new CustomResource( rbean.getJndiName() );

        //jr.setDescription(rbean.getDescription()); // FIXME: getting error

        // sets the enable flag
        jr.setEnabled( rbean.isEnabled() );

        // sets the resource type
        jr.setResType( rbean.getResType() );

        // sets the factory class name
        jr.setFactoryClass( rbean.getFactoryClass() );

        // sets the properties
        ElementProperty[] s = rbean.getElementProperty();
        if (s!= null) {
            for(int j = 0; j <s.length; j++) {

                com.sun.enterprise.config.serverbeans.ElementProperty next =
                    (com.sun.enterprise.config.serverbeans.ElementProperty) s[j];

                ResourceProperty rp =
                    new ResourcePropertyImpl(next.getName(), next.getValue());

                jr.addProperty(rp);
            }
        }

        return jr;
    
public static J2EEResourcetoExternalJndiJ2EEResource(com.sun.enterprise.config.serverbeans.ExternalJndiResource rbean)
Returns a new instance of j2ee external jndi resource from the given config bean. This method gets called from the external resource deployer to convert external-jndi-resource config bean into external-jndi j2ee resource.

param
rbean external-jndi-resource config bean
return
a new instance of j2ee external jndi resource
throws
ConfigException if an error while loading data into beans


        ExternalJndiResource jr = new ExternalJndiResource(rbean.getJndiName());

        //jr.setDescription( rbean.getDescription() ); // FIXME: getting error

        // sets the enable flag
        jr.setEnabled( rbean.isEnabled() );

        // sets the jndi look up name
        jr.setJndiLookupName( rbean.getJndiLookupName() );

        // sets the resource type
        jr.setResType( rbean.getResType() );

        // sets the factory class name
        jr.setFactoryClass( rbean.getFactoryClass() );

        // sets the properties
        ElementProperty[] s = rbean.getElementProperty();
        if (s!= null) {
            for (int j=0; j<s.length; j++) {
                com.sun.enterprise.config.serverbeans.ElementProperty next =
                    (com.sun.enterprise.config.serverbeans.ElementProperty) s[j];

                ResourceProperty rp =
                    new ResourcePropertyImpl(next.getName(), next.getValue());
                jr.addProperty(rp);
            }
        }

        return jr;
    
public static J2EEResourcetoMailJ2EEResource(com.sun.enterprise.config.serverbeans.MailResource rbean)
Returns a new instance of j2ee mail resource from the given config bean. This method gets called from the mail resource deployer to convert mail config bean into mail j2ee resource.

param
rbean mail-resource config bean
return
a new instance of j2ee mail resource
throws
ConfigException if an error while loading data into beans


        MailResource jr = new MailResource(rbean.getJndiName());

        //jr.setDescription(rbean.getDescription()); // FIXME: getting error
        jr.setEnabled(rbean.isEnabled());
        jr.setStoreProtocol(rbean.getStoreProtocol());
        jr.setStoreProtocolClass(rbean.getStoreProtocolClass());
        jr.setTransportProtocol(rbean.getTransportProtocol());
        jr.setTransportProtocolClass(rbean.getTransportProtocolClass());
        jr.setMailHost(rbean.getHost());
        jr.setUsername(rbean.getUser());
        jr.setMailFrom(rbean.getFrom());
        jr.setDebug(rbean.isDebug());

        // sets the properties
        ElementProperty[] s = rbean.getElementProperty();
        if (s != null) {
            for (int j = 0; j < s.length; j++) {
                com.sun.enterprise.config.serverbeans.ElementProperty next =
                    (com.sun.enterprise.config.serverbeans.ElementProperty)s[j];

                ResourceProperty rp =
                    new ResourcePropertyImpl(next.getName(), next.getValue());
                jr.addProperty(rp);
            }
        }

        return jr;
    
public static J2EEResourcetoPMFJ2EEResource(com.sun.enterprise.config.serverbeans.PersistenceManagerFactoryResource rbean)
Returns a new instance of j2ee pmf resource from the given config bean. This method gets called from the Persistence Manager Factory Resource deployer to convert persistence-manager-resource-factory config bean into pmf j2ee resource.

param
rbean persistence-manager-resource-factory config bean
return
a new instance of j2ee pmf resource
throws
ConfigException if an error while loading data into beans

        PMFResource jr = new PMFResource(rbean.getJndiName());
        jr.setEnabled(rbean.isEnabled());
        jr.setFactoryClass(rbean.getFactoryClass());
        jr.setJdbcResourceJndiName(rbean.getJdbcResourceJndiName());

        ElementProperty[] s = rbean.getElementProperty();
        if (s!= null) {
            for (int j = 0; j <s.length; j++) {
                com.sun.enterprise.config.serverbeans.ElementProperty next1 =
                (com.sun.enterprise.config.serverbeans.ElementProperty) s[j];
                ResourceProperty rp = new ResourcePropertyImpl(next1.getName(),
                        next1.getValue());
                jr.addProperty(rp);
            }
        }
        //jr.setDescription(next.getDescription()); // FIXME add this

        return jr;