use of com.orientechnologies.common.console.annotation.ConsoleCommand in project orientdb by orientechnologies.
the class OConsoleDatabaseApp method properties.
@ConsoleCommand(description = "Return all configured properties")
public void properties() {
message("\nPROPERTIES:");
final List<ODocument> resultSet = new ArrayList<ODocument>();
for (Entry<String, String> p : properties.entrySet()) {
final ODocument row = new ODocument();
resultSet.add(row);
row.field("NAME", p.getKey());
row.field("VALUE", p.getValue());
}
final OTableFormatter formatter = new OTableFormatter(this);
formatter.writeRecords(resultSet, -1);
message("\n");
}
use of com.orientechnologies.common.console.annotation.ConsoleCommand in project orientdb by orientechnologies.
the class OConsoleDatabaseApp method profileStorageOff.
@ConsoleCommand(description = "Switches off storage profiling for issued set of commands and " + "returns reslut of profiling.")
public void profileStorageOff() {
final Collection<ODocument> result = (Collection<ODocument>) sqlCommand("profile", " storage off", "\nProfiling of storage is switched off\n", false);
final String profilingWasNotSwitchedOn = "Can not retrieve results of profiling, probably profiling was not switched on";
if (result == null) {
message(profilingWasNotSwitchedOn);
return;
}
final Iterator<ODocument> profilerIterator = result.iterator();
if (profilerIterator.hasNext()) {
final ODocument profilerDocument = profilerIterator.next();
if (profilerDocument == null)
message(profilingWasNotSwitchedOn);
else
message("Profiling result is : \n%s\n", profilerDocument.toJSON("prettyPrint"));
} else {
message(profilingWasNotSwitchedOn);
}
}
use of com.orientechnologies.common.console.annotation.ConsoleCommand in project orientdb by orientechnologies.
the class OConsoleDatabaseApp method info.
@ConsoleCommand(aliases = { "status" }, description = "Display information about the database", onlineHelp = "Console-Command-Info")
public void info() {
if (currentDatabaseName != null) {
message("\nCurrent database: " + currentDatabaseName + " (url=" + currentDatabase.getURL() + ")");
final OStorage stg = currentDatabase.getStorage();
if (stg instanceof OStorageRemote) {
listServers();
}
listProperties();
listClusters(null);
listClasses();
listIndexes();
}
}
use of com.orientechnologies.common.console.annotation.ConsoleCommand in project orientdb by orientechnologies.
the class OConsoleApplication method execute.
protected RESULT execute(String iCommand) {
iCommand = iCommand.trim();
if (iCommand.length() == 0)
// NULL LINE: JUMP IT
return RESULT.OK;
if (isComment(iCommand))
// COMMENT: JUMP IT
return RESULT.OK;
String[] commandWords = OStringParser.getWords(iCommand, wordSeparator);
for (String cmd : helpCommands) if (cmd.equals(commandWords[0])) {
if (iCommand.length() > cmd.length())
help(iCommand.substring(cmd.length() + 1));
else
help(null);
return RESULT.OK;
}
for (String cmd : exitCommands) if (cmd.equalsIgnoreCase(commandWords[0])) {
return RESULT.EXIT;
}
Method lastMethodInvoked = null;
final StringBuilder lastCommandInvoked = new StringBuilder(1024);
String commandLowerCase = "";
for (int i = 0; i < commandWords.length; i++) {
if (i > 0) {
commandLowerCase += " ";
}
commandLowerCase += commandWords[i].toLowerCase();
}
for (Entry<Method, Object> entry : getConsoleMethods().entrySet()) {
final Method m = entry.getKey();
final String methodName = m.getName();
final ConsoleCommand ann = m.getAnnotation(ConsoleCommand.class);
final StringBuilder commandName = new StringBuilder();
char ch;
int commandWordCount = 1;
for (int i = 0; i < methodName.length(); ++i) {
ch = methodName.charAt(i);
if (Character.isUpperCase(ch)) {
commandName.append(" ");
ch = Character.toLowerCase(ch);
commandWordCount++;
}
commandName.append(ch);
}
if (!commandLowerCase.equals(commandName.toString()) && !commandLowerCase.startsWith(commandName.toString() + " ")) {
if (ann == null)
continue;
String[] aliases = ann.aliases();
if (aliases == null || aliases.length == 0)
continue;
boolean aliasMatch = false;
for (String alias : aliases) {
if (iCommand.startsWith(alias.split(" ")[0])) {
aliasMatch = true;
commandWordCount = 1;
break;
}
}
if (!aliasMatch)
continue;
}
Object[] methodArgs;
// BUILD PARAMETERS
if (ann != null && !ann.splitInWords()) {
methodArgs = new String[] { iCommand.substring(iCommand.indexOf(' ') + 1) };
} else {
final int actualParamCount = commandWords.length - commandWordCount;
if (m.getParameterTypes().length > actualParamCount) {
// METHOD PARAMS AND USED PARAMS MISMATCH: CHECK FOR OPTIONALS
for (int paramNum = m.getParameterAnnotations().length - 1; paramNum > actualParamCount - 1; paramNum--) {
final Annotation[] paramAnn = m.getParameterAnnotations()[paramNum];
if (paramAnn != null)
for (int annNum = paramAnn.length - 1; annNum > -1; annNum--) {
if (paramAnn[annNum] instanceof ConsoleParameter) {
final ConsoleParameter annotation = (ConsoleParameter) paramAnn[annNum];
if (annotation.optional())
commandWords = OArrays.copyOf(commandWords, commandWords.length + 1);
break;
}
}
}
}
methodArgs = OArrays.copyOfRange(commandWords, commandWordCount, commandWords.length);
}
try {
m.invoke(entry.getValue(), methodArgs);
} catch (IllegalArgumentException e) {
lastMethodInvoked = m;
// GET THE COMMAND NAME
lastCommandInvoked.setLength(0);
for (int i = 0; i < commandWordCount; ++i) {
if (lastCommandInvoked.length() > 0)
lastCommandInvoked.append(" ");
lastCommandInvoked.append(commandWords[i]);
}
continue;
} catch (Exception e) {
if (e.getCause() != null)
onException(e.getCause());
else
e.printStackTrace(err);
return RESULT.ERROR;
}
return RESULT.OK;
}
if (lastMethodInvoked != null)
syntaxError(lastCommandInvoked.toString(), lastMethodInvoked);
error("\n!Unrecognized command: '%s'", iCommand);
return RESULT.ERROR;
}
use of com.orientechnologies.common.console.annotation.ConsoleCommand in project orientdb by orientechnologies.
the class OConsoleApplication method getConsoleMethods.
/**
* Returns a map of all console method and the object they can be called on.
*
* @return Map<Method,Object>
*/
protected Map<Method, Object> getConsoleMethods() {
if (methods != null)
return methods;
// search for declared command collections
final Iterator<OConsoleCommandCollection> ite = ServiceLoader.load(OConsoleCommandCollection.class).iterator();
final Collection<Object> candidates = new ArrayList<Object>();
candidates.add(this);
while (ite.hasNext()) {
try {
// make a copy and set it's context
final OConsoleCommandCollection cc = ite.next().getClass().newInstance();
cc.setContext(this);
candidates.add(cc);
} catch (InstantiationException ex) {
Logger.getLogger(OConsoleApplication.class.getName()).log(Level.WARNING, ex.getMessage());
} catch (IllegalAccessException ex) {
Logger.getLogger(OConsoleApplication.class.getName()).log(Level.WARNING, ex.getMessage());
}
}
methods = new TreeMap<Method, Object>(new Comparator<Method>() {
public int compare(Method o1, Method o2) {
final ConsoleCommand ann1 = o1.getAnnotation(ConsoleCommand.class);
final ConsoleCommand ann2 = o2.getAnnotation(ConsoleCommand.class);
if (ann1 != null && ann2 != null) {
if (ann1.priority() != ann2.priority())
// PRIORITY WINS
return ann1.priority() - ann2.priority();
}
int res = o1.getName().compareTo(o2.getName());
if (res == 0)
res = o1.toString().compareTo(o2.toString());
return res;
}
});
for (final Object candidate : candidates) {
final Method[] classMethods = candidate.getClass().getMethods();
for (Method m : classMethods) {
if (Modifier.isAbstract(m.getModifiers()) || Modifier.isStatic(m.getModifiers()) || !Modifier.isPublic(m.getModifiers())) {
continue;
}
if (m.getReturnType() != Void.TYPE) {
continue;
}
methods.put(m, candidate);
}
}
return methods;
}
Aggregations