33import com .github .cleverage .elasticsearch .annotations .IndexMapping ;
44import com .github .cleverage .elasticsearch .annotations .IndexName ;
55import com .github .cleverage .elasticsearch .annotations .IndexType ;
6+ import com .typesafe .config .ConfigValue ;
67import org .apache .commons .lang3 .StringUtils ;
78import org .reflections .Reflections ;
8- import play .Application ;
9- import play .Configuration ;
109import play .Logger ;
10+ import play .api .Configuration ;
11+ import play .api .Environment ;
1112import play .libs .Json ;
12- import play .libs .ReflectionsCache ;
13+ import play .libs .MyReflectionsCache ;
14+ import scala .Option ;
15+ import scala .Tuple2 ;
16+ import scala .collection .Iterator ;
1317
14- import java .util .*;
18+ import java .util .Arrays ;
19+ import java .util .HashMap ;
20+ import java .util .HashSet ;
21+ import java .util .LinkedList ;
22+ import java .util .Map ;
23+ import java .util .Set ;
24+
25+ ;
1526
1627
1728/**
@@ -94,24 +105,34 @@ public class IndexConfig {
94105 public boolean routingReqd = false ;
95106
96107 /**
97- * Play application
108+ * Play configuration
109+ */
110+ public Configuration configuration ;
111+
112+ /**
113+ * Play environment
98114 */
99- public Application application ;
115+ public Environment environment ;
116+
117+ private static final Option <scala .collection .immutable .Set <String >> empty = Option .apply (new scala .collection .immutable .HashSet <>());
118+
119+ public IndexConfig (Environment environment , Configuration configuration ) {
120+
121+ this .environment = environment ;
122+ this .configuration = configuration ;
100123
101- public IndexConfig (Application app ) {
102- this .application = app ;
103- this .client = app .configuration ().getString ("elasticsearch.client" );
104- this .sniffing = app .configuration ().getBoolean ("elasticsearch.sniff" , true );
105- this .local = app .configuration ().getBoolean ("elasticsearch.local" );
106- this .localConfig = app .configuration ().getString ("elasticsearch.config.resource" );
107- this .clusterName = app .configuration ().getString ("elasticsearch.cluster.name" );
124+ this .client = (configuration .getString ("elasticsearch.client" , empty ) == Option .apply ((String )null )) ? "" : configuration .getString ("elasticsearch.client" , empty ).get ();
125+ this .clusterName = (configuration .getString ("elasticsearch.cluster.name" , empty ) == Option .apply ((String )null )) ? "" : configuration .getString ("elasticsearch.cluster.name" , empty ).get ();
126+ this .indexClazzs = (configuration .getString ("elasticsearch.index.clazzs" , empty ) == Option .apply ((String )null )) ? "" : configuration .getString ("elasticsearch.index.clazzs" , empty ).get ();
127+ String indexNameConf = (configuration .getString ("elasticsearch.index.name" , empty ) == Option .apply ((String )null )) ? "" : configuration .getString ("elasticsearch.index.name" , empty ).get ();
108128
129+ this .sniffing = (configuration .getBoolean ("elasticsearch.sniff" ) == Option .apply (null )) ? false : (Boolean )configuration .getBoolean ("elasticsearch.sniff" ).get ();
130+ this .local = (configuration .getBoolean ("elasticsearch.local" ) == Option .apply (null )) ? false : (Boolean )configuration .getBoolean ("elasticsearch.local" ).get ();
131+ this .localConfig = (configuration .getString ("elasticsearch.config.resource" , empty ) == Option .apply ((String )null )) ? "" : configuration .getString ("elasticsearch.config.resource" , empty ).get ();
109132
110- this .showRequest = app .configuration ().getBoolean ("elasticsearch.index.show_request" , false );
111- this .dropOnShutdown = app .configuration ().getBoolean ("elasticsearch.index.dropOnShutdown" , false );
112- this .indexClazzs = app .configuration ().getString ("elasticsearch.index.clazzs" );
133+ this .showRequest = (configuration .getBoolean ("elasticsearch.index.show_request" ) == Option .apply (null )) ? false : (Boolean )configuration .getBoolean ("elasticsearch.index.show_request" ).get ();
134+ this .dropOnShutdown = (configuration .getBoolean ("elasticsearch.index.dropOnShutdown" ) == Option .apply (null )) ? false : (Boolean )configuration .getBoolean ("elasticsearch.index.dropOnShutdown" ).get ();
113135
114- String indexNameConf = app .configuration ().getString ("elasticsearch.index.name" );
115136 if (indexNameConf != null ) {
116137 LinkedList <String > indexNamesL = new LinkedList <String >();
117138 String [] indexNamesTab = indexNameConf .split ("," );
@@ -136,7 +157,7 @@ public IndexConfig(Application app) {
136157 }
137158
138159 private void loadSettingsFromConfig (String indexName ) {
139- String setting = application . configuration (). getString ("elasticsearch." + indexName + ".settings" );
160+ String setting = ( configuration . getString ( "elasticsearch." + indexName + ".settings" , empty ) == Option . apply (( String ) null )) ? "" : configuration . getString ("elasticsearch." + indexName + ".settings" , empty ). get ( );
140161 if (StringUtils .isNotEmpty (setting )) {
141162 indexSettings .put (indexName , setting );
142163 }
@@ -155,7 +176,7 @@ public void loadFromAnnotations() {
155176 // Loading class and annotation for set mapping if is present
156177 Logger .debug ("ElasticSearch : Registering class " + aClass );
157178
158- klass = Class .forName (aClass , true , application . classloader ());
179+ klass = Class .forName (aClass , true , configuration . getClass (). getClassLoader ());
159180 Object o = klass .newInstance ();
160181
161182 String indexType = getIndexType (o );
@@ -167,6 +188,7 @@ public void loadFromAnnotations() {
167188 indexMappings .put (path , indexMapping );
168189 }
169190 } catch (Throwable e ) {
191+ e .printStackTrace ();
170192 Logger .error (e .getMessage ());
171193 }
172194 }
@@ -177,16 +199,22 @@ public void loadFromAnnotations() {
177199 * @param indexName
178200 */
179201 private void loadMappingFromConfig (String indexName ) {
180- Configuration mappingConfig = application .configuration ().getConfig ("elasticsearch." + indexName + ".mappings" );
202+ Configuration mappingConfig = (configuration .getConfig ("elasticsearch." + indexName + ".mappings" ) == Option .apply ((Configuration )null )) ? null : configuration .getConfig ("elasticsearch." + indexName + ".mappings" ).get ();
203+
181204 if (mappingConfig != null ) {
182- Map <String , Object > mappings = mappingConfig .asMap ();
183- for (String indexType : mappings .keySet ()) {
205+
206+ Iterator <Tuple2 <String , ConfigValue >> iter = mappingConfig .entrySet ().iterator ();
207+
208+ while (iter .hasNext ()) {
209+ Tuple2 <String , ConfigValue > mapping = iter .next ();
210+ String indexType = mapping ._1 ();
184211 IndexQueryPath indexQueryPath = new IndexQueryPath (indexName , indexType );
185- if (mappings .get (indexType ) instanceof String ) {
186- indexMappings .put (indexQueryPath , (String ) mappings .get (indexType ));
212+
213+ if (mapping ._2 ().unwrapped () instanceof String ) {
214+ indexMappings .put (indexQueryPath , (String ) mapping ._2 ().unwrapped ());
187215 } else {
188216 try {
189- indexMappings .put (indexQueryPath , Json .toJson (mappings . get ( indexType )).toString ());
217+ indexMappings .put (indexQueryPath , Json .toJson (mapping . _2 (). unwrapped ( )).toString ());
190218 } catch (Exception e ) {
191219 Logger .warn ("Incorrect value in elasticsearch.index.mappings" , e );
192220 }
@@ -231,11 +259,14 @@ private Set<String> getClazzs() {
231259 for (String load : toLoad ) {
232260 load = load .trim ();
233261 if (load .endsWith (".*" )) {
234- Reflections reflections = ReflectionsCache .getReflections (application .classloader (), load .substring (0 , load .length () - 2 ));
235- for (Class c :reflections .getTypesAnnotatedWith (IndexName .class )){
262+
263+ //TODO: remove the "play.libs.{MyClasspath,MyReflectionsCache}" local patch when we upgrade to play 2.5.0
264+ Reflections reflections = MyReflectionsCache .getReflections (environment .classLoader (), load .substring (0 , load .length () - 2 ));
265+
266+ for (Class c : reflections .getTypesAnnotatedWith (IndexName .class )){
236267 classes .add (c .getName ());
237268 }
238- for (Class c :reflections .getTypesAnnotatedWith (IndexType .class )){
269+ for (Class c : reflections .getTypesAnnotatedWith (IndexType .class )){
239270 classes .add (c .getName ());
240271 }
241272 } else {
0 commit comments