|
Struts example source code file (CompoundRootAccessor.java)
The Struts CompoundRootAccessor.java source code/* * Copyright 2002-2006,2009 The Apache Software Foundation. * * 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 com.opensymphony.xwork2.ognl.accessor; import com.opensymphony.xwork2.XWorkException; import com.opensymphony.xwork2.ognl.OgnlValueStack; import com.opensymphony.xwork2.inject.Inject; import com.opensymphony.xwork2.util.CompoundRoot; import com.opensymphony.xwork2.util.ValueStack; import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; import ognl.*; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.util.*; /** * A stack that is able to call methods on objects in the stack. * * @author $Author: martinc $ * @author Rainer Hermanns * @version $Revision: 894090 $ */ public class CompoundRootAccessor implements PropertyAccessor, MethodAccessor, ClassResolver { /** * Used by OGNl to generate bytecode */ public String getSourceAccessor(OgnlContext context, Object target, Object index) { return null; } /** * Used by OGNl to generate bytecode */ public String getSourceSetter(OgnlContext context, Object target, Object index) { return null; } private final static Logger LOG = LoggerFactory.getLogger(CompoundRootAccessor.class); private final static Class[] EMPTY_CLASS_ARRAY = new Class[0]; private static Map invalidMethods = new HashMap(); static boolean devMode = false; @Inject("devMode") public static void setDevMode(String mode) { devMode = "true".equals(mode); } public void setProperty(Map context, Object target, Object name, Object value) throws OgnlException { CompoundRoot root = (CompoundRoot) target; OgnlContext ognlContext = (OgnlContext) context; for (Object o : root) { if (o == null) { continue; } try { if (OgnlRuntime.hasSetProperty(ognlContext, o, name)) { OgnlRuntime.setProperty(ognlContext, o, name, value); return; } else if (o instanceof Map) { Map<Object, Object> map = (Map) o; try { map.put(name, value); return; } catch (UnsupportedOperationException e) { // This is an unmodifiable Map, so move on to the next element in the stack } } // } catch (OgnlException e) { // if (e.getReason() != null) { // final String msg = "Caught an Ognl exception while setting property " + name; // log.error(msg, e); // throw new RuntimeException(msg, e.getReason()); // } } catch (IntrospectionException e) { // this is OK if this happens, we'll just keep trying the next } } Boolean reportError = (Boolean) context.get(ValueStack.REPORT_ERRORS_ON_NO_PROP); final String msg = "No object in the CompoundRoot has a publicly accessible property named '" + name + "' (no setter could be found)."; if ((reportError != null) && (reportError.booleanValue())) { throw new XWorkException(msg); } else { if (devMode) { LOG.warn(msg); } } } public Object getProperty(Map context, Object target, Object name) throws OgnlException { CompoundRoot root = (CompoundRoot) target; OgnlContext ognlContext = (OgnlContext) context; if (name instanceof Integer) { Integer index = (Integer) name; return root.cutStack(index.intValue()); } else if (name instanceof String) { if ("top".equals(name)) { if (root.size() > 0) { return root.get(0); } else { return null; } } for (Object o : root) { if (o == null) { continue; } try { if ((OgnlRuntime.hasGetProperty(ognlContext, o, name)) || ((o instanceof Map) && ((Map) o).containsKey(name))) { return OgnlRuntime.getProperty(ognlContext, o, name); } } catch (OgnlException e) { if (e.getReason() != null) { final String msg = "Caught an Ognl exception while getting property " + name; throw new XWorkException(msg, e); } } catch (IntrospectionException e) { // this is OK if this happens, we'll just keep trying the next } } //property was not found if (context.containsKey(OgnlValueStack.THROW_EXCEPTION_ON_FAILURE)) throw new NoSuchPropertyException(target, name); else return null; } else { return null; } } public Object callMethod(Map context, Object target, String name, Object[] objects) throws MethodFailedException { CompoundRoot root = (CompoundRoot) target; if ("describe".equals(name)) { Object v; if (objects != null && objects.length == 1) { v = objects[0]; } else { v = root.get(0); } if (v instanceof Collection || v instanceof Map || v.getClass().isArray()) { return v.toString(); } try { Map<String, PropertyDescriptor> descriptors = OgnlRuntime.getPropertyDescriptors(v.getClass()); int maxSize = 0; for (String pdName : descriptors.keySet()) { if (pdName.length() > maxSize) { maxSize = pdName.length(); } } SortedSet<String> set = new TreeSet Other Struts examples (source code examples)Here is a short list of links related to this Struts CompoundRootAccessor.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.