KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > client > DeploymentFacilityImpl


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
24 package com.sun.enterprise.deployment.client;
25
26 import com.sun.appserv.management.base.XTypes;
27 import com.sun.appserv.management.client.AppserverConnectionSource;
28 import com.sun.appserv.management.client.ConnectionSource;
29 import com.sun.appserv.management.client.ProxyFactory;
30 import com.sun.appserv.management.client.TLSParams;
31 import com.sun.appserv.management.config.*;
32 import com.sun.appserv.management.config.DomainConfig;
33 import com.sun.appserv.management.deploy.DeploymentMgr;
34 import com.sun.appserv.management.util.misc.SetUtil;
35 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration;
36 import com.sun.enterprise.deployapi.ProgressObjectImpl;
37 import com.sun.enterprise.deployapi.SunTarget;
38 import com.sun.enterprise.deployapi.SunTargetModuleID;
39 import com.sun.enterprise.deployment.backend.DeploymentStatus;
40 import com.sun.enterprise.deployment.client.JESProgressObject;
41 import com.sun.enterprise.deployment.deploy.shared.Archive;
42 import com.sun.enterprise.deployment.util.DeploymentProperties;
43 import com.sun.enterprise.deployment.util.DOLUtils;
44 import com.sun.enterprise.util.i18n.StringManager;
45
46 import java.io.*;
47 import java.net.URL JavaDoc;
48 import java.util.*;
49
50 import javax.enterprise.deploy.shared.CommandType JavaDoc;
51 import javax.enterprise.deploy.spi.Target JavaDoc;
52 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
53 import javax.net.ssl.X509TrustManager;
54
55 /**
56  * This interface implements basic deployment related facilities
57  * such as deploying any j2ee modules on a Domain Admin Server
58  * or target servers as well as retrieving non portable artifacts
59  * for successful runs in a client mode configuration.
60  *
61  */

