Commits

rgielen  committed 66d6b19

XW-536:
ObjectTypeDeterminerFactory confuses user about "detected" GenericsObjectTypeDeterminer

- GenericObjectTypeDeterminer now default
- improved logging

git-svn-id: http://svn.opensymphony.com/svn/xwork/branches/2.0@1540e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 8f43adc
  • Branches 2.0, xwork_2_0_7

Comments (0)

Files changed (2)

File src/java/com/opensymphony/xwork2/util/ObjectTypeDeterminerFactory.java

 /*
- * Copyright (c) 2002-2006 by OpenSymphony
+ * Copyright (c) 2002-2007 by OpenSymphony
  * All rights reserved.
  */
 package com.opensymphony.xwork2.util;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import com.opensymphony.xwork2.util.ClassLoaderUtil;
 
 /**
  * Factory for getting an instance of {@link ObjectTypeDeterminer}.
- * <br/>
- * Will use <code>com.opensymphony.xwork2.util.GenericsObjectTypeDeterminer</code> if running on JDK5 or higher.
- * If not <code>com.opensymphony.xwork2.util.ObjectTypeDeterminer</code> is used.
+ * <p/>
+ * Will use <code>com.opensymphony.xwork2.util.GenericsObjectTypeDeterminer</code> by default.
+ *
+ * @see com.opensymphony.xwork2.util.GenericsObjectTypeDeterminer
+ * @see com.opensymphony.xwork2.util.ObjectTypeDeterminer
+ * @see com.opensymphony.xwork2.util.DefaultObjectTypeDeterminer
  *
  * @author plightbo
  * @author Rainer Hermanns
+ * @author Rene Gielen
  */
 public class ObjectTypeDeterminerFactory {
     private static final Log LOG = LogFactory.getLog(ObjectTypeDeterminerFactory.class);
 
-    private static ObjectTypeDeterminer instance = new DefaultObjectTypeDeterminer();
+    private static ObjectTypeDeterminer instance = new GenericsObjectTypeDeterminer();
 
     static {
-        try {
-            Class c = ClassLoaderUtil.loadClass("com.opensymphony.xwork2.util.GenericsObjectTypeDeterminer",
-                    ObjectTypeDeterminerFactory.class);
-
-            LOG.info("Detected GenericsObjectTypeDeterminer, initializing it...");
-            instance = (ObjectTypeDeterminer) c.newInstance();
-        } catch (ClassNotFoundException e) {
-            // this is fine, just fall back to the default object type determiner
-        } catch (Exception e) {
-            LOG.error("Exception when trying to create new GenericsObjectTypeDeterminer", e);
-        }
+        LOG.info("Setting GenericsObjectTypeDeterminer as default ...");
     }
 
+    /**
+     * Sets a new instance of ObjectTypeDeterminer to be used.
+     *
+     * @param instance  instance of ObjectTypeDeterminer
+     */
     public static void setInstance(ObjectTypeDeterminer instance) {
-        ObjectTypeDeterminerFactory.instance = instance;
+        if (instance != null) {
+            if (!instance.getClass().equals(ObjectTypeDeterminerFactory.instance.getClass())) {
+                LOG.info("Switching to ObjectTypeDeterminer of type " + instance.getClass().getName());
+            }
+            ObjectTypeDeterminerFactory.instance = instance;
+        }
     }
 
+    /**
+     * Gets the instance of ObjectTypeDeterminer to be used.
+     *
+     * @return instance of ObjectTypeDeterminer
+     */
     public static ObjectTypeDeterminer getInstance() {
         return instance;
     }
+
 }

File src/test/com/opensymphony/xwork2/util/ObjectTypeDeterminerFactoryTest.java

+package com.opensymphony.xwork2.util;
+
+import junit.framework.*;
+
+/**
+ * ObjectTypeDeterminerFactoryTest test for given class.
+ *
+ * @author Rene Gielen
+ */
+public class ObjectTypeDeterminerFactoryTest extends TestCase {
+
+
+    public void testDefaultInstanceTypeIsGenericsObjectTypeDeterminer() throws Exception {
+        assertEquals(ObjectTypeDeterminerFactory.getInstance().getClass(),GenericsObjectTypeDeterminer.class);
+    }
+
+    public void testSetInstance() throws Exception {
+        ObjectTypeDeterminer objectTypeDeterminer = ObjectTypeDeterminerFactory.getInstance();
+        try {
+            ObjectTypeDeterminerFactory.setInstance(null);
+            assertEquals(ObjectTypeDeterminerFactory.getInstance(), objectTypeDeterminer);
+            ObjectTypeDeterminerFactory.setInstance(new DefaultObjectTypeDeterminer());
+            assertFalse(ObjectTypeDeterminerFactory.getInstance().getClass().equals(objectTypeDeterminer.getClass()));
+        } finally {
+            ObjectTypeDeterminerFactory.setInstance(objectTypeDeterminer);
+        }
+    }
+}