KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > CommandMapper


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 /**
25  * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
26  *
27  * Copyright 2001-2002 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  */

31 package com.sun.enterprise.admin.monitor;
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Properties JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
37
38 import javax.management.MalformedObjectNameException JavaDoc;
39 import javax.management.ObjectName JavaDoc;
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 //i18n import
47
import com.sun.enterprise.util.i18n.StringManager;
48
49 /**
50  * Maps commands to Command objects. The command objects encapsulate monitoring
51  * functionality exposed to the user interface.
52  */

53 public class CommandMapper {
54
55     /**
56      * A map to track instance name and corresponding command mapper
57      */

58     private static HashMap JavaDoc instanceMap = new HashMap JavaDoc();
59
60     /**
61      * Name of the instance
62      */

63     private String JavaDoc instanceName;
64
65     // i18n StringManager
66
private static StringManager localStrings =
67         StringManager.getManager( CommandMapper.class );
68
69     /**
70      * Private constructor. Use factory method to get an instance.
71      */

72     private CommandMapper() {
73     }
74
75     /**
76      * Map CLI command and dotted name to a command object.
77      * @param command the cli command, valid values are CLI_COMMAND_GET and
78      * CLI_COMMAND_LIST
79      * @param dottedName the dotted name to which this command applies
80      * @throws IllegalArgumentException if the specified command is unknown
81      * @throws MalformedNameException if specified dottedName is incorrect
82      * @retun A command object that encapsulates the requested command
83      */

84     public MonitorCommand mapCliCommand(String JavaDoc command, String JavaDoc 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 JavaDoc msg = localStrings.getString( "admin.monitor.unknown_cli_command", command );
93             throw new IllegalArgumentException JavaDoc( msg );
94         }
95         return monitorCommand;
96     }
97
98     /**
99      * Map CLI get command to a command object.
100      *
101      * A dotted name for get command is of the form
102      * <code>instanceName([.type[.name])*.(star|attrName)</code> where,
103      * instanceName is name of server instance, type is derived from
104      * MonitoredObjectType, name is monitored component name, star is
105      * the character <code>*</code> (denotes all attributes) and attrName is
106      * the name of the attribute to get.
107      * @param dottedName dotted name to get.
108      * @throws MalformedNameException if the specified dotted name can not be
109      * used for a get command.
110      * @retun A command object that encapsulates the requested get command
111      */

