24
24
import com .google .common .collect .Maps ;
25
25
import com .google .common .collect .Sets ;
26
26
import org .elasticsearch .ElasticSearchException ;
27
+ import org .elasticsearch .common .Strings ;
27
28
import org .elasticsearch .common .collect .MapBuilder ;
28
29
import org .elasticsearch .common .component .AbstractComponent ;
29
30
import org .elasticsearch .common .component .LifecycleComponent ;
30
- import org .elasticsearch .common .inject .Inject ;
31
31
import org .elasticsearch .common .inject .Module ;
32
32
import org .elasticsearch .common .settings .ImmutableSettings ;
33
33
import org .elasticsearch .common .settings .Settings ;
@@ -64,15 +64,26 @@ static class OnModuleReference {
64
64
}
65
65
}
66
66
67
- @ Inject
67
+ /**
68
+ * Constructs a new PluginService
69
+ * @param settings The settings of the system
70
+ * @param environment The environment of the system
71
+ */
68
72
public PluginsService (Settings settings , Environment environment ) {
69
73
super (settings );
70
74
this .environment = environment ;
71
75
72
- loadPluginsIntoClassLoader ();
73
-
74
- // first, find all the ones that are in the classpath
75
76
Map <String , Plugin > plugins = Maps .newHashMap ();
77
+
78
+ //first we load all the default plugins from the settings
79
+ String [] defaultPluginsClasses = settings .getAsArray ("plugin.types" );
80
+ for (String pluginClass : defaultPluginsClasses ) {
81
+ Plugin plugin = loadPlugin (pluginClass , settings );
82
+ plugins .put (plugin .name (), plugin );
83
+ }
84
+
85
+ // now, find all the ones that are in the classpath
86
+ loadPluginsIntoClassLoader ();
76
87
plugins .putAll (loadPluginsFromClasspath (settings ));
77
88
Set <String > sitePlugins = sitePlugins ();
78
89
@@ -85,7 +96,7 @@ public PluginsService(Settings settings, Environment environment) {
85
96
}
86
97
}
87
98
if (!missingPlugins .isEmpty ()) {
88
- throw new ElasticSearchException ("Missing mandatory plugins " + missingPlugins );
99
+ throw new ElasticSearchException ("Missing mandatory plugins [ " + Strings . collectionToDelimitedString ( missingPlugins , ", " ) + "]" );
89
100
}
90
101
}
91
102
@@ -321,18 +332,8 @@ private Map<String, Plugin> loadPluginsFromClasspath(Settings settings) {
321
332
try {
322
333
is = pluginUrl .openStream ();
323
334
pluginProps .load (is );
324
- String sPluginClass = pluginProps .getProperty ("plugin" );
325
- Class <? extends Plugin > pluginClass = (Class <? extends Plugin >) settings .getClassLoader ().loadClass (sPluginClass );
326
- Plugin plugin ;
327
- try {
328
- plugin = pluginClass .getConstructor (Settings .class ).newInstance (settings );
329
- } catch (NoSuchMethodException e ) {
330
- try {
331
- plugin = pluginClass .getConstructor ().newInstance ();
332
- } catch (NoSuchMethodException e1 ) {
333
- throw new ElasticSearchException ("No constructor for [" + pluginClass + "]" );
334
- }
335
- }
335
+ String pluginClassName = pluginProps .getProperty ("plugin" );
336
+ Plugin plugin = loadPlugin (pluginClassName , settings );
336
337
plugins .put (plugin .name (), plugin );
337
338
} catch (Exception e ) {
338
339
logger .warn ("failed to load plugin from [" + pluginUrl + "]" , e );
@@ -348,4 +349,25 @@ private Map<String, Plugin> loadPluginsFromClasspath(Settings settings) {
348
349
}
349
350
return plugins ;
350
351
}
352
+
353
+ private Plugin loadPlugin (String className , Settings settings ) {
354
+ try {
355
+ Class <? extends Plugin > pluginClass = (Class <? extends Plugin >) settings .getClassLoader ().loadClass (className );
356
+ try {
357
+ return pluginClass .getConstructor (Settings .class ).newInstance (settings );
358
+ } catch (NoSuchMethodException e ) {
359
+ try {
360
+ return pluginClass .getConstructor ().newInstance ();
361
+ } catch (NoSuchMethodException e1 ) {
362
+ throw new ElasticSearchException ("No constructor for [" + pluginClass + "]. A plugin class must " +
363
+ "have either an empty default constructor or a single argument constructor accepting a " +
364
+ "Settings instance" );
365
+ }
366
+ }
367
+
368
+ } catch (Exception e ) {
369
+ throw new ElasticSearchException ("Failed to load plugin class [" + className + "]" , e );
370
+ }
371
+
372
+ }
351
373
}
0 commit comments