1 23 24 29 package com.sun.enterprise.management.deploy; 30 31 import java.io.File ; 32 import java.io.IOException ; 33 import java.util.Collections ; 34 import java.util.ArrayList ; 35 import java.util.List ; 36 37 import javax.management.Notification ; 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 50 public final class DeployThread extends Thread 51 { 52 private DeployThreadParams mParams; 53 private final Object mDeployID; 54 private final DeploymentCallback mDeploymentCallback; 55 56 private boolean mQuit; 57 private boolean mDone; 58 private Throwable mThrowable; 59 private long mDoneMillis; 60 private DeploymentStatusImpl mDeploymentStatus; 61 62 private final List mQueuedNotifications; 63 64 65 public 66 DeployThread( 67 final Object 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 <Notification >(); 84 85 } 86 87 88 public void 89 setParams( final DeployThreadParams params ) 90 { 91 if ( mParams != null ) 92 { 93 throw new IllegalArgumentException (); 94 } 95 96 mParams = params; 97 } 98 99 public void 100 queueNotification( final Notification notif ) 101 { 102 if ( isDone() ) 103 { 104 throw new IllegalArgumentException ( 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 [] 115 takeNotifications() 116 { 117 Notification [] notifs = null; 118 119 synchronized( mQueuedNotifications ) 120 { 121 notifs = new Notification [ 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 e ) 149 { 150 } 151 } 152 153 return( true ); 154 } 155 156 static private void 157 trace( Object 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 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 try 201 { 202 if ( mParams == null ) 203 { 204 throw new IllegalArgumentException ( "no params specified" ); 205 } 206 207 mDeploymentStatus = deploy( mParams, mDeploymentCallback ); 209 } 211 catch( Throwable 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 if ( mParams.getDeployFile() != null ) 231 { 232 mParams.getDeployFile().delete(); 234 } 235 236 if ( mParams.getPlanFile() != null ) 238 { 239 mParams.getPlanFile().delete(); 241 } 242 } 243 finally 244 { 245 mDoneMillis = System.currentTimeMillis(); 246 mDone = true; 247 } 248 249 } 251 252 255 public long 256 getMillisSinceDone() 257 { 258 return( isDone() ? (System.currentTimeMillis() - mDoneMillis) : 0 ); 259 } 260 261 264 public Object 265 getID() 266 { 267 return( mDeployID ); 268 } 269 270 271 274 public DeploymentProgress 275 getDeploymentProgress() 276 { 277 final byte progressPercent = 0; 278 final String description = "<no description>"; 279 280 final DeploymentProgressImpl progress = 281 new DeploymentProgressImpl( progressPercent, description, null); 282 283 return( progress ); 284 } 285 286 287 290 public boolean 291 isDone() 292 { 293 return( mDone ); 294 } 295 296 299 public boolean 300 getSuccess() 301 { 302 return( isDone() && mDeploymentStatus != null ); 303 } 304 305 310 public Throwable 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 |