1515 */
1616package org .seasar .doma .internal .apt ;
1717
18+ import java .io .IOException ;
19+ import java .io .InputStream ;
20+ import java .io .InputStreamReader ;
21+ import java .util .Collections ;
1822import java .util .Date ;
23+ import java .util .HashMap ;
24+ import java .util .Map ;
25+ import java .util .Properties ;
26+ import java .util .concurrent .ConcurrentHashMap ;
1927
28+ import javax .annotation .processing .Filer ;
2029import javax .annotation .processing .ProcessingEnvironment ;
30+ import javax .tools .FileObject ;
31+ import javax .tools .StandardLocation ;
2132
2233import org .seasar .doma .internal .Artifact ;
2334
@@ -47,12 +58,14 @@ public final class Options {
4758
4859 public static final String VERSION_VALIDATION = "doma.version.validation" ;
4960
61+ public static final String CONFIG_PATH = "doma.config.path" ;
62+
5063 public static final String LOMBOK_ALL_ARGS_CONSTRUCTOR = "doma.lombok.AllArgsConstructor" ;
5164
5265 public static final String LOMBOK_VALUE = "doma.lombok.Value" ;
5366
5467 public static boolean isTestEnabled (ProcessingEnvironment env ) {
55- String test = env . getOptions (). get ( Options .TEST );
68+ String test = getOption ( env , Options .TEST );
5669 return Boolean .valueOf (test ).booleanValue ();
5770 }
5871
@@ -71,69 +84,120 @@ public static Date getDate(ProcessingEnvironment env) {
7184 }
7285
7386 public static boolean isDebugEnabled (ProcessingEnvironment env ) {
74- String debug = env . getOptions (). get ( Options .DEBUG );
87+ String debug = getOption ( env , Options .DEBUG );
7588 return Boolean .valueOf (debug ).booleanValue ();
7689 }
7790
7891 public static String getDaoPackage (ProcessingEnvironment env ) {
79- String pkg = env . getOptions (). get ( Options .DAO_PACKAGE );
92+ String pkg = getOption ( env , Options .DAO_PACKAGE );
8093 return pkg != null ? pkg : null ;
8194 }
8295
8396 public static String getDaoSubpackage (ProcessingEnvironment env ) {
84- String subpackage = env . getOptions (). get ( Options .DAO_SUBPACKAGE );
97+ String subpackage = getOption ( env , Options .DAO_SUBPACKAGE );
8598 return subpackage != null ? subpackage : null ;
8699 }
87100
88101 public static String getDaoSuffix (ProcessingEnvironment env ) {
89- String suffix = env . getOptions (). get ( Options .DAO_SUFFIX );
102+ String suffix = getOption ( env , Options .DAO_SUFFIX );
90103 return suffix != null ? suffix : Constants .DEFAULT_DAO_SUFFIX ;
91104 }
92105
93106 public static String getEntityFieldPrefix (ProcessingEnvironment env ) {
94- String prefix = env . getOptions (). get ( Options .ENTITY_FIELD_PREFIX );
107+ String prefix = getOption ( env , Options .ENTITY_FIELD_PREFIX );
95108 if ("none" .equalsIgnoreCase (prefix )) {
96109 return "" ;
97110 }
98111 return prefix != null ? prefix : Constants .DEFAULT_ENTITY_FIELD_PREFIX ;
99112 }
100113
101114 public static String getExprFunctions (ProcessingEnvironment env ) {
102- String name = env . getOptions (). get ( Options .EXPR_FUNCTIONS );
115+ String name = getOption ( env , Options .EXPR_FUNCTIONS );
103116 return name != null ? name : null ;
104117 }
105118
106119 public static String getDomainConverters (ProcessingEnvironment env ) {
107- String converters = env . getOptions (). get ( Options .DOMAIN_CONVERTERS );
120+ String converters = getOption ( env , Options .DOMAIN_CONVERTERS );
108121 return converters != null ? converters : null ;
109122 }
110123
111124 public static boolean getSqlValidation (ProcessingEnvironment env ) {
112- String v = env . getOptions (). get ( Options .SQL_VALIDATION );
125+ String v = getOption ( env , Options .SQL_VALIDATION );
113126 return v != null ? Boolean .valueOf (v ).booleanValue () : true ;
114127 }
115128
116129 public static boolean getVersionValidation (ProcessingEnvironment env ) {
117- String v = env . getOptions (). get ( Options .VERSION_VALIDATION );
130+ String v = getOption ( env , Options .VERSION_VALIDATION );
118131 return v != null ? Boolean .valueOf (v ).booleanValue () : true ;
119132 }
120133
134+ public static String getConfigPath (ProcessingEnvironment env ) {
135+ String configPath = env .getOptions ().get (Options .CONFIG_PATH );
136+ return configPath != null ? configPath : Constants .DEFAULT_CONFIG_PATH ;
137+ }
138+
121139 public static String getLombokAllArgsConstructor (ProcessingEnvironment env ) {
122- String name = env . getOptions (). get ( Options .LOMBOK_ALL_ARGS_CONSTRUCTOR );
140+ String name = getOption ( env , Options .LOMBOK_ALL_ARGS_CONSTRUCTOR );
123141 return name != null ? name : Constants .DEFAULT_LOMBOK_ALL_ARGS_CONSTRUCTOR ;
124142 }
125143
126144 public static String getLombokValue (ProcessingEnvironment env ) {
127- String name = env . getOptions (). get ( Options .LOMBOK_VALUE );
145+ String name = getOption ( env , Options .LOMBOK_VALUE );
128146 return name != null ? name : Constants .DEFAULT_LOMBOK_VALUE ;
129147 }
130148
149+ private static String getOption (ProcessingEnvironment env , String key ) {
150+ String v = env .getOptions ().get (key );
151+ if (v != null ) {
152+ return v ;
153+ }
154+
155+ return getConfig (env ).get (key );
156+ }
157+
158+ private static Map <String , Map <String , String >> configCache = new ConcurrentHashMap <>();
159+ private static Map <String , String > getConfig (ProcessingEnvironment env ) {
160+ FileObject config = getFileObject (env , "" , getConfigPath (env ));
161+ if (config == null ) {
162+ return Collections .emptyMap ();
163+ }
164+ return configCache .computeIfAbsent (config .toUri ().getPath (), configPath -> {
165+ try {
166+ return loadProperties (config );
167+ } catch (IOException e ) {
168+ return Collections .emptyMap ();
169+ }
170+ });
171+ }
172+
173+ private static FileObject getFileObject (ProcessingEnvironment env , String pkg , String relativeName ) {
174+ Filer filer = env .getFiler ();
175+
176+ try {
177+ return filer .getResource (StandardLocation .CLASS_OUTPUT , pkg , relativeName );
178+ } catch (IOException e ) {
179+ return null ;
180+ }
181+ }
182+
183+ @ SuppressWarnings ("unchecked" )
184+ private static Map <String , String > loadProperties (FileObject config ) throws IOException {
185+ try (InputStream is = config .openInputStream ();
186+ InputStreamReader isr = new InputStreamReader (is , "UTF-8" )){
187+ Properties props = new Properties ();
188+ props .load (isr );
189+ return (Map <String , String >) new HashMap (props );
190+ }
191+ }
192+
131193 protected static class Constants {
132194
133195 public static final String DEFAULT_DAO_SUFFIX = "Impl" ;
134196
135197 public static final String DEFAULT_ENTITY_FIELD_PREFIX = "$" ;
136198
199+ public static final String DEFAULT_CONFIG_PATH = "doma.compile.config" ;
200+
137201 public static final String DEFAULT_LOMBOK_ALL_ARGS_CONSTRUCTOR = "lombok.AllArgsConstructor" ;
138202
139203 public static final String DEFAULT_LOMBOK_VALUE = "lombok.Value" ;
0 commit comments