KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > helper > DeployHelper


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.helper;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.Serializable JavaDoc;
28
29 import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.HashSet JavaDoc;
33
34 import javax.management.ObjectName JavaDoc;
35 import javax.management.ListenerNotFoundException JavaDoc;
36 import javax.management.NotificationListener JavaDoc;
37
38 import com.sun.appserv.management.DomainRoot;
39 import com.sun.appserv.management.base.Util;
40
41
42 import com.sun.appserv.management.helper.Helper;
43 import com.sun.appserv.management.helper.DeployNotificationListener;
44 import com.sun.appserv.management.helper.Misc;
45
46 import com.sun.appserv.management.deploy.DeploymentMgr;
47 import com.sun.appserv.management.deploy.DeploymentProgress;
48 import com.sun.appserv.management.deploy.DeploymentStatus;
49 import static com.sun.appserv.management.deploy.DeploymentStatus.*;
50 import static com.sun.appserv.management.deploy.DeploymentMgr.*;
51
52 import com.sun.appserv.management.config.DeployedItemRefConfig;
53 import com.sun.appserv.management.config.DeployedItemRefConfigCR;
54 import com.sun.appserv.management.config.StandaloneServerConfig;
55 import com.sun.appserv.management.config.ClusterConfig;
56 import com.sun.appserv.management.config.DomainConfig;
57
58
59
60 /**
61     Helper class for simplifying deployment.
62     @since AppServer 9.0
63  */

