source: trunk/autoquest-plugin-http/src/main/java/de/ugoe/cs/autoquest/plugin/http/eventcore/EqualSOAPDataMap.java @ 1986

Last change on this file since 1986 was 1986, checked in by sherbold, 9 years ago
  • EqualSOAPDataMap now serializable
  • Property svn:mime-type set to text/plain
File size: 5.5 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.plugin.http.eventcore;
16
17import java.io.IOException;
18import java.io.ObjectInputStream;
19import java.io.ObjectOutputStream;
20import java.io.Serializable;
21import java.util.Collections;
22import java.util.HashMap;
23import java.util.Map;
24import java.util.Random;
25import java.util.Set;
26
27import org.apache.commons.collections4.set.ListOrderedSet;
28
29/**
30 * <p>
31 * Handles all request bodies of equal SOAP events. Can be used to view either all request bodies sent to an operation or to randomly draw one of the bodies.
32 * </p>
33 *
34 * @author Steffen Herbold
35 */
36public class EqualSOAPDataMap implements Serializable {
37   
38    /**  */
39    private static final long serialVersionUID = 1L;
40
41    /**
42     * Singleton. Handle to only instance of this class
43     */
44    transient private static EqualSOAPDataMap INSTANCE = new EqualSOAPDataMap();
45   
46    /**
47     * Map with all soapRequestBodies for all equal {@link SimpleSOAPEventType}s
48     */
49    private Map<SimpleSOAPEventType, ListOrderedSet<String>> soapRequestBodies = new HashMap<>();
50   
51    /**
52     * random number generator for picking a random request body
53     */
54    transient private final Random random;
55   
56    /**
57     * <p>
58     * return the instance of the class
59     * </p>
60     *
61     * @return
62     */
63    public static EqualSOAPDataMap getInstance() {
64        return INSTANCE;
65    }
66   
67    private EqualSOAPDataMap() {
68        // private constructor to avoid initialization
69        random = new Random();
70    }
71   
72    /**
73     * <p>
74     * Manual serialization of the object. Necessary to guarantee the singleton
75     * property.
76     * </p>
77     *
78     * @param s
79     *            output stream for the serialization
80     * @throws IOException
81     *             thrown if there is problem writing to the output stream
82     */
83    private void writeObject(ObjectOutputStream s) throws IOException {
84            s.defaultWriteObject();
85            s.writeObject(soapRequestBodies);
86    }
87
88    /**
89     * <p>
90     * Manual de-serialization of the object. Necessary to guarantee the
91     * singleton property.
92     *
93     * @param s
94     *            input stream for the de-serialization
95     * @throws IOException
96     *             thrown if there is problem reading from the input stream
97     * @throws ClassNotFoundException
98     *             thrown if there is a problem reading from the input stream
99     */
100    @SuppressWarnings("unchecked")
101    private void readObject(ObjectInputStream s) throws IOException,
102                    ClassNotFoundException {
103            s.defaultReadObject();
104            if (INSTANCE == null) {
105                INSTANCE = new EqualSOAPDataMap();
106            }
107            INSTANCE.soapRequestBodies = (Map<SimpleSOAPEventType, ListOrderedSet<String>>) s.readObject();
108    }
109
110    /**
111     * <p>
112     * Manual de-serialization to guarantee the singleton property.
113     * </p>
114     *
115     * @return instance of the container
116     */
117    private Object readResolve() {
118            return INSTANCE;
119    }
120
121    /**
122     * <p>
123     * Adds a new body to the map.
124     * </p>
125     *
126     * @param simpleSOAPEventType
127     */
128    public void add(SimpleSOAPEventType simpleSOAPEventType, String soapRequestBody) {
129        if( soapRequestBody!=null ) {
130            ListOrderedSet<String> requestBodySet = soapRequestBodies.get(simpleSOAPEventType);
131            if( requestBodySet==null ) {
132                requestBodySet = new ListOrderedSet<>();
133                soapRequestBodies.put(simpleSOAPEventType, requestBodySet);
134            }
135            requestBodySet.add(soapRequestBody);
136        }
137    }
138   
139    /**
140     * <p>
141     * Retrieves all bodies associated with the simpleSoapEventType; null if not found
142     * </p>
143     *
144     * @param simpleSoapEventType
145     * @return
146     */
147    public Set<String> getAll(SimpleSOAPEventType simpleSoapEventType) {
148        return Collections.unmodifiableSet(soapRequestBodies.get(simpleSoapEventType));
149    }
150   
151    /**
152     * <p>
153     * Randomly draws one of the SOAP event type bodies associated with the event
154     * </p>
155     *
156     * @param simpleSOAPEventType
157     * @return
158     */
159    public String getRandom(SimpleSOAPEventType simpleSOAPEventType) {
160        ListOrderedSet<String> requestBodySet = soapRequestBodies.get(simpleSOAPEventType);
161        if( requestBodySet==null || requestBodySet.isEmpty() ) {
162            throw new RuntimeException("no request body known for SimpleSOAPEventType: " + simpleSOAPEventType);
163        }
164       
165        return requestBodySet.get(random.nextInt(requestBodySet.size()));
166    }
167   
168    /**
169     * <p>
170     * resets the internal map by creating new one
171     * </p>
172     *
173     */
174    public void reset() {
175        soapRequestBodies = new HashMap<>();
176    }
177}
Note: See TracBrowser for help on using the repository browser.