forked from e-is/ucoinj
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Code refactoring on ES service - Add ES start detection, then run init commands
- Loading branch information
Showing
39 changed files
with
1,482 additions
and
2,050 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,56 +23,126 @@ | |
*/ | ||
|
||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableSet; | ||
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.duniter.core.client.config.ConfigurationProvider; | ||
import org.duniter.core.client.model.local.Peer; | ||
import org.duniter.core.exception.TechnicalException; | ||
import org.duniter.core.util.StringUtils; | ||
import org.duniter.elasticsearch.service.ServiceLocator; | ||
import org.elasticsearch.common.component.*; | ||
import org.elasticsearch.common.inject.Inject; | ||
import org.elasticsearch.common.logging.ESLogger; | ||
import org.elasticsearch.common.logging.ESLoggerFactory; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.nuiton.config.ApplicationConfig; | ||
import org.nuiton.config.ApplicationConfigHelper; | ||
import org.nuiton.config.ApplicationConfigProvider; | ||
import org.nuiton.config.ArgumentsParserException; | ||
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; | ||
import java.util.Set; | ||
|
||
import static org.nuiton.i18n.I18n.t; | ||
|
||
/** | ||
* Access to configuration options | ||
* @author Benoit Lavenier <[email protected]> | ||
* @since 1.0 | ||
*/ | ||
public class PluginSettings { | ||
/** Logger. */ | ||
private ESLogger log = ESLoggerFactory.getLogger(PluginSettings.class.getName()); | ||
public class PluginSettings extends AbstractLifecycleComponent<PluginSettings> { | ||
|
||
private org.elasticsearch.common.settings.Settings settings; | ||
private Settings settings; | ||
|
||
/** | ||
* Delegate application config. | ||
*/ | ||
protected final ApplicationConfig applicationConfig; | ||
protected final org.duniter.core.client.config.Configuration clientConfig; | ||
|
||
@Inject | ||
public PluginSettings(org.elasticsearch.common.settings.Settings settings) { | ||
super(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); | ||
clientConfig = new org.duniter.core.client.config.Configuration(applicationConfig); | ||
Configuration.setInstance(clientConfig); | ||
|
||
} | ||
|
||
@Override | ||
protected void doStart() { | ||
|
||
|
||
// get all config providers | ||
Set<ApplicationConfigProvider> providers = | ||
ImmutableSet.of(new ConfigurationProvider()); | ||
|
||
// load all default options | ||
ApplicationConfigHelper.loadAllDefaultOption(applicationConfig, | ||
providers); | ||
|
||
// Ovverides defaults | ||
String baseDir = settings.get("path.home"); | ||
applicationConfig.setDefaultOption(ConfigurationOption.BASEDIR.getKey(), baseDir); | ||
applicationConfig.setDefaultOption(ConfigurationOption.NODE_HOST.getKey(), getNodeBmaHost()); | ||
applicationConfig.setDefaultOption(ConfigurationOption.NODE_PORT.getKey(), String.valueOf(getNodeBmaPort())); | ||
applicationConfig.setDefaultOption(ConfigurationOption.NODE_PROTOCOL.getKey(), getNodeBmaPort() == 443 ? "https" : "http"); | ||
|
||
try { | ||
applicationConfig.parse(new String[]{}); | ||
|
||
} catch (ArgumentsParserException e) { | ||
throw new TechnicalException(t("duniter4j.config.parse.error"), e); | ||
} | ||
|
||
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(); | ||
} | ||
applicationConfig.setOption( | ||
ConfigurationOption.BASEDIR.getKey(), | ||
appBasedir.getAbsolutePath()); | ||
|
||
// Init i18n | ||
try { | ||
initI18n(); | ||
} | ||
catch(IOException e) { | ||
logger.error(String.format("Could not init i18n: %s", e.getMessage()), e); | ||
} | ||
} | ||
|
||
@Override | ||
protected void doStop() { | ||
|
||
} | ||
|
||
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"); | ||
@Override | ||
protected void doClose() { | ||
|
||
//initI18n(); | ||
} | ||
|
||
public String getNodeBmaHost() { | ||
|
@@ -91,10 +161,14 @@ public int getIndexBulkSize() { | |
return settings.getAsInt("duniter.bulk.size", 1000); | ||
} | ||
|
||
public String getIndexStringAnalyzer() { | ||
public String getDefaultStringAnalyzer() { | ||
return settings.get("duniter.string.analyzer", "english"); | ||
} | ||
|
||
public boolean reloadIndices() { | ||
return settings.getAsBoolean("duniter.indices.reload", false); | ||
} | ||
|
||
public File getTempDirectory() { | ||
return Configuration.instance().getTempDirectory(); | ||
} | ||
|
@@ -103,31 +177,46 @@ public boolean isDevMode() { | |
return settings.getAsBoolean("duniter.dev.enable", false); | ||
} | ||
|
||
/* */ | ||
public Peer checkAndGetPeer() { | ||
if (StringUtils.isBlank(getNodeBmaHost())) { | ||
logger.error("ERROR: node host is required"); | ||
System.exit(-1); | ||
return null; | ||
} | ||
if (getNodeBmaPort() <= 0) { | ||
logger.error("ERROR: node port is required"); | ||
System.exit(-1); | ||
return null; | ||
} | ||
|
||
Peer peer = new Peer(getNodeBmaHost(), getNodeBmaPort()); | ||
return peer; | ||
} | ||
|
||
/* protected methods */ | ||
|
||
protected void initI18n() throws IOException { | ||
Configuration config = Configuration.instance(); | ||
|
||
// --------------------------------------------------------------------// | ||
// init i18n | ||
// --------------------------------------------------------------------// | ||
|
||
File i18nDirectory = new File(Configuration.instance().getDataDirectory(), "i18n"); | ||
File i18nDirectory = new File(clientConfig.getDataDirectory(), "i18n"); | ||
if (i18nDirectory.exists()) { | ||
// clean i18n cache | ||
FileUtils.cleanDirectory(i18nDirectory); | ||
} | ||
|
||
FileUtils.forceMkdir(i18nDirectory); | ||
|
||
if (log.isDebugEnabled()) { | ||
log.debug("I18N directory: " + i18nDirectory); | ||
if (logger.isDebugEnabled()) { | ||
logger.debug("I18N directory: " + i18nDirectory); | ||
} | ||
|
||
Locale i18nLocale = config.getI18nLocale(); | ||
Locale i18nLocale = clientConfig.getI18nLocale(); | ||
|
||
if (log.isInfoEnabled()) { | ||
log.info(String.format("Starts i18n with locale [%s] at [%s]", | ||
if (logger.isInfoEnabled()) { | ||
logger.info(String.format("Starts i18n with locale [%s] at [%s]", | ||
i18nLocale, i18nDirectory)); | ||
} | ||
I18n.init(new UserI18nInitializer( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.