Example usage for org.springframework.context ConfigurableApplicationContext CONFIG_LOCATION_DELIMITERS

List of usage examples for org.springframework.context ConfigurableApplicationContext CONFIG_LOCATION_DELIMITERS

Introduction

In this page you can find the example usage for org.springframework.context ConfigurableApplicationContext CONFIG_LOCATION_DELIMITERS.

Prototype

String CONFIG_LOCATION_DELIMITERS

To view the source code for org.springframework.context ConfigurableApplicationContext CONFIG_LOCATION_DELIMITERS.

Click Source Link

Document

Any number of these characters are considered delimiters between multiple context config paths in a single String value.

Usage

From source file:com.remind.bpf.common.mybatis.MySqlSessionFactoryBean.java

/**
 * Build a {@code SqlSessionFactory} instance.
 *
 * The default implementation uses the standard MyBatis {@code XMLConfigBuilder} API to build a
 * {@code SqlSessionFactory} instance based on an Reader.
 *
 * @return SqlSessionFactory/*w  w  w  .  j  av a2s .  com*/
 * @throws IOException if loading the config file failed
 */
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {

    Configuration configuration;

    XMLConfigBuilder xmlConfigBuilder = null;
    if (this.configLocation != null) {
        xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null,
                this.configurationProperties);
        configuration = xmlConfigBuilder.getConfiguration();
    } else {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Property 'configLocation' not specified, using default MyBatis Configuration");
        }
        configuration = new Configuration();
        configuration.setVariables(this.configurationProperties);
    }

    if (this.objectFactory != null) {
        configuration.setObjectFactory(this.objectFactory);
    }

    if (this.objectWrapperFactory != null) {
        configuration.setObjectWrapperFactory(this.objectWrapperFactory);
    }

    if (hasLength(this.typeAliasesPackage)) {
        String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeAliasPackageArray) {
            configuration.getTypeAliasRegistry().registerAliases(packageToScan,
                    typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Scanned package: '" + packageToScan + "' for aliases");
            }
        }
    }

    //python
    if (hasLength(this.typeAliasesPackages)) {
        //???
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        //?
        String[] typeAliasPackagesArray = tokenizeToStringArray(this.typeAliasesPackages,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);

        for (String typeAliasPackages : typeAliasPackagesArray) {
            if (typeAliasPackages != null && typeAliasPackages.trim().length() > 0) {

                //?form
                Resource[] packageToScanArray = resolver.getResources(typeAliasPackages);

                if (packageToScanArray != null && packageToScanArray.length > 0) {
                    for (Resource packageToScan : packageToScanArray) {
                        if (packageToScan != null) {
                            String aliases = convertToPackageFormat(packageToScan);

                            if (aliases != null && aliases.trim().length() > 0) {
                                configuration.getTypeAliasRegistry().registerAliases(aliases,
                                        typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);

                                if (this.logger.isDebugEnabled()) {
                                    this.logger.debug("Scanned package: '" + aliases + "' for aliases");
                                }
                            } else
                                this.logger.error("Scanned package path is null for aliases");
                        }
                    }
                }
            }
        }
    }

    if (!isEmpty(this.typeAliases)) {
        for (Class<?> typeAlias : this.typeAliases) {
            configuration.getTypeAliasRegistry().registerAlias(typeAlias);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Registered type alias: '" + typeAlias + "'");
            }
        }
    }

    if (!isEmpty(this.plugins)) {
        for (Interceptor plugin : this.plugins) {
            configuration.addInterceptor(plugin);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Registered plugin: '" + plugin + "'");
            }
        }
    }

    if (hasLength(this.typeHandlersPackage)) {
        String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeHandlersPackageArray) {
            configuration.getTypeHandlerRegistry().register(packageToScan);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Scanned package: '" + packageToScan + "' for type handlers");
            }
        }
    }

    if (!isEmpty(this.typeHandlers)) {
        for (TypeHandler<?> typeHandler : this.typeHandlers) {
            configuration.getTypeHandlerRegistry().register(typeHandler);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Registered type handler: '" + typeHandler + "'");
            }
        }
    }

    if (xmlConfigBuilder != null) {
        try {
            xmlConfigBuilder.parse();

            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Parsed configuration file: '" + this.configLocation + "'");
            }
        } catch (Exception ex) {
            throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
        } finally {
            ErrorContext.instance().reset();
        }
    }

    if (this.transactionFactory == null) {
        this.transactionFactory = new SpringManagedTransactionFactory();
    }

    Environment environment = new Environment(this.environment, this.transactionFactory, this.dataSource);
    configuration.setEnvironment(environment);

    if (this.databaseIdProvider != null) {
        try {
            configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
        } catch (SQLException e) {
            throw new NestedIOException("Failed getting a databaseId", e);
        }
    }

    if (!isEmpty(this.mapperLocations)) {
        for (Resource mapperLocation : this.mapperLocations) {
            if (mapperLocation == null) {
                continue;
            }

            try {
                XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
                        configuration, mapperLocation.toString(), configuration.getSqlFragments());
                xmlMapperBuilder.parse();
            } catch (Exception e) {
                throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
            } finally {
                ErrorContext.instance().reset();
            }

            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Parsed mapper file: '" + mapperLocation + "'");
            }
        }
    } else {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Property 'mapperLocations' was not specified or no matching resources found");
        }
    }

    return this.sqlSessionFactoryBuilder.build(configuration);
}

From source file:core.mybatis.SqlSessionFactoryBean.java

