Search in sources :

Example 1 with XMLConfigBuilder

use of org.apache.ibatis.builder.xml.XMLConfigBuilder in project camunda-bpm-platform by camunda.

the class ProcessEngineConfigurationImpl method initSqlSessionFactory.

protected void initSqlSessionFactory() {
    // to protect access to cachedSqlSessionFactory see CAM-6682
    synchronized (ProcessEngineConfigurationImpl.class) {
        if (isUseSharedSqlSessionFactory) {
            sqlSessionFactory = cachedSqlSessionFactory;
        }
        if (sqlSessionFactory == null) {
            InputStream inputStream = null;
            try {
                inputStream = getMyBatisXmlConfigurationSteam();
                // update the jdbc parameters to the configured ones...
                Environment environment = new Environment("default", transactionFactory, dataSource);
                Reader reader = new InputStreamReader(inputStream);
                Properties properties = new Properties();
                if (isUseSharedSqlSessionFactory) {
                    properties.put("prefix", "${@org.camunda.bpm.engine.impl.context.Context@getProcessEngineConfiguration().databaseTablePrefix}");
                } else {
                    properties.put("prefix", databaseTablePrefix);
                }
                initSqlSessionFactoryProperties(properties, databaseTablePrefix, databaseType);
                XMLConfigBuilder parser = new XMLConfigBuilder(reader, "", properties);
                Configuration configuration = parser.getConfiguration();
                configuration.setEnvironment(environment);
                configuration = parser.parse();
                configuration.setDefaultStatementTimeout(jdbcStatementTimeout);
                if (isJdbcBatchProcessing()) {
                    configuration.setDefaultExecutorType(ExecutorType.BATCH);
                }
                sqlSessionFactory = new DefaultSqlSessionFactory(configuration);
                if (isUseSharedSqlSessionFactory) {
                    cachedSqlSessionFactory = sqlSessionFactory;
                }
            } catch (Exception e) {
                throw new ProcessEngineException("Error while building ibatis SqlSessionFactory: " + e.getMessage(), e);
            } finally {
                IoUtil.closeSilently(inputStream);
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Configuration(org.apache.ibatis.session.Configuration) ProcessEngineConfiguration(org.camunda.bpm.engine.ProcessEngineConfiguration) DmnEngineConfiguration(org.camunda.bpm.dmn.engine.DmnEngineConfiguration) DefaultDmnEngineConfiguration(org.camunda.bpm.dmn.engine.impl.DefaultDmnEngineConfiguration) InputStream(java.io.InputStream) DefaultSqlSessionFactory(org.apache.ibatis.session.defaults.DefaultSqlSessionFactory) Environment(org.apache.ibatis.mapping.Environment) ScriptingEnvironment(org.camunda.bpm.engine.impl.scripting.env.ScriptingEnvironment) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) XMLConfigBuilder(org.apache.ibatis.builder.xml.XMLConfigBuilder) Properties(java.util.Properties) ProcessEngineException(org.camunda.bpm.engine.ProcessEngineException) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) ProcessEngineException(org.camunda.bpm.engine.ProcessEngineException)

Example 2 with XMLConfigBuilder

use of org.apache.ibatis.builder.xml.XMLConfigBuilder in project Activiti by Activiti.

the class ProcessEngineConfigurationImpl method initMybatisConfiguration.

public Configuration initMybatisConfiguration(Environment environment, Reader reader, Properties properties) {
    XMLConfigBuilder parser = new XMLConfigBuilder(reader, "", properties);
    Configuration configuration = parser.getConfiguration();
    if (databaseType != null) {
        configuration.setDatabaseId(databaseType);
    }
    configuration.setEnvironment(environment);
    initMybatisTypeHandlers(configuration);
    initCustomMybatisMappers(configuration);
    configuration = parseMybatisConfiguration(configuration, parser);
    return configuration;
}
Also used : ProcessEngineConfiguration(org.activiti.engine.ProcessEngineConfiguration) Configuration(org.apache.ibatis.session.Configuration) XMLConfigBuilder(org.apache.ibatis.builder.xml.XMLConfigBuilder)

Example 3 with XMLConfigBuilder

use of org.apache.ibatis.builder.xml.XMLConfigBuilder in project gocd by gocd.

the class SqlSessionFactoryBean method buildSqlSessionFactory.

private SqlSessionFactory buildSqlSessionFactory() throws IOException {
    SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
    XMLConfigBuilder xmlConfigBuilder = new XMLConfigBuilder(configLocation.getInputStream());
    Configuration configuration = xmlConfigBuilder.getConfiguration();
    configuration.setEnvironment(new Environment(getClass().getSimpleName(), new SpringManagedTransactionFactory(), this.dataSource));
    xmlConfigBuilder.parse();
    return factoryBuilder.build(configuration);
}
Also used : Configuration(org.apache.ibatis.session.Configuration) XMLConfigBuilder(org.apache.ibatis.builder.xml.XMLConfigBuilder) Environment(org.apache.ibatis.mapping.Environment) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) SpringManagedTransactionFactory(org.mybatis.spring.transaction.SpringManagedTransactionFactory)

Example 4 with XMLConfigBuilder

use of org.apache.ibatis.builder.xml.XMLConfigBuilder in project mybatis-3 by mybatis.

the class XmlConfigBuilderTest method shouldSuccessfullyLoadXMLConfigFile.

@Test
void shouldSuccessfullyLoadXMLConfigFile() throws Exception {
    // System.setProperty(XPathParser.KEY_USE_XSD, "true");
    String resource = "org/apache/ibatis/builder/xsd/CustomizedSettingsMapperConfig.xml";
    try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
        XMLConfigBuilder builder = new XMLConfigBuilder(inputStream);
        Configuration config = builder.parse();
        assertEquals(AutoMappingBehavior.NONE, config.getAutoMappingBehavior());
        assertEquals(AutoMappingUnknownColumnBehavior.WARNING, config.getAutoMappingUnknownColumnBehavior());
        assertFalse(config.isCacheEnabled());
        assertTrue(config.getProxyFactory() instanceof CglibProxyFactory);
        assertTrue(config.isLazyLoadingEnabled());
        assertTrue(config.isAggressiveLazyLoading());
        assertFalse(config.isMultipleResultSetsEnabled());
        assertFalse(config.isUseColumnLabel());
        assertTrue(config.isUseGeneratedKeys());
        assertEquals(ExecutorType.BATCH, config.getDefaultExecutorType());
        assertEquals(Integer.valueOf(10), config.getDefaultStatementTimeout());
        assertEquals(Integer.valueOf(100), config.getDefaultFetchSize());
        assertTrue(config.isMapUnderscoreToCamelCase());
        assertTrue(config.isSafeRowBoundsEnabled());
        assertEquals(LocalCacheScope.STATEMENT, config.getLocalCacheScope());
        assertEquals(JdbcType.NULL, config.getJdbcTypeForNull());
        assertEquals(new HashSet<>(Arrays.asList("equals", "clone", "hashCode", "toString", "xxx")), config.getLazyLoadTriggerMethods());
        assertFalse(config.isSafeResultHandlerEnabled());
        assertTrue(config.getDefaultScriptingLanguageInstance() instanceof RawLanguageDriver);
        assertTrue(config.isCallSettersOnNulls());
        assertEquals("mybatis_", config.getLogPrefix());
        assertEquals(Slf4jImpl.class.getName(), config.getLogImpl().getName());
        assertEquals(JBoss6VFS.class.getName(), config.getVfsImpl().getName());
        assertEquals(String.class.getName(), config.getConfigurationFactory().getName());
        assertTrue(config.isShrinkWhitespacesInSql());
        assertEquals(Author.class, config.getTypeAliasRegistry().getTypeAliases().get("blogauthor"));
        assertEquals(Blog.class, config.getTypeAliasRegistry().getTypeAliases().get("blog"));
        assertEquals(Cart.class, config.getTypeAliasRegistry().getTypeAliases().get("cart"));
        assertTrue(config.getTypeHandlerRegistry().getTypeHandler(Integer.class) instanceof CustomIntegerTypeHandler);
        assertTrue(config.getTypeHandlerRegistry().getTypeHandler(Long.class) instanceof CustomLongTypeHandler);
        assertTrue(config.getTypeHandlerRegistry().getTypeHandler(String.class) instanceof CustomStringTypeHandler);
        assertTrue(config.getTypeHandlerRegistry().getTypeHandler(String.class, JdbcType.VARCHAR) instanceof CustomStringTypeHandler);
        ExampleObjectFactory objectFactory = (ExampleObjectFactory) config.getObjectFactory();
        assertEquals(1, objectFactory.getProperties().size());
        assertEquals("100", objectFactory.getProperties().getProperty("objectFactoryProperty"));
        assertTrue(config.getObjectWrapperFactory() instanceof CustomObjectWrapperFactory);
        assertTrue(config.getReflectorFactory() instanceof CustomReflectorFactory);
        ExamplePlugin plugin = (ExamplePlugin) config.getInterceptors().get(0);
        assertEquals(1, plugin.getProperties().size());
        assertEquals("100", plugin.getProperties().getProperty("pluginProperty"));
        Environment environment = config.getEnvironment();
        assertEquals("development", environment.getId());
        assertTrue(environment.getDataSource() instanceof UnpooledDataSource);
        assertTrue(environment.getTransactionFactory() instanceof JdbcTransactionFactory);
        assertEquals("derby", config.getDatabaseId());
        assertEquals(4, config.getMapperRegistry().getMappers().size());
        assertTrue(config.getMapperRegistry().hasMapper(CachedAuthorMapper.class));
        assertTrue(config.getMapperRegistry().hasMapper(CustomMapper.class));
        assertTrue(config.getMapperRegistry().hasMapper(BlogMapper.class));
        assertTrue(config.getMapperRegistry().hasMapper(NestedBlogMapper.class));
    } finally {
    // System.clearProperty(XPathParser.KEY_USE_XSD);
    }
}
Also used : CustomIntegerTypeHandler(org.apache.ibatis.builder.typehandler.CustomIntegerTypeHandler) NestedBlogMapper(org.apache.ibatis.domain.blog.mappers.NestedBlogMapper) BlogMapper(org.apache.ibatis.domain.blog.mappers.BlogMapper) CustomStringTypeHandler(org.apache.ibatis.builder.CustomStringTypeHandler) CustomLongTypeHandler(org.apache.ibatis.builder.CustomLongTypeHandler) InputStream(java.io.InputStream) NestedBlogMapper(org.apache.ibatis.domain.blog.mappers.NestedBlogMapper) ExamplePlugin(org.apache.ibatis.builder.ExamplePlugin) CustomReflectorFactory(org.apache.ibatis.builder.CustomReflectorFactory) CustomMapper(org.apache.ibatis.builder.mapper.CustomMapper) CustomObjectWrapperFactory(org.apache.ibatis.builder.CustomObjectWrapperFactory) UnpooledDataSource(org.apache.ibatis.datasource.unpooled.UnpooledDataSource) JBoss6VFS(org.apache.ibatis.io.JBoss6VFS) CglibProxyFactory(org.apache.ibatis.executor.loader.cglib.CglibProxyFactory) XMLConfigBuilder(org.apache.ibatis.builder.xml.XMLConfigBuilder) RawLanguageDriver(org.apache.ibatis.scripting.defaults.RawLanguageDriver) ExampleObjectFactory(org.apache.ibatis.builder.ExampleObjectFactory) Environment(org.apache.ibatis.mapping.Environment) Slf4jImpl(org.apache.ibatis.logging.slf4j.Slf4jImpl) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory) Test(org.junit.jupiter.api.Test)

Example 5 with XMLConfigBuilder

use of org.apache.ibatis.builder.xml.XMLConfigBuilder in project mybatis-3 by mybatis.

the class XmlConfigBuilderTest method shouldSuccessfullyLoadMinimalXMLConfigFile.

@Test
void shouldSuccessfullyLoadMinimalXMLConfigFile() throws Exception {
    // System.setProperty(XPathParser.KEY_USE_XSD, "true");
    String resource = "org/apache/ibatis/builder/xsd/MinimalMapperConfig.xml";
    try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
        XMLConfigBuilder builder = new XMLConfigBuilder(inputStream);
        Configuration config = builder.parse();
        assertNotNull(config);
        assertEquals(AutoMappingBehavior.PARTIAL, config.getAutoMappingBehavior());
        assertEquals(AutoMappingUnknownColumnBehavior.NONE, config.getAutoMappingUnknownColumnBehavior());
        assertTrue(config.isCacheEnabled());
        assertTrue(config.getProxyFactory() instanceof JavassistProxyFactory);
        assertFalse(config.isLazyLoadingEnabled());
        assertFalse(config.isAggressiveLazyLoading());
        assertTrue(config.isMultipleResultSetsEnabled());
        assertTrue(config.isUseColumnLabel());
        assertFalse(config.isUseGeneratedKeys());
        assertEquals(ExecutorType.SIMPLE, config.getDefaultExecutorType());
        assertNull(config.getDefaultStatementTimeout());
        assertNull(config.getDefaultFetchSize());
        assertFalse(config.isMapUnderscoreToCamelCase());
        assertFalse(config.isSafeRowBoundsEnabled());
        assertEquals(LocalCacheScope.SESSION, config.getLocalCacheScope());
        assertEquals(JdbcType.OTHER, config.getJdbcTypeForNull());
        assertEquals(new HashSet<>(Arrays.asList("equals", "clone", "hashCode", "toString")), config.getLazyLoadTriggerMethods());
        assertTrue(config.isSafeResultHandlerEnabled());
        assertTrue(config.getDefaultScriptingLanguageInstance() instanceof XMLLanguageDriver);
        assertFalse(config.isCallSettersOnNulls());
        assertNull(config.getLogPrefix());
        assertNull(config.getLogImpl());
        assertNull(config.getConfigurationFactory());
        assertFalse(config.isShrinkWhitespacesInSql());
    } finally {
    // System.clearProperty(XPathParser.KEY_USE_XSD);
    }
}
Also used : JavassistProxyFactory(org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory) InputStream(java.io.InputStream) XMLConfigBuilder(org.apache.ibatis.builder.xml.XMLConfigBuilder) XMLLanguageDriver(org.apache.ibatis.scripting.xmltags.XMLLanguageDriver) Test(org.junit.jupiter.api.Test)

