KickJava   Java API By Example, From Geeks To Geeks.

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


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.CommandValidationException;
27 import com.sun.enterprise.cli.framework.CommandException;
28 import com.sun.enterprise.cli.framework.CLILogger;
29 import com.sun.enterprise.admin.pluggable.ClientPluggableFeatureFactory;
30 import com.sun.enterprise.admin.servermgmt.DomainsManager;
31 import com.sun.enterprise.admin.servermgmt.DomainConfig;
32 import com.sun.enterprise.admin.servermgmt.InstancesManager;
33 import com.sun.enterprise.admin.common.Status;
34
35 /**
36  * This is a sample Deploy command
37  * @version $Revision: 1.3 $
38  */

39 public class StopDomainCommand extends BaseLifeCycleCommand {
40     
41     /**
42      * An abstract method that validates the options
43      * on the specification in the xml properties file
44      * This method verifies for the correctness of number of
45      * operands and if all the required options are supplied by the client.
46      * @return boolean returns true if success else returns false
47      */

48     public boolean validateOptions() throws CommandValidationException {
49         return super.validateOptions();
50     }
51     
52     public void stopDomain (String JavaDoc domainName) throws CommandException, CommandValidationException
53     {
54         try {
55             final ClientPluggableFeatureFactory fac = getFeatureFactory();
56             final DomainsManager mgr = fac.getDomainsManager();
57             final DomainConfig cfg = getDomainConfig(domainName);
58             final InstancesManager im = mgr.getInstancesManager(cfg);
59             final int state = im.getInstanceStatus();
60             final String JavaDoc[] domains = mgr.listDomains(cfg);
61             boolean exists = false;
62             
63             for(int i = 0; domains != null && i < domains.length; i++)
64             {
65                 if(domains[i].equals(domainName))
66                 {
67                     exists = true;
68                     break;
69                 }
70             }
71
72             if(!exists)
73             {
74                 // it doesn't exist -- let the existing code throw an Exception with
75
// the correct error message
76
mgr.stopDomain(cfg);
77             }
78
79             // check if the Domain is running
80
else if (state == Status.kInstanceRunningCode)
81             {
82                 mgr.stopDomain(cfg);
83             
84                 CLILogger.getInstance().printDetailMessage(getLocalizedString("DomainStopped",
85                                              new Object JavaDoc[] {domainName}));
86             }
87             else
88             {
89                 // bnevins Oct 2004
90
// it exists and it is already not running.
91
// This has officially been defined as an error. So we have to throw an Exception.
92

93                 throw new CommandException(getLocalizedString("CannotStopDomainAlreadyStopped",
94                        new Object JavaDoc[] {domainName}));
95             }
96         }
97         catch(Exception JavaDoc e) {
98             CLILogger.getInstance().printDetailMessage(e.getLocalizedMessage());
99             throw new CommandException(getLocalizedString("CannotStopDomain",
100                                                           new Object JavaDoc[] {domainName} ), e);
101         }
102     }
103     //
104
/**
105      * An abstract method that Executes the command
106      * @throws CommandException
107      */

108     public void runCommand() throws CommandException, CommandValidationException {
109         validateOptions();
110         String JavaDoc domainName = null;
111         try {
112             domainName = getDomainName();
113         } catch(Exception JavaDoc e) {
114             CLILogger.getInstance().printDetailMessage(e.getLocalizedMessage());
115             domainName = domainName==null?getLocalizedString("Undefined"):domainName;
116             throw new CommandException(getLocalizedString("CannotStopDomain",
117                                                           new Object JavaDoc[] {domainName} ), e);
118         }
119         stopDomain(domainName);
120     }
121 }
122
Popular Tags