001/* 002 * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.mybatisflex.processor.config; 018 019import com.mybatisflex.processor.util.FileUtil; 020 021import javax.annotation.processing.Filer; 022import javax.tools.FileObject; 023import javax.tools.StandardLocation; 024import java.io.File; 025import java.io.InputStream; 026import java.io.InputStreamReader; 027import java.io.Reader; 028import java.nio.charset.StandardCharsets; 029import java.nio.file.Files; 030import java.util.ArrayList; 031import java.util.List; 032import java.util.Properties; 033 034/** 035 * Mybatis Flex 生成配置。 036 * 037 * @author 王帅 038 * @since 2023-06-22 039 */ 040public class MybatisFlexConfig { 041 042 /** 043 * 配置文件名。 044 */ 045 private static final String APT_FILE_NAME = "mybatis-flex.config"; 046 047 /** 048 * mybatis-flex.properties 049 */ 050 protected final Properties properties = new Properties(); 051 052 public MybatisFlexConfig(Filer filer) { 053 try { 054 //target/classes/ 055 FileObject resource = filer.createResource(StandardLocation.CLASS_OUTPUT, "", "mybatis-flex"); 056 File classPathFile = new File(resource.toUri()).getParentFile(); 057 058 String projectRootPath = FileUtil.getProjectRootPath(classPathFile, 10); 059 060 List<File> aptConfigFiles = new ArrayList<>(); 061 062 while (projectRootPath != null && classPathFile != null 063 && projectRootPath.length() <= classPathFile.getAbsolutePath().length()) { 064 File aptConfig = new File(classPathFile, APT_FILE_NAME); 065 if (aptConfig.exists()) { 066 aptConfigFiles.add(aptConfig); 067 } 068 classPathFile = classPathFile.getParentFile(); 069 } 070 071 072 for (File aptConfigFile : aptConfigFiles) { 073 try (InputStream stream = Files.newInputStream(aptConfigFile.toPath()); 074 Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) { 075 076 Properties config = new Properties(); 077 config.load(reader); 078 079 boolean stopBubbling = false; 080 for (Object key : config.keySet()) { 081 if (!properties.containsKey(key)) { 082 properties.put(key, config.getProperty((String) key)); 083 } 084 if ("processor.stopBubbling".equalsIgnoreCase((String) key) 085 && "true".equalsIgnoreCase(String.valueOf(config.getProperty((String) key)))) { 086 stopBubbling = true; 087 } 088 } 089 if (stopBubbling) { 090 break; 091 } 092 } 093 } 094 095 } catch (Exception e) { 096 e.printStackTrace(); 097 } 098 } 099 100 public String get(ConfigurationKey key) { 101 return properties.getProperty(key.getConfigKey(), key.getDefaultValue()); 102 } 103 104}