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 remove-community-users
038: * subcommand. The remove-community-users subcommand calls
039: * the CommunityManagerMBean and removes community users.
040: */
041: public class RemoveCommunityUsersCommand 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: String name = getOption("name");
059: String user = getOption("user");
060: List users = getMultiValueOption("users");
061: String search = getOption("search");
062: boolean dryRun = getBooleanOption("dryrun");
063:
064: MBeanServerConnection mbsc = getMBeanServerConnection(
065: getUserId(), getPassword(), getHost());
066:
067: HashMap hm = new HashMap();
068:
069: if (search != null && search != "") {
070: LinkedList path1 = new LinkedList();
071: path1.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
072: path1.addFirst("amobjsearch");
073: ObjectName on = AdminClientUtil
074: .getResourceMBeanObjectName(
075: AdminClientUtil.AMOBJECTSEARCH_MBEAN_TYPE,
076: path1);
077:
078: method = "queryObjectTypes";
079: Object[] params1 = {};
080: String[] signatures1 = {};
081: HashMap objectTypes = (HashMap) mbsc.invoke(on, method,
082: params1, signatures1);
083:
084: method = "queryRootSuffix";
085: Object[] params2 = {};
086: String[] signatures2 = {};
087: String baseDN = (String) mbsc.invoke(on, method,
088: params2, signatures2);
089:
090: method = "searchObjects";
091: Object[] params3 = { baseDN, search,
092: (Integer) objectTypes.get("user"),
093: new Integer(2) };
094: String[] signatures3 = { "java.lang.String",
095: "java.lang.String", "java.lang.Integer",
096: "java.lang.Integer" };
097: hm = (HashMap) mbsc.invoke(on, method, params3,
098: signatures3);
099: }
100:
101: if (users != null && users.size() > 0) {
102: Iterator i = users.iterator();
103: while (i.hasNext()) {
104: String testUser = (String) i.next();
105: if (!hm.containsKey(testUser)) {
106: hm.put(testUser, "");
107: }
108: }
109: }
110:
111: if (user != null && user != "") {
112: if (!hm.containsKey(user)) {
113: hm.put(user, "");
114: }
115: }
116:
117: LinkedList path2 = new LinkedList();
118: path2.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
119: path2.addFirst(getPortalId());
120: path2.addFirst("CommunityManager");
121: ObjectName on = AdminClientUtil.getResourceMBeanObjectName(
122: AdminClientUtil.COMMUNITYMANAGER_MBEAN_TYPE, path2);
123:
124: if (dryRun) {
125: sb.append(getLocalizedString("communities.dryrun"));
126: }
127:
128: Iterator i = hm.keySet().iterator();
129: while (i.hasNext()) {
130: user = (String) i.next();
131:
132: Object[] params4 = { name, user };
133: String[] signatures4 = { "java.lang.String",
134: "java.lang.String" };
135:
136: method = "isCommunityUser";
137: Boolean hasUser = (Boolean) mbsc.invoke(on, method,
138: params4, signatures4);
139: if (hasUser.booleanValue()) {
140: sb.append(getLocalizedString(
141: "communities.string.removing",
142: new Object[] { user }));
143: if (!dryRun) {
144: method = "removeCommunityUser";
145: mbsc.invoke(on, method, params4, signatures4);
146: }
147: } else {
148: sb.append(getLocalizedString(
149: "communities.string.notremoving",
150: new Object[] { user }));
151: }
152: }
153: } catch (InstanceNotFoundException ie) {
154: throw new CommandException(getLocalizedString(
155: ERROR_MBEAN_INSTANCE_NOT_FOUND,
156: new Object[] { method }), ie);
157: } catch (MBeanException me) {
158: throw new CommandException(getLocalizedString(
159: ERROR_JMX_INVOKE, new Object[] { method }), me);
160: } catch (ReflectionException re) {
161: throw new CommandException(getLocalizedString(
162: ERROR_MBEAN_REFLECTION_ERROR,
163: new Object[] { method }), re);
164: } catch (CommandException ce) {
165: throw ce;
166: } catch (Exception e) {
167: throw new CommandException(
168: getLocalizedString(COMMAND_FAILED), e);
169: } finally {
170: closeMBeanServerConnection();
171: }
172:
173: CLILogger.getInstance().printMessage(sb.toString());
174: }
175:
176: }
|