001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: *
023: * $Id: CmdFileParser.java 7382 2007-05-15 05:33:34Z lzheng $
024: */
025: package com.bostechcorp.cbesb.runtime.ftp;
026:
027: import java.io.File;
028: import java.io.FileNotFoundException;
029: import java.io.IOException;
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: import javax.xml.parsers.DocumentBuilder;
034: import javax.xml.parsers.DocumentBuilderFactory;
035: import javax.xml.parsers.ParserConfigurationException;
036:
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039: import org.w3c.dom.DOMException;
040: import org.w3c.dom.Document;
041: import org.w3c.dom.Element;
042: import org.w3c.dom.Node;
043: import org.w3c.dom.NodeList;
044: import org.xml.sax.SAXException;
045:
046: import com.bostechcorp.cbesb.common.util.ErrorUtil;
047: import com.bostechcorp.cbesb.runtime.ftp.interpreter.CommandInterpreter;
048:
049: /**
050: * @author j.zhang
051: * @version 1.0.0
052: */
053: public class CmdFileParser {
054:
055: protected final transient Log logger = LogFactory
056: .getLog(getClass());
057:
058: protected static final String FTP_REQUEST = "ftp_request";
059:
060: protected static final String XML_NS = "xmlns";
061:
062: protected static final String CMD_NAMESPACE = "http://cbesb.bostechcorp.com/wsdl/ftp/1.0";
063:
064: protected static final String COMMANDS = "commands";
065:
066: protected static final String CONNECT = "connect";
067:
068: protected static final String HOST = "host";
069:
070: protected static final String PORT = "port";
071:
072: protected static final String DISCONNECT = "disconnect";
073:
074: protected static final String LOGIN = "login";
075:
076: protected static final String USER = "user";
077:
078: protected static final String PASSWORD = "password";
079:
080: protected static final String LOGOUT = "logout";
081:
082: protected static final String SITE_COMMAND = "siteCommand";
083:
084: protected static final String SET_CONNECTION_MODE = "setConnectionMode";
085:
086: protected static final String SET_TRANSFER_MODE = "setTransferMode";
087:
088: protected static final String CHANGE_WORKING_DIR = "changeWorkingDir";
089:
090: protected static final String CHANGE_TO_PARENT_DIR = "changeToParentDir";
091:
092: protected static final String GET = "get";
093:
094: protected static final String DELETE_AFTER_TRANSFER = "deleteAfterTransfer";
095:
096: protected static final String LOCAL_NAME = "localName";
097:
098: protected static final String PUT = "put";
099:
100: protected static final String REMOTE_NAME = "remoteName";
101:
102: protected static final String DELETE_FILE = "deleteFile";
103:
104: protected static final String RENAME = "rename";
105:
106: protected static final String TO_NAME = "toName";
107:
108: protected static final String CREATE_DIRECTORY = "createDirectory";
109:
110: protected static final String REMOVE_DIRECTORY = "removeDirectory";
111:
112: protected static final String M_GET = "mget";
113:
114: protected static final String M_PUT = "mput";
115:
116: protected static final String M_DELETE_FILES = "mDeleteFiles";
117:
118: protected static final String CHANGE_LOCAL_WORKING_DIR = "changeLocalWorkingDir";
119:
120: protected static final String DELETE_LOCAL_FILE = "deleteLocalFile";
121:
122: protected static final String RENAME_LOCAL = "renameLocal";
123:
124: protected static final String CREATE_LOCAL_DIRECTORY = "createLocalDirectory";
125:
126: protected static final String REMOVE_LOCAL_DIRECTORY = "removeLocalDirectory";
127:
128: protected static final String M_DELETE_LOCAL_FILES = "mDeleteLocalFiles";
129:
130: /*
131: * public static void main(String arge[]) { long lasting =
132: * System.currentTimeMillis(); FTPClient ftp = new FTPClient(); File cmdFile =
133: * new File("c:\\documents and settings\\j.zhang\\my
134: * documents\\script.xml"); CommandInterpreter interpreter = new
135: * CommandInterpreter(); runCmdInterpreter(interpreter, cmdFile);
136: * interpreter.execute(ftp); System.out.println("Time: " +
137: * (System.currentTimeMillis() - lasting) + " ms"); }
138: */
139:
140: public static void runCmdInterpreter(
141: CommandInterpreter interpreter, File cmdFile) {
142: DocumentBuilderFactory domfac = DocumentBuilderFactory
143: .newInstance();
144: try {
145: DocumentBuilder dombuilder = domfac.newDocumentBuilder();
146: Document doc = dombuilder.parse(cmdFile);
147: runCmdInterpreter(interpreter, doc);
148: } catch (ParserConfigurationException pce) {
149: ErrorUtil.printError("Exception in runCmdInterpreter(): ",
150: pce);
151: } catch (SAXException saxe) {
152: ErrorUtil.printError("Exception in runCmdInterpreter(): ",
153: saxe);
154: } catch (IOException ioe) {
155: ErrorUtil.printError("Exception in runCmdInterpreter(): ",
156: ioe);
157: }
158:
159: }
160:
161: public static void runCmdInterpreter(
162: CommandInterpreter interpreter, Document doc) {
163: try {
164: Element ftp_request = doc.getDocumentElement();
165:
166: if (ftp_request.getLocalName().equals(FTP_REQUEST)
167: && ftp_request.getNamespaceURI().equals(
168: CMD_NAMESPACE)) {
169: NodeList commandsList = ftp_request.getChildNodes();
170: runCmdInterpreter(interpreter, commandsList);
171: } else {
172: throw new Exception(
173: "Invalid namespace or local name for ftp_request tag: {"
174: + ftp_request.getNamespaceURI() + "}"
175: + ftp_request.getLocalName() + "\"");
176:
177: }
178:
179: // if (!FTP_REQUEST.equals(ftp_request.getNodeName())) {
180: // throw new Exception("The root tag name: \"" +
181: // ftp_request.getNodeName()
182: // + "\" is invalid.");
183: // }
184: // if (!CMD_NAMESPACE.equals(ftp_request.getAttribute(XML_NS))) {
185: // throw new Exception("The root tag xmlns: \"" +
186: // ftp_request.getAttribute(XML_NS)
187: // + "\" is invalid.");
188: // }
189: // NodeList commandsList = ftp_request.getChildNodes();
190: // runCmdInterpreter(interpreter, commandsList);
191: } catch (ParserConfigurationException pce) {
192:
193: ErrorUtil.printError("Exception in runCmdInterpreter(): ",
194: pce);
195: } catch (FileNotFoundException fnfe) {
196: ErrorUtil.printError("Exception in runCmdInterpreter(): ",
197: fnfe);
198: } catch (SAXException saxe) {
199: ErrorUtil.printError("Exception in runCmdInterpreter(): ",
200: saxe);
201: } catch (IOException ioe) {
202: ErrorUtil.printError("Exception in runCmdInterpreter(): ",
203: ioe);
204: } catch (Exception e) {
205: ErrorUtil.printError("Exception in runCmdInterpreter(): ",
206: e);
207: }
208: }
209:
210: public static void runCmdInterpreter(
211: CommandInterpreter interpreter, NodeList commandsList) {
212: if (commandsList != null) {
213: for (int i = 0; i < commandsList.getLength(); i++) {
214: Node commands = commandsList.item(i);
215: if (commands.getNodeType() == Node.ELEMENT_NODE
216: && COMMANDS.equals(commands.getNodeName())) {
217: for (Node command = commands.getFirstChild(); command != null; command = command
218: .getNextSibling()) {
219: if (command.getNodeType() == Node.ELEMENT_NODE) {
220: if (CONNECT.equals(command.getNodeName())) {
221: Map<String, String> paramMap = new HashMap<String, String>();
222: if (command.getAttributes()
223: .getNamedItem(HOST) != null) {
224: paramMap.put(HOST, command
225: .getAttributes()
226: .getNamedItem(HOST)
227: .getNodeValue());
228: if (command.getAttributes()
229: .getNamedItem(PORT) != null) {
230: paramMap.put(PORT, command
231: .getAttributes()
232: .getNamedItem(PORT)
233: .getNodeValue());
234: }
235: }
236: interpreter.addCommand(CONNECT,
237: paramMap);
238: }
239: if (DISCONNECT
240: .equals(command.getNodeName())) {
241: Map<String, String> paramMap = new HashMap<String, String>();
242: interpreter.addCommand(DISCONNECT,
243: paramMap);
244: }
245: if (LOGIN.equals(command.getNodeName())) {
246: Map<String, String> paramMap = new HashMap<String, String>();
247: if (command.getAttributes()
248: .getNamedItem(USER) != null) {
249: paramMap.put(USER, command
250: .getAttributes()
251: .getNamedItem(USER)
252: .getNodeValue());
253: if (command.getAttributes()
254: .getNamedItem(PASSWORD) != null) {
255: paramMap.put(PASSWORD, command
256: .getAttributes()
257: .getNamedItem(PASSWORD)
258: .getNodeValue());
259: }
260: }
261: interpreter.addCommand(LOGIN, paramMap);
262: }
263: if (LOGOUT.equals(command.getNodeName())) {
264: Map<String, String> paramMap = new HashMap<String, String>();
265: interpreter
266: .addCommand(LOGOUT, paramMap);
267: }
268: if (SITE_COMMAND.equals(command
269: .getNodeName())) {
270: Map<String, String> paramMap = new HashMap<String, String>();
271: paramMap
272: .put(SITE_COMMAND, command
273: .getFirstChild()
274: .getNodeValue());
275: interpreter.addCommand(SITE_COMMAND,
276: paramMap);
277: }
278: if (SET_CONNECTION_MODE.equals(command
279: .getNodeName())) {
280: Map<String, String> paramMap = new HashMap<String, String>();
281: paramMap.put(SET_CONNECTION_MODE,
282: command.getFirstChild()
283: .getNodeValue());
284: interpreter.addCommand(
285: SET_CONNECTION_MODE, paramMap);
286: }
287: if (SET_TRANSFER_MODE.equals(command
288: .getNodeName())) {
289: Map<String, String> paramMap = new HashMap<String, String>();
290: paramMap
291: .put(SET_TRANSFER_MODE, command
292: .getFirstChild()
293: .getNodeValue());
294: interpreter.addCommand(
295: SET_TRANSFER_MODE, paramMap);
296: }
297: if (CHANGE_WORKING_DIR.equals(command
298: .getNodeName())) {
299: Map<String, String> paramMap = new HashMap<String, String>();
300: paramMap.put(CHANGE_WORKING_DIR,
301: command.getFirstChild()
302: .getNodeValue());
303: interpreter.addCommand(
304: CHANGE_WORKING_DIR, paramMap);
305: }
306: if (CHANGE_TO_PARENT_DIR.equals(command
307: .getNodeName())) {
308: Map<String, String> paramMap = new HashMap<String, String>();
309: interpreter.addCommand(
310: CHANGE_TO_PARENT_DIR, paramMap);
311: }
312: if (GET.equals(command.getNodeName())) {
313: Map<String, String> paramMap = new HashMap<String, String>();
314: paramMap
315: .put(GET, command
316: .getFirstChild()
317: .getNodeValue());
318: if (command.getAttributes()
319: .getNamedItem(LOCAL_NAME) != null) {
320: paramMap.put(LOCAL_NAME, command
321: .getAttributes()
322: .getNamedItem(LOCAL_NAME)
323: .getNodeValue());
324: }
325: if (command.getAttributes()
326: .getNamedItem(
327: DELETE_AFTER_TRANSFER) != null) {
328: paramMap
329: .put(
330: DELETE_AFTER_TRANSFER,
331: command
332: .getAttributes()
333: .getNamedItem(
334: DELETE_AFTER_TRANSFER)
335: .getNodeValue());
336: }
337: interpreter.addCommand(GET, paramMap);
338: }
339: if (PUT.equals(command.getNodeName())) {
340: Map<String, String> paramMap = new HashMap<String, String>();
341: paramMap
342: .put(PUT, command
343: .getFirstChild()
344: .getNodeValue());
345: if (command.getAttributes()
346: .getNamedItem(REMOTE_NAME) != null) {
347: paramMap.put(REMOTE_NAME, command
348: .getAttributes()
349: .getNamedItem(REMOTE_NAME)
350: .getNodeValue());
351: }
352: interpreter.addCommand(PUT, paramMap);
353: }
354: if (DELETE_FILE.equals(command
355: .getNodeName())) {
356: Map<String, String> paramMap = new HashMap<String, String>();
357: paramMap
358: .put(DELETE_FILE, command
359: .getFirstChild()
360: .getNodeValue());
361: interpreter.addCommand(DELETE_FILE,
362: paramMap);
363: }
364: if (RENAME.equals(command.getNodeName())) {
365: Map<String, String> paramMap = new HashMap<String, String>();
366: paramMap
367: .put(RENAME, command
368: .getFirstChild()
369: .getNodeValue());
370: try {
371: if (command.getAttributes()
372: .getNamedItem(TO_NAME) != null) {
373: paramMap.put(TO_NAME, command
374: .getAttributes()
375: .getNamedItem(TO_NAME)
376: .getNodeValue());
377: } else {
378: throw new Exception(
379: "The value of toName attribute is required.");
380: }
381: } catch (DOMException e) {
382: ErrorUtil
383: .printError(
384: "Exception in runCmdInterpreter(): ",
385: e);
386: } catch (Exception e) {
387: ErrorUtil
388: .printError(
389: "Exception in runCmdInterpreter(): ",
390: e);
391: }
392: interpreter
393: .addCommand(RENAME, paramMap);
394: }
395: if (CREATE_DIRECTORY.equals(command
396: .getNodeName())) {
397: Map<String, String> paramMap = new HashMap<String, String>();
398: paramMap
399: .put(CREATE_DIRECTORY, command
400: .getFirstChild()
401: .getNodeValue());
402: interpreter.addCommand(
403: CREATE_DIRECTORY, paramMap);
404: }
405: if (REMOVE_DIRECTORY.equals(command
406: .getNodeName())) {
407: Map<String, String> paramMap = new HashMap<String, String>();
408: paramMap
409: .put(REMOVE_DIRECTORY, command
410: .getFirstChild()
411: .getNodeValue());
412: interpreter.addCommand(
413: REMOVE_DIRECTORY, paramMap);
414: }
415: if (M_GET.equals(command.getNodeName())) {
416: Map<String, String> paramMap = new HashMap<String, String>();
417: paramMap
418: .put(M_GET, command
419: .getFirstChild()
420: .getNodeValue());
421: if (command.getAttributes()
422: .getNamedItem(
423: DELETE_AFTER_TRANSFER) != null) {
424: paramMap
425: .put(
426: DELETE_AFTER_TRANSFER,
427: command
428: .getAttributes()
429: .getNamedItem(
430: DELETE_AFTER_TRANSFER)
431: .getNodeValue());
432: }
433: if (command.getAttributes()
434: .getNamedItem(
435: DELETE_AFTER_TRANSFER) != null) {
436: paramMap
437: .put(
438: DELETE_AFTER_TRANSFER,
439: command
440: .getAttributes()
441: .getNamedItem(
442: DELETE_AFTER_TRANSFER)
443: .getNodeValue());
444: }
445: interpreter.addCommand(M_GET, paramMap);
446: }
447: if (M_PUT.equals(command.getNodeName())) {
448: Map<String, String> paramMap = new HashMap<String, String>();
449: paramMap
450: .put(M_PUT, command
451: .getFirstChild()
452: .getNodeValue());
453: interpreter.addCommand(M_PUT, paramMap);
454: }
455: if (M_DELETE_FILES.equals(command
456: .getNodeName())) {
457: Map<String, String> paramMap = new HashMap<String, String>();
458: paramMap
459: .put(M_DELETE_FILES, command
460: .getFirstChild()
461: .getNodeValue());
462: interpreter.addCommand(M_DELETE_FILES,
463: paramMap);
464: }
465: if (CHANGE_LOCAL_WORKING_DIR.equals(command
466: .getNodeName())) {
467: Map<String, String> paramMap = new HashMap<String, String>();
468: paramMap.put(CHANGE_LOCAL_WORKING_DIR,
469: command.getFirstChild()
470: .getNodeValue());
471: interpreter.addCommand(
472: CHANGE_LOCAL_WORKING_DIR,
473: paramMap);
474: }
475: if (DELETE_LOCAL_FILE.equals(command
476: .getNodeName())) {
477: Map<String, String> paramMap = new HashMap<String, String>();
478: paramMap
479: .put(DELETE_LOCAL_FILE, command
480: .getFirstChild()
481: .getNodeValue());
482: interpreter.addCommand(
483: DELETE_LOCAL_FILE, paramMap);
484: }
485: if (RENAME_LOCAL.equals(command
486: .getNodeName())) {
487: Map<String, String> paramMap = new HashMap<String, String>();
488: paramMap
489: .put(RENAME_LOCAL, command
490: .getFirstChild()
491: .getNodeValue());
492: try {
493: if (command.getAttributes()
494: .getNamedItem(TO_NAME) != null) {
495: paramMap.put(TO_NAME, command
496: .getAttributes()
497: .getNamedItem(TO_NAME)
498: .getNodeValue());
499: } else {
500: throw new Exception(
501: "The value of toName attribute is required.");
502: }
503: } catch (DOMException e) {
504:
505: ErrorUtil
506: .printError(
507: "Exception in ConfigFactory(): ",
508: e);
509: ;
510: } catch (Exception e) {
511: ErrorUtil
512: .printError(
513: "Exception in ConfigFactory(): ",
514: e);
515: }
516: interpreter.addCommand(RENAME_LOCAL,
517: paramMap);
518: }
519: if (CREATE_LOCAL_DIRECTORY.equals(command
520: .getNodeName())) {
521: Map<String, String> paramMap = new HashMap<String, String>();
522: paramMap.put(CREATE_LOCAL_DIRECTORY,
523: command.getFirstChild()
524: .getNodeValue());
525: interpreter.addCommand(
526: CREATE_LOCAL_DIRECTORY,
527: paramMap);
528: }
529: if (REMOVE_LOCAL_DIRECTORY.equals(command
530: .getNodeName())) {
531: Map<String, String> paramMap = new HashMap<String, String>();
532: paramMap.put(REMOVE_LOCAL_DIRECTORY,
533: command.getFirstChild()
534: .getNodeValue());
535: interpreter.addCommand(
536: REMOVE_LOCAL_DIRECTORY,
537: paramMap);
538: }
539: if (M_DELETE_LOCAL_FILES.equals(command
540: .getNodeName())) {
541: Map<String, String> paramMap = new HashMap<String, String>();
542: paramMap.put(M_DELETE_LOCAL_FILES,
543: command.getFirstChild()
544: .getNodeValue());
545: interpreter.addCommand(
546: M_DELETE_LOCAL_FILES, paramMap);
547: }
548: }
549: }
550: }
551: }
552: }
553: }
554: }
|