KickJava   Java API By Example, From Geeks To Geeks.

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


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.enterprise.management.deploy;
24
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Collections JavaDoc;
30
31 import java.io.File JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.Serializable JavaDoc;
35
36 import javax.management.ObjectName JavaDoc;
37 import javax.management.Notification JavaDoc;
38
39
40 import com.sun.appserv.management.base.Utility;
41 import com.sun.appserv.management.base.Singleton;
42 import com.sun.appserv.management.base.XTypes;
43 import com.sun.appserv.management.deploy.DeploymentMgr;
44
45 import com.sun.enterprise.management.support.UniqueIDGenerator;
46
47 import com.sun.appserv.management.deploy.DeploymentSupport;
48 import com.sun.appserv.management.deploy.DeploymentSource;
49 import com.sun.appserv.management.deploy.DeploymentStatus;
50 import com.sun.appserv.management.deploy.DeploymentProgress;
51 import com.sun.appserv.management.base.UploadDownloadMgr;
52
53 import com.sun.appserv.management.util.misc.GSetUtil;
54 import com.sun.appserv.management.util.jmx.NotificationBuilder;
55
56 import com.sun.enterprise.management.support.AMXNonConfigImplBase;
57
58
59 /**
60     Implementation note--the design of this class is unnecessarily
61     complicated due to the inclusion of certain polling-style methods
62     such as takeNotifications() and getFinalDeploymentStatus(). If this
63     aspect of the API can be eliminated, the implemention of this class
64     as well as DeployThread can be simplified. This limitation was
65     driven by the issue of not having Notification support in the http
66     connector used by the deployment team.
67  */

