KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > loader > enhancer > EnhancerPrepare


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.loader.enhancer;
31
32 import com.caucho.bytecode.ByteCodeParser;
33 import com.caucho.bytecode.JavaClass;
34 import com.caucho.bytecode.JavaField;
35 import com.caucho.bytecode.JavaMethod;
36 import com.caucho.java.WorkDir;
37 import com.caucho.java.gen.JavaClassGenerator;
38 import com.caucho.util.L10N;
39 import com.caucho.vfs.JarPath;
40 import com.caucho.vfs.Path;
41 import com.caucho.vfs.ReadStream;
42 import com.caucho.vfs.Vfs;
43 import com.caucho.vfs.WriteStream;
44
45 import java.net.URL JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.logging.Level JavaDoc;
48 import java.util.logging.Logger JavaDoc;
49
50 /**
51  * Prepares a class for enhancement.
52  */

53 public class EnhancerPrepare {
54   private static final L10N L = new L10N(EnhancerPrepare.class);
55   private static final Logger JavaDoc log =
56     Logger.getLogger(EnhancerPrepare.class.getName());
57
58   private static final int ACC_PUBLIC = 0x1;
59   private static final int ACC_PRIVATE = 0x2;
60   private static final int ACC_PROTECTED = 0x4;
61
62   private JavaClassGenerator _javaGen = new JavaClassGenerator();
63
64   private ClassLoader JavaDoc _loader;
65
66   private Path _workPath;
67
68   private ArrayList JavaDoc<ClassEnhancer> _enhancerList =
69     new ArrayList JavaDoc<ClassEnhancer>();
70
71   private String JavaDoc _baseSuffix = "";
72   private String JavaDoc _extSuffix = "__ResinExt";
73
74   private boolean _isParentStarted;
75
76   /**
77    * Sets the class loader.
78    */

79   public void setClassLoader(ClassLoader JavaDoc loader)
80   {
81     _loader = loader;
82   }
83
84   /**
85    * Gets the work path.
86    */

87   public Path getWorkPath()
88   {
89     if (_workPath != null)
90       return _workPath;
91     else
92       return WorkDir.getLocalWorkDir(_loader);
93   }
94
95   /**
96    * Sets the work path.
97    */

98   public void setWorkPath(Path workPath)
99   {
100     _workPath = workPath;
101   }
102
103   /**
104    * Gets the work path.
105    */

106   public final Path getPreWorkPath()
107   {
108     return getWorkPath().lookup("pre-enhance");
109   }
110
111   /**
112    * Gets the work path.
113    */

114   public final Path getPostWorkPath()
115   {
116     return getWorkPath().lookup("post-enhance");
117   }
118
119   /**
120    * Adds a class enhancer.
121    */

122   public void addEnhancer(ClassEnhancer enhancer)
123   {
124     _enhancerList.add(enhancer);
125   }
126
127   /**
128    * Moves the old class.
129    */

130   public void renameClass(String JavaDoc sourceClass, String JavaDoc targetClass)
131   {
132     Path source = getSource(sourceClass);
133
134     if (source == null || source.getLastModified() <= 0)
135       return;
136
137     Path path = getPreWorkPath();
138     Path target = path.lookup(targetClass.replace('.', '/') + ".class");
139
140     if (target.getLastModified() <= 0 ||
141         target.getLastModified() < source.getLastModified()) {
142       try {
143         target.remove();
144       } catch (Throwable JavaDoc e) {
145         log.log(Level.WARNING, e.toString(), e);
146       }
147
148       try {
149         ByteCodeParser parser = new ByteCodeParser();
150         ReadStream is = source.openRead();
151         WriteStream os = null;
152
153         try {
154           JavaClass cl = parser.parse(is);
155
156           String JavaDoc cpOldName = sourceClass.replace('.', '/');
157           String JavaDoc cpClassName = targetClass.replace('.', '/');
158
159           int utf8Index = cl.getConstantPool().addUTF8(cpClassName).getIndex();
160           cl.getConstantPool().getClass(cpOldName).setNameIndex(utf8Index);
161
162           cl.setThisClass(cpClassName);
163
164           // need to set descriptors, too
165

166           // set private fields to protected
167
ArrayList JavaDoc<JavaField> fields = cl.getFieldList();
168           for (int i = 0; i < fields.size(); i++) {
169             JavaField field = fields.get(i);
170
171             int accessFlags = field.getAccessFlags();
172
173             if ((accessFlags & ACC_PRIVATE) != 0) {
174               accessFlags = (accessFlags & ~ ACC_PRIVATE) | ACC_PROTECTED;
175               field.setAccessFlags(accessFlags);
176             }
177           }
178
179           // set private methods to protected
180
ArrayList JavaDoc<JavaMethod> methods = cl.getMethodList();
181           for (int i = 0; i < methods.size(); i++) {
182             JavaMethod method = methods.get(i);
183
184             int accessFlags = method.getAccessFlags();
185
186             if ((accessFlags & ACC_PRIVATE) != 0) {
187               accessFlags = (accessFlags & ~ ACC_PRIVATE) | ACC_PROTECTED;
188               method.setAccessFlags(accessFlags);
189             }
190           }
191
192           for (int i = 0; i < _enhancerList.size(); i++) {
193             _enhancerList.get(i).preEnhance(cl);
194           }
195
196           target.getParent().mkdirs();
197
198           os = target.openWrite();
199
200           cl.write(os);
201         } finally {
202           if (is != null)
203             is.close();
204
205           if (os != null)
206             os.close();
207         }
208       } catch (Exception JavaDoc e) {
209         log.log(Level.WARNING, e.toString(), e);
210
211       }
212     }
213   }
214
215   private Path getSource(String JavaDoc className)
216   {
217     ClassLoader JavaDoc loader = _loader;
218     if (loader == null)
219       loader = Thread.currentThread().getContextClassLoader();
220
221     URL JavaDoc url = loader.getResource(className.replace('.', '/') + ".class");
222
223     // XXX: workaround for tck
224
String JavaDoc s = url.toString();
225     int index = s.indexOf("jar!/");
226     if (index > 0) {
227       s = s.substring(9, index+3);
228       Path path = JarPath.create(Vfs.lookup(s));
229       path = path.lookup(className.replace('.', '/') + ".class");
230       return path;
231     }
232
233     if (url != null)
234       return Vfs.lookup(url.toString());
235     else
236       return null;
237   }
238
239   /**
240    * Moves the old class.
241    */

242   protected JavaClass renameClass(JavaClass jClass, String JavaDoc targetClass)
243   {
244     String JavaDoc cpOldName = jClass.getThisClass();
245     String JavaDoc cpClassName = targetClass.replace('.', '/');
246
247     int utf8Index = jClass.getConstantPool().addUTF8(cpClassName).getIndex();
248     jClass.getConstantPool().getClass(cpOldName).setNameIndex(utf8Index);
249
250     jClass.setThisClass(cpClassName);
251
252     // need to set descriptors, too
253

254     // set private fields to protected
255
ArrayList JavaDoc<JavaField> fields = jClass.getFieldList();
256     for (int i = 0; i < fields.size(); i++) {
257       JavaField field = fields.get(i);
258
259       int accessFlags = field.getAccessFlags();
260
261       if ((accessFlags & ACC_PRIVATE) != 0) {
262         accessFlags = (accessFlags & ~ ACC_PRIVATE) | ACC_PROTECTED;
263         field.setAccessFlags(accessFlags);
264       }
265     }
266
267     // set private methods to protected
268
ArrayList JavaDoc<JavaMethod> methods = jClass.getMethodList();
269     for (int i = 0; i < methods.size(); i++) {
270       JavaMethod method = methods.get(i);
271
272       int accessFlags = method.getAccessFlags();
273
274       if ((accessFlags & ACC_PRIVATE) != 0) {
275         accessFlags = (accessFlags & ~ ACC_PRIVATE) | ACC_PROTECTED;
276         method.setAccessFlags(accessFlags);
277       }
278     }
279
280     return jClass;
281   }
282 }
283
Popular Tags