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;
013:
014: import java.text.MessageFormat;
015: import java.io.File;
016:
017: //JMX
018: import javax.management.MBeanServerConnection;
019: import javax.management.ObjectName;
020: import javax.management.InstanceNotFoundException;
021: import javax.management.MBeanException;
022: import javax.management.ReflectionException;
023: import javax.management.MalformedObjectNameException;
024:
025: //PS Admin Common
026: import com.sun.portal.admin.common.util.AdminClientUtil;
027: import com.sun.portal.util.FileWildFilter;
028:
029: //CLI framework
030: import com.sun.enterprise.cli.framework.*;
031:
032: /**
033: * This class implements the psadmin configure-instances subcommand.
034: * This CLI is meant as a private CLI used only during configuration
035: * The configure-instances subcommand calls the PortalMBean:configureInstances call
036: * for all instances that belong to that portal, which does the the following tasks:
037: * <UL>
038: * <LI>adds to instance server classpath entries specified in the conf/server.classpath if present
039: * <LI>add to instance jvm-options entries specified in the conf/jvmoptions.properties if present
040: * <LI>add to instance server.policy entries specified in the conf/server.policy if present
041: * <LI>adds to instance jdbc resources specified in the conf/*.datasource if present
042: * <LI>adds to instance jndi lookups specified in the conf/*.jndi if present
043: * <UL>
044: * Note: All configuration should check if entries being configured already exist.
045: * Note: These configuration require a webcontainer restart before coming into affect
046: */
047:
048: public class ConfigureInstancesCommand extends AdminBaseCommand {
049:
050: // Option definitions
051: public static final String OPT_CONF_DIR = "confdir";
052:
053: //private member variables
054: private File m_confDir;
055:
056: /**
057: * Executes the subcommand.
058: *
059: * @exception com.sun.enterprise.cli.framework.CommandException If error occurrs during the execution.
060: */
061: public void runCommand() throws CommandException,
062: CommandValidationException {
063: validateOptions();
064: validatePortalId();
065: validateConfiguration();
066:
067: String operation = "";
068:
069: try {
070:
071: // Get the MBean server connection
072: MBeanServerConnection msc = getMBeanServerConnection(
073: getUserId(), getPassword(), getHost());
074:
075: // Get the MBean object for the portal
076: ObjectName pObjName = AdminClientUtil
077: .getPortalMBeanObjectName(
078: AdminClientUtil.DEFAULT_DOMAIN,
079: getPortalId());
080:
081: // Setting the params and signature
082: Object[] params = new Object[] { m_confDir
083: .getAbsolutePath() };
084: String[] signature = new String[] { "java.lang.String" };
085: operation = "configureInstances";
086:
087: // Invoke the configureInstances operation on the PortalMBean
088: // This method returns a boolean true in case of successful configuration
089: Boolean ret = (Boolean) msc.invoke(pObjName, operation,
090: params, signature);
091: boolean success = ret.booleanValue();
092: if (!success) {
093: throw new CommandException(
094: getLocalizedString(ERROR_INSTANCE_CONF_FAILED));
095: }
096:
097: } catch (InstanceNotFoundException ie) {
098: Object tok[] = { operation };
099: throw new CommandException(getLocalizedString(
100: ERROR_MBEAN_INSTANCE_NOT_FOUND, tok), ie);
101: } catch (MBeanException me) {
102: Object tok[] = { operation };
103: throw new CommandException(getLocalizedString(
104: ERROR_JMX_INVOKE, tok), me);
105: } catch (ReflectionException re) {
106: Object tok[] = { operation };
107: throw new CommandException(getLocalizedString(
108: ERROR_MBEAN_REFLECTION_ERROR, tok), re);
109: } catch (MalformedObjectNameException mle) {
110: throw new CommandException(
111: getLocalizedString(ERROR_OBJECT_NAME), mle);
112: } catch (CommandException ce) {
113: throw ce;
114: } catch (Exception ex) {
115: throw new CommandException(
116: getLocalizedString(COMMAND_FAILED), ex);
117: } finally {
118: closeMBeanServerConnection();
119: }
120: }
121:
122: protected void validateConfiguration() throws CommandException {
123: String confDir = getOption(OPT_CONF_DIR);
124: m_confDir = new File(confDir);
125: //If /conf does not exist or is not directory
126: if (!m_confDir.exists() || !m_confDir.isDirectory()) {
127: Object tokens[] = new Object[] { confDir };
128: throw new CommandException(getLocalizedString(
129: ERROR_INVALID_INSTANCE_CONF_DIR, tokens));
130: }
131:
132: boolean doConfig = false;
133:
134: //Does conf/server.classpath exist?
135: if (!doConfig) {
136: doConfig = doConfigFilesExist("server.classpath");
137: }
138:
139: //Does conf/jvmoptions.properties exist?
140: if (!doConfig) {
141: doConfig = doConfigFilesExist("jvmoptions.properties");
142: }
143:
144: //Does conf/server.policy exist?
145: if (!doConfig) {
146: doConfig = doConfigFilesExist("server.policy");
147: }
148:
149: //Does conf/*.datasource exist?
150: if (!doConfig) {
151: doConfig = doConfigFilesExist("[\\S|\\s]*\\.datasource");
152: }
153:
154: //Does conf/*.jndi exist?
155: if (!doConfig) {
156: doConfig = doConfigFilesExist("[\\S|\\s]*\\.jndi");
157: }
158:
159: //if doConfig is still false then nothing to validate
160: if (!doConfig) {
161: Object tokens[] = new Object[] { confDir };
162: throw new CommandException(getLocalizedString(
163: ERROR_INSTANCE_CONF_NOT_REQD, tokens));
164: }
165: }
166:
167: private boolean doConfigFilesExist(String filesRegEx) {
168: FileWildFilter filter = new FileWildFilter(filesRegEx, null,
169: FileWildFilter.ONLY_FILE);
170: File[] files = m_confDir.listFiles(filter);
171: if (files != null && files.length != 0)
172: return true;
173: else
174: return false;
175: }
176:
177: }
|