com.pactera.edg.am.metamanager.extractor.util.AdapterContextLoader.java Source code

Java tutorial

Introduction

Here is the source code for com.pactera.edg.am.metamanager.extractor.util.AdapterContextLoader.java

Source

/*
 * Copyright 2009 by pactera.edg.am Corporation. Address:HePingLi East Street No.11
 * 5-5, BeiJing,
 * 
 * All rights reserved.
 * 
 * This software is the confidential and proprietary information of pactera.edg.am
 * Corporation ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with pactera.edg.am.
 */

package com.pactera.edg.am.metamanager.extractor.util;

import java.util.Arrays;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.pactera.edg.am.metamanager.extractor.bo.ExtractorLogLevel;

/**
 * ?spring?,spring:OS?;?OS?;spring
 * context, ??OS??
 * 
 * @author user
 * @version 1.0 Date: Aug 7, 2009
 * 
 */
public class AdapterContextLoader {

    private static Log log = LogFactory.getLog(AdapterContextLoader.class);

    /**
     * 
     * SpringClassPathXMLApplicationContext,????${var}
     * prop??? ??.properties??,??${var}
     * 
     * @param configLocations
     *            application context?
     * @param props
     *            ??application context????key/value
     * @return ApplicationContext Application
     *         Context,?,??,bean,null
     * @exception
     */
    public static ApplicationContext createApplicationContext(String[] configLocations, Properties props) {
        // :spring?system properties??.
        Properties bakProps = null;
        String logMsg = null;
        try {
            if (props != null && props.size() > 0) {
                // system properties
                bakProps = System.getProperties();

                for (Object key : props.keySet()) {
                    System.setProperty((String) key, props.getProperty((String) key));
                }

                if (log.isDebugEnabled()) {
                    StringBuffer sb = new StringBuffer();
                    sb.append("??springxml?${var}prop:\n");
                    for (Object key : props.keySet()) {
                        sb.append(key + "=" + System.getProperty((String) key));
                        sb.append("\n");
                    }
                    log.debug(sb.toString());
                }
            }
            return new ClassPathXmlApplicationContext(configLocations);
        } catch (BeanDefinitionStoreException bse) {
            // 
            logMsg = new StringBuilder("?spring,?")
                    .append(Arrays.toString(configLocations)).append(bse.getMessage()).toString();
            log.error(logMsg, bse);
            AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.ERROR, logMsg);
            throw bse;
        } catch (BeanCreationException ce) {
            // BEAN,?RMI??
            logMsg = new StringBuilder("?spring,?BEAN,")
                    .append(Arrays.toString(configLocations)).append(ce.getMessage()).toString();
            log.error(logMsg, ce);
            if (logMsg.indexOf("ORA-01017") > -1 || logMsg.indexOf("Invalid password") > -1) {
                // ORACLE????
                AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.ERROR,
                        "??/?,?,???????!");
            } else {
                AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.ERROR, logMsg);
            }
            throw ce;
        } finally {
            // system properties,???,??
            if (bakProps != null) {
                System.setProperties(bakProps);
            }
        }
    }
}