[richfaces-svn-commits] JBoss Rich Faces SVN: r13496 - in trunk/test-applications/realworld2/web/src/main: java/org/richfaces/realworld/util and 1 other directories.

richfaces-svn-commits at lists.jboss.org richfaces-svn-commits at lists.jboss.org
Fri Apr 10 10:31:21 EDT 2009


Author: Alex.Kolonitsky
Date: 2009-04-10 10:31:21 -0400 (Fri, 10 Apr 2009)
New Revision: 13496

Modified:
   trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/manager/ImageManager.java
   trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/util/CopyImageStuff.java
   trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/util/FileUtils.java
   trunk/test-applications/realworld2/web/src/main/resources/messages_en.properties
Log:
Change web application path resolver.

Modified: trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/manager/ImageManager.java
===================================================================
--- trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/manager/ImageManager.java	2009-04-10 14:22:36 UTC (rev 13495)
+++ trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/manager/ImageManager.java	2009-04-10 14:31:21 UTC (rev 13496)
@@ -113,10 +113,10 @@
     }
     
     public List<MetaTag> autoComplete(Object suggest) {
-    	String temp = (String)suggest;
-    	if(temp.trim().equals("")){
-    		return null;
-    	}
+        String temp = (String) suggest;
+        if (temp.trim().equals("")) {
+            return null;
+        }
         return imageAction.getTagsLikeString((String)suggest);
     }
     

Modified: trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/util/CopyImageStuff.java
===================================================================
--- trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/util/CopyImageStuff.java	2009-04-10 14:22:36 UTC (rev 13495)
+++ trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/util/CopyImageStuff.java	2009-04-10 14:31:21 UTC (rev 13496)
@@ -3,11 +3,6 @@
  */
 package org.richfaces.realworld.util;
 
-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;
@@ -15,9 +10,21 @@
 import org.jboss.seam.annotations.Out;
 import org.jboss.seam.annotations.Scope;
 import org.jboss.seam.annotations.Startup;
-import org.richfaces.realworld.service.Constants;
+import org.jboss.seam.contexts.ServletLifecycle;
+import static org.richfaces.realworld.service.Constants.IMAGE_FOLDER;
+import static org.richfaces.realworld.service.Constants.REALWORLD_FOLDER;
+import static org.richfaces.realworld.service.Constants.TEMP_DIR;
+import static org.richfaces.realworld.service.Constants.UPLOAD_FOLDER_PATH_ERROR;
+import static org.richfaces.realworld.service.Constants.WEB_INF;
+import static org.richfaces.realworld.util.FileUtils.copyDirectory;
+import static org.richfaces.realworld.util.FileUtils.deleteDirectory;
+import static org.richfaces.realworld.util.FileUtils.joinFiles;
 
+import javax.servlet.ServletContext;
+import java.io.File;
+import java.io.IOException;
 
