KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > EventDispatcher


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;
22
23 import com.db4o.reflect.*;
24
25 /** @exclude */
26 public final class EventDispatcher {
27     private static final String JavaDoc[] events = {
28         "objectCanDelete",
29         "objectOnDelete",
30         "objectOnActivate",
31         "objectOnDeactivate",
32         "objectOnNew",
33         "objectOnUpdate",
34         "objectCanActivate",
35         "objectCanDeactivate",
36         "objectCanNew",
37         "objectCanUpdate"
38     };
39     
40     static final int CAN_DELETE = 0;
41     static final int DELETE = 1;
42     static final int SERVER_COUNT = 2;
43     static final int ACTIVATE = 2;
44     static final int DEACTIVATE = 3;
45     static final int NEW = 4;
46     public static final int UPDATE = 5;
47     static final int CAN_ACTIVATE = 6;
48     static final int CAN_DEACTIVATE = 7;
49     static final int CAN_NEW = 8;
50     static final int CAN_UPDATE = 9;
51     static final int COUNT = 10;
52     
53     private final ReflectMethod[] methods;
54     
55     private EventDispatcher(ReflectMethod[] methods_){
56         methods = methods_;
57     }
58     
59     boolean dispatch(YapStream stream, Object JavaDoc obj, int eventID){
60         if(methods[eventID] != null){
61             Object JavaDoc[] parameters = new Object JavaDoc[]{stream};
62             int stackDepth = stream.stackDepth();
63             int topLevelCallId = stream.topLevelCallId();
64             stream.stackDepth(0);
65             try{
66                 Object JavaDoc res = methods[eventID].invoke(obj,parameters);
67                 if(res != null && res instanceof Boolean JavaDoc){
68                     return ((Boolean JavaDoc)res).booleanValue();
69                 }
70             }catch(Throwable JavaDoc t){
71                 // TODO: Exceptions in callbacks should be wrapped and thrown up.
72

73             } finally {
74                 stream.stackDepth(stackDepth);
75                 stream.topLevelCallId(topLevelCallId);
76             }
77         }
78         return true;
79     }
80     
81     static EventDispatcher forClass(YapStream a_stream, ReflectClass classReflector){
82         
83         if(a_stream == null || classReflector == null){
84             return null;
85         }
86         
87         EventDispatcher dispatcher = null;
88         int count = 0;
89         if(a_stream.configImpl().callbacks()){
90             count = COUNT;
91         }else if(a_stream.configImpl().isServer()){
92             count = SERVER_COUNT;
93         }
94         if(count > 0){
95             ReflectClass[] parameterClasses = {a_stream.i_handlers.ICLASS_OBJECTCONTAINER};
96             ReflectMethod[] methods = new ReflectMethod[COUNT];
97             for (int i = COUNT -1; i >=0; i--){
98                 try{
99                     ReflectMethod method = classReflector.getMethod(events[i], parameterClasses);
100                     if (null == method) {
101                         method = classReflector.getMethod(toPascalCase(events[i]), parameterClasses);
102                     }
103                     if( method != null){
104                         methods[i] = method;
105                         if(dispatcher == null){
106                             dispatcher = new EventDispatcher(methods);
107                         }
108                     }
109                 }catch(Throwable JavaDoc t){}
110             }
111         }
112         
113         return dispatcher;
114     }
115
116     private static String JavaDoc toPascalCase(String JavaDoc name) {
117         return name.substring(0, 1).toUpperCase() + name.substring(1);
118     }
119 }
120
Popular Tags