From: Will S. <wsa...@us...> - 2005-05-07 23:34:30
|
Update of /cvsroot/mockobjects/mockobjects-java/src/extensions/com/mockobjects/atg/command In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29548/src/extensions/com/mockobjects/atg/command Added Files: NucleusTestCase.java CommandRunner.java Log Message: Add revised ATG mockobjects information. --- NEW FILE: NucleusTestCase.java --- /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2001-2004 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The name "MockObjects" must not be * used to endorse or promote products derived from this software * without prior written permission. For written permission, please * contact ap...@ap.... * * 5. Products derived from this software may not be called * "MockObjects", nor may "MockObjects" appear in their name, * without prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ package com.mockobjects.atg.command; import java.io.PrintWriter; import atg.adapter.gsa.xml.TemplateParser; import atg.nucleus.GenericService; import atg.nucleus.Nucleus; import atg.nucleus.logging.LogListener; import atg.repository.Repository; /** * This is an example test case that uses the command runner to start up a very minimal * Nucleus. * <p> * Note that you must set the system property atg.dynamo.root to point to your ATG Dynamo * setting, and have a Java VM on your path. * </p> * * @author wsargent * @version $Revision: 1.1 $ * @since Feb 22, 2004 */ public class NucleusTestCase extends GenericService { /** Print writer. */ protected static PrintWriter out = new PrintWriter(System.out); /** * */ public NucleusTestCase() throws Exception { super(); } public static void main(String[] argv) throws Exception { NucleusTestCase runner = new NucleusTestCase(); String[] settings = CommandRunner.getSystemPaths("DAS;DAS-UI;DPS"); String dynamoHome = CommandRunner.getDynamoHome(); String className = runner.getClass().getName(); String classPath = settings[0]; String configPath = settings[1]; String modulesPath = settings[2]; CommandRunner.callParserInNewVM(dynamoHome, className, classPath, modulesPath, configPath); } /** * Sets up the logging services inside nucleus. */ public void doStartService() { // Set up the logging to the template parser. LogListener listener = TemplateParser.getLogListener("-- ldr: ", out); addLogListener(listener); // Set up all the default logging. setLoggingDebug(true); setLoggingInfo(true); setLoggingWarning(true); setLoggingError(true); } /** * Runs the thingy inside Nucleus. * * @param args DOCUMENT ME! */ public void run(Repository pRepository) throws Exception { doStartService(); Nucleus.getGlobalNucleus().logDebug("hello"); } } --- NEW FILE: CommandRunner.java --- /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2001-2004 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The name "MockObjects" must not be * used to endorse or promote products derived from this software * without prior written permission. For written permission, please * contact ap...@ap.... * * 5. Products derived from this software may not be called * "MockObjects", nor may "MockObjects" appear in their name, * without prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ package com.mockobjects.atg.command; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.io.PrintWriter; import java.util.List; import atg.adapter.gsa.xml.TemplateParser; import atg.applauncher.AppLauncher; import atg.applauncher.AppModule; import atg.applauncher.AppModuleManager; import atg.applauncher.LocalAppModuleManager; import atg.applauncher.MultiInstallLocalAppModuleManager; import atg.core.util.StringUtils; /** * A generic command runner. This code will start up a very limited Nucleus * process and provide a link to the repository. * * <p> * The keen observer of source might determine a certain level of complexity in * the base class. The approach is to minimize the information that needs to * be passed in to methods which can be easily subclassed. So we take a list * of modules and a classname, generate the classpath and configpath we know * we need to run the test, and then create a new Process which has the * correct classpath and configpath to supply to the TemplateParser. * </p> * * <p> * We need to go through this tortured process because ironically one of the * things that Java is bad at is managing its own classpath. We can hack * around the problem of loading classes dynamically in a single VM by using a * classloader, but Dynamo uses a global classloader to resolve classes in * Nucleus, so that approach is out. * </p> * * <p> * The template parser, since it can take an "invoke" class, will then create a * new instance of the class given by getClassName(), and then call the run() * method on it. There's a callProperties method you can use which will * resolve any extra components to execute a unit test. * </p> * * <p> * Needless to say, this doesn't do the JUnit framework much good, but it's the * least amount of set up necessary for subclasses to create something useful. * </p> * * <p> * If you think this is bad, you should see what Cactus goes through. * </p> * * @author Will Sargent * @version $Id: CommandRunner.java,v 1.1 2005/05/07 23:34:16 wsargent Exp $ */ public abstract class CommandRunner { //------------------------------------- /** Print writer. */ protected static PrintWriter mOut; public static String sClassName; /** * No arg constructor. */ public CommandRunner() { } //------------------------------------- /** * Gets the appropriate classpath, configpath and modules path, as the * first, second and third elements of the array, respectively. * * @param pModules DOCUMENT ME! * * @return DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ public static String[] getSystemPaths(String pModules) throws Exception { // Set VM Arguments String dynamoHome = getDynamoHome(); setDynamoProperties(dynamoHome); String modulePath = pModules; String[] modules = { "-environment", modulePath }; String[] settings = getDynamoSettings(modules); // Wierd, it doesn't add localconfig... settings[1] += (File.pathSeparatorChar + dynamoHome + "/localconfig"); return settings; } /** * Gets the dynamo home. * * @return * * @throws IllegalStateException DOCUMENT ME! */ public static String getDynamoHome() { String dynamoRoot = System.getProperty("atg.dynamo.root"); if (StringUtils.isEmpty(dynamoRoot)) { String msg = "atg.dynamo.root must be defined as a system property."; throw new IllegalStateException(msg); } String dynamoHome = dynamoRoot + File.separator + "home"; return dynamoHome; } //------------------------------------- /** * This sets up the CommandRunner and calls it with some arguments, then * runs it through the TemplateParser so we can feed data into the GSA. * * <p> * This method is not currently used, but * </p> * * @param pClassName DOCUMENT ME! * @param pClassPath DOCUMENT ME! * @param pConfigPath DOCUMENT ME! */ public static void callParserInSameVM(String pClassName, String pClassPath, String pConfigPath) { mOut = new PrintWriter(System.out); String command = pClassName + ".run"; // use the template parser to set up // a nucleus in which we can run String[] args = new String[4]; args[0] = ("-invoke"); args[1] = command; args[2] = pClassPath; args[3] = pConfigPath; int ret = TemplateParser.runParser(args, mOut); if (ret != 0) { fail("TemplateParser failed with status " + ret); } } //------------------------------------- public static void log(String msg) { System.out.println(msg); } //------------------------------------- public static void fail(Exception pException) { System.out.println("Exception: "); pException.printStackTrace(); System.exit(-1); } //------------------------------------- public static void fail(String pMessage) { System.out.println("Error: " + pMessage); System.exit(-1); } //-------------------------------- /** * Sets the system properties * * @param pDynamoHome DOCUMENT ME! */ public static void setDynamoProperties(String pDynamoHome) { String dynamoHome = pDynamoHome; // Set VM Arguments System.setProperty("atg.dynamo.server.home", dynamoHome); System.setProperty("java.security.policy", dynamoHome + "/lib/java.policy"); System.setProperty("atg.dynamo.home", dynamoHome); System.setProperty("atg.dynamo.display", ""); System.setProperty("java.protocol.handler.pkgs", "atg.net.www.protocol"); System.setProperty("java.naming.factory.url.pkgs", "atg.jndi.url"); System.setProperty("java.naming.factory.initial", "atg.j2ee.jndi.SerialInitContextFactory"); /* System.setProperty("atg.j2ee.container.nonValidatingParserClass", "com.sun.xml.parser.Parser"); System.setProperty("atg.j2ee.container.xmlToolsFactoryClass", "atg.xml.tools.sun.SunXMLToolsFactory"); */ } //------------------------------------- /** * Invokes the parser in a new VM which has the correct classpath and * configpath. * * @param pDynamoHome DOCUMENT ME! * @param pCommand DOCUMENT ME! * @param pClassPath DOCUMENT ME! * @param pModules DOCUMENT ME! * @param pConfigPath DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ public static void callParserInNewVM(String pDynamoHome, String pCommand, String pClassPath, String pModules, String pConfigPath) throws Exception { final String dynamoHome = pDynamoHome; String dynamoModule = pModules; PrintStream out = System.out; out.println("DYNAMO_HOME: " + dynamoHome); out.println("-------------------"); out.println("MODULES: " + dynamoModule); out.println("-------------------"); // For some reason, it doesn't include localconfig so we add it here... String dynamoConfig = pConfigPath + File.pathSeparatorChar + dynamoHome + "/localconfig"; out.println("CONFIGPATH: " + dynamoConfig); out.println("-------------------"); // .\lib\launcher.jar;.\locallib\; out.println("CLASSPATH: " + pClassPath); out.println("-------------------"); BufferedWriter configWriter = null; try { File configFile = new File("dynamo.config"); FileWriter fwriter = new FileWriter(configFile); configWriter = new BufferedWriter(fwriter); configWriter.write(dynamoConfig); } catch (IOException ie) { ie.printStackTrace(); } finally { if (configWriter != null) { configWriter.close(); } } String dynamoCommand = getDynamoCommand(dynamoHome, pCommand + ".run", pClassPath, dynamoModule, dynamoConfig); invokeProcess(dynamoHome, dynamoCommand); } /** * Invokes the TemplateParser in a VM which has the classes to properly run * it. * * @param dynamoHome * @param command */ public static void invokeProcess(String dynamoHome, String command) { try { Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec(command, null, new File(dynamoHome)); InputStream err = process.getErrorStream(); InputStream in = process.getInputStream(); Thread errorThread = new Thread(new StreamReader(err, System.err)); Thread inputThread = new Thread(new StreamReader(in, System.out)); errorThread.start(); inputThread.start(); } catch (IOException ie) { ie.printStackTrace(); } } //------------------------------------- /** * */ public static String getDynamoCommand(String dynamoHome, String command, String classPath, String dynamoModule, String dynamoConfig) { StringBuffer b = new StringBuffer(); b.append("javaw"); b.append(" "); b.append("-classpath"); b.append(" "); b.append(classPath); b.append(" "); b.append("-Xnoclassgc"); b.append(" "); b.append("-Xms128m"); b.append(" "); b.append("-Xmx256m"); b.append(" "); b.append("-Xverify:none"); b.append(" "); b.append("-Djava.security.policy=lib/java.policy"); b.append(" "); b.append("-Datg.dynamo.server.home=\"" + dynamoHome + "\""); b.append(" "); b.append("-Djava.security.policy=" + dynamoHome + "/lib/java.policy"); b.append(" "); b.append("-Datg.dynamo.home=" + dynamoHome); b.append(" "); b.append("-Datg.dynamo.root=" + dynamoHome + "/../"); b.append(" "); b.append("-Djava.naming.factory.initial=atg.j2ee.jndi.SerialInitContextFactory"); b.append(" "); b.append("-Datg.j2ee.container.nonValidatingParserClass=com.sun.xml.parser.Parser"); b.append(" "); b.append("-Datg.j2ee.container.xmlToolsFactoryClass=atg.xml.tools.sun.SunXMLToolsFactory"); b.append(" "); b.append("-Datg.dynamo.display="); b.append(" "); b.append("-Djava.protocol.handler.pkgs=atg.net.www.protocol"); b.append(" "); b.append("-Djava.naming.factory.url.pkgs=atg.jndi.url"); b.append(" "); b.append("atg.adapter.gsa.xml.TemplateParser"); b.append(" "); b.append("-invoke"); b.append(" "); b.append(command); b.append(" "); b.append("-configPath"); b.append(" "); b.append(dynamoConfig); return b.toString(); } //-------------------------------- /** * Returns array of strings, where the first element is the classpath, the * second module is the configpath, and the third module is the dynamo * modules. * * @param args DOCUMENT ME! * * @return DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ public static String[] getDynamoSettings(String[] args) throws Exception { // Spit out shell commands that will define the CLASSPATH and // CONFIGPATH variables. File dynamoRoot = new File(System.getProperty("atg.dynamo.root")); String modulePath = System.getProperty("atg.dynamo.modulepath"); AppModuleManager mgr = null; if (modulePath == null) { System.out.println("Using local app -- define atg.dynamo.modulepath to use DAS modules"); mgr = new LocalAppModuleManager(dynamoRoot); } else { mgr = new MultiInstallLocalAppModuleManager(modulePath, dynamoRoot); } AppLauncher launcher = AppLauncher.getLauncher(mgr, args[1]); StringBuffer classPath = new StringBuffer(launcher.getClassPath()); String systemClassPath = System.getProperty("java.class.path"); if (systemClassPath.length() > 0) { classPath.append(File.pathSeparatorChar); classPath.append(systemClassPath); } // form the boot class path from built-in settings // followed by launcher settings... String bootClassPath = launcher.getClassPath(true); if (bootClassPath != null) { if (bootClassPath.length() == 0) { bootClassPath = null; } else { StringBuffer strbufBoot = new StringBuffer(bootClassPath); strbufBoot.append(File.pathSeparatorChar); strbufBoot.append(System.getProperty("sun.boot.class.path", "")); bootClassPath = strbufBoot.toString(); } } // form CONFIGPATH from launcher's modules' manifest // attributes followed by explicit settings. Note: config // paths are reverse of module dependency order, since // dependent modules must occur last here. /* This code doesn't compile, and is only useful for liveconfig anyway... WS. List attributes = new ArrayList(); attributes.add(AppModule.MODULE_CONFIG_PATH); if ("on".equalsIgnoreCase(System.getProperty("atg.dynamo.liveconfig"))) { attributes.add(AppModule.MODULE_LIVECONFIG_PATH); } */ List configPaths = launcher.getReversedFileListAttributeValues(AppModule.MODULE_CONFIG_PATH); StringBuffer configPath = new StringBuffer(); for (int i = 0; i < configPaths.size(); i++) { String configDir = ((String) configPaths.get(i)).trim(); if (configDir.length() > 0) { if (configPath.length() > 0) { configPath.append(File.pathSeparatorChar); } configPath.append(configDir); } } if ((args.length >= 3) && (args[2].trim().length() > 0)) { if (configPath.length() > 0) { configPath.append(File.pathSeparatorChar); } configPath.append(args[2].trim()); } /* if (bootClassPath != null) { System.out.println(AppUtils.shellVariableAppend( "JAVA_ARGS", " -Xbootclasspath:" + bootClassPath)); } // Determine whether any modules specify shell scripts, and // include invocations of those scripts. List configScripts = launcher.getReversedFileListAttributeValues(AppModule.MODULE_CONFIG_SCRIPTS); for (int i = 0; i < configScripts.size(); i++) { System.out.println(AppUtils.scriptInvocation((String) configScripts.get(i))); } */ // set the DYNAMO_MODULES to include dependent modules StringBuffer modules = new StringBuffer(); List names = launcher.getModules(); for (int i = names.size(); i > 0; i--) { if (modules.length() > 0) { modules.append(File.pathSeparator); } AppModule am = (AppModule) names.get(i - 1); modules.append(am.getName()); } // Turn the string buffers into strings. return new String[] { classPath.toString(), configPath.toString(), modules.toString() }; } /** * Picks input coming from the process and prints it out. (No, this * doesn't happen automatically.) * * @author wsargent * @version $Revision: 1.1 $ * * @since Feb 22, 2004 */ static class StreamReader implements Runnable { InputStream in; PrintStream out; /** * DOCUMENT ME! * * @param pIn * @param pOut */ public StreamReader(InputStream pIn, PrintStream pOut) { in = pIn; out = pOut; } public void run() { BufferedReader bin = new BufferedReader(new InputStreamReader(in)); String line = null; try { for (;;) { try { while ((line = bin.readLine()) != null) { out.println(line); } Thread.sleep(100); } catch (InterruptedException e) { } } } catch (IOException e) { } } } } |