KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > FilterEnhancer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * FilterEnhancer.java
26  */

27
28 package com.sun.jdo.api.persistence.enhancer;
29
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.PrintWriter JavaDoc;
34 import java.io.DataInputStream JavaDoc;
35 import java.io.DataOutputStream JavaDoc;
36
37 import java.util.Properties JavaDoc;
38
39 import com.sun.jdo.api.persistence.model.Model;
40
41 import com.sun.jdo.api.persistence.enhancer.meta.JDOMetaData;
42 import com.sun.jdo.api.persistence.enhancer.meta.JDOMetaDataModelImpl;
43 import com.sun.jdo.api.persistence.enhancer.meta.JDOMetaDataPropertyImpl;
44
45 import com.sun.jdo.api.persistence.enhancer.impl.EnhancerControl;
46 import com.sun.jdo.api.persistence.enhancer.impl.Environment;
47 import com.sun.jdo.api.persistence.enhancer.impl.ClassControl;
48
49 import com.sun.jdo.api.persistence.enhancer.classfile.ClassFile;
50
51 //@olsen: added: support for I18N
52
import com.sun.jdo.api.persistence.enhancer.util.Support;
53 import com.sun.jdo.api.persistence.enhancer.util.UserException;
54 import com.sun.jdo.api.persistence.enhancer.util.ClassFileSource;
55
56
57 //@lars: the output stream is always written with the class - even if it hasn't been enhanced
58
//@lars: added an error-PrintWriter to all constructors
59
//@lars: changes to reflect the new ByteCodeEnhancer interface
60

61
62 /**
63  * Implements a JDO enhancer as a byte-code filtering tool.
64  */

