FileDocCategorySizeDatePackage
ResourceUtilities.javaAPI DocGlassfish v2 API12965Fri May 04 22:35:16 BST 2007com.sun.enterprise.resource

ResourceUtilities

public class ResourceUtilities extends Object
A class that holds utility/helper routines. Expected to contain static methods to perform utility operations.
since
Appserver 9.0

Fields Summary
private static final Logger
_logger
private static final com.sun.enterprise.util.i18n.StringManager
localStrings
Constructors Summary
private ResourceUtilities()

    
      /*disallowed*/ 
   
Methods Summary
private static java.lang.StringgetIdToCompare(Resource res)

       final AttributeList attrs = res.getAttributes();
       final String type = res.getType();
       String id;
       if (Resource.JDBC_CONNECTION_POOL.equals(type) ||
           Resource.CONNECTOR_CONNECTION_POOL.equals(type)){
          id = getNamedAttributeValue(attrs, CONNECTION_POOL_NAME);   // this should come from refactored stuff TBD
       }
       else if (Resource.CONNECTOR_SECURITY_MAP.equals(type)) {
           id = getNamedAttributeValue(attrs, SECURITY_MAP_NAME);  // this should come from refactored stuff TBD
       }
       else if (Resource.RESOURCE_ADAPTER_CONFIG.equals(type)) {
           id = getNamedAttributeValue(attrs, RESOURCE_ADAPTER_CONFIG_NAME);  // this should come from refactored stuff TBD
       }
       else {
           //it is OK to assume that this Resource will one of the *RESOURCEs?
           id = getNamedAttributeValue(attrs, JNDI_NAME); // this should come from refactored stuff TBD
       }
       return ( id );
   
private static java.lang.StringgetNamedAttributeValue(javax.management.AttributeList attrs, java.lang.String aName)

       String value = null;
       for (final Object obj : attrs) {
           if (obj instanceof Attribute) {
               final Attribute a = (Attribute) obj;
               if (aName.equals(a.getName())) {
                   value = a.getValue().toString();
               }
           }
       }
       return ( value );
     
public static java.util.SetgetResourceConfigConflicts(java.util.Set resSet, com.sun.enterprise.config.ConfigContext cc)
Checks if any of the Resource in the given set has a conflict with resource definitions in the domain.xml. A conflict is defined based on the type of the resource. For example, a JDBC Resource has "jndi-name" that is the identifying key where as for a JDBC Connection Pool, it is the "name" that must be unique.

param
resSet a Set of Resource elements.
param
cc instance of ConfigContext that you want to confirm this against. May not be null.
return
a Set of Resource elements that contains conflicting elements from the given Set. This method does not create any Resource elements. It just references an element in conflict from a Set that is returned. If there are no conflicts, an empty Set is returned. This method never returns a null. If the given Set is null, an empty Set is returned.
throws
ConfigException if there is any error with accessing the configuration hierarchy.

       final Set<Resource> conflicts = new HashSet<Resource>();
       if (resSet != null) {
           for (final Resource res : resSet) {
               final String id = getIdToCompare(res);
               final ConfigBean sb = ResourceHelper.findResource(cc, id);
               if (sb != null) {
                   conflicts.add(res);
               }
           }
       }
       return ( conflicts );
   
public static voidgetResourceConflictsWithDomainXML(java.util.List resList, com.sun.enterprise.config.ConfigContext configContext)
Checks if any of the Resource in the given set has a conflict with resource definitions in the domain.xml. A conflict is defined based on the type of the resource. For example, a JDBC Resource has "jndi-name" that is the identifying key where as for a JDBC Connection Pool, it is the "name" that must be unique.

