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;
022:
023: import com.db4o.reflect.*;
024:
025: /** @exclude */
026: public final class EventDispatcher {
027: private static final String[] events = { "objectCanDelete",
028: "objectOnDelete", "objectOnActivate", "objectOnDeactivate",
029: "objectOnNew", "objectOnUpdate", "objectCanActivate",
030: "objectCanDeactivate", "objectCanNew", "objectCanUpdate" };
031:
032: static final int CAN_DELETE = 0;
033: static final int DELETE = 1;
034: static final int SERVER_COUNT = 2;
035: static final int ACTIVATE = 2;
036: static final int DEACTIVATE = 3;
037: static final int NEW = 4;
038: public static final int UPDATE = 5;
039: static final int CAN_ACTIVATE = 6;
040: static final int CAN_DEACTIVATE = 7;
041: static final int CAN_NEW = 8;
042: static final int CAN_UPDATE = 9;
043: static final int COUNT = 10;
044:
045: private final ReflectMethod[] methods;
046:
047: private EventDispatcher(ReflectMethod[] methods_) {
048: methods = methods_;
049: }
050:
051: boolean dispatch(ObjectContainerBase stream, Object obj, int eventID) {
052: if (methods[eventID] == null) {
053: return true;
054: }
055: Object[] parameters = new Object[] { stream };
056: int stackDepth = stream.stackDepth();
057: int topLevelCallId = stream.topLevelCallId();
058: stream.stackDepth(0);
059: try {
060: Object res = methods[eventID].invoke(obj, parameters);
061: if (res instanceof Boolean) {
062: return ((Boolean) res).booleanValue();
063: }
064: } finally {
065: stream.stackDepth(stackDepth);
066: stream.topLevelCallId(topLevelCallId);
067: }
068: return true;
069: }
070:
071: static EventDispatcher forClass(ObjectContainerBase a_stream,
072: ReflectClass classReflector) {
073:
074: if (a_stream == null || classReflector == null) {
075: return null;
076: }
077:
078: EventDispatcher dispatcher = null;
079: int count = 0;
080: if (a_stream.configImpl().callbacks()) {
081: count = COUNT;
082: } else if (a_stream.configImpl().isServer()) {
083: count = SERVER_COUNT;
084: }
085: if (count > 0) {
086: ReflectClass[] parameterClasses = { a_stream._handlers.ICLASS_OBJECTCONTAINER };
087: ReflectMethod[] methods = new ReflectMethod[COUNT];
088: for (int i = COUNT - 1; i >= 0; i--) {
089: ReflectMethod method = classReflector.getMethod(
090: events[i], parameterClasses);
091: if (null == method) {
092: method = classReflector.getMethod(
093: toPascalCase(events[i]), parameterClasses);
094: }
095: if (method != null) {
096: methods[i] = method;
097: if (dispatcher == null) {
098: dispatcher = new EventDispatcher(methods);
099: }
100: }
101: }
102: }
103:
104: return dispatcher;
105: }
106:
107: private static String toPascalCase(String name) {
108: return name.substring(0, 1).toUpperCase() + name.substring(1);
109: }
110:
111: public boolean hasEventRegistered(int eventID) {
112: return methods[eventID] != null;
113: }
114: }
|