Advertisement
Guest User

Untitled

a guest
Nov 7th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1.  
  2.  
  3. import com.mks.api.CmdRunner;
  4. import com.mks.api.Command;
  5. import com.mks.api.IntegrationPoint;
  6. import com.mks.api.IntegrationPointFactory;
  7. import com.mks.api.response.APIException;
  8. import com.mks.api.response.Response;
  9. import com.mks.api.Session;
  10. import com.mks.api.util.APIVersion;
  11. import com.mks.api.util.MKSLogger;
  12. import java.util.Properties;
  13. import java.io.IOException;
  14. import java.io.File;
  15.  
  16. import javax.swing.JOptionPane;
  17.  
  18. /**
  19. * This class represents the Server Integration Point to a server.
  20. */
  21. public class APISession
  22. {
  23.  
  24.  
  25. // User's current working directory
  26. private static final String tmpDir = System.getProperty("java.io.tmpdir");
  27. // System file separator
  28. private static final String fs = System.getProperty("file.separator");
  29. // Log file location
  30. public static final String LOGFILE = tmpDir + fs + "importCSV.log";
  31.  
  32. // Class variables used to create an API Session
  33. private String hostName;
  34. private int port;
  35. private boolean isSecure;
  36. private String userName;
  37. private String password;
  38.  
  39. // API Specific Objects
  40. private IntegrationPoint ip;
  41. private Session apiSession;
  42.  
  43. // Log all MKS API Commands
  44. private MKSLogger logger;
  45. private Properties loggerProps;
  46.  
  47.  
  48.  
  49. /**
  50. * Constructor for the API Session Object
  51. */
  52. public APISession()
  53. {
  54. // Delete the previous log file
  55. File logFile = new File(LOGFILE);
  56. if( logFile.canRead() ){ logFile.delete(); }
  57. // Configure the logger
  58. logger = new MKSLogger(LOGFILE);
  59. logger.configure(getLoggerProperties());
  60. logger.message("Logger initialized...");
  61. logger.message("MKS API Version: " + APIVersion.API_4_16.getMinor()+""+APIVersion.API_4_16.getMajor());
  62. // Create the login screen
  63.  
  64. }
  65.  
  66. private Properties getLoggerProperties()
  67. {
  68. // Initialize logger properties
  69. loggerProps = new Properties();
  70.  
  71. // Logging Categories
  72. loggerProps.put("mksis.logger.message.includeCategory.DEBUG", "10");
  73. loggerProps.put("mksis.logger.message.includeCategory.WARNING", "10");
  74. loggerProps.put("mksis.logger.message.includeCategory.GENERAL", "10");
  75. loggerProps.put("mksis.logger.message.includeCategory.ERROR", "10");
  76. // Output Format
  77. loggerProps.put("mksis.logger.message.defaultFormat", "{2}({3}): {4}");
  78. loggerProps.put("mksis.logger.message.format.DEBUG", "{2}({3}): {4}");
  79. loggerProps.put("mksis.logger.message.format.WARNING", "* * * * {2} * * * * ({3}): {4}");
  80. loggerProps.put("mksis.logger.message.format.ERROR", "* * * * {2} * * * * ({3}): {4}");
  81.  
  82. return loggerProps;
  83. }
  84.  
  85. public MKSLogger getLogger()
  86. {
  87. return logger;
  88. }
  89.  
  90. public void connect()
  91. {
  92. // Keeping trying to connect until successful
  93. // or user terminates program...
  94. try
  95. {
  96. connectToSuccess();
  97. }
  98. catch(APIException aex)
  99. {
  100. //ExceptionHandler eh = new ExceptionHandler(aex);
  101. logger.message("Error Connected..."+aex.getMessage() );
  102. connect();
  103. }
  104. }
  105. public void setUserParametres(String hostName, int port, String userName, String password ){
  106. this.hostName = hostName;
  107. this.port = port;
  108. this.userName = userName;
  109. this.password = password;
  110. }
  111.  
  112. private void connectToSuccess() throws APIException
  113. {
  114. // Show the modal login screen.
  115.  
  116. // Get the user entered data
  117. hostName = "win7dev";
  118. port = 7001;
  119. //isSecure = loginFrame.getSecureConnection();
  120. userName ="administrator";
  121. password = "password";
  122.  
  123. // Create a Server Integration Point based on the API Version
  124. logger.message(MKSLogger.DEBUG, "Creating server integration point...");
  125. ip = IntegrationPointFactory.getInstance().createIntegrationPoint(hostName, port, isSecure, APIVersion.API_4_16.getMajor(),APIVersion.API_4_16.getMinor());
  126. logger.message(MKSLogger.DEBUG, "Server integration point created!");
  127. // Create an authenticated session
  128. logger.message(MKSLogger.DEBUG, "Creating api session...");
  129. apiSession = ip.createSession(userName, password);
  130. logger.message(MKSLogger.DEBUG, "API Session created!");
  131. // Open a connection to the MKS Integrity Server
  132. logger.message(MKSLogger.DEBUG, "Connecting to MKS Integrity Server...");
  133. Command imConnect = new Command(Command.IM, "connect");
  134. CmdRunner cmdRunner = apiSession.createCmdRunner();
  135. cmdRunner.setDefaultHostname(hostName);
  136. cmdRunner.setDefaultPort(port);
  137. cmdRunner.setDefaultUsername(userName);
  138. cmdRunner.setDefaultPassword(password);
  139. Response res = runCommand(imConnect);
  140. logger.message(MKSLogger.DEBUG, res.getCommandString());
  141. logger.message(MKSLogger.DEBUG, "im connect exit code was " + res.getExitCode());
  142. logger.message(MKSLogger.DEBUG, "Releasing command runner...");
  143. //cmdRunner.release();
  144. logger.message(MKSLogger.DEBUG, "Command runner released!");
  145. logger.message(MKSLogger.DEBUG, "Connection successfully established!");
  146. }
  147.  
  148. /**
  149. * This function executes a generic API/CLI Command
  150. * @param cmd MKS API Command Object representing a CLI command
  151. * @return MKS API Response Object
  152. * @throws APIException
  153. */
  154. public Response runCommand(Command cmd) throws APIException
  155. {
  156. logger.message(MKSLogger.DEBUG, MKSLogger.LOW, "Attempting to run " + cmd.getApp() + ' ' + cmd.getCommandName());
  157. CmdRunner cmdRunner = apiSession.createCmdRunner();
  158. cmdRunner.setDefaultHostname(hostName);
  159. cmdRunner.setDefaultPort(port);
  160. cmdRunner.setDefaultUsername(userName);
  161. cmdRunner.setDefaultPassword(password);
  162. Response res = cmdRunner.execute(cmd);
  163. cmdRunner.release();
  164. logger.message(MKSLogger.DEBUG, MKSLogger.LOW, "Command " + res.getCommandString() + " completed with exit code " + res.getExitCode());
  165. return res;
  166. }
  167.  
  168. /**
  169. * This function executes a generic API/CLI Command impersonating another user
  170. * @param cmd MKS API Command Object representing a CLI command
  171. * @param impersonateUser The user to impersonate
  172. * @return MKS API Response Object
  173. * @throws APIException
  174. */
  175. public Response runCommandAs(Command cmd, String impersonateUser) throws APIException
  176. {
  177. logger.message(MKSLogger.DEBUG, MKSLogger.LOW, "Attempting to run " + cmd.getApp() + ' ' + cmd.getCommandName() + " as " + impersonateUser);
  178. CmdRunner cmdRunner = apiSession.createCmdRunner();
  179. cmdRunner.setDefaultHostname(hostName);
  180. cmdRunner.setDefaultPort(port);
  181. cmdRunner.setDefaultUsername(userName);
  182. cmdRunner.setDefaultPassword(password);
  183. cmdRunner.setDefaultImpersonationUser(impersonateUser);
  184. Response res = cmdRunner.execute(cmd);
  185. cmdRunner.release();
  186. logger.message(MKSLogger.DEBUG, MKSLogger.LOW, "Command " + res.getCommandString() + " completed with exit code " + res.getExitCode());
  187. return res;
  188. }
  189.  
  190. /**
  191. * Terminate the API Session and Integration Point
  192. * @throws APIException
  193. * @throws IOException
  194. */
  195. public void Terminate() throws APIException, IOException
  196. {
  197. logger.message(MKSLogger.DEBUG, MKSLogger.LOW, "Attempting to terminate API Session");
  198. if( null != apiSession )
  199. {
  200. apiSession.release();
  201. }
  202.  
  203. if( null != ip )
  204. {
  205. ip.release();
  206. }
  207. logger.message(MKSLogger.DEBUG, MKSLogger.LOW, "API Session terminated!");
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement