Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common
   * Development and Distribution License("CDDL") (collectively, the
   * "License"). You may not use this file except in compliance with the
  * License. You can obtain a copy of the License at
  * http://www.netbeans.org/cddl-gplv2.html
  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  * specific language governing permissions and limitations under the
  * License.  When distributing the software, include this License Header
  * Notice in each file and include the License file at
  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Sun in the GPL Version 2 section of the License file that
  * accompanied this code. If applicable, add the following below the
  * License Header, with the fields enclosed by brackets [] replaced by
  * your own identifying information:
  * "Portions Copyrighted [year] [name of copyright owner]"
  *
  * Contributor(s):
  *
  * Portions Copyrighted 2007 Sun Microsystems, Inc.
  */
 package org.netbeans.modules.j2ee.sun.api.restricted;
 
 import java.util.Map;

Author(s):
Nitya Doraisamy
 
 public class RegistrationUtils {
 
     private static String POOL_EXTENSION = "_Base";
     private static String DELETE_POOL = "deleteJdbcConnectionPool";
     private static String DELETE_JDBC = "deleteJdbcResource";
     private static String DELETE_MAIL = "deleteMailResource";
     private static String DELETE_CONNECTOR = "deleteConnectorResource";
     private static String DELETE_CONNECTORPOOL = "deleteConnectorConnectionPool";
     private static String DELETE_ADMINOBJECT = "deleteAdminObjectResource";
     private static String DAS_SERVER_NAME = "server";
     
     private static final Logger LOG = Logger.getLogger(RegistrationUtils.class.getName());
     
     public RegistrationUtils() {
     }
     
     public static void checkUpdateServerResources(ServerInterface mejbjava.io.File primaryFile) {
         Resources resources = ResourceUtils.getResourcesGraph(primaryFile);
         updateJDBCResourcesAndPools(resourcesmejb);    
         updateMailResources(resourcesmejb);    
         updateJMSResources(resourcesmejb);    
     }
 
     private static void updateJDBCResourcesAndPools(Resources resourcesServerInterface mejb){
         HashMap serverDatasources = getServerResources(mejb.);
         
         JdbcConnectionPool[] pools = resources.getJdbcConnectionPool();
         JdbcResource[] dataSources = resources.getJdbcResource();
         
         //Delete datasources that are in this project
         HashMap dupJdbcResources = getProjectDatasources(serverDatasourcesdataSources);
         deleteServerResources(dupJdbcResourcesmejb);
         
         for(int i=0; i<pools.lengthi++){           
             JdbcConnectionPool connectionPoolBean = pools[i];
             String newPoolName = connectionPoolBean.getName();
             
             //Is this pool registered on the server.
             if(serverPools.containsKey(newPoolName)){
                 HashMap serverJdbcResources = getReferringResources(newPoolNameserverDatasourcesmejb);               
                 if(serverJdbcResources.size() > 0){
                     //Change this connectionPoolName
                     copyServerPool(serverPoolsnewPoolName.mejb);
                     updateExternalResource(serverJdbcResourcesnewPoolNamemejb);
                 }
                 deleteOldServerPool(newPoolNamemejb);
            }else{
                //delete pool.
                deleteOldServerPool(newPoolNamemejb);
            }
        }
    }
    
    private static void updateMailResources(Resources resourcesServerInterface mejb){
        HashMap serverMailResources = getServerResources(mejb.);
        MailResource[] mails = resources.getMailResource();
        //Delete mail resources that are in this project
        HashMap dupMailResources = getProjectMailResources(serverMailResourcesmails);
        deleteServerResources(dupMailResourcesmejb);
    }
    
    private static void updateJMSResources(Resources resourcesServerInterface mejb){
        updateAdminObjects(resourcesmejb);
        updateConnectorsAndPools(resourcesmejb);
    }
    
    private static void updateAdminObjects(Resources resourcesServerInterface mejb){
        HashMap serverAdminObjects = getServerResources(mejb.);
        AdminObjectResource[] adminObjects = resources.getAdminObjectResource();
        //Delete adminObjects resources that are in this project
        HashMap dupAdminObjects = getProjectAdminObjects(serverAdminObjectsadminObjects);
        deleteServerResources(dupAdminObjectsmejb);
    }
    
    private static void updateConnectorsAndPools(Resources resourcesServerInterface mejb){
        HashMap serverConnectorPools = getServerConnectionPools(mejb.);
        HashMap serverConnectors = getServerResources(mejb.);
        
        ConnectorConnectionPool[] connectorPools = resources.getConnectorConnectionPool();
        ConnectorResource[] connectors = resources.getConnectorResource();
        
        //Delete connectors that are in this project
        HashMap dupConnectors = getProjectConnectors(serverConnectorsconnectors);
        deleteServerResources(dupConnectorsmejb);
        
        for(int i=0; i<connectorPools.lengthi++){           
            ConnectorConnectionPool connectorPoolBean = connectorPools[i];
            String newPoolName = connectorPoolBean.getName();
            
            //Is this pool registered on the server.
            if(serverConnectorPools.containsKey(newPoolName)){
                HashMap serverConnectorResources = getReferringResources(newPoolNameserverConnectorsmejb);               
                if(serverConnectorResources.size() > 0){
                    //Change this connectorPoolName
                    copyServerPool(serverConnectorPoolsnewPoolName.mejb);
                    updateExternalResource(serverConnectorResourcesnewPoolNamemejb);
                }
                deleteOldServerPool(newPoolNamemejb);
            }else{
                //delete pool.
                deleteOldServerPool(newPoolNamemejb);
            }
        }
    }
    
    public static HashMap getServerConnectionPools(ServerInterface mejbString operationName){
        HashMap<StringObjectNamepools = new HashMap<StringObjectName>();
        try {
            ObjectName configObjName = new ObjectName(.);
            ObjectName[] resourceObjects = (ObjectName[])  mejb.invoke(configObjNameoperationNamenullnull);
            for(int i=0; i<resourceObjects.lengthi++){
                ObjectName objName = resourceObjects[i];
                String poolName = (String)mejb.getAttribute(objName"name"); //NOI18N
                pools.put(poolNameobjName);
            } // for - each connection pool
        } catch (Exception ex) {
            .log(."Unable to get server connection pools"ex);
        }
        return pools;
    }
    
    public static HashMap getReferringResources(String poolNameHashMap serverResourcesServerInterface mejb) {
        HashMap<StringObjectNamereferringResources = new HashMap<StringObjectName>();
        try{
            for(Iterator itr=serverResources.keySet().iterator(); itr.hasNext();){
                String resourceName = (String)itr.next();
                ObjectName objName = (ObjectName)serverResources.get(resourceName);
                
                String connpoolName = (String)mejb.getAttribute(objName"pool-name"); //NOI18N
                if(connpoolName.equals(poolName)){
                    referringResources.put(resourceNameobjName);
                }
            }
        }catch(Exception ex){
            .log(."Unable to get referring resources"ex);
        }
        return referringResources;
    }
    public static HashMap getProjectDatasources(HashMap serverJdbcResourcesJdbcResource[] dataSources){
        HashMap<StringObjectNamedatasources = new HashMap<StringObjectName>();
        for(int i=0; i<dataSources.lengthi++){
            JdbcResource dsResource = dataSources[i];
            String dsName = dsResource.getJndiName();
            if(serverJdbcResources.containsKey(dsName)){
                datasources.put(dsName, (ObjectName)serverJdbcResources.get(dsName));
            }
        }
        return datasources;
    }
    
    public static HashMap getProjectMailResources(HashMap serverMailResourcesMailResource[] mails){
        HashMap<StringObjectNamemailResources = new HashMap<StringObjectName>();
        for(int i=0; i<mails.lengthi++){
            MailResource mailResource = mails[i];
            String mailName = mailResource.getJndiName();
            if(serverMailResources.containsKey(mailName)){
                mailResources.put(mailName, (ObjectName)serverMailResources.get(mailName));
            }
        }
        return mailResources;
    }
    
    public static HashMap getProjectAdminObjects(HashMap serverAdminObjectsAdminObjectResource[] adminObjects){
        HashMap<StringObjectNameadminObjectResources = new HashMap<StringObjectName>();
        for(int i=0; i<adminObjects.lengthi++){
            AdminObjectResource aoResource = adminObjects[i];
            String jndiName = aoResource.getJndiName();
            if(serverAdminObjects.containsKey(jndiName)){
                adminObjectResources.put(jndiName, (ObjectName)serverAdminObjects.get(jndiName));
            }
        }
        return adminObjectResources;
    }
    
    public static HashMap getProjectConnectors(HashMap serverConnectorsConnectorResource[] connectors){
        HashMap<StringObjectNameconnectorResources = new HashMap<StringObjectName>();
        for(int i=0; i<connectors.lengthi++){
            ConnectorResource connResource = connectors[i];
            String jndiName = connResource.getJndiName();
            if(serverConnectors.containsKey(jndiName)){
                connectorResources.put(jndiName, (ObjectName)serverConnectors.get(jndiName));
            }
        }
        return connectorResources;
    }
    
    public static void updateExternalResource(HashMap serverResourcesString newPoolNameServerInterface mejb){
        try{
            String updatedPoolName = newPoolName + ;
            for(Iterator itr=serverResources.values().iterator(); itr.hasNext();){
                ObjectName dsObjName = (ObjectName)itr.next();
                Attribute poolNameAttr = new Attribute("pool-name"updatedPoolName);
                mejb.setAttribute(dsObjNamepoolNameAttr);
            }
        }catch(Exception ex){
            //Could not update resource. 
            ..println(ex.getLocalizedMessage());
        }
    }
    
    
    
    public static void copyServerPool(HashMap serverPoolsString newPoolNameString operationNameServerInterface mejb){
        try{
            String updatedPoolName = newPoolName + ;
            if(! serverPools.containsKey(updatedPoolName)){
                ObjectName serverPoolObj = (ObjectName)serverPools.get(newPoolName);
                Map attributeInfos = ResourceUtils.getResourceAttributeNames(serverPoolObjmejb);
                attributeInfos.remove("name");
                String[] attrNames = (String[]) attributeInfos.keySet().toArray(new String[attributeInfos.size()]);
                AttributeList attrList = mejb.getAttributes(serverPoolObjattrNames);
                Attribute nameAttr = new Attribute("name"updatedPoolName);
                attrList.add(nameAttr);
                          
                Properties props = new Properties();
                AttributeList propsList = (AttributeList)mejb.invoke(serverPoolObj.nullnull);             
                for(int i=0; i<propsList.size(); i++){
                    Attribute propAttr = (Attribute)propsList.get(i);
                    String propName = propAttr.getName();
                    Object propValue = propAttr.getValue();
                    if(propValue != null){
                        props.put(propNamepropValue);
                    }    
                }
                
                Object[] params = new Object[]{attrListpropsnull};
                ResourceUtils.createResource(operationNameparamsmejb);
            }
        }catch(Exception ex){  
            //Unable to copy pool
            ..println(ex.getLocalizedMessage());
        }
    }
    
    public static void deleteOldServerPool(String newPoolNameString operationNameServerInterface mejb){
        try{
            ObjectName objName = new ObjectName(.);
            mejb.invoke(objNameoperationNamenew Object[]{newPoolName},
                    new String[]{"java.lang.String""java.lang.String"} );
        }catch(Exception ex){
            .log(."Unable to clean up existing duplicate pools"ex);
         }
    }
    
    public static void deleteServerResources(HashMap serverResourcesServerInterface mejbString operationName){
        try{
            ObjectName objName = new ObjectName(.);
            for(Iterator itr = serverResources.keySet().iterator(); itr.hasNext();){
                String jdbcName = (String)itr.next();
                mejb.invoke(objNameoperationNamenew Object[]{jdbcName},
                        new String[]{"java.lang.String""java.lang.String"} );
            }
        }catch(Exception ex){
            .log(."Unable to clean up existing duplicate datasources"ex);
        }
    }
    
    public static HashMap getServerResources(ServerInterface mejbString operationName){
        HashMap<StringObjectNameserverResources = new HashMap<StringObjectName>();
        try {
            ObjectName configObjName = new ObjectName(.);
            ObjectName[] resourceObjects = (ObjectName[]) mejb.invoke(configObjNameoperationNamenullnull);
            for(int i=0; i<resourceObjects.lengthi++){
                ObjectName objName = resourceObjects[i];
                String jndiName = (String)mejb.getAttribute(objName"jndi-name"); //NOI18N
                serverResources.put(jndiNameobjName);
            } // for 
        } catch (Exception ex) {
            .log(."Unable to get server datasources"ex);
        }
        return serverResources;
    }
    
New to GrepCode? Check out our FAQ X