Commits

Anonymous committed 43b4e4c

Added support for constructor injections

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1294e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (3)

src/java/com/opensymphony/xwork2/config/impl/LocatableFactory.java

-package com.opensymphony.xwork2.config.impl;
-
-import com.opensymphony.xwork2.inject.Context;
-import com.opensymphony.xwork2.inject.Factory;
-import com.opensymphony.xwork2.util.location.Located;
-import com.opensymphony.xwork2.util.location.LocationUtils;
-
-/**
- * Attaches location information to the factory.  Construction limited to no-arg 
- * constructors.  
- */
-public class LocatableFactory extends Located implements Factory {
-
-    private Class cls;
-    public LocatableFactory(Class cls, Object location) {
-        this.cls = cls;
-        setLocation(LocationUtils.getLocation(location));
-    }
-    
-    public Object create(Context context) throws Exception {
-        Object obj = cls.newInstance();
-        context.getContainer().inject(obj);
-        return obj;
-    }
-    
-    public String toString() {
-        StringBuilder sb = new StringBuilder();
-        sb.append(super.toString());
-        sb.append(" defined at ");
-        sb.append(getLocation().toString());
-        return sb.toString();
-    }
-}

src/java/com/opensymphony/xwork2/config/providers/XmlConfigurationProvider.java

 import com.opensymphony.xwork2.XWorkException;
 import com.opensymphony.xwork2.config.*;
 import com.opensymphony.xwork2.config.entities.*;
-import com.opensymphony.xwork2.config.impl.LocatableFactory;
-import com.opensymphony.xwork2.inject.Container;
-import com.opensymphony.xwork2.inject.ContainerBuilder;
-import com.opensymphony.xwork2.inject.Inject;
-import com.opensymphony.xwork2.inject.Scope;
+import com.opensymphony.xwork2.inject.LocatableFactory;
+import com.opensymphony.xwork2.inject.*;
 import com.opensymphony.xwork2.util.DomHelper;
 import com.opensymphony.xwork2.util.location.LocatableProperties;
 import com.opensymphony.xwork2.util.location.Location;
                                 if (LOG.isDebugEnabled()) {
                                     LOG.debug("Loaded type:"+type+" name:"+name+" impl:"+impl);
                                 }
-                                containerBuilder.factory(ctype, name, new LocatableFactory(cimpl, childNode), scope);
+                                containerBuilder.factory(ctype, name, new LocatableFactory(name, ctype, cimpl, scope, childNode), scope);
                             }
                             loadedBeans.put(ctype.getName()+name, child);
                         } catch (Throwable ex) {

src/java/com/opensymphony/xwork2/inject/LocatableFactory.java

+package com.opensymphony.xwork2.inject;
+
+import com.opensymphony.xwork2.util.location.Located;
+import com.opensymphony.xwork2.util.location.LocationUtils;
+
+import java.util.LinkedHashMap;
+
+/**
+ * Attaches location information to the factory.  Construction limited to no-arg
+ * constructors.
+ */
+public class LocatableFactory<T> extends Located implements Factory<T> {
+
+    volatile ContainerImpl.ConstructorInjector<? extends T> constructor;
+    private Class implementation;
+    private Class type;
+    private String name;
+    private Scope scope;
+
+    public LocatableFactory(String name, Class type, Class implementation, Scope scope, Object location) {
+        this.implementation = implementation;
+        this.type = type;
+        this.name = name;
+        this.scope = scope;
+        setLocation(LocationUtils.getLocation(location));
+    }
+
+    @SuppressWarnings("unchecked")
+    public T create(Context context) {
+        if (constructor == null) {
+            this.constructor =
+                    ((ContainerImpl)context.getContainer()).getConstructor(implementation);
+        }
+        InternalContext ctx = new InternalContext((ContainerImpl) context.getContainer());
+        ctx.setExternalContext((ExternalContext)context);
+        return (T) constructor.construct(ctx, type);
+    }
+
+    public String toString() {
+        String fields = new LinkedHashMap<String, Object>() {
+            {
+                put("type", type);
+                put("name", name);
+                put("implementation", implementation);
+                put("scope", scope);
+            }
+        }.toString();
+        StringBuilder sb = new StringBuilder(fields);
+        sb.append(super.toString());
+        sb.append(" defined at ");
+        sb.append(getLocation().toString());
+        return sb.toString();
+    }
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.