001: package com.sun.portal.admin.cli.commands.rewriter;
002:
003: //PS Admin Common
004: import com.sun.portal.admin.common.util.AdminClientUtil;
005: import com.sun.portal.admin.common.PSMBeanException;
006:
007: //JMX
008: import javax.management.MBeanServerConnection;
009: import javax.management.ObjectName;
010: import javax.management.InstanceNotFoundException;
011: import javax.management.MBeanException;
012: import javax.management.MalformedObjectNameException;
013:
014: import java.io.*;
015: import java.util.LinkedList;
016:
017: import com.sun.enterprise.cli.framework.*;
018: import com.sun.portal.admin.cli.commands.AdminBaseCommand;
019:
020: public class StoreRuleSetSubcommand extends AdminBaseCommand {
021:
022: public boolean validateOptions() throws CommandValidationException {
023: return super .validateOptions();
024: }
025:
026: public void runCommand() throws CommandException,
027: CommandValidationException {
028: if (!validateOptions()) {
029: throw new CommandValidationException(
030: getLocalizedString(ERROR_OPTION_VALIDATION_FAILED));
031: }
032: String subcommandName = getName();
033:
034: if (subcommandName == null) {
035: CLILogger.getInstance().printMessage(
036: getLocalizedString(COMMAND_FAILED));
037: return;
038: }
039: String method = "";
040:
041: try {
042: Object params[] = { getRuleSetXML() };
043: String signature[] = { "java.lang.String" };
044:
045: MBeanServerConnection msc = getMBeanServerConnection(
046: getUserId(), getPassword(), getHost());
047:
048: LinkedList path = new LinkedList();
049: path.addFirst(getDomainId());
050: path.addFirst("rewriter");
051:
052: ObjectName objName = AdminClientUtil
053: .getResourceMBeanObjectName(
054: AdminClientUtil.REWRITER_MBEAN_TYPE, path);
055: if (subcommandName
056: .equalsIgnoreCase("create-rewriter-ruleset"))
057: method = "storeRule";
058: else if (subcommandName
059: .equalsIgnoreCase("set-rewriter-ruleset"))
060: method = "setRule";
061:
062: Object returnValue = msc.invoke(objName, method, params,
063: signature);
064: if (!returnValue.toString().equalsIgnoreCase("false")
065: && !returnValue.toString().equalsIgnoreCase("true"))
066: // CLILogger.getInstance().printMessage(getLocalizedString(COMMAND_FAILED));
067: {
068: handleReturnValue(returnValue);
069: }
070: closeMBeanServerConnection();
071: } catch (InstanceNotFoundException ie) {
072: //System.out.println("InstanceNotFoundException ");
073: throw new CommandException(getLocalizedString(
074: ERROR_MBEAN_REFLECTION_ERROR,
075: new Object[] { method }), null);
076: } catch (MBeanException me) {
077: PSMBeanException pme = (PSMBeanException) me
078: .getTargetException();
079: throw new CommandException(getLocalizedString(pme
080: .getErrorKey(), pme.getTokens()));
081: } catch (MalformedObjectNameException mle) {
082: //System.out.println("MalformedObjectNameException ");
083: mle.printStackTrace();
084: throw new CommandException(
085: getLocalizedString(ERROR_OBJECT_NAME), null);
086: } catch (CommandException ce) {
087: //System.out.println("CommandException ");
088: ce.printStackTrace();
089: throw ce;
090: } catch (PSMBeanException pme) {
091: throw new CommandException(getLocalizedString(pme
092: .getErrorKey(), pme.getTokens()));
093: } catch (Exception ex) {
094: //System.out.println("Exception ");
095: ex.printStackTrace();
096: throw new CommandException(
097: getLocalizedString(COMMAND_FAILED), null);
098: }
099:
100: }
101:
102: protected String getRuleSetID() {
103: String id = getOption("rule");
104: if (id == null)
105: id = "";
106: return id;
107: }
108:
109: protected String getRuleSetXML() throws PSMBeanException {
110: String filename = getOption("file");
111: if (filename == null)
112: filename = "";
113: String lResult = "";
114: try {
115: if ((new File(filename)).exists()) {
116: BufferedReader buffReader = new BufferedReader(
117: new FileReader(filename));
118: StringBuffer sb = new StringBuffer();
119: char data[] = new char[2048];
120:
121: for (int count = 0; (count = buffReader.read(data)) != -1;)
122: sb.append(data, 0, count);
123:
124: buffReader.close();
125: lResult = sb.toString();
126: } else {
127: String token[] = { filename };
128: throw new PSMBeanException(
129: "psadmin.error.rewriter.filedoesntexists",
130: token);
131: }
132: } catch (IOException e) {
133:
134: }
135: return lResult;
136: }
137: }
|