Java tutorial
/******************************************************************************* * Copyright (c) 2015 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package com.ibm.dbwkl.request.internal; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import com.ibm.dbwkl.STAFHandler; import com.ibm.dbwkl.logging.LogLevel; import com.ibm.dbwkl.logging.Logger; import com.ibm.dbwkl.request.parser.Options; import com.ibm.staf.STAFResult; import com.ibm.staf.STAFUtil; /** * Handles the automatic setup process for the DB2 workload service into the STAF.cfg file * * */ public class SetupHandler extends InternalRequest { /* (non-Javadoc) * @see com.ibm.dbwkl.request.internal.InternalRequest#execute() */ @Override protected STAFResult execute() { // check if setup was done before to prevent having the entries twice boolean setupDone = IsSetupDone() && !IsOverrideOptionGiven(); if (setupDone) { return new STAFResult(STAFResult.AlreadyExists, "Setup already done. In order to rerun the setup process and replace the existing entries use the option OVERRIDE."); } // get the config file File configFile = null; try { configFile = searchForConfigFile(); } catch (FileNotFoundException e) { Logger.log("STAF config file not found", LogLevel.Error); return new STAFResult(STAFResult.FileOpenError); } // in case the override option is given, remove the existing entries STAFResult removeResult = RemoveAutoSetup(configFile); if (removeResult.rc != STAFResult.Ok) { return removeResult; } // write the auto setup to the config file STAFResult writeResult = WriteAutoSetup(configFile); if (writeResult.rc != STAFResult.Ok) { return writeResult; } return new STAFResult(STAFResult.Ok); } /** * @param configFile * @return returns the result after removing the setup lines */ private STAFResult RemoveAutoSetup(File configFile) { StringBuilder fileContent = new StringBuilder(); try { // make a backup copy File backupConfigFile = new File(configFile.getAbsolutePath() + ".backup"); FileUtils.copyFile(configFile, backupConfigFile); // read the file FileReader reader = new FileReader(configFile); BufferedReader br = new BufferedReader(reader); String line = null; boolean inDB2WKLSection = false; while ((line = br.readLine()) != null) { // on the way, remove all db2wkl lines if (line.trim().startsWith("# [DB2WKL SETUP]") && inDB2WKLSection == false) inDB2WKLSection = true; if (!inDB2WKLSection) { fileContent.append(line + "\n"); if (line.trim().startsWith("# [DB2WKL SETUP]") && inDB2WKLSection == true) inDB2WKLSection = false; } } br.close(); reader.close(); // write back the whole file FileWriter writer = new FileWriter(configFile); BufferedWriter bw = new BufferedWriter(writer); bw.write(fileContent.toString()); bw.close(); writer.close(); return new STAFResult(STAFResult.Ok); } catch (IOException e) { Logger.log( "Could not remove existing entries in the config file. If the config file is empty/wrong now replace it with the backup STAF.cfg.backup: " + e.getMessage(), LogLevel.Error); return new STAFResult(STAFResult.FileWriteError); } } /** * @param configFile * @return returns the result after writing the new setup file */ private STAFResult WriteAutoSetup(File configFile) { // Open the file and add the options try { FileWriter writer = new FileWriter(configFile, true); BufferedWriter bw = new BufferedWriter(writer); String jcclibs = this.parseResult.get(Options.SETUP_JCCLIBS); bw.newLine(); bw.write("# [DB2WKL SETUP] (Don't remove the comments!)\n"); bw.write("# DB2 Workload Service\n"); bw.write("# (this section was generated by the setup command)" + "\n"); bw.write("SET SYSTEM VAR IBM/DB2WKL/AutoSetup=true" + "\n"); if (this.parseResult.containsKey(Options.SETUP_JCCLIBS)) { bw.write("SET SYSTEM VAR IBM/OMPE/DB2WKL/JCCLIB=\"" + FilenameUtils.separatorsToUnix(jcclibs) + "\"" + "\n"); STAFHandler.instance.getSTAFHandle().submit2("local", "VAR", "SET SYSTEM VAR IBM/OMPE/DB2WKL/JCCLIB=\"" + FilenameUtils.separatorsToUnix(jcclibs) + "\""); } else { bw.write( "SET SYSTEM VAR IBM/OMPE/DB2WKL/JCCLIB=\"{STAF/config/STAFroot}/services/db2wkl/libs/jcc/\"" + "\n"); STAFHandler.instance.getSTAFHandle().submit2("local", "VAR", "SET SYSTEM VAR IBM/OMPE/DB2WKL/JCCLIB=\"{STAF/config/STAFroot}/services/db2wkl/libs/jcc/\""); } bw.write( "SERVICE db2wkl LIBRARY JSTAF EXECUTE {STAF/config/STAFroot}/services/db2wkl/db2wkl.jar OPTION JVMName=DB2WKLJVM" + "\n"); bw.write("# [DB2WKL SETUP]\n"); bw.newLine(); bw.close(); writer.close(); // when writing the file was successful also register the variables to be available immedately STAFHandler.instance.getSTAFHandle().submit2("local", "VAR", "SET SYSTEM VAR IBM/DB2WKL/AutoSetup=true"); } catch (IOException e) { Logger.log("Could not write into config file: " + e.getMessage(), LogLevel.Error); return new STAFResult(STAFResult.FileWriteError); } return new STAFResult(STAFResult.Ok); } /** * Returns whether the override option is given and therefore override the current config values * * @return true in case the user wants to override the current entries in the config file */ private boolean IsOverrideOptionGiven() { return this.parseResult.containsKey(Options.SETUP_OVERRIDE); } /** * Checks whether the setup was done before * * @return true in case the automatic setup was done before */ private boolean IsSetupDone() { STAFResult varRequest = STAFUtil.resolveInitVar("{IBM/DB2WKL/AutoSetup}", STAFHandler.instance.getSTAFHandle()); if (varRequest.rc == STAFResult.VariableDoesNotExist) { return false; } return true; } /** * Searches for the STAF.cfg file and returns a handler on it * * @return STAF.cfg file * @throws FileNotFoundException STAF.cfg file was not found */ private File searchForConfigFile() throws FileNotFoundException { STAFResult pathRequest = STAFUtil.resolveInitVar("{STAF/config/ConfigFile}", STAFHandler.instance.getSTAFHandle()); if (pathRequest.rc == STAFResult.Ok) { String path = pathRequest.result; File stafCFGFile = new File(path); if (!stafCFGFile.exists()) throw new FileNotFoundException(); return stafCFGFile; } else { throw new FileNotFoundException(); } } }