/**
 * Build a {@code SqlSessionFactory} instance.
 *
 * The default implementation uses the standard MyBatis {@code XMLConfigBuilder} API to build a
 * {@code SqlSessionFactory} instance based on an Reader.
 *
 * @return SqlSessionFactory/*from   ww  w  .ja  va 2 s.  c  o  m*/
 * @throws IOException if loading the config file failed
 */
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {

    Configuration configuration;

    XMLConfigBuilder xmlConfigBuilder = null;
    if (this.configLocation != null) {
        xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null,
                this.configurationProperties);
        configuration = xmlConfigBuilder.getConfiguration();
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Property 'configLocation' not specified, using default MyBatis Configuration");
        }
        configuration = new Configuration();
        configuration.setVariables(this.configurationProperties);
    }

    if (this.objectFactory != null) {
        configuration.setObjectFactory(this.objectFactory);
    }

    if (this.objectWrapperFactory != null) {
        configuration.setObjectWrapperFactory(this.objectWrapperFactory);
    }

    if (hasLength(this.typeAliasesPackage)) {
        Map<String, String> filterMap = new HashMap<String, String>();
        ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<Class<?>>();
        String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeAliasPackageArray) {
            //???domain wangxz update 20140417
            resolverUtil.findAnnotated(core.mybatis.MyBatisDomain.class, packageToScan);
            Set<Class<? extends Class<?>>> typeSet = resolverUtil.getClasses();
            if (typeSet != null) {
                Iterator<Class<? extends Class<?>>> it = typeSet.iterator();
                String packageName = "";
                while (it.hasNext()) {
                    Class<? extends Class<?>> c = it.next();
                    packageName = c.getPackage().getName();
                    if (!filterMap.containsKey(packageName)) {
                        filterMap.put(packageName, packageName);
                        configuration.getTypeAliasRegistry().registerAliases(packageName,
                                typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
                        System.out.println("Scanned package: '" + packageName + "' for aliases");
                    }
                }
            }
        }
    }

    if (!isEmpty(this.typeAliases)) {
        for (Class<?> typeAlias : this.typeAliases) {
            configuration.getTypeAliasRegistry().registerAlias(typeAlias);
            if (logger.isDebugEnabled()) {
                logger.debug("Registered type alias: '" + typeAlias + "'");
            }
        }
    }

    if (!isEmpty(this.plugins)) {
        for (Interceptor plugin : this.plugins) {
            configuration.addInterceptor(plugin);
            if (logger.isDebugEnabled()) {
                logger.debug("Registered plugin: '" + plugin + "'");
            }
        }
    }

    if (hasLength(this.typeHandlersPackage)) {
        String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeHandlersPackageArray) {
            configuration.getTypeHandlerRegistry().register(packageToScan);
            if (logger.isDebugEnabled()) {
                logger.debug("Scanned package: '" + packageToScan + "' for type handlers");
            }
        }
    }

    if (!isEmpty(this.typeHandlers)) {
        for (TypeHandler<?> typeHandler : this.typeHandlers) {
            configuration.getTypeHandlerRegistry().register(typeHandler);
            if (logger.isDebugEnabled()) {
                logger.debug("Registered type handler: '" + typeHandler + "'");
            }
        }
    }

    if (xmlConfigBuilder != null) {
        try {
            xmlConfigBuilder.parse();

            if (logger.isDebugEnabled()) {
                logger.debug("Parsed configuration file: '" + this.configLocation + "'");
            }
        } catch (Exception ex) {
            throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
        } finally {
            ErrorContext.instance().reset();
        }
    }

    if (this.transactionFactory == null) {
        this.transactionFactory = new SpringManagedTransactionFactory();
    }

    Environment environment = new Environment(this.environment, this.transactionFactory, this.dataSource);
    configuration.setEnvironment(environment);

    if (this.databaseIdProvider != null) {
        try {
            configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
        } catch (SQLException e) {
            throw new NestedIOException("Failed getting a databaseId", e);
        }
    }

    if (!isEmpty(this.mapperLocations)) {
        for (Resource mapperLocation : this.mapperLocations) {
            if (mapperLocation == null) {
                continue;
            }

            try {
                XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
                        configuration, mapperLocation.toString(), configuration.getSqlFragments());
                xmlMapperBuilder.parse();
            } catch (Exception e) {
                throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
            } finally {
                ErrorContext.instance().reset();
            }

            if (logger.isDebugEnabled()) {
                logger.debug("Parsed mapper file: '" + mapperLocation + "'");
            }
        }
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Property 'mapperLocations' was not specified or no matching resources found");
        }
    }

    return this.sqlSessionFactoryBuilder.build(configuration);
}

From source file:com.lawyer.cores.framework.mybatis.SqlSessionFactoryBean.java

/**
 * Build a {@code SqlSessionFactory} instance.
 *
 * The default implementation uses the standard MyBatis {@code XMLConfigBuilder} API to build a
 * {@code SqlSessionFactory} instance based on an Reader.
 *
 * @return SqlSessionFactory//  w  w  w.  jav  a  2  s  . c om
 * @throws IOException if loading the config file failed
 */
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {

    Configuration configuration;

    XMLConfigBuilder xmlConfigBuilder = null;
    if (this.configLocation != null) {
        xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null,
                this.configurationProperties);
        configuration = xmlConfigBuilder.getConfiguration();
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Property 'configLocation' not specified, using default MyBatis Configuration");
        }
        configuration = new Configuration();
        configuration.setVariables(this.configurationProperties);
    }

    if (this.objectFactory != null) {
        configuration.setObjectFactory(this.objectFactory);
    }

    if (this.objectWrapperFactory != null) {
        configuration.setObjectWrapperFactory(this.objectWrapperFactory);
    }

    if (hasLength(this.typeAliasesPackage)) {
        Map<String, String> filterMap = new HashMap<String, String>();
        ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<Class<?>>();
        String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeAliasPackageArray) {
            //???domain wangxz update 20140417
            resolverUtil.findAnnotated(MyBatisDomain.class, packageToScan);
            Set<Class<? extends Class<?>>> typeSet = resolverUtil.getClasses();
            if (typeSet != null) {
                Iterator<Class<? extends Class<?>>> it = typeSet.iterator();
                String packageName = "";
                while (it.hasNext()) {
                    Class<? extends Class<?>> c = it.next();
                    packageName = c.getPackage().getName();
                    if (!filterMap.containsKey(packageName)) {
                        filterMap.put(packageName, packageName);
                        configuration.getTypeAliasRegistry().registerAliases(packageName,
                                typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
                        //System.out.println("Scanned package: '" + packageName + "' for aliases");
                    }
                }
            }
        }
    }

    if (!isEmpty(this.typeAliases)) {
        for (Class<?> typeAlias : this.typeAliases) {
            configuration.getTypeAliasRegistry().registerAlias(typeAlias);
            if (logger.isDebugEnabled()) {
                logger.debug("Registered type alias: '" + typeAlias + "'");
            }
        }
    }

    if (!isEmpty(this.plugins)) {
        for (Interceptor plugin : this.plugins) {
            configuration.addInterceptor(plugin);
            if (logger.isDebugEnabled()) {
                logger.debug("Registered plugin: '" + plugin + "'");
            }
        }
    }

    if (hasLength(this.typeHandlersPackage)) {
        String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeHandlersPackageArray) {
            configuration.getTypeHandlerRegistry().register(packageToScan);
            if (logger.isDebugEnabled()) {
                logger.debug("Scanned package: '" + packageToScan + "' for type handlers");
            }
        }
    }

    if (!isEmpty(this.typeHandlers)) {
        for (TypeHandler<?> typeHandler : this.typeHandlers) {
            configuration.getTypeHandlerRegistry().register(typeHandler);
            if (logger.isDebugEnabled()) {
                logger.debug("Registered type handler: '" + typeHandler + "'");
            }
        }
    }

    if (xmlConfigBuilder != null) {
        try {
            xmlConfigBuilder.parse();

            if (logger.isDebugEnabled()) {
                logger.debug("Parsed configuration file: '" + this.configLocation + "'");
            }
        } catch (Exception ex) {
            throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
        } finally {
            ErrorContext.instance().reset();
        }
    }

    if (this.transactionFactory == null) {
        this.transactionFactory = new SpringManagedTransactionFactory();
    }

    Environment environment = new Environment(this.environment, this.transactionFactory, this.dataSource);
    configuration.setEnvironment(environment);

    if (this.databaseIdProvider != null) {
        try {
            configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
        } catch (SQLException e) {
            throw new NestedIOException("Failed getting a databaseId", e);
        }
    }

    if (!isEmpty(this.mapperLocations)) {
        for (Resource mapperLocation : this.mapperLocations) {
            if (mapperLocation == null) {
                continue;
            }

            try {
                XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
                        configuration, mapperLocation.toString(), configuration.getSqlFragments());
                xmlMapperBuilder.parse();
            } catch (Exception e) {
                throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
            } finally {
                ErrorContext.instance().reset();
            }

            if (logger.isDebugEnabled()) {
                logger.debug("Parsed mapper file: '" + mapperLocation + "'");
            }
        }
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Property 'mapperLocations' was not specified or no matching resources found");
        }
    }

    return this.sqlSessionFactoryBuilder.build(configuration);
}

