[jbpm-commits] JBoss JBPM SVN: r5638 - in projects/zync: lib and 1 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Mon Sep 14 15:44:39 EDT 2009


Author: tom.baeyens at jboss.com
Date: 2009-09-14 15:44:39 -0400 (Mon, 14 Sep 2009)
New Revision: 5638

Added:
   projects/zync/.classpath
   projects/zync/.project
   projects/zync/lib/
   projects/zync/lib/commons-net-2.0.jar
   projects/zync/src/
   projects/zync/src/Zync.java
Log:
initial import

Added: projects/zync/.classpath
===================================================================
--- projects/zync/.classpath	                        (rev 0)
+++ projects/zync/.classpath	2009-09-14 19:44:39 UTC (rev 5638)
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+	<classpathentry kind="lib" path="lib/commons-net-2.0.jar"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>


Property changes on: projects/zync/.classpath
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: projects/zync/.project
===================================================================
--- projects/zync/.project	                        (rev 0)
+++ projects/zync/.project	2009-09-14 19:44:39 UTC (rev 5638)
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>zync</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>


Property changes on: projects/zync/.project
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: projects/zync/lib/commons-net-2.0.jar
===================================================================
(Binary files differ)


Property changes on: projects/zync/lib/commons-net-2.0.jar
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: projects/zync/src/Zync.java
===================================================================
--- projects/zync/src/Zync.java	                        (rev 0)
+++ projects/zync/src/Zync.java	2009-09-14 19:44:39 UTC (rev 5638)
@@ -0,0 +1,332 @@
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import org.apache.commons.net.ftp.FTP;
+import org.apache.commons.net.ftp.FTPClient;
+import org.apache.commons.net.ftp.FTPFile;
+import org.apache.commons.net.ftp.FTPReply;
+
+
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+/**
+ * @author Tom Baeyens
+ */
+public class Zync {
+  
+  static FTPClient ftpClient = new FTPClient();
+  
+  static List<String> textExtensions = new ArrayList<String>();
+  {
+    textExtensions.add("txt");
+    textExtensions.add("html");
+    textExtensions.add("css");
+    textExtensions.add("xml");
+    textExtensions.add("java");
+  }
+
+  static String ftpServer;
+  static String ftpUsername;
+  static String ftpPassword;
+  static String ftpDirectory;
+  static String localDirectory;
+  static List<String> localFileNames = new ArrayList<String>();
+  static List<String> previousLocalFileNames = new ArrayList<String>();
+
+  public static void main(String[] args) {
+    loadConfiguration();
+    loadPreviousLocalFileNames();
+
+    try {
+      connect();
+
+      syncDirectory("");
+      
+    } catch(Exception e) {
+      log(e.getMessage());
+
+    } finally {
+      disconnect();
+    }
+
+    saveLocalFileNames();
+
+    log("done.");
+  }
+
+  static void saveLocalFileNames() {
+    String zyncPreviousFileNamesPath = System.getProperty("user.home")+"/.zync/previous.filenames.txt";
+    try {
+      FileOutputStream outputStream = new FileOutputStream(zyncPreviousFileNamesPath, false);
+      OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
+      PrintWriter writer = new PrintWriter(outputStreamWriter);
+      try {
+        for (String localFileName: localFileNames) {
+          writer.println(localFileName);
+        }
+      } finally {
+        writer.close();
+        outputStreamWriter.close();
+        outputStream.close();
+      }
+    } catch (Exception e) {
+      throw new RuntimeException("problem writing "+zyncPreviousFileNamesPath+": "+e.getMessage());
+    }
+  }
+
+  static void loadPreviousLocalFileNames() {
+    String zyncPreviousFileNamesPath = System.getProperty("user.home")+"/.zync/previous.filenames.txt";
+    File file = new File(zyncPreviousFileNamesPath);
+    if (file.exists()) {
+      try {
+        InputStream inputStream = new FileInputStream(file);
+        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
+        BufferedReader reader = new BufferedReader(inputStreamReader);
+        try {
+          String line;
+          while ((line=reader.readLine())!=null) {
+            previousLocalFileNames.add(line);
+          }
+        } finally {
+          inputStream.close();
+        }
+      } catch (Exception e) {
+        throw new RuntimeException("problem reading "+zyncPreviousFileNamesPath+": "+e.getMessage());
+      }
+    }
+  }
+
+  private static void loadConfiguration() {
+    String zyncCfgPath = System.getProperty("user.home")+"/.zync/zync.properties";
+    File file = new File(zyncCfgPath);
+    if (!file.exists()) {
+      throw new RuntimeException("zync config file "+zyncCfgPath+" does not exist");
+    }
+
+    Properties properties = new Properties();
+    try {
+      FileInputStream inputStream = new FileInputStream(file);
+      properties.load(inputStream);
+    } catch (Exception e) {
+      throw new RuntimeException("couldn't load zync config file "+zyncCfgPath+": "+e.getMessage(), e);
+    }
+
+    ftpServer = properties.getProperty("ftp.server");
+    ftpUsername = properties.getProperty("ftp.username");
+    ftpPassword = properties.getProperty("ftp.password");
+    ftpDirectory = properties.getProperty("ftp.directory");
+    localDirectory = properties.getProperty("local.directory");
+  }
+
+  static void connect() throws Exception {
+    int reply;
+    log("Connecting to " + ftpServer + "...");
+    ftpClient.connect(ftpServer);
+    ftpClient.login(ftpUsername, ftpPassword);
+    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
+    log("Connected to " + ftpServer + ".");
+    log(ftpClient.getReplyString());
+
+    // After connection attempt, you should check the reply code to verify
+    // success.
+    reply = ftpClient.getReplyCode();
+
+    if(!FTPReply.isPositiveCompletion(reply)) {
+      ftpClient.disconnect();
+      System.err.println("FTP server refused connection.");
+      return;
+    }
+  }
+
+  static void disconnect() {
+    if(ftpClient.isConnected()) {
+      try {
+        ftpClient.disconnect();
+      } catch(IOException ioe) {
+        // do nothing
+      }
+    }
+  }
+
+  static void syncDirectory(String directory) {
+    try {
+      log("syncing directory "+directory);
+      
+      Set<String> subDirectories = new HashSet<String>(); 
+
+      Map<String, File> localFiles = new HashMap<String, File>(); 
+      String localDirectoryPathname = ("".equals(directory) ? localDirectory : localDirectory+"/"+directory);
+      log("scanning local directory "+localDirectoryPathname);
+      File localDirFile = new File(localDirectoryPathname);
+      if (!localDirFile.exists()) {
+        log("creating local directory "+localDirectoryPathname);
+        localDirFile.mkdirs();
+      }
+      File[] directoryFiles = localDirFile.listFiles();
+      if (directoryFiles!=null) {
+        for (File directoryFile: directoryFiles) {
+          String fileName = directoryFile.getName();
+          if (directoryFile.isFile()) {
+            localFiles.put(fileName, directoryFile);
+            localFileNames.add(directory+"/"+fileName);
+          } else if (directoryFile.isDirectory()) {
+            subDirectories.add(fileName);
+          }
+        }
+      }
+      
+
+      String remoteDir = ftpDirectory+"/"+directory;
+      //ftpClient.mkd(remoteDir);
+      FTPFile[] ftpFilesArray = ftpClient.listFiles(remoteDir);
+      log("scanning remote directory " + directory);
+      Map<String, FTPFile> ftpFiles = new HashMap<String, FTPFile>();
+      if (ftpFilesArray!=null) {
+        for (FTPFile ftpFile: ftpFilesArray) {
+          String ftpFileName = ftpFile.getName();
+          if (ftpFile.isFile()) {
+            ftpFiles.put(ftpFileName, ftpFile);
+          } else if ( ftpFile.isDirectory()
+                      && !".".equals(ftpFileName)
+                      && !"..".equals(ftpFileName)
+                    ) {
+            subDirectories.add(ftpFileName);
+          }
+        }
+      }
+
+      for (String localFileName: localFiles.keySet()) {
+        File localFile = localFiles.get(localFileName);
+        FTPFile ftpFile = ftpFiles.get(localFileName);
+        
+        Calendar localTimestamp = new GregorianCalendar();
+        localTimestamp.setTimeInMillis(localFile.lastModified());
+        localTimestamp.set(Calendar.SECOND, 0);
+        localTimestamp.set(Calendar.MILLISECOND, 0);
+        
+        if (ftpFile==null) {
+          log("remote file "+localFileName+" does not exist.");
+          uploadFile(localFile, directory);
+        } else if (isBefore(localTimestamp, ftpFile.getTimestamp())) {
+          log("local file "+localFileName+" is newest");
+        } else if (isBefore(ftpFile.getTimestamp(), localTimestamp)) {
+          log("remote file "+localFileName+" is newest");
+        } else if (localFile.length() != ftpFile.getSize()) {
+          log("warning: files of same date have different size "+localFileName+" local:"+localFile.length()+" remote:"+ftpFile.getSize());
+        } else {
+          log("file "+localFileName+" is in sync");
+        }
+      }
+
+      for (String ftpFileName: ftpFiles.keySet()) {
+        FTPFile ftpFile = ftpFiles.get(ftpFileName);
+        File localFile = localFiles.get(ftpFileName);
+        
+        if (localFile==null) {
+          if (previousLocalFileNames.contains(directory+"/"+ftpFileName)) {
+            log("file "+ftpFileName+" was removed locally");
+          } else {
+            log("local file "+ftpFileName+" does not exist");
+          }
+        }
+      }
+      
+      for (String subDirectory: subDirectories) {
+        syncDirectory( getRemoteFileName(directory, subDirectory) );
+      }
+
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
+  }
+
+  static void uploadFile(File localFile, String directory) throws Exception {
+    String localFileName = localFile.getName();
+    String remoteFileName = getRemoteFileName(directory, localFileName);
+    FileInputStream localFileStream = new FileInputStream(localFile);
+    boolean isTextFile = isTextFile(localFileName);
+    if (isTextFile) {
+      ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
+    }
+    try {
+      log("uploading "+localFileName+" --> "+remoteFileName);
+      ftpClient.storeFile(remoteFileName, localFileStream);
+      int reply = ftpClient.getReplyCode();
+      if (FTPReply.isPositiveCompletion(reply)) {
+        log("file upload successfull");
+      } else {
+        log("file upload failed: "+reply);
+      }
+    } finally {
+      if (isTextFile) {
+        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
+      }
+    }
+  }
+
+  private static String getRemoteFileName(String directory, String fileName) {
+    String remoteFileName = ftpDirectory;
+    if (!"".equals(ftpDirectory)) {
+      remoteFileName += "/";
+    }
+    remoteFileName += directory;
+    if (!"".equals(directory)){
+     remoteFileName += "/";
+    }
+    remoteFileName += fileName;
+    return remoteFileName;
+  }
+  
+  private static boolean isTextFile(String fileName) {
+    int lastDotIndex = fileName.lastIndexOf('.'); 
+    if ( (lastDotIndex==-1) && (fileName.length()>=lastDotIndex+1) ) {
+      return false;
+    }
+    String extension = fileName.substring(lastDotIndex+1);
+    return textExtensions.contains(extension);
+  }
+
+  static boolean isBefore(Calendar first, Calendar second) {
+    return first.getTimeInMillis()<second.getTimeInMillis();
+  }
+
+  static void log(String msg) {
+    System.out.println(msg);
+  }
+}


Property changes on: projects/zync/src/Zync.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain



More information about the jbpm-commits mailing list