1 23 24 package com.sun.enterprise.cli.commands; 25 26 import com.sun.enterprise.cli.framework.*; 27 28 import javax.management.MBeanServerConnection ; 29 import javax.management.ObjectName ; 30 31 import com.sun.appserv.management.util.misc.ExceptionUtil; 32 import com.sun.appserv.management.base.Util; 33 import javax.management.MalformedObjectNameException ; 34 import com.sun.appserv.management.config.ResourceRefConfig; 35 import com.sun.appserv.management.config.ResourceConfig; 36 import com.sun.appserv.management.base.XTypes; 37 import java.lang.NullPointerException ; 38 import javax.management.RuntimeOperationsException ; 39 40 import java.util.Map ; 41 import java.util.HashMap ; 42 import java.util.Set ; 43 import java.util.Iterator ; 44 import java.util.Vector ; 45 import java.lang.IllegalArgumentException ; 46 import java.io.File ; 47 import java.io.IOException ; 48 49 50 51 54 public class AMXListResourcesCommand extends S1ASCommand 55 { 56 public final static String DOMAIN_CONFIG_OBJECT_NAME = "amx:j2eeType=X-DomainConfig,name=na"; 57 public final static String SERVER_CONFIG_OBJECT_NAME = "amx:j2eeType=X-StandaloneServerConfig,name="; 58 public final static String CLUSTER_CONFIG_OBJECT_NAME = "amx:j2eeType=X-ClusterConfig,name="; 59 public final static String TARGET_NAME = "target"; 60 61 68 public boolean validateOptions() throws CommandValidationException 69 { 70 return super.validateOptions(); 71 } 72 73 77 public void runCommand() throws CommandException, CommandValidationException 78 { 79 if (!validateOptions()) 80 throw new CommandValidationException("Validation is false"); 81 82 83 MBeanServerConnection mbsc = getMBeanServerConnection(getHost(), getPort(), 85 getUser(), getPassword()); 86 final Vector vTargetName = getOperands(); 87 final String targetName = (vTargetName.size()>0)?(String )vTargetName.get(0):null; 88 89 ObjectName targetON = (targetName!=null && !targetName.equals(DOMAIN))? 92 getTargetConfigObjectName(mbsc, targetName):null; 93 94 final Object [] params = getParamsInfo(); 95 final String operationName = getOperationName(); 96 final String [] types = getTypesInfo(); 97 final String j2eeType = (String ) ((Vector ) getProperty(PARAMS)).get(0); 98 99 try { 100 Object resources = mbsc.invoke(Util.newObjectName(DOMAIN_CONFIG_OBJECT_NAME), 101 operationName, 102 params, types); 103 104 Map candidates = (Map )resources; 105 106 if (targetON != null ) { 107 candidates = getResourcesFromTarget(mbsc, targetON, candidates); 108 } 109 displayMap("", (Map )candidates); 110 CLILogger.getInstance().printDetailMessage(getLocalizedString( 111 "CommandSuccessful", 112 new Object [] {name})); 113 } 114 catch (Exception e) { 115 displayExceptionMessage(e); 116 } 117 118 119 } 120 121 122 130 private Map getResourcesFromTarget(MBeanServerConnection mbsc, 131 ObjectName targetON, 132 Map candidates) 133 throws Exception 134 { 135 Object resourceRefs = mbsc.invoke(targetON, 136 "getContaineeObjectNameMap", 137 new Object []{new String (XTypes.RESOURCE_REF_CONFIG)}, 138 new String []{"java.lang.String"}); 139 140 final Set resourceKeySet = ((Map )resourceRefs).keySet(); 141 final Iterator resourceKeyIter = resourceKeySet.iterator(); 142 Map resMap = new HashMap (); 143 while (resourceKeyIter.hasNext()) { 144 final String valueName = (String )resourceKeyIter.next(); 145 CLILogger.getInstance().printDebugMessage("Candidate = " + valueName ); 146 if (candidates.containsKey(valueName)) { 147 resMap.put(valueName, candidates.get(valueName)); 148 } 149 } 150 return resMap; 151 } 152 153 154 162 public void displayExceptionMessage(Exception e) throws CommandException 163 { 164 Throwable rootException = ExceptionUtil.getRootCause(e); 166 167 if (rootException.getLocalizedMessage() != null) 168 CLILogger.getInstance().printDetailMessage(rootException.getLocalizedMessage()); 169 throw new CommandException(getLocalizedString("CommandUnSuccessful", 170 new Object [] {name} ), e); 171 172 } 173 174 175 182 private ObjectName getTargetConfigObjectName(final MBeanServerConnection mbsc, 183 final String targetName) 184 throws CommandException 185 { 186 try { 187 ObjectName scON = Util.newObjectName(SERVER_CONFIG_OBJECT_NAME+targetName); 188 if (!mbsc.isRegistered(scON)) 189 scON = Util.newObjectName(CLUSTER_CONFIG_OBJECT_NAME+targetName); 190 if (!mbsc.isRegistered(scON)) 191 throw new CommandException(getLocalizedString("InvalidTargetName")); 192 193 return scON; 194 } 195 catch (RuntimeOperationsException roe) 196 { 197 throw new CommandException(roe); 198 } 199 catch (IOException ioe) 200 { 201 throw new CommandException(ioe); 202 } 203 } 204 205 206 209 private void displayMap(final String msg, final Map m) 210 { 211 final Set keySet = m.keySet(); 212 if (keySet.isEmpty()) { 213 CLILogger.getInstance().printDetailMessage( 214 getLocalizedString("NoElementsToList")); 215 return; 216 } 217 final Iterator keyIter = keySet.iterator(); 218 219 while (keyIter.hasNext()) { 220 final String valueName = (String )keyIter.next(); 221 CLILogger.getInstance().printMessage(msg + " " + valueName ); 222 } 223 } 224 225 } 226 | Popular Tags |