+
 /**
  * @author Andrey Markavtsov
  *
@@ -27,74 +34,58 @@
 @Scope(ScopeType.APPLICATION)
 @Startup
 public class CopyImageStuff {
-	
+
 	@Out(scope = ScopeType.APPLICATION)
-	File uploadRoot;
+	private File uploadRoot;
 	
 	@Out(scope = ScopeType.APPLICATION)
-	String uploadRootPath;
+	private String uploadRootPath;
 	
-	String imageSrc;
+	private String imageSrc;
 	
 	@Create
-	public void create() throws Exception {
+	public void create() throws IOException {
 		resolveImageFolder();
 		resolveUploadRoot();
-		
+
 		copyImages();
 	}
 	
 	@Destroy
 	public void destroy()throws IOException {
-		FileUtils.deleteDirectory(uploadRoot, true);
+		deleteDirectory(uploadRoot, true);
 	}
 	
-	void resolveImageFolder() {
-		//TODO nick - what this cast is for?
-		URLClassLoader loader = (URLClassLoader)getClass().getClassLoader();
-		//TODO nick - rewrite that
-		URL path = loader.getResource("");
-		String classLoadPath = null;
-		String realPath = null;
+	private void resolveImageFolder() {
+		final ServletContext servletContext = ServletLifecycle.getServletContext();
 		
-		if (path != null) {
-			classLoadPath = path.getFile();
-		}
-		
-		if (classLoadPath != null) {
-			int index = classLoadPath.indexOf(Constants.WEB_INF);
-			if (index != -1) {
-				realPath = classLoadPath.substring(0, index + Constants.WEB_INF.length()) + Constants.IMAGE_FOLDER;
-			}
-		}
-		
-		if (realPath != null) {
-			this.imageSrc = realPath;
+		if (servletContext != null) {
+			this.imageSrc = joinFiles(servletContext.getRealPath(""),
+					WEB_INF, IMAGE_FOLDER);
 		}else {
-			throw new NullPointerException(Constants.UPLOAD_FOLDER_PATH_ERROR);
+			throw new IllegalStateException(UPLOAD_FOLDER_PATH_ERROR);
 		}
 
 	}
 	
-	void resolveUploadRoot()throws IOException {
-		String property = System.getProperty(Constants.TEMP_DIR);
-		if(!property.endsWith(File.separator)){
-			property+=File.separator;
+	private void resolveUploadRoot() throws IOException {
+		uploadRoot = new File(joinFiles(
+				System.getProperty(TEMP_DIR), REALWORLD_FOLDER));
+
+		if (uploadRoot.exists()) {
+			deleteDirectory(uploadRoot, true);
 		}
-		String uploadRootPath = property + Constants.REALWORLD_FOLDER + File.separator;
-		if (uploadRootPath != null) {
-			uploadRoot = new File(uploadRootPath);
-			if (uploadRoot.exists()) {
-				FileUtils.deleteDirectory(uploadRoot, true);
-			}
-			uploadRoot.mkdir();
-			this.uploadRootPath = uploadRoot.getCanonicalPath();
-		}else {
-			throw new NullPointerException(Constants.UPLOAD_ROOT_CREATION_ERROR);
-		}
+
+		uploadRoot.mkdir();
+
+		uploadRootPath = uploadRoot.getCanonicalPath();
 	}
 	
-	void copyImages()throws IOException {
-		FileUtils.copyDirectory(new File(imageSrc), uploadRoot);
+	private void copyImages() {
+		try {
+			copyDirectory(new File(imageSrc), uploadRoot);
+		} catch (IOException e) {
+			System.out.println("ERROR on copy '"+imageSrc+"' to '"+uploadRoot+ '\'');
+		}
 	}
 }
\ No newline at end of file

Modified: 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	2009-04-10 14:22:36 UTC (rev 13495)
+++ trunk/test-applications/realworld2/web/src/main/java/org/richfaces/realworld/util/FileUtils.java	2009-04-10 14:31:21 UTC (rev 13496)
@@ -92,13 +92,23 @@
         } else {
             if (dir.exists()) {
                 final boolean isFileDeleted = dir.delete();
-                System.out.println((isFileDeleted ? "OK     " : "CANCEL ") +
+                System.out.println((isFileDeleted ? "OK     " : "ERROR ") +
                         "Delete file '" + dir.getPath() + '\'');
             }
         }
         dir.delete();
         return true;
     }
+	
+	public static String joinFiles(String... files) {
+		final StringBuilder res = new StringBuilder();
+		for (String file : files) {
+			res.append(file).append(File.separatorChar);
+		}
+
+		return res.substring(0, res.length() - 1);
+	}
+
     
     public static void deleteFile(File file) {
         if (file.exists()) {

Modified: trunk/test-applications/realworld2/web/src/main/resources/messages_en.properties
===================================================================
--- trunk/test-applications/realworld2/web/src/main/resources/messages_en.properties	2009-04-10 14:22:36 UTC (rev 13495)
+++ trunk/test-applications/realworld2/web/src/main/resources/messages_en.properties	2009-04-10 14:31:21 UTC (rev 13496)
@@ -197,6 +197,7 @@
 user_profile=View profile
 user_show_albums=Show albums
 
+size=Size
 camera=Camera
 uploaded=Uploaded at
 




More information about the richfaces-svn-commits mailing list