Skip to content

Commit 3fdb9f0

Browse files
ubonesskimchy
authored andcommitted
Enabled the option of configuring plugin types in the settings. This will also help in tests when testing plugin related functionality
1 parent 2dd8267 commit 3fdb9f0

File tree

2 files changed

+159
-18
lines changed

2 files changed

+159
-18
lines changed

src/main/java/org/elasticsearch/plugins/PluginsService.java

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
import com.google.common.collect.Maps;
2525
import com.google.common.collect.Sets;
2626
import org.elasticsearch.ElasticSearchException;
27+
import org.elasticsearch.common.Strings;
2728
import org.elasticsearch.common.collect.MapBuilder;
2829
import org.elasticsearch.common.component.AbstractComponent;
2930
import org.elasticsearch.common.component.LifecycleComponent;
30-
import org.elasticsearch.common.inject.Inject;
3131
import org.elasticsearch.common.inject.Module;
3232
import org.elasticsearch.common.settings.ImmutableSettings;
3333
import org.elasticsearch.common.settings.Settings;
@@ -64,15 +64,26 @@ static class OnModuleReference {
6464
}
6565
}
6666

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+
*/
6872
public PluginsService(Settings settings, Environment environment) {
6973
super(settings);
7074
this.environment = environment;
7175

72-
loadPluginsIntoClassLoader();
73-
74-
// first, find all the ones that are in the classpath
7576
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();
7687
plugins.putAll(loadPluginsFromClasspath(settings));
7788
Set<String> sitePlugins = sitePlugins();
7889

@@ -85,7 +96,7 @@ public PluginsService(Settings settings, Environment environment) {
8596
}
8697
}
8798
if (!missingPlugins.isEmpty()) {
88-
throw new ElasticSearchException("Missing mandatory plugins " + missingPlugins);
99+
throw new ElasticSearchException("Missing mandatory plugins [" + Strings.collectionToDelimitedString(missingPlugins, ", ") + "]");
89100
}
90101
}
91102

@@ -321,18 +332,8 @@ private Map<String, Plugin> loadPluginsFromClasspath(Settings settings) {
321332
try {
322333
is = pluginUrl.openStream();
323334
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);
336337
plugins.put(plugin.name(), plugin);
337338
} catch (Exception e) {
338339
logger.warn("failed to load plugin from [" + pluginUrl + "]", e);
@@ -348,4 +349,25 @@ private Map<String, Plugin> loadPluginsFromClasspath(Settings settings) {
348349
}
349350
return plugins;
350351
}
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+
}
351373
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. ElasticSearch licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.test.integration.node;
21+
22+
import org.elasticsearch.ElasticSearchException;
23+
import org.elasticsearch.common.component.AbstractLifecycleComponent;
24+
import org.elasticsearch.common.component.Lifecycle;
25+
import org.elasticsearch.common.component.LifecycleComponent;
26+
import org.elasticsearch.common.inject.Inject;
27+
import org.elasticsearch.common.inject.Singleton;
28+
import org.elasticsearch.common.settings.Settings;
29+
import org.elasticsearch.node.internal.InternalNode;
30+
import org.elasticsearch.plugins.AbstractPlugin;
31+
import org.elasticsearch.test.integration.AbstractNodesTests;
32+
import org.testng.annotations.Test;
33+
34+
import java.util.ArrayList;
35+
import java.util.Collection;
36+
37+
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
38+
import static org.hamcrest.MatcherAssert.assertThat;
39+
import static org.hamcrest.Matchers.is;
40+
41+
/**
42+
*
43+
*/
44+
public class InternalNodeTests extends AbstractNodesTests {
45+
46+
@Test
47+
public void testDefaultPluginConfiguration() throws Exception {
48+
49+
Settings settings = settingsBuilder()
50+
.put("plugin.types", TestPlugin.class.getName())
51+
.build();
52+
53+
InternalNode node = (InternalNode) buildNode("test", settings);
54+
55+
TestService service = node.injector().getInstance(TestService.class);
56+
assertThat(service.state.initialized(), is(true));
57+
node.start();
58+
assertThat(service.state.started(), is(true));
59+
node.stop();
60+
assertThat(service.state.stopped(), is(true));
61+
node.close();
62+
assertThat(service.state.closed(), is(true));
63+
}
64+
65+
public static class TestPlugin extends AbstractPlugin {
66+
67+
public TestPlugin() {
68+
}
69+
70+
@Override
71+
public String name() {
72+
return "test";
73+
}
74+
75+
@Override
76+
public String description() {
77+
return "test plugin";
78+
}
79+
80+
@Override
81+
public Collection<Class<? extends LifecycleComponent>> services() {
82+
Collection<Class<? extends LifecycleComponent>> services = new ArrayList<Class<? extends LifecycleComponent>>();
83+
services.add(TestService.class);
84+
return services;
85+
}
86+
}
87+
88+
@Singleton
89+
public static class TestService extends AbstractLifecycleComponent<TestService> {
90+
91+
private Lifecycle state;
92+
93+
@Inject
94+
public TestService(Settings settings) {
95+
super(settings);
96+
logger.info("initializing");
97+
state = new Lifecycle();
98+
}
99+
100+
@Override
101+
protected void doStart() throws ElasticSearchException {
102+
logger.info("starting");
103+
state.moveToStarted();
104+
}
105+
106+
@Override
107+
protected void doStop() throws ElasticSearchException {
108+
logger.info("stopping");
109+
state.moveToStopped();
110+
}
111+
112+
@Override
113+
protected void doClose() throws ElasticSearchException {
114+
logger.info("closing");
115+
state.moveToClosed();
116+
}
117+
}
118+
119+
}

0 commit comments

Comments
 (0)