Created
August 2, 2012 08:47
-
-
Save anonymous/3235533 to your computer and use it in GitHub Desktop.
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
package commands; | |
import com.google.common.collect.ImmutableMap; | |
import com.google.common.collect.ImmutableSet; | |
import com.sun.enterprise.admin.cli.CLICommand; | |
import com.sun.enterprise.admin.cli.LocalServerCommand; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.PrintStream; | |
import java.lang.reflect.Field; | |
import java.util.Collection; | |
import java.util.Comparator; | |
import java.util.Enumeration; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Set; | |
import java.util.TreeSet; | |
import java.util.jar.JarEntry; | |
import java.util.jar.JarFile; | |
import java.util.zip.ZipException; | |
import org.glassfish.api.Param; | |
import org.glassfish.api.admin.AdminCommand; | |
import org.glassfish.config.support.CommandTarget; | |
import org.glassfish.config.support.TargetType; | |
import org.jvnet.hk2.annotations.Service; | |
public class Extractor { | |
public static class Command { | |
public final Service service; | |
public final List<ParamField> fields; | |
public final TargetType targetType; | |
public final boolean local; | |
public Command(Service service, List<ParamField> fields, TargetType targetType, boolean local) { | |
this.service = service; | |
this.fields = fields; | |
this.targetType = targetType; | |
this.local = local; | |
} | |
} | |
public static class ParamField { | |
public final Param param; | |
public final Field field; | |
public ParamField(Param param, Field field) { | |
this.param = param; | |
this.field = field; | |
} | |
} | |
public static void getParams(List<ParamField> params, Class<?> cls) { | |
for(Field f: cls.getDeclaredFields()) { | |
Param p = f.getAnnotation(Param.class); | |
if (p != null) { | |
params.add(new ParamField(p, f)); | |
} | |
} | |
Class<?> sup = cls.getSuperclass(); | |
if (sup != null) { | |
getParams(params, sup); | |
} | |
} | |
private static Map<String, String> primaries = new ImmutableMap.Builder<String, String>() | |
.put("delete-node-config", "'*:nodes:_asadmin_nodes_config'") | |
.put("delete-node-dcom", "'*:nodes:_asadmin_nodes_dcom'") | |
.put("delete-node-ssh", "'*:nodes:_asadmin_nodes_ssh'") | |
.put("ping-node-dcom", "'*:nodes:_asadmin_nodes_dcom'") | |
.put("ping-node-ssh", "'*:nodes:_asadmin_nodes_ssh'") | |
.put("update-node-config", "'*:nodes:_asadmin_nodes_config'") | |
.put("update-node-dcom", "'*:nodes:_asadmin_nodes_dcom'") | |
.put("update-node-ssh", "'*:nodes:_asadmin_nodes_ssh'") | |
.put("create-service", "'*:instances:_asadmin_instances'") // or domain | |
.put("delete-instance", "'*:instances:_asadmin_instances'") | |
.put("delete-local-instance", "'*:instances:_asadmin_instances'") | |
.put("restart-instance", "'*:instances:_asadmin_instances'") | |
.put("restart-local-instance", "'*:instances:_asadmin_instances'") | |
.put("start-instance", "'*:instances:_asadmin_instances'") | |
.put("start-local-instance", "'*:instances:_asadmin_instances'") | |
.put("stop-instance", "'*:instances:_asadmin_instances'") | |
.put("stop-local-instance", "'*:instances:_asadmin_instances'") | |
.put("delete-cluster", "'*:clusters:_asadmin_clusters'") | |
.put("start-cluster", "'*:clusters:_asadmin_clusters'") | |
.put("stop-cluster", "'*:clusters:_asadmin_clusters'") | |
.build(); | |
private static Set<String> primariesWithTargets = new ImmutableSet.Builder<String>() | |
.add("list-admin-objects") | |
.add("list-application-refs") | |
.add("list-applications") | |
.add("list-audit-modules") | |
.add("list-auth-realms") | |
.add("list-clusters") | |
.add("list-components") | |
.add("list-configs") | |
.add("list-connector-resources") | |
.add("list-custom-resources") | |
.add("list-file-users") | |
.add("list-http-lb-configs") | |
.add("list-http-listeners") | |
.add("list-iiop-listeners") | |
.add("list-jacc-providers") | |
.add("list-javamail-resources") | |
.add("list-jms-resources") | |
.add("list-jmsdest") | |
.add("list-jndi-entries") | |
.add("list-jndi-resources") | |
.add("list-lifecycle-modules") | |
.add("list-log-attributes") | |
.add("list-message-security-providers") | |
.add("list-network-listeners") | |
.add("list-protocols") | |
.add("list-resource-refs") | |
.add("list-system-properties") | |
.add("list-timers") | |
.add("list-transports") | |
.add("list-virtual-servers") | |
.build(); | |
public static String targetFun(TargetType targetType) { | |
TreeSet<String> funs = new TreeSet<String>(); | |
for(CommandTarget target: targetType.value()) { | |
funs.add(target.name().toLowerCase()); | |
} | |
StringBuilder f = new StringBuilder("_asadmin_targets"); | |
for(String fun: funs) { | |
f.append("_").append(fun); | |
} | |
return f.toString(); | |
} | |
public static void handle(Command cmd) { | |
if ("help".equals(cmd.service.name())) { | |
System.out.println(" " + cmd.service.name() + ")\n _describe -t help-commands \"asadmin help command\" _1st_arguments\n ;;"); | |
return; | |
} | |
TreeSet<String> params = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); | |
if (!cmd.local || "login".equals(cmd.service.name())) { | |
params.add("'--host+:'"); | |
params.add("'--port+:'"); | |
} | |
boolean primary = false; | |
String parg = primaries.get(cmd.service.name()); | |
if (parg != null) { | |
params.add(parg); | |
primary = true; | |
} else if (primariesWithTargets.contains(cmd.service.name()) && cmd.targetType != null) { | |
params.add("'*:targets:" + targetFun(cmd.targetType) + "'"); | |
primary = true; | |
} | |
for(ParamField pf: cmd.fields) { | |
if (!pf.field.getName().startsWith("_") && (!primary || !pf.param.primary())) { | |
String name = (pf.param.name() != null && pf.param.name().length() > 0) ? pf.param.name() : pf.field.getName(); | |
if (name.length() > 0 && !name.startsWith("_")) { | |
name = pf.param.primary() ? "*" : name.toLowerCase(); | |
StringBuilder v = new StringBuilder(); | |
if (pf.param.acceptableValues() != null && pf.param.acceptableValues().length() > 0) { | |
v.append(name).append(":(").append(pf.param.acceptableValues().replaceAll(", *", " ")).append(")"); | |
} else if (pf.field.getType() == Boolean.class || pf.field.getType() == Boolean.TYPE) { | |
v.append(name).append(":(true false)"); | |
} else if (pf.field.getType() == File.class || | |
pf.field.getType().isArray() && pf.field.getType().getComponentType() == File.class) { | |
v.append(name).append(":directory:_files"); | |
} else if ("node".equals(name)) { | |
v.append(name).append(":_asadmin_nodes"); | |
} else if ("cluster".equals(name)) { | |
v.append(name).append(":_asadmin_clusters"); | |
} else if ("target".equals(name) && cmd.targetType != null) { | |
v.append(name).append(":").append(targetFun(cmd.targetType)); | |
} | |
StringBuilder p = new StringBuilder(); | |
if (pf.param.primary()) { | |
if (v.length() > 0) { | |
p.append("'").append(v.toString()).append("'"); | |
} | |
} else { | |
p.append("'--").append(name).append("+:").append(v.toString()).append("'"); | |
} | |
if (p.length() > 0) | |
params.add(p.toString()); | |
} | |
} | |
} | |
StringBuilder b = new StringBuilder(); | |
for(String param: params) { | |
b.append(param).append(" "); | |
} | |
if (b.length() > 1) { | |
System.out.print(" " + cmd.service.name() + ")\n _command_args=("); | |
System.out.print(b.substring(0, b.length() -1)); | |
System.out.println(")\n ;;"); | |
} | |
} | |
public static Collection<Command> getCommands() throws Exception { | |
TreeSet<Command> ret = new TreeSet<Command>(new Comparator<Command>() { | |
@Override | |
public int compare(Command arg0, Command arg1) { | |
return String.CASE_INSENSITIVE_ORDER.compare(arg0.service.name(), arg1.service.name()); | |
} | |
}); | |
for(String el: System.getProperty("java.class.path").split(":")) { | |
if (!el.contains("guava")) { | |
try { | |
JarFile jar = new JarFile(el); | |
for(Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements(); ) { | |
JarEntry entry = entries.nextElement(); | |
String name = entry.getName(); | |
if (name.endsWith(".class")) { | |
try { | |
Class<?> cls = Class.forName(name.substring(0, name.length() - 6).replace("/", ".")); | |
boolean local = false; | |
if (LocalServerCommand.class.isAssignableFrom(cls) || CLICommand.class.isAssignableFrom(cls)) | |
local = true; | |
if (AdminCommand.class.isAssignableFrom(cls) || local) { | |
Service s = cls.getAnnotation(Service.class); | |
if (s != null && s.name().length() > 0 && !s.name().startsWith("_")) { | |
List<ParamField> fields = new LinkedList<ParamField>(); | |
getParams(fields, cls); | |
ret.add(new Command(s, fields, cls.getAnnotation(TargetType.class), local)); | |
} | |
} | |
} catch (NoClassDefFoundError err) { | |
} catch (ExceptionInInitializerError eiie) { | |
} catch (UnsatisfiedLinkError ule) { | |
} catch (ClassNotFoundException cnfe) { | |
} catch (NoSuchMethodError nsme) { | |
} catch (IllegalAccessError iae) { | |
} catch (SecurityException se) { | |
} | |
} | |
} | |
} catch (ZipException ze) { | |
} catch (IOException ioe) { | |
} | |
} | |
} | |
return ret; | |
} | |
public static void main(String argv[]) throws Exception { | |
PrintStream out = System.out; | |
PrintStream err = new PrintStream(new ByteArrayOutputStream()); | |
System.setErr(err); | |
System.setOut(err); | |
Collection<Command> cmds = getCommands(); | |
System.setOut(out); | |
for(Command cmd: cmds) { | |
handle(cmd); | |
} | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment