1 23 24 31 package com.sun.enterprise.admin.monitor; 32 33 import java.util.ArrayList ; 34 import java.util.HashMap ; 35 import java.util.Properties ; 36 import java.util.StringTokenizer ; 37 38 import javax.management.MalformedObjectNameException ; 39 import javax.management.ObjectName ; 40 41 import com.sun.enterprise.admin.common.MalformedNameException; 42 import com.sun.enterprise.admin.common.Name; 43 import com.sun.enterprise.admin.common.ObjectNames; 44 import com.sun.enterprise.admin.server.core.AdminService; 45 46 import com.sun.enterprise.util.i18n.StringManager; 48 49 53 public class CommandMapper { 54 55 58 private static HashMap instanceMap = new HashMap (); 59 60 63 private String instanceName; 64 65 private static StringManager localStrings = 67 StringManager.getManager( CommandMapper.class ); 68 69 72 private CommandMapper() { 73 } 74 75 84 public MonitorCommand mapCliCommand(String command, String dottedName) 85 throws MalformedNameException { 86 MonitorCommand monitorCommand = null; 87 if (CLI_COMMAND_GET.equalsIgnoreCase(command)) { 88 monitorCommand = mapGetCommand(dottedName); 89 } else if (CLI_COMMAND_LIST.equalsIgnoreCase(command)) { 90 monitorCommand = mapListCommand(dottedName); 91 } else { 92 String msg = localStrings.getString( "admin.monitor.unknown_cli_command", command ); 93 throw new IllegalArgumentException ( msg ); 94 } 95 return monitorCommand; 96 } 97 98 112 public MonitorGetCommand mapGetCommand(String dottedName) 113 throws MalformedNameException { 114 ParsedDottedName result = parseDottedName(dottedName, CLI_COMMAND_GET); 115 MonitorGetCommand command; 116 if (result.monitoredObjectType != null) { 117 command = new MonitorGetCommand(result.objectName, 118 result.monitoredObjectType, result.attributeName); 119 } else { 120 command = new MonitorGetCommand(result.objectName, 121 result.attributeName); 122 } 123 return command; 124 } 125 126 138 public MonitorListCommand mapListCommand(String dottedName) 139 throws MalformedNameException { 140 ParsedDottedName result = parseDottedName(dottedName, CLI_COMMAND_LIST); 141 MonitorListCommand command; 142 if (result.monitoredObjectType != null) { 143 command = new MonitorListCommand(result.objectName, 144 result.monitoredObjectType); 145 } else { 146 command = new MonitorListCommand(result.objectName); 147 } 148 return command; 149 } 150 151 public MonitorSetCommand mapSetCommand(String dottedName, Object args) 152 throws MalformedNameException { 153 ParsedDottedName result = parseDottedName(dottedName, CLI_COMMAND_SET); 154 MonitorSetCommand command = null; 155 String argsStr = (String )args; 156 StringTokenizer st = new StringTokenizer (argsStr, ","); 157 String [] commandArgs = new String [st.countTokens()]; 158 int i = 0; 159 while (st.hasMoreTokens()){ 160 commandArgs[i] = st.nextToken(); 161 i++; 162 } 163 command = new MonitorSetCommand(result.objectName, 164 result.monitoredObjectType, result.operationName, 165 commandArgs); 166 return command; 167 } 168 169 175 public static CommandMapper getInstance(String instanceName) { 176 CommandMapper cm = (CommandMapper)instanceMap.get(instanceName); 177 if (cm == null) { 178 cm = new CommandMapper(); 179 cm.instanceName = instanceName; 180 instanceMap.put(instanceName, cm); 181 } 182 return cm; 183 } 184 185 192 private ParsedDottedName parseDottedName(String dottedString, String command) 193 throws MalformedNameException { 194 ArrayList tokenList = new ArrayList (); 195 Name dottedName = new Name (dottedString); 196 int nTokens = dottedName.getNumParts(); 197 if (nTokens < 1) { 198 String msg = localStrings.getString( "admin.monitor.name_does_not_contain_any_tokens", dottedString ); 199 throw new MalformedNameException( msg ); 200 } 201 for (int j = 0; j < nTokens; j++) { 202 tokenList.add(dottedName.getNamePart(j).toString()); 203 } 204 return parseTokens(tokenList, command, dottedString); 205 } 206 207 221 private ParsedDottedName parseTokens(ArrayList tokenList, String command, 222 String dottedName) throws MalformedNameException { 223 Properties props = new Properties (); 224 props.put(ObjectNames.kTypeKeyName, ObjectNames.kMonitoringType); 225 props.put(ObjectNames.kNameKeyName, ObjectNames.kMonitoringRootClass); 226 props.put(ObjectNames.kMonitoringClassName, 227 ObjectNames.kMonitoringRootClass); 228 props.put(ObjectNames.kServerInstanceKeyName, instanceName); 229 int count = tokenList.size(); 230 MonitoredObjectType type = null; 231 int tokenCount = count; 236 if (command.equals(CLI_COMMAND_GET) || command.equals(CLI_COMMAND_SET)) { 237 tokenCount = count - 1; 238 } 239 boolean processType = true; 240 for (int i = 1; i < tokenCount; i++) { 241 String token = (String )tokenList.get(i); 242 if (processType) { 243 type = MonitoredObjectType.getMonitoredObjectTypeOrNull(token); 244 if (type == null) { 245 String msg = localStrings.getString( "admin.monitor.invalid_entry", dottedName, token ); 246 throw new MalformedNameException( msg ); 247 } 248 String typeName = type.getTypeName(); 249 if (type.isSingleton()) { 250 swapNameType(props, typeName, typeName); 251 processType = true; 252 type = null; 253 } else { 254 processType = false; 255 } 256 } else { 257 swapNameType(props, type.getTypeName(), token); 258 processType = true; 259 type = null; 260 } 261 } 262 ParsedDottedName result = new ParsedDottedName(); 263 try { 264 result.objectName = new ObjectName (ObjectNames.kDefaultIASDomainName, 265 props); 266 } catch (MalformedObjectNameException ione) { 267 throw new MalformedNameException(ione.getMessage()); 268 } 269 if (type != null) { 274 result.monitoredObjectType = type; 275 } 276 if (command.equals(CLI_COMMAND_GET)) { 277 result.attributeName = (String )tokenList.get(count -1); 278 } else if (command.equals(CLI_COMMAND_SET)) 279 result.operationName = (String )tokenList.get(count -1); 280 281 return result; 282 } 283 284 293 private void swapNameType(Properties props, String newType, String newName) { 294 String oldType = props.getProperty(ObjectNames.kMonitoringClassName); 295 String oldName = props.getProperty(ObjectNames.kNameKeyName); 296 props.put(ObjectNames.kMonitoringClassName, newType); 297 props.put(ObjectNames.kNameKeyName, newName); 298 props.put(oldType, oldName); 299 } 300 301 304 public static final String CLI_COMMAND_GET = "GET"; 305 306 309 public static final String CLI_COMMAND_LIST = "LIST"; 310 311 314 public static final String CLI_COMMAND_SET = "SET"; 315 } 316 317 321 class ParsedDottedName { 322 ObjectName objectName; 323 MonitoredObjectType monitoredObjectType; 324 String attributeName; 325 String operationName; 326 } 327 | Popular Tags |