[jboss-svn-commits] JBL Code SVN: r10929 - labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/notification.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Apr 12 12:00:43 EDT 2007


Author: derek.adams
Date: 2007-04-12 12:00:43 -0400 (Thu, 12 Apr 2007)
New Revision: 10929

Modified:
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/notification/NotifyFTP.java
Log:
Implemented proper FTPEpr handing.

Modified: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/notification/NotifyFTP.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/notification/NotifyFTP.java	2007-04-12 15:59:46 UTC (rev 10928)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/notification/NotifyFTP.java	2007-04-12 16:00:43 UTC (rev 10929)
@@ -3,54 +3,131 @@
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.OutputStream;
+import java.net.URISyntaxException;
 
 import org.apache.commons.io.IOUtils;
-import org.apache.log4j.Logger;
+import org.apache.commons.lang.StringUtils;
 import org.jboss.soa.esb.addressing.eprs.FTPEpr;
+import org.jboss.soa.esb.helpers.ConfigTree;
 import org.jboss.soa.esb.message.Message;
 import org.jboss.soa.esb.util.RemoteFileSystem;
 import org.jboss.soa.esb.util.RemoteFileSystemException;
 import org.jboss.soa.esb.util.RemoteFileSystemFactory;
 
 /**
- * Sends
+ * Sends a message to an outgoing FTP server. The notification-details property looks 
+ * something like:
+ * 
+ * <code>
+ *		<NotificationList type="OK" xmlns="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.0.1.xsd">
+ *			<target class="NotifyFTP">
+ *				<ftp URL="ftp://username:pwd@server.com/remote/dir" filename="output.txt"/>
+ *			</target>
+ *		</NotificationList>
+ * </code>
+ * 
  * @author <a href="mailto:rex.sheridan at sapience360.com">Rex Sheridan</a>
  */
 public class NotifyFTP extends NotificationTarget {
 
-	private static final Logger logger = Logger.getLogger(NotifyFTP.class);
+	/** Configuration entries */
+	private ConfigTree config;
 
+	/** Used to get access to remote filesystem */
 	private FTPEpr epr;
 
+	/** Filename created on server */
 	private String fileName;
 
+	/** FTP child tag name */
+	public static final String CHILD_FTP = "ftp";
+
+	/** Filename attribute */
+	public static final String ATTR_FILENAME = "filename";
+
+	/** Base filename for temp file */
+	public static final String TEMP_FILE_BASE = "jbossesb-NotifyFTP";
+
+	/**
+	 * Create an outgoing FTP notification based on the given configuration.
+	 * 
+	 * @param config
+	 */
+	public NotifyFTP(ConfigTree config) {
+		this.config = config;
+	}
+
+	/**
+	 * Get the ConfigTree for the 'ftp' element.
+	 * 
+	 * @return
+	 */
+	protected ConfigTree getFtpConfig() throws NotificationException {
+		ConfigTree[] ftps = config.getChildren(CHILD_FTP);
+		if (ftps.length != 1) {
+			throw new NotificationException("NotifyFTP requires exactly one 'ftp' element.");
+		}
+		return ftps[0];
+	}
+
+	/**
+	 * Lazy loads filename from config tree.
+	 * 
+	 * @return
+	 */
+	protected String getFileName() throws NotificationException {
+		if (fileName == null) {
+			ConfigTree ftpConfig = getFtpConfig();
+			fileName = ftpConfig.getAttribute(ATTR_FILENAME);
+			if (StringUtils.isEmpty(fileName)) {
+				throw new NotificationException("NotifyFTP: Filename attribute is required.");
+			}
+		}
+		return fileName;
+	}
+
+	/**
+	 * Builds an FTP EPR from the configutation data.
+	 * 
+	 * @param config
+	 * @return
+	 */
+	protected FTPEpr getFtpEpr() throws NotificationException {
+		if (epr == null) {
+			ConfigTree ftpConfig = getFtpConfig();
+			String url = ftpConfig.getAttribute(FTPEpr.URL_TAG);
+			try {
+				epr = new FTPEpr(url);
+			} catch (URISyntaxException e) {
+				throw new NotificationException(e);
+			}
+		}
+		return epr;
+	}
+
 	/*
 	 * (non-Javadoc)
 	 * 
 	 * @see org.jboss.soa.esb.notification.NotificationTarget#sendNotification(org.jboss.soa.esb.message.Message)
 	 */
 	public void sendNotification(Message message) throws NotificationException {
-		OutputStream stream = null;
+		FileOutputStream stream = null;
 		File tmpFile = null;
 		RemoteFileSystem rfs = null;
 		try {
-			rfs = RemoteFileSystemFactory.getRemoteFileSystem(epr, true);
-			tmpFile = File.createTempFile("jbossesb-NotifyFTP", null);
+			tmpFile = File.createTempFile(TEMP_FILE_BASE, null);
 			stream = new FileOutputStream(tmpFile);
-			writeValueToStream(message, stream);
-			rfs.uploadFile(tmpFile, fileName);
+			IOUtils.write(message.getBody().getContents(), stream);
+			stream.close();
+			rfs = RemoteFileSystemFactory.getRemoteFileSystem(getFtpEpr(), true);
+			rfs.uploadFile(tmpFile, getFileName());
 		} catch (RemoteFileSystemException e) {
 			throw new NotificationException("Could not complete FTP notification", e);
 		} catch (IOException e) {
 			throw new NotificationException("Could not complete FTP notification", e);
 		} finally {
 			if (stream != null) {
-				try {
-					stream.close();
-				} catch (IOException e) {
-					logger.error("Unable to close stream", e);
-				}
+				IOUtils.closeQuietly(stream);
 			}
 			if (tmpFile != null) {
 				tmpFile.delete();
@@ -59,49 +136,5 @@
 				rfs.quit();
 			}
 		}
-
 	}
-
-	/**
-	 * @param value
-	 * @param stream
-	 * @throws NotificationException
-	 * @throws IOException
-	 */
-	private void writeValueToStream(Message message, OutputStream stream)
-			throws NotificationException, IOException {
-		IOUtils.write(message.getBody().getContents(), stream);
-		stream.flush();
-		stream.close();
-	}
-
-	/**
-	 * @return the epr
-	 */
-	public FTPEpr getEpr() {
-		return epr;
-	}
-
-	/**
-	 * @param epr
-	 *            the epr to set
-	 */
-	public void setEpr(FTPEpr epr) {
-		this.epr = epr;
-	}
-
-	/**
-	 * @return the fileName
-	 */
-	public String getFileName() {
-		return fileName;
-	}
-
-	/**
-	 * @param fileName
-	 *            the fileName to set
-	 */
-	public void setFileName(String fileName) {
-		this.fileName = fileName;
-	}
 }
\ No newline at end of file




More information about the jboss-svn-commits mailing list