KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > storagemanager > logging > LoggingStorageManager


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.storagemanager.logging;
13
14 import com.versant.core.server.CompiledQuery;
15 import com.versant.core.metadata.*;
16 import com.versant.core.common.*;
17 import com.versant.core.jdo.QueryDetails;
18 import com.versant.core.jdo.VersantQueryPlan;
19 import com.versant.core.storagemanager.StorageManager;
20 import com.versant.core.storagemanager.ApplicationContext;
21 import com.versant.core.storagemanager.ExecuteQueryReturn;
22 import com.versant.core.storagemanager.RunningQuery;
23 import com.versant.core.logging.LogEventStore;
24 import com.versant.core.util.IntArray;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 /**
31  * Decorates another storage manager to add event logging.
32  */

33 public final class LoggingStorageManager implements StorageManager {
34
35     private final LoggingStorageManagerFactory smf;
36     private final StorageManager sm;
37     private final LogEventStore pes;
38     private final ModelMetaData jmd;
39     private final int id;
40
41     private static int lastId;
42
43     public LoggingStorageManager(LoggingStorageManagerFactory smf,
44             StorageManager sm) {
45         this.smf = smf;
46         this.sm = sm;
47         this.pes = smf.getLogEventStore();
48         this.jmd = smf.getModelMetaData();
49         id = ++lastId;
50     }
51
52     public StorageManager getStorageManager() {
53         return sm;
54     }
55
56     public LogEventStore getPerfEventStore() {
57         return pes;
58     }
59
60     private RuntimeException JavaDoc handleException(Throwable JavaDoc cause,
61             StorageManagerEvent ev) {
62         BindingSupportImpl
63          bsi
64             = BindingSupportImpl.getInstance();
65         if (ev != null) {
66             ev.setErrorMsg(cause);
67         }
68         if (bsi.isOwnException(cause)) {
69             return (RuntimeException JavaDoc)cause;
70         } else {
71             if (Debug.DEBUG) {
72                 cause.printStackTrace(System.out);
73             }
74             if (bsi.isError(cause)) {
75                 if (bsi.isOutOfMemoryError(cause)) {
76                     return bsi.exception(cause.toString(), cause);
77                 }
78                 throw (Error JavaDoc)cause;
79             }
80             return bsi.internal(cause.toString(), cause);
81         }
82     }
83
84     public void begin(boolean optimistic) {
85         StorageManagerEvent ev = null;
86         if (pes.isFine()) {
87             pes.log(ev = new SmTxEvent(id, SmTxEvent.BEGIN, optimistic));
88         }
89         try {
90             smf.txCount++;
91             sm.begin(optimistic);
92         } catch (Throwable JavaDoc e) {
93             throw handleException(e, ev);
94         } finally {
95             if (ev != null) ev.updateTotalMs();
96         }
97     }
98
99     public void commit() {
100         StorageManagerEvent ev = null;
101         if (pes.isFine()) {
102             pes.log(ev = new SmTxEvent(id, SmTxEvent.COMMIT));
103         }
104         try {
105             smf.txCommitCount++;
106             sm.commit();
107         } catch (Throwable JavaDoc e) {
108             smf.txCommitErrorCount++;
109             throw handleException(e, ev);
110         } finally {
111             if (ev != null) ev.updateTotalMs();
112         }
113     }
114
115     public void rollback() {
116         StorageManagerEvent ev = null;
117         if (pes.isFine()) {
118             pes.log(ev = new SmTxEvent(id, SmTxEvent.ROLLBACK));
119         }
120         try {
121             smf.txRollbackCount++;
122             sm.rollback();
123         } catch (Throwable JavaDoc e) {
124             throw handleException(e, ev);
125         } finally {
126             if (ev != null) ev.updateTotalMs();
127         }
128     }
129
130     public void setConnectionPolicy(int policy) {
131         sm.setConnectionPolicy(policy);
132     }
133
134     public void setLockingPolicy(int policy) {
135         sm.setLockingPolicy(policy);
136     }
137
138     public int getLockingPolicy() {
139         return sm.getLockingPolicy();
140     }
141
142     public void logEvent(int level, String JavaDoc description, int ms) {
143         sm.logEvent(level, description, ms);
144     }
145
146     public StatesReturned fetch(ApplicationContext context, OID oid,
147             State current, FetchGroup fetchGroup, FieldMetaData triggerField) {
148         SmStatesReturnedEvent ev = null;
149         if (pes.isFine()) {
150             ev = createAndLogSmFetchEvent(oid, fetchGroup, triggerField);
151         }
152         try {
153             smf.fetchCount++;
154             StatesReturned ans = sm.fetch(context, oid, current, fetchGroup,
155                     triggerField);
156             if (ev != null) fillEventForPacket(ev, ans);
157             return ans;
158         } catch (Throwable JavaDoc e) {
159             smf.fetchErrorCount++;
160             throw handleException(e, ev);
161         } finally {
162             if (ev != null) ev.updateTotalMs();
163         }
164     }
165
166     public StatesReturned fetch(ApplicationContext context, OIDArray oids,
167             FieldMetaData triggerField) {
168         SmStatesReturnedEvent ev = null;
169         if (pes.isFine()) {
170             ev = createAndLogSmFetchBulkEvent(oids, triggerField);
171         }
172         try {
173             smf.fetchCount++;
174             StatesReturned ans = sm.fetch(context, oids, triggerField);
175             if (ev != null) fillEventForPacket(ev, ans);
176             return ans;
177         } catch (Throwable JavaDoc e) {
178             smf.fetchErrorCount++;
179             throw handleException(e, ev);
180         } finally {
181             if (ev != null) ev.updateTotalMs();
182         }
183     }
184
185     public StatesReturned store(StatesToStore toStore, DeletePacket toDelete,
186             boolean returnFieldsUpdatedBySM, int storeOption,
187             boolean evictClasses) {
188         SmStoreEvent ev = null;
189         if (pes.isFine()) {
190             ev = createAndLogSmStoreEvent(toStore, toDelete,
191                     returnFieldsUpdatedBySM, storeOption, evictClasses);
192         }
193         try {
194             switch (storeOption) {
195                 case STORE_OPTION_COMMIT:
196                     smf.txCommitCount++;
197                     break;
198                 case STORE_OPTION_FLUSH:
199                     smf.txFlushCount++;
200                     break;
201             }
202             StatesReturned ans = sm.store(toStore, toDelete,
203                     returnFieldsUpdatedBySM, storeOption, evictClasses);
204             if (ev != null) fillEventForPacket(ev, ans);
205             return ans;
206         } catch (Throwable JavaDoc e) {
207             switch (storeOption) {
208                 case STORE_OPTION_COMMIT:
209                     smf.txCommitErrorCount++;
210                     break;
211                 case STORE_OPTION_FLUSH:
212                     smf.txFlushErrorCount++;
213                     break;
214             }
215             throw handleException(e, ev);
216         } finally {
217             if (ev != null) ev.updateTotalMs();
218         }
219     }
220
221     public OID createOID(ClassMetaData cmd) {
222         return sm.createOID(cmd);
223     }
224
225     public CompiledQuery compileQuery(QueryDetails query) {
226         SmQueryEvent ev = null;
227         if (pes.isFiner()) {
228             pes.log(ev = new SmQueryEvent(id, StorageManagerEvent.COMPILE,
229                     query, null, jmd));
230         }
231         try {
232             return sm.compileQuery(query);
233         } catch (Throwable JavaDoc e) {
234             throw handleException(e, ev);
235         } finally {
236             if (ev != null) ev.updateTotalMs();
237         }
238     }
239
240     public ExecuteQueryReturn executeQuery(ApplicationContext context,
241             QueryDetails query, CompiledQuery compiledQuery, Object JavaDoc[] params) {
242         SmQueryEvent ev = null;
243         if (pes.isFine()) {
244             pes.log(ev = new SmQueryEvent(id, StorageManagerEvent.EXEC,
245                     query, compiledQuery, jmd));
246         }
247         try {
248             smf.queryCount++;
249             return sm.executeQuery(context, query, compiledQuery, params);
250         } catch (Throwable JavaDoc e) {
251             smf.queryErrorCount++;
252             throw handleException(e, ev);
253         } finally {
254             if (ev != null) ev.updateTotalMs();
255         }
256     }
257
258     public QueryResultContainer executeQueryAll(ApplicationContext context,
259             QueryDetails query, CompiledQuery compiledQuery, Object JavaDoc[] params) {
260         SmQueryEvent ev = null;
261         if (pes.isFine()) {
262             pes.log(ev = new SmQueryEvent(id, StorageManagerEvent.EXEC_ALL,
263                     query, compiledQuery, jmd));
264         }
265         try {
266             smf.queryCount++;
267             QueryResultContainer ans = sm.executeQueryAll(context, query,
268                     compiledQuery, params);
269             if (ev != null) {
270                 fillEventForPacket(ev, ans.container);
271             }
272             return ans;
273         } catch (Throwable JavaDoc e) {
274             smf.queryErrorCount++;
275             throw handleException(e, ev);
276         } finally {
277             if (ev != null) ev.updateTotalMs();
278         }
279     }
280
281     public int executeQueryCount(QueryDetails query, CompiledQuery compiledQuery,
282             Object JavaDoc[] params) {
283         SmQueryEvent ev = null;
284         if (pes.isFine()) {
285             pes.log(ev = new SmQueryEvent(id, StorageManagerEvent.EXEC_COUNT,
286                     query, compiledQuery, jmd));
287         }
288         try {
289             smf.queryCount++;
290             int ans = sm.executeQueryCount(query, compiledQuery, params);
291             if (ev != null) {
292                 ev.setCount(ans);
293             }
294             return ans;
295         } catch (Throwable JavaDoc e) {
296             smf.queryErrorCount++;
297             throw handleException(e, ev);
298         } finally {
299             if (ev != null) ev.updateTotalMs();
300         }
301     }
302
303     public VersantQueryPlan getQueryPlan(QueryDetails query,
304             CompiledQuery compiledQuery, Object JavaDoc[] params) {
305         return sm.getQueryPlan(query, compiledQuery, params);
306     }
307
308     public QueryResultContainer fetchNextQueryResult(ApplicationContext context,
309             RunningQuery runningQuery, int skipAmount) {
310         SmQueryEvent ev = null;
311         if (pes.isFine()) {
312             ev = new SmQueryEvent(id, StorageManagerEvent.FETCH_BATCH,
313                     runningQuery.getQueryDetails(), null, jmd);
314             ev.setSkipAmount(skipAmount);
315             pes.log(ev);
316         }
317         try {
318             smf.fetchCount++;
319             QueryResultContainer ans = sm.fetchNextQueryResult(context,
320                     runningQuery, skipAmount);
321             if (ev != null) {
322                 fillEventForPacket(ev, ans.container);
323             }
324             return ans;
325         } catch (Throwable JavaDoc e) {
326             smf.fetchErrorCount++;
327             throw handleException(e, ev);
328         } finally {
329             if (ev != null) ev.updateTotalMs();
330         }
331     }
332
333     public QueryResultContainer fetchRandomAccessQueryResult(
334             ApplicationContext context, RunningQuery runningQuery, int index,
335             int fetchAmount) {
336         SmQueryEvent ev = null;
337         if (pes.isFine()) {
338             ev = new SmQueryEvent(id, StorageManagerEvent.FETCH_INDEX,
339                     runningQuery.getQueryDetails(), null, jmd);
340             ev.setIndex(index);
341             ev.setFetchAmount(fetchAmount);
342             pes.log(ev);
343         }
344         try {
345             smf.fetchCount++;
346             QueryResultContainer ans = sm.fetchRandomAccessQueryResult(context,
347                     runningQuery, index, fetchAmount);
348             if (ev != null) {
349                 fillEventForPacket(ev, ans.container);
350             }
351             return ans;
352         } catch (Throwable JavaDoc e) {
353             smf.fetchErrorCount++;
354             throw handleException(e, ev);
355         } finally {
356             if (ev != null) ev.updateTotalMs();
357         }
358     }
359
360     public int getRandomAccessQueryCount(ApplicationContext context,
361             RunningQuery runningQuery) {
362         SmQueryEvent ev = null;
363         if (pes.isFine()) {
364             ev = new SmQueryEvent(id, StorageManagerEvent.FETCH_COUNT,
365                     runningQuery.getQueryDetails(), null, jmd);
366             pes.log(ev);
367         }
368         try {
369             int ans = sm.getRandomAccessQueryCount(context, runningQuery);
370             if (ev != null) {
371                 ev.setCount(ans);
372             }
373             return ans;
374         } catch (Throwable JavaDoc e) {
375             throw handleException(e, ev);
376         } finally {
377             if (ev != null) ev.updateTotalMs();
378         }
379     }
380
381     public void closeQuery(RunningQuery runningQuery) {
382         SmQueryEvent ev = null;
383         if (pes.isFiner()) {
384             ev = new SmQueryEvent(id, StorageManagerEvent.QUERY_CLOSE,
385                     runningQuery.getQueryDetails(), null, jmd);
386             pes.log(ev);
387         }
388         try {
389             sm.closeQuery(runningQuery);
390         } catch (Throwable JavaDoc e) {
391             throw handleException(e, ev);
392         } finally {
393             if (ev != null) ev.updateTotalMs();
394         }
395     }
396
397     public Object JavaDoc getDatastoreConnection() {
398         return sm.getDatastoreConnection();
399     }
400
401     public boolean isNotifyDirty() {
402         return sm.isNotifyDirty();
403     }
404
405     public void notifyDirty(OID oid) {
406         sm.notifyDirty(oid);
407     }
408
409     public void reset() {
410         sm.reset();
411     }
412
413     public void destroy() {
414         sm.destroy();
415     }
416
417     public StorageManager getInnerStorageManager() {
418         return sm;
419     }
420
421     public boolean hasDatastoreConnection() {
422         return sm.hasDatastoreConnection();
423     }
424
425     public Map JavaDoc getStatus() {
426         return sm.getStatus();
427     }
428
429     public void setUserObject(Object JavaDoc o) {
430         sm.setUserObject(o);
431     }
432
433     private SmFetchEvent createAndLogSmFetchEvent(OID oid,
434             FetchGroup fetchGroup, FieldMetaData triggerField) {
435         ClassMetaData cmd = oid.getAvailableClassMetaData();
436         String JavaDoc fieldName;
437         if (triggerField == null) {
438             fieldName = null;
439         } else {
440             if (triggerField.classMetaData != cmd) {
441                 fieldName = triggerField.getQName();
442             } else {
443                 fieldName = triggerField.name;
444             }
445         }
446         SmFetchEvent ev = new SmFetchEvent(id, StorageManagerEvent.FETCH,
447                 cmd == null ? null : cmd.qname,
448                 oid.toPkString(),
449                 fetchGroup == null ? null : fetchGroup.name,
450                 fieldName);
451         pes.log(ev);
452         return ev;
453     }
454
455     private SmFetchBulkEvent createAndLogSmFetchBulkEvent(OIDArray oids,
456             FieldMetaData triggerField) {
457         int n = oids.size();
458         SmFetchBulkEvent ev = new SmFetchBulkEvent(id,
459                 StorageManagerEvent.FETCH_BULK, n, FetchGroup.DFG_NAME,
460                 triggerField.getQName());
461         if (pes.isFiner()) {
462             OID[] a = oids.oids;
463             String JavaDoc[] strings = new String JavaDoc[n];
464             boolean found[] = new boolean[jmd.classes.length];
465             IntArray classIds = new IntArray();
466             ArrayList JavaDoc cnames = new ArrayList JavaDoc();
467             for (int i = 0; i < n; i++) {
468                 OID oid = a[i];
469                 strings[i] = oid.toStringImp();
470                 int classIndex = oid.getClassIndex();
471                 if (classIndex >= 0 && !found[classIndex]) {
472                     found[classIndex] = true;
473                     ClassMetaData cmd = jmd.classes[classIndex];
474                     classIds.add(cmd.classId);
475                     cnames.add(cmd.qname);
476                 }
477             }
478             ev.setOids(strings);
479             String JavaDoc[] sa = new String JavaDoc[cnames.size()];
480             cnames.toArray(sa);
481             ev.setLookupClasses(classIds.toArray(), sa);
482         }
483         pes.log(ev);
484         return ev;
485     }
486
487     private void fillEventForPacket(SmStatesReturnedEvent ev,
488             StatesReturned container) {
489         int n = container.size();
490         ev.setReturnedSize(n);
491         if (pes.isFiner()) {
492             ev.setReturnedOIDs(convertOidsToStrings(
493                     container.iteratorForOIDs(), n, ev));
494         }
495     }
496
497     private String JavaDoc[] convertOidsToStrings(Iterator JavaDoc iter, int size,
498             SmStatesReturnedEvent ev) {
499         String JavaDoc[] oids = new String JavaDoc[size];
500         boolean found[] = new boolean[jmd.classes.length];
501         IntArray classIds = new IntArray();
502         ArrayList JavaDoc cnames = new ArrayList JavaDoc();
503         if (ev.getLookupClassIDs() != null) {
504             int[] a = ev.getLookupClassIDs();
505             String JavaDoc[] sa = ev.getLookupClassNames();
506             for (int i = 0; i < a.length; i++) {
507                 found[jmd.getClassMetaData(a[i]).index] = true;
508                 classIds.add(a[i]);
509                 cnames.add(sa[i]);
510             }
511         }
512         int count = 0;
513         for (; iter.hasNext(); ) {
514             OID oid = (OID)iter.next();
515             oids[count++] = oid.toStringImp();
516             int classIndex = oid.getClassIndex();
517             if (classIndex >= 0 && !found[classIndex]) {
518                 found[classIndex] = true;
519                 ClassMetaData cmd = jmd.classes[classIndex];
520                 classIds.add(cmd.classId);
521                 cnames.add(cmd.qname);
522             }
523         }
524         String JavaDoc[] ca = new String JavaDoc[cnames.size()];
525         cnames.toArray(ca);
526         ev.setLookupClasses(classIds.toArray(), ca);
527         return oids;
528     }
529
530     private SmStoreEvent createAndLogSmStoreEvent(
531             StatesToStore toStore, DeletePacket toDelete,
532             boolean returnFieldsUpdatedBySM, int storeOption,
533             boolean evictClasses) {
534         SmStoreEvent ev = new SmStoreEvent(id, returnFieldsUpdatedBySM,
535                 storeOption, evictClasses);
536         int n = toStore == null ? 0 : toStore.size();
537         ev.setToStoreSize(n);
538         if (pes.isFiner() && toStore != null) {
539             ev.setToStoreOIDs(convertOidsToStrings(toStore.iteratorForOIDs(), n, ev));
540         }
541         n = toDelete == null ? 0 : toDelete.size();
542         ev.setToDeleteSize(n);
543         if (pes.isFiner() && toDelete != null) {
544             ev.setToDeleteOIDs(convertOidsToStrings(toDelete.iterator(), n, ev));
545         }
546         pes.log(ev);
547         return ev;
548     }
549
550 }
551
Popular Tags