001: /*
002: * $Id: StrutsObjectFactory.java 506198 2007-02-12 00:57:44Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: // Copyright 2006 Google Inc. All Rights Reserved.
022: package org.apache.struts2.impl;
023:
024: import com.opensymphony.xwork2.ObjectFactory;
025: import com.opensymphony.xwork2.Result;
026: import com.opensymphony.xwork2.config.ConfigurationException;
027: import com.opensymphony.xwork2.config.entities.InterceptorConfig;
028: import com.opensymphony.xwork2.config.entities.ResultConfig;
029: import com.opensymphony.xwork2.interceptor.Interceptor;
030: import com.opensymphony.xwork2.util.OgnlUtil;
031:
032: import java.util.HashMap;
033: import java.util.Map;
034:
035: public class StrutsObjectFactory extends ObjectFactory {
036:
037: public Interceptor buildInterceptor(
038: InterceptorConfig interceptorConfig, Map refParams)
039: throws ConfigurationException {
040: String className = interceptorConfig.getClassName();
041:
042: Map<String, String> params = new HashMap<String, String>();
043: Map typeParams = interceptorConfig.getParams();
044: if (typeParams != null && !typeParams.isEmpty())
045: params.putAll(typeParams);
046: if (refParams != null && !refParams.isEmpty())
047: params.putAll(refParams);
048: params.putAll(refParams);
049:
050: try {
051: // interceptor instances are long-lived and used across user sessions, so don't try to pass in any extra
052: // context
053: Object o = buildBean(className, null);
054: OgnlUtil.setProperties(params, o);
055:
056: if (o instanceof Interceptor) {
057: Interceptor interceptor = (Interceptor) o;
058: interceptor.init();
059: return interceptor;
060: }
061:
062: // This is for the new API:
063: // if (o instanceof org.apache.struts2.spi.Interceptor)
064: // return new InterceptorAdapter((org.apache.struts2.spi.Interceptor) o);
065:
066: throw new ConfigurationException("Class [" + className
067: + "] does not implement Interceptor",
068: interceptorConfig);
069: } catch (InstantiationException e) {
070: throw new ConfigurationException(
071: "Unable to instantiate an instance of Interceptor class ["
072: + className + "].", e, interceptorConfig);
073: } catch (IllegalAccessException e) {
074: throw new ConfigurationException(
075: "IllegalAccessException while attempting to instantiate an instance of Interceptor class ["
076: + className + "].", e, interceptorConfig);
077: } catch (Exception e) {
078: throw new ConfigurationException(
079: "Caught Exception while registering Interceptor class "
080: + className, e, interceptorConfig);
081: } catch (NoClassDefFoundError e) {
082: throw new ConfigurationException(
083: "Could not load class "
084: + className
085: + ". Perhaps it exists but certain dependencies are not available?",
086: e, interceptorConfig);
087: }
088: }
089:
090: public Result buildResult(ResultConfig resultConfig,
091: Map extraContext) throws Exception {
092: String resultClassName = resultConfig.getClassName();
093: if (resultClassName == null)
094: return null;
095:
096: Object result = buildBean(resultClassName, extraContext);
097: OgnlUtil.setProperties(resultConfig.getParams(), result,
098: extraContext);
099:
100: if (result instanceof Result)
101: return (Result) result;
102:
103: // This is for the new API:
104: // if (result instanceof org.apache.struts2.spi.Result)
105: // return new ResultAdapter((org.apache.struts2.spi.Result) result);
106:
107: throw new ConfigurationException(result.getClass().getName()
108: + " does not implement Result.");
109: }
110: }
|