/*********************************************************************** * * Copyright (C) 2006 Novell, Inc. All Rights Reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; version 2.1 * of the License. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, Novell, Inc. * * To contact Novell about this file by physical or electronic mail, * you may find current contact information at www.novell.com. * * Author: Greg Richardson * ***********************************************************************/ import java.io.*; import java.util.*; /** * Summary description for SetupAsWindowsService */ public class SetupAsWindowsService { final static int ERROR_NO_ERROR = 0; final static int ERROR_INVALID_NUMBER_OF_PARAMS = -1; final static int ERROR_EXEC_FAILED = -2; final static int ERROR_EXEC_INTERRUPTED = -3; final static int ERROR_IO_EXCEPTION = -13; final static int ERROR_UNABLE_TO_READ_PROPERTIES = -16; final static int ERROR_MISSING_INSTALL_DIR = -18; final static int ERROR_INSTALL_DIR_NOT_A_DIR = -19; final static int ERROR_BAD_INSTALL_DIR_PARAM = -20; final static int ERROR_BAD_PROPERTY_FILE_PARAM = -21; final static int ERROR_MISSING_PROPERTIES_FILE = -22; final static int ERROR_MISSING_INSTALL_DIR_PARAM = -23; final static int ERROR_MISSING_PROPERTY_FILE_PARAM = -24; final static int ERROR_BAD_PROPERTY_PARAM = -29; final static String INSTALL_DIR_PROPERTY = "ATS_INSTALL_DIR"; final static String PROPERTY_FILE_PARAM = "propertyfile="; final static String INSTALL_DIR = "installdir="; final static String PROPERTY_FILE = "propertyfile="; Properties properties; File fileProperties; FileInputStream fisProperties; File fileOutput; //File file; //FileWriter fw; String sInstallDir; String sOutput; int rc; public static void main(String[] args) { SetupAsWindowsService p = new SetupAsWindowsService(args); System.exit(p.rc); } SetupAsWindowsService(String[] args) { rc = ERROR_NO_ERROR; properties = new Properties(); fileProperties = null; fisProperties = null; fileOutput = null; try { //file = new File("c:\\test6.log"); //fw = new FileWriter(file); // Process the arguments log("Process args"); if (ERROR_NO_ERROR == (rc = processArgs(args))) { // Process the properties log("Process properties"); if (ERROR_NO_ERROR == (rc = processProperties())) { log("setupService"); rc = setupService(); } } } catch (IOException e) { rc = ERROR_IO_EXCEPTION; } finally { try { log(rc); //fw.flush(); //fw.close(); } catch (Exception e1) { } } } int processArgs(String[] argsOld) { String sProperties; File fileInstallDir = null; int iEquals; String sKey; String sValue; int iOld; int i; String args[] = new String[argsOld.length]; int iNew; log("Original arg count " + argsOld.length); for (i = 0; i < argsOld.length; i++) { log("Arg " + i + " = " + argsOld[i] + "\r\n"); } // Validate the number of parameters if (args.length < 2) { return ERROR_INVALID_NUMBER_OF_PARAMS; } iNew = -1; for (iOld = 0; iOld < argsOld.length; iOld++) { if (0 <= argsOld[iOld].indexOf("=")) { iNew++; args[iNew] = argsOld[iOld]; for (i = iOld + 1; i < argsOld.length && (-1 == argsOld[i].indexOf("=")); i++) { args[iNew] += " " + argsOld[i]; } } } log("New arg count " + args.length); for (i = 0; i < args.length; i++) { if (null == args[i]) { continue; } log("arg[" + i + "] = " +args[i]); // is this the install dir param? if (args[i].startsWith(INSTALL_DIR)) { // Make sure it is more the the param tag if (args[i].length() <= INSTALL_DIR.length()) { return ERROR_BAD_INSTALL_DIR_PARAM; } sInstallDir = args[i].substring(INSTALL_DIR.length()).trim(); fileInstallDir = new File(sInstallDir); // Make sure the install dir can be found if (!fileInstallDir.exists()) { return ERROR_MISSING_INSTALL_DIR; } // Make sure the install dir is a directory if (!fileInstallDir.isDirectory()) { return ERROR_INSTALL_DIR_NOT_A_DIR; } properties.setProperty(INSTALL_DIR_PROPERTY, sInstallDir); } // is this the properties file param? else if (args[i].startsWith(PROPERTY_FILE_PARAM)) { // Make sure it is more than the param tag if (args[i].length() <= PROPERTY_FILE_PARAM.length()) { return ERROR_BAD_PROPERTY_FILE_PARAM; } sProperties = args[i].substring(PROPERTY_FILE_PARAM.length()).trim(); fileProperties = new File(sProperties); // Make sure the properties file can be found if (!fileProperties.exists()) { return ERROR_MISSING_PROPERTIES_FILE; } // Read the properties try { fisProperties = new FileInputStream(fileProperties); properties.load(fisProperties); } catch (IOException ioe) { return ERROR_UNABLE_TO_READ_PROPERTIES; } } // Handle additional parameters else { if (-1 == (iEquals = args[i].indexOf("=")) || 0 == iEquals || args[i].length() == iEquals) { return ERROR_BAD_PROPERTY_PARAM; } sKey = args[i].substring(0, iEquals); sValue = args[i].substring(iEquals + 1); properties.setProperty(sKey, sValue); } } // Make sure we got an install dir if (null == fileInstallDir) { return ERROR_MISSING_INSTALL_DIR_PARAM; } // Note: the properties file parameter is optional return ERROR_NO_ERROR; } int processProperties() { try { Enumeration e; String sKey; String sValue; e = properties.propertyNames(); while (e.hasMoreElements()) { sKey = (String)e.nextElement(); sValue = (String)properties.get(sKey); log("Property key = " + sKey + " Value = " + sValue); } } catch (Exception ex1) { return -111; } return ERROR_NO_ERROR; } int setupService() { String sDirInstall = (String)properties.get(INSTALL_DIR_PROPERTY); String sDirCatalinaBase = sInstallDir + "ats/catalinabase"; String sDirCatalinaHome = (String)properties.get("TOMCAT_HOME"); String sExe = (String)properties.get("TOMCAT5"); String sFileJaasConf = (String)properties.get("JAAS_CONF"); String sDirConfig = sDirInstall + "ats/etc/svc"; String sDirJavaHome = (String)properties.get("ATS_JAVA_HOME"); String sLogPrefix = "casa-auth-token-svc"; String sDirLogs = sDirCatalinaBase + "/logs"; String sCommand = ""; int iReturn = ERROR_NO_ERROR; sCommand = sExe + " //IS//CasaAuthTokenService --StartClass org.apache.catalina.startup.Bootstrap --StopClass org.apache.catalina.startup.Bootstrap --StartParams start --StopParams stop"; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService --Startup auto"; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService --JvmOptions \"-Dcatalina.base=" + sDirCatalinaBase + "\""; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService ++JvmOptions \"-Dcatalina.home=" + sDirCatalinaHome + "\""; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService ++JvmOptions \"-Djava.endorsed.dirs=" + sDirCatalinaHome + "/common/endorsed\""; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService ++JvmOptions \"-Djava.security.auth.login.config=" + sFileJaasConf + "\""; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService ++JvmOptions \"-Dcom.novell.casa.authtoksvc.config=" + sDirConfig + "\""; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService ++JvmOptions \"-Dlog4j.configuration=file:" + sDirInstall + "/ats/etc/log4j.properties"; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService ++JvmOptions \"-Djava.io.tmpdir=" + sDirCatalinaBase+ "/temp\""; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService --StartMode jvm --StopMode jvm"; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService --JvmMs 128 --JvmMx 512"; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService --Classpath \"" + sDirCatalinaHome + "/bin/bootstrap.jar\""; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService --Jvm \"" + sDirJavaHome + "/jre/bin/server/jvm.dll\""; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService --LogPath \"" + sDirLogs + "\""; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService --LogPrefix \"" + sLogPrefix + "_service.log\""; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService --StdOutput \"" + sDirLogs + File.separator + sLogPrefix + "_stdout.log\""; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService --StdError \"" + sDirLogs + File.separator + sLogPrefix + "_stderr.log\""; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService --DisplayName " + "CasaAuthTokenSvc"; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } sCommand = sExe + " //US//CasaAuthTokenService --Description " + "\"Casa Authentication Token Service\""; if (ERROR_NO_ERROR != (iReturn = invokeCommand(sCommand))) { return iReturn; } return ERROR_NO_ERROR; } int invokeCommand(String sCommand) { Process p; int rc; log("invoke command: " + sCommand); Runtime runtime = Runtime.getRuntime(); try { p = runtime.exec(sCommand); try { rc = p.waitFor(); log("invoke command return code: " + rc); } catch (InterruptedException ie) { log(ERROR_EXEC_INTERRUPTED, sCommand); return ERROR_EXEC_INTERRUPTED; } } catch (IOException e) { log("IOException"); return ERROR_EXEC_FAILED; } return ERROR_NO_ERROR; } void log(int err) { log(err, null); } void log(int err, String s) { String sMessage = ""; switch (err) { case ERROR_NO_ERROR: sMessage = "No error"; break; case ERROR_INVALID_NUMBER_OF_PARAMS: sMessage = "Invalid number of parameters: 4 expected"; break; case ERROR_IO_EXCEPTION: sMessage = "IOException"; break; case ERROR_EXEC_FAILED: sMessage = "Exec failed"; break; case ERROR_EXEC_INTERRUPTED: sMessage = "Exec interrupted"; break; case ERROR_UNABLE_TO_READ_PROPERTIES: sMessage = "nable to read properties"; break; case ERROR_MISSING_INSTALL_DIR: sMessage = "Missing install directory"; break; case ERROR_INSTALL_DIR_NOT_A_DIR: sMessage = "Install directory is not a directory"; break; case ERROR_BAD_INSTALL_DIR_PARAM: sMessage = "Bad install directory parameter"; break; case ERROR_BAD_PROPERTY_FILE_PARAM: sMessage = "Bad property file parameter"; break; case ERROR_MISSING_PROPERTIES_FILE: sMessage = "Missing properties file"; break; case ERROR_MISSING_INSTALL_DIR_PARAM: sMessage = "Missing install directory parameter"; break; case ERROR_MISSING_PROPERTY_FILE_PARAM: sMessage = "Missing property file parameter"; break; case ERROR_BAD_PROPERTY_PARAM: sMessage = "Bad property parameter"; break; default: sMessage = "Unknown error: " + err; break; } if (null != s) { sMessage = sMessage + s; } log(sMessage); } void log(String s) { /* try { fw.write(this.getClass().getName() + ": " + s + "\r\n"); } catch (IOException ioe) { } */ } }