Example usage for org.apache.ibatis.type TypeHandlerRegistry register

List of usage examples for org.apache.ibatis.type TypeHandlerRegistry register

Introduction

In this page you can find the example usage for org.apache.ibatis.type TypeHandlerRegistry register.

Prototype

public void register(Class<?> javaTypeClass, Class<?> typeHandlerClass) 

Source Link

Usage

From source file:org.flowable.common.engine.impl.AbstractEngineConfigurator.java

License:Apache License

protected void registerCustomMybatisMappings(AbstractEngineConfiguration engineConfiguration) {
    String cfgPath = getMybatisCfgPath();
    if (cfgPath != null) {
        Set<String> resources = new HashSet<>();

        ClassLoader classLoader = engineConfiguration.getClassLoader();
        if (classLoader == null) {
            classLoader = this.getClass().getClassLoader();
        }/*from  w w  w . ja va 2s  .  c o m*/

        List<MybatisTypeAliasConfigurator> typeAliasConfigurators = new ArrayList<>();
        List<MybatisTypeHandlerConfigurator> typeHandlerConfigurators = new ArrayList<>();
        try (InputStream inputStream = classLoader.getResourceAsStream(cfgPath)) {
            DocumentBuilderFactory docBuilderFactory = createDocumentBuilderFactory();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document document = docBuilder.parse(inputStream);

            NodeList typeAliasList = document.getElementsByTagName("typeAlias");
            for (int i = 0; i < typeAliasList.getLength(); i++) {
                Node node = typeAliasList.item(i);
                MybatisTypeAliasConfigurator typeAlias = new MybatisTypeAliasConfigurator() {
                    @Override
                    public void configure(TypeAliasRegistry typeAliasRegistry) {
                        try {
                            typeAliasRegistry.registerAlias(
                                    node.getAttributes().getNamedItem("alias").getTextContent(),
                                    Class.forName(node.getAttributes().getNamedItem("type").getTextContent()));
                        } catch (Exception e) {
                            throw new FlowableException("Failed to load type alias class", e);
                        }
                    }
                };
                typeAliasConfigurators.add(typeAlias);

            }

            NodeList typeHandlerList = document.getElementsByTagName("tagHandler");
            for (int i = 0; i < typeHandlerList.getLength(); i++) {
                Node node = typeHandlerList.item(i);
                MybatisTypeHandlerConfigurator typeHandler = new MybatisTypeHandlerConfigurator() {
                    @Override
                    public void configure(TypeHandlerRegistry typeHandlerRegistry) {
                        try {
                            typeHandlerRegistry.register(
                                    node.getAttributes().getNamedItem("javaType").getTextContent(),
                                    node.getAttributes().getNamedItem("handler").getTextContent());
                        } catch (Exception e) {
                            throw new FlowableException("Failed to load type handler class", e);
                        }
                    }
                };
                typeHandlerConfigurators.add(typeHandler);
            }

            NodeList nodeList = document.getElementsByTagName("mapper");
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                resources.add(node.getAttributes().getNamedItem("resource").getTextContent());
            }

        } catch (IOException e) {
            throw new FlowableException("Could not read IDM Mybatis configuration file", e);
        } catch (ParserConfigurationException | SAXException e) {
            throw new FlowableException("Could not parse Mybatis configuration file", e);
        }

        if (typeAliasConfigurators.size() > 0) {
            if (engineConfiguration.getDependentEngineMybatisTypeAliasConfigs() == null) {
                engineConfiguration.setDependentEngineMybatisTypeAliasConfigs(typeAliasConfigurators);

            } else {
                engineConfiguration.getDependentEngineMybatisTypeAliasConfigs().addAll(typeAliasConfigurators);
            }
        }

        if (typeHandlerConfigurators.size() > 0) {
            if (engineConfiguration.getDependentEngineMybatisTypeHandlerConfigs() == null) {
                engineConfiguration.setDependentEngineMybatisTypeHandlerConfigs(typeHandlerConfigurators);

            } else {
                engineConfiguration.getDependentEngineMybatisTypeHandlerConfigs()
                        .addAll(typeHandlerConfigurators);
            }
        }

        if (engineConfiguration.getCustomMybatisXMLMappers() == null) {
            engineConfiguration.setCustomMybatisXMLMappers(resources);
        } else {
            engineConfiguration.getCustomMybatisXMLMappers().addAll(resources);
        }
    }
}