View Javadoc

1   /*
2    * $Id: StrutsObjectFactory.java 506198 2007-02-12 00:57:44Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  // Copyright 2006 Google Inc. All Rights Reserved.
22  
23  package org.apache.struts2.impl;
24  
25  import com.opensymphony.xwork2.ObjectFactory;
26  import com.opensymphony.xwork2.Result;
27  import com.opensymphony.xwork2.config.ConfigurationException;
28  import com.opensymphony.xwork2.config.entities.InterceptorConfig;
29  import com.opensymphony.xwork2.config.entities.ResultConfig;
30  import com.opensymphony.xwork2.interceptor.Interceptor;
31  import com.opensymphony.xwork2.util.OgnlUtil;
32  
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  public class StrutsObjectFactory extends ObjectFactory {
37  
38      public Interceptor buildInterceptor(InterceptorConfig interceptorConfig, Map refParams)
39              throws ConfigurationException {
40          String className = interceptorConfig.getClassName();
41  
42          Map<String, String> params = new HashMap<String, String>();
43          Map typeParams = interceptorConfig.getParams();
44          if (typeParams != null && !typeParams.isEmpty())
45              params.putAll(typeParams);
46          if (refParams != null && !refParams.isEmpty())
47              params.putAll(refParams);
48          params.putAll(refParams);
49  
50          try {
51              // interceptor instances are long-lived and used across user sessions, so don't try to pass in any extra
52              // context
53              Object o = buildBean(className, null);
54              OgnlUtil.setProperties(params, o);
55  
56              if (o instanceof Interceptor) {
57                  Interceptor interceptor = (Interceptor) o;
58                  interceptor.init();
59                  return interceptor;
60              }
61  
62  // This is for the new API:
63  //            if (o instanceof org.apache.struts2.spi.Interceptor)
64  //                return new InterceptorAdapter((org.apache.struts2.spi.Interceptor) o);
65  
66              throw new ConfigurationException(
67                      "Class [" + className + "] does not implement Interceptor", interceptorConfig);
68          } catch (InstantiationException e) {
69              throw new ConfigurationException(
70                      "Unable to instantiate an instance of Interceptor class [" + className + "].",
71                      e, interceptorConfig);
72          } catch (IllegalAccessException e) {
73              throw new ConfigurationException(
74                      "IllegalAccessException while attempting to instantiate an instance of Interceptor class ["
75                              + className + "].",
76                      e, interceptorConfig);
77          } catch (Exception e) {
78              throw new ConfigurationException(
79                      "Caught Exception while registering Interceptor class " + className,
80                      e, interceptorConfig);
81          } catch (NoClassDefFoundError e) {
82              throw new ConfigurationException(
83                      "Could not load class " + className
84                              + ". Perhaps it exists but certain dependencies are not available?",
85                      e, interceptorConfig);
86          }
87      }
88  
89      public Result buildResult(ResultConfig resultConfig, Map extraContext) throws Exception {
90          String resultClassName = resultConfig.getClassName();
91          if (resultClassName == null)
92              return null;
93  
94          Object result = buildBean(resultClassName, extraContext);
95          OgnlUtil.setProperties(resultConfig.getParams(), result, extraContext);
96  
97          if (result instanceof Result)
98              return (Result) result;
99  
100 // This is for the new API:
101 //        if (result instanceof org.apache.struts2.spi.Result)
102 //            return new ResultAdapter((org.apache.struts2.spi.Result) result);
103 
104         throw new ConfigurationException(result.getClass().getName() + " does not implement Result.");
105     }
106 }