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.test.mock;
016    
017    import java.io.ByteArrayInputStream;
018    import java.io.ByteArrayOutputStream;
019    import java.io.IOException;
020    import java.io.ObjectInputStream;
021    import java.io.ObjectOutputStream;
022    import java.util.Collections;
023    import java.util.Enumeration;
024    import java.util.HashMap;
025    import java.util.Map;
026    import java.util.Set;
027    
028    import org.apache.hivemind.ApplicationRuntimeException;
029    
030    /**
031     *  
032     * Base class for holders of named attributes such as
033     * {@link javax.servlet.http.HttpSession}, 
034     * {@link javax.servlet.http.HttpServletRequest}
035     * and {@link javax.servlet.ServletContext}.
036     *
037     *
038     * @author Howard Lewis Ship
039     * @since 4.0
040     */
041    
042    public class AttributeHolder
043    {
044        private Map _attributes = new HashMap();
045    
046        public Object getAttribute(String name)
047        {
048            return _attributes.get(name);
049        }
050    
051        public Enumeration getAttributeNames()
052        {
053            return getEnumeration(_attributes);
054        }
055    
056        protected Enumeration getEnumeration(Map map)
057        {
058            return Collections.enumeration(map.keySet());
059        }
060    
061        public String[] getAttributeNamesArray()
062        {
063            Set keys = _attributes.keySet();
064            int count = keys.size();
065    
066            String[] array = new String[count];
067    
068            return (String[]) keys.toArray(array);
069        }
070    
071        public void setAttribute(String name, Object value)
072        {
073            _attributes.put(name, value);
074        }
075    
076        public void removeAttribute(String name)
077        {
078            _attributes.remove(name);
079        }
080    
081        /**
082         *  Serializes and then deserializes the {@link Map}
083         *  containing all attributes.
084         * 
085         **/
086    
087        public void simulateFailover()
088        {
089            byte[] serialized = serializeAttributes();
090    
091            _attributes = null;
092    
093            Map restoredAttributes = deserializeAttributes(serialized);
094    
095            _attributes = restoredAttributes;
096        }
097    
098        private byte[] serializeAttributes()
099        {
100            try
101            {
102                ByteArrayOutputStream bos = new ByteArrayOutputStream();
103                ObjectOutputStream oos = new ObjectOutputStream(bos);
104    
105                oos.writeObject(_attributes);
106    
107                oos.close();
108    
109                return bos.toByteArray();
110            }
111            catch (IOException ex)
112            {
113                throw new ApplicationRuntimeException("Unable to serialize attributes.", ex);
114            }
115        }
116    
117        private Map deserializeAttributes(byte[] serialized)
118        {
119            try
120            {
121                ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
122                ObjectInputStream ois = new ObjectInputStream(bis);
123    
124                Map result = (Map) ois.readObject();
125    
126                return result;
127            }
128            catch (Exception ex)
129            {
130                throw new ApplicationRuntimeException("Unable to deserialize attributes.", ex);
131            }
132        }
133    }