Solved

What does this Java Class do?

Posted on 2007-03-29
1
298 Views
Last Modified: 2010-03-31
What does this class do?

package org.muos.cc.support;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.muos.cc.ClearCaseService;
import org.muos.management.managementSupport.ui.UserInterface;
import org.muos.mte.TestEnvConsole;

public class DefaultServiceImpl implements ClearCaseService
{
   //to get or show information
   private UserInterface userInterface;

   //Pointer to TestEnvironmentConsole if available
   private TestEnvConsole testEnvConsole;
   //Environments operating system
   private String os;
   //System user (badge number)
   private String user;
   //current cleartool command
   private String cleartoolCmd;
   //logger
   private Logger logger;

   /**
    * Default constructor with configuration.
    *
    */
   public DefaultServiceImpl(String os, String cleartoolCmd, TestEnvConsole console, String user, UserInterface userInterface) {
         this.logger = Logger.getLogger("org.muos.cc.support.DefaultServiceImpl");
         this.os = os;
         this.cleartoolCmd = cleartoolCmd;
         this.testEnvConsole = console;
         this.userInterface = userInterface;
         this.user = user;
   }

   /**
    * This method searches for all of the current user's views and allows one to be selected
    *
    */
   public void selectView()
   {
      Object [] views = getViews(); //retrieves views for dialog box

      try
      {
         //prompt user to select a view
         String choice = userInterface.promptWithSelection("Select a UCM view...", "UCM Views", views, views[0]);
         //if cancel not selected
         if(choice != null)
         {
            //attempt to start view
            startView((String)choice);
         }
      }
      catch(Exception e)
      {
         logger.warn(e.toString());
         userInterface.error(e, "Error");
      }
   }

   /**
    * Attempts to check in the specified file from UCM
    *
    * @param file File to be checked in
    * @return 0 if successful, !=0 if error occurs
    */
   public int checkin(String file)
   {
      int exit_status = 1;

      //prompt user for comments for checkin
      String comment = userInterface.prompt("Insert checkin comments", "UCM Checkin");
      comment = comment.trim(); //trims trailing spaces

      //creates command string depending on operating system (Win/*nix)
      String [] cmmd;
      if(comment.length() == 0)
      {
         cmmd = new String[] { cleartoolCmd, "ci", "-nc", file};
      }
      else
      {
         cmmd = new String[] { cleartoolCmd, "ci", "-c", comment, file};
      }

      exit_status = executeCommand(cmmd);

      //0 = successful, failure otherwise
      if(exit_status == 0)
      {
         userInterface.message(file + " checked in!", "UCM Checkin");
      }

      return exit_status;
   }

   /**
    * Attempts to check out the specified file from the UCM system
    * @param file File to be checked in
    * @return 0 if successful, !=0 if failure
    */
   public int checkout(String file)
   {
      int exit_status = 1;

      //prompt user for comments for checkout
      String comment = "";
      comment = userInterface.prompt("Insert checkout comments", "UCM Checkout");
      comment = comment.trim(); //trims trailing spaces

      //creates command string depending on operating system (Win/*nix)
      String [] cmmd;
      if(comment.length() == 0)
      {
         cmmd = new String[] { cleartoolCmd, "co", "-nc", file};
      }
      else
      {
         cmmd = new String[] { cleartoolCmd, "co", "-c", comment, file};
      }

      //attempt to execute the command
      exit_status = executeCommand(cmmd);

      //0 is successful, failure otherwise
      if(exit_status == 0)
      {
            userInterface.message(file + " checked out!", "UCM Checkout");
      }

         return exit_status;
   }

