hammant 01/10/31 04:43:23 Added: apps/db/src/java/org/apache/avalon/db/bcel/actions AbstractBCELAction.java BCELCreateTable.java BCELInsertSingleValue.java apps/db/src/java/org/apache/avalon/db/bcel/parser BCELSQLParser.java GeneratedObjectClassLoader.java Log: New bcel package Revision Changes Path 1.1 jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/bcel/actions/AbstractBCELAction.java Index: AbstractBCELAction.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE file. */ package org.apache.avalon.db.bcel.actions; import org.apache.avalon.db.services.DatabasePersistor; import org.apache.avalon.db.bcel.parser.GeneratedObjectClassLoader; import org.apache.avalon.db.bcel.parser.BCELSQLParser; public class AbstractBCELAction { protected DatabasePersistor mDatabasePersistor; protected BCELSQLParser mBCELSQLParser; public void setDatabasePersistor(DatabasePersistor databasePersistor) { mDatabasePersistor = databasePersistor; } public void setBCELSQLParser(BCELSQLParser bcelSQLParser) { mBCELSQLParser = bcelSQLParser; } } 1.1 jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/bcel/actions/BCELCreateTable.java Index: BCELCreateTable.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE file. */ package org.apache.avalon.db.bcel.actions; import org.apache.avalon.db.actions.CreateTable; import org.apache.avalon.db.actions.ActionException; import org.apache.avalon.db.services.DatabasePersistor; import org.apache.avalon.db.data.Table; import org.apache.bcel.classfile.JavaClass; /** * Class BCELCreateTable * * * @author Paul Hammant <a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a> * @version $Revision: 1.1 $ */ public class BCELCreateTable extends AbstractBCELAction implements CreateTable { private JavaClass mPendingClass; private String mTableName; private int mRecordCount; /** * Constructor BCELCreateTable * * * @param pendingClass * @param tableName * */ public BCELCreateTable(JavaClass pendingClass, String tableName) { mPendingClass = pendingClass; mTableName = tableName; } /** * Method execute * * */ public void execute() throws ActionException { if (mDatabasePersistor.tableExists(mTableName)) { throw new ActionException("Table " + mTableName + " Already Exists"); } byte[] bytes = mPendingClass.getBytes(); mBCELSQLParser.addGeneratedClass(mTableName, bytes); Table table = (Table) mBCELSQLParser.getGeneratedInstance(mTableName); mDatabasePersistor.addTable(mTableName, table); } public int getRecordCount() { return mRecordCount; } } 1.1 jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/bcel/actions/BCELInsertSingleValue.java Index: BCELInsertSingleValue.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE file. */ package org.apache.avalon.db.bcel.actions; import org.apache.avalon.db.actions.Insert; import org.apache.avalon.db.data.Table; import org.apache.avalon.db.services.DatabasePersistor; import org.apache.bcel.classfile.JavaClass; import java.util.Vector; import java.util.Iterator; import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; /** * Class BCELInsertSingleValue * * * @author Paul Hammant <a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a> * @version $Revision: 1.1 $ */ public class BCELInsertSingleValue extends AbstractBCELAction implements Insert { private Vector mColumnNames; private Vector mColumnValues; private Table mTable; /** * Constructor BCELInsertSingleValue * * * @param table * @param columnNames * @param columnValues * */ public BCELInsertSingleValue(Table table, Vector columnNames, Vector columnValues) { mTable = table; mColumnNames = columnNames; mColumnValues = columnValues; } public void setDatabasePersistor(DatabasePersistor databasePersistor) { mDatabasePersistor = databasePersistor; } /** * Method execute * * */ public void execute() { StringBuffer sb = new StringBuffer(); Iterator it = mColumnNames.iterator(); Iterator it2 = mColumnValues.iterator(); //repeated setxxxxx(yyyyyy); try { while (it.hasNext()) { String name = (String) it.next(); Object value = it2.next(); Method meth = mTable.getClass().getDeclaredMethod("set" + name, new Class[]{ value.getClass() }); meth.invoke(mTable, new Object[]{ value }); } } catch (NoSuchMethodException nsme) { //TODO throw System.out.println("nsme " + nsme.getMessage()); } catch (IllegalAccessException iae) { //TODO System.out.println("nsme " + iae.getMessage()); } catch (InvocationTargetException ite) { //TODO System.out.println("nsme " + ite.getMessage()); } } } 1.1 jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/bcel/parser/BCELSQLParser.java Index: BCELSQLParser.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE file. */ package org.apache.avalon.db.bcel.parser; import org.apache.avalon.db.services.SQLParser; import org.apache.avalon.db.services.DatabasePersistor; import org.apache.avalon.db.actions.Action; import org.apache.avalon.db.actions.Select; import org.apache.avalon.db.actions.CreateTable; import org.apache.avalon.db.actions.Insert; import org.apache.avalon.db.transport.Request; import org.apache.avalon.db.transport.SelectRequest; import org.apache.avalon.db.transport.CreateTableRequest; import org.apache.avalon.db.transport.InsertRequest; import org.apache.avalon.db.bcel.actions.BCELCreateTable; import org.apache.avalon.db.bcel.actions.BCELInsertSingleValue; import org.apache.avalon.db.data.Table; import org.apache.avalon.db.data.Column; import org.apache.avalon.db.data.impl.DefaultColumn; import org.apache.avalon.phoenix.Block; import org.apache.avalon.framework.context.Contextualizable; import org.apache.avalon.framework.context.Context; import org.apache.avalon.framework.context.ContextException; import org.apache.avalon.framework.component.Composable; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.component.ComponentException; import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.ConfigurationException; import org.apache.avalon.framework.activity.Initializable; import org.apache.avalon.framework.logger.AbstractLoggable; import org.apache.bcel.Constants; import org.apache.bcel.generic.*; import org.apache.bcel.classfile.Field; import java.util.StringTokenizer; import java.util.Vector; import java.util.HashMap; /** * Class BCELSQLParser * * * @author Paul Hammant <a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a> * @version $Revision: 1.1 $ */ public class BCELSQLParser extends AbstractLoggable implements Block, SQLParser, Contextualizable, Composable, Configurable, Initializable { private DatabasePersistor mDatabasePersistor; private GeneratedObjectClassLoader mGenClassLoader = new GeneratedObjectClassLoader(); private static int mGenObjSeq; private HashMap mGeneratedObjTranslation = new HashMap(); /** * Method createAction * * * @param request * * @return * */ public Action createAction(Request request) { return null; } /** * Method contextualize * * * @param context * * @throws ContextException * */ public void contextualize(Context context) throws ContextException {} /** * Method compose * * * @param componentManager * * @throws ComponentException * */ public void compose(ComponentManager componentManager) throws ComponentException { mDatabasePersistor = (DatabasePersistor) componentManager.lookup(DatabasePersistor.class.getName()); } /** * Method configure * * * @param configuration * * @throws ConfigurationException * */ public void configure(Configuration configuration) throws ConfigurationException {} /** * Method initialize * * * @throws Exception * */ public void initialize() throws Exception {} /** * Method createSelectAction * * * @param request * * @return * */ public Select createSelectAction(SelectRequest request) { return null; } private String getWordOne(String str) { int ix0 = str.indexOf(' '); if (ix0 == -1) { ix0 = str.length(); } return str.substring(0, ix0); } private String getWordTwo(String str) { int ix0 = str.indexOf(' '); int ix1 = str.indexOf(' ', ix0 + 1); if (ix1 == -1) { ix1 = str.length(); } return str.substring(ix0 + 1, ix1); } private String getWordThree(String str) { int ix0 = str.indexOf(' '); int ix1 = str.indexOf(' ', ix0 + 1); int ix2 = str.indexOf(' ', ix1 + 1); if (ix2 == -1) { ix2 = str.length(); } return str.substring(ix1 + 1, ix2); } private String getWordFour(String str) { int ix0 = str.indexOf(' '); int ix1 = str.indexOf(' ', ix0 + 1); int ix2 = str.indexOf(' ', ix1 + 1); int ix3 = str.indexOf(' ', ix2 + 1); if (ix3 == -1) { ix3 = str.length(); } return str.substring(ix2 + 1, ix3); } private String getBracketedExpr(String str) { int ix0 = str.indexOf('('); int ix1 = str.lastIndexOf(')'); System.out.println("str=" + str); System.out.println("ix0=" + ix0); System.out.println("ix1=" + ix1); return str.substring(ix0 + 1, ix1); } /** * Method createCreateTableAction * * * @param request * * @return * */ public CreateTable createCreateTableAction(CreateTableRequest request) { String table = getWordThree(request.getSql()); String values = getBracketedExpr(request.getSql()); StringTokenizer st = new StringTokenizer(values, ","); ClassGen cg = new ClassGen(table + "Row", "org.apache.avalon.db.data.impl.AbstractTableRow", "<SQL Generated>", Constants.ACC_PUBLIC | Constants.ACC_SUPER, null); ConstantPoolGen cp = cg.getConstantPool(); Vector columns = new Vector(); while (st.hasMoreTokens()) { String fieldStr = st.nextToken(); StringTokenizer st2 = new StringTokenizer(fieldStr, " "); System.out.println("FieldStr=" + fieldStr); String fieldName = st2.nextToken(); String sqlFieldType = st2.nextToken(); Type fieldType = getType(sqlFieldType); Column column = new DefaultColumn(fieldName, sqlFieldType, fieldType.toString()); columns.add(column); createField(cp, cg, fieldType, fieldName); createSetter(cp, cg, fieldType, fieldName, table); } //TODO need to create constrcutor suitable for superclass. try { cg.getJavaClass().dump(table + "Row.class"); } catch (java.io.IOException e) { System.err.println(e); } CreateTable action = new BCELCreateTable(cg.getJavaClass(), table); action.setDatabasePersistor(mDatabasePersistor); return action; } private Type getType(String type) { System.out.println("Type=(" + type + ")"); if (type.equals("varchar")) { return Type.STRING; } else if (type.equals("char")) { return Type.STRING; } return Type.VOID; } private void createField(ConstantPoolGen cp, ClassGen cg, Type fieldType, String name) { System.out.println("fn=" + name); System.out.println("Type2=" + fieldType); FieldGen fg = new FieldGen(Constants.ACC_PUBLIC, fieldType, name, cp); cg.addField(fg.getField()); } private void createSetter(ConstantPoolGen cp, ClassGen cg, Type fieldType, String name, String table) { InstructionList il = new InstructionList(); MethodGen mg = new MethodGen(Constants.ACC_PUBLIC, // access flags Type.VOID, // return type new Type[]{ fieldType }, new String[]{ "value" }, // arg names "set" + name, table, // method, class il, cp); mg.stripAttributes(true); mg.setMaxStack(); mg.setMaxLocals(); cg.addMethod(mg.getMethod()); } /** * Method createInsertAction * * * @param request * * @return * */ public Insert createInsertAction(InsertRequest request) { String tableName = getWordThree(request.getSql()); Table table = mDatabasePersistor.getTable(tableName); String type = getWordFour(request.getSql()); if (type.startsWith("(")) { //return createNamedColumnsInsert(table); } else if (type.startsWith("values")) { return createAllValuesInsert(table, request.getSql()); } else if (type.startsWith("select")) { //return createSelectInsert(table); } return null; } //private Insert createNamedColumnsInsert(Table table, String sql ) { //} private Insert createAllValuesInsert(Table table, String sql) { String values = getBracketedExpr(sql); StringTokenizer st = new StringTokenizer(values, ","); Vector valuesVec = new Vector(); while (st.hasMoreTokens()) { valuesVec.add(st.nextToken().trim()); } //TODO get columns from table. return new BCELInsertSingleValue(table, null, valuesVec); } //private Insert createSelectInsert(Table table, String sql) { //} public void addGeneratedClass(String name, byte[] bytes) { mGenClassLoader.addGeneratedClass(name, bytes); } public Object getGeneratedInstance(String name) { return mGenClassLoader.getGeneratedInstance(name); } /** * Method main * * * @param args * */ public static void main(String[] args) { BCELSQLParser parser = new BCELSQLParser(); parser.createCreateTableAction( new CreateTableRequest("CREATE TABLE Fred (Wilma varchar, Barney varchar)")); } } 1.1 jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/bcel/parser/GeneratedObjectClassLoader.java Index: GeneratedObjectClassLoader.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE file. */ package org.apache.avalon.db.bcel.parser; import java.net.URLClassLoader; import java.util.HashMap; public class GeneratedObjectClassLoader extends ClassLoader { private HashMap mGeneratedClasses = new HashMap(); public Class findClass(String name) { Holder holder = (Holder) mGeneratedClasses.get(name); byte[] bytes = holder.getBytes(); return defineClass(name, bytes, 0, bytes.length); } void addGeneratedClass(String name, byte[] bytes) { mGeneratedClasses.put(name, new Holder(bytes)); } Object getGeneratedInstance(String name) { Class clazz = findClass(name); try { return clazz.newInstance(); } catch (Throwable t) { return null; } } private class Holder { private byte[] ba; Holder(byte[] ba) { this.ba = ba; } byte[] getBytes() { return ba; } } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>