KickJava   Java API By Example, From Geeks To Geeks.

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


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.Set JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Collections JavaDoc;
32
33 import java.io.IOException JavaDoc;
34 import java.io.File JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.FileInputStream JavaDoc;
37 import java.io.FileOutputStream JavaDoc;
38 import java.io.Serializable JavaDoc;
39
40 import javax.management.ObjectName JavaDoc;
41 import javax.management.AttributeList JavaDoc;
42 import javax.management.Notification JavaDoc;
43 import javax.management.NotificationListener JavaDoc;
44 import javax.management.MBeanServerConnection JavaDoc;
45 import javax.management.NotCompliantMBeanException JavaDoc;
46 import javax.management.ListenerNotFoundException JavaDoc;
47
48 import com.sun.appserv.management.deploy.DeploymentMgr;
49 import com.sun.appserv.management.deploy.DeploymentMgr;
50 import com.sun.appserv.management.deploy.DeploymentProgress;
51 import com.sun.appserv.management.deploy.DeploymentStatus;
52 import com.sun.appserv.management.deploy.DeploymentSupport;
53
54 import com.sun.appserv.management.base.XTypes;
55 import com.sun.appserv.management.config.StandaloneServerConfig;
56 import com.sun.appserv.management.config.DeployedItemRefConfig;
57 import com.sun.appserv.management.config.DeployedItemRefConfigCR;
58
59
60 import com.sun.appserv.management.util.misc.ExceptionUtil;
61 import com.sun.appserv.management.util.misc.MapUtil;
62 import com.sun.appserv.management.util.misc.GSetUtil;
63 import com.sun.appserv.management.util.misc.TypeCast;
64
65
66 import com.sun.enterprise.management.AMXTestBase;
67 import com.sun.enterprise.management.Capabilities;
68
69 import com.sun.enterprise.management.PropertyKeys;
70 import com.sun.enterprise.management.Capabilities;
71
72
73 /**
74  */

75 public final class DeploymentMgrTest extends AMXTestBase
76 {
77         public
78     DeploymentMgrTest( )
79         throws IOException JavaDoc
80     {
81     }
82     
83         public static Capabilities
84     getCapabilities()
85     {
86         return getOfflineCapableCapabilities( false );
87     }
88     
89     
90     private static final class MyNotificationListener
91         implements NotificationListener JavaDoc
92     {
93         private final Object JavaDoc mDeployID;
94         private boolean mIsCompleted;
95         
96             public
97         MyNotificationListener( final Object JavaDoc deployID )
98         {
99             mDeployID = deployID;
100             mIsCompleted = false;
101         }
102         
103             public boolean
104         isCompleted()
105         {
106             return( mIsCompleted );
107         }
108         
109             public synchronized void
110         handleNotification(
111             final Notification JavaDoc notif,
112             final Object JavaDoc handback)
113         {
114             try
115             {
116                 realHandleNotification( notif, handback );
117             }
118             catch( Exception JavaDoc e )
119             {
120                 e.printStackTrace();
121             }
122         }
123         
124             public void
125         realHandleNotification(
126             final Notification JavaDoc notif,
127             final Object JavaDoc handback)
128         {
129             final String JavaDoc type = notif.getType();
130             final Map JavaDoc<String JavaDoc,Serializable JavaDoc> m = TypeCast.asMap( notif.getUserData() );
131             final Object JavaDoc deployID = m.get( DeploymentMgr.NOTIF_DEPLOYMENT_ID_KEY );
132             
133             //trace( deployID + ": received " + type );
134
if ( deployID.equals( mDeployID ) )
135             {
136                 if ( type.equals( DeploymentMgr.DEPLOYMENT_STARTED_NOTIFICATION_TYPE ) )
137                 {
138                 }
139                 else if ( deployID.equals( mDeployID ) )
140                 {
141                     assert( deployID != null && deployID.equals( mDeployID ) );
142                     //trace( "DeploymentMgrTest.handleNotification: " + deployID + ": " + type );
143

144                     if ( type.equals( DeploymentMgr.DEPLOYMENT_COMPLETED_NOTIFICATION_TYPE ) )
145                     {
146                         final Map JavaDoc<String JavaDoc,Serializable JavaDoc> statusData = TypeCast.asMap(
147                             m.get( DeploymentMgr.NOTIF_DEPLOYMENT_COMPLETED_STATUS_KEY ) );
148                         
149                         final DeploymentStatus status =
150                             DeploymentSupport.mapToDeploymentStatus( statusData );
151                         
152                         assert( mDeployID.equals( deployID ) ) :
153                             "deploy ID mismatch: " + mDeployID + " != " + deployID;
154                         
155                         mIsCompleted = true;
156                     }
157                     else if ( type.equals( DeploymentMgr.DEPLOYMENT_PROGRESS_NOTIFICATION_TYPE ) )
158                     {
159                         final Map JavaDoc<String JavaDoc,Serializable JavaDoc> progressData = TypeCast.asMap(
160                             m.get( DeploymentMgr.NOTIF_DEPLOYMENT_PROGRESS_KEY ) );
161                         TypeCast.checkMap( progressData, String JavaDoc.class, Serializable JavaDoc.class);
162                         
163                         final DeploymentProgress progress =
164                             DeploymentSupport.mapToDeploymentProgress( progressData );
165                         
166                         //trace( deployID + ": " + progress.getProgressPercent() + "%" );
167
}
168                     else
169                     {
170                         assert( false ) : "Unknown deployment notification type: " + type;
171                     }
172                 }
173             }
174         }
175     }
176     
177     
178         public DeploymentMgr
179     getDeploymentMgr()
180     {
181         return( getDomainRoot().getDeploymentMgr() );
182     }
183     
184     
185         public Object JavaDoc
186     uploadFile( final String JavaDoc name, final InputStream JavaDoc is )
187         throws IOException JavaDoc
188     {
189         final DeploymentMgr mgr = getDeploymentMgr();
190         assert( mgr != null );
191         //mgr.setTrace( true );
192

193         final int totalSize = is.available();
194         final int chunkSize = 1 + 32 * 1024; // a screwball size
195

196         final Object JavaDoc uploadID = mgr.initiateFileUpload( name, totalSize );
197         int remaining = totalSize;
198         boolean done = false;
199         while ( remaining != 0 )
200         {
201             final int actual = remaining < chunkSize ? remaining : chunkSize;
202             
203             final byte[] bytes = new byte[ actual ];
204             is.read( bytes );
205             done = mgr.uploadBytes( uploadID, bytes );
206             remaining -= actual;
207         }
208         assert( done );
209         
210         return( uploadID );
211     }
212     
213         public Object JavaDoc
214     uploadFile( final File JavaDoc theFile )
215         throws IOException JavaDoc
216     {
217         final FileInputStream JavaDoc is = new FileInputStream JavaDoc( theFile );
218         Object JavaDoc id = null;
219         
220         try
221         {
222             id = uploadFile( theFile.getName(), is );
223         }
224         finally
225         {
226             is.close();
227         }
228         return( id );
229     }
230     
231         public void
232     testDownloadFile(
233         final String JavaDoc moduleID,
234         final String JavaDoc filename,
235         final int chunkSize )
236         throws IOException JavaDoc
237     {
238         final DeploymentMgr mgr =
239                 getDomainRoot().getDeploymentMgr();
240         
241         final Object JavaDoc id = mgr.initiateFileDownload( moduleID, filename );
242         
243         //trace( "downloading for: " + id );
244

245         final int actualChunkSize = chunkSize < mgr.MAX_DOWNLOAD_CHUNK_SIZE ?
246             chunkSize : mgr.MAX_DOWNLOAD_CHUNK_SIZE;
247             
248         final long length = mgr.getDownloadLength( id );
249         long doneSoFar = 0;
250         while ( doneSoFar < length )
251         {
252             final byte[] bytes = mgr.downloadBytes( id, actualChunkSize );
253             
254             doneSoFar += bytes.length;
255         }
256     }
257     
258     private final int K = 1024;
259     private final int MEGABYTE = K * K;
260     
261         public void
262     XXXXtestDownloadFile1()
263         throws IOException JavaDoc
264     {
265         printVerbose( "testDownloadFile1" );
266         testDownloadFile( "moduleID", "filename", MEGABYTE );
267         printVerbose( "testDownloadFile1 DONE" );
268     }
269      
270         public void
271     testUndeployNonExistentModule()
272     {
273         printVerbose( "testUndeployNonExistentModule" );
274         try
275         {
276             getDeploymentMgr().undeploy( "does_not_exist", null );
277         }
278         catch( Exception JavaDoc e )
279         {
280             // good!
281
final Throwable JavaDoc t = ExceptionUtil.getRootCause( e );
282             assert( t instanceof IllegalArgumentException JavaDoc );
283         }
284         printVerbose( "testUndeployNonExistentModule DONE" );
285     }
286     
287         private DeploymentStatus
288     deploy( final File JavaDoc theFile )
289         throws IOException JavaDoc
290     {
291         //printVerbose( "Uploading: " + quote( theFile ) );
292
final Object JavaDoc uploadID = uploadFile( theFile );
293         final DeploymentMgr mgr = getDeploymentMgr();
294         
295         final Object JavaDoc deployID = mgr.initDeploy( );
296         assert( deployID instanceof String JavaDoc );
297         final MyNotificationListener myListener = new MyNotificationListener( deployID);
298         
299         DeploymentStatus status = null;
300         mgr.addNotificationListener( myListener, null, null );
301         try
302         {
303             final Map JavaDoc<String JavaDoc,String JavaDoc> options = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
304             options.put( DeploymentMgr.DEPLOY_OPTION_FORCE_KEY, Boolean.TRUE.toString() );
305             options.put( DeploymentMgr.DEPLOY_OPTION_VERIFY_KEY, Boolean.TRUE.toString() );
306             options.put( DeploymentMgr.DEPLOY_OPTION_DESCRIPTION_KEY, "test deploy" );
307             options.put( DeploymentMgr.DEPLOY_OPTION_AVAILABILITY_ENABLED_KEY, Boolean.FALSE.toString() );
308             options.put( DeploymentMgr.DEPLOY_OPTION_ENABLE_KEY, Boolean.TRUE.toString() );
309             
310             mgr.startDeploy( deployID, uploadID, null, null);
311                     
312             printVerbose( NEWLINE + "Deploying: " + quote( theFile ) +
313                 ", deploy options: " + MapUtil.toString( options, ", ") );
314             while ( ! myListener.isCompleted() )
315             {
316                 try
317                 {
318                     //trace( "testDeployFile: sleeping for: " + deployID);
319
Thread.sleep( 500 );
320                 }
321                 catch( InterruptedException JavaDoc e )
322                 {
323                 }
324             }
325             
326             final Map JavaDoc<String JavaDoc,Serializable JavaDoc> deploymentStatusMap = mgr.getFinalDeploymentStatus( deployID );
327             status = DeploymentSupport.mapToDeploymentStatus( deploymentStatusMap );
328         }
329         finally
330         {
331             try
332             {
333                 mgr.removeNotificationListener( myListener );
334             }
335             catch( Exception JavaDoc e )
336             {
337             }
338         }
339         
340         return( status );
341     }
342     
343         protected void
344     removeAllRefs( final String JavaDoc moduleID )
345     {
346         final Set JavaDoc<DeployedItemRefConfig> refs =
347             getQueryMgr().queryJ2EETypeNameSet( XTypes.DEPLOYED_ITEM_REF_CONFIG, moduleID );
348         
349         for( final DeployedItemRefConfig ref : refs )
350         {
351             final DeployedItemRefConfigCR container = (DeployedItemRefConfigCR)ref.getContainer();
352             
353             container.removeDeployedItemRefConfig( ref.getName() );
354         }
355     }
356     
357         public void
358     undeploy( final String JavaDoc moduleID )
359     {
360         removeAllRefs( moduleID );
361         
362         final DeploymentMgr mgr = getDeploymentMgr();
363         
364         final long start = now();
365         final DeploymentStatus status = DeploymentSupport.mapToDeploymentStatus( mgr.undeploy( moduleID, null ) );
366         assert( status.getStageStatus() == DeploymentStatus.STATUS_CODE_SUCCESS );
367         
368         printElapsed( "undeploy " + moduleID, start );
369     }
370     
371      private static final Set JavaDoc<String JavaDoc> ARCHIVE_SUFFIXES =
372          GSetUtil.newUnmodifiableStringSet(
373             "ear", "war", "rar", "jar"
374          );
375          
376         private void
377     addArchivesInDirectory(
378         final File JavaDoc dir,
379         final List JavaDoc<File JavaDoc> archives )
380     {
381         assert dir.isDirectory();
382         
383         final File JavaDoc[] contents = dir.listFiles();
384         for( final File JavaDoc f : contents )
385         {
386             if ( f.isDirectory() )
387             {
388                 addArchivesInDirectory( f, archives );
389             }
390             else
391             {
392                 final String JavaDoc name = f.getName();
393                 final int idx = name.lastIndexOf( "." );
394                 final String JavaDoc suffix = (idx <= 0 ? "" : name.substring( idx + 1, name.length() )).toLowerCase();
395                 
396                 if ( ARCHIVE_SUFFIXES.contains(suffix ) )
397                 {
398                     archives.add( f );
399                 }
400             }
401         }
402     }
403     
404         public void
405     testDeployUndeployModules( )
406     {
407         final String JavaDoc filesString = getEnvString( PropertyKeys.ARCHIVES_TO_DEPLOY_KEY, "" ).trim();
408         final String JavaDoc[] names = filesString.split( PropertyKeys.ARCHIVES_DELIM );
409         
410         final List JavaDoc<File JavaDoc> archives = new ArrayList JavaDoc<File JavaDoc>();
411         for( int i = 0; i < names.length; ++i )
412         {
413             names[ i ] = names[i].trim();
414             
415             final File JavaDoc f = new File JavaDoc( names[ i ] );
416             if ( ! f.exists() )
417             {
418                 warning( "File " + f + " does not exist" );
419             }
420             if ( f.isDirectory() )
421             {
422                 addArchivesInDirectory( f, archives);
423             }
424             else
425             {
426                 archives.add( f );
427             }
428         }
429         
430         List JavaDoc<String JavaDoc> moduleIDs = deployModules( archives );
431         final List JavaDoc<Exception JavaDoc> results = undeployModules( moduleIDs );
432         
433         // now deploy again and leave in place, for the use of subsequent unit tests
434
moduleIDs = deployModules( archives );
435         
436         // now add references to them in server
437
final StandaloneServerConfig server =
438             getDomainConfig().getStandaloneServerConfigMap().get( "server" );
439         for( final String JavaDoc moduleID : moduleIDs )
440         {
441             server.createDeployedItemRefConfig( moduleID );
442             println( "Added ref to: " + moduleID );
443         }
444     }
445     
446     
447         protected List JavaDoc<Exception JavaDoc>
448     undeployModules( final List JavaDoc<String JavaDoc> moduleIDs )
449     {
450         final List JavaDoc<Exception JavaDoc> results = new ArrayList JavaDoc<Exception JavaDoc>();
451         
452         for ( final String JavaDoc moduleID : moduleIDs )
453         {
454             if ( moduleID != null )
455             {
456                 try
457                 {
458                     undeploy( moduleID );
459                     println( "Undeployed: " + moduleID );
460                     results.add( null );
461                 }
462                 catch( Exception JavaDoc e )
463                 {
464                     warning( "FAILURE undeploying module " + moduleID );
465                     results.add( e );
466                 }
467             }
468         }
469         return results;
470     }
471     
472     /**
473         Test deployment and undeployment of modules specified by PropertyKeys.ARCHIVES_TO_DEPLOY_KEY.
474         @return moduleIDs for deployed items
475      */

476         protected List JavaDoc<String JavaDoc>
477     deployModules( final List JavaDoc<File JavaDoc> files )
478     {
479         final List JavaDoc<String JavaDoc> moduleIDs = new ArrayList JavaDoc<String JavaDoc>();
480         
481         if ( files.size() == 0 )
482         {
483             warning( "testDeployUndeployModules: no modules specified via property " +
484                 PropertyKeys.ARCHIVES_TO_DEPLOY_KEY + ", NO MODULES WILL BE DEPLOYED." );
485         }
486         else
487         {
488             final Set JavaDoc<String JavaDoc> failedSet = new HashSet JavaDoc<String JavaDoc>();
489             final Set JavaDoc<String JavaDoc> successSet = new HashSet JavaDoc<String JavaDoc>();
490             
491             for( final File JavaDoc theFile : files )
492             {
493                 try
494                 {
495                     if ( theFile.exists() )
496                     {
497                         final long start = now();
498                         final DeploymentStatus status = deploy( theFile );
499                         final long elapsed = now() - start;
500                         
501                         String JavaDoc msg = "Deployed: " +
502                             quote( theFile ) + " in " + elapsed + "ms";
503                         if ( getVerbose() )
504                         {
505                             msg = msg + ", DeploymentStatus = " + status;
506                         }
507                         println( msg );
508                             
509                         if ( status.getStageStatus() != DeploymentStatus.STATUS_CODE_SUCCESS )
510                         {
511                             warning( "DeploymentMgrTest.testDeployUndeployModules: expected STATUS_CODE_SUCCESS " +
512                             "for " + quote( theFile.toString() ) +
513                             ", got " + status.getStageStatus() );
514                             failedSet.add( theFile.toString() );
515                             continue;
516                         }
517                         
518                         String JavaDoc moduleID =
519                             (String JavaDoc)status.getAdditionalStatus().get( DeploymentStatus.MODULE_ID_KEY );
520                             
521                         if ( moduleID == null )
522                         {
523                             moduleID =(String JavaDoc) status.getAdditionalStatus().get( "moduleid" );
524                             assert( moduleID != null );
525                             
526                             warning( "WARNING: used 'moduleid' instead of " +
527                                 "DeploymentStatus.MODULE_ID_KEY (" + DeploymentStatus.MODULE_ID_KEY +
528                                 ") as workaround for bug #6218705" );
529                         }
530                         moduleIDs.add( moduleID );
531                         
532                         successSet.add( theFile.toString() );
533                     }
534                     else
535                     {
536                         warning( "testDeployUndeployModules: file " +
537                             quote( theFile.toString() ) + " does not exist." );
538                     }
539                 }
540                 catch( Throwable JavaDoc t )
541                 {
542                     warning( "Error deploying archive: " + quote( theFile.toString() ) );
543                 }
544             }
545             
546             if ( failedSet.size() != 0 )
547             {
548                 failure( "testDeployUndeployModules: failure count = " +
549                     failedSet.size() + " modules: " + toString( failedSet ) );
550             }
551         }
552         return moduleIDs;
553     }
554 }
555
556
557
558
559
560
561
562
563
564
565
566
Popular Tags