112     public MonitorGetCommand mapGetCommand(String JavaDoc 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     /**
127      * Map CLI list command to a command object.
128      *
129      * A dotted name for list command is of the form
130      * <code>instanceName([.type[.name])*</code> where, instanceName is name
131      * of server instance, type is derived from MonitoredObjectType, name is
132      * monitored component name.
133      * @param dottedName dotted name to list.
134      * @throws MalformedNameException if the specified dotted name can not be
135      * used for a list command.
136      * @retun A command object that encapsulates the requested list command
137      */

138     public MonitorListCommand mapListCommand(String JavaDoc 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 JavaDoc dottedName, Object JavaDoc args)
152             throws MalformedNameException {
153         ParsedDottedName result = parseDottedName(dottedName, CLI_COMMAND_SET);
154         MonitorSetCommand command = null;
155         String JavaDoc argsStr = (String JavaDoc)args;
156         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(argsStr, ",");
157         String JavaDoc[] commandArgs = new String JavaDoc[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     /**
170      * Get command mapper for specified server instance. This is the factory
171      * method to be used to obtain instances of command mapper.
172      * @param instanceName name of the instance
173      * @return a command mapper for specified instance
174      */

175     public static CommandMapper getInstance(String JavaDoc 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     /**
186      * Parse dotted name for the specified command. This method splits the
187      * dotted string into tokens and then invokes parseTokens().
188      * @param dottedString the dotted name
189      * @param command the CLI command
190      * @return parsed dotted name as an object
191      */

192     private ParsedDottedName parseDottedName(String JavaDoc dottedString, String JavaDoc command)
193            throws MalformedNameException {
194         ArrayList JavaDoc tokenList = new ArrayList JavaDoc();
195         Name JavaDoc dottedName = new Name JavaDoc(dottedString);
196         int nTokens = dottedName.getNumParts();
197         if (nTokens < 1) {
198             String JavaDoc 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     /**
208      * Parse tokens derived from the dotted name for specified CLI command. This
209      * method tries to derive MBean object name and other parameters for the
210      * specified command.
211      * @param tokenList list of tokens
212      * @param command the CLI command
213      * @param dottedName the dotted name, this is used only for generating
214      * the message for MalformedNameException
215      * @return parsed dotted name as an object
216      * @throws MalformedNameException if any of the type specified in CLI dotted
217      * name is invalid, or if JMX object name can not be created using
218      * specified dotted name (for example - if the dotted name contains a
219      * comma).
220      */

221     private ParsedDottedName parseTokens(ArrayList JavaDoc tokenList, String JavaDoc command,
222             String JavaDoc dottedName) throws MalformedNameException {
223         Properties JavaDoc props = new Properties JavaDoc();
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         // 1st token is name of the instance, ignore.
232
// Process 2nd token onwards - either singleton-type or type.name. The
233
// last token is a special case because it can be a wildcard or
234
// attribute name for get command, a type or name for list command
235
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 JavaDoc token = (String JavaDoc)tokenList.get(i);
242             if (processType) {
243                 type = MonitoredObjectType.getMonitoredObjectTypeOrNull(token);
244                 if (type == null) {
245                     String JavaDoc msg = localStrings.getString( "admin.monitor.invalid_entry", dottedName, token );
246                     throw new MalformedNameException( msg );
247                 }
248                 String JavaDoc 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 JavaDoc(ObjectNames.kDefaultIASDomainName,
265                     props);
266         } catch (MalformedObjectNameException JavaDoc ione) {
267             throw new MalformedNameException(ione.getMessage());
268         }
269         // type != null -- Means that a name was expected in parsing. For
270
// LIST command implies list of objects of this type. For GET command
271
// implies all specified attribute(or wildcard) of on specified type of
272
// objects.
273
if (type != null) {
274             result.monitoredObjectType = type;
275         }
276         if (command.equals(CLI_COMMAND_GET)) {
277             result.attributeName = (String JavaDoc)tokenList.get(count -1);
278         } else if (command.equals(CLI_COMMAND_SET))
279             result.operationName = (String JavaDoc)tokenList.get(count -1);
280         
281         return result;
282     }
283
284     /**
285      * Apply a transform to specified properties. The transform is to extract
286      * properties "name" and "type" and put the value of type as key and
287      * value of name as correponding value and then replacing values of "name"
288      * and "type" by specified new values (newName and newType).
289      * @param props the property list
290      * @param newType new value for property "type"
291      * @param newName new value for property "name"
292      */

293     private void swapNameType(Properties JavaDoc props, String JavaDoc newType, String JavaDoc newName) {
294         String JavaDoc oldType = props.getProperty(ObjectNames.kMonitoringClassName);
295         String JavaDoc 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     /**
302      * Constant to denote CLI get command
303      */

304     public static final String JavaDoc CLI_COMMAND_GET = "GET";
305
306     /**
307      * Constant to denote CLI list command
308      */

309     public static final String JavaDoc CLI_COMMAND_LIST = "LIST";
310
311     /**
312      * Constant to denote CLI set command
313      */

314     public static final String JavaDoc CLI_COMMAND_SET = "SET";
315 }
316
317 /**
318  * Parsed dotted name. A parsed dotted name has three components - JMX object
319  * name, sub monitored object type (if any) and attribute name (if any).
320  */

321 class ParsedDottedName {
322     ObjectName JavaDoc objectName;
323     MonitoredObjectType monitoredObjectType;
324     String JavaDoc attributeName;
325     String JavaDoc operationName;
326 }
327
Popular Tags