KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > inside > diagnostic > DiagnosticProcessor


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.inside.diagnostic;
22
23 import com.db4o.*;
24 import com.db4o.diagnostic.*;
25 import com.db4o.foundation.*;
26 import com.db4o.query.*;
27
28 /**
29  * @exclude
30  *
31  * FIXME: remove me from the core and make me a facade over Events
32  */

33 public class DiagnosticProcessor implements DiagnosticConfiguration, DeepClone{
34     
35     private Collection4 _listeners;
36     
37     public DiagnosticProcessor() {
38     }
39     
40     private DiagnosticProcessor(Collection4 listeners) {
41         _listeners = listeners;
42     }
43
44     public void addListener(DiagnosticListener listener) {
45         if(_listeners == null){
46             _listeners = new Collection4();
47         }
48         _listeners.add(listener);
49     }
50     
51     public void checkClassHasFields(YapClass yc){
52         YapField[] fields = yc.i_fields;
53         if(fields != null && fields.length == 0){
54             String JavaDoc name = yc.getName();
55             String JavaDoc[] ignoredPackages = new String JavaDoc[]{
56                 "java.util."
57             };
58             for (int i = 0; i < ignoredPackages.length; i++) {
59                 if (name.indexOf(ignoredPackages[i]) == 0){
60                     return;
61                 }
62             }
63             if(isDb4oClass(yc)){
64                 return;
65             }
66             onDiagnostic(new ClassHasNoFields(name));
67         }
68     }
69
70     public void checkUpdateDepth(int depth) {
71         if (depth > 1) {
72             onDiagnostic(new UpdateDepthGreaterOne(depth));
73         }
74     }
75
76     public Object JavaDoc deepClone(Object JavaDoc context) {
77         return new DiagnosticProcessor(cloneListeners());
78     }
79
80     private Collection4 cloneListeners() {
81         return _listeners != null
82             ? new Collection4(_listeners)
83             : null;
84     }
85
86     public boolean enabled(){
87         return _listeners != null;
88     }
89     
90     private boolean isDb4oClass(YapClass yc){
91         return Platform4.isDb4oClass(yc.getName());
92     }
93
94     public void loadedFromClassIndex(YapClass yc) {
95         if(isDb4oClass(yc)){
96             return;
97         }
98         onDiagnostic(new LoadedFromClassIndex(yc.getName()));
99     }
100
101     public void descendIntoTranslator(YapClass parent,String JavaDoc fieldName) {
102         onDiagnostic(new DescendIntoTranslator(parent.getName(),fieldName));
103     }
104     
105     public void nativeQueryUnoptimized(Predicate predicate) {
106         onDiagnostic(new NativeQueryNotOptimized(predicate));
107     }
108
109     private void onDiagnostic(Diagnostic d) {
110         if(_listeners == null){
111             return;
112         }
113         Iterator4 i = _listeners.iterator();
114         while(i.moveNext()){
115             ((DiagnosticListener)i.current()).onDiagnostic(d);
116         }
117     }
118     
119     public void removeAllListeners() {
120         _listeners = null;
121     }
122 }
123
Popular Tags