From source file:com.bstek.dorado.web.loader.DoradoLoader.java

public synchronized void preload(ServletContext servletContext, boolean processOriginContextConfigLocation)
        throws Exception {
    if (preloaded) {
        throw new IllegalStateException("Dorado base configurations already loaded.");
    }//from  w  ww  .  j av  a2  s  .  co m
    preloaded = true;

    // ?
    ConsoleUtils.outputLoadingInfo("Initializing " + DoradoAbout.getProductTitle() + " engine...");
    ConsoleUtils.outputLoadingInfo("[Vendor: " + DoradoAbout.getVendor() + "]");

    ConfigureStore configureStore = Configure.getStore();
    doradoHome = System.getenv("DORADO_HOME");

    // ?DoradoHome
    String intParam;
    intParam = servletContext.getInitParameter("doradoHome");
    if (intParam != null) {
        doradoHome = intParam;
    }
    if (doradoHome == null) {
        doradoHome = DEFAULT_DORADO_HOME;
    }

    configureStore.set(HOME_PROPERTY, doradoHome);
    ConsoleUtils.outputLoadingInfo("[Home: " + StringUtils.defaultString(doradoHome, "<not assigned>") + "]");

    // ResourceLoader
    ResourceLoader resourceLoader = new ServletContextResourceLoader(servletContext) {
        @Override
        public Resource getResource(String resourceLocation) {
            if (resourceLocation != null && resourceLocation.startsWith(HOME_LOCATION_PREFIX)) {
                resourceLocation = ResourceUtils.concatPath(doradoHome,
                        resourceLocation.substring(HOME_LOCATION_PREFIX_LEN));
            }
            return super.getResource(resourceLocation);
        }
    };

    String runMode = null;
    if (StringUtils.isNotEmpty(doradoHome)) {
        String configureLocation = HOME_LOCATION_PREFIX + "configure.properties";
        loadConfigureProperties(configureStore, resourceLoader, configureLocation, false);
    }

    runMode = configureStore.getString("core.runMode");

    if (StringUtils.isNotEmpty(runMode)) {
        loadConfigureProperties(configureStore, resourceLoader,
                CORE_PROPERTIES_LOCATION_PREFIX + "configure-" + runMode + ".properties", true);

        if (StringUtils.isNotEmpty(doradoHome)) {
            loadConfigureProperties(configureStore, resourceLoader,
                    HOME_LOCATION_PREFIX + "configure-" + runMode + ".properties", true);
        }
    }

    ConsoleUtils.outputConfigureItem("core.runMode");
    ConsoleUtils.outputConfigureItem("core.addonLoadMode");

    File tempDir;
    String tempDirPath = configureStore.getString("core.tempDir");
    if (StringUtils.isNotBlank(tempDirPath)) {
        tempDir = new File(tempDirPath);
    } else {
        tempDir = new File(WebUtils.getTempDir(servletContext), ".dorado");
    }

    boolean supportsTempFile = configureStore.getBoolean("core.supportsTempFile");
    TempFileUtils.setSupportsTempFile(supportsTempFile);
    if (supportsTempFile) {
        if ((tempDir.exists() && tempDir.isDirectory()) || tempDir.mkdir()) {
            TempFileUtils.setTempDir(tempDir);
        }
        ConsoleUtils.outputLoadingInfo("[TempDir: " + TempFileUtils.getTempDir().getPath() + "]");
    } else {
        ConsoleUtils.outputLoadingInfo("Temp file is forbidden.");
    }

    // 
    File storeDir;
    String storeDirSettring = configureStore.getString("core.storeDir");
    if (StringUtils.isNotEmpty(storeDirSettring)) {
        storeDir = new File(storeDirSettring);
        File testFile = new File(storeDir, ".test");
        if (!testFile.mkdirs()) {
            throw new IllegalStateException(
                    "Store directory [" + storeDir.getAbsolutePath() + "] is not writable in actually.");
        }
        testFile.delete();
    } else {
        storeDir = new File(tempDir, "dorado-store");
        configureStore.set("core.storeDir", storeDir.getAbsolutePath());
    }
    ConsoleUtils.outputConfigureItem("core.storeDir");

    // gothrough packages
    String addonLoadMode = Configure.getString("core.addonLoadMode");
    String[] enabledAddons = StringUtils.split(Configure.getString("core.enabledAddons"), ",; \n\r");
    String[] disabledAddon = StringUtils.split(Configure.getString("core.disabledAddon"), ",; \n\r");

    Collection<PackageInfo> packageInfos = PackageManager.getPackageInfoMap().values();
    int addonNumber = 0;
    for (PackageInfo packageInfo : packageInfos) {
        String packageName = packageInfo.getName();
        if (packageName.equals("dorado-core")) {
            continue;
        }

        if (addonNumber > 9999) {
            packageInfo.setEnabled(false);
        } else if (StringUtils.isEmpty(addonLoadMode) || "positive".equals(addonLoadMode)) {
            packageInfo.setEnabled(!ArrayUtils.contains(disabledAddon, packageName));
        } else {
            // addonLoadMode == negative
            packageInfo.setEnabled(ArrayUtils.contains(enabledAddons, packageName));
        }

        if (packageInfo.isEnabled()) {
            addonNumber++;
        }
    }

    // print packages
    int i = 0;
    for (PackageInfo packageInfo : packageInfos) {
        ConsoleUtils.outputLoadingInfo(
                StringUtils.rightPad(String.valueOf(++i) + '.', 4) + "Package [" + packageInfo.getName() + " - "
                        + StringUtils.defaultIfBlank(packageInfo.getVersion(), "<Unknown Version>") + "] found."
                        + ((packageInfo.isEnabled() ? "" : " #DISABLED# ")));
    }

    // load packages
    for (PackageInfo packageInfo : packageInfos) {
        if (!packageInfo.isEnabled()) {
            pushLocations(contextLocations, packageInfo.getComponentLocations());
            continue;
        }

        PackageListener packageListener = packageInfo.getListener();
        if (packageListener != null) {
            packageListener.beforeLoadPackage(packageInfo, resourceLoader);
        }

        PackageConfigurer packageConfigurer = packageInfo.getConfigurer();

        if (StringUtils.isNotEmpty(packageInfo.getPropertiesLocations())) {
            for (String location : org.springframework.util.StringUtils.tokenizeToStringArray(
                    packageInfo.getPropertiesLocations(),
                    ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS)) {
                loadConfigureProperties(configureStore, resourceLoader, location, false);
            }
        }

        String[] locations;
        if (packageConfigurer != null) {
            locations = packageConfigurer.getPropertiesConfigLocations(resourceLoader);
            if (locations != null) {
                for (String location : locations) {
                    loadConfigureProperties(configureStore, resourceLoader, location, false);
                }
            }
        }

        // ?Spring?
        pushLocations(contextLocations, packageInfo.getContextLocations());
        if (packageConfigurer != null) {
            locations = packageConfigurer.getContextConfigLocations(resourceLoader);
            if (locations != null) {
                for (String location : locations) {
                    pushLocation(contextLocations, location);
                }
            }
        }

        pushLocations(servletContextLocations, packageInfo.getServletContextLocations());
        if (packageConfigurer != null) {
            locations = packageConfigurer.getServletContextConfigLocations(resourceLoader);
            if (locations != null) {
                for (String location : locations) {
                    pushLocation(servletContextLocations, location);
                }
            }
        }

        packageInfo.setLoaded(true);
    }

    // ?dorado-homepropertiesaddon
    if (StringUtils.isNotEmpty(doradoHome)) {
        String configureLocation = HOME_LOCATION_PREFIX + "configure.properties";
        loadConfigureProperties(configureStore, resourceLoader, configureLocation, true);
        if (StringUtils.isNotEmpty(runMode)) {
            loadConfigureProperties(configureStore, resourceLoader,
                    CORE_PROPERTIES_LOCATION_PREFIX + "configure-" + runMode + ".properties", true);
            loadConfigureProperties(configureStore, resourceLoader,
                    HOME_LOCATION_PREFIX + "configure-" + runMode + ".properties", true);
        }
    }

    Resource resource;

    // context
    if (processOriginContextConfigLocation) {
        intParam = servletContext.getInitParameter(CONTEXT_CONFIG_LOCATION);
        if (intParam != null) {
            pushLocations(contextLocations, intParam);
        }
    }

    resource = resourceLoader.getResource(HOME_CONTEXT_XML);
    if (resource.exists()) {
        pushLocations(contextLocations, HOME_CONTEXT_XML);
    }

    if (StringUtils.isNotEmpty(runMode)) {
        String extHomeContext = HOME_CONTEXT_PREFIX + '-' + runMode + CONTEXT_FILE_EXT;
        resource = resourceLoader.getResource(extHomeContext);
        if (resource.exists()) {
            pushLocations(contextLocations, extHomeContext);
        }
    }

    // servlet-context
    intParam = servletContext.getInitParameter(SERVLET_CONTEXT_CONFIG_LOCATION);
    if (intParam != null) {
        pushLocations(servletContextLocations, intParam);
    }
    resource = resourceLoader.getResource(HOME_SERVLET_CONTEXT_XML);
    if (resource.exists()) {
        pushLocations(servletContextLocations, HOME_SERVLET_CONTEXT_XML);
    }

    if (StringUtils.isNotEmpty(runMode)) {
        String extHomeContext = HOME_SERVLET_CONTEXT_PREFIX + '-' + runMode + CONTEXT_FILE_EXT;
        resource = resourceLoader.getResource(extHomeContext);
        if (resource.exists()) {
            pushLocations(servletContextLocations, extHomeContext);
        }
    }

    ConsoleUtils.outputConfigureItem(RESOURCE_LOADER_PROPERTY);
    ConsoleUtils.outputConfigureItem(BYTE_CODE_PROVIDER_PROPERTY);

    String contextLocationsFromProperties = configureStore.getString(CONTEXT_CONFIG_PROPERTY);
    if (contextLocationsFromProperties != null) {
        pushLocations(contextLocations, contextLocationsFromProperties);
    }
    configureStore.set(CONTEXT_CONFIG_PROPERTY, StringUtils.join(getRealResourcesPath(contextLocations), ';'));
    ConsoleUtils.outputConfigureItem(CONTEXT_CONFIG_PROPERTY);

    String serlvetContextLocationsFromProperties = configureStore.getString(SERVLET_CONTEXT_CONFIG_PROPERTY);
    if (serlvetContextLocationsFromProperties != null) {
        pushLocations(servletContextLocations, serlvetContextLocationsFromProperties);
    }
    configureStore.set(SERVLET_CONTEXT_CONFIG_PROPERTY,
            StringUtils.join(getRealResourcesPath(servletContextLocations), ';'));
    ConsoleUtils.outputConfigureItem(SERVLET_CONTEXT_CONFIG_PROPERTY);

    // ?WebContext
    DoradoContext context = DoradoContext.init(servletContext, false);
    Context.setFailSafeContext(context);
}

