Enabled the option of configuring plugin types in the settings. This will also help in tests when testing plugin related functionality

This commit is contained in:
uboness 2012-08-21 18:40:42 +02:00 committed by Shay Banon
parent 2dd82675fb
commit 3fdb9f0a27
2 changed files with 159 additions and 18 deletions

View File

@ -24,10 +24,10 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
@ -64,15 +64,26 @@ public class PluginsService extends AbstractComponent {
}
}
@Inject
/**
* Constructs a new PluginService
* @param settings The settings of the system
* @param environment The environment of the system
*/
public PluginsService(Settings settings, Environment environment) {
super(settings);
this.environment = environment;
loadPluginsIntoClassLoader();
// first, find all the ones that are in the classpath
Map<String, Plugin> plugins = Maps.newHashMap();
//first we load all the default plugins from the settings
String[] defaultPluginsClasses = settings.getAsArray("plugin.types");
for (String pluginClass : defaultPluginsClasses) {
Plugin plugin = loadPlugin(pluginClass, settings);
plugins.put(plugin.name(), plugin);
}
// now, find all the ones that are in the classpath
loadPluginsIntoClassLoader();
plugins.putAll(loadPluginsFromClasspath(settings));
Set<String> sitePlugins = sitePlugins();
@ -85,7 +96,7 @@ public class PluginsService extends AbstractComponent {
}
}
if (!missingPlugins.isEmpty()) {
throw new ElasticSearchException("Missing mandatory plugins " + missingPlugins);
throw new ElasticSearchException("Missing mandatory plugins [" + Strings.collectionToDelimitedString(missingPlugins, ", ") + "]");
}
}
@ -321,18 +332,8 @@ public class PluginsService extends AbstractComponent {
try {
is = pluginUrl.openStream();
pluginProps.load(is);
String sPluginClass = pluginProps.getProperty("plugin");
Class<? extends Plugin> pluginClass = (Class<? extends Plugin>) settings.getClassLoader().loadClass(sPluginClass);
Plugin plugin;
try {
plugin = pluginClass.getConstructor(Settings.class).newInstance(settings);
} catch (NoSuchMethodException e) {
try {
plugin = pluginClass.getConstructor().newInstance();
} catch (NoSuchMethodException e1) {
throw new ElasticSearchException("No constructor for [" + pluginClass + "]");
}
}
String pluginClassName = pluginProps.getProperty("plugin");
Plugin plugin = loadPlugin(pluginClassName, settings);
plugins.put(plugin.name(), plugin);
} catch (Exception e) {
logger.warn("failed to load plugin from [" + pluginUrl + "]", e);
@ -348,4 +349,25 @@ public class PluginsService extends AbstractComponent {
}
return plugins;
}
private Plugin loadPlugin(String className, Settings settings) {
try {
Class<? extends Plugin> pluginClass = (Class<? extends Plugin>) settings.getClassLoader().loadClass(className);
try {
return pluginClass.getConstructor(Settings.class).newInstance(settings);
} catch (NoSuchMethodException e) {
try {
return pluginClass.getConstructor().newInstance();
} catch (NoSuchMethodException e1) {
throw new ElasticSearchException("No constructor for [" + pluginClass + "]. A plugin class must " +
"have either an empty default constructor or a single argument constructor accepting a " +
"Settings instance");
}
}
} catch (Exception e) {
throw new ElasticSearchException("Failed to load plugin class [" + className + "]", e);
}
}
}

View File

@ -0,0 +1,119 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.test.integration.node;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.component.Lifecycle;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.Singleton;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.internal.InternalNode;
import org.elasticsearch.plugins.AbstractPlugin;
import org.elasticsearch.test.integration.AbstractNodesTests;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.Collection;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
/**
*
*/
public class InternalNodeTests extends AbstractNodesTests {
@Test
public void testDefaultPluginConfiguration() throws Exception {
Settings settings = settingsBuilder()
.put("plugin.types", TestPlugin.class.getName())
.build();
InternalNode node = (InternalNode) buildNode("test", settings);
TestService service = node.injector().getInstance(TestService.class);
assertThat(service.state.initialized(), is(true));
node.start();
assertThat(service.state.started(), is(true));
node.stop();
assertThat(service.state.stopped(), is(true));
node.close();
assertThat(service.state.closed(), is(true));
}
public static class TestPlugin extends AbstractPlugin {
public TestPlugin() {
}
@Override
public String name() {
return "test";
}
@Override
public String description() {
return "test plugin";
}
@Override
public Collection<Class<? extends LifecycleComponent>> services() {
Collection<Class<? extends LifecycleComponent>> services = new ArrayList<Class<? extends LifecycleComponent>>();
services.add(TestService.class);
return services;
}
}
@Singleton
public static class TestService extends AbstractLifecycleComponent<TestService> {
private Lifecycle state;
@Inject
public TestService(Settings settings) {
super(settings);
logger.info("initializing");
state = new Lifecycle();
}
@Override
protected void doStart() throws ElasticSearchException {
logger.info("starting");
state.moveToStarted();
}
@Override
protected void doStop() throws ElasticSearchException {
logger.info("stopping");
state.moveToStopped();
}
@Override
protected void doClose() throws ElasticSearchException {
logger.info("closing");
state.moveToClosed();
}
}
}