Modified:
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/AbstractBaseController.java
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/AbstractBaseFactory.java
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/CommonPool.java
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/PoolStaticUtils.java
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/StackPool.java
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/context/PoolScope.java
trunk/JGentleProject/src/org/jgentleframework/utils/ReflectUtils.java
Log:
Optimize code
Modified:
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/AbstractBaseController.java
==============================================================================
---
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/AbstractBaseController.java
(original)
+++
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/AbstractBaseController.java
Wed Jul 15 04:51:31 2009
@@ -28,6 +28,7 @@
import org.jgentleframework.context.injecting.Provider;
import
org.jgentleframework.services.objectpooling.annotation.CanBePooledMethod;
import
org.jgentleframework.services.objectpooling.annotation.DeactivateMethod;
+import
org.jgentleframework.services.objectpooling.annotation.SystemPooling;
import
org.jgentleframework.services.objectpooling.annotation.ValidateMethod;
import org.jgentleframework.services.objectpooling.context.CanBePooled;
import org.jgentleframework.services.objectpooling.context.Deactivate;
@@ -60,15 +61,17 @@
*/
protected void activatesObject(Object obj) throws Exception {
- if (ReflectUtils.isCast(Initializing.class, obj))
- ((Initializing) obj).activate();
- else if (this.definition
- .isAnnotationPresentAtAnyMethods(InitializingMethod.class)) {
- List<Method> methods = this.definition
- .getMethodsAnnotatedWith(InitializingMethod.class);
- for (Method method : methods) {
- method.setAccessible(true);
- method.invoke(obj);
+ if (obj != null) {
+ if (ReflectUtils.isCast(Initializing.class, obj))
+ ((Initializing) obj).activate();
+ else if (this.definition
+ .isAnnotationPresentAtAnyMethods(InitializingMethod.class)) {
+ List<Method> methods = this.definition
+ .getMethodsAnnotatedWith(InitializingMethod.class);
+ for (Method method : methods) {
+ method.setAccessible(true);
+ method.invoke(obj);
+ }
}
}
}
@@ -83,26 +86,29 @@
protected boolean canBePooled(Object obj) throws Throwable {
try {
- if (this.isCanBePooled()
- && ReflectUtils.isCast(CanBePooled.class, obj)) {
- return ((CanBePooled) obj).canBePooled();
- }
- else if (this.definition
- .isAnnotationPresentAtAnyMethods(CanBePooledMethod.class)) {
- List<Method> methods = this.definition
- .getMethodsAnnotatedWith(CanBePooledMethod.class);
- for (Method method : methods) {
- if (method.getReturnType() == Boolean.class
- || method.getReturnType() == boolean.class) {
- method.setAccessible(true);
- return (Boolean) method.invoke(obj);
- }
- else {
- if (log.isErrorEnabled()) {
- log.error(
- "The 'return type' of canBePooled method ["
- + method + "] must be boolean !!",
- new UnsupportedOperationException());
+ if (obj != null) {
+ if (this.isCanBePooled()
+ && ReflectUtils.isCast(CanBePooled.class, obj)) {
+ return ((CanBePooled) obj).canBePooled();
+ }
+ else if (this.definition
+ .isAnnotationPresentAtAnyMethods(CanBePooledMethod.class)) {
+ List<Method> methods = this.definition
+ .getMethodsAnnotatedWith(CanBePooledMethod.class);
+ for (Method method : methods) {
+ if (method.getReturnType() == Boolean.class
+ || method.getReturnType() == boolean.class) {
+ method.setAccessible(true);
+ return (Boolean) method.invoke(obj);
+ }
+ else {
+ if (log.isErrorEnabled()) {
+ log.error(
+ "The 'return type' of canBePooled method ["
+ + method
+ + "] must be boolean !!",
+ new UnsupportedOperationException());
+ }
}
}
}
@@ -116,7 +122,8 @@
notifyAll();
}
}
- return true;
+ // return default value of CanBePooled attribute.
+ return SystemPooling.DEFAULT_CAN_BE_POOLED;
}
/**
@@ -171,16 +178,18 @@
protected void deactivateObject(Object obj) throws Throwable {
try {
- if (ReflectUtils.isCast(Deactivate.class, obj)) {
- ((Deactivate) obj).deactivate();
- }
- else if (this.definition
- .isAnnotationPresentAtAnyMethods(DeactivateMethod.class)) {
- List<Method> methods = this.definition
- .getMethodsAnnotatedWith(DeactivateMethod.class);
- for (Method method : methods) {
- method.setAccessible(true);
- method.invoke(obj);
+ if (obj != null) {
+ if (ReflectUtils.isCast(Deactivate.class, obj)) {
+ ((Deactivate) obj).deactivate();
+ }
+ else if (this.definition
+ .isAnnotationPresentAtAnyMethods(DeactivateMethod.class)) {
+ List<Method> methods = this.definition
+ .getMethodsAnnotatedWith(DeactivateMethod.class);
+ for (Method method : methods) {
+ method.setAccessible(true);
+ method.invoke(obj);
+ }
}
}
}
@@ -223,15 +232,17 @@
protected void destroyObject(Object obj) throws Throwable {
try {
- if (ReflectUtils.isCast(Disposable.class, obj))
- ((Disposable) obj).destroy();
- else if (this.definition
- .isAnnotationPresentAtAnyMethods(DisposableMethod.class)) {
- List<Method> methods = this.definition
- .getMethodsAnnotatedWith(DisposableMethod.class);
- for (Method method : methods) {
- method.setAccessible(true);
- method.invoke(obj);
+ if (obj != null) {
+ if (ReflectUtils.isCast(Disposable.class, obj))
+ ((Disposable) obj).destroy();
+ else if (this.definition
+ .isAnnotationPresentAtAnyMethods(DisposableMethod.class)) {
+ List<Method> methods = this.definition
+ .getMethodsAnnotatedWith(DisposableMethod.class);
+ for (Method method : methods) {
+ method.setAccessible(true);
+ method.invoke(obj);
+ }
}
}
}
@@ -252,27 +263,31 @@
protected void validatesObject(Object obj) throws Throwable {
try {
- if (this.isTestOnObtain()
- && ReflectUtils.isCast(Validate.class, obj)) {
- if (!((Validate) obj).validate())
- throw new Exception("Validate failed !!");
- }
- else if (this.definition
- .isAnnotationPresentAtAnyMethods(ValidateMethod.class)) {
- List<Method> methods = this.definition
- .getMethodsAnnotatedWith(ValidateMethod.class);
- for (Method method : methods) {
- if (method.getReturnType() == Boolean.class
- || method.getReturnType() == boolean.class) {
- method.setAccessible(true);
- if (!(Boolean) method.invoke(obj))
- throw new Exception("Validate failed !!");
- }
- else {
- if (log.isErrorEnabled()) {
- log.error("The 'return type' of validate method ["
- + method + "] must be boolean !!",
- new UnsupportedOperationException());
+ if (obj != null) {
+ if (this.isTestOnObtain()
+ && ReflectUtils.isCast(Validate.class, obj)) {
+ if (!((Validate) obj).validate())
+ throw new Exception("Validate failed !!");
+ }
+ else if (this.definition
+ .isAnnotationPresentAtAnyMethods(ValidateMethod.class)) {
+ List<Method> methods = this.definition
+ .getMethodsAnnotatedWith(ValidateMethod.class);
+ for (Method method : methods) {
+ if (method.getReturnType() == Boolean.class
+ || method.getReturnType() == boolean.class) {
+ method.setAccessible(true);
+ if (!(Boolean) method.invoke(obj))
+ throw new Exception("Validate failed !!");
+ }
+ else {
+ if (log.isErrorEnabled()) {
+ log.error(
+ "The 'return type' of validate method ["
+ + method
+ + "] must be boolean !!",
+ new UnsupportedOperationException());
+ }
}
}
}
Modified:
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/AbstractBaseFactory.java
==============================================================================
---
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/AbstractBaseFactory.java
(original)
+++
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/AbstractBaseFactory.java
Wed Jul 15 04:51:31 2009
@@ -55,17 +55,17 @@
else {
deactivateObject(obj);
}
- synchronized (this) {
- if (isEnable() && success) {
- pool.add(new TimestampObjectBean<Object>(obj));
- }
- }
if (!success
|| !isEnable()
|| ((this.getMaxIdle() >= 0) && (pool.size() >= this
.getMaxIdle()))) {
destroyObject(obj);
}
+ synchronized (this) {
+ if (isEnable() && success) {
+ pool.add(new TimestampObjectBean<Object>(obj));
+ }
+ }
if (decrementNumActive) {
synchronized (this) {
this.numActive--;
@@ -101,16 +101,7 @@
@Override
public void returnObject(Object obj) throws Throwable {
- try {
- addObjectToPool(obj, true);
- }
- catch (Exception e) {
- destroyObject(obj);
- synchronized (this) {
- this.numActive--;
- notifyAll();
- }
- }
+ addObjectToPool(obj, true);
}
/*
Modified:
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/CommonPool.java
==============================================================================
---
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/CommonPool.java
(original)
+++
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/CommonPool.java
Wed Jul 15 04:51:31 2009
@@ -145,6 +145,12 @@
}
}
}
+ else {
+ Object result = pair.getValue();
+ activatesObject(result);
+ validatesObject(result);
+ return result;
+ }
}
}
}
Modified:
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/PoolStaticUtils.java
==============================================================================
---
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/PoolStaticUtils.java
(original)
+++
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/PoolStaticUtils.java
Wed Jul 15 04:51:31 2009
@@ -17,18 +17,13 @@
*/
package org.jgentleframework.services.objectpooling;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
import java.util.Iterator;
-import java.util.List;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Stack;
import java.util.Timer;
import java.util.TimerTask;
-import org.jgentleframework.context.beans.Initializing;
-import org.jgentleframework.context.beans.annotation.InitializingMethod;
import org.jgentleframework.utils.ReflectUtils;
import org.jgentleframework.utils.data.TimestampObjectBean;
@@ -243,48 +238,27 @@
AbstractBaseFactory pool) {
if (pair != null) {
- boolean removeObject = false;
+ boolean remove = false;
final long idleTimeMilis = System.currentTimeMillis()
- pair.getTstamp();
// check minimum evictable idle time
if ((pool.getMinEvictableIdleTime() > 0)
&& (idleTimeMilis > pool.getMinEvictableIdleTime())) {
- removeObject = true;
+ remove = true;
}
else if ((pool.getSoftMinEvictableIdleTime() > 0)
&& (idleTimeMilis > pool.getSoftMinEvictableIdleTime())
&& (pool.getNumIdle() > pool.getMinIdle())) {
- removeObject = true;
+ remove = true;
}
- if (pool.isTestWhileIdle() && !removeObject) {
+ if (pool.isTestWhileIdle() && !remove) {
boolean active = false;
try {
- if (ReflectUtils
- .isCast(Initializing.class, pair.getValue())) {
- ((Initializing) pair.getValue()).activate();
- }
- else if (pool.definition
- .isAnnotationPresentAtAnyMethods(InitializingMethod.class)) {
- List<Method> methods = pool.definition
- .getMethodsAnnotatedWith(InitializingMethod.class);
- for (Method method : methods) {
- method.setAccessible(true);
- method.invoke(pair.getValue());
- }
- }
+ pool.activatesObject(pair.getValue());
active = true;
}
- catch (SecurityException e1) {
- removeObject = true;
- }
- catch (IllegalArgumentException e1) {
- removeObject = true;
- }
- catch (IllegalAccessException e1) {
- removeObject = true;
- }
- catch (InvocationTargetException e1) {
- removeObject = true;
+ catch (Exception e) {
+ remove = true;
}
if (active) {
try {
@@ -292,11 +266,11 @@
pool.deactivateObject(pair.getValue());
}
catch (Throwable e) {
- removeObject = true;
+ remove = true;
}
}
}
- if (removeObject) {
+ if (remove) {
pool.pool.remove(pair);
try {
pool.destroyObject(pair.getValue());
Modified:
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/StackPool.java
==============================================================================
---
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/StackPool.java
(original)
+++
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/StackPool.java
Wed Jul 15 04:51:31 2009
@@ -29,6 +29,9 @@
* @author Quoc Chung - mailto: <a
* href="mailto:skydu...@yahoo.com">skydu...@yahoo.com</a>
* @date Apr 10, 2009
+ * @see AbstractBaseFactory
+ * @see AbstractBaseController
+ * @see AbstractBasePooling
*/
public class StackPool extends AbstractBaseFactory {
/*
Modified:
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/context/PoolScope.java
==============================================================================
---
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/context/PoolScope.java
(original)
+++
trunk/JGentleProject/src/org/jgentleframework/services/objectpooling/context/PoolScope.java
Wed Jul 15 04:51:31 2009
@@ -181,7 +181,9 @@
result = pool.obtainObject();
}
catch (NoSuchElementException e) {
- // ignore
+ if (log.isErrorEnabled()) {
+ log.error(e.getMessage(), e);
+ }
}
catch (Throwable e) {
if (log.isErrorEnabled()) {
Modified:
trunk/JGentleProject/src/org/jgentleframework/utils/ReflectUtils.java
==============================================================================
--- trunk/JGentleProject/src/org/jgentleframework/utils/ReflectUtils.java
(original)
+++ trunk/JGentleProject/src/org/jgentleframework/utils/ReflectUtils.java
Wed Jul 15 04:51:31 2009
@@ -40,6 +40,9 @@
import org.jgentleframework.core.reflection.metadata.AnnoMeta;
import org.jgentleframework.core.reflection.metadata.MetaDataFactory;
+import sun.reflect.FieldAccessor;
+import sun.reflect.ReflectionFactory;
+
/**
* Provides some of static methods in order to execute common reflect
* operations.
@@ -1307,5 +1310,37 @@
}
}
return lst.toArray(new Method[lst.size()]);
+ }
+
+ /** The Constant MODIFIERS_FIELD. */
+ private static final String MODIFIERS_FIELD = "modifiers";
+
+ /** The Constant reflection. */
+ private static final ReflectionFactory reflection = ReflectionFactory
+ .getReflectionFactory();
+
+ /**
+ * Sets new value to static final field.
+ *
+ * @param field
+ * the field
+ * @param value
+ * the new value
+ * @throws NoSuchFieldException
+ * the no such field exception
+ * @throws IllegalAccessException
+ * the illegal access exception
+ */
+ public static void setStaticFinalField(Field field, Object value)
+ throws NoSuchFieldException, IllegalAccessException {
+
+ field.setAccessible(true);
+ Field modifiersField = Field.class.getDeclaredField(MODIFIERS_FIELD);
+ modifiersField.setAccessible(true);
+ int modifiers = modifiersField.getInt(field);
+ modifiers &= ~Modifier.FINAL;
+ modifiersField.setInt(field, modifiers);
+ FieldAccessor fa = reflection.newFieldAccessor(field, false);
+ fa.set(null, value);
}
}