Browse Source

improve and refactory

JamesZhan 13 years ago
parent
commit
c18371b6eb

+ 1 - 1
.gitignore

@@ -2,7 +2,7 @@
 *.class
 *.class
 *.tmp
 *.tmp
 *.log
 *.log
-bin
+/bin
 build.sh
 build.sh
 integration-repo
 integration-repo
 build
 build

+ 1 - 1
README.rst

@@ -88,4 +88,4 @@ Blog()这行代码也不是必须)**
     }
     }
 
 
 
 
-Javadoc: http://www.osctools.net/apidocs/apidoc?api=jfinal 
+Javadoc: http://www.ostools.net/apidocs/apidoc?api=jfinal 

+ 59 - 20
src/com/jfinal/core/Controller.java

@@ -343,24 +343,56 @@ public abstract class Controller {
 	}
 	}
 	
 	
 	/**
 	/**
-	 * Return a value from cookie.
+	 * Get cookie value by cookie name.
 	 */
 	 */
-	public String getCookieValue(String name, String defaultValue) {
-		Cookie cookie = getCookie(name);
+	public String getCookie(String name, String defaultValue) {
+		Cookie cookie = getCookieObject(name);
 		return cookie != null ? cookie.getValue() : defaultValue;
 		return cookie != null ? cookie.getValue() : defaultValue;
 	}
 	}
 	
 	
 	/**
 	/**
-	 * Return a value from cookie.
+	 * Get cookie value by cookie name.
 	 */
 	 */
-	public String getCookieValue(String name) {
-		return getCookieValue(name, null);
+	public String getCookie(String name) {
+		return getCookie(name, null);
 	}
 	}
 	
 	
 	/**
 	/**
-	 * Return a cookie.
+	 * Get cookie value by cookie name and convert to Integer.
 	 */
 	 */
