KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.management.MBeanServerConnection JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 import com.sun.appserv.management.util.misc.ExceptionUtil;
32 import com.sun.appserv.management.base.Util;
33 import javax.management.MalformedObjectNameException JavaDoc;
34 import java.lang.NullPointerException JavaDoc;
35 import javax.management.RuntimeOperationsException JavaDoc;
36
37 import java.util.Map JavaDoc;
38 import java.util.Set JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.lang.IllegalArgumentException JavaDoc;
41 import java.io.File JavaDoc;
42 import java.io.IOException JavaDoc;
43
44
45
46 /**
47  * @version $Revision: 1.3 $
48  */

49 public class AMXDeleteResourceCommand extends S1ASCommand
50 {
51     public final static String JavaDoc DOMAIN_CONFIG_OBJECT_NAME = "amx:j2eeType=X-DomainConfig,name=na";
52     public final static String JavaDoc SERVER_CONFIG_OBJECT_NAME = "amx:j2eeType=X-StandaloneServerConfig,name=";
53     public final static String JavaDoc CLUSTER_CONFIG_OBJECT_NAME = "amx:j2eeType=X-ClusterConfig,name=";
54     public final static String JavaDoc TARGET_NAME = "target";
55
56     /**
57      * An abstract method that validates the options
58      * on the specification in the xml properties file
59      * This method verifies for the correctness of number of
60      * operands and if all the required options are supplied by the client.
61      * @return boolean returns true if success else returns false
62      */

63     public boolean validateOptions() throws CommandValidationException
64     {
65         return super.validateOptions();
66     }
67    
68     /**
69      * An abstract method that Executes the command
70      * @throws CommandException
71      */

72     public void runCommand() throws CommandException, CommandValidationException
73     {
74         if (!validateOptions())
75             throw new CommandValidationException("Validation is false");
76         
77         //use http connector
78
MBeanServerConnection JavaDoc mbsc = getMBeanServerConnection(getHost(), getPort(),
79                                                               getUser(), getPassword());
80         final String JavaDoc targetName = (String JavaDoc)getOption(TARGET_NAME);
81         
82         //if targetName is not null, then try to get the Config ObjectName of the
83
//target before deleting the resource because we don't want to delete
84
//the resource if the target does not exist.
85
ObjectName JavaDoc scON = (targetName!=null && !targetName.equals(DOMAIN))?
86                           getTargetConfigObjectName(mbsc, targetName):null;
87
88         final Object JavaDoc[] params = getParamsInfo();
89         final String JavaDoc operationName = getOperationName();
90         final String JavaDoc[] types = getTypesInfo();
91         
92         try {
93             if (scON!=null)
94             {
95                 //remove reference to the target
96
mbsc.invoke(scON, "removeResourceRefConfig",
97                             new Object JavaDoc[]{new String JavaDoc((String JavaDoc)getOperands().get(0))},
98                             new String JavaDoc[]{"java.lang.String"} );
99                         
100             }
101             Object JavaDoc returnValue = mbsc.invoke(Util.newObjectName(DOMAIN_CONFIG_OBJECT_NAME),
102                                              operationName,
103                                              params, types);
104             CLILogger.getInstance().printDetailMessage(getLocalizedString(
105                                                        "CommandSuccessful",
106                                                        new Object JavaDoc[] {name}));
107         }
108         catch (Exception JavaDoc e) {
109             displayExceptionMessage(e);
110         }
111
112
113     }
114
115     
116         /**
117          * This routine will display the exception message if the option
118          * --terse is given. This routine will get the root of the exception
119          * and display the message. It will then wrap it with CommaneException
120          * and throw the exception to be handled by CLI framework.
121          * @param e
122          * @throws CommandException
123          */

124     public void displayExceptionMessage(Exception JavaDoc e) throws CommandException
125     {
126             //get the root cause of the exception
127

128         Throwable JavaDoc rootException = ExceptionUtil.getRootCause(e);
129
130         if (rootException.getLocalizedMessage() != null)
131             CLILogger.getInstance().printDetailMessage(rootException.getLocalizedMessage());
132         throw new CommandException(getLocalizedString("CommandUnSuccessful",
133                                                       new Object JavaDoc[] {name} ), e);
134
135     }
136
137
138
139         /**
140          * This routine will get the StandaloneServerConfig or ClusterConfig
141          * by the given target name.
142          * @param MBeanServerConnection
143          * @param targetName
144          * @return ObjectName
145          */

146     private ObjectName JavaDoc getTargetConfigObjectName(final MBeanServerConnection JavaDoc mbsc,
147                                                  final String JavaDoc targetName)
148         throws CommandException
149     {
150
151         try {
152             ObjectName JavaDoc scON = Util.newObjectName(SERVER_CONFIG_OBJECT_NAME+targetName);
153             if (!mbsc.isRegistered(scON))
154                 scON = Util.newObjectName(CLUSTER_CONFIG_OBJECT_NAME+targetName);
155             if (!mbsc.isRegistered(scON))
156                 throw new CommandException(getLocalizedString("InvalidTargetName"));
157         
158             return scON;
159         }
160         catch (RuntimeOperationsException JavaDoc roe)
161         {
162             throw new CommandException(roe);
163         }
164         catch (IOException JavaDoc ioe)
165         {
166             throw new CommandException(ioe);
167         }
168     }
169
170 }
171
Popular Tags