   /**
    * Attempts to check out the specified file from the UCM system
    * @param file File to be checked in
    * @return 0 if successful, !=0 if failure
    */
   public int undoCheckout(String file)
   {
      int exit_status = 1;

      //Ask if the user would like to keep a private file
      String keepPrivate = userInterface.comfirm("Would you like to save a private copy of the file?", "UCM Uncheckout File...");

      //creates command string depending on operating system (Win/*nix)
      String [] command_string;
      if(UserInterface.YES.equals(keepPrivate))
      {
         command_string = new String[] { cleartoolCmd, "unco", "-keep", file };
      }
      else
      {
         command_string = new String[] { cleartoolCmd, "unco", "-rm", file };
      }

      //attempt to execute the command
      exit_status = executeCommand(command_string);

      if(exit_status == 0)
      {
         userInterface.message(file + " un-checked out!", "Undo Confirmation");
      }

      return exit_status;
   }

   public int makeNewElem(String dir, String file) {
            int exit_status = 1;

            //prompt user for comments for checkout
            String comment = "";
            if(!userInterface.isQuiet())
            {
                  comment = userInterface.prompt("Insert checkout comments", "UCM Checkout");
            }
            comment = comment.trim(); //trims trailing spaces

            //creates command string depending on operating system (Win/*nix)
            String [] cmmd;
            if(comment.length() == 0)
            {
               cmmd = new String[] { cleartoolCmd, "co", "-nc", dir};
            }
            else
            {
               cmmd = new String[] { cleartoolCmd, "co", "-c", comment, dir};
            }

            //attempt to execute the command
            exit_status = executeCommand(cmmd);

            //0 is successful, failure otherwise
            if(exit_status != 0)
            {
                 if(!userInterface.isQuiet())
                 {
                       userInterface.error("Check out directory failed! Can not create new Element! ", "Fail");
                 }
                 return exit_status;
            }
            if(!userInterface.isQuiet())
            {
                  userInterface.message(dir + " checked out!", "UCM Checkout");
            }

            //Prepare command to make new element
            cmmd = new String[] {cleartoolCmd, "mkelem", file};
            //Execute this command to create new element;
            exit_status = executeCommand(cmmd);
            if(exit_status != 0)
            {
                  if(!userInterface.isQuiet())
                  {
                        userInterface.error("Execute command failed.", "Fail");
                  }
                  return exit_status;
            }
            if(!userInterface.isQuiet())
            {
                  userInterface.message( " make new element " + file, "UCM Checkout");
            }

            //Prepare command to checkin new element
            cmmd = new String[] {cleartoolCmd, "ci", dir};
            exit_status = executeCommand(cmmd);
            if(exit_status != 0)
            {
                  if(!userInterface.isQuiet())
                  {
                        userInterface.error("Execute command failed.", "Fail");
                  }
                  return exit_status;
            }
            if(!userInterface.isQuiet())
            {
                  userInterface.message( " check in  dir " + dir + " \n new element " + file + "added. ", "UCM Checkout");
            }
            return exit_status;
   }

   /**
    * Returns a Object array of views for the current user
    * @return Array of view names (String)
    */
   private Object [] getViews()
   {
      Vector<Object> viewVector = new Vector<Object>();   //temporary holding place for views

      //create command based on operating system
      String [] command_string;
      command_string = new String[] { cleartoolCmd, "lsview", "-s", "*" + user + "*"};

      //attempt to execute command
      BufferedReader input = null;
      try
      {
         Runtime rt = Runtime.getRuntime();
         Process getViews = rt.exec(command_string);
         InputStream is = getViews.getInputStream();
         input = new BufferedReader(new InputStreamReader(is));
         String fromCommand = input.readLine();
         while(fromCommand != null)
         {
            viewVector.add(fromCommand);
            fromCommand = input.readLine();
         }

      }
      catch(Exception e)
      {
         logger.warn(e.toString());
         userInterface.error(e, "Error");
      }
      finally
      {
         if(input != null)
         {
            try
            {
               input.close();
            }
            catch(Exception e)
            {
               logger.warn(e.toString());
               e.printStackTrace();
            }
         }
      }

      //place views into the object array then empty and remove the vector
      Object [] returnMe = viewVector.toArray();
      viewVector = null;

      return returnMe;
   }