-	public Cookie getCookie(String name) {
+	public Integer getCookieToInt(String name) {
+		String result = getCookie(name);
+		return result != null ? Integer.parseInt(result) : null;
+	}
+	
+	/**
+	 * Get cookie value by cookie name and convert to Integer.
+	 */
+	public Integer getCookieToInt(String name, Integer defaultValue) {
+		String result = getCookie(name);
+		return result != null ? Integer.parseInt(result) : defaultValue;
+	}
+	
+	/**
+	 * Get cookie value by cookie name and convert to Long.
+	 */
+	public Long getCookieToLong(String name) {
+		String result = getCookie(name);
+		return result != null ? Long.parseLong(result) : null;
+	}
+	
+	/**
+	 * Get cookie value by cookie name and convert to Long.
+	 */
+	public Long getCookieToLong(String name, Long defaultValue) {
+		String result = getCookie(name);
+		return result != null ? Long.parseLong(result) : defaultValue;
+	}
+	
+	/**
+	 * Get cookie object by cookie name.
+	 */
+	public Cookie getCookieObject(String name) {
 		Cookie[] cookies = request.getCookies();
 		Cookie[] cookies = request.getCookies();
 		if (cookies != null)
 		if (cookies != null)
 			for (Cookie cookie : cookies)
 			for (Cookie cookie : cookies)
@@ -370,10 +402,11 @@ public abstract class Controller {
 	}
 	}
 	
 	
 	/**
 	/**
-	 * Return cookies array
+	 * Get all cookie objects.
 	 */
 	 */
-	public Cookie[] getCookies() {
-		return request.getCookies();
+	public Cookie[] getCookieObjects() {
+		Cookie[] result = request.getCookies();
+		return result != null ? result : new Cookie[0];
 	}
 	}
 	
 	
 	/**
 	/**
@@ -392,10 +425,7 @@ public abstract class Controller {
 	 * @param path see Cookie.setPath(String)
 	 * @param path see Cookie.setPath(String)
 	 */
 	 */
 	public Controller setCookie(String name, String value, int maxAgeInSeconds, String path) {
 	public Controller setCookie(String name, String value, int maxAgeInSeconds, String path) {
-		Cookie cookie = new Cookie(name, value);
-		cookie.setMaxAge(maxAgeInSeconds);
-		cookie.setPath(path);
-		response.addCookie(cookie);
+		setCookie(name, value, maxAgeInSeconds, path, null);
 		return this;
 		return this;
 	}
 	}
 	
 	
@@ -409,7 +439,8 @@ public abstract class Controller {
 	 */
 	 */
 	public Controller setCookie(String name, String value, int maxAgeInSeconds, String path, String domain) {
 	public Controller setCookie(String name, String value, int maxAgeInSeconds, String path, String domain) {
 		Cookie cookie = new Cookie(name, value);
 		Cookie cookie = new Cookie(name, value);
-		cookie.setDomain(domain);
+		if (domain != null)
+			cookie.setDomain(domain);
 		cookie.setMaxAge(maxAgeInSeconds);
 		cookie.setMaxAge(maxAgeInSeconds);
 		cookie.setPath(path);
 		cookie.setPath(path);
 		response.addCookie(cookie);
 		response.addCookie(cookie);
@@ -420,7 +451,7 @@ public abstract class Controller {
 	 * Set Cookie with path = "/".
 	 * Set Cookie with path = "/".
 	 */
 	 */
 	public Controller setCookie(String name, String value, int maxAgeInSeconds) {
 	public Controller setCookie(String name, String value, int maxAgeInSeconds) {
-		setCookie(name, value, maxAgeInSeconds, "/");
+		setCookie(name, value, maxAgeInSeconds, "/", null);
 		return this;
 		return this;
 	}
 	}
 	
 	
@@ -428,7 +459,7 @@ public abstract class Controller {
 	 * Remove Cookie with path = "/".
 	 * Remove Cookie with path = "/".
 	 */
 	 */
 	public Controller removeCookie(String name) {
 	public Controller removeCookie(String name) {
-		setCookie(name, null, 0, "/");
+		setCookie(name, null, 0, "/", null);
 		return this;
 		return this;
 	}
 	}
 	
 	
@@ -436,7 +467,15 @@ public abstract class Controller {
 	 * Remove Cookie.
 	 * Remove Cookie.
 	 */
 	 */
 	public Controller removeCookie(String name, String path) {
 	public Controller removeCookie(String name, String path) {
-		setCookie(name, null, 0, path);
+		setCookie(name, null, 0, path, null);
+		return this;
+	}
+	
+	/**
+	 * Remove Cookie.
+	 */
+	public Controller removeCookie(String name, String path, String domain) {
+		setCookie(name, null, 0, path, domain);
 		return this;
 		return this;
 	}
 	}
 	
 	
@@ -628,7 +667,7 @@ public abstract class Controller {
 	}
 	}
 	
 	
 	private Locale getLocaleFromCookie() {
 	private Locale getLocaleFromCookie() {
-		Cookie cookie = getCookie(I18N_LOCALE);
+		Cookie cookie = getCookieObject(I18N_LOCALE);
 		if (cookie != null) {
 		if (cookie != null) {
 			return I18N.localeFromString(cookie.getValue());
 			return I18N.localeFromString(cookie.getValue());
 		}
 		}

+ 1 - 1
src/com/jfinal/ext/render/CaptchaRender.java

@@ -154,7 +154,7 @@ public class CaptchaRender extends Render {
 			return false;
 			return false;
 		try {
 		try {
 			inputRandomCode = encrypt(inputRandomCode);
 			inputRandomCode = encrypt(inputRandomCode);
-			return inputRandomCode.equals(controller.getCookieValue(randomCodeKey));
+			return inputRandomCode.equals(controller.getCookie(randomCodeKey));
 		} catch (Exception e) {
 		} catch (Exception e) {
 			e.printStackTrace();
 			e.printStackTrace();
 			return false;
 			return false;

+ 1 - 1
src/com/jfinal/i18n/I18N.java

@@ -53,7 +53,7 @@ public class I18N {
 	
 	
 	public static I18N me() {
 	public static I18N me() {
 		if (me == null) {
 		if (me == null) {
-			synchronized (me) {
+			synchronized (I18N.class) {
 				me = new I18N();
 				me = new I18N();
 			}
 			}
 		}
 		}

+ 17 - 1
src/com/jfinal/plugin/activerecord/Model.java

@@ -249,7 +249,7 @@ public abstract class Model<M extends Model> implements Serializable {
 	 * Danger! The update method will ignore the attribute if you change it directly.
 	 * Danger! The update method will ignore the attribute if you change it directly.
 	 * You must use set method to change attribute that update method can handle it.
 	 * You must use set method to change attribute that update method can handle it.
 	 */
 	 */
-	Map<String, Object> getAttrs() {
+	protected Map<String, Object> getAttrs() {
 		return attrs;
 		return attrs;
 	}
 	}
 	
 	
@@ -675,5 +675,21 @@ public abstract class Model<M extends Model> implements Serializable {
 	public Page<M> paginateByCache(String cacheName, Object key, int pageNumber, int pageSize, String select, String sqlExceptSelect) {
 	public Page<M> paginateByCache(String cacheName, Object key, int pageNumber, int pageSize, String select, String sqlExceptSelect) {
 		return paginateByCache(cacheName, key, pageNumber, pageSize, select, sqlExceptSelect, NULL_PARA_ARRAY);
 		return paginateByCache(cacheName, key, pageNumber, pageSize, select, sqlExceptSelect, NULL_PARA_ARRAY);
 	}
 	}
+	
+	/**
+	 * Return attribute names of this model.
+	 */
+	public String[] getAttrNames() {
+		Set<String> attrNameSet = attrs.keySet();
+		return attrNameSet.toArray(new String[attrNameSet.size()]);
+	}
+	
+	/**
+	 * Return attribute values of this model.
+	 */
+	public Object[] getAttrValues() {
+		java.util.Collection<Object> attrValueCollection = attrs.values();
+		return attrValueCollection.toArray(new Object[attrValueCollection.size()]);
+	}
 }
 }
 
 

+ 1 - 1
src/com/jfinal/plugin/druid/DruidStatViewHandler.java

@@ -71,7 +71,7 @@ public class DruidStatViewHandler extends Handler {
 	        }
 	        }
 	        // String uri = contextPath + servletPath;
 	        // String uri = contextPath + servletPath;
 	        // String path = requestURI.substring(contextPath.length() + servletPath.length());
 	        // String path = requestURI.substring(contextPath.length() + servletPath.length());
-	        int index = visitPath.length();
+	        int index = contextPath.length() + visitPath.length();
 	        String uri = requestURI.substring(0, index);
 	        String uri = requestURI.substring(0, index);
 	        String path = requestURI.substring(index);
 	        String path = requestURI.substring(index);