KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > helper > DeployNotificationListener


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 package com.sun.appserv.management.helper;
24
25 import java.util.Map JavaDoc;
26 import java.io.Serializable JavaDoc;
27
28 import javax.management.NotificationListener JavaDoc;
29 import javax.management.Notification JavaDoc;
30 import javax.management.ListenerNotFoundException JavaDoc;
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 /**
43     A NotificationListener designed to listen to the {@link DeploymentMgr}.
44     <p>
45     Note that Notifications are not guaranteed to be delivered in order.
46     Thus, it is theoretically possible for a DEPLOYMENT_COMPLETED_NOTIFICATION_TYPE
47     to be received before a DEPLOYMENT_STARTED_NOTIFICATION_TYPE.
48     <p>
49     A subclass may choose to override {@link #deploymentStarted},
50     {@link #deploymentProgress} and
51     @since AppServer 9.0
52  */

53 public class DeployNotificationListener
54     implements NotificationListener JavaDoc
55 {
56     private final Object JavaDoc 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 JavaDoc deployID )
66     {
67         if ( deploymentMgr == null || deployID == null )
68         {
69             throw new IllegalArgumentException JavaDoc();
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     /**
81         @return the DeploymentMgr in use for deployment.
82      */

83         public DeploymentMgr
84     getDeploymentMgr()
85     {
86         return mDeploymentMgr;
87     }
88     
89     /**
90         @return the deployID this listener responds to
91      */

92         public Object JavaDoc
93     getDeployID()
94     {
95         return mDeployID;
96     }
97     
98     /**
99         Return true if deployment has finished.
100      */

101         public boolean
102     isCompleted()
103     {
104         return( mIsCompleted );
105     }
106     
107     
108     /**
109         @return DeploymentStatus, may be null if not finished or abnormally terminated.
110      */

111         public DeploymentStatus
112     getDeploymentStatus()
113     {
114         return( mDeploymentStatus );
115     }
116     
117     
118     /**
119         @return the latest DeploymentProgress, may be null
120      */

121         public DeploymentProgress
122     getDeploymentProgress()
123     {
124         return( mDeploymentProgress );
125     }
126     
127     /**
128         Callback for all Notifications occurring during deployment.
129         Note for public use.
130      */

131         public synchronized void
132     handleNotification(
133         final Notification JavaDoc notif,
134         final Object JavaDoc handback)
135     {
136         try
137         {
138             realHandleNotification( notif, handback );
139         }
140         catch( Exception JavaDoc e )
141         {
142             e.printStackTrace();
143         }
144     }
145     
146     /**
147         Deployment has finished. For convenience, the DeploymentStatus
148         has been extracted from the Notification.
149      */

150         protected void
151     deploymentDone(
152         final Notification JavaDoc notif,
153         final DeploymentStatus status )
154     {
155         //SampleUtil.println( "Deployment completed for " + deployID + " with status: " +
156
//status.getStageStatus() );
157
mIsCompleted = true;
158         mDeploymentStatus = status;
159     }
160     
161     /**
162         Deployment has been cancelled. There may or may not be a
163         DeploymentStatus.
164      */

165         protected void
166     deploymentAborted(
167         final Notification JavaDoc notif,
168         final DeploymentStatus status )
169     {
170         mIsCompleted = true;
171         mDeploymentStatus = status;
172     }
173     
174     
175     /**
176         Deployment progress. For convenience, the DeploymentProgress
177         has been extracted from the Notification.
178      */

179         protected void
180     deploymentProgress(
181         final Notification JavaDoc notif,
182         final DeploymentProgress status )
183     {
184         mDeploymentProgress = status;
185     }
186     
187     /**
188         Deployment has begun. The Notification contains no additional
189         data.
190      */

191         protected void
192     deploymentStarted( final Notification JavaDoc notif )
193     {
194     }
195     
196     /**
197         Clean things up. This mainly means
198         removing this listener from the DeploymentMgr.
199      */

200         public void
201     cleanup()
202     {
203         try
204         {
205             mDeploymentMgr.removeNotificationListener( this, null, null );
206         }
207         catch( ListenerNotFoundException JavaDoc e )
208         {
209         }
210     }
211     
212     /**
213         Handle a received Notification.
214      */

215         protected final void
216     realHandleNotification(
217         final Notification JavaDoc notif,
218         final Object JavaDoc handback)
219     {
220         final String JavaDoc type = notif.getType();
221         final Map JavaDoc<String JavaDoc,?> m = TypeCast.asMap( notif.getUserData() );
222         final Object JavaDoc 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 JavaDoc<String JavaDoc,Serializable JavaDoc> statusData = (Map JavaDoc<String JavaDoc,Serializable JavaDoc>)
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 JavaDoc<String JavaDoc,Serializable JavaDoc> statusData = (Map JavaDoc<String JavaDoc,Serializable JavaDoc>)
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