KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @version $Revision: 1.3 $
47  */

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

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

71     public void runCommand() throws CommandException, CommandValidationException
72     {
73         if (!validateOptions())
74             throw new CommandValidationException("Validation is false");
75
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 creating the resource because we don't want to create
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             Object JavaDoc returnValue = mbsc.invoke(Util.newObjectName(DOMAIN_CONFIG_OBJECT_NAME),
94                                              operationName,
95                                              params, types);
96             if (scON!=null)
97             {
98                 //create reference to the target
99
mbsc.invoke(scON, "createResourceRefConfig",
100                             new Object JavaDoc[]{new String JavaDoc((String JavaDoc)getOperands().get(0))},
101                             new String JavaDoc[]{"java.lang.String"} );
102             }
103             CLILogger.getInstance().printDetailMessage(getLocalizedString(
104                                                        "CommandSuccessful",
105                                                        new Object JavaDoc[] {name}));
106
107         }
108         catch (Exception JavaDoc e) {
109             displayExceptionMessage(e);
110         }
111     }
112
113         /**
114          * This routine will display the exception message if the option
115          * --terse is given. This routine will get the root of the exception
116          * and display the message. It will then wrap it with CommaneException
117          * and throw the exception to be handled by CLI framework.
118          * @param e
119          * @throws CommandException
120          */

121     public void displayExceptionMessage(Exception JavaDoc e) throws CommandException
122     {
123             //get the root cause of the exception
124
Throwable JavaDoc rootException = ExceptionUtil.getRootCause(e);
125         
126         if (rootException.getLocalizedMessage() != null)
127             CLILogger.getInstance().printDetailMessage(rootException.getLocalizedMessage());
128         throw new CommandException(getLocalizedString("CommandUnSuccessful",
129                                                       new Object JavaDoc[] {name} ), e);
130
131     }
132     
133
134         /**
135          * This routine will get the StandaloneServerConfig or ClusterConfig
136          * by the given target name.
137          * @param MBeanServerConnection
138          * @param targetName
139          * @return ObjectName
140          */

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