source: trunk/java-client/org/nzdl/gsdl/service/NzdlLogWrapper.java@ 2278

Last change on this file since 2278 was 2278, checked in by say1, 23 years ago

changed JAVACOPTIONS. log files now written to home directory. error catching in CSModel. ChangeLog.

  • Property svn:keywords set to Author Date Id Revision
File size: 8.3 KB
Line 
1/*
2 * NzdlWrapper.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
21package org.nzdl.gsdl.service;
22
23import java.util.Map;
24import java.util.Set;
25import java.util.List;
26import java.util.Date;
27import java.io.Writer;
28import java.io.FileWriter;
29
30
31/**
32 * A Wrapper around an NzdlService object that adds logging functionality.
33 *
34 * @author Brett Sheeran ([email protected]) (comments)
35 * @author Stuart Yeates ([email protected])
36 * @version $Revision: 2278 $
37 */
38public class NzdlLogWrapper
39 extends NzdlWrapper {
40
41 Writer logFile = null;
42 String tag = "NzdlLogWrapper: ";
43 boolean calculateDiff = true;
44 boolean logBefore = false;
45 boolean logAfter = false;
46 Date whenCallStarted = null;
47
48
49 /**
50 * The bean constructor ...
51 */
52 public NzdlLogWrapper() {
53 }
54 /**
55 * The normal constructor
56 */
57 public NzdlLogWrapper(NzdlService nzdl, String logFilename,
58 String tag) {
59 super(nzdl);
60 this.tag = tag;
61 try {
62 Date date = new Date();
63 // the true is append ...
64 String dir = System.getProperties().getProperty("user.home");
65 String sep = System.getProperties().getProperty("file.separator");
66 logFile = new FileWriter(dir+sep+logFilename, true);
67 logFile.write(tag + "Log started at:" + date + " == " +
68 date.getTime() + "\n");
69 logFile.write(tag + "timestamps in milliseconds since January 1, 1970, 00:00:00 GMT \n");
70 logFile.flush();
71 } catch (Exception exception) {
72 System.err.println("unable to create log file:" + exception);
73 }
74 }
75
76 protected void logBefore(String method) {
77 if (whenCallStarted != null)
78 throw new Error ("synchronisation required ! (whenCallStarted != null)");
79 whenCallStarted = new Date();
80 if (logBefore)
81 try {
82 logFile.write(this.tag + "before " + method + "() " +
83 whenCallStarted.getTime() + "\n");
84 logFile.flush();
85 } catch (Exception exception) {
86 System.err.println("unable to write log file:" + exception);
87 }
88 }
89
90 protected void logAfter(String method) {
91 if (whenCallStarted == null)
92 throw new Error ("synchronisation required ! (whenCallStarted == null)");
93 Date now = new Date();
94 try {
95 if (logAfter)
96 logFile.write(this.tag + "after " + method + "() " +
97 whenCallStarted.getTime() + "\n");
98 if (calculateDiff)
99 logFile.write(this.tag + "time for " + method + "() " +
100 (now.getTime() -whenCallStarted.getTime()) + "ms\n");
101 logFile.flush();
102 } catch (Exception exception) {
103 System.err.println("unable to write log file:" + exception);
104 }
105 whenCallStarted = null;
106 }
107
108 /**
109 * Allow the client to configure a CORBA server.
110 * @param key the parameter to be set.
111 * @param values what that parameter will be set to.
112 */
113 public void configure( String _key, Set _values ) {
114 logBefore("configure");
115 super.configure(_key, _values);
116 logAfter("configure");
117 }
118
119 /**
120 * Obtain the set of collection names for a Greenstone library.
121 * @return A set of collection names in string format.
122 */
123 public Set getCollectionSet( ) {
124 logBefore("getCollectionSet");
125 Set result = super.getCollectionSet();
126 logAfter("getCollectionSet");
127 return result;
128 }
129
130 /**
131 * Check if ORB knows of this collection.
132 * @param name the name of a collecion to be searched for.
133 * @return boolean <TT>true</TT> if collection is found, else <TT>false</TT>.
134 */
135 public boolean hasCollection( String _name ) {
136 logBefore("hasCollection");
137 boolean result = super.hasCollection(_name);
138 logAfter("hasCollection");
139 return result;
140 }
141
142
143 /**
144 * Check if ORB can communicate with of this collection.
145 * @param name the name of a collecion to be pinged.
146 * @return boolean <TT>true</TT> if collection returned ping, else
147 * <TT>false</TT>.
148 */
149 public boolean pingCollection( String _name ) {
150 logBefore("pingCollection");
151 boolean result = super.pingCollection(_name);
152 logAfter("pingCollection");
153 return result;
154 }
155
156 /**
157 * Extract information on this collection into a
158 * {@link NzdlCollectionInfo NzdlCollectionInfo} object
159 * @param name the collection to be retrieved.
160 * @return an instance of
161 * {@link NzdlCollectionInfo NzdlCollectionInfo} containing data on a
162 * collection
163 */
164 public NzdlCollectionInfo getCollectionInfo( String _name ) {
165 logBefore(" getCollectionInfo");
166 NzdlCollectionInfo result = super.getCollectionInfo(_name);
167 logAfter(" getCollectionInfo");
168 return result;
169 }
170
171 /**
172 * Returns the set of filters for the service object.
173 * @param name the collection name
174 * @return a set of strings that name the filters, for example
175 * BrowseFilter, QueryFilter and NullFilter.
176 */
177 public Set getFilterSet( String _name ) {
178 logBefore("getFilterSet");
179 Set result = super.getFilterSet(_name);
180 logAfter("getFilterSet");
181 return result;
182 }
183
184 /**
185 * @param name the collection name
186 * @return the Set of (name, value) value pairs for all metadata
187 * associated with a collection
188 */
189 public Set getMetaTagSet( String _name ) {
190 logBefore("getMetaTagSet");
191 Set result = super.getMetaTagSet(_name);
192 logAfter("getMetaTagSet");
193 return result;
194 }
195
196 /**
197 * Returns the Greenstone Markup Language (GML) text for a particular
198 * document from a collection.
199 * @param name the name of the collection.
200 * @param docID the document identity string
201 * @return the GML document in the form of a string.
202 */
203 public String getDocument( String _name, String _docID ) {
204 logBefore("getDocument");
205 String result = super.getDocument(_name, _docID);
206 logAfter("getDocument");
207 return result;
208 }
209
210 /**
211 * Returns the set of value(s) for a metatag from a particular document.
212 * @param name collection name
213 * @param docID the document identifier.
214 * @param metaTag the metatag name such as: Title, Author, Date and Images.
215 * @return The set of value(s) for the requested metatag and document. In
216 * cases such as Title, Author, Date there will probably be only one value
217 * per document.
218 */
219 public Set getMetaData( String _name, String _docID, String _metaTag ) {
220 logBefore("getMetaData");
221 Set result = super.getMetaData( _name, _docID, _metaTag );
222 logAfter("getMetaData");
223 return result;
224 }
225
226 /**
227 * Returns a map of value(s) for a metatag from a list of documents.
228 * @param name collection name
229 * @param docIDs a list of document identifier strings.
230 * @param metaTag the metatag name such as: Title, Author, Date and Images.
231 * @return The map of value(s) for the requested metatag and document. In
232 * cases such as Title, Author, Date there will probably be only one value
233 * per document.
234 */
235 public Map getMetaData( String _name, List _docIDs, String _metaTag ) {
236 logBefore("getMetaData");
237 Map result = super.getMetaData( _name, _docIDs, _metaTag );
238 logAfter("getMetaData");
239 return result;
240 }
241
242 /**
243 * Services a {@link NzdlRequest NzdlRequest} to a collection and places
244 * the results into a {@link NzdlResponse NzdlResponse} object. <br>
245 * Pre: Created instances of NzdlRequest and NzdlResponse. <br>
246 * Post: Placed the result data into NzdlResponse object.
247 * @param name the collection name.
248 * @param request an instance of NzdlRequest with query data to be serviced
249 * @param response an instance of NzdlResponse. where the results of
250 * sevciving the query will be placed
251 */
252 public void service( String _name, NzdlRequest _request,
253 NzdlResponse _response ) {
254 logBefore("service");
255 super.service(_name,_request,_response);
256 logAfter("service");
257 }
258
259}
Note: See TracBrowser for help on using the repository browser.