001: /**
002: * $Id: ListAttributesCommand.java,v 1.8 2005/11/07 17:40:16 cathywu Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.admin.cli.commands;
014:
015: import java.util.logging.Logger;
016: import java.util.logging.Level;
017:
018: //CLI framework
019: import com.sun.enterprise.cli.framework.*;
020:
021: //JMX
022: import javax.management.MBeanServerConnection;
023: import javax.management.ObjectName;
024: import javax.management.MBeanException;
025: import javax.management.ReflectionException;
026: import javax.management.MalformedObjectNameException;
027: import javax.management.InstanceNotFoundException;
028:
029: import com.sun.portal.admin.common.util.AdminUtil;
030: import com.sun.portal.admin.common.AttrOptionConstants;
031: import com.sun.portal.admin.cli.display.AttributeDisplayHandlerFactory;
032: import com.sun.portal.admin.cli.display.AttributeDisplayHandler;
033: import com.sun.portal.admin.common.PSMBeanException;
034: import com.sun.portal.admin.common.AttributeInfo;
035:
036: import java.util.Arrays;
037: import java.util.List;
038: import java.util.Map;
039: import java.util.SortedMap;
040: import java.util.TreeMap;
041: import java.util.Iterator;
042:
043: public class ListAttributesCommand extends GenericCommand {
044: String[] portalCentricComponents = { "desktop", "producer",
045: "consumer", "subscriptions" };
046:
047: public void runCommand() throws CommandException,
048: CommandValidationException {
049: if (!validateOptions()) {
050: throw new CommandValidationException(
051: getLocalizedString(ERROR_OPTION_VALIDATION_FAILED));
052: }
053:
054: String portalId = getOption(OPT_PORTAL_ID);
055: validatePortalId(portalId);
056:
057: MBeanServerConnection mbsc = getMBeanServerConnection(
058: getUserId(), getPassword(), getHost());
059:
060: String operation = "";
061: try {
062: ObjectName objectName = portalId == null ? AdminUtil
063: .getPortalDomainMBeanObjectName(getDomainId())
064: : AdminUtil.getPortalMBeanObjectName(getDomainId(),
065: portalId);
066:
067: Map optMap = getOptions();
068: optMap.put("operation", "list");
069: // Setting the params and signature
070: Object[] params = { optMap };
071: String[] signature = { "java.util.Map" };
072: operation = "listAttributes";
073:
074: Object returnValue = mbsc.invoke(objectName, operation,
075: params, signature);
076: formatAndDisplay((Map) returnValue,
077: getBooleanOption(OPT_VERBOSE));
078: } catch (InstanceNotFoundException ie) {
079: logger.log(Level.SEVERE, "PSALI_CSPACC0005", ie);
080: throw new CommandException(getLocalizedString(
081: ERROR_MBEAN_INSTANCE_NOT_FOUND,
082: new Object[] { operation }), ie);
083: } catch (MBeanException me) {
084: logger.log(Level.SEVERE, "PSALI_CSPACC0006", me);
085: boolean psmbe = me.getCause() instanceof PSMBeanException;
086: if (getBooleanOption(OPT_DEBUG)) {
087: if (psmbe) {
088: String dbgMsg = me.getCause().getMessage();
089: if (dbgMsg != null) {
090: CLILogger.getInstance().printMessage(dbgMsg);
091: }
092: }
093: }
094: if (psmbe) {
095: throw new CommandException(
096: getLocalizedString(((PSMBeanException) me
097: .getCause()).getErrorKey()), me);
098: } else {
099: throw new CommandException(getLocalizedString(
100: ERROR_JMX_INVOKE, new Object[] { operation }),
101: me);
102: }
103: } catch (ReflectionException re) {
104: logger.log(Level.SEVERE, "PSALI_CSPACC0007", re);
105: throw new CommandException(getLocalizedString(
106: ERROR_MBEAN_REFLECTION_ERROR,
107: new Object[] { operation }), re);
108: } catch (Exception ex) {
109: logger.log(Level.SEVERE, "PSALI_CSPACC0010", ex);
110: throw new CommandException(
111: getLocalizedString(COMMAND_FAILED), ex);
112: } finally {
113: closeMBeanServerConnection();
114: }
115: }
116:
117: private void validatePortalId(String portalId)
118: throws CommandException {
119: if (portalId != null) {
120: validatePortalId();
121: } else {
122: //portal-id is rerquired option only for portal-centric components
123: String comp = getOption(AttrOptionConstants.OPT_COMPONENT);
124: List comps = Arrays.asList(portalCentricComponents);
125: if (comps.contains(comp)) {
126: throw new CommandException(
127: getLocalizedString("error.psadmin.invalid.portalid"));
128: }
129: }
130: }
131:
132: private void formatAndDisplay(Map map, boolean verbose) {
133: SortedMap sorted = new TreeMap(map);
134: Iterator iter = sorted.keySet().iterator();
135: if (!verbose) {
136: //short format
137: while (iter.hasNext()) {
138: String attrName = (String) iter.next();
139: List info = (List) sorted.get(attrName);
140: String typeStr = "";
141: if (!info.isEmpty()) {
142: int type = ((Integer) info.get(0)).intValue();
143: typeStr = getLocalizedTypeString(type);
144: }
145: CLILogger.getInstance().printMessage(
146: attrName + " {" + typeStr + "}\n");
147: }
148: } else {
149: //long format
150: while (iter.hasNext()) {
151: String attrName = (String) iter.next();
152: List info = (List) sorted.get(attrName);
153: String typeStr = "";
154: String desc = null;
155: String privileges = null;
156: String example = null;
157: if (!info.isEmpty()) {
158: int size = info.size();
159: int type = ((Integer) info.get(0)).intValue();
160: typeStr = getLocalizedTypeString(type);
161: if (size > 1) {
162: String d = (String) info.get(1);
163: if (d != null) {
164: desc = getLocalizedString(d);
165: }
166: }
167: if (size > 2) {
168: Integer i = (Integer) info.get(2);
169: if (i != null) {
170: privileges = getLocalizedPrivilegeString(i
171: .intValue());
172: } else {
173: privileges = getLocalizedPrivilegeString((AttributeInfo.READ_WRITE));
174: }
175: }
176: if (size > 3) {
177: String e = (String) info.get(3);
178: if (e != null) {
179: example = getLocalizedString(e);
180: }
181: }
182: }
183: CLILogger.getInstance().printMessage("----------");
184: CLILogger
185: .getInstance()
186: .printMessage(
187: getLocalizedString("psadmin.list.attr.name.label")
188: + ": " + attrName);
189: CLILogger
190: .getInstance()
191: .printMessage(
192: getLocalizedString("psadmin.list.attr.type.label")
193: + ": " + typeStr);
194: if (desc != null) {
195: CLILogger
196: .getInstance()
197: .printMessage(
198: getLocalizedString("psadmin.list.attr.desc.label")
199: + ": " + desc);
200: }
201: CLILogger
202: .getInstance()
203: .printMessage(
204: getLocalizedString("psadmin.list.attr.privilege.label")
205: + ": " + privileges);
206: if (example != null) {
207: CLILogger
208: .getInstance()
209: .printMessage(
210: getLocalizedString("psadmin.list.attr.example.label")
211: + ": " + example);
212: }
213: }
214: }
215: }
216:
217: private String getLocalizedPrivilegeString(int priv) {
218: String privStr = null;
219: switch (priv) {
220: case AttributeInfo.READ_WRITE:
221: privStr = getLocalizedString("psadmin.list.privilege.read.write");
222: break;
223: case AttributeInfo.READ_ONLY:
224: privStr = getLocalizedString("psadmin.list.privilege.read.only");
225: break;
226: case AttributeInfo.WRITE_ONLY:
227: privStr = getLocalizedString("psadmin.list.privilege.write.only");
228: break;
229: default:
230: privStr = "";
231: }
232: return privStr;
233: }
234:
235: private String getLocalizedTypeString(int type) {
236: String typeStr = null;
237: switch (type) {
238: case AttributeInfo.SINGLE_BOOLEAN:
239: typeStr = getLocalizedString("psadmin.list.type.single.boolean");
240: break;
241: case AttributeInfo.SINGLE_STRING:
242: typeStr = getLocalizedString("psadmin.list.type.single.string");
243: break;
244: case AttributeInfo.SINGLE_NUMERIC:
245: typeStr = getLocalizedString("psadmin.list.type.single.numeric");
246: break;
247: case AttributeInfo.LIST_STRING:
248: typeStr = getLocalizedString("psadmin.list.type.list.string");
249: break;
250: case AttributeInfo.LIST_NUMERIC:
251: typeStr = getLocalizedString("psadmin.list.type.list.numeric");
252: break;
253: case AttributeInfo.SINGLE_CHOICE_STRING:
254: typeStr = getLocalizedString("psadmin.list.type.single.choice.string");
255: break;
256: case AttributeInfo.SINGLE_CHOICE_NUMERIC:
257: typeStr = getLocalizedString("psadmin.list.type.single.choice.numeric");
258: break;
259: case AttributeInfo.MULTIPLE_CHOICE_STRING:
260: typeStr = getLocalizedString("psadmin.list.type.multiple.choice.string");
261: break;
262: case AttributeInfo.MULTIPLE_CHOICE_NUMERIC:
263: typeStr = getLocalizedString("psadmin.list.type.multiple.choice.numeric");
264: break;
265: default:
266: typeStr = "";
267: break;
268: }
269: return typeStr;
270: }
271: }
|