001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.engine;
016    
017    import java.util.Map;
018    
019    import org.apache.hivemind.util.Defense;
020    import org.apache.tapestry.util.QueryParameterMap;
021    
022    /**
023     * Implementation of {@link org.apache.tapestry.engine.ServiceEncoding}, which adds the ability to
024     * determine when the encoding has been modified.
025     * 
026     * @author Howard M. Lewis Ship
027     * @since 4.0
028     */
029    public class ServiceEncodingImpl implements ServiceEncoding
030    {
031        private String _servletPath;
032    
033        /**
034         * Map of query parameter values; key is string name, value is either a string, an array of
035         * strings, or null. Could have done this with subclassing rather than delegation.
036         */
037    
038        private final QueryParameterMap _parameters;
039    
040        private boolean _modified;
041    
042        public boolean isModified()
043        {
044            return _modified;
045        }
046    
047        public void resetModified()
048        {
049            _modified = false;
050        }
051    
052        /**
053         * Creates a new instance with a new map of parameters.
054         */
055    
056        public ServiceEncodingImpl(String servletPath)
057        {
058            this(servletPath, new QueryParameterMap());
059        }
060    
061        public ServiceEncodingImpl(String servletPath, Map parametersMap)
062        {
063            this(servletPath, new QueryParameterMap(parametersMap));
064        }
065    
066        public ServiceEncodingImpl(String servletPath, QueryParameterMap parameters)
067        {
068            Defense.notNull(servletPath, "servletPath");
069            Defense.notNull(parameters, "parameters");
070    
071            _servletPath = servletPath;
072    
073            _parameters = parameters;
074        }
075    
076        public String getParameterValue(String name)
077        {
078            return _parameters.getParameterValue(name);
079        }
080    
081        public String[] getParameterValues(String name)
082        {
083            return _parameters.getParameterValues(name);
084        }
085    
086        public void setServletPath(String servletPath)
087        {
088            Defense.notNull(servletPath, "servletPath");
089    
090            _servletPath = servletPath;
091            _modified = true;
092        }
093    
094        public void setParameterValue(String name, String value)
095        {
096            _parameters.setParameterValue(name, value);
097    
098            _modified = true;
099        }
100    
101        public void setParameterValues(String name, String[] values)
102        {
103            _parameters.setParameterValues(name, values);
104    
105            _modified = true;
106        }
107    
108        public String getServletPath()
109        {
110            return _servletPath;
111        }
112    
113        public String[] getParameterNames()
114        {
115            return _parameters.getParameterNames();
116        }
117    
118    }