diff --git a/README b/README index 20f8288..eb5ba97 100644 --- a/README +++ b/README @@ -2,3 +2,27 @@ Code here is licensed under ASLv2. http://www.apache.org/licenses/LICENSE-2.0.txt + +Usage + +To build run "ant clean build" or "mvn assembly:assembly" in generatePluginConfig + +Check with your legal department before running "mvn assembly:assembly" as this will pull in Oracle source code. + + +To run + +This program requires tools.jar, it will be included in the jar file if you compiled using maven. + + +If you built with maven run with java -jar ws-generatepluginconfig-0.1-SNAPSHOT-jar-with-dependencies.jar + +If you built with ant run with something like java -cp ./ws-generatepluginconfig.jar:/tools.jar com.logicali.wlp.tools.plugincfg.Main + + +Optional command line arguments + +--outputDir= output files to DIRECTORY with the server name as a prefix to prevent duplicates. Without this argument they will be placed in the server's default output directories. +--serverName= the name of a single server to probe. Without this argument all servers will be probed. + + diff --git a/generatePluginConfig/pom.xml b/generatePluginConfig/pom.xml index 732ba2d..191d460 100644 --- a/generatePluginConfig/pom.xml +++ b/generatePluginConfig/pom.xml @@ -6,6 +6,14 @@ jar 0.1-SNAPSHOT + + + com.sun + tools + 1.6 + + + Liberty profile plugin config generator This executable jar file will get a locally running liberty server to generate a plugin configuration file for a Web server plugin. @@ -28,6 +36,14 @@ + maven-compiler-plugin + 2.0.2 + + 1.6 + 1.6 + + + org.apache.maven.plugins maven-jar-plugin @@ -39,7 +55,39 @@ ${project.artifactId} + + maven-assembly-plugin + + + + com.logicali.wlp.tools.plugincfg.Main + + + + jar-with-dependencies + + + + + org.codehaus.mojo + exec-maven-plugin + + + + java + + + + + com.logicali.wlp.tools.plugincfg.Main + java + + --outputDir=/tmp/ + + + + diff --git a/generatePluginConfig/src/main/java/com/logicali/wlp/tools/plugincfg/Main.java b/generatePluginConfig/src/main/java/com/logicali/wlp/tools/plugincfg/Main.java index b616ee2..ac1e98d 100644 --- a/generatePluginConfig/src/main/java/com/logicali/wlp/tools/plugincfg/Main.java +++ b/generatePluginConfig/src/main/java/com/logicali/wlp/tools/plugincfg/Main.java @@ -17,6 +17,8 @@ import java.io.File; import java.io.IOException; +import java.util.LinkedList; +import java.util.List; import javax.management.InstanceNotFoundException; import javax.management.MBeanException; @@ -28,50 +30,174 @@ import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; +import com.sun.tools.attach.AttachNotSupportedException; +import com.sun.tools.attach.VirtualMachine; +import com.sun.tools.attach.VirtualMachineDescriptor; + public class Main { - public static void main(String[] argArray) { - ServerInfo args = new ServerInfo(argArray); - String address = args.getJMXAddress(); - File serverOut = args.getOutputDir(); - if (address != null) { - try { - JMXServiceURL url = new JMXServiceURL(address); - JMXConnector conn = JMXConnectorFactory.connect(url); - MBeanServerConnection mbeanServer = conn.getMBeanServerConnection(); - mbeanServer.invoke(new ObjectName( - "WebSphere:name=com.ibm.ws.jmx.mbeans.generatePluginConfig"), - "generateDefaultPluginConfig", new Object[0], new String[0]); - System.out.println("Plugin configuration file written to " - + serverOut + "/plugin-cfg.xml"); - } catch (InstanceNotFoundException e) { - reportError( - "The MBean used to generate the plugin configuration could not be located", - e); - } catch (MalformedObjectNameException e) { - reportError("An internal error occurred. The MBean name was not valid", - e); - } catch (MBeanException e) { - reportError( - "The MBean threw an exception generating the plugin configuration", - e.getTargetException()); - } catch (ReflectionException e) { - reportError("An error occurred calling the MBean", - e.getTargetException()); - } catch (IOException e) { - reportError("An I/O error happened while generating the plugin config", - e); - } - } else { - System.err.println("Unable to connect to " + args.getServerName() - + "'s MBean server"); - } - } - - /** - * @param e - */ - private static void reportError(String msg, Exception e) { - System.err.println(msg); - System.err.println(e.getMessage()); - } -} \ No newline at end of file + public static void main(String[] argArray) { + + String serverName = null; + File outDir = null; + + boolean moveFile = false; + //boolean hintFile = false; + + for (int i = 0; i < argArray.length; i++) { + if (argArray[i].startsWith("--serverName=")) { + serverName = argArray[i].substring("--serverName=".length()); + } + else if (argArray[i].startsWith("--outputDir=")) { + outDir = new File ( argArray[i].substring("--outputDir=".length())); + moveFile = true; + // hintFile = true; + } + else if (argArray[i].startsWith("--help") || argArray[i].startsWith("-h")) { + System.out.println("Generate plugin-cfg.xml from liberty servers"); + System.out.println(""); + System.out.println("Arguements"); + System.out.println(""); + System.out.println("--serverName= the name of a single server to probe. Without this argument all servers will be probed."); + System.out.println("--outputDir= output files to DIRECTORY with the server name as a prefix to prevent duplicates. Without this argument they will be placed in the server's default output directories. "); + } else { + System.out.println("Unkown argument:" + argArray[i]); + System.exit(1); + } + } + + List vmds = findLibertyServer(serverName); + + for (VirtualMachineDescriptor vmd : vmds) + { + + VirtualMachine vm = null; + try { + vm = VirtualMachine.attach(vmd); + File outputDir; + + ServerInfo args = new ServerInfo(argArray, vm, vmd); + String address = args.getJMXAddress(); + + if (outDir == null){ + outputDir = args.getServerOutputDir(); + } else { + outputDir = outDir; + } + + if (address != null) { + JMXServiceURL url = new JMXServiceURL(address); + JMXConnector conn = JMXConnectorFactory.connect(url); + MBeanServerConnection mbeanServer = conn.getMBeanServerConnection(); + mbeanServer.invoke(new ObjectName( + "WebSphere:name=com.ibm.ws.jmx.mbeans.generatePluginConfig"), + "generateDefaultPluginConfig", new Object[0], new String[0]); + + if(moveFile){ + + File plginFile; + File[] findPlginFile = args.getServerOutputDir().listFiles(); + for (File f : findPlginFile){ + if (f.getName().equals("plugin-cfg.xml")){ + plginFile = f; + plginFile.renameTo(new File(outputDir, args.getServerName()+"-plugin-cfg.xml")); + break; + } + } + System.out.println("Plugin configuration file written to " + + outputDir + "/"+args.getServerName()+"-plugin-cfg.xml"); + } else { + System.out.println("Plugin configuration file written to " + + outputDir + "/plugin-cfg.xml"); + } + + /*if (hintFile){ + File hintFile = new File(outDir + "/hintfile.txt"); + + String[] mbeansListTmp = (String[]) mbeanServer.queryNames(null, null); + ArrayList mbeansList = new ArrayList(mbeansListTmp); + for (int i = mbeansList.length() -1; i >=0; i--){ + if (! mbeansList.get(i).contains("WebSphere:service=com.ibm.websphere.application.ApplicationMBean")){ + mbeansList.remove(i); + } + } + mbeansList = mbeansList.sort(); + for (String s : mbeansList){ + s.substring(s.lastIndexOf("name=") + 1); + //TODO write the file as a CSL + } + }*/ + } + else { + System.err.println("Unable to connect to " + args.getServerName() + + "'s MBean server"); + } + } catch (InstanceNotFoundException e) { + reportError( + "The MBean used to generate the plugin configuration could not be located", + e); + } catch (MalformedObjectNameException e) { + reportError("An internal error occurred. The MBean name was not valid", + e); + } catch (MBeanException e) { + reportError( + "The MBean threw an exception generating the plugin configuration", + e.getTargetException()); + } catch (ReflectionException e) { + reportError("An error occurred calling the MBean", + e.getTargetException()); + } catch (IOException e) { + reportError("An I/O error happened while generating the plugin config", + e); + } catch (AttachNotSupportedException e) { + reportError( + "Unable to attach to the server VM. This is possibly because the server and this program are not using the same JVM.", + e); + } catch (Exception e) { + reportError( + "Some other error.", + e); + } finally { + try { + if (vm != null){ + vm.detach(); + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + } + + + +private static List findLibertyServer(String nullableSN) { + List vmdsUnfiltered = VirtualMachine.list(); + + List vmds = new LinkedList(); + + for (VirtualMachineDescriptor vmd : vmdsUnfiltered) { + String displayName = vmd.displayName(); + + if (nullableSN == null && displayName.contains("ws-server.jar ")) { + vmds.add(vmd); + } + else if (nullableSN != null && displayName.endsWith("ws-server.jar " + nullableSN)) { + vmds.add(vmd); + } + + } + + return vmds; +} + + + +/** + * @param e + */ +private static void reportError(String msg, Exception e) { + System.err.println(msg); + System.err.println(e.getMessage()); +} +} diff --git a/generatePluginConfig/src/main/java/com/logicali/wlp/tools/plugincfg/ServerInfo.java b/generatePluginConfig/src/main/java/com/logicali/wlp/tools/plugincfg/ServerInfo.java index 19834cd..1c10ed8 100644 --- a/generatePluginConfig/src/main/java/com/logicali/wlp/tools/plugincfg/ServerInfo.java +++ b/generatePluginConfig/src/main/java/com/logicali/wlp/tools/plugincfg/ServerInfo.java @@ -16,208 +16,86 @@ import com.sun.tools.attach.VirtualMachineDescriptor; class ServerInfo { - private final String serverName; - private final File outputDir; - private final String address; - - public ServerInfo(String[] args) { - String sn = "defaultServer"; - String id = null; - String a = null; - File outDir = null; - String od = null; - String usrDir = null; - - for (int i = 0; i < args.length; i++) { - if (args[i].startsWith("--installDir=")) { - id = args[i].substring("--installDir=".length()); - } else if (args[i].startsWith("--outputDir=")) { - od = args[i].substring("--outputDir=".length()); - } else if (args[i].startsWith("--userDir=")) { - usrDir = args[i].substring("--userDir=".length()); - } else if (args[i].startsWith("--")) { - System.err.println("Unknown argument: " + args[i]); - } else if (i == args.length - 1) { - sn = args[i]; - } else { - System.err - .println("Invalid position. The server name must be entered as the last argument."); - } - } - - if (od == null) { - od = getOutputDir(id, usrDir, sn); - } - - if (od != null) { - outDir = new File(od, sn); - - File serverWorkarea = new File(outDir, "/workarea/com.ibm.ws.jmx.local.address"); - if (serverWorkarea.exists()) { - BufferedReader reader = null; - try { - reader = new BufferedReader(new FileReader(serverWorkarea)); - a = reader.readLine(); - } catch (IOException e) { - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - } - } - } - } - } - - if (a == null) { - VirtualMachine vm = findLibertyServer(sn); - Properties props; - try { - props = vm.getSystemProperties(); - outDir = new File(props.getProperty("server.output.dir")); - a = getAddress(vm, props); - vm.detach(); - } catch (IOException e) { - reportError("An I/O error happened while generating the plugin config", - e); - } - } - - serverName = sn; - address = a; - outputDir = outDir; - } - - private String getAddress(VirtualMachine vm, Properties props) { - String address = null; - try { - address = (String) vm.getAgentProperties().get( - "com.sun.management.jmxremote.localConnectorAddress"); - if (address == null) { - String javaHome = props.getProperty("java.home"); - String agent = javaHome + File.separator + "lib" + File.separator - + "management-agent.jar"; - vm.loadAgent(agent); - address = (String) vm.getAgentProperties().get( - "com.sun.management.jmxremote.localConnectorAddress"); - } - } catch (IOException e) { - reportError("An I/O error happened while generating the plugin config", - e); - } catch (AgentLoadException e) { - reportError( - "An error occurred loading the JMX Management agent in the target JVM", - e); - } catch (AgentInitializationException e) { - reportError( - "An error occurred initializing the JMX Management agent in the target JVM", - e); - } - - return address; - } - - private VirtualMachine findLibertyServer(String sn) { - List vmds = VirtualMachine.list(); - - for (VirtualMachineDescriptor vmd : vmds) { - String displayName = vmd.displayName(); - if (displayName.contains("ws-launch.jar")) { - if (displayName.contains("ws-launch.jar " + sn)) { - try { - return VirtualMachine.attach(vmd); - } catch (AttachNotSupportedException e) { - reportError( - "Unable to attach to the server VM. This is possibly because the server and this program are not using the same JVM.", - e); - } catch (IOException e) { - reportError("An I/O error happened while generating the plugin config", - e); - } - } - } - } - - return null; - } - - private String getOutputDir(String id, String usrDir, String sn) { - if (usrDir != null) { - return usrDir + "/" + sn; - } - - String result = findOutputDir(System.getenv()); - if (result != null) return result; - - if (id != null) { - File etc = new File(id, "etc"); - if (etc.isDirectory() && etc.exists()) { - File serverEnv = new File(etc, "server.env"); - Map env = readIntoMap(serverEnv); - return findOutputDir(env); - } - - return id + "/usr/servers/" + sn; - } - - return null; - } - - private Map readIntoMap(File serverEnv) { - Map env = new HashMap(); - BufferedReader reader = null; - try { - reader = new BufferedReader(new FileReader(serverEnv)); - String line; - while ((line = reader.readLine()) != null) { - int index = line.indexOf('='); - if (index != -1) { - String before = line.substring(0, index); - String after = line.substring(index + 1); - env.put(before.trim(), after.trim()); - } - } - } catch (IOException e) { - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - } - } - } - return env; - } - - private String findOutputDir(Map env) { - String od = env.get("WLP_OUTPUT_DIR"); - if (od == null) { - String usrDir = env.get("WLP_USER_DIR"); - if (usrDir != null) { - od = usrDir + "/servers"; - } - } - return od; - } - - /** - * @param e - */ - private static void reportError(String msg, Exception e) { - System.err.println(msg); - System.err.println(e.getMessage()); - } - - public String getServerName() { - return serverName; - } - - public String getJMXAddress() { - return address; - } - - public File getOutputDir() { - return outputDir; - } -} \ No newline at end of file + private final String serverName; + private final String address; + private File serverOutputDir = null; + + public ServerInfo(String[] args, VirtualMachine vm, VirtualMachineDescriptor vmd) { + String a = null; + String od = null; + String servername = vmd.displayName(); + + String startName = servername.substring(servername.indexOf("ws-server.jar ") + "ws-server.jar ".length()); + + if (startName.indexOf(" ") != -1){ + serverName = startName.substring(0, startName.indexOf(" ")); + } else{ + serverName = startName.substring(0, startName.length()); + } + + + + Properties props; + try { + props = vm.getSystemProperties(); + a = getAddress(vm, props); + serverOutputDir = new File(props.getProperty("server.output.dir")); + } catch (IOException e) { + reportError("An I/O error happened while generating the plugin config", + e); + } + + + address = a; + } + + private String getAddress(VirtualMachine vm, Properties props) { + String address = null; + try { + address = (String) vm.getSystemProperties().get( + "com.sun.management.jmxremote.localConnectorAddress"); + if (address == null) { + String javaHome = props.getProperty("java.home"); + String agent = javaHome + File.separator + "lib" + File.separator + + "management-agent.jar"; + vm.loadAgent(agent); + address = (String) vm.getSystemProperties().get( + "com.sun.management.jmxremote.localConnectorAddress"); + } + } catch (IOException e) { + reportError("An I/O error happened while generating the plugin config", + e); + } catch (AgentLoadException e) { + reportError( + "An error occurred loading the JMX Management agent in the target JVM", + e); + } catch (AgentInitializationException e) { + reportError( + "An error occurred initializing the JMX Management agent in the target JVM", + e); + } + + return address; + } + + + /** + * @param e + */ + private static void reportError(String msg, Exception e) { + System.err.println(msg); + System.err.println(e.getMessage()); + } + + public String getServerName() { + return serverName; + } + + public String getJMXAddress() { + return address; + } + + public File getServerOutputDir() { + return serverOutputDir; + } +}