64 public final class DeployHelper extends Helper
65 {
66     private DeployNotificationListener mListener;
67     private final DeploymentMgr mDeploymentMgr;
68     private boolean mDidAssociate;
69     
70         public
71     DeployHelper( final DeploymentMgr deploymentMgr )
72     {
73         super( deploymentMgr.getDomainRoot() );
74         
75         mDeploymentMgr = deploymentMgr;
76         mListener = null;
77         mDidAssociate = false;
78     }
79     
80     /**
81         To monitor progress or check on final status,
82         use the listener.
83         @see DeployNotificationListener
84      */

85         public DeployNotificationListener
86     getDeployNotificationListener()
87     {
88         return mListener;
89     }
90     
91         public final static String JavaDoc
92     getDefaultAppName( final String JavaDoc archiveName )
93     {
94         String JavaDoc result = archiveName;
95         
96         final int idx = archiveName.lastIndexOf( "." );
97         if ( idx > 1 )
98         {
99             result = archiveName.substring( 0, idx );
100         }
101         
102         return( result );
103     }
104     
105     /**
106         Deploy the archive.
107         Subsequent progress and status should be obtained
108         from the listener returned from {@link #getDeployNotificationListener}.
109         <p>
110         For most errors, an Exception is not thrown; the returned
111         DeploymentStatus should be checked for errors using
112         {@link DeploymentStatus#getStageStatus}, verifing that
113         {@link DeploymentStatus#STATUS_CODE_SUCCESS} was received.
114         <p>
115         If no targets are specified, then the app is deployed, but not
116         associated with any server or cluster.
117         
118         @param archive the archive to deploy
119         @param deployOptions optional deployment options as defined by {@link DeploymentMgr}
120         @see DeploymentStatus
121         @see DeploymentProgress
122      */

123         public void
124     deploy(
125         final File JavaDoc archive,
126         final Map JavaDoc<String JavaDoc,String JavaDoc> deployOptions )
127         throws IOException JavaDoc
128     {
129         if ( archive == null )
130         {
131             throw new IllegalArgumentException JavaDoc();
132         }
133
134         final Object JavaDoc uploadID = Misc.uploadFile(
135             getDomainRoot().getUploadDownloadMgr(),
136             archive );
137         
138         final Object JavaDoc deployID = mDeploymentMgr.initDeploy( );
139         mListener = new DeployNotificationListener( mDeploymentMgr, deployID );
140         
141         try
142         {
143             final String JavaDoc archiveName = archive.getName();
144             
145             final Map JavaDoc<String JavaDoc,String JavaDoc> actualOptions = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
146             if ( deployOptions != null )
147             {
148                 actualOptions.putAll( deployOptions );
149             }
150             if ( ! actualOptions.containsKey( DEPLOY_OPTION_NAME_KEY ) )
151             {
152                 final String JavaDoc appName = getDefaultAppName( archiveName );
153                 actualOptions.put( DEPLOY_OPTION_NAME_KEY, appName );
154             }
155             
156             mDeploymentMgr.startDeploy( deployID, uploadID, null, actualOptions );
157         }
158         finally
159         {
160             // remove right away; not all failures notify the listener
161
mListener.cleanup();
162         }
163     }
164     
165     /**
166         Wait for deployment to finish, then return status.
167         @return final status
168      */

169         public DeploymentStatus
170     waitTillDone( final long pollMillis )
171     {
172         // sanity check
173
if ( pollMillis > 5 * 1000 )
174         {
175             throw new IllegalArgumentException JavaDoc();
176         }
177         
178         // a more sophisticated solution would be to have
179
// the DeploymentNotificationListener wake us up...
180
while ( ! mListener.isCompleted() )
181         {
182             Util.sleep( pollMillis );
183         }
184         
185         final DeploymentStatus status = mListener.getDeploymentStatus();
186         
187         return status;
188     }
189     
190     /**
191         Get a Set of all {@link StandaloneServerConfig} and
192         {@link ClusterConfig} corresponding to the target names.
193         @param domainRoot
194         @param names names (eg getName()) of {@link StandaloneServerConfig} or {@link ClusterConfig}
195      */

196         public static Set JavaDoc<DeployedItemRefConfigCR>
197     getTargetProxies(
198         final DomainRoot domainRoot,
199         final String JavaDoc[] names )
200     {
201         final DomainConfig domainConfig = domainRoot.getDomainConfig();
202         
203         final Set JavaDoc<DeployedItemRefConfigCR> result = new HashSet JavaDoc<DeployedItemRefConfigCR>();
204         
205         final Map JavaDoc<String JavaDoc,StandaloneServerConfig> serverConfigs =
206             domainConfig.getStandaloneServerConfigMap();
207             
208         final Map JavaDoc<String JavaDoc,ClusterConfig> clusterConfigs =
209             domainConfig.getClusterConfigMap();
210             
211         for( final String JavaDoc name : names )
212         {
213             if ( serverConfigs.containsKey( name ) )
214             {
215                 result.add( serverConfigs.get( name ) );
216             }
217             else if ( clusterConfigs.containsKey( name ) )
218             {
219                 result.add( clusterConfigs.get( name ) );
220             }
221         }
222         
223         return result;
224     }
225     
226     /**
227         Associate the specified targets with the deployed application by
228         creating {@link DeployedItemRefConfig} configuration within the targets.
229         Call only after deployment has finished, and call it only once.
230         
231         @param targets names (eg getName()) of {@link StandaloneServerConfig} or {@link ClusterConfig}
232         @param refOptions options to be passed when creating
233                         {@link DeployedItemRefConfig} (may be null)
234      */

235         public Set JavaDoc<DeployedItemRefConfig>
236     createReferences(
237         final String JavaDoc[] targets,
238         final Map JavaDoc<String JavaDoc,String JavaDoc> refOptions )
239     {
240         if ( targets == null || targets.length == 0 )
241         {
242             throw new IllegalArgumentException JavaDoc();
243         }
244
245         if ( mListener == null || ! mListener.isCompleted() )
246         {
247             throw new IllegalStateException JavaDoc();
248         }
249         
250         final DeploymentStatus status = mListener.getDeploymentStatus();
251         
252         if ( status == null || status.getStageStatus() == STATUS_CODE_FAILURE )
253         {
254             throw new IllegalStateException JavaDoc();
255         }
256         
257         if ( mDidAssociate )
258         {
259             throw new IllegalStateException JavaDoc();
260         }
261         
262         // OK, go ahead
263
final Map JavaDoc<String JavaDoc,Serializable JavaDoc> additionalStatus = status.getAdditionalStatus();
264         final String JavaDoc moduleID = (String JavaDoc)additionalStatus.get( MODULE_ID_KEY );
265     
266         final Set JavaDoc<DeployedItemRefConfig> refs = new HashSet JavaDoc<DeployedItemRefConfig>();
267         final Set JavaDoc<DeployedItemRefConfigCR> proxies = getTargetProxies( getDomainRoot(), targets );
268         for( final DeployedItemRefConfigCR cr : proxies )
269         {
270             final DeployedItemRefConfig ref =
271                 cr.createDeployedItemRefConfig( moduleID, refOptions );
272             refs.add( ref );
273         }
274         
275         return refs;
276     }
277     
278     /**
279         Deploy the archive and associate it with all specified targets.
280         Calls {@link #deploy},
281         {@link #waitTillDone}, and
282         {@link #createReferences}.
283         
284         @param archive the archive to deploy
285         @param deployOptions optional deployment options as defined by {@link DeploymentMgr}
286         @param targets names (eg getName()) of
287                     {@link StandaloneServerConfig} or {@link ClusterConfig}
288         @param refOptions options for creating references, see {@link DeployedItemRefConfigCR}
289         @return final status
290      */

291         public DeploymentStatus
292     deploy(
293         final File JavaDoc archive,
294         final Map JavaDoc<String JavaDoc,String JavaDoc> deployOptions,
295         final String JavaDoc[] targets,
296         final Map JavaDoc<String JavaDoc,String JavaDoc> refOptions )
297         throws IOException JavaDoc
298     {
299         deploy( archive, deployOptions );
300         
301         final DeploymentStatus status = waitTillDone( 50 );
302         
303         if ( targets != null && targets.length != 0 )
304         {
305             createReferences( targets, refOptions );
306         }
307         
308         return status;
309     }
310     
311     /**
312         Perform default deployment. Calls deploy( archive, null, targets, null );
313         @param archive
314         @param targets
315         @return final status
316      */

317         public DeploymentStatus
318     deploy(
319         final File JavaDoc archive,
320         final String JavaDoc[] targets )
321         throws IOException JavaDoc
322     {
323         return deploy( archive, null, targets, null );
324     }
325 }
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
Popular Tags