Ejemplo n.º 1
0
 private void sqlElement(List<XNode> list, String requiredDatabaseId) throws Exception {
   for (XNode context : list) {
     String databaseId = context.getStringAttribute("databaseId");
     String id = context.getStringAttribute("id");
     id = builderAssistant.applyCurrentNamespace(id, false);
     if (databaseIdMatchesCurrent(id, databaseId, requiredDatabaseId))
       sqlFragments.put(id, context);
   }
 }
Ejemplo n.º 2
0
 private void objectFactoryElement(XNode context) throws Exception {
   if (context != null) {
     String type = context.getStringAttribute("type");
     Properties properties = context.getChildrenAsProperties();
     ObjectFactory factory = (ObjectFactory) resolveClass(type).newInstance();
     factory.setProperties(properties);
     configuration.setObjectFactory(factory);
   }
 }
Ejemplo n.º 3
0
 private DataSourceFactory dataSourceElement(XNode context) throws Exception {
   if (context != null) {
     String type = context.getStringAttribute("type");
     Properties props = context.getChildrenAsProperties();
     DataSourceFactory factory = (DataSourceFactory) resolveClass(type).newInstance();
     factory.setProperties(props);
     return factory;
   }
   throw new BuilderException("Environment declaration requires a DataSourceFactory.");
 }
Ejemplo n.º 4
0
 private void pluginElement(XNode parent) throws Exception {
   if (parent != null) {
     for (XNode child : parent.getChildren()) {
       String interceptor = child.getStringAttribute("interceptor");
       Properties properties = child.getChildrenAsProperties();
       Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance();
       interceptorInstance.setProperties(properties);
       configuration.addInterceptor(interceptorInstance);
     }
   }
 }
Ejemplo n.º 5
0
 private void processConstructorElement(
     XNode resultChild, Class<?> resultType, List<ResultMapping> resultMappings) throws Exception {
   List<XNode> argChildren = resultChild.getChildren();
   for (XNode argChild : argChildren) {
     ArrayList<ResultFlag> flags = new ArrayList<ResultFlag>();
     flags.add(ResultFlag.CONSTRUCTOR);
     if ("idArg".equals(argChild.getName())) {
       flags.add(ResultFlag.ID);
     }
     resultMappings.add(buildResultMappingFromContext(argChild, resultType, flags));
   }
 }
Ejemplo n.º 6
0
 private String processNestedResultMappings(XNode context, List<ResultMapping> resultMappings)
     throws Exception {
   if ("association".equals(context.getName())
       || "collection".equals(context.getName())
       || "case".equals(context.getName())) {
     if (context.getStringAttribute("select") == null) {
       ResultMap resultMap = resultMapElement(context, resultMappings);
       return resultMap.getId();
     }
   }
   return null;
 }
Ejemplo n.º 7
0
 private void cacheRefElement(XNode context) {
   if (context != null) {
     configuration.addCacheRef(
         builderAssistant.getCurrentNamespace(), context.getStringAttribute("namespace"));
     CacheRefResolver cacheRefResolver =
         new CacheRefResolver(builderAssistant, context.getStringAttribute("namespace"));
     try {
       cacheRefResolver.resolveCacheRef();
     } catch (IncompleteElementException e) {
       configuration.addIncompleteCacheRef(cacheRefResolver);
     }
   }
 }
Ejemplo n.º 8
0
 private void databaseIdProviderElement(XNode context) throws Exception {
   DatabaseIdProvider databaseIdProvider = null;
   if (context != null) {
     String type = context.getStringAttribute("type");
     if ("VENDOR".equals(type)) type = "DB_VENDOR"; // awful patch to keep backward compatibility
     Properties properties = context.getChildrenAsProperties();
     databaseIdProvider = (DatabaseIdProvider) resolveClass(type).newInstance();
     databaseIdProvider.setProperties(properties);
   }
   Environment environment = configuration.getEnvironment();
   if (environment != null && databaseIdProvider != null) {
     String databaseId = databaseIdProvider.getDatabaseId(environment.getDataSource());
     configuration.setDatabaseId(databaseId);
   }
 }
