/*
* Copyrights : CNRS
* Author : Oleg Lodygensky
* Acknowledgment : XtremWeb-HEP is based on XtremWeb 1.8.0 by inria : http://www.xtremweb.net/
* Web : http://www.xtremweb-hep.org
*
* This file is part of XtremWeb-HEP.
*
* XtremWeb-HEP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* XtremWeb-HEP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XtremWeb-HEP. If not, see .
*
*/
package xtremweb.client;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.UnknownHostException;
import java.security.AccessControlException;
import java.security.InvalidKeyException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateExpiredException;
import java.text.ParseException;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.net.ssl.SSLHandshakeException;
import org.xml.sax.SAXException;
import xtremweb.common.AppInterface;
import xtremweb.common.AppTypeEnum;
import xtremweb.common.CPUEnum;
import xtremweb.common.CommandLineOptions;
import xtremweb.common.CommandLineParser;
import xtremweb.common.CommonVersion;
import xtremweb.common.DataInterface;
import xtremweb.common.DataTypeEnum;
import xtremweb.common.GroupInterface;
import xtremweb.common.Logger;
import xtremweb.common.LoggerLevel;
import xtremweb.common.MD5;
import xtremweb.common.MileStone;
import xtremweb.common.OSEnum;
import xtremweb.common.SessionInterface;
import xtremweb.common.StatusEnum;
import xtremweb.common.StreamIO;
import xtremweb.common.Table;
import xtremweb.common.UID;
import xtremweb.common.UserGroupInterface;
import xtremweb.common.UserInterface;
import xtremweb.common.UserRightEnum;
import xtremweb.common.Version;
import xtremweb.common.WorkInterface;
import xtremweb.common.XMLValue;
import xtremweb.common.XMLVector;
import xtremweb.common.XMLable;
import xtremweb.common.XWAccessRights;
import xtremweb.common.XWConfigurator;
import xtremweb.common.XWPropertyDefs;
import xtremweb.common.XWReturnCode;
import xtremweb.common.XWRole;
import xtremweb.common.XWTools;
import xtremweb.common.Zipper;
import xtremweb.communications.CommClient;
import xtremweb.communications.Connection;
import xtremweb.communications.IdRpc;
import xtremweb.communications.SmartSocketsProxy;
import xtremweb.communications.URI;
import xtremweb.communications.XMLRPCCommand;
import xtremweb.communications.XMLRPCCommandGet;
import xtremweb.communications.XMLRPCCommandGetApps;
import xtremweb.communications.XMLRPCCommandGetDatas;
import xtremweb.communications.XMLRPCCommandGetGroupWorks;
import xtremweb.communications.XMLRPCCommandGetGroups;
import xtremweb.communications.XMLRPCCommandGetHosts;
import xtremweb.communications.XMLRPCCommandGetSessions;
import xtremweb.communications.XMLRPCCommandGetTasks;
import xtremweb.communications.XMLRPCCommandGetTraces;
import xtremweb.communications.XMLRPCCommandGetUserGroups;
import xtremweb.communications.XMLRPCCommandGetUsers;
import xtremweb.communications.XMLRPCCommandGetWorks;
import xtremweb.communications.XMLRPCCommandRemove;
import xtremweb.communications.XMLRPCCommandSend;
import xtremweb.communications.XWPostParams;
/**
* Created: Oct 1st, 2003
*
* This class describes a generic client to XtremWeb. It is designed to submit,
* delete jobs as to get job status and results.
*
* Examples can be found in CommandLineParser.
*
* @see xtremweb.common.CommandLineParser
*
* @author Oleg Lodygensky
*/
public final class Client {
/**
* This is the output stream
*
* @since 7.0.0
*/
private PrintStream out;
/**
* This is the logger
*
* @since 7.0.0
*/
private final Logger logger;
/**
* This stores and tests the command line parameters
*/
private CommandLineParser args;
/**
* This is needed to call this class methods from GUI
*/
public void setArguments(CommandLineParser a) {
args = a;
}
/**
* This stores client config such as login, password, server addr etc.
*/
private XWConfigurator config;
public XWConfigurator getConfig() {
return config;
}
public void setConfig(XWConfigurator c) {
config = c;
}
/**
* This stores the macro file line number.
*/
private int macroLineNumber;
/**
* This is the macro file provided using --xwmacro para
*/
private File macroFile;
/**
* This is set to true if a macro file is provided using --xwmacro param
*/
private boolean executingMacro;
/**
* This is set to true if --xwshell param has been provided. The client then
* opens a socket and waits for incoming connection
*
* @since 7.0.0
*/
private boolean shellRunning;
/**
* This retrieves and initializes the default communication client
*
* @return the default communication client
* @exception IOException
* is thrown if cache directory can not be created or if we
* can't retrieve the default client
* @throws InstantiationException
*/
public CommClient commClient() throws IOException, InstantiationException {
CommClient client = commClients.get(Connection.xwScheme());
if (client == null) {
client = config.defaultCommClient();
}
client.setLoggerLevel(logger.getLoggerLevel());
commClients.put(Connection.xwScheme(), client);
client.setAutoClose(false);
return client;
}
/**
* This retrieves the comm client for the givenU RI and initializes it
*
* @param uri
* is the uri to retrieve comm client for
* @return the expected comm client
* @throws IOException
*/
public CommClient commClient(URI uri) throws IOException {
CommClient client = commClients.get(uri.getScheme());
if (client == null) {
try {
client = config.getCommClient(uri);
} catch (final InstantiationException e) {
throw new IOException("can't get client for " + uri);
}
}
commClients.put(uri.getScheme(), client);
client.setAutoClose(false);
return client;
}
/**
* This retrieves the data for the given URI by calling getData(uri, false)
*
* @see #getData(URI)URI, boolean)
* @param uri
* is the data uri
* @return the data
* @throws InstantiationException
* @throws AccessControlException
*/
private DataInterface getData(URI uri) throws IOException,
ClassNotFoundException, SAXException, InvalidKeyException,
URISyntaxException, AccessControlException, InstantiationException {
return getData(uri, false);
}
/**
* This retrieves the data for the given URI
*
* @param uri
* is the URI of the data to retrieve
* @param display
* tells to write data description to stdout
* @return
* @throws IOException
* @throws IllegalAccessException
* @throws SAXException
* @throws ClassNotFoundException
* @throws AccessControlException
* @throws InvalidKeyException
* @throws URISyntaxException
* @throws InstantiationException
*/
private DataInterface getData(URI uri, boolean display) throws IOException,
InvalidKeyException, AccessControlException,
ClassNotFoundException, SAXException, URISyntaxException,
InstantiationException {
XMLRPCCommandGet cmd = new XMLRPCCommandGet(uri);
DataInterface data = null;
CommClient commClient = null;
try {
data = (DataInterface) sendCommand(cmd, display);
if (data != null) {
return data;
}
commClient = commClient(uri);
data = (DataInterface) commClient.get(uri);
if (data == null) {
throw new ClassNotFoundException("can't retrieve data " + uri);
}
return data;
} finally {
cmd = null;
commClient = null;
}
}
/**
* This retrieves the data from server This does nothing if uri parameter is
* null.
*
* @param uri
* is the data uri
* @param download
* if false, data content is not downloaded if data already in
* cache; if true, data content is downloaded even if data
* already in cache
* @throws IOException
* @throws ClassNotFoundException
* @throws MalformedURLException
* @throws UnknownHostException
* @throws ConnectException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws URISyntaxException
* @throws SAXException
* @throws InvalidKeyException
* @throws AccessControlException
*/
private void downloadData(URI uri, boolean download)
throws ConnectException, UnknownHostException,
MalformedURLException, ClassNotFoundException, IOException,
InvalidKeyException, SAXException, URISyntaxException,
AccessControlException, InstantiationException {
if (uri == null) {
return;
}
if (uri.isHttp()) {
wget(uri, new URL(uri.toString()));
return;
}
// insure data is in cache
final DataInterface data = getData(uri);
if (data == null) {
logger.error("Cant retrieve data " + uri);
return;
}
if (download == false) {
return;
}
if (data.getSize() <= 0) {
logger.warn("" + uri + " not downloaded : size <= 0");
return;
}
final CommClient commClient = commClient(uri);
File fdata = null;
final UID uid = uri.getUID();
String fext = "";
if (data.getType() != null) {
fext = data.getType().getFileExtension();
}
String dataName = data.getName();
String fname = uid.toString();
if (dataName != null) {
dataName = dataName.replace('/', '_');
dataName = dataName.replace('\\', '_');
fname = uid.toString() + "_" + dataName;
}
fname += fext;
if (args.getOption(CommandLineOptions.OUT) != null) {
fname = (String) args.getOption(CommandLineOptions.OUT);
}
fdata = new File(fname);
if (data.getMD5() == null) {
throw new IOException(uri.toString() + " MD5 is not set");
}
logger.debug("Download uri = " + uri + " fdata = " + fdata);
commClient.downloadData(uri, fdata);
if (data.getMD5().compareTo(MD5.asHex(MD5.getHash(fdata))) != 0) {
fdata.delete();
throw new IOException(uri.toString() + " MD5 differs");
}
logger.info("Downloaded to : " + fname);
}
/**
* This does nothing if url parameter is null. This retrieves the data from
* an HTTP server Depending on download parameter, this downloads the data
* content, if not already in cache
*
* @param uri
* is the data uri
* @param url
* is the data url
* @throws IOException
* on cache error
*/
private void wget(URI uri, URL url) throws IOException {
if (url == null) {
return;
}
final CommClient commClient = commClient(uri);
commClient.addToCache(uri);
final String fname = url.getPath().replace('/', '_').replace(' ', '_');
final File fdata = new File(fname);
IOException ioe = null;
StreamIO io = null;
try {
mileStone.println("Reading file " + fdata);
io = new StreamIO(null, new DataInputStream(url.openStream()),
false);
io.readFileContent(fdata);
io.close();
mileStone.println("Read file " + fdata);
} catch (final IOException e) {
logger.exception(e);
ioe = e;
} finally {
if (io != null) {
io.close();
}
if (ioe != null) {
throw ioe;
}
}
}
/**
* This is the zip file
*/
private final Zipper zipper;
/**
* Don't forget to remove temp ZIP file when submitting job
*/
private boolean newZip = false;
/**
* This is the zip file name; amazing is it not?
*/
private String zipFileName;
/**
* This sets the logger level. This also sets the logger levels checkboxes
* menu item.
*/
public void setLoggerLevel(LoggerLevel l) {
logger.setLoggerLevel(l);
if (zipper != null) {
zipper.setLoggerLevel(l);
}
}
/**
* This aims to display some time stamps
*/
private MileStone mileStone;
/**
* This hashtable stores comm clients for each schema
*/
private final Hashtable commClients;
/**
* This is the default constructor
*/
private Client(final String[] a) throws ParseException {
final String[] argv = a.clone();
logger = new Logger(this);
out = System.out;
executingMacro = false;
shellRunning = false;
config = null;
macroLineNumber = 0;
macroFile = null;
zipper = new Zipper();
commClients = new Hashtable();
args = new CommandLineParser(argv);
if (args.help()
|| ((args.command() == IdRpc.NULL)
&& (args.getOption(CommandLineOptions.GUI) == null) && (args
.getOption(CommandLineOptions.MACRO) == null))) {
usage(args.command());
}
try {
config = (XWConfigurator) args.getOption(CommandLineOptions.CONFIG);
setLoggerLevel(config.getLoggerLevel());
} catch (final NullPointerException e) {
logger.exception(e);
if (args.getOption(CommandLineOptions.GUI) == null) {
logger.fatal("You must provide a config file, using \"--xwconfig\" !");
} else {
new MileStone(XWTools.split(new String()));
mileStone = new MileStone(Client.class);
}
}
}
/**
* This shows application usage
*/
private void usage(IdRpc idrpc) {
usage(idrpc, null);
}
/**
* This shows application usage
*
* @see xtremweb.common.CommandLineOptions#usage()
*/
private void usage(IdRpc idrpc, String msg) {
if ((idrpc == null) || (idrpc == IdRpc.NULL)) {
CommandLineParser
.usage("This is the XWHEP client to use and manage the platform ("
+ CommonVersion.getCurrent().full() + ")");
} else {
println("+--------- XWHEP client "
+ CommonVersion.getCurrent().full()
+ " ------------------------------------------+");
if (msg != null) {
println(" Error : " + msg);
}
println(" Usage : " + idrpc.helpClient());
println("+-----------------------------------------------------------------------------+\n");
}
if (shellRunning == false) {
System.exit(XWReturnCode.PARSING.ordinal());
}
}
/**
* This summarizes action details to stdout
*
* @see xtremweb.common.CommandLineOptions#verbose()
*/
private void verbose() {
println(args.isVerbose(), "server = " + config.getCurrentDispatcher());
println(args.isVerbose(), "login = " + config.getUser().getLogin());
args.verbose();
}
/**
* This sets the output stream
*
* @since 7.0.0
*/
public void setPrintStream(PrintStream p) {
out = p;
}
/**
* This prints out a string, if expected.
* Output is formated accordingly to args.outputFormat
.
*
* @see #args
* @param write
* tells whether to write or not
* @param str
* is the string to eventually print out
*/
private void println(boolean write, String str) {
if (write == false) {
return;
}
if (!args.html()) {
out.println(str);
return;
}
final Collection array = XWTools.split(str, "\t ,;");
for (final Iterator iter = array.iterator(); iter.hasNext();) {
out.println("" + iter.next() + " | ");
}
array.clear();
}
/**
* This prints out a string if allowed(--verbose option)
*
* @param str
* is the string to eventually print out
*/
private void println(String str) {
println(true, str);
}
/**
* This prints out a string if allowed(--verbose option)
*
* @param str
* is the string to eventually print out
*/
private void println(URI uri) {
println(true, uri.toString());
}
/**
* This prints a message to std err and exits.
*
* @param msg
* is the message to print to stderr
* @param code
* is the return code to use on exit
*/
private void exit(String msg, XWReturnCode code) {
if (newZip && (args.getOption(CommandLineOptions.KEEPZIP) == null)) {
File file = new File(zipFileName);
if (file.exists()) {
file.delete();
}
file = null;
}
if (code != XWReturnCode.SUCCESS) {
logger.error(msg);
} else {
logger.info(msg);
}
if (shellRunning) {
println(msg);
}
try {
final CommClient client = commClient();
client.setAutoClose(true);
commClient().close();
} catch (final Exception e) {
}
if (shellRunning == false) {
System.exit(code.ordinal());
}
}
/**
* This is called on communication error This may occur if requested object
* if not found This terminates this application.
*
* @param uri
* is the not found object URI
* @since 5.8.0
*/
private void objectNotFound() {
if (executingMacro == false) {
exit("object not found", XWReturnCode.NOTFOUND);
}
}
/**
* This is called on communication error This may occur if user don't hae
* the right to execute the command, or if requested object if not found
* This terminates this application.
*/
private void connectionRefused() {
exit("connection refused", XWReturnCode.CONNECTION);
}
/**
* This is called when XtremWeb server is not reachable. This terminates
* this application.
*/
private void handshakeError() {
exit("Encryption error : bad public key", XWReturnCode.HANDSHAKE);
}
/**
* This tells whether XMLable.XMLHEADER has been printed
*/
private boolean xmlHeaderPrinted = false;
/**
* This prints XMLable.XMLHEADER to stdout if not already printed
*/
private void printXMLHeader() {
if (xmlHeaderPrinted == false) {
println(XMLable.XMLHEADER);
xmlHeaderPrinted = true;
}
}
/**
* This inserts an HTML header This displays nothing on any other output
* format. Header is only inserted if there is no macro or if force is true
*
* @param force
* forces output if true
*/
private void header(IdRpc cmd) {
if (args.xml()) {
printXMLHeader();
println(cmd.toXml());
}
if (args.html() == false) {
return;
}
final Date currentDate = new Date();
println("XWHEP Client " + cmd.toString() + "("
+ currentDate + ")" + "");
println("XWHEP Client " + cmd.toString() + "(" + currentDate + ")"
+ "
");
}
/**
* This inserts a trailer, depending on output format This displays nothing
* on any other output format. Trailer is only inserted if there is no macro
* or if force is true
*
* @param force
* forces output if true
*/
private void trailer(IdRpc cmd) {
if (args.xml()) {
println(cmd.toXml(true));
}
if (args.html()) {
println("
");
}
}
/**
* This writes out an HTML and XML tag. Does nothing for any other format.
*
* @param t
* is the ML tag
* @param open
* is true to open the tags, false to close it
*/
private void tag(boolean open, String t) {
if (args.html() || args.xml()) {
if ((t != null) && (t.length() > 0)) {
println(true, "<" + (open == true ? "" : "/") + t + ">");
}
}
}
/**
* This starts a new line, depending on output format. For HTML format, this
* opens a TR tag; for XML format, this opens the provided tag. This
* displays nothing on any other output format.
*
* @param t
* is the XML tag to open, if any
*/
private void startLine(String t) {
String str = t;
if (args.html()) {
str = "tr";
}
if (args.html() || args.xml()) {
tag(true, str);
}
}
/**
* This opens a new HTML table row. This does nothing if output format is
* not HTML.
*/
private void startLine() {
startLine(null);
}
/**
* This ends a line, depending on output format. For HTML format, this
* closes a TR tag; for XML format, this closes the provided tag. This
* displays nothing on any other output format.
*
* @param t
* is the XML tag to open
*/
private void endLine(String t) {
String str = t;
if (args.html()) {
str = "tr";
}
if (args.html() || args.xml()) {
tag(false, str);
}
}
/**
* This closes an HTML table row. This does nothing if output format is not
* HTML.
*/
private void endLine() {
endLine(null);
}
/**
* This sends command to XtremWeb server, read answer and eventually write
* retrieved object to stdout
*
* @param command
* is the XMLRPC command to send
* @param display
* tells to write downloaded object to terminal
* @return an XMLable object or null
* @throws SAXException
* @throws ClassNotFoundException
* @throws IOException
* @throws AccessControlException
* @throws InvalidKeyException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public XMLable sendCommand(XMLRPCCommand command, boolean display)
throws InvalidKeyException, AccessControlException, IOException,
ClassNotFoundException, SAXException, InstantiationException {
XMLable result = null;
result = command.exec(commClient());
if (display) {
startLine();
if (args.xml()) {
println(true, result.toXml());
} else {
println(true,
result.toHexString(args.csv() || args.html(),
args.shortFormat()));
}
endLine();
}
return result;
}
/**
* This changes access rights Command line parameters :
* --xwchmod MOD UID | URI [UID | URI ...]
MOD follows unix
* chmod syntax :
*
* - [aoug][+-][rwx]
*
- an octal value (e.g. 777)
*
* URI | UID may represent any objects
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private void chmod() throws IOException, ClassNotFoundException,
ParseException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException,
InstantiationException, IllegalAccessException {
Collection params = (Collection) args.commandParams();
if (params == null) {
XWAccessRights.help(new PrintStream(System.out));
exit("no UID provided", XWReturnCode.PARSING);
}
final Iterator enumParams = params.iterator();
String modstr = (String) enumParams.next();
while (enumParams.hasNext()) {
Object param = enumParams.next();
URI uri = null;
if (param instanceof UID) {
uri = commClient().newURI((UID) param);
}
if (param instanceof URI) {
uri = (URI) param;
}
if (uri == null) {
param = null;
throw new ParseException("uri is null", 0);
}
Table obj = get(uri, false, true);
if (obj != null) {
XWAccessRights rights = obj.getAccessRights();
rights.chmod(modstr);
obj.setAccessRights(rights);
commClient().send(new XMLRPCCommandSend(uri, obj));
rights = null;
}
obj = null;
param = null;
}
modstr = null;
params.clear();
params = null;
}
/**
* This retrieves command line parameter and calls get(Vector)
* Command line parameters : --xwget URI | UID [ URI | UID...]
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws IllegalAccessException
* @throws InstantiationException
* @see #get(Vector)
*/
private void get() throws IOException, ClassNotFoundException,
SAXException, URISyntaxException, InvalidKeyException,
AccessControlException, InstantiationException,
IllegalAccessException {
Collection params = (Collection) args.commandParams();
get(params, true);
params.clear();
params = null;
}
/**
* This retrieves objects from server by calling get(URI, display, false)
* for each Vector element
*
* @param params
* is a Vector of UID/URI
* @param display
* tells to print found object or not
* @throws URISyntaxException
* @throws SAXException
* @throws IOException
* @throws AccessControlException
* @throws InvalidKeyException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws ClassNotFoundException
* @see #get(URI, boolean, boolean)
*/
private void get(Collection params, boolean display)
throws InvalidKeyException, AccessControlException, IOException,
SAXException, URISyntaxException, ClassNotFoundException,
InstantiationException {
if (params == null) {
throw new IOException("no URI provided");
}
header(IdRpc.GET);
boolean headoneshot = true;
boolean bypass = (args.command() == IdRpc.GETWORKS)
|| (args.command() == IdRpc.GETTASKS)
|| (args.command() == IdRpc.GETHOSTS);
if (args.command() == IdRpc.GET) {
bypass = true;
}
Iterator theEnum = params.iterator();
while (theEnum.hasNext()) {
Object param = theEnum.next();
Table obj = null;
try {
obj = get((URI) param, display, bypass);
} catch (final ClassCastException cce) {
// maybe it is an UID...
try {
obj = get((UID) param, display, bypass);
} catch (final ClassCastException e1) {
try {
obj = get((URI) param, display, bypass);
} catch (final ClassCastException e2) {
try {
// this may be the case if param has been retrieved
// from server
obj = get((UID) ((XMLValue) param).getValue(),
display, bypass);
} catch (final ClassCastException e3) {
obj = getApp((String) param);
if (obj == null) {
obj = getUser((String) param, false);
}
if (obj != null) {
get(obj.getUID(), display, bypass);
continue;
}
}
}
}
}
if ((obj == null)
|| (args.getOption(CommandLineOptions.DOWNLOAD) == null)) {
param = null;
continue;
}
if (headoneshot && (args.html() || args.csv())) {
println(true, new AppInterface().getColumns());
headoneshot = false;
}
URI dataUri = null;
StatusEnum jobStatus = StatusEnum.NONE;
boolean jobResult = false;
if (obj instanceof WorkInterface) {
dataUri = ((WorkInterface) obj).getResult();
jobStatus = ((WorkInterface) obj).getStatus();
jobResult = true;
} else if (obj instanceof DataInterface) {
dataUri = ((DataInterface) obj).getURI();
jobStatus = StatusEnum.COMPLETED;
}
switch (jobStatus) {
case COMPLETED:
if (dataUri != null) {
downloadData(dataUri, true);
}
break;
case RUNNING:
break;
}
if (jobResult
&& (args.getOption(CommandLineOptions.NOERASE) == null)) {
if (dataUri != null) {
remove(dataUri);
}
switch (jobStatus) {
case COMPLETED:
case ERROR:
logger.debug("Removing job " + obj.getUID());
remove(obj.getUID());
break;
}
}
dataUri = null;
jobStatus = null;
param = null;
obj = null;
}
theEnum = null;
if (display) {
trailer(IdRpc.GET);
}
}
/**
* This calls get(commClient.newUIR(uid), false, false);
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public Table get(UID uid) throws IOException, ClassNotFoundException,
SAXException, URISyntaxException, InvalidKeyException,
AccessControlException, InstantiationException {
URI uri = commClient().newURI(uid);
final Table ret = get(uri, false, false);
uri = null;
return ret;
}
/**
* This calls get(uri, false, false);
*
* @throws SAXException
* @throws IOException
* @throws URISyntaxException
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
*/
public Table get(URI uri) throws InvalidKeyException,
AccessControlException, IOException, SAXException,
URISyntaxException, InstantiationException {
return get(uri, false, false);
}
/**
* This calls get(newUIR(uid), display, bypass);
*
* @param uid
* @param display
* @param bypass
* @return
* @throws InstantiationException
* @throws IOException
* @throws URISyntaxException
* @throws SAXException
* @throws ClassNotFoundException
* @throws AccessControlException
* @throws InvalidKeyException
*/
private Table get(UID uid, boolean display, boolean bypass)
throws URISyntaxException, IOException, InvalidKeyException,
AccessControlException, ClassNotFoundException, SAXException,
InstantiationException {
URI uri = commClient().newURI(uid);
final Table ret = get(uri, display, bypass);
uri = null;
return ret;
}
/**
* This retrieves an object from XtremWeb server This may write description
* to stdout accordingly to display parameter Command line
* parameters : --xwget UID [UID...]
*
* @param uri
* is the application URI
* @param display
* tells whether to write object description to stdout
* @param bypass
* tells to use cache or not; if true cache is not used, if false
* cache is used
* @return an AppInterface
* @throws InstantiationException
* @throws IOException
* @throws URISyntaxException
* @throws SAXException
* @throws AccessControlException
* @throws InvalidKeyException
*/
private Table get(URI uri, boolean display, boolean bypass)
throws IOException, InvalidKeyException, AccessControlException,
SAXException, URISyntaxException, InstantiationException {
CommClient commClient = commClient();
Table ret = null;
if ((args.command() == IdRpc.GETTASKS)
|| (args.command() == IdRpc.GETTASK)) {
ret = commClient.getTask(uri, bypass);
} else {
ret = commClient.get(uri, bypass);
}
if (ret == null) {
commClient = null;
return null;
}
if (display) {
startLine();
String str = null;
if (args.xml()) {
str = ret.toXml();
} else {
str = ret.toString(args.csv() || args.html(),
args.shortFormat());
}
println(true, str);
str = null;
endLine();
}
return ret;
}
/**
* This retrieves application from XtremWeb server This may write app
* description to stdout, accordingly to dislay parameter
* Command line parameters : --xwgetapp UID [UID...]
*
* @param name
* is the application name
* @return an AppInterface
* @throws InstantiationException
* @throws URISyntaxException
* @throws SAXException
* @throws ClassNotFoundException
* @throws IOException
* @throws AccessControlException
* @throws InvalidKeyException
*/
public AppInterface getApp(String name) throws InvalidKeyException,
AccessControlException, IOException, ClassNotFoundException,
SAXException, URISyntaxException, InstantiationException {
return commClient().getApp(name, false);
}
/**
* This retrieves, stores and displays applications installed in XtremWeb
* server. Command line parameters : --xwgetapps
*
* @throws IllegalAccessException
* @throws SAXException
* @throws ClassNotFoundException
* @throws InstantiationException
* @throws IOException
* @throws URISyntaxException
* @throws AccessControlException
* @throws InvalidKeyException
* @see #getApps(boolean)
*/
public Collection getApps() throws InvalidKeyException,
AccessControlException, URISyntaxException, IOException,
InstantiationException, ClassNotFoundException, SAXException,
IllegalAccessException {
return getApps(false);
}
/**
* This retrieves, stores and displays applications installed in XtremWeb
* server. Command line parameters : --xwgetapps
*
* @param display
* is true to display applications
* @throws InstantiationException
* @throws IOException
* @throws URISyntaxException
* @throws IllegalAccessException
* @throws SAXException
* @throws ClassNotFoundException
* @throws AccessControlException
* @throws InvalidKeyException
*/
private Collection getApps(boolean display)
throws URISyntaxException, IOException, InstantiationException,
InvalidKeyException, AccessControlException,
ClassNotFoundException, SAXException, IllegalAccessException {
Collection uids = null;
final URI uri = commClient().newURI();
final XMLRPCCommandGetApps cmd = new XMLRPCCommandGetApps(uri,
config.getUser());
final XMLVector xmluids = (XMLVector) sendCommand(cmd, false);
uids = xmluids.getXmlValues();
get(uids, display);
return uids;
}
/**
* This inserts/updates an application in server.
* This does not read from cache because we need the exact last app
* definition in order to correctly update it if it exists.
* Command line parameters : --xwsendapp appName cpuType osName binFileName
*
The user must have the right to do so
*
* @throws IOException
* @throws ParseException
* @throws SAXException
* @throws ClassNotFoundException
* @throws InstantiationException
* @throws URISyntaxException
* @throws AccessControlException
* @throws InvalidKeyException
*/
private void sendApp() throws IOException, ParseException,
InvalidKeyException, AccessControlException, URISyntaxException,
InstantiationException, ClassNotFoundException, SAXException {
List appParams = (List) args.commandParams();
AppInterface app = null;
File xmlFile = (File) args.getOption(CommandLineOptions.XML);
if (xmlFile != null) {
FileInputStream fis = null;
try {
fis = new FileInputStream(xmlFile);
app = (AppInterface) Table.newInterface(fis);
} finally {
fis.close();
fis = null;
xmlFile = null;
}
if (app.getName() == null) {
throw new IOException("application name cannot be null");
}
if (app.getUID() == null) {
UID uid = new UID();
app.setUID(uid);
uid = null;
}
} else {
if (appParams == null) {
throw new ParseException("no param provided", 0);
}
try {
app = (AppInterface) get((URI) appParams.get(0), false, true);
} catch (final Exception appe) {
// maybe it is an UID...
try {
app = (AppInterface) get((UID) appParams.get(0), false,
true);
} catch (final ClassCastException e) {
// maybe it is a string...
app = commClient().getApp((String) appParams.get(0), true);
}
}
if (app == null) {
UID uid = new UID();
app = new AppInterface(uid);
uid = null;
app.setName((String) appParams.get(0));
} else {
// let see if we can modify this app
commClient().send(app);
}
File binaryFile = null;
URI binaryUri = null;
CPUEnum cpu = null;
OSEnum os = null;
XWAccessRights accessRights = XWAccessRights.DEFAULT;
AppTypeEnum apptype = null;
for (int i = 1; i < appParams.size(); i++) {
logger.debug("params[" + i + "] = "
+ appParams.get(i).toString());
if ((binaryFile == null) && (binaryUri == null)) {
try {
try {
binaryUri = (URI) appParams.get(i);
if (binaryUri.isFile()) {
binaryFile = new File(binaryUri.getPath());
throw new IOException(binaryUri.toString()
+ " is a local file");
}
} catch (final Exception e0) {
binaryUri = null;
//
// maybe a local file
//
if (binaryFile == null) {
binaryFile = new File((String) appParams.get(i));
}
if (binaryFile.exists()) {
logger.debug(appParams.get(i).toString()
+ " is local file");
continue;
}
binaryFile = null;
}
} catch (final Exception e) {
logger.finest(appParams.get(i).toString()
+ " is not an URI");
binaryFile = null;
}
}
try {
accessRights = new XWAccessRights((String) appParams.get(i));
logger.debug(appParams.get(i).toString()
+ " are access rights");
continue;
} catch (final Exception e) {
logger.finest(appParams.get(i).toString()
+ " are not access rights");
}
try {
apptype = (AppTypeEnum) appParams.get(i);
logger.debug(appParams.get(i).toString()
+ " is an applicaiton type");
continue;
} catch (final Exception e) {
logger.finest(appParams.get(i).toString()
+ " is not an application type");
}
try {
if (binaryUri == null) {
binaryUri = commClient().newURI((UID) appParams.get(i));
logger.debug(appParams.get(i).toString() + " is an UID");
continue;
}
} catch (final Exception e) {
logger.finest(appParams.get(i).toString()
+ " is not an UID");
}
try {
if (cpu == null) {
cpu = (CPUEnum) appParams.get(i);
logger.debug(appParams.get(i).toString() + " is a CPU");
continue;
}
} catch (final Exception e) {
logger.finest(appParams.get(i).toString() + " is not a CPU");
}
try {
if (os == null) {
os = (OSEnum) appParams.get(i);
logger.debug(appParams.get(i).toString() + " is an OS");
continue;
}
} catch (final Exception e) {
logger.finest(appParams.get(i).toString() + " is not an OS");
}
}
if (apptype == null) {
throw new ParseException("unknown application type", 0);
}
if (os == null) {
throw new ParseException("unknown OS", 0);
}
if ((cpu == null) && (os != OSEnum.JAVA)) {
throw new ParseException("unknown CPU", 0);
}
if (binaryUri == null) {
if (binaryFile != null) {
DataTypeEnum type = DataTypeEnum.getFileType(binaryFile);
if (type == null) {
type = DataTypeEnum.BINARY;
}
binaryUri = sendData(os, cpu, type, accessRights, new URI(
"file://" + binaryFile.getCanonicalPath()),
binaryFile.getName());
type = null;
} else {
logger.warn("no binary found (no URI, no file)");
}
}
logger.debug("uri = " + binaryUri + " os = " + os + " cpu = " + cpu);
app.setBinary(cpu, os, binaryUri);
app.setAccessRights(accessRights);
app.setType(apptype);
binaryFile = null;
binaryUri = null;
cpu = null;
os = null;
accessRights = null;
apptype = null;
}
commClient().send(app);
println(commClient().newURI(app.getUID()));
app = null;
appParams = null;
}
/**
* This removes objects from server.
* Command line parameters : --xwremove UID [UID...]
*
The user must have the right to do so
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private void remove() throws IOException, ParseException,
ClassNotFoundException, InvalidKeyException,
AccessControlException, InstantiationException,
IllegalAccessException {
final Collection params = (Collection) args.commandParams();
if (params == null) {
exit("no URI provided", XWReturnCode.PARSING);
}
final Iterator enumParams = params.iterator();
while (enumParams.hasNext()) {
Object param = enumParams.next();
try {
remove((UID) param);
} catch (final ClassCastException e) {
remove((URI) param);
} catch (final InvalidKeyException e) {
throw e;
} catch (final AccessControlException e) {
throw e;
} catch (final Exception e) {
throw new IOException(e.getMessage());
}
param = null;
}
params.clear();
}
/**
* This removes an object from server.
* Command line parameters : --xwremove UID | URI
* The user must have the right to do so
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public void remove(URI uri) throws IOException, ClassNotFoundException,
InvalidKeyException, AccessControlException, InstantiationException {
XMLRPCCommandRemove cmd = null;
try {
cmd = new XMLRPCCommandRemove(uri);
sendCommand(cmd, false);
} catch (final SAXException e) {
logger.exception(e);
} finally {
cmd = null;
}
}
/**
* This removes an object from server.
* Command line parameters : --xwremove UID | URI
* The user must have the right to do so
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public void remove(UID uid) throws IOException, UnknownHostException,
ConnectException, ClassNotFoundException, URISyntaxException,
InvalidKeyException, AccessControlException, InstantiationException {
URI uri = null;
try {
uri = commClient().newURI(uid);
remove(uri);
} finally {
uri = null;
}
}
/**
* This retrieves, stores and displays data installed in XtremWeb server.
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws IllegalAccessException
* @throws InstantiationException
* @see #getDatas(boolean)
*/
public Collection getDatas() throws IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException,
InstantiationException, IllegalAccessException {
return getDatas(false);
}
/**
* This retrieves, stores and displays data installed in XtremWeb server.
* Command line parameters : --xwdatas
*
* @param display
* is true to write data descriptions to stdout
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @throws IllegalAccessException
* @see #getDatas()
*/
private Collection getDatas(boolean display) throws IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException,
InstantiationException, IllegalAccessException {
final URI uri = commClient().newURI();
final XMLRPCCommandGetDatas cmd = new XMLRPCCommandGetDatas(uri,
config.getUser());
final XMLVector xmluids = (XMLVector) sendCommand(cmd, false);
final Collection uids = xmluids.getXmlValues();
get(uids, display);
return uids;
}
/**
* This inserts a new data in server.
* Command line parameters : --xwsenddata dataName cpuType
* osName DataTypeEnum accessRight [binFileName | binFileURI | binFileUID ]
*
The user must have the right to do so
*
* @throws IOException
* on I/O error
* @throws InvalidKeyException
* on authentication or authorization error (user don't have
* rights to send data)
* @throws ParseException
* on command line parameter error
* @throws InstantiationException
* if the data URI scheme is unknwon
* @throws URISyntaxException
* on URI error
* @throws CertificateException
* @throws CertificateExpiredException
* @throws SAXException
* @throws ClassNotFoundException
* @throws AccessControlException
*/
private void sendData() throws IOException, ParseException,
InvalidKeyException, URISyntaxException, InstantiationException,
CertificateExpiredException, CertificateException,
AccessControlException, ClassNotFoundException, SAXException {
File dataFile = null;
String dataName = null;
CPUEnum cpu = null;
OSEnum os = null;
XWAccessRights accessRights = null;
DataTypeEnum DataTypeEnum = null;
URI dataUri = null;
UID uid = new UID();
DataInterface data = null;
File xmlFile = (File) args.getOption(CommandLineOptions.XML);
if (xmlFile != null) {
FileInputStream fis = null;
try {
fis = new FileInputStream(xmlFile);
data = (DataInterface) Table.newInterface(fis);
} finally {
fis.close();
fis = null;
xmlFile = null;
}
if (data.getUID() == null) {
data.setUID(uid);
}
dataName = data.getName();
} else {
final List dataParams = (List) args.commandParams();
if (dataParams == null) {
throw new ParseException("no param provided", 0);
}
for (int i = 0; i < dataParams.size(); i++) {
logger.debug("params[" + i + "] = "
+ dataParams.get(i).toString());
if ((dataFile == null) && (dataUri == null)) {
try {
try {
dataUri = (URI) dataParams.get(i);
if (dataUri.isFile()) {
dataFile = new File(dataUri.getPath());
throw new ClassCastException(dataUri.toString()
+ " is a local file");
}
} catch (final ClassCastException e0) {
dataUri = null;
if (dataFile == null) {
dataFile = new File((String) dataParams.get(i));
}
if (!dataFile.exists()) {
throw new IOException("file not found");
}
if (!dataFile.isFile() && !dataFile.isDirectory()) {
exit("" + dataFile + " is not a regular file",
XWReturnCode.FATAL);
}
if (dataFile.isHidden()) {
exit("" + dataFile + " is hidden",
XWReturnCode.FATAL);
}
continue;
}
} catch (final Exception e) {
logger.debug("not an URI");
dataFile = null;
}
}
try {
cpu = (CPUEnum) dataParams.get(i);
continue;
} catch (final Exception e) {
logger.debug("not a CPU");
}
try {
os = (OSEnum) dataParams.get(i);
continue;
} catch (final Exception e) {
logger.debug("not an OS");
}
try {
accessRights = new XWAccessRights(
(String) dataParams.get(i));
continue;
} catch (final Exception e) {
logger.debug("not an access right");
}
try {
DataTypeEnum = (DataTypeEnum) dataParams.get(i);
continue;
} catch (final Exception e) {
logger.debug("not a data type");
}
try {
dataName = (String) dataParams.get(i);
} catch (final ClassCastException e) {
}
}
dataParams.clear();
data = new DataInterface(uid);
if ((dataUri == null) || (dataUri.isFile())) {
dataUri = commClient().newURI(data.getUID());
} else if (dataUri.isXtremWeb() == false) {
throw new ParseException(
"You must provide an XML file to insert an URI like "
+ dataUri.getScheme(), 0);
}
data.setURI(dataUri);
if ((dataName == null) && (dataFile != null)) {
dataName = dataFile.getName();
}
data.setName(dataName);
if ((DataTypeEnum == null) && (dataFile != null)) {
data.setType(DataTypeEnum.getFileType(dataFile));
}
if (DataTypeEnum != null) {
data.setType(DataTypeEnum);
if (DataTypeEnum == DataTypeEnum.X509) {
if (dataFile != null) {
XWTools.checkCertificate(dataFile);
accessRights = XWAccessRights.USERREADWRITE;
}
}
}
if (accessRights != null) {
data.setAccessRights(accessRights);
}
if (dataFile != null) {
logger.debug("Data file = " + dataFile.toString());
} else {
logger.info("no data content");
}
if (cpu != null) {
data.setCpu(cpu);
}
if (os != null) {
data.setOs(os);
}
}
if (dataName.toLowerCase()
.endsWith(DataTypeEnum.ZIP.getFileExtension()) == false) {
newZip = zip(dataFile);
}
if (newZip) {
data.setType(DataTypeEnum.ZIP);
dataFile = null;
dataFile = new File(zipper.getFileName());
}
sendData(data, dataFile);
if (newZip && (args.getOption(CommandLineOptions.KEEPZIP) == null)) {
File file = new File(zipFileName);
if (file.exists()) {
file.delete();
}
newZip = false;
file = null;
}
if (dataUri == null) {
dataUri = commClient().newURI(data.getUID());
}
println(dataUri);
dataFile = null;
dataName = null;
cpu = null;
os = null;
accessRights = null;
DataTypeEnum = null;
dataUri = null;
uid = null;
}
/**
* This inserts a new data in server
*
* @throws IOException
* @throws URISyntaxException
* @throws SAXException
* @throws ClassNotFoundException
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
*/
public URI sendData(OSEnum os, CPUEnum cpu, DataTypeEnum type,
XWAccessRights accessRights, URI uri, String name)
throws IOException, URISyntaxException, InvalidKeyException,
AccessControlException, ClassNotFoundException, SAXException,
InstantiationException {
logger.debug("sendData(" + os + ", " + cpu + ", " + type + ", "
+ accessRights + ", " + uri + ", " + name + ")");
File dataFile = null;
String dataName = name;
UID uid = new UID();
final DataInterface data = new DataInterface(uid);
uid = null;
URI dataUri = commClient().newURI(data.getUID());
data.setType(type);
data.setOs(os);
data.setCpu(cpu);
data.setAccessRights(accessRights);
if (uri != null) {
if (uri.isFile()) {
dataFile = new File(uri.getPath());
dataName = dataFile.getName();
final DataTypeEnum thisType = DataTypeEnum
.getFileType(dataFile);
if ((thisType == DataTypeEnum.TEXT)
&& (type == DataTypeEnum.URIPASSTHROUGH)) {
data.setType(type);
}
if (thisType == DataTypeEnum.X509) {
data.setAccessRights(XWAccessRights.USERALL);
}
} else {
dataUri = uri;
}
}
data.setURI(dataUri);
data.setName(dataName);
dataName = null;
dataUri = null;
return sendData(data, dataFile);
}
/**
* This calls sendData(data, new File(dataPath))
*
* @param data
* is the meta data of the data to send
* @param dataPath
* is the path to the data content on local fs
* @return the data URI
* @see #sendData(DataInterface, File)
*/
public URI sendData(DataInterface data, String dataPath)
throws InvalidKeyException, AccessControlException, IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InstantiationException {
final File f = new File(dataPath);
return sendData(data, f);
}
/**
* This inserts a new data in server.
* Command line parameters : --xwsenddata <data name>
* <cpu type> <os name> <bin file name>
The
* user must have the right to do so
*
* @return the data URI
* @param data
* describe the data to send
* @param dataFile
* contains data itself
* @throws IOException
* @throws URISyntaxException
* @throws SAXException
* @throws ClassNotFoundException
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
*/
public URI sendData(DataInterface data, File dataFile) throws IOException,
InvalidKeyException, AccessControlException,
ClassNotFoundException, SAXException, URISyntaxException,
InstantiationException {
if (dataFile == null) {
data.setStatus(StatusEnum.AVAILABLE);
} else {
if (dataFile.exists() == false) {
throw new IOException(dataFile.getName() + " does not exist");
}
data.setStatus(StatusEnum.UNAVAILABLE);
data.setSize(dataFile.length());
data.setMD5(MD5.asHex(MD5.getHash(dataFile)));
}
logger.debug("sendData(" + data.toXml() + ", " + dataFile + ")");
commClient().send(data);
final URI uri = data.getURI();
if (dataFile != null) {
try {
commClient().uploadData(uri, dataFile);
} catch (final Exception e) {
logger.exception("Upload error", e);
try {
commClient().setAutoClose(true);
commClient().close();
commClient().remove(uri);
} finally {
exit("Upload error " + uri, XWReturnCode.DISK);
}
}
}
data = null;
return uri;
}
/**
* This retrieves, stores and displays groups installed in XtremWeb server.
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws IllegalAccessException
* @throws InstantiationException
* @see #getGroups(boolean)
*/
public Collection getGroups() throws IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException,
InstantiationException, IllegalAccessException {
return getGroups(false);
}
/**
* This retrieves, stores and displays groups installed in XtremWeb server.
* Command line parameters : --xwgroups
*
* @param display
* is true to write group descriptions to stdout
* @throws URISyntaxException
* @throws InstantiationException
* @throws SAXException
* @throws ClassNotFoundException
* @throws IOException
* @throws AccessControlException
* @throws InvalidKeyException
* @throws IllegalAccessException
*/
private Collection getGroups(boolean display)
throws InvalidKeyException, AccessControlException, IOException,
ClassNotFoundException, SAXException, InstantiationException,
URISyntaxException, IllegalAccessException {
final XMLVector xmluids = (XMLVector) sendCommand(
new XMLRPCCommandGetGroups(commClient().newURI(),
config.getUser()), false);
final Collection uids = xmluids.getXmlValues();
get(uids, display);
return uids;
}
/**
* This inserts a new group in server.
* Command line parameters : --xwaddgroup <group name>
* <cpu type> <os name> <bin file name>
The
* user must have the right to do so
*/
private void sendGroup() throws ParseException, IOException, SAXException,
URISyntaxException {
final List params = (List) args.commandParams();
GroupInterface group = null;
final UID uid = new UID();
final File xmlFile = (File) args.getOption(CommandLineOptions.XML);
if (xmlFile != null) {
FileInputStream fis = null;
try {
fis = new FileInputStream(xmlFile);
group = (GroupInterface) Table.newInterface(fis);
} catch (final Exception e) {
throw new IOException(e.getMessage());
} finally {
if (fis != null) {
fis.close();
}
fis = null;
}
if (group.getUID() == null) {
group.setUID(uid);
}
if (group.getName() == null) {
throw new IOException("group name can not be null");
}
if (group.getOwner() == null) {
throw new IOException("group owner can not be null");
}
} else {
group = new GroupInterface(uid);
if (group.getOwner() == null) {
group.setOwner(config.getUser().getUID());
}
int paramIdx = 0;
try {
group.setName((String) params.get(paramIdx++));
} catch (final Exception e) {
throw new ParseException("group name not provided", 0);
}
try {
group.setSession((UID) params.get(paramIdx++));
} catch (final Exception e) {
}
}
try {
commClient().send(group);
println(commClient().newURI(uid));
} catch (final Exception e) {
throw new IOException(e.getMessage());
} finally {
if (params != null) {
params.clear();
}
group = null;
}
}
/**
* This retrieves, stores and displays hosts installed in XtremWeb server.
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getHosts(boolean)
*/
public Collection getHosts() throws IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException, InstantiationException {
return getHosts(false);
}
/**
* This retrieves, stores and displays hosts installed in XtremWeb server.
* Command line parameters : --xwhosts
*
* @param display
* is true to write host descriptions to stdout
* @throws URISyntaxException
* @throws InstantiationException
* @throws SAXException
* @throws ClassNotFoundException
* @throws IOException
* @throws AccessControlException
* @throws InvalidKeyException
* @throws IllegalAccessException
* @see #getHosts
*/
private Collection getHosts(boolean display)
throws InvalidKeyException, AccessControlException, IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InstantiationException {
final XMLVector xmluids = (XMLVector) sendCommand(
new XMLRPCCommandGetHosts(commClient().newURI(),
config.getUser()), false);
final Collection uids = xmluids.getXmlValues();
get(uids, display);
return uids;
}
/**
* This inserts a new host in server.
* Command line parameters : --xwaddhost <host name>
* <cpu type> <os name> <bin file name>
The
* user must have the right to do so
*/
private void sendHost() throws IOException {
exit("Can't send host", XWReturnCode.FATAL);
}
/**
* This retrieves command line parameter and writes session description
*
* @return an SessionInterface
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getSession(UID, boolean)
*/
private void getSession() throws IOException, ClassNotFoundException,
SAXException, URISyntaxException, InvalidKeyException,
AccessControlException, InstantiationException {
Collection params = (Collection) args.commandParams();
get(params, true);
params.clear();
params = null;
}
/**
* This retrieves, stores and displays sessions installed in XtremWeb
* server.
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getSessions(boolean)
*/
public Collection getSessions() throws IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException, InstantiationException {
return getSessions(false);
}
/**
* This retrieves, stores and displays sessions installed in XtremWeb
* server. Command line parameters : --xwsessions
*
* @param display
* is true to write session descriptions to stdout
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getSessions
*/
private Collection getSessions(boolean display)
throws IOException, ClassNotFoundException, SAXException,
URISyntaxException, InvalidKeyException, AccessControlException,
InstantiationException {
final XMLVector xmluids = (XMLVector) sendCommand(
new XMLRPCCommandGetSessions(commClient().newURI(),
config.getUser()), false);
final Collection uids = xmluids.getXmlValues();
get(uids, display);
return uids;
}
/**
* This inserts a new session in server.
* Command line parameters : --xwaddsession <session
* name> <cpu type> <os name> <bin file name>
*
The user must have the right to do so
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
*/
private void sendSession() throws ParseException, IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException, InstantiationException {
final List params = (List) args.commandParams();
final UID uid = new UID();
SessionInterface session = null;
final File xmlFile = (File) args.getOption(CommandLineOptions.XML);
if (xmlFile != null) {
FileInputStream fis = null;
try {
fis = new FileInputStream(xmlFile);
session = (SessionInterface) Table.newInterface(fis);
} catch (final Exception e) {
throw new IOException(e.getMessage());
} finally {
fis.close();
fis = null;
}
if (session.getUID() == null) {
session.setUID(uid);
}
if (session.getName() == null) {
throw new ParseException("session name can not be null", 0);
}
if (session.getOwner() == null) {
throw new ParseException("session client can not be null", 0);
}
} else {
session = new SessionInterface();
int paramIdx = 0;
session.setUID(uid);
try {
session.setName((String) params.get(paramIdx++));
} catch (final Exception e) {
throw new ParseException("session name not provided", 0);
}
session.setOwner(config.getUser().getUID());
}
commClient().send(session);
final URI uri = commClient().newURI(session.getUID());
println(uri);
params.clear();
session = null;
}
/**
* This retrieves registered tasks
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getTasks(boolean)
*/
public Collection getTasks() throws IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException, InstantiationException {
return getTasks(false);
}
/**
* This retrieves registered tasks Command line parameters :
* --xwtasks
*
* @param display
* is true to write task descriptions to stdout
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getTasks
*/
private Collection getTasks(boolean display) throws IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException, InstantiationException {
final XMLVector xmluids = (XMLVector) sendCommand(
new XMLRPCCommandGetTasks(commClient().newURI(),
config.getUser()), false);
final Collection uids = xmluids.getXmlValues();
get(uids, display);
return uids;
}
/**
* This retrieves command line parameter and writes trace description
*
* @return an TraceInterface
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getTrace(UID, boolean)
*/
private void getTrace() throws IOException, ClassNotFoundException,
SAXException, URISyntaxException, InvalidKeyException,
AccessControlException, InstantiationException {
final Collection params = (Collection) args.commandParams();
get(params, true);
params.clear();
}
/**
* This retrieves, stores and displays traces installed in XtremWeb server.
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getTraces(boolean)
*/
public Collection getTraces() throws IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException, InstantiationException {
return getTraces(false);
}
/**
* This retrieves, stores and displays traces installed in XtremWeb server.
* Command line parameters : --xwtraces
*
* @param display
* is true to write trace descriptions to stdout
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getTraces
*/
private Collection getTraces(boolean display) throws IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException, InstantiationException {
final XMLVector xmluids = (XMLVector) sendCommand(
new XMLRPCCommandGetTraces(commClient().newURI(),
config.getUser()), false);
final Collection uids = xmluids.getXmlValues();
get(uids, display);
return uids;
}
/**
* This inserts a new trace in server.
* Command line parameters : --xwaddtrace <trace name>
* <cpu type> <os name> <bin file name>
The
* user must have the right to do so
*/
private void sendTrace() throws IOException {
exit("Can't send trace", XWReturnCode.FATAL);
}
/**
* This retrieves command line parameter and writes user group description
*
* @return an UserGroupInterface
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getUserGroup(UID, boolean)
*/
private void getUserGroup() throws IOException, ClassNotFoundException,
SAXException, URISyntaxException, InvalidKeyException,
AccessControlException, InstantiationException {
final Collection params = (Collection) args.commandParams();
get(params, true);
params.clear();
}
/**
* This retrieves, stores and displays userGroups installed in XtremWeb
* server.
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getUserGroups(boolean)
*/
public Collection getUserGroups() throws IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException, InstantiationException {
return getUserGroups(false);
}
/**
* This retrieves, stores and displays userGroups installed in XtremWeb
* server. Command line parameters : --xwuserGroups
*
*
* @param display
* is true to write userGroup descriptions to stdout
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getUserGroups
*/
public Collection getUserGroups(boolean display)
throws IOException, ClassNotFoundException, SAXException,
URISyntaxException, InvalidKeyException, AccessControlException,
InstantiationException {
final XMLVector xmluids = (XMLVector) sendCommand(
new XMLRPCCommandGetUserGroups(commClient().newURI(),
config.getUser()), false);
final Collection uids = xmluids.getXmlValues();
get(uids, display);
return uids;
}
/**
* This inserts a new userGroup in server.
* Command line parameters : --xwsenduserGroup <userGroup
* name> <cpu type> <os name> <bin file name>
*
The user must have the right to do so
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
*/
private void sendUserGroup() throws ParseException, IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException, InstantiationException {
UserGroupInterface group = null;
UserInterface groupAdmin = null;
final UID groupUID = new UID();
final File xmlFile = (File) args.getOption(CommandLineOptions.XML);
if (xmlFile != null) {
FileInputStream fis = null;
try {
fis = new FileInputStream(xmlFile);
group = (UserGroupInterface) Table.newInterface(fis);
} catch (final Exception e) {
throw new IOException(e.getMessage());
} finally {
if (fis != null) {
fis.close();
}
fis = null;
}
if (group.getUID() == null) {
group.setUID(groupUID);
}
if (group.getLabel() == null) {
throw new ParseException("user group label can not be null", 0);
}
} else {
List params = (List) args.commandParams();
if (params == null) {
throw new ParseException("no param provided", 0);
}
String groupLabel = (String) params.get(0);
group = new UserGroupInterface(groupUID);
group.setLabel(groupLabel);
String adminLogin = null;
String adminPassword = null;
String adminEmail = null;
try {
adminLogin = (String) params.get(1);
} catch (final Exception e) {
throw new ParseException("group admin login not provided", 0);
}
try {
adminPassword = (String) params.get(2);
} catch (final Exception e) {
throw new ParseException("group admin password not provided", 0);
}
try {
adminEmail = (String) params.get(3);
} catch (final Exception e) {
throw new ParseException("group admin mail not provided", 0);
}
UserRightEnum adminRights = UserRightEnum.ADVANCED_USER;
UID adminUID = new UID();
groupAdmin = new UserInterface(adminUID);
groupAdmin.setLogin(adminLogin);
groupAdmin.setPassword(adminPassword);
groupAdmin.setEMail(adminEmail);
groupAdmin.setRights(adminRights);
sendUser(groupAdmin, false);
group.setOwner(adminUID);
params.clear();
params = null;
groupLabel = null;
adminLogin = null;
adminPassword = null;
adminEmail = null;
adminUID = null;
adminRights = null;
}
try {
commClient().send(group);
println(commClient().newURI(group.getUID()));
if (groupAdmin != null) {
groupAdmin.setGroup(groupUID);
sendUser(groupAdmin, true,
" administrator of group " + group.getLabel());
}
} catch (final Exception e) {
connectionRefused();
} finally {
group = null;
groupAdmin = null;
}
}
/**
* This retrieves command line parameter and writes user description
*
* @return an UserInterface
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getUser(UID, boolean)
*/
private void getUser() throws IOException, ClassNotFoundException,
SAXException, URISyntaxException, InvalidKeyException,
AccessControlException, InstantiationException {
final Collection params = (Collection) args.commandParams();
get(params, true);
params.clear();
}
/**
* This calls getUser(login, false)
*
* @param login
* is the user login
* @return an UserInterface
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getUser(String, boolean)
*/
public UserInterface getUser(String login) throws IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException, InstantiationException {
return getUser(login, false);
}
/**
* This retrieves user from XtremWeb server This may write description to
* stdout, accordingly to dislay parameter
*
* @param login
* is the use login
* @param display
* tells to write descriptio to stdout, or not
* @return an UserInterface
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
*/
private UserInterface getUser(String login, boolean display)
throws IOException, ClassNotFoundException, SAXException,
URISyntaxException, InvalidKeyException, AccessControlException,
InstantiationException {
final UserInterface user = commClient().getUser(login, false);
if (display) {
startLine();
String str = null;
if (args.xml()) {
str = user.toXml();
} else {
str = user.toString(args.csv() || args.html());
}
println(true, str);
str = null;
endLine();
}
return user;
}
/**
* This retrieves, stores and displays users installed in XtremWeb server.
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getUsers(boolean)
*/
public Collection getUsers() throws IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException, InstantiationException {
return getUsers(false);
}
/**
* This retrieves, stores and displays users installed in XtremWeb server.
* Command line parameters : --xwusers
*
* @param display
* is true to write user descriptions to stdout
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #get(Vector, boolean)
*/
private Collection getUsers(boolean display) throws IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException, InstantiationException {
final URI uri = commClient().newURI();
final XMLRPCCommandGetUsers cmd = new XMLRPCCommandGetUsers(uri,
config.getUser());
final XMLVector xmluids = (XMLVector) sendCommand(cmd, false);
final Collection uids = xmluids.getXmlValues();
get(uids, display);
return uids;
}
/**
* This inserts a new user in server.
* Command line parameters : --xwsenduser <user login>
* <uesr password> <user email> <user rights>
*
The user must have the right to do so
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
*/
private void sendUser() throws ParseException, IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException, InstantiationException {
final UID uid = new UID();
UserInterface user = null;
final File xmlFile = (File) args.getOption(CommandLineOptions.XML);
if (xmlFile != null) {
FileInputStream fis = null;
try {
fis = new FileInputStream(xmlFile);
user = (UserInterface) Table.newInterface(fis);
} catch (final Exception e) {
throw new IOException(e.getMessage());
} finally {
if (fis != null) {
fis.close();
}
fis = null;
}
if (user.getLogin() == null) {
throw new ParseException("user login can not be null", 0);
}
if (user.getPassword() == null) {
throw new ParseException("user password can not be null", 0);
}
} else {
List params = (List) args.commandParams();
if (params == null) {
throw new ParseException("no param provided", 0);
}
String login = null;
String password = null;
String email = null;
UserRightEnum rights = UserRightEnum.STANDARD_USER;
URI groupURI = null;
try {
login = (String) params.get(0);
} catch (final Exception e) {
throw new ParseException("login not provided", 0);
}
try {
password = (String) params.get(1);
} catch (final Exception e) {
throw new ParseException("password not provided", 0);
}
try {
email = (String) params.get(2);
} catch (final Exception e) {
throw new ParseException("mail not provided", 0);
}
int paramindex = 3;
try {
rights = UserRightEnum
.valueOf(((String) params.get(paramindex))
.toUpperCase());
} catch (final IllegalArgumentException iae) {
try {
rights = UserRightEnum.fromInt(Integer
.parseInt((String) params.get(paramindex)));
} catch (final Exception another) {
}
} catch (final ArrayIndexOutOfBoundsException aiobe) {
} catch (final ClassCastException e) {
paramindex = 2;
}
paramindex++;
try {
groupURI = (URI) params.get(paramindex);
} catch (final ClassCastException e) {
try {
groupURI = commClient()
.newURI((UID) params.get(paramindex));
} catch (final ClassCastException e2) {
exit("Not a group :" + params.get(paramindex),
XWReturnCode.FATAL);
}
} catch (final ArrayIndexOutOfBoundsException e) {
}
user = new UserInterface(uid);
user.setLogin(login);
user.setPassword(password);
user.setEMail(email);
user.setRights(rights);
if (groupURI != null) {
user.setGroup(groupURI.getUID());
}
params.clear();
params = null;
password = null;
email = null;
rights = null;
groupURI = null;
}
sendUser(user);
user = null;
}
private void sendUser(UserInterface user) throws ParseException,
IOException, ClassNotFoundException, SAXException,
URISyntaxException, InvalidKeyException, AccessControlException,
InstantiationException {
sendUser(user, true);
}
private void sendUser(UserInterface user, boolean display)
throws ParseException, IOException, ClassNotFoundException,
SAXException, URISyntaxException, InvalidKeyException,
AccessControlException, InstantiationException {
final String msg = null;
sendUser(user, display, msg);
}
private void sendUser(UserInterface user, boolean display, String msg)
throws ParseException, IOException, ClassNotFoundException,
SAXException, URISyntaxException, InvalidKeyException,
AccessControlException, InstantiationException {
commClient().send(user);
if (display == false) {
return;
}
println("# XWHEP client config file");
if (msg != null) {
println("# " + msg);
}
println("# " + CommonVersion.getCurrent().full());
println("# Role");
println(XWPropertyDefs.ROLE + "=" + XWRole.CLIENT);
println("# XWHEP server");
println(XWPropertyDefs.DISPATCHERS + "="
+ config.getCurrentDispatcher());
println("# Login and password to connect to the server");
println(XWPropertyDefs.LOGIN + "=" + user.getLogin());
println(XWPropertyDefs.PASSWORD + "=" + user.getPassword());
println(XWPropertyDefs.USERUID + "=" + user.getUID());
println("# logger level");
println(XWPropertyDefs.LOGGERLEVEL + "=" + LoggerLevel.INFO);
final String certPath = config.getPath(XWPropertyDefs.SSLKEYSTORE);
if ((certPath != null) && (new File(certPath)).exists()) {
println("# worker keystore");
println(XWPropertyDefs.SSLKEYSTORE + "=" + certPath);
}
}
/**
* This retrieves command line parameter and writes work description This
* always download work from server; this does not use cache
*
* @return an WorkInterface
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getWork(UID, boolean)
*/
private void getWork() throws IOException, ClassNotFoundException,
SAXException, URISyntaxException, InvalidKeyException,
AccessControlException, InstantiationException {
final Collection params = (Collection) args.commandParams();
get(params, true);
params.clear();
}
/**
* This retrieves, stores and displays works installed in XtremWeb server.
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getWorks(boolean)
*/
public Collection getWorks() throws IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException, InstantiationException {
return getWorks(false);
}
/**
* This retrieves works from server Command line parameters :
* --xwworks
*
* @param display
* is true to write work descriptions to stdout
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getWorks
*/
private Collection getWorks(boolean display) throws IOException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException, InstantiationException {
final StatusEnum status = (StatusEnum) args
.getOption(CommandLineOptions.STATUS);
final XMLRPCCommandGetWorks cmd = new XMLRPCCommandGetWorks(
commClient().newURI(), config.getUser());
cmd.setStatus(status);
final XMLVector xmluids = (XMLVector) sendCommand(cmd, false);
final Collection uids = xmluids.getXmlValues();
get(uids, display);
return uids;
}
/**
* This retrieves works for a given group Command line
* parameters : --xwgroupworks
*
* @param display
* is true to write work descriptions to stdout
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
* @see #getWorks
*/
private Collection getGroupWorks(boolean display)
throws IOException, ClassNotFoundException, SAXException,
URISyntaxException, InvalidKeyException, AccessControlException,
InstantiationException {
Collection params = (Collection) args.commandParams();
Iterator theEnum = params.iterator();
Object param = theEnum.next();
GroupInterface group = null;
try {
group = (GroupInterface) get((URI) param);
} catch (final ClassCastException e) {
group = (GroupInterface) get((UID) param);
}
XMLRPCCommandGetGroupWorks cmd = new XMLRPCCommandGetGroupWorks(
commClient().newURI(group.getUID()), config.getUser());
XMLVector xmluids = (XMLVector) sendCommand(cmd, false);
cmd = null;
final Vector uids = xmluids.getXmlValues();
get(uids, display);
params.clear();
params = null;
theEnum = null;
param = null;
group = null;
xmluids = null;
return uids;
}
/**
* This zippes dirin, if any
*/
protected boolean zipDirin(WorkInterface work) throws IOException {
if (args.getOption(CommandLineOptions.DONTZIP) != null) {
return false;
}
mileStone.println("");
boolean newZip = true;
String[] filesHierarchy = new String[1];
String savname = zipFileName;
filesHierarchy[0] = zipFileName;
zipFileName = work.getUID().toString()
+ DataTypeEnum.ZIP.getFileExtension();
zipper.setFileName(zipFileName);
if (zipper.zip(filesHierarchy, true) == false) {
zipFileName = savname;
logger.debug(zipFileName + " is not zipped; it is kept 'as is'");
newZip = false;
}
mileStone.println("");
filesHierarchy = null;
savname = null;
return newZip;
}
/**
* This zippes a file
*
* @since 8.0.0
*/
protected boolean zip(File path) throws IOException {
if (args.getOption(CommandLineOptions.DONTZIP) != null) {
return false;
}
if (path == null) {
return false;
}
mileStone.println("");
boolean newZip = true;
String[] filesHierarchy = new String[1];
zipFileName = path.getName() + DataTypeEnum.ZIP.getFileExtension();
filesHierarchy[0] = path.getAbsolutePath();
zipper.setFileName(zipFileName);
if (zipper.zip(filesHierarchy, true) == false) {
logger.debug(zipFileName + " is not zipped; it is kept 'as is'");
newZip = false;
}
mileStone.println("");
filesHierarchy = null;
return newZip;
}
/**
* This inserts (submits) a new work in server.
*
* Command line parameters : --xwsendwork application name or UID [ --xwcert X.509 cert or proxy ] [ --xwenv dirinFile | URI | UID ] [ '<' ]
*
* @throws AccessControlException
* @throws InvalidKeyException
* @throws InstantiationException
*/
private void sendWork() throws IOException, ParseException,
ClassNotFoundException, SAXException, URISyntaxException,
InvalidKeyException, AccessControlException, InstantiationException {
File zipFile = null;
final UID uid = new UID();
WorkInterface work = null;
final File xmlFile = (File) args.getOption(CommandLineOptions.XML);
if (xmlFile != null) {
FileInputStream fis = null;
try {
fis = new FileInputStream(xmlFile);
work = (WorkInterface) Table.newInterface(fis);
} catch (final Exception e) {
throw new IOException(e.getMessage());
} finally {
if (fis != null) {
fis.close();
}
fis = null;
}
if (work.getUID() == null) {
work.setUID(uid);
}
try {
if (get(work.getApplication()) == null) {
throw new IOException("Can't retrieve application");
}
} catch (final Exception e2) {
throw new IOException(e2.toString());
}
} else {
work = new WorkInterface();
List params = (List) args.commandParams();
work.setUID(uid);
AppInterface app = null;
try {
app = (AppInterface) get(((URI) params.get(0)).getUID());
} catch (final Exception e) {
try {
app = (AppInterface) get((UID) params.get(0));
} catch (final Exception e2) {
try {
app = getApp((String) params.get(0));
} catch (final Exception e3) {
app = null;
}
}
}
if (app == null) {
if (params != null) {
params.clear();
}
params = null;
throw new ParseException("Can't retrieve application "
+ (params != null ? params.get(0).toString() : ""), 0);
}
work.setApplication(app.getUID());
app = null;
try {
final String lp = (String) args
.getOption(CommandLineOptions.LISTENPORT);
if (lp != null) {
work.setListenPort(lp);
}
} catch (final Exception e) {
logger.exception("Can't set LISTENPORT", e);
}
try {
final String fa = (String) args
.getOption(CommandLineOptions.FORWARDADDRESSES);
if (fa != null) {
work.setSmartSocketClient(fa);
}
} catch (final Exception e) {
logger.exception("Can't set FORWARDADDRESSES", e);
}
try {
Long wct = (Long) args
.getOption(CommandLineOptions.WALLCLOCKTIME);
if (wct != null) {
work.setMaxWallClockTime(wct.longValue());
}
wct = null;
} catch (final Exception e) {
logger.exception("Can't set WALLCLOCKTIME", e);
}
DataTypeEnum dirinType = null;
final List envs = (List) args.getOption(CommandLineOptions.ENV);
if (envs != null) {
if (envs.size() == 1) {
final Object envparam = envs.get(0);
URI envuri = null;
UID envuid = null;
try {
envuri = (URI) envparam;
if (envuri.isFile() == false) {
work.setDirin(envuri);
} else {
zipFileName = envuri.getPath();
}
} catch (final ClassCastException e) {
try {
envuid = (UID) envparam;
work.setDirin(commClient().newURI(envuid));
} catch (final ClassCastException e2) {
zipFileName = (String) envparam;
}
}
envuri = null;
envuid = null;
} else {
dirinType = DataTypeEnum.URIPASSTHROUGH;
final String fname = uid.toString()
+ DataTypeEnum.TEXT.getFileExtension();
final PrintWriter writer = new PrintWriter(
new BufferedWriter(new FileWriter(fname)));
final Iterator envsenum = envs.iterator();
while (envsenum.hasNext()) {
final String envp = envsenum.next().toString();
if ((envp == null)
|| (envp.indexOf(Connection.SCHEMESEPARATOR) < 0)) {
throw new URISyntaxException(envp, "scheme error");
}
writer.println(envp);
}
writer.close();
zipFileName = fname;
}
}
if (zipFileName != null) {
if (zipFileName.toLowerCase().endsWith(
DataTypeEnum.ZIP.getFileExtension()) == false) {
newZip = zipDirin(work);
}
if (newZip) {
dirinType = DataTypeEnum.ZIP;
}
zipFile = new File(zipFileName);
URI zipUri = sendData(OSEnum.NONE, CPUEnum.NONE, dirinType,
XWAccessRights.DEFAULT,
new URI("file://" + zipFile.getCanonicalPath()),
zipFile.getName());
work.setDirin(zipUri);
if (newZip) {
File file = new File(zipFileName);
if (file.exists()) {
file.delete();
}
newZip = false;
file = null;
}
zipUri = null;
}
File stdin = null;
if (args.getOption(CommandLineOptions.STDIN) != null) {
try {
URI uri = (URI) args.getOption(CommandLineOptions.STDIN);
if (uri.isFile() == false) {
work.setStdin((URI) args
.getOption(CommandLineOptions.STDIN));
} else {
stdin = new File(uri.getPath());
}
uri = null;
} catch (final ClassCastException e) {
try {
work.setStdin(commClient().newURI(
(UID) args.getOption(CommandLineOptions.STDIN)));
} catch (final ClassCastException e2) {
stdin = new File(
(String) args
.getOption(CommandLineOptions.STDIN));
}
}
}
if (stdin != null) {
URI stdinUri = sendData(OSEnum.NONE, CPUEnum.NONE,
DataTypeEnum.TEXT, XWAccessRights.DEFAULT, new URI(
"file://" + stdin.getCanonicalPath()),
stdin.getName());
work.setStdin(stdinUri);
stdinUri = null;
}
stdin = null;
String cmdLineStr = new String(" ");
// i = 1 to bypass application name
for (int i = 1; i < params.size(); i++) {
cmdLineStr += params.get(i).toString() + " ";
}
if (cmdLineStr.indexOf(XWTools.QUOTE) != -1) {
throw new ParseException(
"6 dec 2005 : command line cannot have \""
+ XWTools.QUOTE
+ "\" character until further notification", 0);
}
work.setCmdLine(cmdLineStr);
cmdLineStr = null;
if (args.getOption(CommandLineOptions.LABEL) != null) {
work.setLabel((String) args.getOption(CommandLineOptions.LABEL));
}
if (args.getOption(CommandLineOptions.EXPECTEDHOST) != null) {
work.setExpectedHost((UID) args
.getOption(CommandLineOptions.EXPECTEDHOST));
}
if (args.getOption(CommandLineOptions.SESSION) != null) {
work.setSession((UID) args
.getOption(CommandLineOptions.SESSION));
}
if (args.getOption(CommandLineOptions.GROUP) != null) {
work.setGroup((UID) args.getOption(CommandLineOptions.GROUP));
}
File certFile = null;
certFile = config.getFile(XWPropertyDefs.X509USERPROXY);
if (args.getOption(CommandLineOptions.CERT) != null) {
try {
URI uri = (URI) args.getOption(CommandLineOptions.CERT);
if (uri.isFile() == false) {
work.setUserProxy(uri);
certFile = null;
logger.debug("cert uri " + uri);
} else {
certFile = new File(uri.getPath());
}
uri = null;
} catch (final ClassCastException e) {
try {
work.setUserProxy(commClient().newURI(
(UID) args.getOption(CommandLineOptions.CERT)));
certFile = null;
logger.debug("cert uid " + work.getUserProxy());
} catch (final ClassCastException e2) {
certFile = new File(
(String) args
.getOption(CommandLineOptions.CERT));
}
}
}
if (certFile != null) {
try {
XWTools.checkCertificate(certFile);
} catch (final Exception e) {
if (args.getOption(CommandLineOptions.CERT) != null) {
exit("Invalid certificate : " + e, XWReturnCode.FATAL);
} else {
logger.info("Will not use $"
+ XWPropertyDefs.X509USERPROXY + " : " + e);
certFile = null;
}
}
if (certFile != null) {
final URI certUri = sendData(OSEnum.NONE, CPUEnum.NONE,
DataTypeEnum.X509, XWAccessRights.USERALL, new URI(
"file://" + certFile.getCanonicalPath()),
certFile.getName());
work.setUserProxy(certUri);
}
}
params.clear();
params = null;
}
if (work != null) {
try {
commClient().send(work);
println(commClient().newURI(work.getUID()));
} catch (final ClassNotFoundException e) {
try {
if (work.getStdin() != null) {
commClient().remove(work.getStdin());
}
} catch (final Exception e1) {
}
try {
if (work.getDirin() != null) {
commClient().remove(work.getDirin());
}
} catch (final Exception e2) {
}
throw e;
}
}
zipFile = null;
work = null;
}
/**
* This pings the server
*
* @see #synchronize()
*/
private void ping() {
CommClient client = null;
try {
client = commClient();
while (true) {
final long start = System.currentTimeMillis();
client.ping();
final long end = System.currentTimeMillis();
final int pingdelai = (int) (end - start);
println("Ping to " + config.getCurrentDispatcher() + ": time="
+ pingdelai + " ms");
Thread.sleep(1000);
}
} catch (final Exception e) {
client = null;
logger.exception(e);
}
}
/**
* This is for communications testing
* Command line parameters : --xwworkrequest
*/
private void workRequest() throws IOException {
final List params = (List) args.commandParams();
try {
final WorkInterface work = commClient().workRequest(
config.getHost());
println("Work received: " + work.toXml());
} catch (final Exception e) {
if (e instanceof SAXException) {
logger.info("Server gave no work to compute");
} else {
logger.exception(e);
}
}
}
/**
* This is for communications testing
* Command line parameters : --xwworkalive [UID]
*/
private void workAlive() throws IOException {
final List params = (List) args.commandParams();
try {
final Hashtable rmiResults = commClient().workAlive(
(UID) params.get(0)).getHashtable();
final Boolean keepWorking = (Boolean) rmiResults.get("keepWorking");
println("alive returns: " + rmiResults.toString());
if (keepWorking != null) {
println("Alive to " + config.getCurrentDispatcher()
+ " : keepWorking = " + keepWorking);
}
} catch (final Exception e) {
//
// a NullPointerException is thrown if user gave no parameter
// on the command line
// even if there may be other exceptions, we just bypass them
//
final Collection jobResults = new Vector();
println("Alive to " + config.getCurrentDispatcher()
+ " : jobResults.size() = " + jobResults.size());
final Hashtable rmiParams = new Hashtable();
rmiParams.put(XWPostParams.JOBRESULTS.toString(), jobResults);
jobResults.clear();
Hashtable rmiResults = null;
try {
rmiResults = commClient().workAlive(rmiParams).getHashtable();
} catch (final Exception e2) {
logger.exception(e2);
exit("Alive to " + config.getCurrentDispatcher()
+ " : connection error " + e2.toString(),
XWReturnCode.CONNECTION);
return;
}
rmiParams.clear();
if (rmiResults == null) {
exit("Alive to " + config.getCurrentDispatcher()
+ " : rmiResults = null", XWReturnCode.CONNECTION);
return;
}
logger.debug(rmiResults.toString());
final String serverVersion = (String) rmiResults
.get(XWPostParams.CURRENTVERSION.toString());
logger.debug("serverVersion = " + serverVersion);
if (serverVersion != null) {
println("Server version : " + serverVersion);
}
final List finishedTasks = (List) rmiResults
.get(XWPostParams.FINISHEDTASKS.toString());
if (finishedTasks != null) {
println("Alive to " + config.getCurrentDispatcher()
+ " : finishedTasks.size() = " + finishedTasks.size());
final Iterator li = finishedTasks.iterator();
while (li.hasNext()) {
final UID uid = li.next();
if (uid != null) {
println("Alive to " + config.getCurrentDispatcher()
+ " : finishedTasks = " + uid);
}
}
}
final List resultsExpected = (List) rmiResults
.get(XWPostParams.RESULTEXPECTEDS.toString());
if (resultsExpected != null) {
println("Alive to " + config.getCurrentDispatcher()
+ " : resultsExpected.size() = "
+ resultsExpected.size());
final Iterator li = resultsExpected.iterator();
while (li.hasNext()) {
final UID uid = li.next();
if (uid != null) {
println("Alive to " + config.getCurrentDispatcher()
+ " : resultExpected = " + uid);
}
}
}
final String newServer = (String) rmiResults
.get(XWPostParams.NEWSERVER.toString());
if (newServer != null) {
println("Alive to " + config.getCurrentDispatcher()
+ " : new server = " + newServer);
}
final Boolean traces = (Boolean) rmiResults.get(XWPostParams.TRACES
.toString());
if (traces != null) {
println("Alive to " + config.getCurrentDispatcher()
+ " : tracing = " + traces);
}
final Integer tracesSendResultDelay = (Integer) rmiResults
.get(XWPostParams.TRACESSENDRESULTDELAY.toString());
if (tracesSendResultDelay != null) {
println("Alive to " + config.getCurrentDispatcher()
+ " : tracesSendResultDelay = " + tracesSendResultDelay);
}
final Integer tracesResultDelay = (Integer) rmiResults
.get(XWPostParams.TRACESRESULTDELAY.toString());
if (tracesResultDelay != null) {
println("Alive to " + config.getCurrentDispatcher()
+ " : tracesResultDelay = " + tracesResultDelay);
}
final Integer newAlivePeriod = (Integer) rmiResults
.get(XWPostParams.ALIVEPERIOD.toString());
if (newAlivePeriod != null) {
println("Alive to " + config.getCurrentDispatcher()
+ ": new alive period = " + newAlivePeriod);
}
final String keystoreUriStr = (String) rmiResults
.get(XWPostParams.KEYSTOREURI.toString());
if ((keystoreUriStr != null) && (keystoreUriStr.length() > 0)) {
try {
final URI keystoreUri = new URI(keystoreUriStr);
final File currentKeystoreFile = new File(
System.getProperty(XWPropertyDefs.JAVAKEYSTORE
.toString()));
final DataInterface newKeystoreData = getData(keystoreUri);
if (newKeystoreData == null) {
throw new IOException(
"Can't retrieve new keystore data "
+ keystoreUri);
}
final String currentKeystoreMD5 = MD5.asHex(MD5
.getHash(currentKeystoreFile));
if (newKeystoreData.getMD5().compareTo(currentKeystoreMD5) != 0) {
println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
println("* *");
println("* ATTENTION *");
println("* There is a new keystore available from server *");
println("* *");
println("* You must download " + keystoreUriStr);
println("* and copy it to " + currentKeystoreFile);
println("* *");
println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
}
} catch (final Exception e2) {
logger.exception("can't check keystore", e2);
}
}
}
if (params != null) {
params.clear();
}
}
/**
* This retrieves the SmartSockets hub address Command line
* parameters : --xwgethubaddr
*
* @since 8.0.0
*/
private String getHubAddr(boolean display) throws IOException {
try {
final Hashtable rmiResults = commClient()
.getHubAddress().getHashtable();
final String hubAddr = rmiResults.get(Connection.HUBPNAME);
if (display) {
println("SmartSockets hub addr = "
+ (hubAddr == null ? "unknown" : hubAddr));
}
return hubAddr;
} catch (final Exception e) {
logger.exception("Can't retrieve SmartSocket hub address", e);
throw new IOException("Can't retrieve SmartSocket hub address");
}
}
/**
* This updates a worker in server by setting its status to true or false
* Command line parameters : --xwupdateworkers <on|off>
* [uids list]
Administrator privileges required
*/
private void updateWorkers() throws IOException {
final Collection uids = (Collection) args.commandParams();
if (uids == null) {
throw new IOException("no param provided");
}
boolean status = false;
final Iterator li = uids.iterator();
final String onoff = (String) li.next();
if (onoff == null) {
throw new IOException("missing valid parameter : on | off");
}
if ((onoff.compareTo("on") == 0) || (onoff.compareTo("1") == 0)
|| (onoff.compareTo(Boolean.toString(true)) == 0)
|| (onoff.compareTo("yes") == 0)) {
status = true;
} else if ((onoff.compareTo("off") == 0) || (onoff.compareTo("0") == 0)
|| (onoff.compareTo(Boolean.toString(false)) == 0)
|| (onoff.compareTo("no") == 0)) {
status = false;
} else {
throw new IOException("invalid parameter : " + onoff);
}
while (li.hasNext()) {
final UID uid = (UID) li.next();
try {
commClient().activateHost(uid, status);
} catch (final Exception e) {
throw new IOException(e.getMessage());
}
}
uids.clear();
}
/**
* This executes requested actions.
* It first tries to connect to the XtremWeb dispatcher.
*/
private void execute() throws IOException {
try {
mileStone = new MileStone(getClass());
} catch (final Exception e) {
logger.exception(e);
if (args.getOption(CommandLineOptions.GUI) == null) {
exit("Can't init comm : " + e, XWReturnCode.FATAL);
}
}
if (config != null) {
String pass = null;
String certificate = null;
try {
final boolean challenge = config.getChallenging();
pass = config.getUser().getPassword();
certificate = config.getUser().getCertificate();
config.setUser(getUser(config.getUser().getLogin()));
config.getUser().setPassword(pass);
config.getUser().setCertificate(certificate);
config.setProperty(XWPropertyDefs.USERUID, config.getUser()
.getUID());
config.setChallenging(challenge);
config.getUser().setChallenging(challenge);
CommClient.setConfig(config);
} catch (final SSLHandshakeException e) {
logger.exception(e);
handshakeError();
} catch (final InvalidKeyException e) {
logger.exception(e);
exit(e.getMessage(), XWReturnCode.AUTHENTICATION);
} catch (final AccessControlException e) {
logger.exception(e);
exit(e.getMessage(), XWReturnCode.AUTHORIZATION);
} catch (final IOException e) {
logger.exception(e);
if (args.getOption(CommandLineOptions.GUI) == null) {
connectionRefused();
}
} catch (final SAXException e) {
connectionRefused();
} catch (final Exception e) {
logger.exception(e);
} finally {
pass = null;
certificate = null;
}
}
if (args.getOption(CommandLineOptions.GUI) != null) {
if (config == null) {
config = new XWConfigurator();
CommClient.setConfig(config);
}
final xtremweb.client.gui.MainFrame frame = new xtremweb.client.gui.MainFrame(
this);
frame.pack();
frame.setVisible(true);
} else {
try {
if (args.getOption(CommandLineOptions.SHELL) != null) {
execShell();
}
if (args.getOption(CommandLineOptions.SMARTSOCKETSPROXY) != null) {
smartSocketsProxy();
} else if (args.getOption(CommandLineOptions.MACRO) != null) {
execMacros();
} else {
doItNow();
}
final CommClient client = commClient();
client.setAutoClose(true);
client.disconnect();
} catch (final SSLHandshakeException e) {
handshakeError();
} catch (final Exception e) {
exit(e.getMessage(), XWReturnCode.CONNECTION);
}
}
}
/**
* This is finally where everything takes place. This is called at least
* once, or once per line found in "macro" file, if any.
*
* @param in
* is the input stream representing the standard input for the
* job
*/
private void doItNow() {
Collection ret = null;
try {
logger.debug("doItNow " + args.command());
switch (args.command()) {
case VERSION:
println("Current version : " + CommonVersion.getCurrent());
try {
final Version serverVersion = commClient().version();
logger.debug("serverVersion = " + serverVersion);
if ((serverVersion != null)
&& !serverVersion.toString().equals(
CommonVersion.getCurrent().toString())) {
println("Server version : " + serverVersion);
println("********** ********** **********");
println("You should upgrade your client");
println("********** ********** **********");
}
} catch (final Exception e) {
logger.exception(e);
}
exit("", XWReturnCode.SUCCESS);
break;
case GET:
get();
break;
case GETAPPS:
ret = getApps(true);
break;
case SENDAPP:
sendApp();
break;
case REMOVE:
remove();
break;
case GETDATAS:
ret = getDatas(true);
break;
case SENDDATA:
sendData();
break;
case GETGROUPS:
ret = getGroups(true);
break;
case SENDGROUP:
sendGroup();
break;
case GETHOSTS:
ret = getHosts(true);
break;
case SENDHOST:
sendHost();
break;
case GETSESSIONS:
ret = getSessions(true);
break;
case SENDSESSION:
sendSession();
break;
case GETTASK:
get();
break;
case GETTASKS:
ret = getTasks(true);
break;
case GETTRACES:
ret = getTraces(true);
break;
case SENDTRACE:
sendTrace();
break;
case GETUSERBYLOGIN:
getUser();
break;
case GETUSERS:
ret = getUsers(true);
break;
case SENDUSER:
sendUser();
break;
case GETUSERGROUPS:
ret = getUserGroups(true);
break;
case SENDUSERGROUP:
sendUserGroup();
break;
case GETWORKS:
ret = getWorks(true);
break;
case GETGROUPWORKS:
ret = getGroupWorks(true);
break;
case SENDWORK:
sendWork();
break;
case PING:
ping();
break;
case WORKALIVE:
case WORKALIVEBYUID:
workAlive();
break;
case WORKREQUEST:
workRequest();
break;
case CHMOD:
chmod();
break;
case GETHUBADDR:
getHubAddr(true);
break;
case ACTIVATEHOST:
updateWorkers();
break;
}
} catch (final SSLHandshakeException ssle) {
logger.exception(ssle);
handshakeError();
} catch (final ParseException ce) {
logger.exception(ce);
usage(args.command(), ce.getMessage());
} catch (final ConnectException ce) {
logger.exception(ce);
connectionRefused();
} catch (final ClassNotFoundException e) {
logger.exception(e);
exit(e.getMessage(), XWReturnCode.DISK);
} catch (final CertificateExpiredException e) {
logger.exception(e);
exit(e.getMessage(), XWReturnCode.AUTHENTICATION);
} catch (final CertificateException e) {
logger.exception(e);
exit(e.getMessage(), XWReturnCode.AUTHENTICATION);
} catch (final InvalidKeyException e) {
logger.exception(e);
exit(e.getMessage(), XWReturnCode.AUTHENTICATION);
} catch (final AccessControlException e) {
logger.exception(e);
exit(e.getMessage(), XWReturnCode.AUTHORIZATION);
} catch (final FileNotFoundException e) {
logger.exception(e);
exit(e.getMessage(), XWReturnCode.DISK);
} catch (final IOException e) {
logger.exception(e);
objectNotFound();
} catch (final Exception e) {
logger.exception(e);
usage(args.command());
} finally {
if (ret != null) {
ret.clear();
}
ret = null;
}
}
/**
* This executes what reader provides
*
* @see #doItNow()
* @since 7.0.0
*/
public IdRpc exec(BufferedReader reader) throws ParseException,
IOException, FileNotFoundException {
final String line = reader.readLine();
logger.finest("line = " + line);
final Collection params = XWTools.split(line);
if ((params == null) || (params.size() < 1)) {
return null;
}
final String[] arrayType = new String[0];
final String[] paramsarray = (String[]) params.toArray(arrayType);
args = new CommandLineParser(paramsarray);
doItNow();
return args.command();
}
/**
* This parses "macro" file and executes each line calling doItNow()
* Command line parameters : --xwmacro <macro file name>
*
*
* @see #doItNow()
*/
private void execMacros() {
executingMacro = true;
final boolean noverbose = (args.getOption(CommandLineOptions.NOVERBOSE) != null);
logger.debug("macro = "
+ args.getOption(CommandLineOptions.MACRO).toString());
macroFile = new File((String) args.getOption(CommandLineOptions.MACRO));
if (args.xml()) {
printXMLHeader();
}
try {
final BufferedReader reader = new BufferedReader(new FileReader(
macroFile));
while (true) {
final IdRpc cmd = exec(reader);
if (cmd == null) {
break;
}
if (noverbose == false) {
logger.info("macro [" + ++macroLineNumber + "] " + "("
+ cmd + ") ");
}
}
} catch (final Exception e) {
exit(e.getMessage(), XWReturnCode.PARSING);
}
}
/**
* This opens a socket and wait for incoming connections. This parses socket
* input stream and executes the command by calling doItNow()
*
* @see #doItNow()
* @since 7.0.0
*/
private void execShell() throws InterruptedException {
shellRunning = true;
try {
final Shell shell = new Shell();
shell.initComm(this);
shell.start();
shell.join();
} finally {
shellRunning = false;
}
}
/**
* This instantiates SmartSockets proxies. If CommandLineOptions.FORWARDPORT
* is set, let start a server like proxy waiting incoming connections from
* local SmartSocketAddress and forwarding them to a local port so that jobs
* running on XWHEP worker will be able to connect on server like
* application running on XWHEP client side.
*
* If CommandLineOptions.FORWARDADDRESSES is set, let start a client like
* proxy waiting incoming connections from local port and forwarding them to
* remote SmartSocketAddress so that applications running on XWHEP client
* side have a chance to connect on server like application running on XWHEP
* worker side.
*
* @throws URISyntaxException
* @throws SAXException
* @throws ClassNotFoundException
* @throws IOException
* @throws AccessControlException
* @throws InvalidKeyException
* @since 8.0.0
*/
private void smartSocketsProxy() throws InterruptedException,
InvalidKeyException, AccessControlException, IOException,
ClassNotFoundException, SAXException, URISyntaxException {
final Vector params = (Vector) args.commandParams();
String hubAddr = null;
try {
final Boolean b = (Boolean) args
.getOption(CommandLineOptions.SMARTSOCKETSPROXY);
hubAddr = getHubAddr(false);
} catch (final Exception e) {
hubAddr = (String) args
.getOption(CommandLineOptions.SMARTSOCKETSPROXY);
}
logger.debug("hubaddr = " + hubAddr);
if ((hubAddr == null) || (hubAddr.length() < 1)) {
exit("No SmartSockets hub address provided", XWReturnCode.PARSING);
}
int forwardPort = -1;
try {
final String forwardPortStr = (String) args
.getOption(CommandLineOptions.FORWARDPORT);
if (forwardPortStr != null) {
forwardPort = Integer.parseInt(forwardPortStr);
}
} catch (final Exception e1) {
exit("Invalid forwarding port : " + e1.getMessage(),
XWReturnCode.PARSING);
}
if (forwardPort != -1) {
try {
final SmartSocketsProxy smartSocketsProxy = new SmartSocketsProxy(
hubAddr, null, forwardPort, true);
println("Starting proxy; listening address : "
+ smartSocketsProxy.getLocalAddress()
+ ", forwarding port : " + forwardPort);
smartSocketsProxy.start();
} catch (final Exception e) {
logger.exception("Can't start new SmartSocket proxy", e);
XWTools.releasePort(forwardPort);
}
return;
}
int listenPort = -1;
try {
listenPort = Integer.parseInt((String) args
.getOption(CommandLineOptions.LISTENPORT));
} catch (final Exception e1) {
exit("Invalid listening port : " + e1.getMessage(),
XWReturnCode.PARSING);
}
int portlimit;
for (portlimit = 0; portlimit < 1000; portlimit++) {
logger.debug("Checking port availability " + listenPort);
if (XWTools.lockPort(listenPort)) {
break;
}
listenPort++;
}
if (portlimit == 1000) {
throw new IOException("can't find any available port to listen to");
}
final String forwardAddr = (String) args
.getOption(CommandLineOptions.FORWARDADDRESSES);
if (forwardAddr != null) {
try {
final SmartSocketsProxy smartSocketsProxy = new SmartSocketsProxy(
hubAddr, forwardAddr, listenPort, false);
println("Starting proxy; listening port : " + listenPort
+ ", forwarding address : "
+ smartSocketsProxy.getRemoteAddress());
smartSocketsProxy.start();
} catch (final Exception e) {
logger.exception("Can't start new SmartSocket proxy", e);
XWTools.releasePort(forwardPort);
}
return;
}
WorkInterface theWork = null;
try {
final Enumeration theEnum = params.elements();
if (theEnum.hasMoreElements()) {
final Object param = theEnum.nextElement();
try {
theWork = (WorkInterface) get((URI) param);
} catch (final ClassCastException cce) {
theWork = (WorkInterface) get((UID) param);
}
}
} catch (final Exception e1) {
exit("No valid URI/UID provided", XWReturnCode.PARSING);
}
if (theWork.getStatus() != StatusEnum.RUNNING) {
exit("Ivalid work status", XWReturnCode.ERROR);
}
final Collection serverAddresses = XWTools.split(
theWork.getSmartSocketAddr(), ",");
if ((serverAddresses == null) || (serverAddresses.size() < 1)) {
exit("No valid server addresses found from work",
XWReturnCode.CONNECTION);
}
final Iterator addressesenum = serverAddresses.iterator();
int portloop = 0;
while (addressesenum.hasNext()) {
final String serverAddr = addressesenum.next();
final int fport = listenPort + portloop++;
try {
final SmartSocketsProxy smartSocketsProxy = new SmartSocketsProxy(
hubAddr, serverAddr, fport, false);
println("Starting proxy; listening port : " + fport
+ ", forwarding address : "
+ smartSocketsProxy.getLocalAddress());
smartSocketsProxy.start();
} catch (final Exception e) {
logger.exception("Can't start new SmartSocket proxy", e);
XWTools.releasePort(fport);
}
}
}
/**
* This is the standard main method
*/
public static void main(String[] argv) {
try {
final Client client = new Client(argv);
client.verbose();
client.execute();
} catch (final Exception e) {
System.err.println(e.getMessage());
}
}
}