Aggregations

XMLConfigBuilder (org.apache.ibatis.builder.xml.XMLConfigBuilder)13 Test (org.junit.jupiter.api.Test)10 InputStream (java.io.InputStream)7 Configuration (org.apache.ibatis.session.Configuration)6 StringReader (java.io.StringReader)4 Environment (org.apache.ibatis.mapping.Environment)4 Properties (java.util.Properties)2 JBoss6VFS (org.apache.ibatis.io.JBoss6VFS)2 Slf4jImpl (org.apache.ibatis.logging.slf4j.Slf4jImpl)2 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 SQLException (java.sql.SQLException)1 ParseException (java.text.ParseException)1 ProcessEngineConfiguration (org.activiti.engine.ProcessEngineConfiguration)1 CustomLongTypeHandler (org.apache.ibatis.builder.CustomLongTypeHandler)1 CustomObjectWrapperFactory (org.apache.ibatis.builder.CustomObjectWrapperFactory)1 CustomReflectorFactory (org.apache.ibatis.builder.CustomReflectorFactory)1 CustomStringTypeHandler (org.apache.ibatis.builder.CustomStringTypeHandler)1 ExampleObjectFactory (org.apache.ibatis.builder.ExampleObjectFactory)1 ExamplePlugin (org.apache.ibatis.builder.ExamplePlugin)1