KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > virtualdatabase > management > AbstractAdminOperation


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2006 Continuent, Inc.
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Emmanuel Cecchet.
19  * Contributor(s): ______________________.
20  */

21
22 package org.continuent.sequoia.controller.virtualdatabase.management;
23
24 import org.continuent.sequoia.common.exceptions.NotImplementedException;
25
26 /**
27  * This class defines an AbstractAdminOperation that is used to account for
28  * currently executing administrative operations on a virtual database. A
29  * virtual database shutdown will not be allowed as long as one of such
30  * operation is pending.
31  *
32  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
33  * @version 1.0
34  */

35 public abstract class AbstractAdminOperation
36 {
37   private long startTime;
38   private long endTime;
39   protected int operationStatus;
40
41   /**
42    * Status of an operation that has not started yet
43    */

44   public static final int STATUS_NOT_STARTED = 0;
45   /**
46    * Status of an operation that is currently executing
47    */

48   public static final int STATUS_EXECUTING = 1;
49   /**
50    * Status of an operation that has successfully completed
51    */

52   public static final int STATUS_SUCCESS = 2;
53   /**
54    * Status of an operation that has failed
55    */

56   public static final int STATUS_FAILED = 3;
57
58   // Object for status completion synchronization
59
private final Object JavaDoc completionStatus = new Object JavaDoc();
60
61   /**
62    * Creates a new <code>AbstractAdminOperation</code> object by initializing
63    * its start time to the current time and its status to STATUS_NOT_STARTED
64    */

65   public AbstractAdminOperation()
66   {
67     startTime = System.currentTimeMillis();
68     operationStatus = STATUS_NOT_STARTED;
69   }
70
71   /**
72    * Cancel the current operation.
73    *
74    * @throws NotImplementedException if the operation is not supported
75    */

76   public abstract void cancel() throws NotImplementedException;
77
78   /**
79    * Returns a String containing a human readable description of the executing
80    * operation.
81    *
82    * @return operation description
83    */

84   public abstract String JavaDoc getDescription();
85
86   /**
87    * Returns the operation completion time.
88    *
89    * @return Returns the endTime.
90    */

91   public final long getEndTime()
92   {
93     return endTime;
94   }
95
96   /**
97    * Sets the operation end time value.
98    *
99    * @param endTime end time in ms.
100    */

101   public final void setEndTime(long endTime)
102   {
103     this.endTime = endTime;
104   }
105
106   /**
107    * Returns the operation starting time.
108    *
109    * @return Returns the start time.
110    */

111   public final long getStartTime()
112   {
113     return startTime;
114   }
115
116   /**
117    * Sets the operation starting time value.
118    *
119    * @param startTime start time in ms.
120    */

121   public final void setStartTime(long startTime)
122   {
123     this.startTime = startTime;
124   }
125
126   /**
127    * Return the current status of the operation.
128    *
129    * @return operation status
130    * @see #STATUS_NOT_STARTED
131    * @see #STATUS_EXECUTING
132    * @see #STATUS_SUCCESS
133    * @see #STATUS_FAILED
134    */

135   public int getStatus()
136   {
137     return operationStatus;
138   }
139
140   /**
141    * Notify the completion of the operation with success or not. This updates
142    * the operation status and end time.
143    *
144    * @param isSuccess true if the operation was successful
145    */

146   public void notifyCompletion(boolean isSuccess)
147   {
148     synchronized (completionStatus)
149     {
150       endTime = System.currentTimeMillis();
151       if (isSuccess)
152         operationStatus = STATUS_SUCCESS;
153       else
154         operationStatus = STATUS_FAILED;
155       completionStatus.notifyAll();
156     }
157   }
158
159   /**
160    * Wait for the operation completion. This method blocks until the command has
161    * completed (successfully or not)
162    */

163   public void waitForCompletion()
164   {
165     synchronized (completionStatus)
166     {
167       while ((operationStatus == STATUS_EXECUTING)
168           || (operationStatus == STATUS_NOT_STARTED))
169       {
170         try
171         {
172           completionStatus.wait();
173         }
174         catch (InterruptedException JavaDoc ignore)
175         {
176         }
177       }
178     }
179   }
180
181 }
182
Popular Tags