KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > nativequery > main > Db4oFileEnhancer


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.nativequery.main;
22
23 import java.io.*;
24 import java.net.*;
25
26 import EDU.purdue.cs.bloat.editor.*;
27 import EDU.purdue.cs.bloat.file.*;
28
29 import com.db4o.nativequery.bloat.*;
30 import com.db4o.nativequery.optimization.*;
31 import com.db4o.query.*;
32
33 public class Db4oFileEnhancer {
34     private NativeQueryEnhancer enhancer=new NativeQueryEnhancer();
35     
36     public void enhance(String JavaDoc sourceDir,String JavaDoc targetDir,String JavaDoc[] classPath,String JavaDoc packagePredicate) throws Exception JavaDoc {
37         File source = new File(sourceDir);
38         File target = new File(targetDir);
39         
40         ClassFileLoader fileLoader=new ClassFileLoader();
41         fileLoader.setClassPath(sourceDir);
42         URL[] urls=new URL[classPath.length+1];
43         urls[0]=source.toURL();
44         for (int pathIdx = 0; pathIdx < classPath.length; pathIdx++) {
45             fileLoader.appendClassPath(classPath[pathIdx]);
46             urls[pathIdx+1]=new File(classPath[pathIdx]).toURL();
47         }
48         URLClassLoader classLoader=new URLClassLoader(urls,ClassLoader.getSystemClassLoader());
49         fileLoader.setOutputDir(target);
50         if(!source.isDirectory()) {
51             throw new IOException("No directory: "+sourceDir);
52         }
53         enhance(source.getCanonicalPath(),source,target,classLoader,new BloatUtil(fileLoader),packagePredicate);
54         fileLoader.done();
55     }
56     
57     private void enhance(String JavaDoc prefix,File source,File target,ClassLoader JavaDoc classLoader,BloatUtil bloatUtil,String JavaDoc packagePredicate) throws Exception JavaDoc {
58         if(source.isDirectory()) {
59             File[] subFiles=source.listFiles(new FileFilter() {
60                 public boolean accept(File file) {
61                     return file.isDirectory()||file.getName().endsWith(".class");
62                 }
63             });
64             target.mkdirs();
65             for (int idx = 0; idx < subFiles.length; idx++) {
66                 enhance(prefix,subFiles[idx],new File(target,subFiles[idx].getName()),classLoader,bloatUtil,packagePredicate);
67             }
68         }
69         else {
70             String JavaDoc className = source.getCanonicalPath().substring(prefix.length()+1);
71             className = className.substring(0, className.length()-".class".length());
72             className=className.replace(File.separatorChar,'.');
73             if(!className.startsWith(packagePredicate)) {
74                 copyFile(source,target);
75                 return;
76             }
77             try {
78                 Class JavaDoc clazz = classLoader.loadClass(className);
79                 Class JavaDoc filterClass = classLoader.loadClass(Predicate.class.getName());
80                 if(filterClass.isAssignableFrom(clazz)&&filterClass!=clazz) {
81                     System.err.println("Processing "+className);
82                     ClassEditor classEditor=bloatUtil.classEditor(className);
83                     enhancer.enhance(bloatUtil,classEditor,Predicate.PREDICATEMETHOD_NAME,classLoader);
84                 }
85                 else {
86                     copyFile(source,target);
87                 }
88             } catch (Exception JavaDoc e) {
89                 e.printStackTrace();
90             } catch (NoClassDefFoundError JavaDoc e) {
91                 System.err.println("Omitting "+className+": Referenced class "+e.getMessage()+" not found.");
92                 copyFile(source,target);
93             }
94         }
95     }
96     
97     private void copyFile(File source, File target) throws IOException {
98         BufferedInputStream in=new BufferedInputStream(new FileInputStream(source));
99         BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(target));
100         try {
101             byte[] buffer=new byte[4096];
102             int bytesRead=0;
103             while((bytesRead=in.read(buffer))>=0) {
104                 out.write(buffer,0,bytesRead);
105             }
106             out.flush();
107         } finally {
108             try {
109                 in.close();
110             } catch (IOException e) {
111             }
112             try {
113                 out.close();
114             } catch (IOException e) {
115             }
116         }
117     }
118
119     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
120         new Db4oFileEnhancer().enhance(args[0],args[1],new String JavaDoc[]{},"");
121     }
122 }
123
Popular Tags