65 //@olsen: added class
66
public class FilterEnhancer
67     extends Support
68     implements ByteCodeEnhancer
69 {
70     static public final String JavaDoc DO_SIMPLE_TIMING
71     = "ByteCodeEnhancer.doSimpleTiming";//NOI18N
72
static public final String JavaDoc VERBOSE_LEVEL
73     = "ByteCodeEnhancer.verboseLevel";//NOI18N
74
static public final String JavaDoc VERBOSE_LEVEL_QUIET
75     = "quiet";//NOI18N
76
static public final String JavaDoc VERBOSE_LEVEL_WARN
77     = "warn";//NOI18N
78
static public final String JavaDoc VERBOSE_LEVEL_VERBOSE
79     = "verbose";//NOI18N
80
static public final String JavaDoc VERBOSE_LEVEL_DEBUG
81     = "debug";//NOI18N
82

83     /* Central repository for the options selected by
84      * the user and the current state of the Filter execution */

85     private Environment env = new Environment();
86
87     private EnhancerControl econtrol = new EnhancerControl(env);
88
89 // private StringWriter errString = new StringWriter();
90
// private PrintWriter err = new PrintWriter(errString, true);
91

92     /**
93      * Initializes an instance of a JDO enhancer.
94      * @param metaData the JDO meta-data object
95      * @param settings enhancement properties
96      * @param out standard ouput stream for the enhancer
97      */

98     protected void init(JDOMetaData metaData,
99                         Properties JavaDoc settings,
100                         PrintWriter JavaDoc out,
101                         PrintWriter JavaDoc err)
102         throws EnhancerUserException, EnhancerFatalError
103     {
104         if (metaData == null) {
105             //@olsen: support for I18N
106
throw new EnhancerFatalError(
107                 getI18N("enhancer.internal_error",//NOI18N
108
"Illegal argument: metaData == null"));//NOI18N
109
}
110
111         env.setJDOMetaData(metaData);
112
113         // set verbose level
114
if (err != null)
115         {
116             env.setErrorWriter(err);
117         }
118         if (out != null)
119         {
120             env.setOutputWriter(out);
121         }
122         final String JavaDoc verboseLevel
123             = (settings == null ? null : settings.getProperty(VERBOSE_LEVEL));
124         if (VERBOSE_LEVEL_QUIET.equals(verboseLevel)) {
125             env.setVerbose(false);
126             env.setQuiet(true);
127         } else if (VERBOSE_LEVEL_WARN.equals(verboseLevel)) {
128             env.setVerbose(false);
129             env.setQuiet(false);
130         } else if (VERBOSE_LEVEL_VERBOSE.equals(verboseLevel)) {
131             env.setVerbose(true);
132             env.setQuiet(false);
133         } else if (VERBOSE_LEVEL_DEBUG.equals(verboseLevel)) {
134             env.setVerbose(true);
135             env.setQuiet(false);
136         } else {
137             env.setVerbose(false);
138             env.setQuiet(false);
139         }
140
141         //@olsen: force settings
142
env.setNoOptimization(true);
143         env.messageNL("FilterEnhancer: forced settings: -noopt");//NOI18N
144
}
145
146     /**
147      * Creates an instance of a JDO enhancer.
148      * @param metaData the JDO meta-data object
149      * @param settings enhancement properties
150      * @param out standard ouput stream for the enhancer
151      */

152     public FilterEnhancer(JDOMetaData metaData,
153                           Properties JavaDoc settings,
154                           PrintWriter JavaDoc out,
155                           PrintWriter JavaDoc err)
156         throws EnhancerUserException, EnhancerFatalError
157     {
158         init(metaData, settings, out, err);
159     }
160
161     /**
162      * Creates an instance of a JDO enhancer.
163      * @param metaData the JDO meta-data properties
164      * @param settings enhancement properties
165      * @param out standard ouput stream for the enhancer
166      */

167     public FilterEnhancer(Properties JavaDoc metaData,
168                           Properties JavaDoc settings,
169                           PrintWriter JavaDoc out,
170                           PrintWriter JavaDoc err)
171         throws EnhancerUserException, EnhancerFatalError
172     {
173         if (metaData == null) {
174             //@olsen: support for I18N
175
throw new EnhancerFatalError(
176                 getI18N("enhancer.internal_error",//NOI18N
177
"Illegal argument: metaData == null"));//NOI18N
178
}
179
180         final JDOMetaData meta
181             = new JDOMetaDataPropertyImpl(metaData, out);
182         init(meta, settings, out, err);
183     }
184
185     /**
186      * Creates an instance of a JDO enhancer.
187      * @param model the JDO model
188      * @param settings enhancement properties
189      * @param out standard ouput stream for the enhancer
190      */

191     public FilterEnhancer(Model metaData,
192                           Properties JavaDoc settings,
193                           PrintWriter JavaDoc out,
194                           PrintWriter JavaDoc err)
195         throws EnhancerUserException, EnhancerFatalError
196     {
197         if (metaData == null) {
198             //@olsen: support for I18N
199
throw new EnhancerFatalError(
200                 getI18N("enhancer.internal_error",//NOI18N
201
"Illegal argument: metaData == null"));//NOI18N
202
}
203
204         final JDOMetaData meta
205             = new JDOMetaDataModelImpl(metaData,
206                                        env.getOutputWriter());
207         init(meta, settings, out, err);
208     }
209
210
211     /**
212      * Enhances a given class according to the JDO meta-data.
213      */

214     public boolean enhanceClassFile(InputStream JavaDoc inByteCode,
215                                     OutputStreamWrapper outByteCode)
216         throws EnhancerUserException, EnhancerFatalError
217     {
218         env.messageNL("FilterEnhancer: enhancing classfile ...");//NOI18N
219

220         // reset environment to clear class map etc.
221
env.reset();
222
223         // enhance class file; check Exceptions
224
final boolean changed;
225         try {
226             changed = enhanceClassFile1(inByteCode, outByteCode);
227         } catch (UserException ex) {
228             // note: catch UserException before RuntimeException
229

230             // reset environment to clear class map etc.
231
env.reset();
232             //@olsen: support for I18N
233
throw new EnhancerUserException(
234                 getI18N("enhancer.error",//NOI18N
235
ex.getMessage()),
236                 ex);
237         } catch (RuntimeException JavaDoc ex) {
238             // note: catch UserException before RuntimeException
239

240             // reset environment to clear class map etc.
241
env.reset();
242             //@olsen: support for I18N
243
ex.printStackTrace ();
244             throw new EnhancerFatalError(
245                 getI18N("enhancer.internal_error",//NOI18N
246
ex.getMessage()),
247                 ex);
248         }
249
250         env.messageNL(changed
251                       ? "FilterEnhancer: classfile enhanced successfully."//NOI18N
252
: "FilterEnhancer: classfile not changed.");//NOI18N
253
return changed;
254     }
255
256     /**
257      * Enhances a given class according to the JDO meta-data.
258      */

259     private boolean enhanceClassFile1(InputStream JavaDoc inByteCode,
260                                       OutputStreamWrapper outByteCode)
261     {
262         // check arguments
263
affirm(inByteCode, "Illegal argument: inByteCode == null.");//NOI18N
264
affirm(outByteCode, "Illegal argument: outByteCode == null.");//NOI18N
265

266         // parse class
267
final ClassFileSource cfs;
268         final ClassFile cf;
269         final ClassControl cc;
270         try {
271             // create class file source
272
cfs = new ClassFileSource(null, inByteCode);
273
274             // create class file
275
final DataInputStream JavaDoc dis = cfs.classFileContents();
276             cf = new ClassFile(dis);
277 //@lars: do not close the input stream
278
// dis.close();
279

280             // create class control
281
cc = new ClassControl(cfs, cf, env);
282             env.addClass(cc);
283
284             // get real class name
285
final String JavaDoc className = cc.className();
286             cfs.setExpectedClassName(className);
287         } catch (IOException JavaDoc ex) {
288             //@olsen: support for I18N
289
throw new UserException(
290                 getI18N("enhancer.io_error_while_reading_stream"),//NOI18N
291
ex);
292         } catch (ClassFormatError JavaDoc ex) {
293             //@olsen: support for I18N
294
throw new UserException(
295                 getI18N("enhancer.class_format_error"),//NOI18N
296
ex);
297         }
298
299         // enhance class
300
econtrol.modifyClasses();
301         if (env.errorCount() > 0) {
302             // retrieve error messages
303
env.getErrorWriter ().flush ();
304             /*
305             final String str = errString.getBuffer().toString();
306
307             // reset env's error writer
308             errString = new StringWriter();
309             err = new PrintWriter(errString, true);
310             env.setErrorWriter(err);
311             */

312
313             //@olsen: support for I18N
314
throw new UserException(env.getLastErrorMessage ());
315         }
316
317         // write class
318
boolean changed = (cc.updated() && cc.filterRequired());
319         try {
320             if (changed)
321             {
322                 env.message("writing enhanced class " + cc.userClassName()//NOI18N
323
+ " to output stream");//NOI18N
324
}
325             else
326             {
327                 env.message("no changes on class " + cc.userClassName());
328             }
329             outByteCode.setClassName (cc.userClassName ());
330             final DataOutputStream JavaDoc dos = new DataOutputStream JavaDoc(outByteCode.getStream ());
331             cf.write(dos);
332             dos.flush();
333         } catch (IOException JavaDoc ex) {
334             //@olsen: support for I18N
335
throw new UserException(
336                 getI18N("enhancer.io_error_while_writing_stream"),//NOI18N
337
ex);
338         }
339         return changed;
340     }
341
342
343     /**********************************************************************
344      *
345      *********************************************************************/

346
347     public boolean enhanceClassFile (InputStream JavaDoc in,
348                                      OutputStream JavaDoc out)
349                    throws EnhancerUserException,
350                           EnhancerFatalError
351     {
352
353         return enhanceClassFile (in, new OutputStreamWrapper (out));
354
355     } //FilterEnhancer.enhanceClassFile()
356

357
358 } //FilterEnhancer
359
Popular Tags