001: /*
002: * Copyright 2005 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: */
013: package com.sun.portal.admin.cli.commands.search;
014:
015: import java.io.*;
016: import java.lang.*;
017: import java.util.*;
018: import java.text.*;
019:
020: // JMX
021: import javax.management.MBeanServerConnection;
022: import javax.management.ObjectName;
023: import javax.management.InstanceNotFoundException;
024: import javax.management.MBeanException;
025: import javax.management.ReflectionException;
026: import javax.management.MalformedObjectNameException;
027: import javax.management.RuntimeOperationsException;
028:
029: // PS Admin Common
030: import com.sun.portal.admin.common.util.AdminClientUtil;
031:
032: // CLI framework
033: import com.sun.enterprise.cli.framework.*;
034:
035: // Base Class
036: import com.sun.portal.admin.cli.commands.GenericCommand;
037:
038: /**
039: * This class implements the psadmin list-search-databases subcommand. The
040: * list-search-databases subcommand calls the DatabaseMBean and shows a list of
041: * search databases.
042: */
043: public class ListDatabasesCommand extends GenericCommand {
044:
045: private static final String DATABASE_LABEL = "psadmin.subcommand.resource.list-search-databases.database.label";
046:
047: /**
048: * An abstract method that executes the command
049: * @throws CommandException
050: * @throws CommandValidationException
051: */
052: public void runCommand() throws CommandException,
053: CommandValidationException {
054: validateOptions();
055: validateSearchServerID();
056:
057: StringBuffer sb = new StringBuffer();
058:
059: try {
060: LinkedList path = new LinkedList();
061: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
062: path.addFirst(getSearchServerId());
063: path.addFirst("database");
064: ObjectName on = AdminClientUtil.getResourceMBeanObjectName(
065: AdminClientUtil.SEARCH_DATABASE_MBEAN_TYPE, path);
066:
067: MBeanServerConnection msc = getMBeanServerConnection(
068: getUserId(), getPassword(), getHost());
069:
070: Object[] params = {};
071: String[] signatures = {};
072: ArrayList data = (ArrayList) msc.invoke(on, "retrieveAll",
073: params, signatures);
074:
075: /*
076: ** There is a weird duplicate databases bug. This has been reported by QA
077: ** but I have never seen it myself. But I have added the code below to
078: ** insure that no duplicate databases are displayed.
079: */
080: ArrayList al = new ArrayList();
081: for (int index = 0; index < data.size(); index++) {
082: String name = (String) data.get(index);
083: if (!al.contains(name)) {
084: al.add(name);
085: }
086: }
087:
088: for (int index = 0; index < al.size(); index++) {
089: sb.append((String) al.get(index) + "\n");
090: }
091: } catch (InstanceNotFoundException ie) {
092: throw new CommandException(getLocalizedString(
093: ERROR_MBEAN_INSTANCE_NOT_FOUND,
094: new Object[] { "getAttribute:All" }), ie);
095: } catch (MBeanException me) {
096: throw new CommandException(getLocalizedString(
097: ERROR_JMX_INVOKE,
098: new Object[] { "getAttribute:All" }), me);
099: } catch (ReflectionException re) {
100: throw new CommandException(getLocalizedString(
101: ERROR_MBEAN_REFLECTION_ERROR,
102: new Object[] { "getAttribute:All" }), re);
103: } catch (CommandException ce) {
104: throw ce;
105: } catch (Exception e) {
106: throw new CommandException(
107: getLocalizedString(COMMAND_FAILED), e);
108: } finally {
109: closeMBeanServerConnection();
110: }
111:
112: CLILogger.getInstance().printMessage(sb.toString());
113: }
114:
115: }
|