68 public final class DeploymentMgrImpl extends AMXNonConfigImplBase
69     implements Utility, Singleton, DeploymentMgr
70 {
71     /**
72         A Map keyed by deployID to values of DeployThread
73      */

74     private final Map JavaDoc<Object JavaDoc,DeployThread> mDeployThreads;
75     
76         public void
77     remove( final String JavaDoc name )
78     {
79         throw new RuntimeException JavaDoc( "not applicable" );
80     }
81     
82     private final UniqueIDGenerator mDeployIDs;
83     
84     private long mDeploymentCompletedNotificationSequenceNumber;
85     
86         public
87     DeploymentMgrImpl( )
88     {
89         mDeployThreads = Collections.synchronizedMap( new HashMap JavaDoc<Object JavaDoc,DeployThread>() );
90         mDeployIDs = new UniqueIDGenerator( "deploy:" );
91         
92         mDeploymentCompletedNotificationSequenceNumber = 0;
93     }
94     
95     private final Set JavaDoc<String JavaDoc> NOTIF_TYPES = GSetUtil.newUnmodifiableStringSet(
96             DEPLOYMENT_STARTED_NOTIFICATION_TYPE,
97             DEPLOYMENT_ABORTED_NOTIFICATION_TYPE,
98             DEPLOYMENT_PROGRESS_NOTIFICATION_TYPE,
99             DEPLOYMENT_COMPLETED_NOTIFICATION_TYPE );
100     
101         protected Set JavaDoc<String JavaDoc>
102     getNotificationTypes( Set JavaDoc<String JavaDoc> existing )
103     {
104         existing.addAll( NOTIF_TYPES );
105         return existing;
106     }
107
108
109     private static final long SECOND_MILLIS = 60 * 1000;
110     private static final long MINUTE_MILLIS = 60 * SECOND_MILLIS;
111     private static final long DEPLOY_KEEP_ALIVE_MILLIS = 15 * MINUTE_MILLIS;
112     
113     
114         private DeployThread
115     removeDeployThread( final Object JavaDoc deployID)
116     {
117         trace( "\n###Removing deploy thread: " + deployID );
118         return( (DeployThread)mDeployThreads.remove( deployID ) );
119     }
120     
121     /**
122         Cleanup any threads that have been done for a proscribed
123         amount of time given by UPLOAD_KEEP_ALIVE_MILLIS. We don't want to clean them
124         up immediately because the client should have a reasonable chance to
125         get the status after completion.
126      */

127         private final void
128     staleDeployCheck()
129     {
130         final Set JavaDoc<Object JavaDoc> keySet = mDeployThreads.keySet();
131         
132         synchronized( mDeployThreads )
133         {
134             final String JavaDoc[] deployIDs = GSetUtil.toStringArray( keySet );
135         
136             for( final String JavaDoc deployID : deployIDs )
137             {
138                 final DeployThread deployThread = (DeployThread)mDeployThreads.get( deployID );
139                 
140                 if ( deployThread.getMillisSinceDone() > DEPLOY_KEEP_ALIVE_MILLIS )
141                 {
142                     removeDeployThread( deployID );
143                 }
144             }
145         }
146     }
147     
148         private DeployThread
149     addDeployThread(
150         final Object JavaDoc deployID )
151     {
152         final DeploymentCallback callback =
153             new InternalDeploymentCallback( deployID );
154             
155         final DeployThread deployThread =
156             new DeployThread( deployID, callback, null );
157         mDeployThreads.put( deployThread.getID(), deployThread );
158         
159         return( deployThread );
160     }
161
162         public Object JavaDoc
163     initDeploy()
164     {
165         final Object JavaDoc deployID = mDeployIDs.createID();
166         
167         final DeployThread deployThread = addDeployThread( deployID );
168         
169         return( deployID );
170     }
171     
172         public void
173     startDeploy(
174         final Object JavaDoc deployID,
175         final Object JavaDoc uploadID,
176         final Object JavaDoc planUploadID,
177         final Map JavaDoc<String JavaDoc,String JavaDoc> options)
178     {
179         staleDeployCheck();
180         
181         final UploadDownloadMgr mgr = getUploadDownloadMgr();
182         
183         final File JavaDoc deployFile = mgr.takeUpload( uploadID );
184         final File JavaDoc planFile = planUploadID == null ? null : mgr.takeUpload( planUploadID );
185         
186         final DeployThreadParams params =
187             new DeployThreadParams(
188                 getQueryMgr(),
189                 options,
190                 deployFile,
191                 planFile );
192         
193         startDeploy( deployID, params );
194     }
195     
196     public <T1 extends Serializable JavaDoc,T2 extends Serializable JavaDoc> void
197     startDeploy(
198         Object JavaDoc deployID,
199         Map JavaDoc<String JavaDoc,T1> sourceData,
200         Map JavaDoc<String JavaDoc,T2> planData,
201         Map JavaDoc<String JavaDoc,String JavaDoc> options)
202     {
203         final DeploymentSource source =
204             DeploymentSupport.mapToDeploymentSource( sourceData );
205             
206         final DeploymentSource plan = planData == null ?
207             null : DeploymentSupport.mapToDeploymentSource( planData );
208             
209         final DeployThreadParams params =
210             new DeployThreadParams( getQueryMgr(), options, source, plan );
211         
212         startDeploy( deployID, params );
213     }
214     
215         private void
216     startDeploy(
217         final Object JavaDoc deployID,
218         final DeployThreadParams params )
219     {
220         final DeployThread deployThread = getDeployThread( deployID );
221         if ( deployThread == null )
222         {
223             throw new IllegalArgumentException JavaDoc( deployID.toString() );
224         }
225         deployThread.setParams( params );
226         
227         /**
228             Issue a DEPLOYMENT_STARTED_NOTIFICATION_TYPE *before*
229             starting the thread, because a client could receive
230             other Notifications (even "done") before the "started" one.
231          */

232         issueDeploymentStartedNotification( deployID );
233         
234         deployThread.start();
235     }
236
237         private DeployThread
238     getDeployThread( Object JavaDoc deployID )
239     {
240         final DeployThread deployThread = (DeployThread)mDeployThreads.get( deployID );
241         if ( deployThread == null )
242         {
243             final IllegalArgumentException JavaDoc e = new IllegalArgumentException JavaDoc( "" + deployID );
244             
245             e.printStackTrace();
246             throw e;
247         }
248         return( deployThread );
249     }
250     
251         private boolean
252     notifsAreDone( final Notification JavaDoc[] notifs )
253     {
254         boolean done = false;
255         
256         for( int i = notifs.length -1; i >= 0; --i )
257         {
258             final String JavaDoc notifType = notifs[ notifs.length - 1].getType();
259         
260             if ( notifType.equals( DEPLOYMENT_COMPLETED_NOTIFICATION_TYPE ) ||
261                         notifType.equals( DEPLOYMENT_ABORTED_NOTIFICATION_TYPE ) )
262             {
263                 done = true;
264                 break;
265             }
266         }
267         
268         return( done );
269     }
270     
271         public Notification JavaDoc[]
272     takeNotifications( final Object JavaDoc deployID)
273     {
274         final DeployThread deployThread = getDeployThread( deployID );
275         
276         final Notification JavaDoc[] notifs = deployThread.takeNotifications();
277         
278         return( notifs );
279     }
280     
281         public boolean
282     abortDeploy(final Object JavaDoc deployID)
283     {
284         final DeployThread deployThread = getDeployThread( deployID );
285         
286         final boolean abortedSuccessfully = deployThread.quit();
287         
288         issueDeploymentAbortedNotification( deployID );
289         
290         return( abortedSuccessfully );
291     }
292     
293     
294         private void
295     issueNotification(
296         final Object JavaDoc deployID,
297         final Notification JavaDoc notif )
298     {
299         // send it, for normal callers
300
sendNotification( notif );
301         
302         trace( "\nDeploymentMgrImpl.issueNotification: sent notification for " +
303             deployID + " = " + notif.toString() );
304         
305         // queue it, for pollers
306
final DeployThread deployThread = getDeployThread( deployID );
307         deployThread.queueNotification( notif );
308     }
309     
310     /**
311      */

312         protected void
313     issueDeploymentStartedNotification( final Object JavaDoc deployID )
314     {
315         final NotificationBuilder builder =
316             getNotificationBuilder( DEPLOYMENT_STARTED_NOTIFICATION_TYPE );
317         
318         final Notification JavaDoc notif = builder.buildNew( );
319         builder.putMapData( notif, NOTIF_DEPLOYMENT_ID_KEY, (Serializable JavaDoc)deployID );
320         
321         issueNotification( deployID, notif );
322     }
323     
324     /**
325      */

326         protected void
327     issueDeploymentDoneNotification(
328         final Object JavaDoc deployID,
329         final DeploymentStatus deploymentStatus )
330     {
331         final NotificationBuilder builder =
332             getNotificationBuilder( DEPLOYMENT_COMPLETED_NOTIFICATION_TYPE );
333         
334         final Notification JavaDoc notif = builder.buildNew( );
335         builder.putMapData( notif, NOTIF_DEPLOYMENT_ID_KEY, (Serializable JavaDoc)deployID );
336         builder.putMapData( notif, NOTIF_DEPLOYMENT_COMPLETED_STATUS_KEY, (Serializable JavaDoc)deploymentStatus.asMap() );
337         
338         issueNotification( deployID, notif );
339     }
340     
341     /**
342      */

343         protected void
344     issueDeploymentProgressNotification(
345         final Object JavaDoc deployID,
346         final DeploymentProgress progress)
347     {
348         final NotificationBuilder builder =
349             getNotificationBuilder( DEPLOYMENT_PROGRESS_NOTIFICATION_TYPE );
350         
351         final Notification JavaDoc notif = builder.buildNew( );
352         builder.putMapData( notif, NOTIF_DEPLOYMENT_ID_KEY, (Serializable JavaDoc)deployID );
353         builder.putMapData( notif, NOTIF_DEPLOYMENT_PROGRESS_KEY, (Serializable JavaDoc)progress.asMap() );
354         
355         issueNotification( deployID, notif );
356     }
357     
358     /**
359      */

360         protected void
361     issueDeploymentAbortedNotification( final Object JavaDoc deployID )
362     {
363         final NotificationBuilder builder =
364             getNotificationBuilder( DEPLOYMENT_ABORTED_NOTIFICATION_TYPE );
365         
366         final Notification JavaDoc notif = builder.buildNew( );
367         builder.putMapData( notif, NOTIF_DEPLOYMENT_ID_KEY, (Serializable JavaDoc)deployID );
368         
369         issueNotification( deployID, notif );
370     }
371
372     private final class InternalDeploymentCallback implements DeploymentCallback
373     {
374         final Object JavaDoc mDeployID;
375     
376             public
377         InternalDeploymentCallback( Object JavaDoc deployID )
378         {
379             mDeployID = deployID;
380         }
381         
382             public void
383         deploymentDone(
384             final DeploymentStatus status )
385         {
386             issueDeploymentDoneNotification( mDeployID, status );
387         }
388         
389             public void
390         deploymentProgress(
391             final DeploymentProgress progress )
392         {
393             issueDeploymentProgressNotification( mDeployID, progress );
394         }
395     }
396
397         public Map JavaDoc<String JavaDoc,Serializable JavaDoc>
398     undeploy(
399         final String JavaDoc moduleID,
400         final Map JavaDoc<String JavaDoc,String JavaDoc> optionalParams)
401     {
402         final Undeployer undeployer = new Undeployer( moduleID, optionalParams );
403         final DeploymentStatus undeploymentStatus = undeployer.undeploy();
404         
405         return( undeploymentStatus.asMap() );
406     }
407
408         public Map JavaDoc<String JavaDoc,Serializable JavaDoc>
409     getFinalDeploymentStatus (Object JavaDoc deployID)
410     {
411         final DeployThread deployThread = removeDeployThread( deployID );
412         
413         if ( deployThread == null )
414         {
415             throw new IllegalArgumentException JavaDoc( deployID.toString() );
416         }
417         
418         return( deployThread.getDeploymentStatus().asMap() );
419     }
420
421         private UploadDownloadMgr
422     getUploadDownloadMgr()
423     {
424         return( getDomainRoot().getUploadDownloadMgr() );
425     }
426
427
428         public Object JavaDoc
429     initiateFileUpload( long totalSize )
430         throws IOException JavaDoc
431     {
432         return initiateFileUpload( null, totalSize );
433     }
434     
435         public Object JavaDoc
436     initiateFileUpload( final String JavaDoc name, final long totalSize )
437         throws IOException JavaDoc
438     {
439         debug( "initiateFileUpload(" + name + ", " + totalSize + ")" );
440         return( getUploadDownloadMgr().initiateUpload( name, totalSize ) );
441     }
442
443         public boolean
444     uploadBytes(
445         final Object JavaDoc uploadID,
446         final byte[] bytes)
447         throws IOException JavaDoc
448     {
449         return( getUploadDownloadMgr().uploadBytes( uploadID, bytes ) );
450     }
451     
452     
453         public Object JavaDoc
454     initiateFileDownload(
455         final String JavaDoc moduleID,
456         final String JavaDoc filename)
457         throws IOException JavaDoc
458     {
459         final DownloadFileSource source = new DownloadFileSource( moduleID, filename );
460         final File JavaDoc theFile = source.getDownloadFile( );
461         final boolean deleteWhenDone = source.isTempFile();
462         
463         return( getUploadDownloadMgr().initiateDownload( theFile, deleteWhenDone ) );
464     }
465
466         
467     /**
468         Get the total length the download will be, in bytes.
469         
470         @param downloadID the file download operation id, from initiateFileDownload()
471      */

472         public long
473     getDownloadLength( final Object JavaDoc downloadID )
474     {
475         return( getUploadDownloadMgr().getDownloadLength( downloadID ) );
476     }
477     
478     
479         public byte[]
480     downloadBytes(
481         final Object JavaDoc downloadID,
482         final int requestSize )
483         throws IOException JavaDoc
484     {
485         return( getUploadDownloadMgr().downloadBytes( downloadID, requestSize ) );
486     }
487
488 }
489
490
491
492
493
494
495
496
Popular Tags