001: /**
002: * $Id: ListConfiguredProducersCommand.java,v 1.4 2006/02/14 21:17:34 pa157442 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.wsrp;
014:
015: //CLI framework
016: import com.sun.enterprise.cli.framework.*;
017:
018: //JMX
019: import javax.management.MBeanServerConnection;
020: import javax.management.ObjectName;
021: import javax.management.MBeanException;
022: import javax.management.ReflectionException;
023: import javax.management.MalformedObjectNameException;
024: import javax.management.InstanceNotFoundException;
025:
026: //PS Admin Common
027: import com.sun.portal.admin.common.PSMBeanException;
028: import com.sun.portal.admin.common.util.AdminClientUtil;
029:
030: import com.sun.portal.admin.cli.commands.GenericCommand;
031:
032: import java.util.List;
033: import java.util.Set;
034: import java.util.TreeSet;
035: import java.util.HashMap;
036: import java.util.Map;
037: import java.util.Iterator;
038: import java.util.LinkedList;
039:
040: public class ListConfiguredProducersCommand extends GenericCommand {
041:
042: public static final String OPT_DN = "dn";
043: //param to be passed to mbean
044: private String dn = null;
045:
046: // list-configured-producers output property keys
047: public static final String PRODUCER_LABEL = "psadmin.subcommand.resource.list-configured-producers.producer.label";
048: public static final String PRODUCER_ID_LABEL = "psadmin.subcommand.resource.list-configured-producers.producer.id.label";
049: public static final String PRODUCER_NAME_LABEL = "psadmin.subcommand.resource.list-configured-producers.producer.name.label";
050: public static final String CONFIGURED_PRODUCER_NAME_LABEL = "psadmin.subcommand.resource.list-configured-producers.configured.producer.name.label";
051: public static final String CONFIGURED_PRODUCER_ID_LABEL = "psadmin.subcommand.resource.list-configured-producers.configured.producer.id.label";
052: public static final String NO_PRODUCER_LABEL = "psadmin.subcommand.resource.list-configured-producers.noproducer.label";
053:
054: public boolean validateOptions() throws CommandValidationException {
055: return super .validateOptions();
056: }
057:
058: public void runCommand() throws CommandException,
059: CommandValidationException {
060: if (!validateOptions()) {
061: throw new CommandValidationException(
062: getLocalizedString(ERROR_OPTION_VALIDATION_FAILED));
063: }
064:
065: String portalId = getOption(OPT_PORTAL_ID);
066: if (portalId != null) {
067: validatePortalId();
068: }
069:
070: MBeanServerConnection mbsc = getMBeanServerConnection(
071: getUserId(), getPassword(), getHost());
072:
073: StringBuffer outStr = new StringBuffer();
074:
075: try {
076:
077: dn = getOption(OPT_DN);
078: // Setting the params and signature
079: Object[] params = { dn };
080: String[] signature = { "java.lang.String" };
081:
082: LinkedList path = new LinkedList();
083: path.addFirst(getDomainId());
084: path.addFirst(getPortalId());
085: path.addFirst("Consumer");
086:
087: ObjectName objectName = AdminClientUtil
088: .getResourceMBeanObjectName(
089: "PortalDomain.Portal.Consumer", path);
090:
091: Map producerObjs = (Map) mbsc.invoke(objectName,
092: "listConfiguredProducers", params, signature);
093:
094: outStr
095: .append("=========================================================\n");
096: if ((producerObjs == null) || (producerObjs.size() == 0)) {
097: outStr.append(getLocalizedString(NO_PRODUCER_LABEL));
098: } else {
099: outStr.append(getLocalizedString(PRODUCER_LABEL));
100: outStr.append("-----------------------------\n");
101: outStr.append(getLocalizedString(PRODUCER_ID_LABEL));
102: outStr.append("\t\t");
103: outStr.append(getLocalizedString(PRODUCER_NAME_LABEL));
104: outStr.append("\n\n");
105: Set keySet = producerObjs.keySet();
106: for (Iterator i = keySet.iterator(); i.hasNext();) {
107: String producerId = (String) i.next();
108: String producerName = (String) producerObjs
109: .get(producerId);
110: outStr.append(getLocalizedString(
111: CONFIGURED_PRODUCER_ID_LABEL,
112: new Object[] { producerId }));
113: outStr.append(getLocalizedString(
114: CONFIGURED_PRODUCER_NAME_LABEL,
115: new Object[] { producerName }));
116: }
117: }
118:
119: outStr
120: .append("=========================================================\n");
121:
122: } catch (InstanceNotFoundException ie) {
123: throw new CommandException(getLocalizedString(
124: ERROR_MBEAN_INSTANCE_NOT_FOUND,
125: new Object[] { "listConfiguredProducers" }), ie);
126: } catch (MBeanException me) {
127: boolean psmbe = me.getCause() instanceof PSMBeanException;
128: if (getBooleanOption(OPT_DEBUG)) {
129: if (psmbe) {
130: String dbgMsg = me.getCause().getMessage();
131: if (dbgMsg != null) {
132: CLILogger.getInstance().printMessage(dbgMsg);
133: }
134: }
135: }
136: if (psmbe) {
137: PSMBeanException pme = (PSMBeanException) me.getCause();
138: if (((PSMBeanException) me.getCause()).getTokens() != null) {
139: throw new CommandException(getLocalizedString(pme
140: .getErrorKey(), pme.getTokens()), pme);
141: } else {
142: throw new CommandException(getLocalizedString(pme
143: .getErrorKey()), me);
144: }
145: } else {
146: throw new CommandException(getLocalizedString(
147: ERROR_JMX_INVOKE,
148: new Object[] { "listConfiguredProducers" }), me);
149: }
150: } catch (ReflectionException re) {
151: throw new CommandException(getLocalizedString(
152: ERROR_MBEAN_REFLECTION_ERROR,
153: new Object[] { "listConfiguredProducers" }), re);
154: } catch (Exception ex) {
155: throw new CommandException(
156: getLocalizedString(COMMAND_FAILED), ex);
157: } finally {
158: closeMBeanServerConnection();
159: }
160:
161: CLILogger.getInstance().printMessage(outStr.toString());
162: }
163:
164: }
|