From source file:org.danielli.xultimate.orm.mybatis.SqlSessionFactoryBean.java

/**
 * Build a {@code SqlSessionFactory} instance.
 *
 * The default implementation uses the standard MyBatis {@code XMLConfigBuilder} API to build a
 * {@code SqlSessionFactory} instance based on an Reader.
 *
 * @return SqlSessionFactory/*w w w  .  j a  va  2  s. c  o  m*/
 * @throws IOException if loading the config file failed
 */
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
    if (this.configuration == null) {
        this.configuration = new Configuration();
    }
    //    Configuration configuration;

    //    XMLConfigBuilder xmlConfigBuilder = null;
    //    if (this.configLocation != null) {
    //      xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties);
    //      configuration = xmlConfigBuilder.getConfiguration();
    //    } else {
    //      if (logger.isDebugEnabled()) {
    //        logger.debug("Property 'configLocation' not specified, using default MyBatis Configuration");
    //      }
    //      configuration = new Configuration();
    //      configuration.setVariables(this.configurationProperties);
    //    }

    if (this.objectFactory != null) {
        configuration.setObjectFactory(this.objectFactory);
    }

    if (this.objectWrapperFactory != null) {
        configuration.setObjectWrapperFactory(this.objectWrapperFactory);
    }

    if (hasLength(this.typeAliasesPackage)) {
        String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeAliasPackageArray) {
            configuration.getTypeAliasRegistry().registerAliases(packageToScan,
                    typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
            if (logger.isDebugEnabled()) {
                logger.debug("Scanned package: '" + packageToScan + "' for aliases");
            }
        }
    }

    if (!isEmpty(this.typeAliases)) {
        for (Class<?> typeAlias : this.typeAliases) {
            configuration.getTypeAliasRegistry().registerAlias(typeAlias);
            if (logger.isDebugEnabled()) {
                logger.debug("Registered type alias: '" + typeAlias + "'");
            }
        }
    }

    if (!isEmpty(this.plugins)) {
        for (Interceptor plugin : this.plugins) {
            configuration.addInterceptor(plugin);
            if (logger.isDebugEnabled()) {
                logger.debug("Registered plugin: '" + plugin + "'");
            }
        }
    }

    if (hasLength(this.typeHandlersPackage)) {
        String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeHandlersPackageArray) {
            configuration.getTypeHandlerRegistry().register(packageToScan);
            if (logger.isDebugEnabled()) {
                logger.debug("Scanned package: '" + packageToScan + "' for type handlers");
            }
        }
    }

    if (!isEmpty(this.typeHandlers)) {
        for (TypeHandler<?> typeHandler : this.typeHandlers) {
            configuration.getTypeHandlerRegistry().register(typeHandler);
            if (logger.isDebugEnabled()) {
                logger.debug("Registered type handler: '" + typeHandler + "'");
            }
        }
    }

    //    if (xmlConfigBuilder != null) {
    //      try {
    //        xmlConfigBuilder.parse();
    //
    //        if (logger.isDebugEnabled()) {
    //          logger.debug("Parsed configuration file: '" + this.configLocation + "'");
    //        }
    //      } catch (Exception ex) {
    //        throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
    //      } finally {
    //        ErrorContext.instance().reset();
    //      }
    //    }

    if (this.transactionFactory == null) {
        this.transactionFactory = new SpringManagedTransactionFactory();
    }

    Environment environment = new Environment(this.environment, this.transactionFactory, this.dataSource);
    configuration.setEnvironment(environment);

    if (this.databaseIdProvider != null) {
        try {
            configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
        } catch (SQLException e) {
            throw new NestedIOException("Failed getting a databaseId", e);
        }
    }

    if (!isEmpty(this.mapperLocations)) {
        for (Resource mapperLocation : this.mapperLocations) {
            if (mapperLocation == null) {
                continue;
            }

            try {
                XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
                        configuration, mapperLocation.toString(), configuration.getSqlFragments());
                xmlMapperBuilder.parse();
            } catch (Exception e) {
                throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
            } finally {
                ErrorContext.instance().reset();
            }

            if (logger.isDebugEnabled()) {
                logger.debug("Parsed mapper file: '" + mapperLocation + "'");
            }
        }
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Property 'mapperLocations' was not specified or no matching resources found");
        }
    }

    return this.sqlSessionFactoryBuilder.build(configuration);
}

