001: /**
002: * Copyright 2004-2005 jManage.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package org.jmanage.core.util;
016:
017: import java.util.StringTokenizer;
018:
019: /**
020: * Provides a mechanism to specify application name, mbean name and
021: * attribute or operation name as a single String expression. The expression
022: * looks like the following:
023: * <br/>
024: * <appName>/<mbeaName>/<attrName>
025: * <p>
026: * The mbeanName could be the "configured" mbean name or the object name.
027: * This depends on the context in which Expression object is used.
028: * <p>
029: * This object is widely used in the jManage application specifically in
030: * the command line UI and the access control system.
031: *
032: * Date: Feb 26, 2005
033: * @author Rakesh Kalra
034: */
035: public class Expression {
036:
037: public static final String WILDCARD = "*";
038: public static final String DELIMITER = "/";
039:
040: private final String exprString;
041: private String appName;
042: private String mbeanName;
043: /* this could be attribute or operation name */
044: private String targetName;
045:
046: public Expression(String appName, String mbeanName,
047: String targetName) {
048:
049: this .appName = appName != null && appName.length() > 0 ? appName
050: : WILDCARD;
051: this .mbeanName = mbeanName != null && mbeanName.length() > 0 ? mbeanName
052: : WILDCARD;
053: this .targetName = targetName != null && targetName.length() > 0 ? targetName
054: : WILDCARD;
055: StringBuffer buff = new StringBuffer(this .appName);
056: buff.append(DELIMITER);
057: buff.append("\"");
058: buff.append(this .mbeanName);
059: buff.append("\"");
060: buff.append(DELIMITER);
061: buff.append(targetName);
062: this .exprString = buff.toString();
063: }
064:
065: public Expression(String exprString) {
066: this (exprString, (Expression) null);
067: }
068:
069: public Expression(String exprString, Expression context) {
070:
071: this .exprString = exprString;
072: StringTokenizer tokenizer = new CustomStringTokenizer(
073: exprString);
074: if (context != null) {
075: this .appName = context.getAppName();
076: this .mbeanName = context.getMBeanName();
077: this .targetName = context.getTargetName();
078: int tokenCount = 0;
079: // we only expect max 3 tokens
080: String[] tokens = new String[3];
081: for (int i = 0; tokenizer.hasMoreTokens() && i < 3; i++) {
082: tokenCount++;
083: tokens[i] = tokenizer.nextToken();
084: }
085: if (tokenizer.hasMoreTokens()) {
086: throw new RuntimeException("invalid expression");
087: }
088: switch (tokenCount) {
089: case 1:
090: targetName = tokens[0];
091: break;
092: case 2:
093: mbeanName = tokens[0];
094: targetName = tokens[1];
095: break;
096: case 3:
097: appName = tokens[0];
098: mbeanName = tokens[1];
099: targetName = tokens[2];
100: break;
101: }
102: } else {
103: if (tokenizer.hasMoreTokens())
104: appName = tokenizer.nextToken();
105: if (tokenizer.hasMoreTokens())
106: mbeanName = tokenizer.nextToken();
107: if (tokenizer.hasMoreTokens())
108: targetName = tokenizer.nextToken();
109: }
110:
111: }
112:
113: public String getAppName() {
114: return appName;
115: }
116:
117: public String getMBeanName() {
118: return mbeanName;
119: }
120:
121: public String getTargetName() {
122: return targetName;
123: }
124:
125: public String toString() {
126: return exprString;
127: }
128:
129: public String getHtmlEscaped() {
130: return exprString.replaceAll("\"", """);
131: }
132:
133: /**
134: * Handles the case where the delimiter "/" is within the expression.
135: * Note that this tokenizer doesn't return the right value for
136: * countTokens()
137: */
138:
139: private class CustomStringTokenizer extends StringTokenizer {
140:
141: public CustomStringTokenizer(String expr) {
142: super (expr, DELIMITER);
143: }
144:
145: /**
146: * Handles "/" within the expression.
147: */
148: public String nextToken() {
149: String token = super .nextToken();
150: if (token.startsWith("\"")) {
151: if (token.endsWith("\"")) {
152: // token ends with double quotes. just drop the double quotes
153: token = token.substring(1, token.length() - 1);
154: } else {
155: /* token starts with double quotes, but doesn't end with it.
156: Keep getting next token,
157: till we find ending double quotes */
158: StringBuffer buff = new StringBuffer(token
159: .substring(1));
160:
161: while (true) {
162: String nextToken = super .nextToken();
163: buff.append(DELIMITER);
164: if (nextToken.endsWith("\"")) {
165: buff.append(nextToken.substring(0,
166: nextToken.length() - 1));
167: break;
168: } else {
169: buff.append(nextToken);
170: }
171: }
172: token = buff.toString();
173: }
174: }
175: return token;
176: }
177:
178: public int countTokens() {
179: throw new RuntimeException("countTokens() is not supported");
180: }
181: }
182: }
|