home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 139 / dpcs0999.iso / Web / CFserver / data1.cab / Java / allaire / cfide / CFServiceControl.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-04-12  |  6.4 KB  |  166 lines

  1. package allaire.cfide;
  2.  
  3. import netscape.application.Alert;
  4. import netscape.application.Application;
  5. import netscape.application.Button;
  6. import netscape.application.ExternalWindow;
  7. import netscape.application.Label;
  8. import netscape.application.Target;
  9. import netscape.application.TextField;
  10. import netscape.application.View;
  11. import netscape.util.Enumeration;
  12. import netscape.util.Vector;
  13.  
  14. public class CFServiceControl extends Application implements Target {
  15.    static final String SERVICE_PARAM = "ServiceName";
  16.    static final String PASS_PARAM = "passkey";
  17.    static final String DEFAULT_SERVICE = "Cold Fusion Application Server";
  18.    static final String SERVICE_ACTION = "Service";
  19.    static final String GET_STATE_COMMAND = "State";
  20.    static final String START_COMMAND = "Start";
  21.    static final String STOP_COMMAND = "Stop";
  22.    static final String COMMAND_OK = "OK";
  23.    static final String COMMAND_CANCEL = "Cancel";
  24.    static final String STARTED_STATE = "Started";
  25.    static final String STOPPED_STATE = "Stopped";
  26.    static final int INVALID_PASSWORD = -100;
  27.    static final String START_TITLE = "Start";
  28.    static final String STOP_TITLE = "Stop";
  29.    static final int X_PADDING = 2;
  30.    static final int HORIZ_OFFSET = 4;
  31.    static final int VERT_OFFSET = 4;
  32.    static final int BUTTON_WIDTH = 60;
  33.    static final int BUTTON_HEIGHT = 20;
  34.    static final int FIELD_HEIGHT = 20;
  35.    static final int PROMPT_WIDTH = 256;
  36.    static final int PROMPT_HEIGHT = 120;
  37.    private ExternalWindow m_prompt;
  38.    private Button m_startButton;
  39.    private Button m_stopButton;
  40.    private Label m_label;
  41.    private TextField m_passwordField;
  42.    private Button m_buttonOK;
  43.    private Button m_buttonCancel;
  44.    private CFNetRouter netRouter;
  45.    private String m_serviceName;
  46.    private String m_username;
  47.    private String m_password;
  48.    private int m_ErrorCode;
  49.    private String m_strLastErrorMessage;
  50.  
  51.    private String getPassword() {
  52.       this.m_prompt.center();
  53.       this.m_prompt.showModally();
  54.       return this.m_passwordField.stringValue();
  55.    }
  56.  
  57.    public void performCommand(String commandName, Object data) {
  58.       try {
  59.          if (commandName.equals("OK")) {
  60.             this.m_prompt.hide();
  61.             return;
  62.          }
  63.  
  64.          if (commandName.equals("Cancel")) {
  65.             this.reportError("Access denied.");
  66.             this.m_passwordField.setStringValue("");
  67.             this.m_prompt.hide();
  68.             return;
  69.          }
  70.  
  71.          this.m_strLastErrorMessage = "";
  72.          Vector vectResults = this.netRouter.callRPC("Service", commandName, this.m_serviceName);
  73.          String resultMessage = "";
  74.  
  75.          for(Enumeration enumResults = vectResults.elements(); enumResults.hasMoreElements(); resultMessage = (String)enumResults.nextElement()) {
  76.          }
  77.  
  78.          this.m_startButton.setEnabled(!resultMessage.equalsIgnoreCase("Started"));
  79.          this.m_stopButton.setEnabled(!resultMessage.equalsIgnoreCase("Stopped"));
  80.       } catch (CFRPCServerException var7) {
  81.          this.m_ErrorCode = var7.getErrorCode();
  82.          if (this.m_ErrorCode == -100) {
  83.             String strPassword = this.getPassword();
  84.             if (strPassword.length() >= 1) {
  85.                this.netRouter.changePassword(strPassword);
  86.                ((Application)this).performCommandLater(this, commandName, (Object)null);
  87.                return;
  88.             }
  89.          }
  90.       } catch (Exception var8) {
  91.          this.reportError(((Throwable)var8).getMessage());
  92.          this.m_startButton.setEnabled(true);
  93.          this.m_stopButton.setEnabled(true);
  94.       }
  95.  
  96.    }
  97.  
  98.    private void reportError(String errorMessage) {
  99.       errorMessage.trim();
  100.       if (errorMessage == null) {
  101.          errorMessage = "An unknown error occurred.";
  102.       } else if (errorMessage.startsWith("<HTML>")) {
  103.          errorMessage = "The Cold Fusion IDE service is not running.\n" + "It must be started from the server in order to control\n" + "other Cold Fusion services from this page.";
  104.       }
  105.  
  106.       Alert.runAlertExternally(Alert.notificationImage(), this.m_serviceName + " error", errorMessage, "OK", (String)null, (String)null);
  107.    }
  108.  
  109.    private String getParameter(String paramName, String defaultValue) {
  110.       String paramValue = ((Application)this).parameterNamed(paramName);
  111.       if (paramValue == null) {
  112.          paramValue = defaultValue;
  113.       }
  114.  
  115.       return paramValue;
  116.    }
  117.  
  118.    public void init() {
  119.       try {
  120.          super.init();
  121.          this.m_strLastErrorMessage = "";
  122.          this.m_serviceName = this.getParameter("ServiceName", "Cold Fusion Application Server");
  123.          this.m_password = this.getParameter("passkey", "");
  124.          this.netRouter = new CFNetRouter("", this.m_password);
  125.          this.m_startButton = new Button(1, 1, 60, 20);
  126.          this.m_startButton.setTitle("Start");
  127.          this.m_startButton.setCommand("Start");
  128.          this.m_startButton.setTarget(this);
  129.          int m_stopButtonX = this.m_startButton.x() + this.m_startButton.width() + 2;
  130.          this.m_stopButton = new Button(m_stopButtonX, 1, 60, 20);
  131.          this.m_stopButton.setTitle("Stop");
  132.          this.m_stopButton.setCommand("Stop");
  133.          this.m_stopButton.setTarget(this);
  134.          View v = ((Application)this).mainRootView();
  135.          v.addSubview(this.m_startButton);
  136.          v.addSubview(this.m_stopButton);
  137.          this.m_prompt = new ExternalWindow();
  138.          this.m_prompt.sizeTo(256, 120);
  139.          this.m_prompt.setResizable(true);
  140.          this.m_label = new Label();
  141.          this.m_label.setTitle("Password: ");
  142.          this.m_label.sizeToMinSize();
  143.          this.m_label.moveTo(4, 4);
  144.          this.m_prompt.addSubview(this.m_label);
  145.          this.m_passwordField = new TextField(this.m_label.bounds().x + this.m_label.bounds().width, 4, this.m_prompt.contentSize().width - 8 - this.m_label.bounds().width, 20);
  146.          this.m_passwordField.setDrawableCharacter('*');
  147.          this.m_prompt.addSubview(this.m_passwordField);
  148.          Button m_buttonCancel = new Button(this.m_passwordField.bounds().x + this.m_passwordField.bounds().width - 60 + 1, this.m_passwordField.bounds().y + this.m_passwordField.height() + 4, 60, 20);
  149.          m_buttonCancel.setTitle("Cancel");
  150.          m_buttonCancel.setCommand("Cancel");
  151.          m_buttonCancel.setTarget(this);
  152.          this.m_prompt.addSubview(m_buttonCancel);
  153.          Button m_buttonOK = new Button(((View)m_buttonCancel).bounds().x - 60 - 4, ((View)m_buttonCancel).bounds().y, 60, 20);
  154.          m_buttonOK.setTitle("OK");
  155.          m_buttonOK.setCommand("OK");
  156.          m_buttonOK.setTarget(this);
  157.          this.m_prompt.addSubview(m_buttonOK);
  158.          this.performCommand("State", "");
  159.       } catch (Exception var6) {
  160.          this.m_strLastErrorMessage = ((Throwable)var6).getMessage();
  161.          this.reportError(this.m_strLastErrorMessage);
  162.       }
  163.  
  164.    }
  165. }
  166.