FileDocCategorySizeDatePackage
EJBQLC.javaAPI DocGlassfish v2 API9215Fri May 04 22:34:50 BST 2007com.sun.jdo.spi.persistence.support.ejb.ejbqlc

EJBQLC

public class EJBQLC extends Object
This class is the driver of the EJBQL compiler. It controls the compiler passes: syntax analysis, semantic analysis and generation of the JDOQL query.

A EJBQLC instance is able to compile multiple EJBQL queries as long as they come from the same deployement descriptor. The class uses the model instance passed to the constructor to access any meta data from the deployement descriptor. Method {@link #compile} compiles a single EJBQL query string together with the java.lang.reflect.Method instance of the corresponding finder/selector method. The result is a JDOQLElements instance, that can be used to construct a JDOQL query instance.

author
Michael Bouschen
author
Shing Wai Chan

Fields Summary
protected com.sun.jdo.api.persistence.model.Model
model
Meta data access.
protected com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper
nameMapper
Name mapping EJB <-> JDO.
protected EJBQLAST
ast
The intermediate form of the EJBQL query string.
private static com.sun.jdo.spi.persistence.utility.logging.Logger
logger
The logger
protected static final ResourceBundle
msgs
I18N support.
public static final String
SIGNATURE
Signature with CVS keyword substitution for identifying the generated code
Constructors Summary
public EJBQLC(com.sun.jdo.api.persistence.model.Model model, com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper nameMapper)
Constructor.

param
model meta data access.
param
nameMapper name mapping EJB <-> JDO.

 //NOI18N
    
                        
        
    
        this.model = model;
        this.nameMapper = nameMapper;
    
Methods Summary
public JDOQLElementscompile(java.lang.String ejbqlQuery, java.lang.reflect.Method method, int resultTypeMapping, boolean finderNotSelector, java.lang.String ejbName)
Compiles the specified query string for the specified finder/selector method.

param
ejbqlQuery the EJBQL query text
param
method the Method instance of the finder or selector
param
resultTypeMapping result-type-mapping element from the DD
param
finderNotSelector true indicates a finder, false a selector
param
ejbName the ejb name of the entity bean

        boolean finer = logger.isLoggable(Logger.FINER);
        boolean finest = logger.isLoggable(Logger.FINEST);
        if (method == null)
            ErrorMsg.fatal(I18NHelper.getMessage(msgs, 
                "ERR_MissingMethodInstance")); //NOI18N
        if ((ejbqlQuery == null) || ejbqlQuery.trim().length() == 0)
            ErrorMsg.error(I18NHelper.getMessage(msgs, 
                "EXC_MissingEjbqlQueryText", ejbName, //NOI18N
                getMethodSignature(method))); 
        if (finer) 
            logger.finer("LOG_EJBQLCCompile", ejbName, //NOI18N
                         getMethodSignature(method), ejbqlQuery);
                                    
        JDOQLElements result = null;
        TypeSupport typeSupport = new TypeSupport(model, nameMapper);
        ParameterSupport paramSupport = new ParameterSupport(method);
        String pass = null;

        try
        {
            // syntax analysis
            pass = "syntax analysis"; //NOI18N
            if (finer) logger.finer("LOG_EJBQLCStartPass", pass); //NOI18N
            EJBQLParser parser = createStringParser(ejbqlQuery);
            parser.query();
            ast = (EJBQLAST)parser.getAST();
            if (finest) logger.finest("LOG_EJBQLCDumpTree", ast.getTreeRepr("(AST)")); //NOI18N

            // semantic analysis
            pass = "semantic analysis"; //NOI18N
            if (finer) logger.finer("LOG_EJBQLCStartPass", pass); //NOI18N
            Semantic semantic = new Semantic();
            semantic.init(typeSupport, paramSupport, method, resultTypeMapping,
                          finderNotSelector, ejbName);
            semantic.setASTFactory(EJBQLASTFactory.getInstance());
            semantic.query(ast);
            ast = (EJBQLAST)semantic.getAST();
            if (finest) logger.finest("LOG_EJBQLCDumpTree", ast.getTreeRepr("(typed AST)")); //NOI18N

            // JDOQL code generation
            pass = "code generation"; //NOI18N
            if (finer) logger.finer("LOG_EJBQLCStartPass", pass); //NOI18N
            JDOQLCodeGeneration codeGen = new JDOQLCodeGeneration();
            codeGen.init(typeSupport, paramSupport);
            codeGen.setASTFactory(EJBQLASTFactory.getInstance());
            codeGen.query(ast);
            result = codeGen.getJDOQLElements();
            if (finer) logger.finer("LOG_EJBQLCResult", result.toString()); //NOI18N
        }
        catch (EJBQLException ex) {
            // add EJB name, finder/selector, EJBQL to error message.
            Object[] msgArgs = { ejbName, getMethodSignature(method), 
                                 ejbqlQuery, ex.getMessage() };
            ErrorMsg.error(I18NHelper.getMessage(msgs, 
                "EXC_InvalidEJBQLQuery", msgArgs)); //NOI18N
        }
        catch (Throwable t) {
            Object[] msgArgs = { ejbName, getMethodSignature(method), 
                                 ejbqlQuery, t.toString() };
            // log a SEVERE message with nested exception
            ErrorMsg.log(Logger.SEVERE, I18NHelper.getMessage(msgs,
                    "EXC_EJBQLQueryInternalError", msgArgs), t); //NOI18N
        }

        // return the JDOQLElements instance representing the elements 
        // of the JDOQL query.
        return result;
    
private EJBQLParsercreateStringParser(java.lang.String text)
Creates an ANTLR EJBQL parser reading a string.

        Reader in = new StringReader(text);
        EJBQLLexer lexer = new EJBQLLexer(in);
        TokenBuffer buffer = new TokenBuffer(lexer);
        EJBQLParser parser = new EJBQLParser(buffer);
        parser.setASTFactory(EJBQLASTFactory.getInstance());
        return parser;
    
private java.lang.StringgetMethodSignature(java.lang.reflect.Method m)
Returns the signature of a method w/o exceptions and modifiers as a string.

        if (m == null) 
            return ""; //NOI18N
        
        return m.getReturnType().getName() + ' " + m.getName() + 
            JavaClassWriterHelper.parenleft_ +
            JavaClassWriterHelper.getParameterTypesList(m) + 
            JavaClassWriterHelper.parenright_ ;