Author: andrei_exadel
Date: 2009-03-20 13:51:16 -0400 (Fri, 20 Mar 2009)
New Revision: 13080
Added:
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/startup/
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/startup/CopyImageStuff.java
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/util/FileUtils.java
Removed:
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/listener/
Modified:
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/manager/FileManager.java
trunk/test-applications/realworld2/web/src/main/webapp/WEB-INF/web.xml
Log:
refactor copy image stuff
Modified:
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/manager/FileManager.java
===================================================================
---
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/manager/FileManager.java 2009-03-20
17:49:48 UTC (rev 13079)
+++
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/manager/FileManager.java 2009-03-20
17:51:16 UTC (rev 13080)
@@ -26,21 +26,21 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
-import java.util.Properties;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;
+import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
+import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.richfaces.realworld.service.Constants;
@@ -50,12 +50,14 @@
@AutoCreate
public class FileManager {
- private static final String REALWORLD_PROPERTIES = "realworld.properties";
private static final String _SMALL160 = "_small160";
private static final String _SMALL120 = "_small120";
private static final String _SMALL80 = "_small80";
private static final String _MEDIUM = "_medium";
private static final String _MINI = "_mini";
+
+ static final String UPLOAD_ROOT_COMPONENT_NAME = "uploadRoot";
+ static final String UPLOAD_ROOT_PATH_COMPONENT_NAME = "uploadRootPath";
private File uploadRoot;
private String uploadRootPath;
@@ -64,13 +66,10 @@
return uploadRoot;
}
- public FileManager() throws FileNotFoundException, IOException{
- if(uploadRoot != null){
- return;
- }
- Properties prop = new Properties();
- prop.load(new FileInputStream(REALWORLD_PROPERTIES));
- this.setUploadRoot(prop.get("uploadRoot").toString());
+ @Create
+ public void create() {
+ uploadRoot = (File)Component.getInstance(UPLOAD_ROOT_COMPONENT_NAME,
ScopeType.APPLICATION);
+ uploadRootPath = (String)Component.getInstance(UPLOAD_ROOT_PATH_COMPONENT_NAME,
ScopeType.APPLICATION);
}
public void setUploadRoot(String uploadRootPath) throws IOException {
Added:
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/startup/CopyImageStuff.java
===================================================================
---
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/startup/CopyImageStuff.java
(rev 0)
+++
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/startup/CopyImageStuff.java 2009-03-20
17:51:16 UTC (rev 13080)
@@ -0,0 +1,104 @@
+/**
+ *
+ */
+package org.richfaces.realworld.startup;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLClassLoader;
+
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Create;
+import org.jboss.seam.annotations.Destroy;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Out;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.Startup;
+import org.richfaces.realworld.util.FileUtils;
+
+
+/**
+ * @author Andrey Markavtsov
+ *
+ */
+
+@Name("CopyImagesStuff")
+(a)Scope(ScopeType.APPLICATION)
+@Startup
+public class CopyImageStuff {
+
+ static final String WEB_INF = "WEB-INF";
+
+ static final String IMAGE_FOLDER = "/Upload";
+
+ static final String REALWORLD_FOLDER = "realworld";
+
+ @Out(scope = ScopeType.APPLICATION)
+ File uploadRoot;
+
+ @Out(scope = ScopeType.APPLICATION)
+ String uploadRootPath;
+
+ String imageSrc;
+
+ @Create
+ public void create() throws Exception {
+ resolveImageFolder();
+ resolveUploadRoot();
+
+ copyImages();
+ }
+
+ @Destroy
+ public void destroy()throws IOException {
+ FileUtils.deleteDirectory(uploadRoot);
+ }
+
+
+ void resolveImageFolder() {
+ URLClassLoader loader = (URLClassLoader)getClass().getClassLoader();
+ URL path = loader.getResource("");
+ String classLoadPath = null;
+ String realPath = null;
+
+ if (path != null) {
+ classLoadPath = path.getFile();
+ }
+
+ if (classLoadPath != null) {
+ int index = classLoadPath.indexOf(WEB_INF);
+ if (index != -1) {
+ realPath = classLoadPath.substring(0, index + WEB_INF.length()) + IMAGE_FOLDER;
+ }
+ }
+
+ if (realPath != null) {
+ this.imageSrc = realPath;
+ }else {
+ throw new NullPointerException("Cannot bound image folder path");
+ }
+
+ }
+
+ void resolveUploadRoot()throws IOException {
+ String uploadRootPath = System.getProperty("java.io.tmpdir") +
REALWORLD_FOLDER + File.separator;
+ if (uploadRootPath != null) {
+ uploadRoot = new File(uploadRootPath);
+ if (!uploadRoot.exists()) {
+ uploadRoot.mkdir();
+ }
+ this.uploadRootPath = uploadRoot.getCanonicalPath();
+ }else {
+ throw new NullPointerException("Upload root was not created");
+ }
+ }
+
+
+ void copyImages()throws IOException {
+ FileUtils.copyDirectory(new File(imageSrc), uploadRoot);
+ }
+
+
+
+}
Added:
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/util/FileUtils.java
===================================================================
---
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/util/FileUtils.java
(rev 0)
+++
trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/util/FileUtils.java 2009-03-20
17:51:16 UTC (rev 13080)
@@ -0,0 +1,227 @@
+package org.richfaces.realworld.util;
+
+import java.io.*;
+import java.util.zip.*;
+
+
+
+/**
+ * Command line program to copy a file to another directory.
+ *
+ * @author Marco Schmidt
+ */
+public class FileUtils {
+ // constant values for the override option
+ public static final int OVERWRITE_ALWAYS = 1;
+ public static final int OVERWRITE_NEVER = 2;
+ public static final int OVERWRITE_ASK = 3;
+
+ // program options initialized to default values
+ private static int bufferSize = 4 * 1024;
+ private static boolean clock = true;
+ private static boolean copyOriginalTimestamp = true;
+ private static boolean verify = true;
+ private static int override = OVERWRITE_ASK;
+
+ public static Long copyFile(File srcFile, File destFile) throws IOException {
+ if(!srcFile.getPath().toLowerCase().endsWith("jpg") &&
!srcFile.getPath().toLowerCase().endsWith("jpeg")){
+ return -1L;
+ }
+ InputStream in = new FileInputStream(srcFile);
+ OutputStream out = new FileOutputStream(destFile);
+ long millis = System.currentTimeMillis();
+ CRC32 checksum = null;
+ if (verify) {
+ checksum = new CRC32();
+ checksum.reset();
+ }
+ byte[] buffer = new byte[bufferSize];
+ int bytesRead;
+ while ((bytesRead = in.read(buffer)) >= 0) {
+ if (verify) {
+ checksum.update(buffer, 0, bytesRead);
+ }
+ out.write(buffer, 0, bytesRead);
+ }
+ out.close();
+ in.close();
+ if (clock) {
+ millis = System.currentTimeMillis() - millis;
+ System.out.println("Second(s): " + (millis / 1000L));
+ }
+ if (verify) {
+ return new Long(checksum.getValue());
+ } else {
+ return null;
+ }
+ }
+
+ public static Long createChecksum(File file) throws IOException {
+ long millis = System.currentTimeMillis();
+ InputStream in = new FileInputStream(file);
+ CRC32 checksum = new CRC32();
+ checksum.reset();
+ byte[] buffer = new byte[bufferSize];
+ int bytesRead;
+ while ((bytesRead = in.read(buffer)) >= 0) {
+ checksum.update(buffer, 0, bytesRead);
+ }
+ in.close();
+ if (clock) {
+ millis = System.currentTimeMillis() - millis;
+ System.out.println("Second(s): " + (millis / 1000L));
+ }
+ return new Long(checksum.getValue());
+ }
+
+ /**
+ * Determine if data is to be copied to given file. Take into consideration
+ * override option and ask user in case file exists and override option is
+ * ask.
+ *
+ * @param file
+ * File object for potential destination file
+ * @return true if data is to be copied to file, false if not
+ */
+ public static boolean doCopy(File file) {
+ boolean exists = file.exists();
+ if (override == OVERWRITE_ALWAYS || !exists) {
+ return true;
+ } else if (override == OVERWRITE_NEVER) {
+ return false;
+ } else if (override == OVERWRITE_ASK) {
+ return readYesNoFromStandardInput("File exists. "
+ + "Overwrite (y/n)?");
+ } else {
+ throw new InternalError("Program error. Invalid "
+ + "value for override: " + override);
+ }
+ }
+
+ public static void main(String[] args) throws IOException {
+ // make sure there are exactly two arguments
+ if (args.length != 2) {
+ System.err.println("Usage: CopyFile SRC-FILE-NAME DEST-DIR-NAME");
+ System.exit(1);
+ }
+ // make sure the source file is indeed a readable file
+ File srcFile = new File(args[0]);
+ if (!srcFile.isFile() || !srcFile.canRead()) {
+ System.err.println("Not a readable file: " + srcFile.getName());
+ System.exit(1);
+ }
+ // make sure the second argument is a directory
+ File destDir = new File(args[1]);
+ if (!destDir.isDirectory()) {
+ System.err.println("Not a directory: " + destDir.getName());
+ System.exit(1);
+ }
+ // create File object for destination file
+ File destFile = new File(destDir, srcFile.getName());
+
+ // check if copying is desired given overwrite option
+ if (!doCopy(destFile)) {
+ return;
+ }
+
+ // copy file, optionally creating a checksum
+ Long checksumSrc = copyFile(srcFile, destFile);
+
+ // copy timestamp of last modification
+ if (copyOriginalTimestamp) {
+ if (!destFile.setLastModified(srcFile.lastModified())) {
+ System.err.println("Error: Could not set "
+ + "timestamp of copied file.");
+ }
+ }
+
+ // optionally verify file
+ if (verify) {
+ System.out.print("Verifying destination file...");
+ Long checksumDest = createChecksum(destFile);
+ if (checksumSrc.equals(checksumDest)) {
+ System.out.println(" OK, files are equal.");
+ } else {
+ System.out.println(" Error: Checksums differ.");
+ }
+ }
+ }
+
+ /**
+ * Print a message to standard output and read lines from standard input
+ * until yes or no (y or n) is entered.
+ *
+ * @param message
+ * informative text to be answered by user
+ * @return user answer, true for yes, false for no.
+ */
+ public static boolean readYesNoFromStandardInput(String message) {
+ System.out.println(message);
+ String line;
+ BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ Boolean answer = null;
+ try {
+ while ((line = in.readLine()) != null) {
+ line = line.toLowerCase();
+ if ("y".equals(line) || "yes".equals(line)) {
+ answer = Boolean.TRUE;
+ break;
+ } else if ("n".equals(line) || "no".equals(line)) {
+ answer = Boolean.FALSE;
+ break;
+ } else {
+ System.out.println("Could not understand answer (\"" + line
+ + "\"). Please use y for yes or n for no.");
+ }
+ }
+ if (answer == null) {
+ throw new IOException("Unexpected end of input from stdin.");
+ }
+ in.close();
+ return answer.booleanValue();
+ } catch (IOException ioe) {
+ throw new InternalError(
+ "Cannot read from stdin or write to stdout.");
+ }
+ }
+
+ public static void copyDirectory(File srcDir, File dstDir)
+ throws IOException {
+
+ if (".svn".equals(srcDir.getName())) {
+ return;
+ }
+
+ if (srcDir.isDirectory()) {
+ if (!dstDir.exists()) {
+ dstDir.mkdir();
+ }
+
+ String[] children = srcDir.list();
+ for (int i = 0; i < children.length; i++) {
+ copyDirectory(new File(srcDir, children[i]), new File(dstDir,
+ children[i]));
+ }
+ } else {
+ FileUtils.copyFile(srcDir, dstDir);
+ }
+ }
+
+ public static void deleteDirectory(File dir) throws IOException {
+ if (dir.isDirectory()) {
+ if (dir.exists()) {
+ File [] children = dir.listFiles();
+ for (int i = 0; i < children.length; i++) {
+ deleteDirectory(children[i]);
+ }
+ }
+
+ } else {
+ if (dir.exists()) {
+ System.out.println(dir.delete());
+ }
+ }
+ dir.delete();
+ }
+
+}
\ No newline at end of file
Modified: trunk/test-applications/realworld2/web/src/main/webapp/WEB-INF/web.xml
===================================================================
--- trunk/test-applications/realworld2/web/src/main/webapp/WEB-INF/web.xml 2009-03-20
17:49:48 UTC (rev 13079)
+++ trunk/test-applications/realworld2/web/src/main/webapp/WEB-INF/web.xml 2009-03-20
17:51:16 UTC (rev 13080)
@@ -77,12 +77,7 @@
<param-value>.xhtml</param-value>
</context-param>
- <context-param>
- <param-name>uploadRoot</param-name>
- <param-value>/srv/upload/as</param-value>
- </context-param>
-
- <context-param>
+ <context-param>
<param-name>storeStrategy</param-name>
<param-value>database</param-value>
</context-param>
@@ -117,8 +112,4 @@
<auth-constraint />
</security-constraint>
- <listener>
- <display-name>copyImageListener</display-name>
- <listener-class>org.richfaces.realworld.listener.CopyImagesStuff</listener-class>
- </listener>
</web-app>
Show replies by date