1 | /*
|
---|
2 | * NzdlCacheWrapper.java
|
---|
3 | * Copyright (C) 2001 New Zealand Digital Library Project
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 |
|
---|
20 | //the package we're in
|
---|
21 | package org.nzdl.gsdl.service;
|
---|
22 |
|
---|
23 | import java.util.Hashtable;
|
---|
24 |
|
---|
25 | /* gsdl corba stuff */
|
---|
26 | import org.nzdl.gsdl.corba.gsdlInterface.corbaFilterRequest;
|
---|
27 | import org.nzdl.gsdl.util.NzdlCorbaFactory;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * A Wrapper around an NzdlService object that adds functionality.
|
---|
31 | *
|
---|
32 | * Typically one or more of the methods will be overridden to
|
---|
33 | * add that functionality
|
---|
34 | *
|
---|
35 | * @author Brett Sheeran ([email protected]) (comments)
|
---|
36 | * @author Stuart Yeates ([email protected])
|
---|
37 | * @version $Revision: 2158 $
|
---|
38 | */
|
---|
39 | public class NzdlCacheWrapper
|
---|
40 | extends NzdlWrapper {
|
---|
41 |
|
---|
42 | private Hashtable _docs;
|
---|
43 | private Hashtable _collectionInfo;
|
---|
44 | private Hashtable _services;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * The bean constructor ...
|
---|
48 | */
|
---|
49 | public NzdlCacheWrapper() {
|
---|
50 | _docs = new Hashtable();
|
---|
51 | _collectionInfo = new Hashtable();
|
---|
52 | _services = new Hashtable();
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * The normal constructor ...
|
---|
57 | */
|
---|
58 | public NzdlCacheWrapper(NzdlService service) {
|
---|
59 | super(service);
|
---|
60 | _docs = new Hashtable();
|
---|
61 | _collectionInfo = new Hashtable();
|
---|
62 | _services = new Hashtable();
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Wraps a getCollectionInfo with a cache check
|
---|
67 | * to see if we've made the same call before ...
|
---|
68 | * @see NzdlService#getCollectionInfo
|
---|
69 | */
|
---|
70 | public NzdlCollectionInfo getCollectionInfo( String _name ) {
|
---|
71 | //System.err.println("in NzdlServiceCachingClient::getCollectionInfo()");
|
---|
72 | NzdlCollectionInfo info = null;
|
---|
73 | info = (NzdlCollectionInfo)_collectionInfo.get(_name);
|
---|
74 | if (info != null)
|
---|
75 | return info;
|
---|
76 | info = super.getCollectionInfo(_name);
|
---|
77 | _collectionInfo.put(_name,info);
|
---|
78 |
|
---|
79 | //System.err.println("Cache miss (getCollectionInfo)");
|
---|
80 | return info;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Wraps a getDocument with a cache check
|
---|
85 | * to see if we've made the same call before ...
|
---|
86 | * @see NzdlService#getDocument
|
---|
87 | */
|
---|
88 | public String getDocument( String _name, String _docID ) {
|
---|
89 | String doc = null;
|
---|
90 | String key = "" + _name + _docID;
|
---|
91 | doc = (String)_docs.get(key);
|
---|
92 | if (doc != null)
|
---|
93 | return doc;
|
---|
94 | doc = super.getDocument(_name,_docID);
|
---|
95 | _docs.put(key, doc);
|
---|
96 |
|
---|
97 | //System.err.println("Cache miss (getDocument) " + _name
|
---|
98 | // + " " +_docID);
|
---|
99 |
|
---|
100 | return doc;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Wraps a service with a cache check
|
---|
105 | * to see if we've made the same call before ...
|
---|
106 | * @see NzdlService#service
|
---|
107 | */
|
---|
108 | public void service( String _name, NzdlRequest _request, NzdlResponse _response ) {
|
---|
109 |
|
---|
110 | String key = _name + corbaFilterRequestToString(_request);
|
---|
111 |
|
---|
112 | if (_services.get(key) != null) {
|
---|
113 | _response.copyData((NzdlResponse) _services.get(key));
|
---|
114 | return;
|
---|
115 | }
|
---|
116 |
|
---|
117 | super.service(_name, _request,_response);
|
---|
118 |
|
---|
119 | _services.put(key, _response);
|
---|
120 |
|
---|
121 | //System.err.println("Cache miss (service)" + key);
|
---|
122 | return;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Stringifies a NzdlRequest object so we can use it as a hash value
|
---|
127 | * @see NzdlRequest
|
---|
128 | */
|
---|
129 | protected String corbaFilterRequestToString(NzdlRequest nzdlrequest)
|
---|
130 | {
|
---|
131 | corbaFilterRequest request = nzdlrequest.getFilter();
|
---|
132 | StringBuffer buf = new StringBuffer(1000);
|
---|
133 |
|
---|
134 | String tmp = NzdlCorbaFactory.toString(request.filter);
|
---|
135 | buf.append(tmp).append('\uFFFF');
|
---|
136 |
|
---|
137 | for (int i = 0; i <request.filterOptions.length;i++){
|
---|
138 | tmp = NzdlCorbaFactory.toString(request.filterOptions[i].name);
|
---|
139 | buf.append(tmp).append('\uFFFF');
|
---|
140 | tmp = NzdlCorbaFactory.toString(request.filterOptions[i].value);
|
---|
141 | buf.append(tmp).append('\uFFFF');
|
---|
142 | }
|
---|
143 |
|
---|
144 | for (int i = 0; i <request.docSet.length;i++){
|
---|
145 | tmp = NzdlCorbaFactory.toString(request.docSet[i]);
|
---|
146 | buf.append(tmp).append('\uFFFF');
|
---|
147 | }
|
---|
148 |
|
---|
149 | buf.append(request.filterResultOptions).append('\uFFFF');
|
---|
150 |
|
---|
151 | tmp = NzdlCorbaFactory.toString(request.requestParams);
|
---|
152 | buf.append(tmp).append('\uFFFF');
|
---|
153 |
|
---|
154 | tmp = NzdlCorbaFactory.toString(request.refParams);
|
---|
155 | buf.append(tmp).append('\uFFFF');
|
---|
156 |
|
---|
157 | for (int i = 0; i <request.fields.length;i++){
|
---|
158 | tmp = NzdlCorbaFactory.toString(request.fields[i]);
|
---|
159 | buf.append(tmp).append('\uFFFF');
|
---|
160 | }
|
---|
161 |
|
---|
162 | buf.append(request.getParents).append('\uFFFF');
|
---|
163 |
|
---|
164 | return buf.toString();
|
---|
165 | }
|
---|
166 |
|
---|
167 | }
|
---|