org.apache.karaf.instance.main.Execute Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.apache.karaf.instance.command Show documentation
Show all versions of org.apache.karaf.instance.command Show documentation
Instance shell commands to manipulate Karaf child instances.
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.apache.karaf.instance.main;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.apache.karaf.instance.command.*;
import org.apache.karaf.shell.commands.Action;
import org.apache.karaf.shell.commands.Command;
import org.apache.karaf.shell.commands.basic.DefaultActionPreparator;
import org.apache.karaf.instance.command.ChangeSshPortCommand;
import org.apache.karaf.instance.core.internal.InstanceServiceImpl;
import org.fusesource.jansi.AnsiConsole;
public class Execute {
static Class extends Action> x = CreateCommand.class;
private static final Class>[] COMMAND_CLASSES = new Class[]{
ChangeOptsCommand.class,
ChangeRmiRegistryPortCommand.class,
ChangeRmiServerPortCommand.class,
ChangeSshPortCommand.class,
CloneCommand.class,
CreateCommand.class,
DestroyCommand.class,
ListCommand.class,
RenameCommand.class,
StartCommand.class,
StatusCommand.class,
StopCommand.class};
private static final Map> COMMANDS = new TreeMap>();
static {
for (Class> c : COMMAND_CLASSES) {
Command ann = c.getAnnotation(Command.class);
if (ann == null) {
continue;
}
COMMANDS.put(ann.name(), c);
}
}
// For testing
static boolean exitAllowed = true;
/**
* Environment variable for specifying extra options to the Karaf instance
* process kicked off from this Java process.
*/
private static final String ENV_KARAF_OPTS = "KARAF_OPTS";
/**
* System property for specifying extra options to the Karaf instance
* process kicked off from this Java process.
*/
private static final String PROP_KARAF_OPTS = "karaf.opts";
public static void main(String[] args) throws Exception {
AnsiConsole.systemInstall();
if (args.length == 0) {
listCommands();
exit(0);
}
String commandName = args[0];
Class> cls = COMMANDS.get(commandName);
if (cls == null) {
System.err.println("Command not found: " + commandName);
exit(-1);
}
String storage = System.getProperty("karaf.instances");
if (storage == null) {
System.err.println("System property 'karaf.instances' is not set. \n" +
"This property needs to be set to the full path of the instance.properties file.");
exit(-2);
}
File storageFile = new File(storage);
System.setProperty("user.dir", storageFile.getParentFile().getParentFile().getCanonicalPath());
try {
String karafOpts = System.getenv(ENV_KARAF_OPTS);
if (karafOpts != null) {
System.setProperty(PROP_KARAF_OPTS, karafOpts);
}
} catch (Exception e) {
System.err.println("Could not read KARAF_OPTS environment variable: " + e.getMessage());
if (System.getProperty("karaf.showStackTrace") != null) {
throw e;
}
}
Object command = cls.newInstance();
if (command instanceof InstanceCommandSupport) {
try {
execute((InstanceCommandSupport) command, storageFile, args);
} catch (Exception e) {
System.err.println("Error execution command '" + commandName + "': " + e.getMessage());
if (System.getProperty("karaf.showStackTrace") != null) {
throw e;
}
}
} else {
System.err.println("Not an instance command: " + commandName);
exit(-3);
}
}
static void execute(InstanceCommandSupport command, File storageFile, String[] args) throws Exception {
DefaultActionPreparator dap = new DefaultActionPreparator();
List