Ejemplo n.º 9
0
 private void objectWrapperFactoryElement(XNode context) throws Exception {
   if (context != null) {
     String type = context.getStringAttribute("type");
     ObjectWrapperFactory factory = (ObjectWrapperFactory) resolveClass(type).newInstance();
     configuration.setObjectWrapperFactory(factory);
   }
 }
Ejemplo n.º 10
0
 private void mapperElement(XNode parent) throws Exception {
   if (parent != null) {
     for (XNode child : parent.getChildren()) {
       if ("package".equals(child.getName())) {
         String mapperPackage = child.getStringAttribute("name");
         configuration.addMappers(mapperPackage);
       } else {
         String resource = child.getStringAttribute("resource");
         String url = child.getStringAttribute("url");
         String mapperClass = child.getStringAttribute("class");
         if (resource != null && url == null && mapperClass == null) {
           ErrorContext.instance().resource(resource);
           InputStream inputStream = Resources.getResourceAsStream(resource);
           XMLMapperBuilder mapperParser =
               new XMLMapperBuilder(
                   inputStream, configuration, resource, configuration.getSqlFragments());
           mapperParser.parse();
         } else if (resource == null && url != null && mapperClass == null) {
           ErrorContext.instance().resource(url);
           InputStream inputStream = Resources.getUrlAsStream(url);
           XMLMapperBuilder mapperParser =
               new XMLMapperBuilder(
                   inputStream, configuration, url, configuration.getSqlFragments());
           mapperParser.parse();
         } else if (resource == null && url == null && mapperClass != null) {
           Class<?> mapperInterface = Resources.classForName(mapperClass);
           configuration.addMapper(mapperInterface);
         } else {
           throw new BuilderException(
               "A mapper element may only specify a url, resource or class, but not more than one.");
         }
       }
     }
   }
 }
Ejemplo n.º 11
0
 private void parseConfiguration(XNode root) {
   try {
     propertiesElement(root.evalNode("properties")); // issue #117 read properties first
     typeAliasesElement(root.evalNode("typeAliases"));
     pluginElement(root.evalNode("plugins"));
     objectFactoryElement(root.evalNode("objectFactory"));
     objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
     settingsElement(root.evalNode("settings"));
     environmentsElement(
         root.evalNode(
             "environments")); // read it after objectFactory and objectWrapperFactory issue #631
     databaseIdProviderElement(root.evalNode("databaseIdProvider"));
     typeHandlerElement(root.evalNode("typeHandlers"));
     mapperElement(root.evalNode("mappers"));
   } catch (Exception e) {
     throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
   }
 }
Ejemplo n.º 12
0
 private void settingsElement(XNode context) throws Exception {
   if (context != null) {
     Properties props = context.getChildrenAsProperties();
     // Check that all settings are known to the configuration class
     MetaClass metaConfig = MetaClass.forClass(Configuration.class);
     for (Object key : props.keySet()) {
       if (!metaConfig.hasSetter(String.valueOf(key))) {
         throw new BuilderException(
             "The setting "
                 + key
                 + " is not known.  Make sure you spelled it correctly (case sensitive).");
       }
     }
     configuration.setAutoMappingBehavior(
         AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL")));
     configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true));
     configuration.setProxyFactory(
         (ProxyFactory) createInstance(props.getProperty("proxyFactory")));
     configuration.setLazyLoadingEnabled(
         booleanValueOf(props.getProperty("lazyLoadingEnabled"), false));
     configuration.setAggressiveLazyLoading(
         booleanValueOf(props.getProperty("aggressiveLazyLoading"), true));
     configuration.setMultipleResultSetsEnabled(
         booleanValueOf(props.getProperty("multipleResultSetsEnabled"), true));
     configuration.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true));
     configuration.setUseGeneratedKeys(
         booleanValueOf(props.getProperty("useGeneratedKeys"), false));
     configuration.setDefaultExecutorType(
         ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));
     configuration.setDefaultStatementTimeout(
         integerValueOf(props.getProperty("defaultStatementTimeout"), null));
     configuration.setMapUnderscoreToCamelCase(
         booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));
     configuration.setSafeRowBoundsEnabled(
         booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false));
     configuration.setLocalCacheScope(
         LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION")));
     configuration.setJdbcTypeForNull(
         JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER")));
     configuration.setLazyLoadTriggerMethods(
         stringSetValueOf(
             props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));
     configuration.setSafeResultHandlerEnabled(
         booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true));
     configuration.setDefaultScriptingLanguage(
         resolveClass(props.getProperty("defaultScriptingLanguage")));
     configuration.setCallSettersOnNulls(
         booleanValueOf(props.getProperty("callSettersOnNulls"), false));
     configuration.setLogPrefix(props.getProperty("logPrefix"));
     configuration.setLogImpl(resolveClass(props.getProperty("logImpl")));
     configuration.setConfigurationFactory(
         resolveClass(props.getProperty("configurationFactory")));
   }
 }
