Revision: 997
http://stripes.svn.sourceforge.net/stripes/?rev=997&view=rev
Author: bengunter
Date: 2008-11-05 19:40:32 +0000 (Wed, 05 Nov 2008)
Log Message:
-----------
First cut at STS-614. Objects created during binding are created through the
new ObjectFactory class instead of a direct call to Class.newInstance().
Modified Paths:
--------------
trunk/stripes/src/net/sourceforge/stripes/util/ReflectUtil.java
trunk/stripes/src/net/sourceforge/stripes/util/bean/PropertyExpressionEvaluation.java
Added Paths:
-----------
trunk/stripes/src/net/sourceforge/stripes/util/ObjectFactory.java
Added: trunk/stripes/src/net/sourceforge/stripes/util/ObjectFactory.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/util/ObjectFactory.java
(rev 0)
+++ trunk/stripes/src/net/sourceforge/stripes/util/ObjectFactory.java
2008-11-05 19:40:32 UTC (rev 997)
@@ -0,0 +1,108 @@
+/* Copyright 2008 Ben Gunter
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.sourceforge.stripes.util;
+
+import net.sourceforge.stripes.controller.StripesFilter;
+import net.sourceforge.stripes.exception.StripesRuntimeException;
+
+/**
+ * <p>
+ * This class is responsible for creating new objects during the binding stage
of the request cycle.
+ * This implementation simply calls [EMAIL PROTECTED] Class#newInstance()} to
obtain a new instance.
+ * </p>
+ * <p>
+ * You can use an alternate implementation by specifying the class name in the
[EMAIL PROTECTED] StripesFilter}
+ * init-param [EMAIL PROTECTED] ObjectFactory.Class} or by placing your
implementation in one of the packages
+ * named in [EMAIL PROTECTED] Extension.Packages}. The specified class must,
of course, be a subclass of
+ * [EMAIL PROTECTED] ObjectFactory}.
+ * </p>
+ *
+ * <pre>
+ * <init-param>
+ * <param-name>ObjectFactory.Class</param-name>
+ *
<param-value>com.mycompany.stripes.ext.MyObjectFactory</param-value>
+ * </init-param>
+ * </pre>
+ *
+ * @author Ben Gunter
+ * @since Stripes 1.5.1
+ */
+public class ObjectFactory {
+ private static final Log log = Log.getInstance(ObjectFactory.class);
+
+ /**
+ * Name of the [EMAIL PROTECTED] StripesFilter} init-param that can be
used to indicate the class name of
+ * the preferred subclass of [EMAIL PROTECTED] ObjectFactory} to use.
+ */
+ public static final String CONFIGURATION_PARAMETER = "ObjectFactory.Class";
+
+ /** Singleton instance */
+ private static ObjectFactory instance;
+
+ /**
+ * Get the singleton object factory. By default, this method returns an
instance of
+ * [EMAIL PROTECTED] ObjectFactory}. The [EMAIL PROTECTED] StripesFilter}
init-param named [EMAIL PROTECTED] ObjectFactory.Class}
+ * can be used to provide an alternate implementation.
+ */
+ public static ObjectFactory getInstance() {
+ if (instance == null) {
+ Class<? extends ObjectFactory> type =
StripesFilter.getConfiguration()
+
.getBootstrapPropertyResolver().getClassProperty(CONFIGURATION_PARAMETER,
+ ObjectFactory.class);
+
+ if (type == null) {
+ instance = new ObjectFactory();
+ }
+ else {
+ try {
+ instance = type.newInstance();
+ }
+ catch (InstantiationException e) {
+ throw new StripesRuntimeException(e);
+ }
+ catch (IllegalAccessException e) {
+ throw new StripesRuntimeException(e);
+ }
+ }
+
+ log.info("ObjectFactory implementation is " +
instance.getClass().getName());
+ }
+
+ return instance;
+ }
+
+ /** Set the singleton instance. */
+ public static void setInstance(ObjectFactory instance) {
+ ObjectFactory.instance = instance;
+ }
+
+ /**
+ * Calls [EMAIL PROTECTED] Class#newInstance()} and returns the newly
created object.
+ *
+ * @param clazz The class to instantiate.
+ * @return The new object
+ */
+ public <T> T newInstance(Class<T> clazz) {
+ try {
+ return clazz.newInstance();
+ }
+ catch (InstantiationException e) {
+ throw new StripesRuntimeException("Could not instantiate " +
clazz, e);
+ }
+ catch (IllegalAccessException e) {
+ throw new StripesRuntimeException("Could not instantiate " +
clazz, e);
+ }
+ }
+}
Modified: trunk/stripes/src/net/sourceforge/stripes/util/ReflectUtil.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/util/ReflectUtil.java
2008-11-03 14:38:00 UTC (rev 996)
+++ trunk/stripes/src/net/sourceforge/stripes/util/ReflectUtil.java
2008-11-05 19:40:32 UTC (rev 997)
@@ -135,7 +135,7 @@
"might get implemented.");
}
else {
- return (T) impl.newInstance();
+ return ObjectFactory.getInstance().<T>newInstance(impl);
}
}
Modified:
trunk/stripes/src/net/sourceforge/stripes/util/bean/PropertyExpressionEvaluation.java
===================================================================
---
trunk/stripes/src/net/sourceforge/stripes/util/bean/PropertyExpressionEvaluation.java
2008-11-03 14:38:00 UTC (rev 996)
+++
trunk/stripes/src/net/sourceforge/stripes/util/bean/PropertyExpressionEvaluation.java
2008-11-05 19:40:32 UTC (rev 997)
@@ -14,6 +14,7 @@
*/
package net.sourceforge.stripes.util.bean;
+import net.sourceforge.stripes.util.ObjectFactory;
import net.sourceforge.stripes.util.ReflectUtil;
import java.beans.PropertyDescriptor;
@@ -645,7 +646,7 @@
return ReflectUtil.getInterfaceInstance(clazz);
}
else {
- return clazz.newInstance();
+ return ObjectFactory.getInstance().newInstance(clazz);
}
}
catch (Exception e) {
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development