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.ta;
022:
023: import com.db4o.activation.Activator;
024: import com.db4o.config.*;
025: import com.db4o.events.*;
026: import com.db4o.internal.*;
027: import com.db4o.internal.diagnostic.*;
028: import com.db4o.reflect.*;
029:
030: public class TransparentActivationSupport implements ConfigurationItem {
031:
032: public void prepare(Configuration configuration) {
033: // Nothing to do...
034: }
035:
036: public void apply(final InternalObjectContainer container) {
037: container.configure().activationDepth(0);
038:
039: EventRegistry factory = EventRegistryFactory
040: .forObjectContainer(container);
041:
042: factory.instantiated().addListener(new EventListener4() {
043: public void onEvent(Event4 e, EventArgs args) {
044: ObjectEventArgs oea = (ObjectEventArgs) args;
045: Object obj = oea.object();
046: if (obj instanceof Activatable) {
047: ((Activatable) obj).bind(activatorForObject(
048: (Transaction) oea.transaction(), obj));
049: }
050: }
051:
052: private Activator activatorForObject(
053: final Transaction transaction, Object obj) {
054: // FIXME: Using ObjectContainerBase here won't work for MTOC.
055: return transaction.referenceForObject(obj);
056: }
057: });
058:
059: final TADiagnosticProcessor processor = new TADiagnosticProcessor(
060: container);
061: factory.classRegistered().addListener(new EventListener4() {
062: public void onEvent(Event4 e, EventArgs args) {
063: ClassEventArgs cea = (ClassEventArgs) args;
064: processor.onClassRegistered(cea.classMetadata());
065: }
066: });
067: }
068:
069: private final class TADiagnosticProcessor {
070:
071: private final InternalObjectContainer _container;
072:
073: public TADiagnosticProcessor(InternalObjectContainer container) {
074: _container = container;
075: }
076:
077: public void onClassRegistered(ClassMetadata clazz) {
078: // if(Platform4.isDb4oClass(clazz.getName())) {
079: // return;
080: // }
081: ReflectClass reflectClass = clazz.classReflector();
082: if (activatableClass().isAssignableFrom(reflectClass)) {
083: return;
084: }
085: if (hasOnlyPrimitiveFields(reflectClass)) {
086: return;
087: }
088: NotTransparentActivationEnabled diagnostic = new NotTransparentActivationEnabled(
089: clazz);
090: DiagnosticProcessor processor = _container.handlers()._diagnosticProcessor;
091: processor.onDiagnostic(diagnostic);
092: }
093:
094: private ReflectClass activatableClass() {
095: return _container.reflector().forClass(Activatable.class);
096: }
097:
098: private boolean hasOnlyPrimitiveFields(ReflectClass clazz) {
099: ReflectClass curClass = clazz;
100: while (curClass != null) {
101: ReflectField[] fields = curClass.getDeclaredFields();
102: for (int fieldIdx = 0; fieldIdx < fields.length; fieldIdx++) {
103: if (!fields[fieldIdx].getFieldType().isPrimitive()) {
104: return false;
105: }
106: }
107: curClass = curClass.getSuperclass();
108: }
109: return true;
110: }
111: }
112: }
|