Skip to content
Snippets Groups Projects
Commit dae5be0c authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

Refactoring code, as a classical ES plugin

parent 6f297822
Branches
Tags
No related merge requests found
Showing
with 357 additions and 784 deletions
......@@ -127,7 +127,7 @@ public enum ConfigurationOption implements ConfigOptionDef {
false),
NODE_CURRENCY(
"duniter4j.node.currency",
"duniter4j.node.blockchain",
n("duniter4j.config.option.node.currency.description"),
"meta_brouzouf",
String.class,
......
......@@ -57,7 +57,7 @@ public interface CurrencyDao extends Bean, EntityDao<Currency> {
Long getCurrencyIdByName(String currencyName);
/**
* Return a (cached) list of currency ids
* Return a (cached) list of blockchain ids
* @return
*/
Set<Long> getCurrencyIds();
......
......@@ -25,10 +25,7 @@ package org.duniter.core.client.service;
import org.duniter.core.beans.Bean;
import org.duniter.core.beans.BeanFactory;
import org.duniter.core.client.service.bma.BlockchainRemoteService;
import org.duniter.core.client.service.bma.NetworkRemoteService;
import org.duniter.core.client.service.bma.TransactionRemoteService;
import org.duniter.core.client.service.bma.WotRemoteService;
import org.duniter.core.client.service.bma.*;
import org.duniter.core.client.service.elasticsearch.CurrencyRegistryRemoteService;
import org.duniter.core.client.service.local.CurrencyService;
import org.duniter.core.client.service.local.PeerService;
......@@ -56,6 +53,10 @@ public class ServiceLocator implements Closeable {
protected ServiceLocator() {
}
protected ServiceLocator(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public void init() {
initBeanFactory();
}
......@@ -132,7 +133,6 @@ public class ServiceLocator implements Closeable {
return getBean(CurrencyRegistryRemoteService.class);
}
public <S extends Bean> S getBean(Class<S> clazz) {
if (beanFactory == null) {
initBeanFactory();
......@@ -148,4 +148,8 @@ public class ServiceLocator implements Closeable {
}
}
public BeanFactory getBeanFactory() {
return beanFactory;
}
}
......@@ -42,10 +42,12 @@ public class BeanFactory implements Closeable{
private final Map<Class<?>, Object> beansCache;
private final ServiceLoader<Bean> beansLoader;
private final Map<Class<? extends Bean>, Class<? extends Bean>> beansClassMap;
public BeanFactory() {
beansCache = new HashMap<>();
beansLoader = ServiceLoader.load(Bean.class);
beansClassMap = new HashMap<>();
}
public <S extends Bean> S getBean(Class<S> clazz) {
......@@ -73,6 +75,9 @@ public class BeanFactory implements Closeable{
public <S extends Bean> S newBean(Class<S> clazz) {
if (log.isTraceEnabled()) {
log.trace(String.format("Asking bean on type [%s]...", clazz.getName()));
}
for (Bean bean: beansLoader) {
......@@ -84,9 +89,31 @@ public class BeanFactory implements Closeable{
}
}
for (Map.Entry<Class<? extends Bean>, Class<? extends Bean>> beanDef : beansClassMap.entrySet()) {
if (log.isTraceEnabled()) {
log.trace(String.format(" Check against type [%s]", beanDef.getKey().getName()));
}
if (clazz.equals(beanDef.getKey())) {
if (log.isDebugEnabled()) {
log.debug(String.format("Creating new bean of type [%s] with class [%s]", clazz.getName(), beanDef.getValue().getName()));
}
Class<? extends Bean> beanDefClass = beanDef.getValue();
try {
Bean bean = beanDefClass.newInstance();
if (clazz.isInstance(bean)) {
return (S) beanDefClass.newInstance();
}
} catch(Exception e) {
// skip
}
}
}
throw new TechnicalException(String.format("Unable to create bean with type [%s]: not configured for the service loader [%s]", clazz.getName(), Bean.class.getCanonicalName()));
}
@Override
public void close() throws IOException {
for(Object bean: beansCache.values()) {
......@@ -103,4 +130,20 @@ public class BeanFactory implements Closeable{
}
}
}
public <S extends Bean> BeanFactory bind(Class<S> def, Class<? extends S> beanClass) {
if (log.isTraceEnabled()) {
log.trace(String.format("Bind on type [%s] with class [%s]", def.getName(), beanClass.getName()));
}
beansClassMap.put(def, beanClass);
return this;
}
public BeanFactory add(Class<? extends Bean> beanClass) {
if (log.isTraceEnabled()) {
log.trace(String.format("Adding bean of type [%s]", beanClass.getName()));
}
beansClassMap.put(beanClass, beanClass);
return this;
}
}
......@@ -24,9 +24,13 @@
</i18n.bundleCsvFile>
<config.i18nBundleName>${i18n.bundleOutputName}</config.i18nBundleName>
<maven.jar.main.class>
<!--maven.jar.main.class>
org.duniter.elasticsearch.cli.Main
</maven.jar.main.class-->
<maven.jar.main.class>
org.elasticsearch.bootstrap.Elasticsearch
</maven.jar.main.class>
<exec.classpathScope>provided</exec.classpathScope>
<duniter4j-elasticsearch.config>${project.basedir}/src/test/resources/duniter4j-elasticsearch-test.properties</duniter4j-elasticsearch.config>
</properties>
......@@ -68,7 +72,7 @@
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<!--scope>provided</scope-->
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
......@@ -195,12 +199,12 @@
<attach>true</attach>
<finalName>${bundlePrefix}</finalName>
<descriptors>
<!--descriptor>
${basedir}/src/main/assembly/standalone.xml
</descriptor-->
<descriptor>
src/main/assembly/standalone.xml
${basedir}/src/main/assembly/plugin.xml
</descriptor>
<descriptors>
<descriptor>${basedir}/src/main/assembly/plugin.xml</descriptor>
</descriptors>
</descriptors>
</configuration>
</execution>
......@@ -265,6 +269,10 @@
<key>duniter4j-elasticsearch.config</key>
<value>${duniter4j-elasticsearch.config}</value>
</property>
<property>
<key>es.path.home</key>
<value>${basedir}/src/test/es-home</value>
</property>
</systemProperties>
</configuration>
</execution>
......
......@@ -15,6 +15,7 @@
<useTransitiveFiltering>true</useTransitiveFiltering>
<excludes>
<exclude>org.elasticsearch:elasticsearch</exclude>
<exclude>net.java.dev.jna:jna</exclude>
</excludes>
</dependencySet>
</dependencySets>
......@@ -31,6 +32,7 @@
<outputDirectory/>
<includes>
<include>plugin-descriptor.properties</include>
<include>plugin-security.policy</include>
</includes>
</fileSet>
</fileSets>
......
app.name=duniter4j
duniter4j.config.path=sqqs
duniter4j.version=${project.version}
duniter4j.site.url=${project.url}
duniter4j.inceptionYear=${project.inceptionYear}
......
name=duniter4j-elasticsearch
#description=${project.description}
description=duniter4j-elasticsearch
description=${project.description}
version=1.0
#version=${project.version}
site=true
version=${project.version}
site=false
jvm=true
classname=org.duniter.elasticsearch.plugin.Plugin
classname=org.duniter.elasticsearch.Plugin
java.version=1.7
elasticsearch.version=2.3.1
isolated=false
\ No newline at end of file
elasticsearch.version=2.3.3
isolated=true
\ No newline at end of file
package org.duniter.elasticsearch.plugin;
package org.duniter.elasticsearch;
/*
* #%L
......@@ -24,34 +24,74 @@ package org.duniter.elasticsearch.plugin;
import com.google.common.collect.Lists;
import org.duniter.elasticsearch.action.RestModule;
import org.duniter.elasticsearch.job.BlockIndexer;
import org.duniter.elasticsearch.security.SecurityModule;
import org.duniter.elasticsearch.service.ServiceModule;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import java.util.Collection;
public class Plugin extends org.elasticsearch.plugins.Plugin {
private ESLogger log = ESLoggerFactory.getLogger(Plugin.class.getName());
private org.elasticsearch.common.settings.Settings settings;
private boolean disable;
@Inject public Plugin(org.elasticsearch.common.settings.Settings settings) {
this.settings = settings;
this.disable = settings.getAsBoolean("duniter.disable", false);
}
@Override
public String name() {
return "duniter4j-elasticsearch";
return "duniter";
}
@Override
public String description() {
return "Duniter4j ElasticSearch Plugin";
return "Duniter ElasticSearch Plugin";
}
@Override
public Collection<Module> nodeModules() {
Collection<Module> modules = Lists.newArrayList();
if (disable) {
log.warn(description() + " has been disabled.");
return modules;
}
modules.add(new SecurityModule());
modules.add(new RestModule());
modules.add(new ServiceModule());
return modules;
}
@Override
public Collection<Class<? extends LifecycleComponent>> nodeServices() {
Collection<Class<? extends LifecycleComponent>> components = Lists.newArrayList();
if (disable) {
return components;
}
components.add(BlockIndexer.class);
//components.add(PluginSettings.class);
// Market
//components.add(CategoryMarketService.class);
//components.add(RecordMarketService.class);
// Registry
//components.add(CurrencyRegistryService.class);
//components.add(CategoryRegistryService.class);
//components.add(CitiesRegistryService.class);
//components.add(RecordRegistryService.class);
// BC
//components.add(BlockBlockchainService.class);
return components;
}
/* -- protected methods -- */
protected void initInstance() {
}
\ No newline at end of file
}
\ No newline at end of file
package org.duniter.elasticsearch;
/*
* #%L
* UCoin Java Client :: Core API
* %%
* Copyright (C) 2014 - 2015 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import org.apache.commons.io.FileUtils;
import org.duniter.core.client.config.Configuration;
import org.duniter.core.client.config.ConfigurationOption;
import org.elasticsearch.common.component.Lifecycle;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.component.LifecycleListener;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.nuiton.config.ApplicationConfig;
import org.nuiton.i18n.I18n;
import org.nuiton.i18n.init.DefaultI18nInitializer;
import org.nuiton.i18n.init.UserI18nInitializer;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
/**
* Access to configuration options
* @author Benoit Lavenier <benoit.lavenier@e-is.pro>
* @since 1.0
*/
public class PluginSettings {
/** Logger. */
private ESLogger log = ESLoggerFactory.getLogger(PluginSettings.class.getName());
private org.elasticsearch.common.settings.Settings settings;
/**
* Delegate application config.
*/
protected final ApplicationConfig applicationConfig;
@Inject
public PluginSettings(org.elasticsearch.common.settings.Settings settings) {
this.settings = settings;
this.applicationConfig = new ApplicationConfig();
// Cascade the application config to the client module
org.duniter.core.client.config.Configuration clientConfig = new org.duniter.core.client.config.Configuration(applicationConfig);
org.duniter.core.client.config.Configuration.setInstance(clientConfig);
String baseDir = settings.get("es.path.home");
applicationConfig.setOption(ConfigurationOption.BASEDIR.getKey(), baseDir);
applicationConfig.setOption(ConfigurationOption.NODE_HOST.getKey(), getNodeBmaHost());
applicationConfig.setOption(ConfigurationOption.NODE_PORT.getKey(), String.valueOf(getNodeBmaPort()));
applicationConfig.setOption(ConfigurationOption.NODE_PROTOCOL.getKey(), getNodeBmaPort() == 443 ? "https" : "http");
//initI18n();
}
public String getNodeBmaHost() {
return settings.get("duniter.host", "cgeek.fr");
}
public int getNodeBmaPort() {
return settings.getAsInt("duniter.port", 9330);
}
public boolean isIndexBulkEnable() {
return settings.getAsBoolean("duniter.bulk.enable", true);
}
public int getIndexBulkSize() {
return settings.getAsInt("duniter.bulk.size", 1000);
}
public String getIndexStringAnalyzer() {
return settings.get("duniter.string.analyzer", "english");
}
public File getTempDirectory() {
return Configuration.instance().getTempDirectory();
}
public boolean isDevMode() {
return settings.getAsBoolean("duniter.dev.enable", false);
}
/* */
protected void initI18n() throws IOException {
Configuration config = Configuration.instance();
// --------------------------------------------------------------------//
// init i18n
// --------------------------------------------------------------------//
File i18nDirectory = new File(Configuration.instance().getDataDirectory(), "i18n");
if (i18nDirectory.exists()) {
// clean i18n cache
FileUtils.cleanDirectory(i18nDirectory);
}
FileUtils.forceMkdir(i18nDirectory);
if (log.isDebugEnabled()) {
log.debug("I18N directory: " + i18nDirectory);
}
Locale i18nLocale = config.getI18nLocale();
if (log.isInfoEnabled()) {
log.info(String.format("Starts i18n with locale [%s] at [%s]",
i18nLocale, i18nDirectory));
}
I18n.init(new UserI18nInitializer(
i18nDirectory, new DefaultI18nInitializer(getI18nBundleName())),
i18nLocale);
}
protected String getI18nBundleName() {
return "duniter4j-elasticsearch-i18n";
}
}
......@@ -33,13 +33,13 @@ import org.elasticsearch.common.inject.Module;
public class RestModule extends AbstractModule implements Module {
@Override protected void configure() {
bind(RestCurrencyIndexAction.class).asEagerSingleton();
//bind(RestCurrencyIndexAction.class).asEagerSingleton();
bind(RestMarketRecordIndexAction.class).asEagerSingleton();
bind(RestRegistryRecordIndexAction.class).asEagerSingleton();
//bind(RestRegistryRecordIndexAction.class).asEagerSingleton();
bind(RestSecurityGetChallengeAction.class).asEagerSingleton();
bind(RestSecurityAuthAction.class).asEagerSingleton();
//bind(RestSecurityGetChallengeAction.class).asEagerSingleton();
//bind(RestSecurityAuthAction.class).asEagerSingleton();
}
}
\ No newline at end of file
......@@ -34,7 +34,7 @@ public class RestCurrencyIndexAction extends BaseRestHandler {
@Inject
public RestCurrencyIndexAction(Settings settings, RestController controller, Client client) {
super(settings, controller, client);
controller.registerHandler(RestRequest.Method.POST, "/currency", this);
controller.registerHandler(RestRequest.Method.POST, "/blockchain", this);
}
@Override
......
......@@ -23,7 +23,7 @@ package org.duniter.elasticsearch.action.market;
*/
import org.duniter.core.exception.BusinessException;
import org.duniter.elasticsearch.service.ServiceLocator;
import org.duniter.elasticsearch.service.market.RecordMarketService;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.ESLogger;
......@@ -39,17 +39,20 @@ public class RestMarketRecordIndexAction extends BaseRestHandler {
private static final ESLogger log = ESLoggerFactory.getLogger(RestMarketRecordIndexAction.class.getName());
private RecordMarketService service;
@Inject
public RestMarketRecordIndexAction(Settings settings, RestController controller, Client client) {
public RestMarketRecordIndexAction(Settings settings, RestController controller, Client client, RecordMarketService service) {
super(settings, controller, client);
controller.registerHandler(POST, "/market/record", this);
this.service = service;
}
@Override
protected void handleRequest(final RestRequest request, RestChannel restChannel, Client client) throws Exception {
try {
String recordId = ServiceLocator.instance().getMarketRecordIndexerService().indexRecordFromJson(request.content().toUtf8());
String recordId = service.indexRecordFromJson(request.content().toUtf8());
restChannel.sendResponse(new BytesRestResponse(OK, recordId));
}
......
......@@ -23,7 +23,7 @@ package org.duniter.elasticsearch.action.registry;
*/
import org.duniter.core.exception.BusinessException;
import org.duniter.elasticsearch.service.ServiceLocator;
import org.duniter.elasticsearch.service.registry.RecordRegistryService;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.ESLogger;
......@@ -39,17 +39,20 @@ public class RestRegistryRecordIndexAction extends BaseRestHandler {
private static final ESLogger log = ESLoggerFactory.getLogger(RestRegistryRecordIndexAction.class.getName());
private RecordRegistryService service;
@Inject
public RestRegistryRecordIndexAction(Settings settings, RestController controller, Client client) {
public RestRegistryRecordIndexAction(Settings settings, RestController controller, Client client, RecordRegistryService service) {
super(settings, controller, client);
controller.registerHandler(POST, "/registry/record", this);
this.service = service;
}
@Override
protected void handleRequest(final RestRequest request, RestChannel restChannel, Client client) throws Exception {
try {
String recordId = ServiceLocator.instance().getRegistryRecordIndexerService().indexRecordFromJson(request.content().toUtf8());
String recordId = service.indexRecordFromJson(request.content().toUtf8());
restChannel.sendResponse(new BytesRestResponse(OK, recordId));
}
......
......@@ -23,10 +23,10 @@ package org.duniter.elasticsearch.action.security;
*/
import org.duniter.core.client.model.bma.gson.GsonUtils;
import org.duniter.core.client.service.ServiceLocator;
import org.duniter.core.util.StringUtils;
import org.duniter.elasticsearch.security.challenge.ChallengeMessageStore;
import org.duniter.elasticsearch.security.token.SecurityTokenStore;
import org.duniter.elasticsearch.service.ServiceLocator;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.ESLogger;
......
package org.duniter.elasticsearch.cli;
/*
* #%L
* UCoin Java Client :: Core API
* %%
* Copyright (C) 2014 - 2015 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.google.common.collect.Lists;
import org.duniter.core.exception.TechnicalException;
import org.duniter.core.util.CommandLinesUtils;
import org.duniter.core.util.StringUtils;
import org.duniter.elasticsearch.config.Configuration;
import org.duniter.elasticsearch.config.ConfigurationAction;
import org.duniter.elasticsearch.service.ServiceLocator;
import org.duniter.elasticsearch.util.Desktop;
import org.duniter.elasticsearch.util.DesktopPower;
import org.apache.commons.io.FileUtils;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.nuiton.config.ApplicationConfig;
import org.nuiton.i18n.I18n;
import org.nuiton.i18n.init.DefaultI18nInitializer;
import org.nuiton.i18n.init.UserI18nInitializer;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
public class Main {
private static String TITLE_SEPARATOR_LINE = "************************************************\n";
private static String TITLE_EMPTY_LINE = "*\n";
private static String TITLE = TITLE_SEPARATOR_LINE
+ TITLE_EMPTY_LINE
+ "* %s\n" // title
+ TITLE_EMPTY_LINE
+ "* %s\n" // sub-title
+ TITLE_EMPTY_LINE + TITLE_SEPARATOR_LINE;
private static final ESLogger log = ESLoggerFactory.getLogger(Main.class.getName());
public static void main(String[] args) {
Main main = new Main();
main.run(args);
}
public void run(String[] args) {
if (log.isInfoEnabled()) {
log.info("Starting duniter4j :: ElasticSearch Indexer with arguments " + Arrays.toString(args));
}
// By default, start
if (args == null || args.length == 0) {
args = new String[] { "--start" };
}
List<String> arguments = Lists.newArrayList(Arrays.asList(args));
arguments.removeAll(Arrays.asList(ConfigurationAction.HELP.aliases));
// Could override config file name (useful for dev)
String configFile = "duniter4j-elasticsearch.config";
if (System.getProperty(configFile) != null) {
configFile = System.getProperty(configFile);
configFile = configFile.replaceAll("\\\\", "/");
}
// Create configuration
Configuration config = new Configuration(configFile, args) {
protected void addAlias(ApplicationConfig applicationConfig) {
super.addAlias(applicationConfig);
// Add custom alias
};
};
Configuration.setInstance(config);
// Init i18n
try {
initI18n(config);
} catch (IOException e) {
throw new TechnicalException("i18n initialization failed", e);
}
// Add hook on system
addShutdownHook();
// Run all actions
try {
config.getApplicationConfig().doAllAction();
} catch (Exception e) {
log.error(e.getMessage(), e);
}
if (config.isDaemon()) {
while (true) {
try {
Thread.sleep(5000);
} catch(InterruptedException e) {
// Nothing
}
}
}
else {
if (arguments.size() > 0) {
// Check if auto-quit if need
boolean quit = true;
for (String startAlias : ConfigurationAction.START.aliases) {
if (arguments.contains(startAlias)) {
quit = false;
break;
}
}
// If scheduling is running, wait quit instruction
if (!quit) {
while (!quit) {
String userInput = CommandLinesUtils.readInput(
String.format(TITLE,
"duniter4j :: Elasticsearch successfully started",
">> To quit, press [Q] or [enter]"),
"Q", true);
quit = StringUtils.isNotBlank(userInput) && "Q".equalsIgnoreCase(userInput);
}
}
}
}
// shutdown
shutdown();
}
/* -- protected methods -- */
/**
* Shutdown all services
*/
protected static void shutdown() {
if (ServiceLocator.instance() != null) {
try {
ServiceLocator.instance().close();
}
catch(IOException e) {
// Silent is gold
}
}
log.info("duniter4j :: ElasticSearch Indexer successfully stopped");
}
protected void initI18n(Configuration config) throws IOException {
// --------------------------------------------------------------------//
// init i18n
// --------------------------------------------------------------------//
File i18nDirectory = new File(config.getDataDirectory(), "i18n");
if (i18nDirectory.exists()) {
// clean i18n cache
FileUtils.cleanDirectory(i18nDirectory);
}
FileUtils.forceMkdir(i18nDirectory);
if (log.isDebugEnabled()) {
log.debug("I18N directory: " + i18nDirectory);
}
Locale i18nLocale = config.getI18nLocale();
if (log.isInfoEnabled()) {
log.info(String.format("Starts i18n with locale [%s] at [%s]",
i18nLocale, i18nDirectory));
}
I18n.init(new UserI18nInitializer(
i18nDirectory, new DefaultI18nInitializer(getI18nBundleName())),
i18nLocale);
}
protected String getI18nBundleName() {
return "duniter4j-elasticsearch-i18n";
}
/**
* Add an OS shutdown hook, to close application on shutdown
*/
private void addShutdownHook() {
// Use shutdownHook to close context on System.exit
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
shutdown();
}
}));
// Add DesktopPower to hook computer shutdown
DesktopPower desktopPower = Desktop.getDesktopPower();
if (desktopPower != null) {
desktopPower.addListener(new DesktopPower.Listener() {
@Override
public void quit() {
if (ServiceLocator.instance() != null) {
try {
ServiceLocator.instance().close();
}
catch(IOException e) {
// Silent is gold
}
}
}
});
}
}
}
......@@ -24,21 +24,6 @@ package org.duniter.elasticsearch.cli.action;
* #L%
*/
import org.duniter.core.client.model.bma.BlockchainParameters;
import org.duniter.core.client.model.bma.gson.GsonUtils;
import org.duniter.core.client.model.local.Peer;
import org.duniter.core.client.service.bma.BlockchainRemoteService;
import org.duniter.core.util.websocket.WebsocketClientEndpoint;
import org.duniter.elasticsearch.config.Configuration;
import org.duniter.elasticsearch.service.ServiceLocator;
import org.duniter.elasticsearch.service.currency.BlockIndexerService;
import org.duniter.elasticsearch.service.market.MarketCategoryIndexerService;
import org.duniter.elasticsearch.service.market.MarketRecordIndexerService;
import org.duniter.elasticsearch.service.registry.RegistryCategoryIndexerService;
import org.duniter.elasticsearch.service.registry.RegistryCitiesIndexerService;
import org.duniter.elasticsearch.service.registry.RegistryCurrencyIndexerService;
import org.duniter.elasticsearch.service.registry.RegistryRecordIndexerService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -46,6 +31,7 @@ public class IndexerCliAction {
/* Logger */
private static final Logger log = LoggerFactory.getLogger(IndexerCliAction.class);
/*
public void indexBlocksFromNode() {
final boolean async = ServiceLocator.instance().getElasticSearchService().isNodeInstance();
......@@ -53,21 +39,21 @@ public class IndexerCliAction {
Runnable runnable = new Runnable() {
@Override
public void run() {
Configuration config = Configuration.instance();
PluginSettings config = PluginSettings.instance();
final Peer peer = checkConfigAndGetPeer(config);
final BlockIndexerService blockIndexerService = ServiceLocator.instance().getBlockIndexerService();
final BlockBlockchainService blockIndexerService = ServiceLocator.instance().getBlockIndexerService();
// Will create the currency if not exist
// Will create the blockchain if not exist
blockIndexerService.indexLastBlocks(peer);
if (async) {
ServiceLocator.instance().getBlockchainRemoteService().addNewBlockListener(peer, new WebsocketClientEndpoint.MessageHandler() {
@Override
public void handleMessage(String message) {
String currencyName = GsonUtils.getValueFromJSONAsString(message, "currency");
blockIndexerService.indexBlockAsJson(peer, message, true /*refresh*/, true /*wait*/);
blockIndexerService.indexCurrentBlockAsJson(currencyName, message, true /*wait*/);
}
String currencyName = GsonUtils.getValueFromJSONAsString(message, "blockchain");
*///blockIndexerService.indexBlockAsJson(peer, message, true /*refresh*/, true /*wait*/);
//blockIndexerService.indexCurrentBlockAsJson(currencyName, message, true /*wait*/);
/* }
});
}
}
......@@ -92,18 +78,18 @@ public class IndexerCliAction {
}
public void resetAllCurrencies() {
RegistryCurrencyIndexerService currencyIndexerService = ServiceLocator.instance().getRegistryCurrencyIndexerService();
CurrencyRegistryService currencyIndexerService = ServiceLocator.instance().getRegistryCurrencyIndexerService();
currencyIndexerService.deleteAllCurrencies();
}
public void resetDataBlocks() {
BlockchainRemoteService blockchainService = ServiceLocator.instance().getBlockchainRemoteService();
BlockIndexerService indexerService = ServiceLocator.instance().getBlockIndexerService();
Configuration config = Configuration.instance();
BlockBlockchainService indexerService = ServiceLocator.instance().getBlockIndexerService();
PluginSettings config = PluginSettings.instance();
Peer peer = checkConfigAndGetPeer(config);
try {
// Get the currency name from node
// Get the blockchain name from node
BlockchainParameters parameter = blockchainService.getParameters(peer);
if (parameter == null) {
log.error(String.format("Could not connect to node [%s:%s]",
......@@ -114,7 +100,7 @@ public class IndexerCliAction {
log.info(String.format("Reset data for index [%s]", currencyName));
// Delete then create index on currency
// Delete then create index on blockchain
boolean indexExists = indexerService.existsIndex(currencyName);
if (indexExists) {
indexerService.deleteIndex(currencyName);
......@@ -129,8 +115,8 @@ public class IndexerCliAction {
}
public void resetMarketRecords() {
MarketRecordIndexerService recordIndexerService = ServiceLocator.instance().getMarketRecordIndexerService();
MarketCategoryIndexerService categoryIndexerService = ServiceLocator.instance().getMarketCategoryIndexerService();
RecordMarketService recordIndexerService = ServiceLocator.instance().getMarketRecordIndexerService();
CategoryMarketService categoryIndexerService = ServiceLocator.instance().getMarketCategoryIndexerService();
try {
// Delete then create index on records
......@@ -150,9 +136,9 @@ public class IndexerCliAction {
}
public void resetRegistry() {
RegistryRecordIndexerService recordIndexerService = ServiceLocator.instance().getRegistryRecordIndexerService();
RegistryCategoryIndexerService categoryIndexerService = ServiceLocator.instance().getRegistryCategoryIndexerService();
RegistryCitiesIndexerService citiesIndexerService = ServiceLocator.instance().getRegistryCitiesIndexerService();
RecordRegistryService recordIndexerService = ServiceLocator.instance().getRegistryRecordIndexerService();
CategoryRegistryService categoryIndexerService = ServiceLocator.instance().getRegistryCategoryIndexerService();
CitiesRegistryService citiesIndexerService = ServiceLocator.instance().getRegistryCitiesIndexerService();
try {
// Delete then create index on records
......@@ -179,11 +165,11 @@ public class IndexerCliAction {
} catch(Exception e) {
log.error("Error during reset registry records: " + e.getMessage(), e);
}
}
}*/
/* -- internal methods -- */
protected Peer checkConfigAndGetPeer(Configuration config) {
/*protected Peer checkConfigAndGetPeer(PluginSettings config) {
if (StringUtils.isBlank(config.getNodeBmaHost())) {
log.error("ERROR: node host is required");
System.exit(-1);
......@@ -197,5 +183,5 @@ public class IndexerCliAction {
Peer peer = new Peer(config.getNodeBmaHost(), config.getNodeBmaPort());
return peer;
}
}*/
}
package org.duniter.elasticsearch.cli.action;
/*
* #%L
* SIH-Adagio :: Shared
* $Id:$
* $HeadURL:$
* %%
* Copyright (C) 2012 - 2014 Ifremer
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import org.duniter.elasticsearch.config.Configuration;
import org.duniter.elasticsearch.service.ElasticSearchService;
import org.duniter.elasticsearch.service.ServiceLocator;
import org.duniter.elasticsearch.service.market.MarketCategoryIndexerService;
import org.duniter.elasticsearch.service.market.MarketRecordIndexerService;
import org.duniter.elasticsearch.service.registry.RegistryCurrencyIndexerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class NodeCliAction {
/* Logger */
private static final Logger log = LoggerFactory.getLogger(NodeCliAction.class);
public void start() {
Configuration config = Configuration.instance();
//config.setNodeElasticSearchLocal(false);
// Starting ES node
ElasticSearchService esService = ServiceLocator.instance().getElasticSearchService();
esService.startNode();
// Wait 5s, to avoid error on existsIndex()
try {
Thread t = new Thread() {
@Override
public void run() {
super.run();
try {
sleep(5000);
}
catch(InterruptedException e) {
// continue
}
catch(IllegalMonitorStateException e) {
e.printStackTrace();
}
}
};
t.start();
t.join();
}
catch(InterruptedException e) {
// continue
}
// Create indexed if need
{
// Currency index
RegistryCurrencyIndexerService currencyIndexerService = ServiceLocator.instance().getRegistryCurrencyIndexerService();
currencyIndexerService.createIndexIfNotExists();
// Product index
MarketRecordIndexerService recordIndexerService = ServiceLocator.instance().getMarketRecordIndexerService();
recordIndexerService.createIndexIfNotExists();
// Category index
MarketCategoryIndexerService categoryIndexerService = ServiceLocator.instance().getMarketCategoryIndexerService();
categoryIndexerService.createIndexIfNotExists();
}
}
/*public void stop() {
// Starting ES node
ElasticSearchService esService = ServiceLocator.instance().getElasticSearchService();
esService.stopNode();
}*/
/* -- protected methods -- */
}
package org.duniter.elasticsearch.config;
/*
* #%L
* UCoin Java Client :: Core API
* %%
* Copyright (C) 2014 - 2015 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.google.common.base.Charsets;
import org.duniter.core.exception.TechnicalException;
import org.nuiton.config.ApplicationConfig;
import org.nuiton.config.ApplicationConfigHelper;
import org.nuiton.config.ApplicationConfigProvider;
import org.nuiton.config.ArgumentsParserException;
import org.nuiton.util.version.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.Locale;
import java.util.Set;
import static org.nuiton.i18n.I18n.t;
/**
* Access to configuration options
* @author Benoit Lavenier <benoit.lavenier@e-is.pro>
* @since 1.0
*/
public class Configuration {
/** Logger. */
private static final Logger log = LoggerFactory.getLogger(Configuration.class);
/**
* Delegate application config.
*/
protected final ApplicationConfig applicationConfig;
private static Configuration instance;
public static Configuration instance() {
return instance;
}
public static void setInstance(Configuration instance) {
Configuration.instance = instance;
// Cascade the application config to the client module
org.duniter.core.client.config.Configuration clientConfig = new org.duniter.core.client.config.Configuration(instance.getApplicationConfig());
org.duniter.core.client.config.Configuration.setInstance(clientConfig);
}
protected final String[] optionKeyToNotSave;
protected File configFile;
public Configuration(ApplicationConfig applicationConfig) {
super();
this.applicationConfig = applicationConfig;
this.optionKeyToNotSave = null;
// Override application version
initVersion(applicationConfig);
}
public Configuration(String file, String... args) {
super();
this.applicationConfig = new ApplicationConfig();
this.applicationConfig.setEncoding(Charsets.UTF_8.name());
this.applicationConfig.setConfigFileName(file);
// get all config providers
Set<ApplicationConfigProvider> providers =
ApplicationConfigHelper.getProviders(null,
null,
null,
true);
// load all default options
ApplicationConfigHelper.loadAllDefaultOption(applicationConfig,
providers);
// Load actions
for (ApplicationConfigProvider provider : providers) {
applicationConfig.loadActions(provider.getActions());
}
// Define Alias
addAlias(applicationConfig);
// Override application version
initVersion(applicationConfig);
// get all transient and final option keys
Set<String> optionToSkip =
ApplicationConfigHelper.getTransientOptionKeys(providers);
if (log.isDebugEnabled()) {
log.debug("Option that won't be saved: " + optionToSkip);
}
optionKeyToNotSave = optionToSkip.toArray(new String[optionToSkip.size()]);
try {
applicationConfig.parse(args);
} catch (ArgumentsParserException e) {
throw new TechnicalException(t("duniter4j.config.parse.error"), e);
}
// TODO Review this, this is very dirty to do this...
File appBasedir = applicationConfig.getOptionAsFile(
ConfigurationOption.BASEDIR.getKey());
if (appBasedir == null) {
appBasedir = new File("");
}
if (!appBasedir.isAbsolute()) {
appBasedir = new File(appBasedir.getAbsolutePath());
}
if (appBasedir.getName().equals("..")) {
appBasedir = appBasedir.getParentFile().getParentFile();
}
if (appBasedir.getName().equals(".")) {
appBasedir = appBasedir.getParentFile();
}
if (log.isInfoEnabled()) {
log.info("Application basedir: " + appBasedir);
}
applicationConfig.setOption(
ConfigurationOption.BASEDIR.getKey(),
appBasedir.getAbsolutePath());
// Init other configuration
org.duniter.core.client.config.Configuration coreConfig = new org.duniter.core.client.config.Configuration(applicationConfig);
org.duniter.core.client.config.Configuration.setInstance(coreConfig);
}
/**
* Override the version default option, from the MANIFEST implementation version (if any)
* @param applicationConfig
*/
protected void initVersion(ApplicationConfig applicationConfig) {
// Override application version
String implementationVersion = this.getClass().getPackage().getSpecificationVersion();
if (implementationVersion != null) {
applicationConfig.setDefaultOption(
ConfigurationOption.VERSION.getKey(),
implementationVersion);
}
}
/**
* Add alias to the given ApplicationConfig. <p/>
* This method could be override to add specific alias
*
* @param applicationConfig
*/
protected void addAlias(ApplicationConfig applicationConfig) {
applicationConfig.addAlias("-h", "--option", ConfigurationOption.NODE_BMA_HOST.getKey());
applicationConfig.addAlias("--host", "--option", ConfigurationOption.NODE_BMA_HOST.getKey());
applicationConfig.addAlias("-p", "--option", ConfigurationOption.NODE_BMA_PORT.getKey());
applicationConfig.addAlias("--port", "--option", ConfigurationOption.NODE_BMA_PORT.getKey());
applicationConfig.addAlias("-esh", "--option", ConfigurationOption.HOST.getKey());
applicationConfig.addAlias("--es-host", "--option", ConfigurationOption.HOST.getKey());
applicationConfig.addAlias("-esp", "--option", ConfigurationOption.PORT.getKey());
applicationConfig.addAlias("--es-port", "--option", ConfigurationOption.PORT.getKey());
applicationConfig.addAlias("--daemon", "--option", ConfigurationOption.DAEMON.getKey(), Boolean.TRUE.toString());
}
public File getConfigFile() {
if (configFile == null) {
File dir = getBasedir();
if (dir == null || !dir.exists()) {
dir = new File(applicationConfig.getUserConfigDirectory());
}
configFile = new File(dir, applicationConfig.getConfigFileName());
}
return configFile;
}
/** @return {@link ConfigurationOption#BASEDIR} value */
public File getBasedir() {
File result = applicationConfig.getOptionAsFile(ConfigurationOption.BASEDIR.getKey());
return result;
}
/** @return {@link ConfigurationOption#DATA_DIRECTORY} value */
public File getDataDirectory() {
File result = applicationConfig.getOptionAsFile(ConfigurationOption.DATA_DIRECTORY.getKey());
return result;
}
/** @return {@link ConfigurationOption#TEMP_DIRECTORY} value */
public File getTempDirectory() {
File result = applicationConfig.getOptionAsFile(ConfigurationOption.TEMP_DIRECTORY.getKey());
return result;
}
/** @return {@link ConfigurationOption#PLUGINS_DIRECTORY} value */
public File getPluginsDirectory() {
File result = applicationConfig.getOptionAsFile(ConfigurationOption.PLUGINS_DIRECTORY.getKey());
return result;
}
/** @return {@link ConfigurationOption#MODE} value */
public boolean isFullMode() {
String launchMode = applicationConfig.getOption(ConfigurationOption.LAUNCH_MODE.getKey());
return "full".equals(launchMode);
}
public ApplicationConfig getApplicationConfig() {
return applicationConfig;
}
public Version getVersion() {
return applicationConfig.getOptionAsVersion(ConfigurationOption.VERSION.getKey());
}
public File getI18nDirectory() {
return applicationConfig.getOptionAsFile(
ConfigurationOption.I18N_DIRECTORY.getKey());
}
public Locale getI18nLocale() {
return applicationConfig.getOptionAsLocale(
ConfigurationOption.I18N_LOCALE.getKey());
}
public void setI18nLocale(Locale locale) {
applicationConfig.setOption(ConfigurationOption.I18N_LOCALE.getKey(), locale.toString());
}
public String getNodeBmaHost() {
return applicationConfig.getOption(ConfigurationOption.NODE_BMA_HOST.getKey());
}
public int getNodeBmaPort() {
return applicationConfig.getOptionAsInt(ConfigurationOption.NODE_BMA_PORT.getKey());
}
public String getHost() {
return applicationConfig.getOption(ConfigurationOption.HOST.getKey());
}
public String getNetworkHost() {
return applicationConfig.getOption(ConfigurationOption.NETWORK_HOST.getKey());
}
public int getPort() {
return applicationConfig.getOptionAsInt(ConfigurationOption.PORT.getKey());
}
public boolean isDaemon() {
return applicationConfig.getOptionAsBoolean(ConfigurationOption.DAEMON.getKey());
}
public boolean isEmbedded() {
return applicationConfig.getOptionAsBoolean(ConfigurationOption.EMBEDDED_ENABLE.getKey());
}
public void setEmbedded(boolean embeddedNode) {
applicationConfig.setOption(ConfigurationOption.EMBEDDED_ENABLE.getKey(), Boolean.toString(embeddedNode));
}
public boolean isLocal() {
return applicationConfig.getOptionAsBoolean(ConfigurationOption.LOCAL_ENABLE.getKey());
}
public boolean isHttpEnable() {
return applicationConfig.getOptionAsBoolean(ConfigurationOption.HTTP_ENABLE.getKey());
}
public String getClusterName() {
return applicationConfig.getOption(ConfigurationOption.CLUSTER_NAME.getKey());
}
public int getTaskExecutorQueueCapacity() {
return applicationConfig.getOptionAsInt(ConfigurationOption.TASK_EXECUTOR_QUEUE_CAPACITY.getKey());
}
public int getTaskExecutorTimeToIdle() {
return applicationConfig.getOptionAsInt(ConfigurationOption.TASK_EXECUTOR_TIME_TO_IDLE.getKey());
}
public boolean isIndexBulkEnable() {
return applicationConfig.getOptionAsBoolean(ConfigurationOption.INDEX_BULK_ENABLE.getKey());
}
public int getIndexBulkSize() {
return applicationConfig.getOptionAsInt(ConfigurationOption.INDEX_BULK_SIZE.getKey());
}
public String getIndexStringAnalyzer() {
return applicationConfig.getOption(ConfigurationOption.INDEX_STRING_ANALYZER.getKey());
}
}
package org.duniter.elasticsearch.config;
/*
* #%L
* SIH-Adagio :: Shared
* $Id:$
* $HeadURL:$
* %%
* Copyright (C) 2012 - 2014 Ifremer
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import org.duniter.elasticsearch.cli.action.HelpCliAction;
import org.duniter.elasticsearch.cli.action.IndexerCliAction;
import org.duniter.elasticsearch.cli.action.NodeCliAction;
import org.nuiton.config.ConfigActionDef;
public enum ConfigurationAction implements ConfigActionDef {
HELP(HelpCliAction.class.getName() + "#show", "--help"),
START(NodeCliAction.class.getName() + "#start", "start"),
INDEX_BLOCKS(IndexerCliAction.class.getName() + "#indexBlocksFromNode", "index"),
RESET_ALL_DATA(IndexerCliAction.class.getName() + "#resetAllData", "reset-data"),
RESET_BLOCKS(IndexerCliAction.class.getName() + "#resetDataBlocks", "reset-blocks"),
RESET_MARKET(IndexerCliAction.class.getName() + "#resetMarketRecords", "reset-market"),
RESET_REGISTRY(IndexerCliAction.class.getName() + "#resetRegistry", "reset-registry");
public String action;
public String[] aliases;
private ConfigurationAction(String action, String... aliases) {
this.action = action;
this.aliases = aliases;
}
@Override
public String getAction() {
return action;
}
@Override
public String[] getAliases() {
return aliases;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment