Skip to content

Commit

Permalink
[all] Add the "embedded" CLI option for the SREs.
Browse files Browse the repository at this point in the history
The SREs may be embedded into another application.
The CLI-Embedded entry in the SRE manifest enables to launch the SRE in
embedded mode.

This option will enable Janus to be embedded into the SARL product at
run-time.

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Sep 10, 2016
1 parent 0ba0167 commit 10453cc
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 26 deletions.
Expand Up @@ -598,6 +598,10 @@ public String getProgramArguments(ILaunchConfiguration configuration) throws Cor
assert cliOptions != null;
String options = null;

if (this.accessor.isEmbeddedSRE(configuration)) {
options = join(options, cliOptions.get(SREConstants.MANIFEST_CLI_EMBEDDED));
}

if (this.accessor.getShowLogoFlag(configuration)) {
options = join(options, cliOptions.get(SREConstants.MANIFEST_CLI_SHOW_LOGO));
} else {
Expand Down
Expand Up @@ -100,6 +100,10 @@ public final class SREConstants {
*/
public static final String MANIFEST_CLI_NO_MORE_OPTION = "CLI-No-More-Option"; //$NON-NLS-1$

/** Field name for CLI option for marking the SRE as embedded in another application..
*/
public static final String MANIFEST_CLI_EMBEDDED = "CLI-Embedded"; //$NON-NLS-1$

/** Attribute name of the SRE's library path (the jar file).
*/
public static final String XML_LIBRARY_PATH = "libraryPath"; //$NON-NLS-1$
Expand Down
Expand Up @@ -112,6 +112,8 @@ public class StandardSREInstall extends AbstractSREInstall {

private String cliNoMoreOption;

private String cliEmbedded;

private String manifestMainClass;

private String manifestName;
Expand Down Expand Up @@ -275,6 +277,7 @@ protected void resolveDirtyFields(boolean forceSettings) {
this.cliBootAgentContextID = sarlSection.getValue(SREConstants.MANIFEST_CLI_BOOT_AGENT_CONTEXT_ID);
this.cliSreOffline = sarlSection.getValue(SREConstants.MANIFEST_CLI_SRE_OFFLINE);
this.cliNoMoreOption = sarlSection.getValue(SREConstants.MANIFEST_CLI_NO_MORE_OPTION);
this.cliEmbedded = sarlSection.getValue(SREConstants.MANIFEST_CLI_EMBEDDED);
//
// Program arguments
final String programArgs = Strings.nullToEmpty(sarlSection.getValue(SREConstants.MANIFEST_PROGRAM_ARGUMENTS));
Expand Down Expand Up @@ -535,6 +538,7 @@ public Map<String, String> getAvailableCommandLineOptions() {
putIfNotempty(options, SREConstants.MANIFEST_CLI_BOOT_AGENT_CONTEXT_ID, this.cliBootAgentContextID);
putIfNotempty(options, SREConstants.MANIFEST_CLI_SRE_OFFLINE, this.cliSreOffline);
putIfNotempty(options, SREConstants.MANIFEST_CLI_NO_MORE_OPTION, this.cliNoMoreOption);
putIfNotempty(options, SREConstants.MANIFEST_CLI_EMBEDDED, this.cliEmbedded);
this.optionBuffer = new SoftReference<>(options);
}
return Collections.unmodifiableMap(options);
Expand Down
Expand Up @@ -440,6 +440,7 @@ private void buildManifest(ManifestUpdater updater, String mainClass) {
updater.addSREAttribute(SREConstants.MANIFEST_CLI_HIDE_INFO, cmd.getHideInfo());
updater.addSREAttribute(SREConstants.MANIFEST_CLI_SHOW_INFO, cmd.getShowInfo());
updater.addSREAttribute(SREConstants.MANIFEST_CLI_NO_MORE_OPTION, cmd.getNoMoreOption());
updater.addSREAttribute(SREConstants.MANIFEST_CLI_EMBEDDED, cmd.getEmbedded());
updater.addSREAttribute(SREConstants.MANIFEST_VM_ARGUMENTS, getVmArguments());
updater.addSREAttribute(SREConstants.MANIFEST_PROGRAM_ARGUMENTS, getApplicationArguments());
}
Expand Down
Expand Up @@ -58,6 +58,9 @@ public class CommandLineOptions {
@Parameter
private String offline;

@Parameter
private String embedded;

@Parameter
private String noMoreOption = "--"; //$NON-NLS-1$

Expand All @@ -78,15 +81,16 @@ public boolean equals(Object obj) {
&& Objects.equals(getDefaultContextId(), other.getDefaultContextId())
&& Objects.equals(getRandomContextId(), other.getRandomContextId())
&& Objects.equals(getBootAgentContextId(), other.getBootAgentContextId())
&& Objects.equals(getNoMoreOption(), other.getNoMoreOption());
&& Objects.equals(getNoMoreOption(), other.getNoMoreOption())
&& Objects.equals(getEmbedded(), other.getEmbedded());
}
return false;
}

@Override
public int hashCode() {
return Objects.hash(this.bootAgentContextId, this.defaultContextId, this.hideInfo, this.hideLogo,
this.noMoreOption, this.offline, this.randomContextId, this.showInfo);
this.noMoreOption, this.offline, this.randomContextId, this.showInfo, this.embedded);
}

/** Replies the command line option for hiding the SRE logo.
Expand Down Expand Up @@ -239,7 +243,6 @@ public String getNoMoreOption() {
return this.noMoreOption;
}


/** Change the command line option for indicates that no more option will be present on the rest of the command line.
*
* @param noMoreOptionCLIOption the command line option.
Expand All @@ -248,6 +251,21 @@ public void setNoMoreOption(String noMoreOptionCLIOption) {
this.noMoreOption = noMoreOptionCLIOption;
}

/** Replies the command line option for indicating that the SRE is embedded in another application.
*
* @return the command line option.
*/
public String getEmbedded() {
return this.embedded;
}

/** Change the command line option for indicating that the SRE is embedded in another application.
*
* @param embeddedCLIOption the command line option.
*/
public void setEmbedded(String embeddedCLIOption) {
this.embedded = embeddedCLIOption;
}

/** Put the string representation of the properties of this object into the given buffer.
*
Expand Down
Expand Up @@ -39,6 +39,7 @@
import com.google.inject.Module;
import com.hazelcast.logging.LoggingService;
import io.janusproject.kernel.Kernel;
import io.janusproject.services.executor.ChuckNorrisException;
import io.janusproject.services.network.NetworkConfig;
import io.janusproject.util.LoggerCreator;
import org.apache.commons.cli.CommandLine;
Expand Down Expand Up @@ -96,7 +97,7 @@ private Boot() {
* @param args - the CLI arguments given to the program.
* @return the arguments that are not recognized as CLI options.
*/
@SuppressWarnings("checkstyle:cyclomaticcomplexity")
@SuppressWarnings({"checkstyle:cyclomaticcomplexity", "checkstyle:npathcomplexity"})
public static String[] parseCommandLine(String[] args) {
final CommandLineParser parser = new DefaultParser();
try {
Expand All @@ -109,6 +110,7 @@ public static String[] parseCommandLine(String[] args) {
}

boolean noLogo = false;
boolean embedded = false;
int verbose = LoggerCreator.toInt(JanusConfig.VERBOSE_LEVEL_VALUE);

final Iterator<Option> optIterator = cmd.iterator();
Expand Down Expand Up @@ -169,12 +171,21 @@ public static String[] parseCommandLine(String[] args) {
case "nologo": //$NON-NLS-1$
noLogo = true;
break;
case "embedded": //$NON-NLS-1$
embedded = true;
break;
default:
}
}

// Change the verbosity
setVerboseLevel(verbose);
// Do nothing at exit
if (embedded) {
setExiter(() -> {
throw new ChuckNorrisException();
});
}
// Show the Janus logo?
if (noLogo || verbose == 0) {
setProperty(JanusConfig.JANUS_LOGO_SHOW_NAME, Boolean.FALSE.toString());
Expand Down Expand Up @@ -246,6 +257,9 @@ public static void main(String[] args) {
assert agent != null;

startJanus((Class<? extends Module>) null, (Class<? extends Agent>) agent, freeArgs);
} catch (ChuckNorrisException exception) {
// Be silent
return;
} catch (Throwable e) {
showError(Locale.getString(Boot.class, "LAUNCHING_ERROR", //$NON-NLS-1$
e.getLocalizedMessage()), e);
Expand Down Expand Up @@ -287,6 +301,9 @@ public static void setConsoleLogger(PrintStream stream) {
public static Options getOptions() {
final Options options = new Options();

options.addOption("e", "embedded", false, //$NON-NLS-1$//$NON-NLS-2$
Locale.getString(Boot.class, "CLI_HELP_E")); //$NON-NLS-1$

options.addOption("B", "bootid", false, //$NON-NLS-1$//$NON-NLS-2$
Locale.getString(Boot.class, "CLI_HELP_B", //$NON-NLS-1$
JanusConfig.BOOT_DEFAULT_CONTEXT_ID_NAME, JanusConfig.RANDOM_DEFAULT_CONTEXT_ID_NAME));
Expand Down Expand Up @@ -659,7 +676,7 @@ public static Kernel startJanus(Module startupModule, Class<? extends Agent> age
* @return the tool for exiting the application.
*/
public static Exiter getExiter() {
return applicationExiter == null ? new StandardExiter() : applicationExiter;
return applicationExiter == null ? () -> System.exit(ERROR_EXIT_CODE) : applicationExiter;
}

/**
Expand All @@ -679,6 +696,7 @@ public static void setExiter(Exiter exiter) {
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
@FunctionalInterface
public interface Exiter {

/**
Expand All @@ -688,25 +706,4 @@ public interface Exiter {

}

/**
* Tool for exiting from the application.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
private static class StandardExiter implements Exiter {

StandardExiter() {
//
}

@Override
public void exit() {
System.exit(ERROR_EXIT_CODE);
}

}

}
@@ -1,3 +1,4 @@
CLI_HELP_E = Janus is run inside another application.
CLI_HELP_H = Display this help.
CLI_HELP_F = Load system properties from a file.
CLI_HELP_D = Define a system property.
Expand Down
@@ -1,3 +1,4 @@
CLI_HELP_E = Janus est exécuté dans une autre application.
CLI_HELP_H = Affichage de cette aide.
CLI_HELP_F = Chargement de propriétés système depuis un fichier.
CLI_HELP_D = Définition d''une propriété système.
Expand Down
Expand Up @@ -444,6 +444,7 @@ public Map<String, String> getAvailableCommandLineOptions() {
options.put(SREConstants.MANIFEST_CLI_RANDOM_CONTEXT_ID, "--randomid"); //$NON-NLS-1$
options.put(SREConstants.MANIFEST_CLI_BOOT_AGENT_CONTEXT_ID, "--bootid"); //$NON-NLS-1$
options.put(SREConstants.MANIFEST_CLI_SRE_OFFLINE, "--offline"); //$NON-NLS-1$
options.put(SREConstants.MANIFEST_CLI_EMBEDDED, "--embedded"); //$NON-NLS-1$
options.put(SREConstants.MANIFEST_CLI_NO_MORE_OPTION, "--"); //$NON-NLS-1$
return Collections.unmodifiableMap(options);
}
Expand Down

0 comments on commit 10453cc

Please sign in to comment.