/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2004 Danet GmbH (www.danet.de), GS-AN.
* All rights reserved.
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: UploadXPDL.java 2406 2007-06-03 19:43:43Z mlipp $
*
* $Log$
* Revision 1.4 2007/03/27 21:59:44 mlipp
* Fixed lots of checkstyle warnings.
*
* Revision 1.3 2007/03/22 13:40:59 schnelle
* Cleanup of output, in case of an import error.
*
* Revision 1.2 2006/10/11 09:14:58 drmlipp
* Fixed error reporting.
*
* Revision 1.1 2006/10/02 08:28:01 mlipp
* Moved upload utility.
*
* Revision 1.3 2006/10/01 21:51:50 mlipp
* Added process upload to installer.
*
* Revision 1.2 2006/09/29 12:32:10 drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.1.1.1 2004/08/18 15:17:39 drmlipp
* Update to 1.2
*
* Revision 1.2 2004/03/18 19:22:30 lipp
* Added support for lookup of process description as resource.
*
* Revision 1.1 2004/03/18 19:02:56 lipp
* Added upload utility.
*
*/
package de.danet.an.workflow.clients.cmdline;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.TextOutputCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import de.danet.an.workflow.api.FactoryConfigurationError;
import de.danet.an.workflow.api.ImportException;
import de.danet.an.workflow.api.PrioritizedMessage;
import de.danet.an.workflow.api.ProcessDefinitionDirectory;
import de.danet.an.workflow.api.WorkflowService;
import de.danet.an.workflow.api.WorkflowServiceFactory;
/**
* This is a simple tool for uploading a process description.
*
* @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
* @version $Revision: 2406 $
*/
public class UploadXPDL {
/**
* Simple login context for authentication.
*/
private static class UploadLoginContext extends LoginContext {
private static class CBH implements CallbackHandler {
private String userName = null;
private String password = null;
public CBH (String userName, String password) {
this.userName = userName;
this.password = password;
}
public void handle (Callback[] callbacks)
throws UnsupportedCallbackException, IOException {
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof TextOutputCallback) {
// display the message according to the specified type
TextOutputCallback toc
= (TextOutputCallback)callbacks[i];
switch (toc.getMessageType()) {
case TextOutputCallback.INFORMATION:
System.err.println(toc.getMessage());
break;
case TextOutputCallback.ERROR:
System.err.println("ERROR: " + toc.getMessage());
break;
case TextOutputCallback.WARNING:
System.err.println("WARNING: " + toc.getMessage());
break;
default:
throw new IOException
("Unsupported message type: "
+ toc.getMessageType());
}
} else if (callbacks[i] instanceof NameCallback) {
// prompt the user for a username
NameCallback nc = (NameCallback)callbacks[i];
nc.setName(userName);
} else if (callbacks[i] instanceof PasswordCallback) {
// prompt the user for sensitive information
PasswordCallback pc = (PasswordCallback)callbacks[i];
pc.setPassword(password.toCharArray());
} else if (callbacks[i].getClass().getName().equals
("weblogic.security.auth.callback.URLCallback")){
} else {
throw new UnsupportedCallbackException
(callbacks[i], "Unrecognized Callback \""
+ callbacks[i].getClass().getName() + "\"");
}
}
}
}
public UploadLoginContext
(String applicationPolicy, String userName, String password)
throws LoginException {
super (applicationPolicy, new CBH(userName, password));
}
}
private void run (String[] args) throws Exception {
try {
LoginContext loginContext
= new UploadLoginContext (args[0], args[1], args[2]);
loginContext.login ();
boolean useRes = args[3].equals ("-R");
doUpload (useRes ? args[4] : args[3], useRes);
loginContext.logout ();
} catch (LoginException e) {
System.err.println("Login error: " + e);
}
}
private void doUpload (String fileName, boolean isResource) {
try {
InputStream is = null;
if (isResource) {
is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream (fileName);
} else {
is = new BufferedInputStream (new FileInputStream (fileName));
}
byte[] buf = new byte[2048];
int count;
ByteArrayOutputStream out = new ByteArrayOutputStream ();
while (true) {
count = is.read(buf);
if (count < 0) {
break;
}
out.write (buf, 0, count);
}
out.close ();
int tries = 20;
for (; tries > 0; tries--) {
try {
WorkflowServiceFactory wfsf
= WorkflowServiceFactory.newInstance ();
WorkflowService wfs = wfsf.newWorkflowService();
ProcessDefinitionDirectory pdd
= wfs.processDefinitionDirectory();
pdd.importProcessDefinitions(out.toByteArray());
break;
} catch (FactoryConfigurationError e) {
System.err.println
("Problem accessing engine, may not be fully deployed "
+ "yet. Retrying " + tries + " more time(s). "
+ "(Problem was: " + e.getMessage() + ")");
try {
Thread.sleep(2500);
} catch (InterruptedException ie) {
// Ignored
}
}
}
if (tries == 0) {
System.err.println ("Cannot upload process definitions.");
System.exit(1);
}
} catch (IOException e) {
System.err.println ("Problem reading file: " + e.getMessage ());
} catch (ImportException e) {
List msgs = e.messages();
for (Iterator i = msgs.iterator(); i.hasNext ();) {
System.err.println
("ImportException: "
+ ((PrioritizedMessage)i.next()).message());
}
}
}
public static void main (String[] args) {
try {
if (args.length < 4) {
System.err.println
("Usage: java de.danet.an.workflow.util.UploadXPDL "
+ "login_domain user password [-R] file_or_resource");
System.exit(-1);
}
(new UploadXPDL()).run (args);
} catch (Exception e) {
e.printStackTrace();
}
}
}