Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.mks.api.CmdRunner;
- import com.mks.api.Command;
- import com.mks.api.IntegrationPoint;
- import com.mks.api.IntegrationPointFactory;
- import com.mks.api.response.APIException;
- import com.mks.api.response.Response;
- import com.mks.api.Session;
- import com.mks.api.util.APIVersion;
- import com.mks.api.util.MKSLogger;
- import java.util.Properties;
- import java.io.IOException;
- import java.io.File;
- import javax.swing.JOptionPane;
- /**
- * This class represents the Server Integration Point to a server.
- */
- public class APISession
- {
- // User's current working directory
- private static final String tmpDir = System.getProperty("java.io.tmpdir");
- // System file separator
- private static final String fs = System.getProperty("file.separator");
- // Log file location
- public static final String LOGFILE = tmpDir + fs + "importCSV.log";
- // Class variables used to create an API Session
- private String hostName;
- private int port;
- private boolean isSecure;
- private String userName;
- private String password;
- // API Specific Objects
- private IntegrationPoint ip;
- private Session apiSession;
- // Log all MKS API Commands
- private MKSLogger logger;
- private Properties loggerProps;
- /**
- * Constructor for the API Session Object
- */
- public APISession()
- {
- // Delete the previous log file
- File logFile = new File(LOGFILE);
- if( logFile.canRead() ){ logFile.delete(); }
- // Configure the logger
- logger = new MKSLogger(LOGFILE);
- logger.configure(getLoggerProperties());
- logger.message("Logger initialized...");
- logger.message("MKS API Version: " + APIVersion.API_4_16.getMinor()+""+APIVersion.API_4_16.getMajor());
- // Create the login screen
- }
- private Properties getLoggerProperties()
- {
- // Initialize logger properties
- loggerProps = new Properties();
- // Logging Categories
- loggerProps.put("mksis.logger.message.includeCategory.DEBUG", "10");
- loggerProps.put("mksis.logger.message.includeCategory.WARNING", "10");
- loggerProps.put("mksis.logger.message.includeCategory.GENERAL", "10");
- loggerProps.put("mksis.logger.message.includeCategory.ERROR", "10");
- // Output Format
- loggerProps.put("mksis.logger.message.defaultFormat", "{2}({3}): {4}");
- loggerProps.put("mksis.logger.message.format.DEBUG", "{2}({3}): {4}");
- loggerProps.put("mksis.logger.message.format.WARNING", "* * * * {2} * * * * ({3}): {4}");
- loggerProps.put("mksis.logger.message.format.ERROR", "* * * * {2} * * * * ({3}): {4}");
- return loggerProps;
- }
- public MKSLogger getLogger()
- {
- return logger;
- }
- public void connect()
- {
- // Keeping trying to connect until successful
- // or user terminates program...
- try
- {
- connectToSuccess();
- }
- catch(APIException aex)
- {
- //ExceptionHandler eh = new ExceptionHandler(aex);
- logger.message("Error Connected..."+aex.getMessage() );
- connect();
- }
- }
- public void setUserParametres(String hostName, int port, String userName, String password ){
- this.hostName = hostName;
- this.port = port;
- this.userName = userName;
- this.password = password;
- }
- private void connectToSuccess() throws APIException
- {
- // Show the modal login screen.
- // Get the user entered data
- hostName = "win7dev";
- port = 7001;
- //isSecure = loginFrame.getSecureConnection();
- userName ="administrator";
- password = "password";
- // Create a Server Integration Point based on the API Version
- logger.message(MKSLogger.DEBUG, "Creating server integration point...");
- ip = IntegrationPointFactory.getInstance().createIntegrationPoint(hostName, port, isSecure, APIVersion.API_4_16.getMajor(),APIVersion.API_4_16.getMinor());
- logger.message(MKSLogger.DEBUG, "Server integration point created!");
- // Create an authenticated session
- logger.message(MKSLogger.DEBUG, "Creating api session...");
- apiSession = ip.createSession(userName, password);
- logger.message(MKSLogger.DEBUG, "API Session created!");
- // Open a connection to the MKS Integrity Server
- logger.message(MKSLogger.DEBUG, "Connecting to MKS Integrity Server...");
- Command imConnect = new Command(Command.IM, "connect");
- CmdRunner cmdRunner = apiSession.createCmdRunner();
- cmdRunner.setDefaultHostname(hostName);
- cmdRunner.setDefaultPort(port);
- cmdRunner.setDefaultUsername(userName);
- cmdRunner.setDefaultPassword(password);
- Response res = runCommand(imConnect);
- logger.message(MKSLogger.DEBUG, res.getCommandString());
- logger.message(MKSLogger.DEBUG, "im connect exit code was " + res.getExitCode());
- logger.message(MKSLogger.DEBUG, "Releasing command runner...");
- //cmdRunner.release();
- logger.message(MKSLogger.DEBUG, "Command runner released!");
- logger.message(MKSLogger.DEBUG, "Connection successfully established!");
- }
- /**
- * This function executes a generic API/CLI Command
- * @param cmd MKS API Command Object representing a CLI command
- * @return MKS API Response Object
- * @throws APIException
- */
- public Response runCommand(Command cmd) throws APIException
- {
- logger.message(MKSLogger.DEBUG, MKSLogger.LOW, "Attempting to run " + cmd.getApp() + ' ' + cmd.getCommandName());
- CmdRunner cmdRunner = apiSession.createCmdRunner();
- cmdRunner.setDefaultHostname(hostName);
- cmdRunner.setDefaultPort(port);
- cmdRunner.setDefaultUsername(userName);
- cmdRunner.setDefaultPassword(password);
- Response res = cmdRunner.execute(cmd);
- cmdRunner.release();
- logger.message(MKSLogger.DEBUG, MKSLogger.LOW, "Command " + res.getCommandString() + " completed with exit code " + res.getExitCode());
- return res;
- }
- /**
- * This function executes a generic API/CLI Command impersonating another user
- * @param cmd MKS API Command Object representing a CLI command
- * @param impersonateUser The user to impersonate
- * @return MKS API Response Object
- * @throws APIException
- */
- public Response runCommandAs(Command cmd, String impersonateUser) throws APIException
- {
- logger.message(MKSLogger.DEBUG, MKSLogger.LOW, "Attempting to run " + cmd.getApp() + ' ' + cmd.getCommandName() + " as " + impersonateUser);
- CmdRunner cmdRunner = apiSession.createCmdRunner();
- cmdRunner.setDefaultHostname(hostName);
- cmdRunner.setDefaultPort(port);
- cmdRunner.setDefaultUsername(userName);
- cmdRunner.setDefaultPassword(password);
- cmdRunner.setDefaultImpersonationUser(impersonateUser);
- Response res = cmdRunner.execute(cmd);
- cmdRunner.release();
- logger.message(MKSLogger.DEBUG, MKSLogger.LOW, "Command " + res.getCommandString() + " completed with exit code " + res.getExitCode());
- return res;
- }
- /**
- * Terminate the API Session and Integration Point
- * @throws APIException
- * @throws IOException
- */
- public void Terminate() throws APIException, IOException
- {
- logger.message(MKSLogger.DEBUG, MKSLogger.LOW, "Attempting to terminate API Session");
- if( null != apiSession )
- {
- apiSession.release();
- }
- if( null != ip )
- {
- ip.release();
- }
- logger.message(MKSLogger.DEBUG, MKSLogger.LOW, "API Session terminated!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement