001: /*
002: Copyright (C) 2004 David Bucciarelli (davibu@interfree.it)
003:
004: This program is free software; you can redistribute it and/or
005: modify it under the terms of the GNU General Public License
006: as published by the Free Software Foundation; either version 2
007: of the License, or (at your option) any later version.
008:
009: This program is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: GNU General Public License for more details.
013:
014: You should have received a copy of the GNU General Public License
015: along with this program; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: */
018:
019: package org.homedns.dade.jcgrid.cmd;
020:
021: import java.io.*;
022: import java.util.*;
023:
024: import org.apache.log4j.*;
025: import org.apache.commons.cli.*;
026:
027: import org.homedns.dade.jcgrid.*;
028: import org.homedns.dade.jcgrid.server.*;
029: import org.homedns.dade.jcgrid.worker.*;
030: import org.homedns.dade.jcgrid.client.*;
031: import org.homedns.dade.jcgrid.admin.*;
032:
033: public class MainCmd {
034: public MainCmd() {
035: }
036:
037: public static void setUpLog4J(String name, boolean consoleOutput)
038: throws Exception {
039: String log4jCfgFileName = System.getProperty("user.dir")
040: + File.separator + "log4j.cfg";
041: File log4jCfgFile = new File(log4jCfgFileName);
042: if (log4jCfgFile.exists())
043: PropertyConfigurator.configureAndWatch(log4jCfgFileName,
044: 15 * 1000);
045: else {
046: // The cfg file doesn't exist so we are going to use the default
047: // configuration
048:
049: BasicConfigurator.configure();
050:
051: Logger.getLogger("org.homedns.dade.jcgrid").setLevel(
052: Level.WARN);
053: Logger.getLogger("DETAIL.org.homedns.dade.jcgrid")
054: .setLevel(Level.WARN);
055:
056: Logger.getRootLogger().removeAllAppenders();
057: Appender app = new RollingFileAppender(new PatternLayout(
058: "%-4r %d [%t] %-5p %x %m%n"), "jcgrid-" + name
059: + ".log", true);
060: Logger.getRootLogger().addAppender(app);
061:
062: if (consoleOutput) {
063: Appender capp = new ConsoleAppender(new PatternLayout(
064: "%-4r %d [%t] %-5p %x %m%n"));
065: Logger.getRootLogger().addAppender(capp);
066: }
067: }
068: }
069:
070: public static CommandLine parseCommonOptions(Options options,
071: GridNodeConfig nodeConfig, String[] args) throws Exception {
072: // Parse common command line options
073:
074: options.addOption("s", true, "set server address");
075: options.addOption("p", true,
076: "set server client communication port");
077: options.addOption("k", true,
078: "set server woker communication port");
079: options.addOption("a", true,
080: "set server admin. communication port");
081: options.addOption("w", true, "set server password");
082: options.addOption("d", true, "set working directory");
083: options.addOption("e", false, "enable secure connection");
084: options.addOption("z", false, "enable compressed connection");
085:
086: if (GridNodeConfig.TYPE_WORKER.equals(nodeConfig.getType()))
087: options.addOption("c", true,
088: "set the number of workers to run");
089:
090: if ((GridNodeConfig.TYPE_WORKER.equals(nodeConfig.getType()))
091: || (GridNodeConfig.TYPE_CLIENT.equals(nodeConfig
092: .getType()))
093: || (GridNodeConfig.TYPE_ADMIN.equals(nodeConfig
094: .getType())))
095: options.addOption("n", true, "set server session name");
096:
097: CommandLineParser parser = new PosixParser();
098: CommandLine cmd = parser.parse(options, args);
099:
100: GridConfig config = nodeConfig.getGridConfig();
101: if (cmd.hasOption("s"))
102: config.setServerAddress(cmd.getOptionValue("s"));
103: if (cmd.hasOption("p"))
104: config.setServerClientPort(Integer.parseInt(cmd
105: .getOptionValue("p")));
106: if (cmd.hasOption("k"))
107: config.setServerWorkerPort(Integer.parseInt(cmd
108: .getOptionValue("k")));
109: if (cmd.hasOption("a"))
110: config.setServerAdminPort(Integer.parseInt(cmd
111: .getOptionValue("a")));
112: if (cmd.hasOption("w"))
113: config.setServerPassword(cmd.getOptionValue("w"));
114: if (cmd.hasOption("e"))
115: config.setUseSecureConnection(true);
116: if (cmd.hasOption("z"))
117: config.setUseCompression(true);
118:
119: if (cmd.hasOption("d"))
120: nodeConfig.setWorkingDir(cmd.getOptionValue("d"));
121:
122: if (cmd.hasOption("?")) {
123: HelpFormatter formatter = new HelpFormatter();
124: formatter.printHelp("JCGrid command", options);
125:
126: System.exit(0);
127: }
128:
129: if (GridNodeConfig.TYPE_WORKER.equals(nodeConfig.getType())) {
130: GridNodeWorkerConfig wCfg = (GridNodeWorkerConfig) nodeConfig;
131:
132: if (cmd.hasOption("c"))
133: wCfg.setWorkerCount(Integer.parseInt(cmd
134: .getOptionValue("c")));
135: }
136:
137: if ((GridNodeConfig.TYPE_WORKER.equals(nodeConfig.getType()))
138: || (GridNodeConfig.TYPE_CLIENT.equals(nodeConfig
139: .getType()))
140: || (GridNodeConfig.TYPE_ADMIN.equals(nodeConfig
141: .getType()))) {
142: GridNodeGenericConfig gCfg = (GridNodeGenericConfig) nodeConfig;
143:
144: if (cmd.hasOption("n"))
145: gCfg.setSessionName(cmd.getOptionValue("n"));
146: }
147:
148: return cmd;
149: }
150: }
|