001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.admin.cli.commands.search;
006:
007: import java.util.*;
008: import java.util.logging.Logger;
009: import java.util.logging.Level;
010:
011: import javax.management.MBeanServerConnection;
012: import javax.management.ObjectName;
013: import javax.management.InstanceNotFoundException;
014: import javax.management.MBeanException;
015: import javax.management.ReflectionException;
016:
017: import com.sun.enterprise.cli.framework.*;
018:
019: import com.sun.portal.admin.cli.commands.GenericCommand;
020: import com.sun.portal.admin.common.util.AdminUtil;
021:
022: /**
023: * This class implements the psadmin list-robot-sites subcommand. The
024: * list-robot-sites subcommand calls the PortalDomainMBean and performs
025: * the following tasks:
026: * <UL>
027: * <LI>list robot sites rules
028: * <UL>
029: */
030:
031: public class ListRobotSitesCommand extends GenericCommand {
032:
033: //public Logger logger = super.getLogger();
034:
035: //command error messages
036:
037: //command options
038: private static final String SITE_ID = "site";
039:
040: String siteID = null;
041:
042: public void runCommand() throws CommandException,
043: CommandValidationException {
044:
045: validateOptions();
046: validateSearchServerID();
047:
048: siteID = getOption(SITE_ID);
049: if (siteID == null || siteID.equals("")) {
050: siteID = "";
051: }
052: HashMap sites = null;
053: try {
054: LinkedList path = new LinkedList();
055: //path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
056: path.addFirst(getDomainId());
057: path.addFirst(getSearchServerId());
058: path.addFirst("robot");
059: ObjectName objName = AdminUtil.getResourceMBeanObjectName(
060: AdminUtil.SEARCH_ROBOT_MBEAN_TYPE, path);
061:
062: Object[] params = { siteID };
063: String[] signature = { "java.lang.String" };
064: MBeanServerConnection msc = getMBeanServerConnection(
065: getUserId(), getPassword(), getHost());
066:
067: sites = (HashMap) msc.invoke(objName, "listSites", params,
068: signature);
069:
070: handleReturnValue(sites);
071:
072: } catch (InstanceNotFoundException ie) {
073: //logger.logp(Level.SEVERE, "ListRobotSitesCommand",
074: // "runCommand()", ie.getMessage(), ie);
075: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", ie);
076: throw new CommandException(getLocalizedString(
077: ERROR_MBEAN_INSTANCE_NOT_FOUND,
078: new Object[] { "listSites" }), ie);
079: } catch (MBeanException me) {
080: //logger.logp(Level.SEVERE, "ListRobotSitesCommand",
081: // "runCommand()", me.getMessage(), me);
082: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", me);
083: throw new CommandException(getLocalizedString(
084: ERROR_JMX_INVOKE, new Object[] { "listSites" }), me);
085: } catch (ReflectionException re) {
086: //logger.logp(Level.SEVERE, "ListRobotSitesCommand",
087: // "runCommand()", re.getMessage(), re);
088: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", re);
089: throw new CommandException(getLocalizedString(
090: ERROR_MBEAN_REFLECTION_ERROR,
091: new Object[] { "listSites" }), re);
092: } catch (CommandException ce) {
093: //logger.logp(Level.SEVERE, "ListRobotSitesCommand",
094: // "runCommand()", ce.getMessage(), ce);
095: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", ce);
096: throw ce;
097: } catch (Exception ex) {
098: ex.printStackTrace();
099: //logger.logp(Level.SEVERE, "ListRobotSitesCommand",
100: // "runCommand()", ex.getMessage(), ex);
101: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", ex);
102: throw new CommandException(
103: getLocalizedString(COMMAND_FAILED), ex);
104: } finally {
105: closeMBeanServerConnection();
106: }
107:
108: }
109:
110: /**
111: * handles return value from mbean
112: * @param sites
113: */
114: protected void handleReturnValue(HashMap sites) {
115:
116: if (sites == null) {
117: //could mean no robot sites created, shouldn't print error
118: CLILogger.getInstance().printMessage(
119: getLocalizedString("robot.site.notfound"));
120: //CLILogger.getInstance().printMessage(getLocalizedString(COMMAND_FAILED));
121: } else {
122:
123: StringBuffer sb = new StringBuffer();
124: HashMap frs = null;
125:
126: if (!siteID.equals("")) { // single site display
127:
128: frs = (HashMap) sites.get(siteID);
129: if (frs != null) {
130: sb.append(getLocalizedString("robot.site.id",
131: new Object[] { siteID })
132: + ", ");
133: sb
134: .append(getLocalizedString(
135: "robot.site.name",
136: new Object[] { (String) frs
137: .get("SiteName") })
138: + ", ");
139: if (((Boolean) frs.get("State")).booleanValue()) {
140: sb
141: .append(getLocalizedString("robot.site.state")
142: + " "
143: + getLocalizedString("robot.site.enable")
144: + "\n");
145: } else {
146: sb
147: .append(getLocalizedString("robot.site.state")
148: + " "
149: + getLocalizedString("robot.site.disable")
150: + "\n");
151: }
152: sb.append("\n");
153:
154: LinkedList domains = (LinkedList) frs
155: .get("DomainGroup");
156: if (domains.size() > 0) {
157: sb
158: .append(getLocalizedString("robot.site.domaingroup")
159: + "\n");
160: for (int i = 0; i < domains.size(); i++) {
161: String domain = (String) domains.get(i);
162: sb.append(getLocalizedString(
163: "robot.site.domain",
164: new Object[] { domain })
165: + "\n");
166: }
167: sb.append("\n");
168: }
169:
170: LinkedList servers = (LinkedList) frs
171: .get("ServerGroup");
172: if (servers.size() > 0) {
173: sb
174: .append(getLocalizedString("robot.site.servergroup")
175: + "\n");
176: for (int i = 0; i < servers.size(); i++) {
177: HashMap server = (HashMap) servers.get(i);
178: sb.append(getLocalizedString(
179: "robot.site.server",
180: new Object[] { (String) server
181: .get("Server") })
182: + ", ");
183: sb.append(getLocalizedString(
184: "robot.site.port",
185: new Object[] { (String) server
186: .get("Port") })
187: + ", ");
188: String proto = (String) server
189: .get("Protocol");
190: sb
191: .append(getLocalizedString(
192: "robot.site.type",
193: new Object[] { getProtocolType(proto) })
194: + "\n");
195: }
196: sb.append("\n");
197: }
198:
199: LinkedList starts = (LinkedList) frs
200: .get("StartingPoints");
201: if (starts.size() > 0) {
202: sb
203: .append(getLocalizedString("robot.site.startingpoints")
204: + "\n");
205: for (int j = 0; j < starts.size(); j++) {
206: HashMap start = (HashMap) starts.get(j);
207: sb.append(getLocalizedString(
208: "robot.site.url",
209: new Object[] { (String) start
210: .get("URLString") })
211: + "\n");
212: //sb.append(getLocalizedString("robot.site.url", new Object[] {(String)start.get("URLString")}) + ", ");
213: //sb.append(getLocalizedString("robot.site.depth", new Object[] {(String)start.get("Depth")}) + "\n");
214: }
215: sb.append("\n");
216: }
217:
218: LinkedList filters = (LinkedList) frs
219: .get("FilterGroup");
220: if (filters.size() > 0) {
221: sb
222: .append(getLocalizedString("robot.site.filterdef")
223: + "\n");
224: for (int k = 0; k < filters.size(); k++) {
225: HashMap filter = (HashMap) filters.get(k);
226: sb.append(getLocalizedString(
227: "robot.site.filterid",
228: new Object[] { (String) filter
229: .get("FilterID") })
230: + ", ");
231: String nickkey = (String) filter
232: .get("FilterNickname");
233: String nickname = getLocalizedString(nickkey);
234: sb.append(getLocalizedString(
235: "robot.site.filternick",
236: new Object[] { nickname })
237: + ", ");
238: String match = (String) filter
239: .get("FilterMatch");
240: if (match.equalsIgnoreCase("deny"))
241: sb
242: .append(getLocalizedString(
243: "robot.site.filtermatch",
244: new Object[] { getLocalizedString("robot.site.filterexclude") })
245: + "\n");
246: else
247: sb
248: .append(getLocalizedString(
249: "robot.site.filtermatch",
250: new Object[] { getLocalizedString("robot.site.filterinclude") })
251: + "\n");
252: }
253: sb.append("\n");
254: }
255:
256: sb
257: .append(getLocalizedString(
258: "robot.site.desc",
259: new Object[] { (String) frs
260: .get("Comments") })
261: + "\n");
262: sb
263: .append(getLocalizedString(
264: "robot.site.dns",
265: new Object[] { (String) frs
266: .get("DNSTrans") })
267: + "\n");
268: }
269:
270: } else { // all sites display
271:
272: Iterator it = sites.keySet().iterator();
273: while (it.hasNext()) {
274: siteID = (String) it.next();
275: frs = (HashMap) sites.get(siteID);
276: if (frs != null) {
277: sb.append(getLocalizedString("robot.site.id",
278: new Object[] { siteID })
279: + ", ");
280: sb.append(getLocalizedString("robot.site.name",
281: new Object[] { (String) frs
282: .get("SiteName") })
283: + ", ");
284:
285: LinkedList domains = (LinkedList) frs
286: .get("DomainGroup");
287: if (domains.size() > 0) {
288: sb
289: .append(getLocalizedString("robot.site.type.domain")
290: + ", ");
291: } else {
292: sb
293: .append(getLocalizedString("robot.site.type.server")
294: + ", ");
295: }
296:
297: if (((Boolean) frs.get("State")).booleanValue()) {
298: sb
299: .append(getLocalizedString("robot.site.state")
300: + " "
301: + getLocalizedString("robot.site.enable")
302: + "\n");
303: } else {
304: sb
305: .append(getLocalizedString("robot.site.state")
306: + " "
307: + getLocalizedString("robot.site.disable")
308: + "\n");
309: }
310: }
311: }
312: } // end of all filters display
313:
314: CLILogger.getInstance().printMessage(sb.toString());
315: } // sites != null
316:
317: }
318:
319: public String getProtocolType(String proto) {
320: if (proto.compareToIgnoreCase("http") == 0)
321: return getLocalizedString("robot.site.type.http");
322: if (proto.compareToIgnoreCase("file") == 0)
323: return getLocalizedString("robot.site.type.file");
324: if (proto.compareToIgnoreCase("ftp") == 0)
325: return getLocalizedString("robot.site.type.ftp");
326: if (proto.compareToIgnoreCase("https") == 0)
327: return getLocalizedString("robot.site.type.https");
328: else
329: return "";
330: }
331:
332: }
|