KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > deploy > DeployThread


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  * $Header: /cvs/glassfish/admin/mbeanapi-impl/tests/com/sun/enterprise/management/deploy/DeployThread.java,v 1.4 2006/03/17 03:24:31 llc Exp $
26  * $Revision: 1.4 $
27  * $Date: 2006/03/17 03:24:31 $
28  */

29 package com.sun.enterprise.management.deploy;
30
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.List JavaDoc;
36
37 import javax.management.Notification JavaDoc;
38
39 import com.sun.appserv.management.deploy.DeploymentProgress;
40 import com.sun.appserv.management.deploy.DeploymentSource;
41 import com.sun.appserv.management.deploy.DeploymentStatus;
42
43 import com.sun.appserv.management.deploy.DeploymentProgressImpl;
44 import com.sun.appserv.management.deploy.DeploymentStatusImpl;
45
46 /**
47     This implementation of DeployThread is a stub implementation designed
48     to replace the normal implementation when unit tests are being run.
49  */

50 public final class DeployThread extends Thread JavaDoc
51 {
52     private DeployThreadParams mParams;
53     private final Object JavaDoc mDeployID;
54     private final DeploymentCallback mDeploymentCallback;
55     
56     private boolean mQuit;
57     private boolean mDone;
58     private Throwable JavaDoc mThrowable;
59     private long mDoneMillis;
60     private DeploymentStatusImpl mDeploymentStatus;
61     
62     private final List JavaDoc mQueuedNotifications;
63     
64     
65         public
66     DeployThread(
67         final Object JavaDoc id,
68         final DeploymentCallback deploymentCallback,
69         final DeployThreadParams params )
70     {
71         mDeployID = id;
72         mDeploymentCallback = deploymentCallback;
73         mParams = params;
74         
75         mDeploymentStatus = null;
76         mThrowable = null;
77         mDone = false;
78         
79         mQuit = false;
80         
81         mDoneMillis = 0;
82         
83         mQueuedNotifications = new ArrayList JavaDoc<Notification JavaDoc>();
84         
85     }
86     
87     
88         public void
89     setParams( final DeployThreadParams params )
90     {
91         if ( mParams != null )
92         {
93             throw new IllegalArgumentException JavaDoc();
94         }
95         
96         mParams = params;
97     }
98     
99         public void
100     queueNotification( final Notification JavaDoc notif )
101     {
102         if ( isDone() )
103         {
104             throw new IllegalArgumentException JavaDoc(
105                 "Notification cannot be queued after being done for: " + mDeployID);
106         }
107         
108         synchronized( mQueuedNotifications )
109         {
110             mQueuedNotifications.add( notif );
111         }
112     }
113     
114         public Notification JavaDoc[]
115     takeNotifications()
116     {
117         Notification JavaDoc[] notifs = null;
118         
119         synchronized( mQueuedNotifications )
120         {
121             notifs = new Notification JavaDoc[ mQueuedNotifications.size() ];
122             mQueuedNotifications.toArray( notifs );
123             mQueuedNotifications.clear();
124         }
125         
126         return( notifs );
127     }
128     
129         public DeploymentStatus
130     getDeploymentStatus( )
131     {
132         return( mDeploymentStatus );
133     }
134     
135     
136         public boolean
137     quit()
138     {
139         mQuit = true;
140         this.interrupt();
141         
142         while ( ! isDone() )
143         {
144             try
145             {
146                 Thread.sleep( 200 );
147             }
148             catch( InterruptedException JavaDoc e )
149             {
150             }
151         }
152         
153         return( true );
154     }
155     
156         static private void
157     trace( Object JavaDoc o )
158     {
159         System.out.println( o.toString() );
160     }
161     
162     
163         private DeploymentStatusImpl
164     deploy(
165         final DeployThreadParams params,
166         final DeploymentCallback callback )
167         throws InterruptedException JavaDoc
168     {
169         int percent = 0;
170         
171         while ( percent < 100 )
172         {
173             percent += 10;
174             
175             final DeploymentProgress progress =
176                 new DeploymentProgressImpl(
177                     (byte)percent, "percent done", null);
178                 
179             callback.deploymentProgress( progress );
180             Thread.sleep( 1 );
181         }
182         
183         final DeploymentStatusImpl deploymentStatus =
184             new DeploymentStatusImpl(
185             0,
186             "completed",
187             "description",
188             null );
189         
190         return( deploymentStatus );
191     }
192
193         public void
194     run()
195     {
196         mThrowable = null;
197         mDone = false;
198         
199         //trace( "DeployThread.run: starting: " + getID() );
200
try
201         {
202             if ( mParams == null )
203             {
204                 throw new IllegalArgumentException JavaDoc( "no params specified" );
205             }
206             
207             //trace( "DeployThread.run: calling deploy() for: " + getID() );
208
mDeploymentStatus = deploy( mParams, mDeploymentCallback );
209             //trace( "DeployThread.run: deploy() successful for: " + getID() );
210
}
211         catch( Throwable JavaDoc t )
212         {
213             mDeploymentStatus =
214             new DeploymentStatusImpl(
215                 -1,
216                 "failure",
217                 "description",
218                 null );
219                 
220             mThrowable = t;
221             
222             mDeploymentStatus.setStageThrowable( t );
223         }
224         
225         try
226         {
227             mDeploymentCallback.deploymentDone( mDeploymentStatus );
228             
229             // success or failure, always kill the file
230
if ( mParams.getDeployFile() != null )
231             {
232                 //trace( "DeployThread.run: deleting deploy file: " + mParams.getDeployFile() );
233
mParams.getDeployFile().delete();
234             }
235             
236             // success or failure, always kill the file
237
if ( mParams.getPlanFile() != null )
238             {
239                 //trace( "DeployThread.run: deleting plan file: " + mParams.getPlanFile() );
240
mParams.getPlanFile().delete();
241             }
242         }
243         finally
244         {
245             mDoneMillis = System.currentTimeMillis();
246             mDone = true;
247         }
248         
249         //trace( "DeployThread.run: done with: " + getID() );
250
}
251     
252     /**
253         @return the number of milliseconds since the deploy finished
254      */

255         public long
256     getMillisSinceDone()
257     {
258         return( isDone() ? (System.currentTimeMillis() - mDoneMillis) : 0 );
259     }
260     
261     /**
262         @return the ID of this DeployThread
263      */

264         public Object JavaDoc
265     getID()
266     {
267         return( mDeployID );
268     }
269     
270     
271     /**
272         @return the ID of this DeployThread
273      */

274         public DeploymentProgress
275     getDeploymentProgress()
276     {
277         final byte progressPercent = 0;
278         final String JavaDoc description = "<no description>";
279         
280         final DeploymentProgressImpl progress =
281             new DeploymentProgressImpl( progressPercent, description, null);
282         
283         return( progress );
284     }
285     
286     
287     /**
288         @return true if done (success or failure)
289      */

290         public boolean
291     isDone()
292     {
293         return( mDone );
294     }
295     
296     /**
297         @return true if done and success
298      */

299         public boolean
300     getSuccess()
301     {
302         return( isDone() && mDeploymentStatus != null );
303     }
304     
305     /**
306         Return any Throwabe that caused a failure
307         
308         @return the Throwable, or null if none.
309      */

310         public Throwable JavaDoc
311     getThrowable()
312     {
313         return( mThrowable );
314     }
315 }
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
Popular Tags