1 23 package com.sun.appserv.management.helper; 24 25 import java.util.Map ; 26 import java.io.Serializable ; 27 28 import javax.management.NotificationListener ; 29 import javax.management.Notification ; 30 import javax.management.ListenerNotFoundException ; 31 32 import com.sun.appserv.management.base.Util; 33 import com.sun.appserv.management.util.misc.TypeCast; 34 35 import com.sun.appserv.management.deploy.DeploymentStatus; 36 import com.sun.appserv.management.deploy.DeploymentMgr; 37 import static com.sun.appserv.management.deploy.DeploymentMgr.*; 38 import com.sun.appserv.management.deploy.DeploymentSupport; 39 import com.sun.appserv.management.deploy.DeploymentProgress; 40 41 42 53 public class DeployNotificationListener 54 implements NotificationListener 55 { 56 private final Object mDeployID; 57 private boolean mIsCompleted; 58 private DeploymentStatus mDeploymentStatus; 59 private DeploymentProgress mDeploymentProgress; 60 private final DeploymentMgr mDeploymentMgr; 61 62 public 63 DeployNotificationListener( 64 final DeploymentMgr deploymentMgr, 65 final Object deployID ) 66 { 67 if ( deploymentMgr == null || deployID == null ) 68 { 69 throw new IllegalArgumentException (); 70 } 71 72 mIsCompleted = false; 73 mDeploymentStatus = null; 74 mDeploymentProgress = null; 75 mDeploymentMgr = deploymentMgr; 76 mDeployID = deployID; 77 mDeploymentMgr.addNotificationListener( this, null, null ); 78 } 79 80 83 public DeploymentMgr 84 getDeploymentMgr() 85 { 86 return mDeploymentMgr; 87 } 88 89 92 public Object 93 getDeployID() 94 { 95 return mDeployID; 96 } 97 98 101 public boolean 102 isCompleted() 103 { 104 return( mIsCompleted ); 105 } 106 107 108 111 public DeploymentStatus 112 getDeploymentStatus() 113 { 114 return( mDeploymentStatus ); 115 } 116 117 118 121 public DeploymentProgress 122 getDeploymentProgress() 123 { 124 return( mDeploymentProgress ); 125 } 126 127 131 public synchronized void 132 handleNotification( 133 final Notification notif, 134 final Object handback) 135 { 136 try 137 { 138 realHandleNotification( notif, handback ); 139 } 140 catch( Exception e ) 141 { 142 e.printStackTrace(); 143 } 144 } 145 146 150 protected void 151 deploymentDone( 152 final Notification notif, 153 final DeploymentStatus status ) 154 { 155 mIsCompleted = true; 158 mDeploymentStatus = status; 159 } 160 161 165 protected void 166 deploymentAborted( 167 final Notification notif, 168 final DeploymentStatus status ) 169 { 170 mIsCompleted = true; 171 mDeploymentStatus = status; 172 } 173 174 175 179 protected void 180 deploymentProgress( 181 final Notification notif, 182 final DeploymentProgress status ) 183 { 184 mDeploymentProgress = status; 185 } 186 187 191 protected void 192 deploymentStarted( final Notification notif ) 193 { 194 } 195 196 200 public void 201 cleanup() 202 { 203 try 204 { 205 mDeploymentMgr.removeNotificationListener( this, null, null ); 206 } 207 catch( ListenerNotFoundException e ) 208 { 209 } 210 } 211 212 215 protected final void 216 realHandleNotification( 217 final Notification notif, 218 final Object handback) 219 { 220 final String type = notif.getType(); 221 final Map <String ,?> m = TypeCast.asMap( notif.getUserData() ); 222 final Object deployID = m.get( DeploymentMgr.NOTIF_DEPLOYMENT_ID_KEY ); 223 224 if ( deployID.equals( mDeployID ) ) 225 { 226 if ( type.equals( DeploymentMgr.DEPLOYMENT_STARTED_NOTIFICATION_TYPE ) ) 227 { 228 deploymentStarted( notif ); 229 } 230 else if ( type.equals( DeploymentMgr.DEPLOYMENT_ABORTED_NOTIFICATION_TYPE ) ) 231 { 232 try 233 { 234 deploymentAborted( notif, null ); 235 } 236 finally 237 { 238 cleanup(); 239 } 240 } 241 else if ( type.equals( DeploymentMgr.DEPLOYMENT_COMPLETED_NOTIFICATION_TYPE ) ) 242 { 243 final Map <String ,Serializable > statusData = (Map <String ,Serializable >) 244 Util.getAMXNotificationValue( notif, NOTIF_DEPLOYMENT_COMPLETED_STATUS_KEY ); 245 246 final DeploymentStatus status = 247 DeploymentSupport.mapToDeploymentStatus( statusData ); 248 249 try 250 { 251 deploymentDone( notif, status ); 252 } 253 finally 254 { 255 cleanup(); 256 } 257 } 258 else if ( type.equals( DeploymentMgr.DEPLOYMENT_PROGRESS_NOTIFICATION_TYPE ) ) 259 { 260 final Map <String ,Serializable > statusData = (Map <String ,Serializable >) 261 Util.getAMXNotificationValue( notif, NOTIF_DEPLOYMENT_PROGRESS_KEY ); 262 263 final DeploymentProgress progress = 264 DeploymentSupport.mapToDeploymentProgress( statusData ); 265 266 deploymentProgress( notif, progress ); 267 } 268 } 269 } 270 } 271 272 | Popular Tags |