62 public class DeploymentFacilityImpl implements DeploymentFacility {
63
64     protected ConnectionSource dasConnection = null;
65     protected ServerConnectionIdentifier serverId = null;
66     protected SunTarget domain = null;
67     protected Boolean JavaDoc localConnection = Boolean.FALSE;
68
69     private static final String JavaDoc DAS = "server";
70     private static StringManager localStrings = StringManager.getManager(DeploymentFacilityImpl.class);
71     
72     public DeploymentFacilityImpl() {}
73     
74     /**
75      * Connects to a particular instance of the domain adminstration
76      * server using the provided connection information
77      */

78     public boolean connect(ServerConnectionIdentifier targetDAS) {
79         try {
80             TLSParams tlsParams = null;
81             if (targetDAS.isSecure()) {
82                 X509TrustManager trustManager =
83                     (X509TrustManager)targetDAS.getConnectionEnvironment().
84                     get(DefaultConfiguration.TRUST_MANAGER_PROPERTY_NAME);
85                 tlsParams = new TLSParams(trustManager, null);
86             }
87
88             dasConnection = new AppserverConnectionSource(
89                 AppserverConnectionSource.PROTOCOL_HTTP,
90                 targetDAS.getHostName(), targetDAS.getHostPort(),
91                 targetDAS.getUserName(), targetDAS.getPassword(),
92                 tlsParams, null);
93         } catch (IllegalArgumentException JavaDoc illEx) {
94             return false;
95         }
96         serverId = targetDAS;
97         domain = new SunTarget(targetDAS);
98         domain.setConnectionSource(dasConnection);
99         domain.setTargetType(TargetType.DOMAIN);
100         return true;
101     }
102     
103     /**
104      * @return true if we are connected to a domain adminstration
105      * server
106      */

107     public boolean isConnected() {
108         if(dasConnection == null) {
109             return false;
110         }
111         return true;
112     }
113     
114     /**
115      * Disconnects from a domain administration server and releases
116      * all associated resouces.
117      */

118     public boolean disconnect() {
119         dasConnection = null;
120         return true;
121     }
122         
123     /**
124      * Initiates a deployment operation on the server, using a source
125      * archive abstraction and an optional deployment plan if the
126      * server specific information is not embedded in the source
127      * archive. The deploymentOptions is a key-value pair map of
128      * deployment options for this operations. Once the deployment
129      * is successful, the targets server instances
130      *
131      * @param source is the j2ee module abstraction (with or without
132      * the server specific artifacts).
133      * @param deploymenPlan is the optional deployment plan is the source
134      * archive is portable.
135      * @param the deployment options
136      * @return a JESProgressObject to receive deployment events.
137      */

138     public JESProgressObject deploy(Target[] targets, Archive source, Archive deploymentPlan, Map deploymentOptions) {
139         if(!isConnected()) {
140             throw new IllegalStateException JavaDoc(localStrings.getString("enterprise.deployment.client.disconnected_state"));
141         }
142         
143         SunTarget[] targetList = getSunTargets(targets);
144         ProgressObjectImpl progressObject = new DeployAction(targetList);
145         Object JavaDoc args[] = new Object JavaDoc[8];
146         args[0] = dasConnection;
147         args[1] = source;
148         args[2] = deploymentPlan;
149         args[3] = (deploymentOptions == null) ? new HashMap() : DeploymentProperties.propsToMap((Properties)deploymentOptions);
150         args[4] = targetList;
151         args[5] = domain;
152         args[6] = localConnection;
153         args[7] = serverId;
154         progressObject.setCommand(CommandType.DISTRIBUTE, args);
155         Thread JavaDoc newThread = new Thread JavaDoc(progressObject);
156         newThread.start();
157         return progressObject;
158     }
159     
160     /**
161      * Initiates a undeployment operation on the server
162      * @param module ID for the component to undeploy
163      * @return a JESProgress to receive undeployment events
164      */

165     // FIXME - this should go once admin-cli changes it code
166
public JESProgressObject undeploy(Target[] targets, String JavaDoc moduleID) {
167         return(undeploy(targets, moduleID, null));
168     }
169     
170     public JESProgressObject undeploy(Target[] targets, String JavaDoc moduleID, Map options) {
171         if(!isConnected()) {
172             throw new IllegalStateException JavaDoc(localStrings.getString("enterprise.deployment.client.disconnected_state"));
173         }
174         SunTarget[] targetList = getSunTargets(targets);
175         ProgressObjectImpl progressObject = new UndeployAction(targetList);
176         Object JavaDoc args[] = new Object JavaDoc[6];
177         args[0] = dasConnection;
178         args[1] = moduleID;
179         args[2] = (options == null) ? new HashMap() : DeploymentProperties.propsToMap((Properties)options);
180         args[3] = targetList;
181         args[4] = domain;
182         args[5] = localConnection;
183         progressObject.setCommand(CommandType.UNDEPLOY, args);
184         Thread JavaDoc newThread = new Thread JavaDoc(progressObject);
185         newThread.start();
186         return progressObject;
187     }
188     
189     /**
190      * Enables a deployed component on the provided list of targets.
191      */

192     public JESProgressObject enable(Target[] targets, String JavaDoc moduleID) {
193         return(changeState(targets, moduleID, CommandType.START));
194     }
195
196     /**
197      * Disables a deployed component on the provided list of targets
198      */

199     public JESProgressObject disable(Target[] targets, String JavaDoc moduleID) {
200         return(changeState(targets, moduleID, CommandType.STOP));
201     }
202
203     private JESProgressObject changeState(Target[] targets, String JavaDoc moduleID, CommandType JavaDoc cmd) {
204         if(!isConnected()) {
205             throw new IllegalStateException JavaDoc(localStrings.getString("enterprise.deployment.client.disconnected_state"));
206         }
207         SunTarget[] targetList = getSunTargets(targets);
208         ProgressObjectImpl progressObject = new ChangeStateAction(targetList);
209         Object JavaDoc args[] = new Object JavaDoc[5];
210         args[0] = dasConnection;
211         args[1] = targetList;
212         args[2] = moduleID;
213         args[3] = cmd;
214         args[4] = domain;
215         progressObject.setCommand(cmd, args);
216         Thread JavaDoc newThread = new Thread JavaDoc(progressObject);
217         newThread.start();
218         return progressObject;
219     }
220     
221     /**
222      * Add an application ref on the selected targets
223      */

224     public JESProgressObject createAppRef(Target[] targets, String JavaDoc moduleID, Map options) {
225         return(doApplicationReferenceAction(targets, moduleID, options, CommandType.DISTRIBUTE));
226     }
227
228     /**
229      * remove the application ref for the provided list of targets.
230      */

231     public JESProgressObject deleteAppRef(Target[] targets, String JavaDoc moduleID, Map options) {
232         return(doApplicationReferenceAction(targets, moduleID, options, CommandType.UNDEPLOY));
233     }
234
235     private JESProgressObject doApplicationReferenceAction(Target[] targets, String JavaDoc moduleID, Map options, CommandType JavaDoc cmd) {
236         if(!isConnected()) {
237             throw new IllegalStateException JavaDoc(localStrings.getString("enterprise.deployment.client.disconnected_state"));
238         }
239         SunTarget[] targetList = getSunTargets(targets);
240         ProgressObjectImpl progressObject = new ApplicationReferenceAction(targetList);
241         Object JavaDoc args[] = new Object JavaDoc[5];
242         args[0] = dasConnection;
243         args[1] = targetList;
244         args[2] = moduleID;
245         args[3] = cmd;
246         args[4] = (options == null) ? new HashMap() : DeploymentProperties.propsToMap((Properties)options);
247         progressObject.setCommand(cmd, args);
248         Thread JavaDoc newThread = new Thread JavaDoc(progressObject);
249         newThread.start();
250         return progressObject;
251     }
252
253     /**
254      * list all application refs that are present in the provided list of targets
255      */

256     public TargetModuleID[] listAppRefs(String JavaDoc[] targets) throws IOException {
257         if(!isConnected()) {
258             throw new IllegalStateException JavaDoc(localStrings.getString("enterprise.deployment.client.disconnected_state"));
259         }
260         Vector tmpVector = new Vector();
261         DomainConfig domainCfg = ProxyFactory.getInstance(dasConnection).getDomainRoot().getDomainConfig();
262         Map serverProxies = domainCfg.getStandaloneServerConfigMap();
263         Map clusterProxies = domainCfg.getClusterConfigMap();
264         Map clusteredServerProxies = domainCfg.getClusteredServerConfigMap();
265         for(int i=0; i<targets.length; i++) {
266             Set proxySet = null;
267             if(serverProxies.get(targets[i]) != null) {
268                 StandaloneServerConfig tgtProxy =
269                     (StandaloneServerConfig)domainCfg.getContainee(
270                         XTypes.STANDALONE_SERVER_CONFIG, targets[i]);
271                 proxySet = tgtProxy.getContaineeSet(XTypes.DEPLOYED_ITEM_REF_CONFIG);
272             } else if(clusterProxies.get(targets[i]) != null) {
273                 ClusterConfig tgtProxy =
274                     (ClusterConfig)domainCfg.getContainee(
275                         XTypes.CLUSTER_CONFIG, targets[i]);
276                 proxySet = tgtProxy.getContaineeSet(XTypes.DEPLOYED_ITEM_REF_CONFIG);
277             } else if(clusteredServerProxies.get(targets[i]) != null) {
278                 ClusteredServerConfig tgtProxy =
279                     (ClusteredServerConfig)domainCfg.getContainee(
280                         XTypes.CLUSTERED_SERVER_CONFIG, targets[i]);
281                 proxySet = tgtProxy.getContaineeSet(XTypes.DEPLOYED_ITEM_REF_CONFIG);
282             } else if(TargetType.DOMAIN.equals(targets[i])) {
283                 StandaloneServerConfig tgtProxy =
284                     (StandaloneServerConfig)domainCfg.getContainee(
285                         XTypes.STANDALONE_SERVER_CONFIG, DAS);
286                 proxySet = tgtProxy.getContaineeSet(XTypes.DEPLOYED_ITEM_REF_CONFIG);
287             } else {
288                 return null;
289             }
290             Object JavaDoc[] appRefs = proxySet.toArray();
291             for(int k=0; k<appRefs.length; k++) {
292                 SunTarget aTarget = new SunTarget(serverId);
293                 aTarget.setAppServerInstance(targets[i]);
294                 aTarget.setConnectionSource(dasConnection);
295                 DeployedItemRefConfig item = (DeployedItemRefConfig) appRefs[k];
296                 SunTargetModuleID tgtId = new SunTargetModuleID(item.getRef(), aTarget);
297                 tmpVector.add(tgtId);
298             }
299         }
300         SunTargetModuleID[] result = new SunTargetModuleID[tmpVector.size()];
301         return (TargetModuleID[]) tmpVector.toArray(result);
302     }
303
304     /**
305      * Create SunTarget[] from given Target[]
306      */

307     private SunTarget[] getSunTargets(Target[] givenTargets) throws IllegalArgumentException JavaDoc {
308         SunTarget[] result = new SunTarget[givenTargets.length];
309         for(int i=0; i<givenTargets.length; i++) {
310             if(givenTargets[i] instanceof SunTarget) {
311                 result[i] = new SunTarget((SunTarget)givenTargets[i]);
312             } else {
313                 throw new IllegalArgumentException JavaDoc(localStrings.getString("enterprise.deployment.client.notASunTarget",
314                                                     givenTargets[i].getClass().getName()));
315             }
316         }
317         return result;
318     }
319     
320     /**
321      * Downloads a particular file from the server repository.
322      * The filePath is a relative path from the root directory of the
323      * deployed component identified with the moduleID parameter.
324      * The resulting downloaded file should be placed in the
325      * location directory keeping the relative path constraint.
326      * Note that the current implementation only supports the download
327      * of the appclient jar file.
328      *
329      * @param location is the root directory where to place the
330      * downloaded file
331      * @param moduleID is the moduleID of the deployed component
332      * to download the file from
333      * @param moduleURI is the relative path to the file in the repository
334      * @return the downloaded local file absolute path.
335      */

336     public String JavaDoc downloadFile(File location, String JavaDoc moduleID, String JavaDoc moduleURI)
337             throws IOException {
338         if(!isConnected()) {
339             throw new IllegalStateException JavaDoc(
340                 localStrings.getString("enterprise.deployment.client.disconnected_state"));
341         }
342
343         return DeploymentClientUtils.downloadClientStubs(
344                     moduleID, location.getAbsolutePath(), dasConnection);
345     }
346     
347     /**
348      * Wait for a Progress object to be in a completed state
349      * (sucessful or failed) and return the DeploymentStatus for
350      * this progress object.
351      * @param the progress object to wait for completion
352      * @return the deployment status
353      */

354     public DeploymentStatus waitFor(JESProgressObject po) {
355         DeploymentStatus status = null;
356         do {
357             try {
358                 Thread.currentThread().sleep(100);
359             } catch (InterruptedException JavaDoc ie) {
360                 // Exception swallowed deliberately
361
}
362             status = po.getCompletedStatus();
363         } while(status == null);
364         return status;
365     }
366      
367     public Target[] createTargets(String JavaDoc[] targets ) {
368         if(!isConnected()) {
369             throw new IllegalStateException JavaDoc(localStrings.getString("enterprise.deployment.client.disconnected_state"));
370         }
371         Target[] targetsArray = new Target[targets.length];
372         Map serverProxies = null;
373         Map clusterProxies = null;
374
375         try {
376             // parse through given targets
377
for(int i=0; i<targets.length; i++) {
378                 
379                 // if this is "domain" add a domain target
380
if(TargetType.DOMAIN.equals(targets[i])) {
381                     // Add a domain target
382
SunTarget dom = new SunTarget(serverId);
383                     dom.setAppServerInstance(TargetType.DOMAIN);
384                     dom.setConnectionSource(dasConnection);
385                     dom.setTargetType(TargetType.DOMAIN);
386                     targetsArray[i] = dom;
387                     continue;
388                 }
389                 // if this is "server" add a server target
390
if(DAS.equals(targets[i])) {
391                     // Add a target for default server
392
SunTarget serv = new SunTarget(serverId);
393                     serv.setAppServerInstance(DAS);
394                     serv.setConnectionSource(dasConnection);
395                     serv.setTargetType(TargetType.STAND_ALONE_SERVER);
396                     targetsArray[i] = serv;
397                     continue;
398                 }
399                 // for PE, it will not come here at all; go ahead and get proxies and server/cluster keysets from the proxies
400
if(serverProxies == null || clusterProxies == null) {
401                     DomainConfig domainCfg = ProxyFactory.getInstance(dasConnection).getDomainRoot().getDomainConfig();
402                     serverProxies = domainCfg.getStandaloneServerConfigMap();
403                     clusterProxies = domainCfg.getClusterConfigMap();
404                 }
405                 // check if ctarget is a stand alone server
406
if(serverProxies.get(targets[i]) != null) {
407                     SunTarget aTarget = new SunTarget(serverId);
408                     aTarget.setAppServerInstance(targets[i]);
409                     aTarget.setConnectionSource(dasConnection);
410                     aTarget.setTargetType(TargetType.STAND_ALONE_SERVER);
411                     targetsArray[i] = aTarget;
412                     continue;
413                 }
414                 // check if ctarget is a cluster
415
if(clusterProxies.get(targets[i]) != null) {
416                     SunTarget aTarget = new SunTarget(serverId);
417                     aTarget.setAppServerInstance(targets[i]);
418                     aTarget.setConnectionSource(dasConnection);
419                     aTarget.setTargetType(TargetType.CLUSTER);
420                     targetsArray[i] = aTarget;
421                     continue;
422                 }
423                 // if we are here, it means given target does not exist at all - return null
424
return null;
425             }
426         } catch (Throwable JavaDoc ex) {
427             return null;
428         }
429         return targetsArray;
430     }
431 }
432
Popular Tags