Ejemplo n.º 13
0
 private boolean databaseIdMatchesCurrent(
     String id, String databaseId, String requiredDatabaseId) {
   if (requiredDatabaseId != null) {
     if (!requiredDatabaseId.equals(databaseId)) {
       return false;
     }
   } else {
     if (databaseId != null) {
       return false;
     }
     // skip this fragment if there is a previous one with a not null
     // databaseId
     if (this.sqlFragments.containsKey(id)) {
       XNode context = this.sqlFragments.get(id);
       if (context.getStringAttribute("databaseId") != null) {
         return false;
       }
     }
   }
   return true;
 }
Ejemplo n.º 14
0
 private void typeHandlerElement(XNode parent) throws Exception {
   if (parent != null) {
     for (XNode child : parent.getChildren()) {
       if ("package".equals(child.getName())) {
         String typeHandlerPackage = child.getStringAttribute("name");
         typeHandlerRegistry.register(typeHandlerPackage);
       } else {
         String javaTypeName = child.getStringAttribute("javaType");
         String jdbcTypeName = child.getStringAttribute("jdbcType");
         String handlerTypeName = child.getStringAttribute("handler");
         Class<?> javaTypeClass = resolveClass(javaTypeName);
         JdbcType jdbcType = resolveJdbcType(jdbcTypeName);
         Class<?> typeHandlerClass = resolveClass(handlerTypeName);
         if (javaTypeClass != null) {
           if (jdbcType == null) {
             typeHandlerRegistry.register(javaTypeClass, typeHandlerClass);
           } else {
             typeHandlerRegistry.register(javaTypeClass, jdbcType, typeHandlerClass);
           }
         } else {
           typeHandlerRegistry.register(typeHandlerClass);
         }
       }
     }
   }
 }
Ejemplo n.º 15
0
 private void propertiesElement(XNode context) throws Exception {
   if (context != null) {
     Properties defaults = context.getChildrenAsProperties();
     String resource = context.getStringAttribute("resource");
     String url = context.getStringAttribute("url");
     if (resource != null && url != null) {
       throw new BuilderException(
           "The properties element cannot specify both a URL and a resource based property file reference.  Please specify one or the other.");
     }
     if (resource != null) {
       defaults.putAll(Resources.getResourceAsProperties(resource));
     } else if (url != null) {
       defaults.putAll(Resources.getUrlAsProperties(url));
     }
     Properties vars = configuration.getVariables();
     if (vars != null) {
       defaults.putAll(vars);
     }
     parser.setVariables(defaults);
     configuration.setVariables(defaults);
   }
 }
Ejemplo n.º 16
0
 private void cacheElement(XNode context) throws Exception {
   if (context != null) {
     String type = context.getStringAttribute("type", "PERPETUAL");
     Class<? extends Cache> typeClass = typeAliasRegistry.resolveAlias(type);
     String eviction = context.getStringAttribute("eviction", "LRU");
     Class<? extends Cache> evictionClass = typeAliasRegistry.resolveAlias(eviction);
     Long flushInterval = context.getLongAttribute("flushInterval");
     Integer size = context.getIntAttribute("size");
     boolean readWrite = !context.getBooleanAttribute("readOnly", false);
     Properties props = context.getChildrenAsProperties();
     builderAssistant.useNewCache(typeClass, evictionClass, flushInterval, size, readWrite, props);
   }
 }
Ejemplo n.º 17
0
 private void configurationElement(XNode context) {
   try {
     String namespace = context.getStringAttribute("namespace");
     if (namespace.equals("")) {
       throw new BuilderException("Mapper's namespace cannot be empty");
     }
     builderAssistant.setCurrentNamespace(namespace);
     cacheRefElement(context.evalNode("cache-ref"));
     cacheElement(context.evalNode("cache"));
     parameterMapElement(context.evalNodes("/mapper/parameterMap"));
     resultMapElements(context.evalNodes("/mapper/resultMap"));
     sqlElement(context.evalNodes("/mapper/sql"));
     buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
   } catch (Exception e) {
     throw new BuilderException("Error parsing Mapper XML. Cause: " + e, e);
   }
 }
Ejemplo n.º 18
0
 private Discriminator processDiscriminatorElement(
     XNode context, Class<?> resultType, List<ResultMapping> resultMappings) throws Exception {
   String column = context.getStringAttribute("column");
   String javaType = context.getStringAttribute("javaType");
   String jdbcType = context.getStringAttribute("jdbcType");
   String typeHandler = context.getStringAttribute("typeHandler");
   Class<?> javaTypeClass = resolveClass(javaType);
   @SuppressWarnings("unchecked")
   Class<? extends TypeHandler<?>> typeHandlerClass =
       (Class<? extends TypeHandler<?>>) resolveClass(typeHandler);
   JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
   Map<String, String> discriminatorMap = new HashMap<String, String>();
   for (XNode caseChild : context.getChildren()) {
     String value = caseChild.getStringAttribute("value");
     String resultMap =
         caseChild.getStringAttribute(
             "resultMap", processNestedResultMappings(caseChild, resultMappings));
     discriminatorMap.put(value, resultMap);
   }
   return builderAssistant.buildDiscriminator(
       resultType, column, javaTypeClass, jdbcTypeEnum, typeHandlerClass, discriminatorMap);
 }
Ejemplo n.º 19
0
 private void environmentsElement(XNode context) throws Exception {
   if (context != null) {
     if (environment == null) {
       environment = context.getStringAttribute("default");
     }
     for (XNode child : context.getChildren()) {
       String id = child.getStringAttribute("id");
       if (isSpecifiedEnvironment(id)) {
         TransactionFactory txFactory =
             transactionManagerElement(child.evalNode("transactionManager"));
         DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
         DataSource dataSource = dsFactory.getDataSource();
         Environment.Builder environmentBuilder =
             new Environment.Builder(id).transactionFactory(txFactory).dataSource(dataSource);
         configuration.setEnvironment(environmentBuilder.build());
       }
     }
   }
 }
Ejemplo n.º 20
0
 private void typeAliasesElement(XNode parent) {
   if (parent != null) {
     for (XNode child : parent.getChildren()) {
       if ("package".equals(child.getName())) {
         String typeAliasPackage = child.getStringAttribute("name");
         configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
       } else {
         String alias = child.getStringAttribute("alias");
         String type = child.getStringAttribute("type");
         try {
           Class<?> clazz = Resources.classForName(type);
           if (alias == null) {
             typeAliasRegistry.registerAlias(clazz);
           } else {
             typeAliasRegistry.registerAlias(alias, clazz);
           }
         } catch (ClassNotFoundException e) {
           throw new BuilderException(
               "Error registering typeAlias for '" + alias + "'. Cause: " + e, e);
         }
       }
     }
   }
 }
Ejemplo n.º 21
0
 private ResultMapping buildResultMappingFromContext(
     XNode context, Class<?> resultType, ArrayList<ResultFlag> flags) throws Exception {
   String property = context.getStringAttribute("property");
   String column = context.getStringAttribute("column");
   String javaType = context.getStringAttribute("javaType");
   String jdbcType = context.getStringAttribute("jdbcType");
   String nestedSelect = context.getStringAttribute("select");
   String nestedResultMap =
       context.getStringAttribute(
           "resultMap",
           processNestedResultMappings(context, Collections.<ResultMapping>emptyList()));
   String notNullColumn = context.getStringAttribute("notNullColumn");
   String columnPrefix = context.getStringAttribute("columnPrefix");
   String typeHandler = context.getStringAttribute("typeHandler");
   String resulSet = context.getStringAttribute("resultSet");
   String foreignColumn = context.getStringAttribute("foreignColumn");
   boolean lazy =
       "lazy"
           .equals(
               context.getStringAttribute(
                   "fetchType", configuration.isLazyLoadingEnabled() ? "lazy" : "eager"));
   Class<?> javaTypeClass = resolveClass(javaType);
   @SuppressWarnings("unchecked")
   Class<? extends TypeHandler<?>> typeHandlerClass =
       (Class<? extends TypeHandler<?>>) resolveClass(typeHandler);
   JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
   return builderAssistant.buildResultMapping(
       resultType,
       property,
       column,
       javaTypeClass,
       jdbcTypeEnum,
       nestedSelect,
       nestedResultMap,
       notNullColumn,
       columnPrefix,
       typeHandlerClass,
       flags,
       resulSet,
       foreignColumn,
       lazy);
 }
