FileDocCategorySizeDatePackage
FilterEnhancer.javaAPI DocGlassfish v2 API13347Fri May 04 22:34:26 BST 2007com.sun.jdo.api.persistence.enhancer

FilterEnhancer

public class FilterEnhancer extends com.sun.jdo.api.persistence.enhancer.util.Support implements ByteCodeEnhancer
Implements a JDO enhancer as a byte-code filtering tool.

Fields Summary
public static final String
DO_SIMPLE_TIMING
public static final String
VERBOSE_LEVEL
public static final String
VERBOSE_LEVEL_QUIET
public static final String
VERBOSE_LEVEL_WARN
public static final String
VERBOSE_LEVEL_VERBOSE
public static final String
VERBOSE_LEVEL_DEBUG
private com.sun.jdo.api.persistence.enhancer.impl.Environment
env
private com.sun.jdo.api.persistence.enhancer.impl.EnhancerControl
econtrol
Constructors Summary
public FilterEnhancer(com.sun.jdo.api.persistence.enhancer.meta.JDOMetaData metaData, Properties settings, PrintWriter out, PrintWriter err)
Creates an instance of a JDO enhancer.

param
metaData the JDO meta-data object
param
settings enhancement properties
param
out standard ouput stream for the enhancer

        init(metaData, settings, out, err);
    
public FilterEnhancer(Properties metaData, Properties settings, PrintWriter out, PrintWriter err)
Creates an instance of a JDO enhancer.

param
metaData the JDO meta-data properties
param
settings enhancement properties
param
out standard ouput stream for the enhancer

        if (metaData == null) {
            //@olsen: support for I18N
            throw new EnhancerFatalError(
                getI18N("enhancer.internal_error",//NOI18N
                        "Illegal argument: metaData == null"));//NOI18N
        }

        final JDOMetaData meta
            = new JDOMetaDataPropertyImpl(metaData, out);
        init(meta, settings, out, err);
    
public FilterEnhancer(com.sun.jdo.api.persistence.model.Model metaData, Properties settings, PrintWriter out, PrintWriter err)
Creates an instance of a JDO enhancer.

param
model the JDO model
param
settings enhancement properties
param
out standard ouput stream for the enhancer

        if (metaData == null) {
            //@olsen: support for I18N
            throw new EnhancerFatalError(
                getI18N("enhancer.internal_error",//NOI18N
                        "Illegal argument: metaData == null"));//NOI18N
        }

        final JDOMetaData meta
            = new JDOMetaDataModelImpl(metaData,
                                       env.getOutputWriter());
        init(meta, settings, out, err);
    
Methods Summary
public booleanenhanceClassFile(java.io.InputStream inByteCode, OutputStreamWrapper outByteCode)
Enhances a given class according to the JDO meta-data.

        env.messageNL("FilterEnhancer: enhancing classfile ...");//NOI18N

        // reset environment to clear class map etc.
        env.reset();

        // enhance class file; check Exceptions
        final boolean changed;
        try {
            changed = enhanceClassFile1(inByteCode, outByteCode);
        } catch (UserException ex) {
            // note: catch UserException before RuntimeException

            // reset environment to clear class map etc.
            env.reset();
            //@olsen: support for I18N
            throw new EnhancerUserException(
                getI18N("enhancer.error",//NOI18N
                        ex.getMessage()),
                ex);
        } catch (RuntimeException ex) {
            // note: catch UserException before RuntimeException

            // reset environment to clear class map etc.
            env.reset();
            //@olsen: support for I18N
            ex.printStackTrace ();
            throw new EnhancerFatalError(
                getI18N("enhancer.internal_error",//NOI18N
                        ex.getMessage()),
                ex);
        }

        env.messageNL(changed
                      ? "FilterEnhancer: classfile enhanced successfully."//NOI18N
                      : "FilterEnhancer: classfile not changed.");//NOI18N
        return changed;
    
public booleanenhanceClassFile(java.io.InputStream in, java.io.OutputStream out)


        return enhanceClassFile (in, new OutputStreamWrapper (out));

    
