001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal.diagnostic;
022:
023: import com.db4o.diagnostic.*;
024: import com.db4o.foundation.*;
025: import com.db4o.internal.*;
026: import com.db4o.query.*;
027:
028: /**
029: * @exclude
030: *
031: * FIXME: remove me from the core and make me a facade over Events
032: */
033: public class DiagnosticProcessor implements DiagnosticConfiguration,
034: DeepClone {
035:
036: private Collection4 _listeners;
037:
038: public DiagnosticProcessor() {
039: }
040:
041: private DiagnosticProcessor(Collection4 listeners) {
042: _listeners = listeners;
043: }
044:
045: public void addListener(DiagnosticListener listener) {
046: if (_listeners == null) {
047: _listeners = new Collection4();
048: }
049: _listeners.add(listener);
050: }
051:
052: public void checkClassHasFields(ClassMetadata yc) {
053: FieldMetadata[] fields = yc.i_fields;
054: if (fields != null && fields.length == 0) {
055: String name = yc.getName();
056: String[] ignoredPackages = new String[] { "java.util." };
057: for (int i = 0; i < ignoredPackages.length; i++) {
058: if (name.indexOf(ignoredPackages[i]) == 0) {
059: return;
060: }
061: }
062: if (isDb4oClass(yc)) {
063: return;
064: }
065: onDiagnostic(new ClassHasNoFields(name));
066: }
067: }
068:
069: public void checkUpdateDepth(int depth) {
070: if (depth > 1) {
071: onDiagnostic(new UpdateDepthGreaterOne(depth));
072: }
073: }
074:
075: public Object deepClone(Object context) {
076: return new DiagnosticProcessor(cloneListeners());
077: }
078:
079: private Collection4 cloneListeners() {
080: return _listeners != null ? new Collection4(_listeners) : null;
081: }
082:
083: public boolean enabled() {
084: return _listeners != null;
085: }
086:
087: private boolean isDb4oClass(ClassMetadata yc) {
088: return Platform4.isDb4oClass(yc.getName());
089: }
090:
091: public void loadedFromClassIndex(ClassMetadata yc) {
092: if (isDb4oClass(yc)) {
093: return;
094: }
095: onDiagnostic(new LoadedFromClassIndex(yc.getName()));
096: }
097:
098: public void descendIntoTranslator(ClassMetadata parent,
099: String fieldName) {
100: onDiagnostic(new DescendIntoTranslator(parent.getName(),
101: fieldName));
102: }
103:
104: public void nativeQueryUnoptimized(Predicate predicate) {
105: onDiagnostic(new NativeQueryNotOptimized(predicate));
106: }
107:
108: public void nativeQueryOptimizerNotLoaded(int reason) {
109: onDiagnostic(new NativeQueryOptimizerNotLoaded(reason));
110: }
111:
112: public void onDiagnostic(Diagnostic d) {
113: if (_listeners == null) {
114: return;
115: }
116: Iterator4 i = _listeners.iterator();
117: while (i.moveNext()) {
118: ((DiagnosticListener) i.current()).onDiagnostic(d);
119: }
120: }
121:
122: public void removeAllListeners() {
123: _listeners = null;
124: }
125: }
|