001: /**
002: * Copyright 2004 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.admin.cli.commands.communities;
013:
014: import java.io.*;
015: import java.lang.*;
016: import java.util.*;
017: import java.text.*;
018:
019: // JMX
020: import javax.management.MBeanServerConnection;
021: import javax.management.ObjectName;
022: import javax.management.InstanceNotFoundException;
023: import javax.management.MBeanException;
024: import javax.management.ReflectionException;
025: import javax.management.MalformedObjectNameException;
026:
027: // PS Admin Common
028: import com.sun.portal.admin.common.util.AdminClientUtil;
029:
030: // CLI framework
031: import com.sun.enterprise.cli.framework.*;
032:
033: // Base Class
034: import com.sun.portal.admin.cli.commands.GenericCommand;
035:
036: /**
037: * This class implements the psadmin list-communities
038: * subcommand. The list-communities subcommand calls
039: * the CommunityManagerMBean and lists all communities.
040: */
041: public class ListCommunitiesCommand extends GenericCommand {
042:
043: private String method = "";
044:
045: /**
046: * An abstract method that executes the command
047: * @throws CommandException
048: * @throws CommandValidationException
049: */
050: public void runCommand() throws CommandException,
051: CommandValidationException {
052: validateOptions();
053: validatePortalId();
054:
055: StringBuffer sb = new StringBuffer();
056:
057: try {
058: method = "searchForCommunities";
059:
060: String search = getOption("search");
061: if (search == null) {
062: search = ".*";
063: } else {
064: search = search.replaceAll("\\*", ".*");
065: }
066:
067: boolean verbose = getBooleanOption("verbose");
068:
069: LinkedList path = new LinkedList();
070: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
071: path.addFirst(getPortalId());
072: path.addFirst("CommunityManager");
073: ObjectName on = AdminClientUtil.getResourceMBeanObjectName(
074: AdminClientUtil.COMMUNITYMANAGER_MBEAN_TYPE, path);
075:
076: MBeanServerConnection mbsc = getMBeanServerConnection(
077: getUserId(), getPassword(), getHost());
078:
079: Object[] params = { search };
080: String[] signatures = { "java.lang.String" };
081: ArrayList data = (ArrayList) mbsc.invoke(on, method,
082: params, signatures);
083: for (int index = 0; index < data.size(); index++) {
084: Properties p = (Properties) data.get(index);
085: if (verbose) {
086: sb.append("name=" + p.getProperty("name"));
087: sb.append("|");
088: sb.append("id=" + p.getProperty("id"));
089: sb.append("|");
090: sb.append("unlisted=" + p.getProperty("unlisted"));
091: sb.append("|");
092: sb.append("membershiprestricted="
093: + p.getProperty("membershiprestricted"));
094: sb.append("|");
095: sb.append("secured=" + p.getProperty("secured"));
096: sb.append("|");
097: sb.append("disabled=" + p.getProperty("disabled"));
098: sb.append("|");
099: sb.append("deleted=" + p.getProperty("deleted"));
100: } else {
101: sb.append(p.getProperty("name"));
102: }
103: sb.append("\n");
104: }
105: } catch (InstanceNotFoundException ie) {
106: throw new CommandException(getLocalizedString(
107: ERROR_MBEAN_INSTANCE_NOT_FOUND,
108: new Object[] { method }), ie);
109: } catch (MBeanException me) {
110: throw new CommandException(getLocalizedString(
111: ERROR_JMX_INVOKE, new Object[] { method }), me);
112: } catch (ReflectionException re) {
113: throw new CommandException(getLocalizedString(
114: ERROR_MBEAN_REFLECTION_ERROR,
115: new Object[] { method }), re);
116: } catch (CommandException ce) {
117: throw ce;
118: } catch (Exception e) {
119: throw new CommandException(
120: getLocalizedString(COMMAND_FAILED), e);
121: } finally {
122: closeMBeanServerConnection();
123: }
124:
125: CLILogger.getInstance().printMessage(sb.toString());
126: }
127:
128: }
|