Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/jbossmx/cluster/watchdog/mbean/watchdog/CallScriptCorrectiveAction.java


1   /**
2    * JBoss, the OpenSource EJB server
3    *
4    * Distributable under LGPL license Version 2.1, February 1999.
5    * See terms of license at gnu.org.
6    */
7   
8   package org.jbossmx.cluster.watchdog.mbean.watchdog;
9   
10  /**
11   * CorrectiveAction for calling scripts and external programs
12   */
13  public class CallScriptCorrectiveAction
14      extends BaseCorrectiveAction
15  {
16      /**
17       * Constructor for CallScriptCorrectiveAction
18       *
19       * @param    scriptName the name of the script / external program to call
20       * @param    timeout the amount of time (milliseconds) to wait for <code>scriptName</code> to finish.
21       */
22      public CallScriptCorrectiveAction(final String scriptName, final long timeout)
23      {
24          this(scriptName, timeout, true, true);
25      }
26  
27      /**
28       * Constructor for CallScriptCorrectiveAction
29       *
30       * @param    scriptName the name of the script / external program to call
31       * @param    timeout the amount of time (milliseconds) to wait for <code>scriptName</code> to finish.
32       * @param    overidesInvokeMethodCorrectiveAction true if this CorrectiveAction overides
33       * {@link InvokeMethodCorrectiveAction}<code>s</code>
34       * @param    overidesRestartAgentCorrectiveAction true if this CorrectiveAction overides
35       * {@link RestartAgentCorrectiveAction}<code>s</code>
36       */
37      public CallScriptCorrectiveAction(final String scriptName, final long timeout,
38                                        final boolean overidesInvokeMethodCorrectiveAction,
39                                        final boolean overidesRestartAgentCorrectiveAction)
40      {
41          m_scriptName = scriptName;
42          m_timeout = timeout;
43  
44          // Let the delay between checks of Process.exitValue be the minimum of 1/4 of timeout or
45          // 5 seconds
46          m_delayTime = java.lang.Math.min(m_timeout / 4, 5000);
47  
48          m_overidesInvokeMethodCorrectiveAction = overidesInvokeMethodCorrectiveAction;
49          m_overidesRestartAgentCorrectiveAction = overidesRestartAgentCorrectiveAction;
50      }
51  
52      /**
53       * Apply this CorrectiveAction, i.e.&nbsp;calls a script / external program.
54       *
55       * @return the exit value of the script / external program
56       * @throws Exception
57       */
58      protected boolean applyImpl() throws Exception
59      {
60          final long startTime = System.currentTimeMillis();
61  
62          Process process = Runtime.getRuntime().exec(m_scriptName);
63  
64          Integer exitValue = null;
65  
66          while(exitValue == null)
67          {
68              try
69              {
70                  exitValue = new Integer(process.exitValue());
71              }
72              catch(IllegalThreadStateException itse)
73              {
74                  exitValue = null;
75              }
76  
77              if((System.currentTimeMillis() - startTime) <= m_timeout)
78              {
79                  try
80                  {
81                      Thread.currentThread().sleep(m_delayTime);
82                  }
83                  catch(Exception e)
84                  {
85                      exitValue = null;
86                  }
87              }
88              else
89              {
90                  break;
91              }
92          }
93  
94          return ((exitValue != null) && (exitValue.intValue() == 0));
95      }
96  
97      /**
98       * Returns true if <code>correctiveAction</code> overides this CorrectiveAction
99       *
100      * @param    correctiveAction the Corrective to compare to this one.
101      *
102      * @return true if <code>correctiveAction</code> overides this CorrectiveAction
103      */
104     public boolean isOverridenBy(final CorrectiveAction correctiveAction)
105     {
106         return false;
107     }
108 
109     /**
110      * Returns whether this CorrectiveAction overides
111      * {@link InvokeMethodCorrectiveAction}<code>s</code>
112      *
113      * @return whether this CorrectiveAction overides
114      * {@link InvokeMethodCorrectiveAction}<code>s</code>
115      */
116     public boolean getOveridesInvokeMethodCorrectiveAction()
117     {
118         return m_overidesInvokeMethodCorrectiveAction;
119     }
120 
121     /**
122      * Returns whether this CorrectiveAction overides
123      * {@link RestartAgentCorrectiveAction}<code>s</code>
124      *
125      * @return whether this CorrectiveAction overides
126      * {@link RestartAgentCorrectiveAction}<code>s</code>
127      */
128     public boolean getOveridesRestartAgentCorrectiveAction()
129     {
130         return m_overidesRestartAgentCorrectiveAction;
131     }
132 
133     /** The script / external program to call */
134     private String m_scriptName;
135     /** The amount of time in milliseconds to wait for the script / external program to finish */
136     private long m_timeout;
137     /** The amount of time between checks to see if the script / external program has finished */
138     private long m_delayTime;
139 
140     /** Flag indicating whether this CorrectiveAction overides
141      *  {@link InvokeMethodCorrectiveAction}<code>s</code> */
142     boolean m_overidesInvokeMethodCorrectiveAction;
143     /** Flag indicating whether this CorrectiveAction overides
144      *  {@link RestartAgentCorrectiveAction}<code>s</code> */
145     boolean m_overidesRestartAgentCorrectiveAction;
146 }