KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > polepos > teams > jdo > JdoEnhancer


1 /*
2  This file is part of the PolePosition database benchmark
3  http://www.polepos.org
4
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  as published by the Free Software Foundation; either version 2
8  of the License, or (at your option) any later version.
9
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public
16  License along with this program; if not, write to the Free
17  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18  MA 02111-1307, USA. */

19
20 package org.polepos.teams.jdo;
21
22 import java.lang.reflect.*;
23 import java.util.*;
24
25 import com.versant.core.jdo.tools.ant.*;
26
27 public class JdoEnhancer {
28
29     private String JavaDoc className;
30     private String JavaDoc methodName;
31     private Class JavaDoc[] types;
32     private Object JavaDoc[] params;
33
34     private final static Map<String JavaDoc, JdoEnhancer> enhancers = registerEnhancers();
35     
36     private JdoEnhancer(){
37     }
38
39     public JdoEnhancer(String JavaDoc enhancerClass, String JavaDoc enhancerMethod, Class JavaDoc[] parameterTypes,
40         Object JavaDoc[] parameters) {
41         className = enhancerClass;
42         methodName = enhancerMethod;
43         types = parameterTypes;
44         params = parameters;
45     }
46
47     public static void main(String JavaDoc[] args) throws Exception JavaDoc{
48         
49         if(args == null || args.length == 0){
50             System.err.println("Supply the name of the enhancer to org.polepos.teams.jdo.JdoEnhancer#main()");
51             printRegisteredEnhancers();
52             return;
53         }
54         
55         JdoEnhancer enhancer = enhancers.get(args[0]);
56         if(enhancer == null){
57             System.err.println("Enhancer " + args[0] + " is not registered in org.polepos.teams.jdo.JdoEnhancer");
58             printRegisteredEnhancers();
59             return;
60         }
61         
62         if(enhancer.isRunnable()){
63             enhancer.run();
64         }else{
65             try {
66                 enhancer.callByReflection();
67             } catch (Exception JavaDoc e) {
68                 System.err.println("Jdo enhancing was not possible with the supplied enhancer name.");
69                 e.printStackTrace();
70                 printRegisteredEnhancers();
71             }
72         }
73     }
74     
75     private static void printRegisteredEnhancers(){
76         System.err.println("The following enhancers are registered, but they are only");
77         System.err.println("available if the respective Jars are present in /lib");
78         for(String JavaDoc key : enhancers.keySet()){
79             System.err.println(key);
80         }
81     }
82
83     private void callByReflection() throws ClassNotFoundException JavaDoc, SecurityException JavaDoc,
84         NoSuchMethodException JavaDoc, IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc,
85         InvocationTargetException {
86         Class JavaDoc clazz = Class.forName(className);
87         Method method = clazz.getMethod(methodName, types);
88         method.invoke(null, params);
89     }
90
91     private final static Map<String JavaDoc, JdoEnhancer> registerEnhancers() {
92
93         Map<String JavaDoc, JdoEnhancer> map = new HashMap<String JavaDoc, JdoEnhancer>();
94         
95         // tested successfully with odbfe.jar and jdo.jar in the lib folder
96
// Both are supplied the ObjectDB free edition downloadable from:
97
// http://www.objectdb.com
98
JdoEnhancer objectDBEnhancer = new JdoEnhancer(
99             "com.objectdb.Enhancer",
100             "enhance",
101             new Class JavaDoc[] { String JavaDoc.class },
102             new Object JavaDoc[] { "org.polepos.teams.jdo.data.*" }
103         );
104         map.put("objectdb", objectDBEnhancer);
105         
106         
107         JdoEnhancer voaEnhancer = new JdoEnhancer(){
108             
109             public boolean isRunnable(){
110                 return true;
111             }
112             
113             public void run(){
114                 com.versant.core.jdo.tools.enhancer.Enhancer.main(
115                     new String JavaDoc[]{
116                     "-p",
117                     "versant.properties",
118                 });
119             }
120             
121         };
122         
123         map.put("voa", voaEnhancer);
124         
125         
126         // TODO: add more enhancers here and register them like above
127

128
129         return map;
130     }
131
132     public void run() {
133         // virtual method to override
134
}
135     
136     public boolean isRunnable() {
137         // virtual method to override
138
return false;
139     }
140
141 }
142
Popular Tags