private booleanenhanceClassFile1(java.io.InputStream inByteCode, OutputStreamWrapper outByteCode)
Enhances a given class according to the JDO meta-data.

        // check arguments
        affirm(inByteCode, "Illegal argument: inByteCode == null.");//NOI18N
        affirm(outByteCode, "Illegal argument: outByteCode == null.");//NOI18N

        // parse class
        final ClassFileSource cfs;
        final ClassFile cf;
        final ClassControl cc;
        try {
            // create class file source
            cfs = new ClassFileSource(null, inByteCode);

            // create class file
            final DataInputStream dis = cfs.classFileContents();
            cf = new ClassFile(dis);
//@lars: do not close the input stream
//            dis.close();

            // create class control
            cc = new ClassControl(cfs, cf, env);
            env.addClass(cc);

            // get real class name
            final String className = cc.className();
            cfs.setExpectedClassName(className);
        } catch (IOException ex) {
            //@olsen: support for I18N
            throw new UserException(
                getI18N("enhancer.io_error_while_reading_stream"),//NOI18N
                ex);
        } catch (ClassFormatError ex) {
            //@olsen: support for I18N
            throw new UserException(
                getI18N("enhancer.class_format_error"),//NOI18N
                ex);
        }

        // enhance class
        econtrol.modifyClasses();
        if (env.errorCount() > 0) {
            // retrieve error messages
            env.getErrorWriter ().flush ();
            /*
            final String str = errString.getBuffer().toString();

            // reset env's error writer
            errString = new StringWriter();
            err = new PrintWriter(errString, true);
            env.setErrorWriter(err);
            */

            //@olsen: support for I18N
            throw new UserException(env.getLastErrorMessage ());
        }

        // write class
        boolean changed = (cc.updated() && cc.filterRequired());
        try {
            if (changed)
            {
                env.message("writing enhanced class " + cc.userClassName()//NOI18N
                            + " to output stream");//NOI18N
            }
            else
            {
                env.message("no changes on class " + cc.userClassName());
            }
            outByteCode.setClassName (cc.userClassName ());
            final DataOutputStream dos = new DataOutputStream(outByteCode.getStream ());
            cf.write(dos);
            dos.flush();
        } catch (IOException ex) {
            //@olsen: support for I18N
            throw new UserException(
                getI18N("enhancer.io_error_while_writing_stream"),//NOI18N
                ex);
        }
        return changed;
    
protected voidinit(com.sun.jdo.api.persistence.enhancer.meta.JDOMetaData metaData, java.util.Properties settings, java.io.PrintWriter out, java.io.PrintWriter err)
Initializes an instance of a JDO enhancer.

param
metaData the JDO meta-data object
param
settings enhancement properties
param
out standard ouput stream for the enhancer


//    private StringWriter errString = new StringWriter();
//    private PrintWriter err = new PrintWriter(errString, true);

                                  
       
                          
                         
                         
          
    
        if (metaData == null) {
            //@olsen: support for I18N
            throw new EnhancerFatalError(
                getI18N("enhancer.internal_error",//NOI18N
                        "Illegal argument: metaData == null"));//NOI18N
        }

        env.setJDOMetaData(metaData);

        // set verbose level
        if  (err != null)
        {
            env.setErrorWriter(err);
        }
        if  (out != null)
        {
            env.setOutputWriter(out);
        }
        final String verboseLevel
            = (settings == null ? null : settings.getProperty(VERBOSE_LEVEL));
        if (VERBOSE_LEVEL_QUIET.equals(verboseLevel)) {
            env.setVerbose(false);
            env.setQuiet(true);
        } else if (VERBOSE_LEVEL_WARN.equals(verboseLevel)) {
            env.setVerbose(false);
            env.setQuiet(false);
        } else if (VERBOSE_LEVEL_VERBOSE.equals(verboseLevel)) {
            env.setVerbose(true);
            env.setQuiet(false);
        } else if (VERBOSE_LEVEL_DEBUG.equals(verboseLevel)) {
            env.setVerbose(true);
            env.setQuiet(false);
        } else {
            env.setVerbose(false);
            env.setQuiet(false);
        }

        //@olsen: force settings
        env.setNoOptimization(true);
        env.messageNL("FilterEnhancer: forced settings: -noopt");//NOI18N