View Javadoc
1   /**
2    * 
3    */
4   package siouxsie.mvc.impl;
5   
6   import java.util.ArrayList;
7   import java.util.Collection;
8   import java.util.Enumeration;
9   import java.util.HashMap;
10  import java.util.HashSet;
11  import java.util.Iterator;
12  import java.util.List;
13  import java.util.Map;
14  import java.util.Set;
15  
16  import ognl.MethodAccessor;
17  import ognl.PropertyAccessor;
18  import siouxsie.mvc.IActionTrigger;
19  
20  import com.opensymphony.xwork2.ActionProxyFactory;
21  import com.opensymphony.xwork2.DefaultActionProxyFactory;
22  import com.opensymphony.xwork2.DefaultTextProvider;
23  import com.opensymphony.xwork2.ObjectFactory;
24  import com.opensymphony.xwork2.TextProvider;
25  import com.opensymphony.xwork2.config.Configuration;
26  import com.opensymphony.xwork2.config.ConfigurationException;
27  import com.opensymphony.xwork2.config.ConfigurationProvider;
28  import com.opensymphony.xwork2.conversion.NullHandler;
29  import com.opensymphony.xwork2.conversion.ObjectTypeDeterminer;
30  import com.opensymphony.xwork2.conversion.impl.DefaultObjectTypeDeterminer;
31  import com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler;
32  import com.opensymphony.xwork2.conversion.impl.XWorkBasicConverter;
33  import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
34  import com.opensymphony.xwork2.inject.ContainerBuilder;
35  import com.opensymphony.xwork2.inject.Scope;
36  import com.opensymphony.xwork2.ognl.ObjectProxy;
37  import com.opensymphony.xwork2.ognl.OgnlReflectionContextFactory;
38  import com.opensymphony.xwork2.ognl.OgnlReflectionProvider;
39  import com.opensymphony.xwork2.ognl.OgnlUtil;
40  import com.opensymphony.xwork2.ognl.OgnlValueStackFactory;
41  import com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor;
42  import com.opensymphony.xwork2.ognl.accessor.ObjectAccessor;
43  import com.opensymphony.xwork2.ognl.accessor.ObjectProxyPropertyAccessor;
44  import com.opensymphony.xwork2.ognl.accessor.XWorkCollectionPropertyAccessor;
45  import com.opensymphony.xwork2.ognl.accessor.XWorkEnumerationAccessor;
46  import com.opensymphony.xwork2.ognl.accessor.XWorkIteratorPropertyAccessor;
47  import com.opensymphony.xwork2.ognl.accessor.XWorkListPropertyAccessor;
48  import com.opensymphony.xwork2.ognl.accessor.XWorkMapPropertyAccessor;
49  import com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor;
50  import com.opensymphony.xwork2.util.CompoundRoot;
51  import com.opensymphony.xwork2.util.PatternMatcher;
52  import com.opensymphony.xwork2.util.ValueStackFactory;
53  import com.opensymphony.xwork2.util.WildcardHelper;
54  import com.opensymphony.xwork2.util.location.LocatableProperties;
55  import com.opensymphony.xwork2.util.reflection.ReflectionContextFactory;
56  import com.opensymphony.xwork2.util.reflection.ReflectionProvider;
57  import com.opensymphony.xwork2.validator.ActionValidatorManager;
58  import com.opensymphony.xwork2.validator.AnnotationActionValidatorManager;
59  import com.opensymphony.xwork2.validator.DefaultActionValidatorManager;
60  import com.opensymphony.xwork2.validator.DefaultValidatorFactory;
61  import com.opensymphony.xwork2.validator.DefaultValidatorFileParser;
62  import com.opensymphony.xwork2.validator.ValidatorFactory;
63  import com.opensymphony.xwork2.validator.ValidatorFileParser;
64  
65  /**
66   * XW initialisation.
67   * Allow the object factory setting.
68   * Code taken from XW 2.1.1.
69   * @author Arnaud Cogoluegnes
70   * @version $Id: SiouxsieMVCConfigurationProvider.java 140 2008-05-25 13:32:56Z acogo $
71   */
72  public class SiouxsieMVCConfigurationProvider implements ConfigurationProvider {
73  	
74  	private Class<? extends ObjectFactory> objectFactoryClazz;
75  
76  	/* (non-Javadoc)
77  	 * @see com.opensymphony.xwork2.config.ContainerProvider#destroy()
78  	 */
79  	public void destroy() {}
80  
81  	/* (non-Javadoc)
82  	 * @see com.opensymphony.xwork2.config.ContainerProvider#init(com.opensymphony.xwork2.config.Configuration)
83  	 */
84  	public void init(Configuration configuration) throws ConfigurationException {}
85  
86  	/* (non-Javadoc)
87  	 * @see com.opensymphony.xwork2.config.ContainerProvider#needsReload()
88  	 */
89  	public boolean needsReload() {
90  		return false;
91  	}
92  
93  	/* (non-Javadoc)
94  	 * @see com.opensymphony.xwork2.config.ContainerProvider#register(com.opensymphony.xwork2.inject.ContainerBuilder, com.opensymphony.xwork2.util.location.LocatableProperties)
95  	 */
96  	public void register(ContainerBuilder builder, LocatableProperties props)
97  			throws ConfigurationException {
98  		// the ObjectFactory implementation to use
99  		// taken from the corresponding property
100 		// (Siouxsie specific code)
101 		builder.factory(ObjectFactory.class,objectFactoryClazz == null ? ObjectFactory.class : objectFactoryClazz, Scope.SINGLETON)
102 		// the action trigger to use ((Siouxsie specific code))
103 		.factory(IActionTrigger.class,ActionTrigger.class, Scope.SINGLETON)
104 		
105 		
106 		// following code taken from XWork
107         .factory(ActionProxyFactory.class, DefaultActionProxyFactory.class, Scope.SINGLETON)
108         .factory(ObjectTypeDeterminer.class, DefaultObjectTypeDeterminer.class, Scope.SINGLETON)
109         .factory(XWorkConverter.class, Scope.SINGLETON)
110         .factory(ValueStackFactory.class, OgnlValueStackFactory.class, Scope.SINGLETON)
111         .factory(ValidatorFactory.class, DefaultValidatorFactory.class, Scope.SINGLETON)
112         .factory(ValidatorFileParser.class, DefaultValidatorFileParser.class, Scope.SINGLETON)
113         .factory(PatternMatcher.class, WildcardHelper.class, Scope.SINGLETON)
114         .factory(ReflectionProvider.class, OgnlReflectionProvider.class, Scope.SINGLETON)
115         .factory(ReflectionContextFactory.class, OgnlReflectionContextFactory.class, Scope.SINGLETON)
116         .factory(PropertyAccessor.class, CompoundRoot.class.getName(), CompoundRootAccessor.class, Scope.SINGLETON)
117         .factory(PropertyAccessor.class, Object.class.getName(), ObjectAccessor.class, Scope.SINGLETON)
118         .factory(PropertyAccessor.class, Iterator.class.getName(), XWorkIteratorPropertyAccessor.class, Scope.SINGLETON)
119         .factory(PropertyAccessor.class, Enumeration.class.getName(), XWorkEnumerationAccessor.class, Scope.SINGLETON)
120         
121         // silly workarounds for ognl since there is no way to flush its caches
122         .factory(PropertyAccessor.class, List.class.getName(), XWorkListPropertyAccessor.class, Scope.SINGLETON)
123         .factory(PropertyAccessor.class, ArrayList.class.getName(), XWorkListPropertyAccessor.class, Scope.SINGLETON)
124         .factory(PropertyAccessor.class, HashSet.class.getName(), XWorkCollectionPropertyAccessor.class, Scope.SINGLETON)
125         .factory(PropertyAccessor.class, Set.class.getName(), XWorkCollectionPropertyAccessor.class, Scope.SINGLETON)
126         .factory(PropertyAccessor.class, HashMap.class.getName(), XWorkMapPropertyAccessor.class, Scope.SINGLETON)
127         .factory(PropertyAccessor.class, Map.class.getName(), XWorkMapPropertyAccessor.class, Scope.SINGLETON)
128         
129         .factory(PropertyAccessor.class, Collection.class.getName(), XWorkCollectionPropertyAccessor.class, Scope.SINGLETON)
130         .factory(PropertyAccessor.class, ObjectProxy.class.getName(), ObjectProxyPropertyAccessor.class, Scope.SINGLETON)
131         .factory(MethodAccessor.class, Object.class.getName(), XWorkMethodAccessor.class, Scope.SINGLETON)
132         .factory(MethodAccessor.class, CompoundRoot.class.getName(), CompoundRootAccessor.class, Scope.SINGLETON)
133         .factory(NullHandler.class, Object.class.getName(), InstantiatingNullHandler.class, Scope.SINGLETON)
134         .factory(ActionValidatorManager.class, AnnotationActionValidatorManager.class, Scope.SINGLETON)
135         .factory(ActionValidatorManager.class, "no-annotations", DefaultActionValidatorManager.class, Scope.SINGLETON)
136         .factory(TextProvider.class, "system", DefaultTextProvider.class, Scope.SINGLETON)
137         .factory(OgnlUtil.class, Scope.SINGLETON)
138         .factory(XWorkBasicConverter.class, Scope.SINGLETON);
139 		props.setProperty("devMode", Boolean.FALSE.toString());
140 	}
141 
142 	/* (non-Javadoc)
143 	 * @see com.opensymphony.xwork2.config.PackageProvider#loadPackages()
144 	 */
145 	public void loadPackages() throws ConfigurationException {}
146 
147 	public void setObjectFactoryClazz(
148 			Class<? extends ObjectFactory> objectFactoryClazz) {
149 		this.objectFactoryClazz = objectFactoryClazz;
150 	}
151 
152 }