   /**
    * Attempts to start the specified view
    * @param view UCM view to be started
    */
   private void startView(String view)
   {
      //creates command based on operating system
      String [] command_string;
      command_string = new String[] {cleartoolCmd, "startview", view};
      //if there's a pointer to the TestEnvConsole, set directory to the vob
      if(testEnvConsole != null)
      {

         //sets diff root path depending on OS
         if(os.substring(0,3).equals("Win"))
         {
            testEnvConsole.setLastSelectedDirectory("X:\\" + view);
         }
         else
         {
            testEnvConsole.setLastSelectedDirectory("/view/" + view + "/vob");
         }

      }

      //Attempt to start the view
      int exit_status = executeCommand(command_string);

      //0 if successful, failure otherwise
      if(exit_status == 0)
      {
         if(testEnvConsole != null)
         {
            testEnvConsole.appendTitle("[Current Working View: " + view + "]");
         }
         userInterface.info(view + " successfully started!", "UCM Message");
      }
   }


   /**
    * This method was abstracted out to execute the specified
    * system command.  Exceptions will be caught here, and any
    * system command that doesn't exit with a 0 will be caught here
    * This method will display a popup with the error.  Display
    * messages for correct execution should be made in the calling
    * method.
    * @param cmd System command to be executed
    * @return Exit status from system command
    */
   private int executeCommand(String [] cmd)
   {
      int exit_status = 0;          //assume execution was successful
      BufferedReader input = null;  //for input stream
      BufferedReader error = null;  //for error stream
      try
      {
         //execute system command
         Runtime rt = Runtime.getRuntime();
         Process proc = rt.exec(cmd);
         InputStream is = proc.getInputStream();
         InputStream es = proc.getErrorStream();
         input = new BufferedReader(new InputStreamReader(is));
         error = new BufferedReader(new InputStreamReader(es));
         String fromCommand = input.readLine();
         String fromError = error.readLine();
         StringBuffer buffer = new StringBuffer();
         StringBuffer errorBuffer = new StringBuffer();

         //read from input stream as system command executes
         while(fromCommand != null  || fromError != null)
         {
            errorBuffer.append(fromError);
            buffer.append(fromCommand);

            fromCommand = input.readLine();
            fromError = error.readLine();
         }

         //waits for command to execute, saves exit status
         exit_status = proc.waitFor();

         //if system command not successful
         if(exit_status != 0)
         {
            userInterface.message(errorBuffer.toString(), "Error");
         }
      }
      //catch any exceptional cases during execution
      catch(Exception e)
      {
         logger.warn(e.toString());
         //display error to the user
         userInterface.error(e, "Error");
      }
      //close all resources
      finally
      {
         if(input != null)
         {
            try
            {
               input.close();
            }
            catch(Exception e)
            {
               e.printStackTrace();
            }
         }
         if(error != null)
         {
            try
            {
               error.close();
            }
            catch(Exception e)
            {
               e.printStackTrace();
            }
         }
      }

      return exit_status;
   }

   /**
    * Set UserConnector to this class.
    * @param userConnector
    */
      public void setUserConnector(UserInterface userConnector) {
            this.userInterface = userConnector;
      }

      public void setOs(String os) {
            this.os = os;
      }

      public void setTestEnvConsole(TestEnvConsole testEnvConsole) {
            this.testEnvConsole = testEnvConsole;
      }

      public void setUser(String user) {
            this.user = user;
      }

      public void setUserInterface(UserInterface userInterface) {
            this.userInterface = userInterface;
      }

      /**
       * This implementation may be wrong, because I don't know how
       * to get a file from the clearcase. Open the view and check
       * out the file, right?
       */
      //FIXME: check the implementation of getting file from clearcase
      public String getConfigFile(String ccView, String ccFileName) {
            this.startView(ccView);
            checkout(ccFileName);
            return ccFileName;
      }
}
0
Comment
Question by:yankeebushsoftware
1 Comment
 
LVL 11

Accepted Solution

by:
elfe69 earned 500 total points
ID: 18816579
It is the implementation of a ClearCase client in Java
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now