From source file:net.butfly.albacore.spring.AutoSqlSessionFactoryBean.java

/**
 * Build a {@code SqlSessionFactory} instance.
 *
 * The default implementation uses the standard MyBatis {@code XMLConfigBuilder} API to build a
 * {@code SqlSessionFactory} instance based on an Reader.
 *
 * @return SqlSessionFactory/*from  ww  w .  j a  va  2  s . co m*/
 * @throws IOException
 *             if loading the config file failed
 */
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
    Configuration configuration;
    XMLConfigBuilder xmlConfigBuilder = null;
    if (this.configLocation == null)
        this.configLocation = Springs
                .searchResource(null == this.mybatisConfigLocationPattern ? DEFALUT_MYBATIS_CONF_PATTERN
                        : this.mybatisConfigLocationPattern);
    if (this.configLocation != null) {
        xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null,
                this.configurationProperties);
        configuration = xmlConfigBuilder.getConfiguration();
        if (logger.isTraceEnabled())
            logger.trace("Albacore Impl for Mybatis AutoSqlSessionFactoryBean - Mybatis configuration loaded: "
                    + this.configLocation);
    } else {
        logger.warn(
                "Albacore Impl for Mybatis AutoSqlSessionFactoryBean - Property 'configLocation' not specified, using default MyBatis Configuration");
        configuration = new Configuration();
        configuration.setVariables(this.configurationProperties);
    }
    this.fillMybatisProperties(configuration);
    if (this.objectFactory != null)
        configuration.setObjectFactory(this.objectFactory);
    if (this.objectWrapperFactory != null)
        configuration.setObjectWrapperFactory(this.objectWrapperFactory);
    if (hasLength(this.typeAliasesPackage)) {
        String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeAliasPackageArray) {
            configuration.getTypeAliasRegistry().registerAliases(packageToScan,
                    typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
            if (logger.isTraceEnabled())
                logger.trace("Albacore Impl for Mybatis AutoSqlSessionFactoryBean - Scanned package: '"
                        + packageToScan + "' for aliases");
        }
    }
    if (!isEmpty(this.typeAliases))
        for (Class<?> typeAlias : this.typeAliases) {
            configuration.getTypeAliasRegistry().registerAlias(typeAlias);
            if (logger.isTraceEnabled())
                logger.trace("Albacore Impl for Mybatis AutoSqlSessionFactoryBean - Registered type alias: '"
                        + typeAlias + "'");
        }
    if (!isEmpty(this.plugins))
        for (Interceptor plugin : this.plugins) {
            configuration.addInterceptor(plugin);
            if (logger.isTraceEnabled())
                logger.trace("Albacore Impl for Mybatis AutoSqlSessionFactoryBean - Registered plugin: '"
                        + plugin + "'");
        }

    if (hasLength(this.typeHandlersPackage)) {
        String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeHandlersPackageArray) {
            configuration.getTypeHandlerRegistry().register(packageToScan);
            if (logger.isTraceEnabled()) {
                logger.trace("Albacore Impl for Mybatis AutoSqlSessionFactoryBean - Scanned package: '"
                        + packageToScan + "' for type handlers");
            }
        }
    }
    if (!isEmpty(this.typeHandlers)) {
        for (TypeHandler<?> typeHandler : this.typeHandlers) {
            configuration.getTypeHandlerRegistry().register(typeHandler);
            if (logger.isTraceEnabled()) {
                logger.trace("Albacore Impl for Mybatis AutoSqlSessionFactoryBean - Registered type handler: '"
                        + typeHandler + "'");
            }
        }
    }
    if (xmlConfigBuilder != null)
        try {
            xmlConfigBuilder.parse();
            if (logger.isTraceEnabled()) {
                logger.trace(
                        "Albacore Impl for Mybatis AutoSqlSessionFactoryBean - Parsed configuration file: '"
                                + this.configLocation + "'");
            }
        } catch (Exception ex) {
            throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
        } finally {
            ErrorContext.instance().reset();
        }
    if (this.transactionFactory == null)
        this.transactionFactory = new SpringManagedTransactionFactory();
    Environment environment = new Environment(this.environment, this.transactionFactory, this.dataSource);
    configuration.setEnvironment(environment);
    if (this.databaseIdProvider != null)
        try {
            configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
        } catch (SQLException e) {
            throw new NestedIOException("Failed getting a databaseId", e);
        }

    if (!isEmpty(this.mapperLocations)) {
        for (Resource mapperLocation : this.mapperLocations)
            if (mapperLocation != null)
                this.registerMapper(configuration, mapperLocation);
    } else if (logger.isTraceEnabled())
        logger.trace(
                "Albacore Impl for Mybatis AutoSqlSessionFactoryBean - Property 'mapperLocations' was not specified or no matching resources found");
    Resource mapperCache = Springs
            .searchResource(null == this.mybatisCacheConfigLocationPattern ? DEFAULT_MYBATIS_CACHE_CONF_PATTERN
                    : this.mybatisCacheConfigLocationPattern);
    if (mapperCache != null)
        this.registerMapper(configuration, mapperCache);
    return this.sqlSessionFactoryBuilder.build(configuration);
}

From source file:org.alfresco.ibatis.HierarchicalSqlSessionFactoryBean.java

/**
 * Build a {@code SqlSessionFactory} instance.
 * <p/>/* w  w  w .  j  av  a2s .  c o m*/
 * The default implementation uses the standard MyBatis {@code XMLConfigBuilder} API to build a
 * {@code SqlSessionFactory} instance based on an Reader.
 *
 * @return SqlSessionFactory
 * @throws IOException if loading the config file failed
 */
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {

    Configuration configuration;

    HierarchicalXMLConfigBuilder xmlConfigBuilder = null;
    if (this.configLocation != null) {
        try {
            xmlConfigBuilder = new HierarchicalXMLConfigBuilder(resourceLoader,
                    this.configLocation.getInputStream(), null, this.configurationProperties);
            configuration = xmlConfigBuilder.getConfiguration();
        } catch (Exception ex) {
            throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
        } finally {
            ErrorContext.instance().reset();
        }
    } else {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Property 'configLocation' not specified, using default MyBatis Configuration");
        }
        configuration = new Configuration();
        configuration.setVariables(this.configurationProperties);
    }

    if (this.objectFactory != null) {
        configuration.setObjectFactory(this.objectFactory);
    }

    if (this.objectWrapperFactory != null) {
        configuration.setObjectWrapperFactory(this.objectWrapperFactory);
    }

    if (hasLength(this.typeAliasesPackage)) {
        String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeAliasPackageArray) {
            configuration.getTypeAliasRegistry().registerAliases(packageToScan,
                    typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Scanned package: '" + packageToScan + "' for aliases");
            }
        }
    }

    if (!isEmpty(this.typeAliases)) {
        for (Class<?> typeAlias : this.typeAliases) {
            configuration.getTypeAliasRegistry().registerAlias(typeAlias);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Registered type alias: '" + typeAlias + "'");
            }
        }
    }

    if (!isEmpty(this.plugins)) {
        for (Interceptor plugin : this.plugins) {
            configuration.addInterceptor(plugin);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Registered plugin: '" + plugin + "'");
            }
        }
    }

    if (hasLength(this.typeHandlersPackage)) {
        String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeHandlersPackageArray) {
            configuration.getTypeHandlerRegistry().register(packageToScan);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Scanned package: '" + packageToScan + "' for type handlers");
            }
        }
    }

    if (!isEmpty(this.typeHandlers)) {
        for (TypeHandler<?> typeHandler : this.typeHandlers) {
            configuration.getTypeHandlerRegistry().register(typeHandler);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Registered type handler: '" + typeHandler + "'");
            }
        }
    }

    if (xmlConfigBuilder != null) {
        try {
            xmlConfigBuilder.parse();

            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Parsed configuration file: '" + this.configLocation + "'");
            }
        } catch (Exception ex) {
            throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
        } finally {
            ErrorContext.instance().reset();
        }
    }

    if (this.transactionFactory == null) {
        this.transactionFactory = new SpringManagedTransactionFactory();
    }

    Environment environment = new Environment(this.environment, this.transactionFactory, this.dataSource);
    configuration.setEnvironment(environment);

    //Commented out to be able to use dummy dataSource in tests.
    /*
    if (this.databaseIdProvider != null) {
    try {
        configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
    } catch (SQLException e) {
        throw new NestedIOException("Failed getting a databaseId", e);
    }
    }
    */

    if (!isEmpty(this.mapperLocations)) {
        for (Resource mapperLocation : this.mapperLocations) {
            if (mapperLocation == null) {
                continue;
            }

            try {
                XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
                        configuration, mapperLocation.toString(), configuration.getSqlFragments());
                xmlMapperBuilder.parse();
            } catch (Exception e) {
                throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
            } finally {
                ErrorContext.instance().reset();
            }

            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Parsed mapper file: '" + mapperLocation + "'");
            }
        }
    } else {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug(
                    "Property 'mapperLocations' was not specified, only MyBatis mapper files specified in the config xml were loaded");
        }
    }

    return this.sqlSessionFactoryBuilder.build(configuration);
}

From source file:org.mybatis.spring.SqlSessionFactoryBean.java