param
resSet a Set of Resource elements.
param
configContext instance of ConfigContext that you want to confirm this against. May not be null.
throws
ResourceConflictException an exception is thrown when an archive is found to have two or more resources that conflict with resources already present in domain.xml.
throws
ConfigException if there is any error with accessing the configuration hierarchy.

         if (resList != null) {
             Iterator<Resource> iterRes = resList.iterator();
             StringBuffer conflictingResources = new StringBuffer();
             while (iterRes.hasNext()) {
                 Resource res = iterRes.next();
                 final String id = getIdToCompare(res);
                 final ConfigBean sb = ResourceHelper.findResource(configContext, id);
                 if (sb != null) {
                     conflictingResources.append("\n");
                     String message = localStrings.getString(
                             "conflict.resource.with.domain.xml",
                             getIdToCompare(res));
                     conflictingResources.append(message);
                     _logger.warning(message);
                     if(_logger.isLoggable(Level.FINE))
                         logAttributes(res);
                 }
             }
             if(conflictingResources.toString().length() > 0){
                 throw new ResourceConflictException(conflictingResources.toString());
                 
             }
         }
     
private static voidlogAttributes(Resource res)

        StringBuffer message = new StringBuffer();
        Iterator<Attribute> attributeIter = res.getAttributes().iterator();
        while(attributeIter.hasNext()){
            Attribute attribute = attributeIter.next();
            message.append(attribute.getName());
            message.append("=");
            message.append(attribute.getValue());
            message.append(" ");
        }
        _logger.fine(localStrings.getString("resource.attributes",
                message.toString()));
    
public static java.util.SetresolveResourceDuplicatesConflictsWithinArchive(java.util.List sunResList)
Resolves all duplicates and conflicts within an archive and returns a set of resources that needs to be created for the archive being deployed. The deployment backend would then use these set of resources to check for conflicts with resources existing in domain.xml and then continue with deployment. All resource duplicates within an archive found are flagged with a WARNING and only one resource is added in the final Resource Set returned. We currently do not handle any resource conflicts found within the archive and the method throws an exception when this condition is detected.

param
sunResList a list of SunResourcesXML corresponding to sun-resources.xml found within an archive.
return
a Set of Resources that have been resolved of duplicates and conflicts.
throws
ResourceConflictException an exception is thrown when an archive is found to have two or more resources that conflict with each other.

         boolean conflictExist = false;
         StringBuffer conflictingResources = new StringBuffer();
         Set<Resource> resourceSet = new HashSet<Resource>();
         Iterator<SunResourcesXML> sunResourcesXMLIter = sunResList.iterator();
         while(sunResourcesXMLIter.hasNext()){
             //get list of resources from one sun-resources.xml file
             SunResourcesXML sunResXML = sunResourcesXMLIter.next(); 
             List<Resource> resources = sunResXML.getResourcesList();
             Iterator<Resource> resourcesIter = resources.iterator();
             //for each resource mentioned
             while(resourcesIter.hasNext()){
                 Resource res = resourcesIter.next();
                 Iterator<Resource> resSetIter = resourceSet.iterator();
                 boolean addResource = true;
                 //check if a duplicate has already been added
                 while(resSetIter.hasNext()){
                     Resource existingRes = resSetIter.next();
                     if(existingRes.equals(res)){
                         //duplicate within an archive
                         addResource = false;
                         _logger.warning(localStrings.getString(
                                 "duplicate.resource.sun.resource.xml",
                                 getIdToCompare(res),
                                 sunResXML.getXMLPath()));
                         break;
                     }
                     //check if another existing resource conflicts with the
                     //resource being added
                     if(existingRes.isAConflict(res)){
                         //conflict within an archive
                         addResource = false;
                         conflictingResources.append("\n");
                         String message = localStrings.getString(
                                 "conflict.resource.sun.resource.xml",
                                 getIdToCompare(res),
                                 sunResXML.getXMLPath());
                         conflictingResources.append(message);
                         _logger.warning(message);
                         if(_logger.isLoggable(Level.FINE))
                             logAttributes(res);
                     }
                 }
                 if(addResource)
                     resourceSet.add(res);
             }
         }
         if(conflictingResources.toString().length() > 0){
             throw new ResourceConflictException(conflictingResources.toString());
         }
         return resourceSet;