This project has retired. For details please refer to its
Attic page.
App xref
1 package org.apache.juddi.ddl.generator;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.net.URL;
21 import java.util.ArrayList;
22 import java.util.EnumSet;
23 import java.util.Enumeration;
24 import java.util.List;
25 import java.util.jar.JarEntry;
26 import java.util.jar.JarFile;
27 import org.hibernate.boot.Metadata;
28 import org.hibernate.boot.MetadataSources;
29 import org.hibernate.boot.registry.StandardServiceRegistry;
30 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
31 import org.hibernate.tool.hbm2ddl.SchemaExport;
32 import org.hibernate.tool.hbm2ddl.SchemaExport.Action;
33 import org.hibernate.tool.schema.TargetType;
34
35
36
37
38
39
40
41
42
43
44
45 public class App {
46
47 private List<Class> jpaClasses = new ArrayList<>();
48
49 public App(String packageName) throws Exception {
50
51 List<Class> classesForPackage = getClassesForPackage(org.apache.juddi.model.Address.class.getPackage());
52 for (Class<Object> clazz : classesForPackage) {
53
54 jpaClasses.add(clazz);
55 }
56 }
57
58 public App(String dir, String packageName) throws Exception {
59
60 List<Class> c = new ArrayList<Class>();
61 processDirectory(new File("../" + dir), packageName, c);
62
63 processDirectory(new File(dir), packageName, c);
64 for (Class<Object> clazz : c) {
65
66 jpaClasses.add(clazz);
67 }
68
69 }
70
71
72
73
74
75
76 private void generate(Dialect dialect) {
77
78 StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
79 ssrb.applySetting("hibernate.dialect", dialect.getDialectClass());
80 StandardServiceRegistry standardServiceRegistry = ssrb.build();
81
82 MetadataSources metadataSources = new MetadataSources(standardServiceRegistry);
83 for (Class clzz : jpaClasses) {
84 metadataSources.addAnnotatedClass(clzz);
85 }
86
87 Metadata metadata = metadataSources.buildMetadata();
88
89 SchemaExport export = new SchemaExport();
90
91 export.setDelimiter(";");
92 export.setOutputFile(dialect.name().toLowerCase() + ".ddl");
93
94 export.execute(EnumSet.of(TargetType.SCRIPT), Action.BOTH, metadata);
95 }
96
97
98
99
100 public static void main(String[] args) throws Exception {
101 App gen = null;
102 if (args != null && args.length == 1) {
103 gen = new App(args[0], "org.apache.juddi.model");
104 } else {
105 gen = new App("org.apache.juddi.model");
106 }
107 for (int i = 0; i < Dialect.values().length; i++) {
108 gen.generate(Dialect.values()[i]);
109 }
110 }
111
112 private static void log(String msg) {
113 System.out.println("ClassDiscovery: " + msg);
114 }
115
116 private static Class<?> loadClass(String className) {
117 try {
118 return Class.forName(className);
119 } catch (ClassNotFoundException e) {
120 throw new RuntimeException("Unexpected ClassNotFoundException loading class '" + className + "'");
121 }
122 }
123
124 private static void processDirectory(File directory, String pkgname, List<Class> classes) {
125 log("Reading Directory '" + directory + "'");
126
127 if (!directory.exists()) {
128 return;
129 }
130 String[] files = directory.list();
131 for (int i = 0; i < files.length; i++) {
132 String fileName = files[i];
133 String className = null;
134
135 if (fileName.endsWith(".class")) {
136
137 className = pkgname + '.' + fileName.substring(0, fileName.length() - 6);
138 }
139 log("FileName '" + fileName + "' => class '" + className + "'");
140 if (className != null) {
141 classes.add(loadClass(className));
142 }
143 File subdir = new File(directory, fileName);
144 if (subdir.isDirectory()) {
145 processDirectory(subdir, pkgname + '.' + fileName, classes);
146 }
147 }
148 }
149
150 private static void processJarfile(URL resource, String pkgname, List<Class> classes) {
151 String relPath = pkgname.replace('.', '/');
152 String resPath = resource.getPath();
153 String jarPath = resPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "");
154 log("Reading JAR file: '" + jarPath + "'");
155 JarFile jarFile;
156 try {
157 jarFile = new JarFile(jarPath);
158 } catch (IOException e) {
159 throw new RuntimeException("Unexpected IOException reading JAR File '" + jarPath + "'", e);
160 }
161 Enumeration<JarEntry> entries = jarFile.entries();
162 while (entries.hasMoreElements()) {
163 JarEntry entry = entries.nextElement();
164 String entryName = entry.getName();
165 String className = null;
166 if (entryName.endsWith(".class") && entryName.startsWith(relPath) && entryName.length() > (relPath.length() + "/".length())) {
167 className = entryName.replace('/', '.').replace('\\', '.').replace(".class", "");
168 }
169 log("JarEntry '" + entryName + "' => class '" + className + "'");
170 if (className != null) {
171 classes.add(loadClass(className));
172 }
173 }
174 }
175
176 public static List<Class> getClassesForPackage(Package pkg) {
177 List<Class> classes = new ArrayList<Class>();
178
179 String pkgname = pkg.getName();
180 log(pkgname);
181 log(pkg.getName());
182 String relPath = pkgname.replace('.', '/');
183
184
185 URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
186 if (resource == null) {
187 processDirectory(new File(pkgname), pkgname, classes);
188 throw new RuntimeException("Unexpected problem: No resource for " + relPath);
189 }
190 log("Package: '" + pkgname + "' becomes Resource: '" + resource.toString() + "'");
191
192 resource.getPath();
193 if (resource.toString().startsWith("jar:")) {
194 processJarfile(resource, pkgname, classes);
195 } else {
196 processDirectory(new File(resource.getPath()), pkgname, classes);
197 }
198
199 return classes;
200 }
201
202
203
204
205
206
207
208 private List<Class> getClasses(String packageName) throws Exception {
209 List classes = new ArrayList();
210 File directory = null;
211
212 ClassLoader cld = Thread.currentThread().getContextClassLoader();
213 cld = org.apache.juddi.model.Address.class.getClassLoader();
214
215 if (cld == null) {
216 throw new ClassNotFoundException("Can't get class loader.");
217 }
218
219 String path = packageName.replace('.', '/');
220
221
222 System.out.println(path);
223 Enumeration resources = cld.getResources(path);
224
225 while (resources.hasMoreElements()) {
226 URL resource = (URL) resources.nextElement();
227 System.out.println(resource.toExternalForm());
228 try {
229 directory = new File(resource.toURI().getPath());
230 } catch (NullPointerException x) {
231 throw new ClassNotFoundException(packageName + " (" + directory
232 + ") does not appear to be a valid package");
233 }
234 if (directory.exists()) {
235 String[] files = directory.list();
236 for (int i = 0; i < files.length; i++) {
237 if (files[i].endsWith(".class")) {
238
239 classes.add(Class.forName(packageName + '.'
240 + files[i].substring(0, files[i].length() - 6)));
241 }
242 }
243 }
244 }
245 if (classes.isEmpty()) {
246 System.err.println("No classes could be loaded.");
247 System.exit(1);
248
249 }
250 return classes;
251 }
252
253
254
255
256 private static enum Dialect {
257
258 ORACLE("org.hibernate.dialect.Oracle10gDialect"),
259 MYSQL("org.hibernate.dialect.MySQLDialect"),
260 HSQL("org.hibernate.dialect.HSQLDialect"),
261 POSTGRES("org.hibernate.dialect.PostgreSQLDialect"),
262 MYSQL5("org.hibernate.dialect.MySQL5Dialect"),
263 DB2("org.hibernate.dialect.DB2Dialect"),
264 Derby("org.hibernate.dialect.DerbyDialect"),
265 MySQLInnoDB("org.hibernate.dialect.MySQLInnoDBDialect"),
266 Oracle9i("org.hibernate.dialect.Oracle9iDialect"),
267 Sybase("org.hibernate.dialect.SybaseDialect"),
268 MSSQL2000("org.hibernate.dialect.SQLServerDialect");
269
270
271 private String dialectClass;
272
273 private Dialect(String dialectClass) {
274 this.dialectClass = dialectClass;
275 }
276
277 public String getDialectClass() {
278 return dialectClass;
279 }
280 }
281 }