/**
 * Build a {@code SqlSessionFactory} instance.
 *
 * The default implementation uses the standard MyBatis {@code XMLConfigBuilder} API to build a
 * {@code SqlSessionFactory} instance based on an Reader.
 * Since 1.3.0, it can be specified a {@link Configuration} instance directly(without config file).
 *
 * @return SqlSessionFactory//  w ww  . ja  v  a 2s . c o m
 * @throws IOException if loading the config file failed
 */
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {

    Configuration configuration;

    XMLConfigBuilder xmlConfigBuilder = null;
    if (this.configuration != null) {
        configuration = this.configuration;
        if (configuration.getVariables() == null) {
            configuration.setVariables(this.configurationProperties);
        } else if (this.configurationProperties != null) {
            configuration.getVariables().putAll(this.configurationProperties);
        }
    } else if (this.configLocation != null) {
        xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null,
                this.configurationProperties);
        configuration = xmlConfigBuilder.getConfiguration();
    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(
                    "Property `configuration` or 'configLocation' not specified, using default MyBatis Configuration");
        }
        configuration = new Configuration();
        configuration.setVariables(this.configurationProperties);
    }

    if (this.objectFactory != null) {
        configuration.setObjectFactory(this.objectFactory);
    }

    if (this.objectWrapperFactory != null) {
        configuration.setObjectWrapperFactory(this.objectWrapperFactory);
    }

    if (this.vfs != null) {
        configuration.setVfsImpl(this.vfs);
    }

    if (hasLength(this.typeAliasesPackage)) {
        String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeAliasPackageArray) {
            configuration.getTypeAliasRegistry().registerAliases(packageToScan,
                    typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Scanned package: '" + packageToScan + "' for aliases");
            }
        }
    }

    if (!isEmpty(this.typeAliases)) {
        for (Class<?> typeAlias : this.typeAliases) {
            configuration.getTypeAliasRegistry().registerAlias(typeAlias);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Registered type alias: '" + typeAlias + "'");
            }
        }
    }

    if (!isEmpty(this.plugins)) {
        for (Interceptor plugin : this.plugins) {
            configuration.addInterceptor(plugin);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Registered plugin: '" + plugin + "'");
            }
        }
    }

    if (hasLength(this.typeHandlersPackage)) {
        String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeHandlersPackageArray) {
            configuration.getTypeHandlerRegistry().register(packageToScan);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Scanned package: '" + packageToScan + "' for type handlers");
            }
        }
    }

    if (!isEmpty(this.typeHandlers)) {
        for (TypeHandler<?> typeHandler : this.typeHandlers) {
            configuration.getTypeHandlerRegistry().register(typeHandler);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Registered type handler: '" + typeHandler + "'");
            }
        }
    }

    if (this.databaseIdProvider != null) {//fix #64 set databaseId before parse mapper xmls
        try {
            configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
        } catch (SQLException e) {
            throw new NestedIOException("Failed getting a databaseId", e);
        }
    }

    if (this.cache != null) {
        configuration.addCache(this.cache);
    }

    if (xmlConfigBuilder != null) {
        try {
            xmlConfigBuilder.parse();

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Parsed configuration file: '" + this.configLocation + "'");
            }
        } catch (Exception ex) {
            throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
        } finally {
            ErrorContext.instance().reset();
        }
    }

    if (this.transactionFactory == null) {
        this.transactionFactory = new SpringManagedTransactionFactory();
    }

    configuration.setEnvironment(new Environment(this.environment, this.transactionFactory, this.dataSource));

    if (!isEmpty(this.mapperLocations)) {
        for (Resource mapperLocation : this.mapperLocations) {
            if (mapperLocation == null) {
                continue;
            }

            try {
                XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
                        configuration, mapperLocation.toString(), configuration.getSqlFragments());
                xmlMapperBuilder.parse();
            } catch (Exception e) {
                throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
            } finally {
                ErrorContext.instance().reset();
            }

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Parsed mapper file: '" + mapperLocation + "'");
            }
        }
    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Property 'mapperLocations' was not specified or no matching resources found");
        }
    }

    return this.sqlSessionFactoryBuilder.build(configuration);
}

From source file:com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean.java

/**
 * Build a {@code SqlSessionFactory} instance.
 *
 * The default implementation uses the standard MyBatis
 * {@code XMLConfigBuilder} API to build a {@code SqlSessionFactory}
 * instance based on an Reader. Since 1.3.0, it can be specified a
 * {@link Configuration} instance directly(without config file).
 *
 * @return SqlSessionFactory//from   w  ww . j a v a2s . c o  m
 * @throws IOException
 *             if loading the config file failed
 */
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {

    Configuration configuration;

    // TODO  MybatisXmlConfigBuilder
    MybatisXMLConfigBuilder xmlConfigBuilder = null;
    if (this.configuration != null) {
        configuration = this.configuration;
        if (configuration.getVariables() == null) {
            configuration.setVariables(this.configurationProperties);
        } else if (this.configurationProperties != null) {
            configuration.getVariables().putAll(this.configurationProperties);
        }
    } else if (this.configLocation != null) {
        xmlConfigBuilder = new MybatisXMLConfigBuilder(this.configLocation.getInputStream(), null,
                this.configurationProperties);
        configuration = xmlConfigBuilder.getConfiguration();
    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(
                    "Property `configuration` or 'configLocation' not specified, using default MyBatis Configuration");
        }
        // TODO ?
        configuration = new MybatisConfiguration();
        configuration.setVariables(this.configurationProperties);
    }

    if (this.objectFactory != null) {
        configuration.setObjectFactory(this.objectFactory);
    }

    if (this.objectWrapperFactory != null) {
        configuration.setObjectWrapperFactory(this.objectWrapperFactory);
    }

    if (this.vfs != null) {
        configuration.setVfsImpl(this.vfs);
    }

    if (hasLength(this.typeAliasesPackage)) {
        // TODO
        String[] typeAliasPackageArray = null;
        if (typeAliasesPackage.contains("*")) {
            typeAliasPackageArray = PackageHelper.convertTypeAliasesPackage(typeAliasesPackage);
        } else {
            typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
                    ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        }
        if (typeAliasPackageArray == null) {
            throw new MybatisPlusException("not find typeAliasesPackage:" + typeAliasesPackage);
        }
        for (String packageToScan : typeAliasPackageArray) {
            configuration.getTypeAliasRegistry().registerAliases(packageToScan,
                    typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Scanned package: '" + packageToScan + "' for aliases");
            }
        }
    }

    if (!isEmpty(this.typeAliases)) {
        for (Class<?> typeAlias : this.typeAliases) {
            configuration.getTypeAliasRegistry().registerAlias(typeAlias);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Registered type alias: '" + typeAlias + "'");
            }
        }
    }

    if (!isEmpty(this.plugins)) {
        for (Interceptor plugin : this.plugins) {
            configuration.addInterceptor(plugin);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Registered plugin: '" + plugin + "'");
            }
        }
    }

    if (hasLength(this.typeHandlersPackage)) {
        String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeHandlersPackageArray) {
            configuration.getTypeHandlerRegistry().register(packageToScan);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Scanned package: '" + packageToScan + "' for type handlers");
            }
        }
    }

    if (!isEmpty(this.typeHandlers)) {
        for (TypeHandler<?> typeHandler : this.typeHandlers) {
            configuration.getTypeHandlerRegistry().register(typeHandler);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Registered type handler: '" + typeHandler + "'");
            }
        }
    }

    if (this.databaseIdProvider != null) {// fix #64 set databaseId before
        // parse mapper xmls
        try {
            configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
        } catch (SQLException e) {
            throw new NestedIOException("Failed getting a databaseId", e);
        }
    }

    if (this.cache != null) {
        configuration.addCache(this.cache);
    }

    if (xmlConfigBuilder != null) {
        try {
            xmlConfigBuilder.parse();

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Parsed configuration file: '" + this.configLocation + "'");
            }
        } catch (Exception ex) {
            throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
        } finally {
            ErrorContext.instance().reset();
        }
    }

    if (this.transactionFactory == null) {
        this.transactionFactory = new SpringManagedTransactionFactory();
    }

    configuration.setEnvironment(new Environment(this.environment, this.transactionFactory, this.dataSource));
    // TODO ?
    if (globalConfig.isAutoSetDbType()) {
        try {
            String jdbcUrl = dataSource.getConnection().getMetaData().getURL();
            globalConfig.setDbTypeByJdbcUrl(jdbcUrl);
        } catch (SQLException e) {
            LOGGER.warn("Warn: Auto Set DbType Fail !  Cause:" + e);
        }
    }
    SqlSessionFactory sqlSessionFactory = this.sqlSessionFactoryBuilder.build(configuration);
    // TODO  sqlSessionFactory
    globalConfig.setSqlSessionFactory(sqlSessionFactory);
    // TODO ?
    globalConfig.setGlobalConfig(configuration);

    if (!isEmpty(this.mapperLocations)) {
        for (Resource mapperLocation : this.mapperLocations) {
            if (mapperLocation == null) {
                continue;
            }

            try {
                // TODO
                MybatisXMLMapperBuilder xmlMapperBuilder = new MybatisXMLMapperBuilder(
                        mapperLocation.getInputStream(), configuration, mapperLocation.toString(),
                        configuration.getSqlFragments());
                xmlMapperBuilder.parse();

            } catch (Exception e) {
                throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
            } finally {
                ErrorContext.instance().reset();
            }

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Parsed mapper file: '" + mapperLocation + "'");
            }
        }
    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Property 'mapperLocations' was not specified or no matching resources found");
        }
    }
    return sqlSessionFactory;
}

