KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > commands > UndeployCommand


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.cli.commands;
25
26 import com.sun.enterprise.cli.framework.*;
27
28 import com.sun.enterprise.deployment.util.DeploymentProperties;
29 import com.sun.enterprise.deployment.client.DeploymentFacility;
30 import com.sun.enterprise.deployment.client.ServerConnectionIdentifier;
31 import com.sun.enterprise.deployment.client.DeploymentFacilityFactory;
32 import com.sun.enterprise.deployment.client.JESTarget;
33 import com.sun.enterprise.deployment.client.JESProgressObject;
34 import com.sun.enterprise.deployment.backend.DeploymentStatus;
35 import javax.enterprise.deploy.spi.Target JavaDoc;
36 import com.sun.enterprise.server.Constants;
37
38 // jdk imports
39
import java.io.*;
40 import java.util.Iterator JavaDoc;
41 import java.util.Map JavaDoc;
42 import java.util.Properties JavaDoc;
43
44
45 /**
46  * Undeploy command
47  * @version $Revision: 1.4 $
48  */

49 public class UndeployCommand extends S1ASCommand
50 {
51     private static final String JavaDoc CASCADE_OPTION = "cascade";
52     private static final String JavaDoc DROPTABLES_OPTION = "droptables";
53     private static final String JavaDoc TARGET_OPTION = "target";
54
55     /**
56      * An abstract method that Executes the command
57      * @throws CommandException
58      */

59     public void runCommand()
60         throws CommandException, CommandValidationException
61     {
62         validateOptions();
63
64         DeploymentFacility df = DeploymentFacilityFactory.getDeploymentFacility();
65         ServerConnectionIdentifier conn = createServerConnectionIdentifier(
66             getHost(), getPort(), getUser(), getPassword());
67         df.connect(conn);
68
69         //prepare data
70
//Target[] targets = new JESTarget[1];
71
final String JavaDoc targetName = getOption(TARGET_OPTION);
72         //targets[0] = new JESTarget(targetName, null);
73
Map JavaDoc deployOptions = createDeploymentProperties();
74
75         JESProgressObject progressObject = null;
76         
77         try
78         {
79             if (df.isConnected())
80             {
81                 CLILogger.getInstance().printDebugMessage("Calling the undeploy with DeployOptions");
82                 Target[] targets = df.createTargets(new String JavaDoc[]{targetName});
83                 if (targets == null)
84                 {
85                     //CLILogger.getInstance().printError(getLocalizedString("InvalidTarget"));
86
throw new CommandException(getLocalizedString("InvalidTarget", new Object JavaDoc[] {targetName}));
87                 }
88
89                 progressObject = df.undeploy(targets, getComponentName(), deployOptions);
90             } else
91             {
92                 CLILogger.getInstance().printError(
93                                    getLocalizedString("CouldNotConnectToDAS"));
94             }
95         }
96         catch (Exception JavaDoc e)
97         {
98             if (e.getLocalizedMessage() != null)
99                 CLILogger.getInstance().printDetailMessage(
100                     e.getLocalizedMessage());
101
102             throw new CommandException(getLocalizedString(
103                 "CommandUnSuccessful", new Object JavaDoc[] {name} ), e);
104         }
105
106         DeploymentStatus status = df.waitFor(progressObject);
107         final String JavaDoc statusString = status.getStageStatusMessage();
108
109
110         if (status != null &&
111             status.getStatus() == DeploymentStatus.FAILURE) {
112             throw new CommandException(getLocalizedString(
113                 "CommandUnSuccessfulWithMsg", new Object JavaDoc[] {name,
114                 statusString} ));
115         } else if (status != null &&
116             status.getStatus() == DeploymentStatus.WARNING) {
117             CLILogger.getInstance().printDetailMessage(getLocalizedString(
118                 "CommandSuccessfulWithMsg",
119                 new Object JavaDoc[] {name, statusString}));
120         } else {
121             CLILogger.getInstance().printDetailMessage(getLocalizedString(
122                 "CommandSuccessful", new Object JavaDoc[] {name} ));
123         }
124
125     }
126
127
128     /**
129      * An abstract method that validates the options
130      * on the specification in the xml properties file
131      * This method verifies for the correctness of number of
132      * operands and if all the required options are supplied by the client.
133      * @return boolean returns true if success else returns false
134      */

135     public boolean validateOptions() throws CommandValidationException
136     {
137         return super.validateOptions();
138     }
139
140
141     /**
142      * this method returns the componet_name operand
143      * @return component_name
144      */

145     private String JavaDoc getComponentName()
146     {
147         return (String JavaDoc) getOperands().get(0);
148     }
149
150
151     /**
152      * creates the DeployProperties which is used as a parameter to the
153      * deploy operation.
154      */

155     private Map JavaDoc createDeploymentProperties()
156     {
157         Properties props = new Properties();
158         final String JavaDoc cascadeOption = getOption(CASCADE_OPTION);
159         final String JavaDoc dropTablesOption = getOption(DROPTABLES_OPTION);
160         final String JavaDoc target = getOption(TARGET_OPTION);
161                 
162         if (props != null)
163             props.put(DeploymentProperties.TARGET, target);
164
165         props.put(DeploymentProperties.NAME, getComponentName() );
166
167         if (cascadeOption != null)
168             props.put(DeploymentProperties.CASCADE, cascadeOption);
169         if (dropTablesOption != null)
170             props.put(Constants.CMP_DROP_TABLES, dropTablesOption);
171         return props;
172     }
173
174
175 }
176
Popular Tags