001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2004 Danet GmbH (www.danet.de), GS-AN.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: UploadXPDL.java,v 1.5 2007/06/03 19:43:43 mlipp Exp $
021: *
022: * $Log: UploadXPDL.java,v $
023: * Revision 1.5 2007/06/03 19:43:43 mlipp
024: * Improved problem reporting.
025: *
026: * Revision 1.4 2007/03/27 21:59:44 mlipp
027: * Fixed lots of checkstyle warnings.
028: *
029: * Revision 1.3 2007/03/22 13:40:59 schnelle
030: * Cleanup of output, in case of an import error.
031: *
032: * Revision 1.2 2006/10/11 09:14:58 drmlipp
033: * Fixed error reporting.
034: *
035: * Revision 1.1 2006/10/02 08:28:01 mlipp
036: * Moved upload utility.
037: *
038: * Revision 1.3 2006/10/01 21:51:50 mlipp
039: * Added process upload to installer.
040: *
041: * Revision 1.2 2006/09/29 12:32:10 drmlipp
042: * Consistently using WfMOpen as projct name now.
043: *
044: * Revision 1.1.1.1 2004/08/18 15:17:39 drmlipp
045: * Update to 1.2
046: *
047: * Revision 1.2 2004/03/18 19:22:30 lipp
048: * Added support for lookup of process description as resource.
049: *
050: * Revision 1.1 2004/03/18 19:02:56 lipp
051: * Added upload utility.
052: *
053: */
054: package de.danet.an.workflow.clients.cmdline;
055:
056: import java.io.BufferedInputStream;
057: import java.io.ByteArrayOutputStream;
058: import java.io.FileInputStream;
059: import java.io.IOException;
060: import java.io.InputStream;
061:
062: import java.util.Iterator;
063: import java.util.List;
064:
065: import javax.security.auth.callback.Callback;
066: import javax.security.auth.callback.CallbackHandler;
067: import javax.security.auth.callback.NameCallback;
068: import javax.security.auth.callback.PasswordCallback;
069: import javax.security.auth.callback.TextOutputCallback;
070: import javax.security.auth.callback.UnsupportedCallbackException;
071: import javax.security.auth.login.LoginContext;
072: import javax.security.auth.login.LoginException;
073:
074: import de.danet.an.workflow.api.FactoryConfigurationError;
075: import de.danet.an.workflow.api.ImportException;
076: import de.danet.an.workflow.api.PrioritizedMessage;
077: import de.danet.an.workflow.api.ProcessDefinitionDirectory;
078: import de.danet.an.workflow.api.WorkflowService;
079: import de.danet.an.workflow.api.WorkflowServiceFactory;
080:
081: /**
082: * This is a simple tool for uploading a process description.
083: *
084: * @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
085: * @version $Revision: 1.5 $
086: */
087:
088: public class UploadXPDL {
089:
090: /**
091: * Simple login context for authentication.
092: */
093: private static class UploadLoginContext extends LoginContext {
094:
095: private static class CBH implements CallbackHandler {
096: private String userName = null;
097: private String password = null;
098:
099: public CBH(String userName, String password) {
100: this .userName = userName;
101: this .password = password;
102: }
103:
104: public void handle(Callback[] callbacks)
105: throws UnsupportedCallbackException, IOException {
106: for (int i = 0; i < callbacks.length; i++) {
107: if (callbacks[i] instanceof TextOutputCallback) {
108: // display the message according to the specified type
109: TextOutputCallback toc = (TextOutputCallback) callbacks[i];
110: switch (toc.getMessageType()) {
111: case TextOutputCallback.INFORMATION:
112: System.err.println(toc.getMessage());
113: break;
114: case TextOutputCallback.ERROR:
115: System.err.println("ERROR: "
116: + toc.getMessage());
117: break;
118: case TextOutputCallback.WARNING:
119: System.err.println("WARNING: "
120: + toc.getMessage());
121: break;
122: default:
123: throw new IOException(
124: "Unsupported message type: "
125: + toc.getMessageType());
126: }
127: } else if (callbacks[i] instanceof NameCallback) {
128: // prompt the user for a username
129: NameCallback nc = (NameCallback) callbacks[i];
130: nc.setName(userName);
131: } else if (callbacks[i] instanceof PasswordCallback) {
132: // prompt the user for sensitive information
133: PasswordCallback pc = (PasswordCallback) callbacks[i];
134: pc.setPassword(password.toCharArray());
135: } else if (callbacks[i]
136: .getClass()
137: .getName()
138: .equals(
139: "weblogic.security.auth.callback.URLCallback")) {
140: } else {
141: throw new UnsupportedCallbackException(
142: callbacks[i],
143: "Unrecognized Callback \""
144: + callbacks[i].getClass()
145: .getName() + "\"");
146: }
147: }
148: }
149: }
150:
151: public UploadLoginContext(String applicationPolicy,
152: String userName, String password) throws LoginException {
153: super (applicationPolicy, new CBH(userName, password));
154: }
155: }
156:
157: private void run(String[] args) throws Exception {
158: try {
159: LoginContext loginContext = new UploadLoginContext(args[0],
160: args[1], args[2]);
161: loginContext.login();
162:
163: boolean useRes = args[3].equals("-R");
164: doUpload(useRes ? args[4] : args[3], useRes);
165:
166: loginContext.logout();
167: } catch (LoginException e) {
168: System.err.println("Login error: " + e);
169: }
170: }
171:
172: private void doUpload(String fileName, boolean isResource) {
173: try {
174: InputStream is = null;
175: if (isResource) {
176: is = Thread.currentThread().getContextClassLoader()
177: .getResourceAsStream(fileName);
178: } else {
179: is = new BufferedInputStream(new FileInputStream(
180: fileName));
181: }
182: byte[] buf = new byte[2048];
183: int count;
184: ByteArrayOutputStream out = new ByteArrayOutputStream();
185: while (true) {
186: count = is.read(buf);
187: if (count < 0) {
188: break;
189: }
190: out.write(buf, 0, count);
191: }
192: out.close();
193:
194: int tries = 20;
195: for (; tries > 0; tries--) {
196: try {
197: WorkflowServiceFactory wfsf = WorkflowServiceFactory
198: .newInstance();
199: WorkflowService wfs = wfsf.newWorkflowService();
200: ProcessDefinitionDirectory pdd = wfs
201: .processDefinitionDirectory();
202: pdd.importProcessDefinitions(out.toByteArray());
203: break;
204: } catch (FactoryConfigurationError e) {
205: System.err
206: .println("Problem accessing engine, may not be fully deployed "
207: + "yet. Retrying "
208: + tries
209: + " more time(s). "
210: + "(Problem was: "
211: + e.getMessage()
212: + ")");
213: try {
214: Thread.sleep(2500);
215: } catch (InterruptedException ie) {
216: // Ignored
217: }
218: }
219: }
220: if (tries == 0) {
221: System.err
222: .println("Cannot upload process definitions.");
223: System.exit(1);
224: }
225: } catch (IOException e) {
226: System.err.println("Problem reading file: "
227: + e.getMessage());
228: } catch (ImportException e) {
229: List msgs = e.messages();
230: for (Iterator i = msgs.iterator(); i.hasNext();) {
231: System.err.println("ImportException: "
232: + ((PrioritizedMessage) i.next()).message());
233: }
234: }
235: }
236:
237: public static void main(String[] args) {
238: try {
239: if (args.length < 4) {
240: System.err
241: .println("Usage: java de.danet.an.workflow.util.UploadXPDL "
242: + "login_domain user password [-R] file_or_resource");
243: System.exit(-1);
244: }
245: (new UploadXPDL()).run(args);
246: } catch (Exception e) {
247: e.printStackTrace();
248: }
249: }
250:
251: }
|