Ejemplo n.º 22
0
 private ResultMap resultMapElement(
     XNode resultMapNode, List<ResultMapping> additionalResultMappings) throws Exception {
   ErrorContext.instance().activity("processing " + resultMapNode.getValueBasedIdentifier());
   String id = resultMapNode.getStringAttribute("id", resultMapNode.getValueBasedIdentifier());
   String type =
       resultMapNode.getStringAttribute(
           "type",
           resultMapNode.getStringAttribute(
               "ofType",
               resultMapNode.getStringAttribute(
                   "resultType", resultMapNode.getStringAttribute("javaType"))));
   String extend = resultMapNode.getStringAttribute("extends");
   Boolean autoMapping = resultMapNode.getBooleanAttribute("autoMapping");
   Class<?> typeClass = resolveClass(type);
   Discriminator discriminator = null;
   List<ResultMapping> resultMappings = new ArrayList<ResultMapping>();
   resultMappings.addAll(additionalResultMappings);
   List<XNode> resultChildren = resultMapNode.getChildren();
   for (XNode resultChild : resultChildren) {
     if ("constructor".equals(resultChild.getName())) {
       processConstructorElement(resultChild, typeClass, resultMappings);
     } else if ("discriminator".equals(resultChild.getName())) {
       discriminator = processDiscriminatorElement(resultChild, typeClass, resultMappings);
     } else {
       ArrayList<ResultFlag> flags = new ArrayList<ResultFlag>();
       if ("id".equals(resultChild.getName())) {
         flags.add(ResultFlag.ID);
       }
       resultMappings.add(buildResultMappingFromContext(resultChild, typeClass, flags));
     }
   }
   ResultMapResolver resultMapResolver =
       new ResultMapResolver(
           builderAssistant, id, typeClass, extend, discriminator, resultMappings, autoMapping);
   try {
     return resultMapResolver.resolve();
   } catch (IncompleteElementException e) {
     configuration.addIncompleteResultMap(resultMapResolver);
     throw e;
   }
 }
Ejemplo n.º 23
0
 private void parameterMapElement(List<XNode> list) throws Exception {
   for (XNode parameterMapNode : list) {
     String id = parameterMapNode.getStringAttribute("id");
     String type = parameterMapNode.getStringAttribute("type");
     Class<?> parameterClass = resolveClass(type);
     List<XNode> parameterNodes = parameterMapNode.evalNodes("parameter");
     List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>();
     for (XNode parameterNode : parameterNodes) {
       String property = parameterNode.getStringAttribute("property");
       String javaType = parameterNode.getStringAttribute("javaType");
       String jdbcType = parameterNode.getStringAttribute("jdbcType");
       String resultMap = parameterNode.getStringAttribute("resultMap");
       String mode = parameterNode.getStringAttribute("mode");
       String typeHandler = parameterNode.getStringAttribute("typeHandler");
       Integer numericScale = parameterNode.getIntAttribute("numericScale");
       ParameterMode modeEnum = resolveParameterMode(mode);
       Class<?> javaTypeClass = resolveClass(javaType);
       JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
       @SuppressWarnings("unchecked")
       Class<? extends TypeHandler<?>> typeHandlerClass =
           (Class<? extends TypeHandler<?>>) resolveClass(typeHandler);
       ParameterMapping parameterMapping =
           builderAssistant.buildParameterMapping(
               parameterClass,
               property,
               javaTypeClass,
               jdbcTypeEnum,
               resultMap,
               modeEnum,
               typeHandlerClass,
               numericScale);
       parameterMappings.add(parameterMapping);
     }
     builderAssistant.addParameterMap(id, parameterClass, parameterMappings);
   }
 }