From source file:com.zyf.framework.plugin.SqlSessionFactoryBean.java

/**
 * Build a {@code SqlSessionFactory} instance.
 *
 * The default implementation uses the standard MyBatis {@code XMLConfigBuilder} API to build a
 * {@code SqlSessionFactory} instance based on an Reader.
 * Since 1.3.0, it can be specified a {@link Configuration} instance directly(without config file).
 *
 * @return SqlSessionFactory/*from   w ww  .  j  a v  a2 s  . c  o m*/
 * @throws IOException if loading the config file failed
 */
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {

    Configuration configuration;

    XMLConfigBuilder xmlConfigBuilder = null;
    if (this.configuration != null) {
        configuration = this.configuration;
        if (configuration.getVariables() == null) {
            configuration.setVariables(this.configurationProperties);
        } else if (this.configurationProperties != null) {
            configuration.getVariables().putAll(this.configurationProperties);
        }
    } else if (this.configLocation != null) {
        xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null,
                this.configurationProperties);
        configuration = xmlConfigBuilder.getConfiguration();
    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(
                    "Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration");
        }
        configuration = new Configuration();
        if (this.configurationProperties != null) {
            configuration.setVariables(this.configurationProperties);
        }
    }

    if (this.objectFactory != null) {
        configuration.setObjectFactory(this.objectFactory);
    }

    if (this.objectWrapperFactory != null) {
        configuration.setObjectWrapperFactory(this.objectWrapperFactory);
    }

    if (this.vfs != null) {
        configuration.setVfsImpl(this.vfs);
    }

    if (hasLength(this.typeAliasesPackage)) {
        String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeAliasPackageArray) {
            configuration.getTypeAliasRegistry().registerAliases(packageToScan,
                    typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Scanned package: '" + packageToScan + "' for aliases");
            }
        }
    }

    if (!isEmpty(this.typeAliases)) {
        for (Class<?> typeAlias : this.typeAliases) {
            configuration.getTypeAliasRegistry().registerAlias(typeAlias);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Registered type alias: '" + typeAlias + "'");
            }
        }
    }

    if (!isEmpty(this.plugins)) {
        for (Interceptor plugin : this.plugins) {
            configuration.addInterceptor(plugin);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Registered plugin: '" + plugin + "'");
            }
        }
    }

    if (hasLength(this.typeHandlersPackage)) {
        String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeHandlersPackageArray) {
            configuration.getTypeHandlerRegistry().register(packageToScan);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Scanned package: '" + packageToScan + "' for type handlers");
            }
        }
    }

    if (!isEmpty(this.typeHandlers)) {
        for (TypeHandler<?> typeHandler : this.typeHandlers) {

            //TODO:
            ClazzTypeScan cts = typeHandler.getClass().getAnnotation(ClazzTypeScan.class);
            if (cts == null) {
                configuration.getTypeHandlerRegistry().register(typeHandler);
            } else {
                TypeScan typescan = cts.value();
                if (typeScanMap.containsKey(typescan.name())) {
                    String scanpath = typeScanMap.get(typescan.name());

                    ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
                    resolverUtil.find(new ResolverUtil.IsA(DescriptionID.class), scanpath);
                    Set<Class<? extends Class<?>>> handlerSet = resolverUtil.getClasses();
                    for (Class<?> type : handlerSet) {
                        try {
                            Constructor<?> c = typeHandler.getClass().getConstructor(Class.class);
                            c.newInstance(type);
                        } catch (NoSuchMethodException | SecurityException e) {
                            e.printStackTrace();
                        } catch (InstantiationException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }

                        if (!type.isAnonymousClass() && !type.isInterface()
                                && !Modifier.isAbstract(type.getModifiers())) {
                            configuration.getTypeHandlerRegistry().register(type, typeHandler.getClass());
                        }
                    }

                }
            }

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Registered type handler: '" + typeHandler + "'");
            }
        }
    }

    if (this.databaseIdProvider != null) {//fix #64 set databaseId before parse mapper xmls
        try {
            configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
        } catch (SQLException e) {
            throw new NestedIOException("Failed getting a databaseId", e);
        }
    }

    if (this.cache != null) {
        configuration.addCache(this.cache);
    }

    if (xmlConfigBuilder != null) {
        try {
            xmlConfigBuilder.parse();

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Parsed configuration file: '" + this.configLocation + "'");
            }
        } catch (Exception ex) {
            throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
        } finally {
            ErrorContext.instance().reset();
        }
    }

    if (this.transactionFactory == null) {
        this.transactionFactory = new SpringManagedTransactionFactory();
    }

    configuration.setEnvironment(new Environment(this.environment, this.transactionFactory, this.dataSource));

    if (!isEmpty(this.mapperLocations)) {
        for (Resource mapperLocation : this.mapperLocations) {
            if (mapperLocation == null) {
                continue;
            }

            try {
                XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
                        configuration, mapperLocation.toString(), configuration.getSqlFragments());
                xmlMapperBuilder.parse();
            } catch (Exception e) {
                throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
            } finally {
                ErrorContext.instance().reset();
            }

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Parsed mapper file: '" + mapperLocation + "'");
            }
        }

        // ThinkGem ?MapperXML?
        if (mapperRefresh.isEnabled()) {
            System.out.println("mapperRefresh loading.............");
            mapperRefresh.setConfiguration(configuration);
            mapperRefresh.setMapperLocations(mapperLocations);
            mapperRefresh.run();
        }

    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Property 'mapperLocations' was not specified or no matching resources found");
        }
    }

    return this.sqlSessionFactoryBuilder.build(configuration);
}