01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18:
19: package org.apache.jmeter.protocol.http.util;
20:
21: import java.util.HashMap;
22: import org.w3c.dom.Document;
23:
24: /**
25: * The purpose of this class is to cache the DOM Documents in memory and by-pass
26: * parsing. For old systems or laptops, it's not practical to parse the XML
27: * documents every time. Therefore using a memory cache can reduce the CPU
28: * usage.
29: * <p>
30: * For now this is a simple version to test the feasibility of caching. If it
31: * works, this class will be replaced with an Apache commons or something
32: * equivalent. If I was familiar with Apache Commons Pool, I would probably use
33: * it, but since I don't know the API, it is quicker for Proof of Concept to
34: * just write a dumb one. If the number documents in the pool exceed several
35: * hundred, it will take a long time for the lookup.
36: * <p>
37: * Created on: Jun 17, 2003<br>
38: *
39: */
40: public final class DOMPool {
41: /**
42: * The cache is created with an initial size of 50. Running a webservice
43: * test on an old system will likely run into memory or CPU problems long
44: * before the HashMap is an issue.
45: */
46: private static final HashMap MEMCACHE = new HashMap(50);
47:
48: /**
49: * Return a document.
50: *
51: * @param key
52: * @return Document
53: */
54: public static synchronized Document getDocument(Object key) {
55: return (Document) MEMCACHE.get(key);
56: }
57:
58: /**
59: * Add an object to the cache.
60: *
61: * @param key
62: * @param data
63: */
64: public static synchronized void putDocument(Object key, Object data) {
65: MEMCACHE.put(key, data);
66: }
67:
68: /**
69: * Private constructor to prevent instantiation.
70: */
71: private DOMPool() {
72: }
73: }
|