Search in sources :

Example 46 with KettlePluginException

use of org.pentaho.di.core.exception.KettlePluginException in project pentaho-kettle by pentaho.

the class AccessOutputMeta method getLayout.

public static final RowMetaInterface getLayout(Table table) throws SQLException, KettleStepException {
    RowMetaInterface row = new RowMeta();
    List<Column> columns = table.getColumns();
    for (int i = 0; i < columns.size(); i++) {
        Column column = columns.get(i);
        int valtype = ValueMetaInterface.TYPE_STRING;
        int length = -1;
        int precision = -1;
        int type = column.getType().getSQLType();
        switch(type) {
            case java.sql.Types.CHAR:
            case java.sql.Types.VARCHAR:
            case // Character Large Object
            java.sql.Types.LONGVARCHAR:
                valtype = ValueMetaInterface.TYPE_STRING;
                length = column.getLength();
                break;
            case java.sql.Types.CLOB:
                valtype = ValueMetaInterface.TYPE_STRING;
                length = DatabaseMeta.CLOB_LENGTH;
                break;
            case java.sql.Types.BIGINT:
                valtype = ValueMetaInterface.TYPE_INTEGER;
                // Max 9.223.372.036.854.775.807
                precision = 0;
                length = 15;
                break;
            case java.sql.Types.INTEGER:
                valtype = ValueMetaInterface.TYPE_INTEGER;
                // Max 2.147.483.647
                precision = 0;
                length = 9;
                break;
            case java.sql.Types.SMALLINT:
                valtype = ValueMetaInterface.TYPE_INTEGER;
                // Max 32.767
                precision = 0;
                length = 4;
                break;
            case java.sql.Types.TINYINT:
                valtype = ValueMetaInterface.TYPE_INTEGER;
                // Max 127
                precision = 0;
                length = 2;
                break;
            case java.sql.Types.DECIMAL:
            case java.sql.Types.DOUBLE:
            case java.sql.Types.FLOAT:
            case java.sql.Types.REAL:
            case java.sql.Types.NUMERIC:
                valtype = ValueMetaInterface.TYPE_NUMBER;
                length = column.getLength();
                precision = column.getPrecision();
                if (length >= 126) {
                    length = -1;
                }
                if (precision >= 126) {
                    precision = -1;
                }
                if (type == java.sql.Types.DOUBLE || type == java.sql.Types.FLOAT || type == java.sql.Types.REAL) {
                    if (precision == 0) {
                        // precision is obviously incorrect if the type if Double/Float/Real
                        precision = -1;
                    }
                } else {
                    if (precision == 0 && length < 18 && length > 0) {
                        // Among others Oracle is affected here.
                        valtype = ValueMetaInterface.TYPE_INTEGER;
                    }
                }
                if (length > 18 || precision > 18) {
                    valtype = ValueMetaInterface.TYPE_BIGNUMBER;
                }
                break;
            case java.sql.Types.DATE:
            case java.sql.Types.TIME:
            case java.sql.Types.TIMESTAMP:
                valtype = ValueMetaInterface.TYPE_DATE;
                break;
            case java.sql.Types.BOOLEAN:
            case java.sql.Types.BIT:
                valtype = ValueMetaInterface.TYPE_BOOLEAN;
                break;
            case java.sql.Types.BINARY:
            case java.sql.Types.BLOB:
            case java.sql.Types.VARBINARY:
            case java.sql.Types.LONGVARBINARY:
                valtype = ValueMetaInterface.TYPE_BINARY;
                break;
            default:
                valtype = ValueMetaInterface.TYPE_STRING;
                length = column.getLength();
                break;
        }
        ValueMetaInterface v;
        try {
            v = ValueMetaFactory.createValueMeta(column.getName(), valtype);
        } catch (KettlePluginException e) {
            throw new KettleStepException(e);
        }
        v.setLength(length, precision);
        row.addValueMeta(v);
    }
    return row;
}
Also used : KettlePluginException(org.pentaho.di.core.exception.KettlePluginException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) RowMeta(org.pentaho.di.core.row.RowMeta) Column(com.healthmarketscience.jackcess.Column) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 47 with KettlePluginException

use of org.pentaho.di.core.exception.KettlePluginException in project pentaho-kettle by pentaho.

the class PluginRegistryIT method testPluginRegistry.

public void testPluginRegistry() throws KettlePluginException {
    PluginRegistry registry = PluginRegistry.getInstance();
    assertNotNull("Registry singleton was not found!", registry);
    // PluginRegistry.init() may have already been called, our test path will be different
    // for each case. If it has not been called, try to register the type and plugin. If
    // it has been called, simply verify the plugin type and plugin have been registered.
    Class<? extends PluginTypeInterface> pluginTypeClass = StepPluginType.class;
    Object pluginClass = new TableInputMeta();
    List<PluginInterface> inputPluginsAtTestStart = registry.getPluginsByCategory(pluginTypeClass, PLUGIN_INPUT_CATEGORY);
    int numInputPluginsAtStart = inputPluginsAtTestStart.size();
    List<PluginInterface> outputPluginsAtTestStart = registry.getPluginsByCategory(pluginTypeClass, PLUGIN_OUTPUT_CATEGORY);
    int numOutputPluginsAtStart = outputPluginsAtTestStart.size();
    try {
        registry.getPluginType(pluginTypeClass);
    } catch (KettlePluginException kpe) {
        // Register a new plugin type...
        registry.registerPluginType(pluginTypeClass);
    }
    // See if the plugin is there...
    try {
        registry.getPluginType(pluginTypeClass);
    } catch (KettlePluginException kpe) {
        fail(pluginTypeClass.getName() + " expected in the PluginRegistry but was not found!");
    }
    PluginInterface plugin = registry.getPlugin(pluginTypeClass, pluginClass);
    Map<Class<?>, String> classMap = new HashMap<Class<?>, String>();
    PluginInterface tableInputPlugin = plugin;
    int numInputPluginsRegistered = 0;
    if (plugin == null) {
        // Register a single step plugin
        // 
        classMap.put(StepMetaInterface.class, "org.pentaho.di.trans.steps.tableinput.TableInputMeta");
        tableInputPlugin = new Plugin(new String[] { TABLE_INPUT_PLUGIN_ID }, pluginTypeClass, StepMetaInterface.class, PLUGIN_INPUT_CATEGORY, TABLE_INPUT_PLUGIN_NAME, TABLE_INPUT_PLUGIN_DESCRIPTION, TABLE_INPUT_PLUGIN_IMAGE_FILE_NAME, false, true, classMap, new ArrayList<String>(), // No error help file
        null, // pluginFolder
        null, // documentation URL
        null, // cases URL
        null, // forum URL
        null);
        registry.registerPlugin(pluginTypeClass, tableInputPlugin);
        numInputPluginsRegistered++;
    }
    // Verify the plugin has been registered
    PluginInterface verify = registry.getPlugin(pluginTypeClass, TABLE_INPUT_PLUGIN_ID);
    assertNotNull("A plugin was not found in the plugin registry", verify);
    assertEquals("A different plugin then expected was retrieved from the plugin registry", verify, tableInputPlugin);
    pluginClass = new TableOutputMeta();
    plugin = registry.getPlugin(pluginTypeClass, pluginClass);
    PluginInterface tableOutputPlugin = plugin;
    int numOutputPluginsRegistered = 0;
    if (plugin == null) {
        // Register a second step plugin
        // 
        classMap = new HashMap<Class<?>, String>();
        classMap.put(StepMetaInterface.class, "org.pentaho.di.trans.steps.tableoutput.TableOutputMeta");
        tableOutputPlugin = new Plugin(new String[] { TABLE_OUTPUT_PLUGIN_ID }, pluginTypeClass, StepMetaInterface.class, PLUGIN_OUTPUT_CATEGORY, TABLE_OUTPUT_PLUGIN_NAME, TABLE_OUTPUT_PLUGIN_DESCRIPTION, TABLE_OUTPUT_PLUGIN_IMAGE_FILE_NAME, false, true, classMap, new ArrayList<String>(), // No error help file
        null, // pluginFolder
        null, // documentation URL
        null, // cases URL
        null, // forum URL
        null);
        registry.registerPlugin(pluginTypeClass, tableOutputPlugin);
        numOutputPluginsRegistered++;
    }
    // Verify the plugin has been registered
    verify = registry.getPlugin(pluginTypeClass, TABLE_OUTPUT_PLUGIN_ID);
    assertNotNull("A plugin was not found in the plugin registry", verify);
    assertEquals("A different plugin then expected was retrieved from the plugin registry", verify, tableOutputPlugin);
    // Get a list by category...
    // 
    List<PluginInterface> inputPlugins = registry.getPluginsByCategory(pluginTypeClass, PLUGIN_INPUT_CATEGORY);
    assertEquals("Exactly one plugin expected in the step plugin input category", numInputPluginsAtStart + numInputPluginsRegistered, inputPlugins.size());
    assertTrue("Input plugins list should contain the table input step", inputPlugins.contains(tableInputPlugin));
    assertFalse("Input plugins list should not contain the table output step", inputPlugins.contains(tableOutputPlugin));
    List<PluginInterface> outputPlugins = registry.getPluginsByCategory(pluginTypeClass, PLUGIN_OUTPUT_CATEGORY);
    assertEquals("Exactly one plugin expected in the step plugin output category", numOutputPluginsAtStart + numOutputPluginsRegistered, outputPlugins.size());
    assertTrue("Output plugins list should contain the table output step", outputPlugins.contains(tableOutputPlugin));
    assertFalse("Output plugins list should not contain the table input step", outputPlugins.contains(tableInputPlugin));
    // List the categories...
    // 
    List<String> categories = registry.getCategories(pluginTypeClass);
    assertTrue("The input category was expected in the categories list", categories.contains(PLUGIN_INPUT_CATEGORY));
    assertTrue("The output category was expected in the categories list", categories.contains(PLUGIN_OUTPUT_CATEGORY));
    // Now have a little bit of class loading fun: load the main class of the plugin
    // 
    Object object = registry.loadClass(tableInputPlugin, StepMetaInterface.class);
    assertNotNull(object);
    // The same but now explicitly asking for the main class
    // 
    Object object2 = registry.loadClass(tableOutputPlugin, StepMetaInterface.class);
    assertNotNull(object2);
    try {
        registry.loadClass(tableInputPlugin, String.class);
        fail("A String class type can't be used when loading a step class");
    } catch (Exception e) {
    // OK!
    }
}
Also used : KettlePluginException(org.pentaho.di.core.exception.KettlePluginException) HashMap(java.util.HashMap) StepMetaInterface(org.pentaho.di.trans.step.StepMetaInterface) ArrayList(java.util.ArrayList) TableOutputMeta(org.pentaho.di.trans.steps.tableoutput.TableOutputMeta) TableInputMeta(org.pentaho.di.trans.steps.tableinput.TableInputMeta) KettlePluginException(org.pentaho.di.core.exception.KettlePluginException)

Aggregations

KettlePluginException (org.pentaho.di.core.exception.KettlePluginException)47 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)23 FileObject (org.apache.commons.vfs2.FileObject)12 Node (org.w3c.dom.Node)11 RowMeta (org.pentaho.di.core.row.RowMeta)10 ValueMetaNone (org.pentaho.di.core.row.value.ValueMetaNone)10 Document (org.w3c.dom.Document)10 ArrayList (java.util.ArrayList)9 KettleStepException (org.pentaho.di.core.exception.KettleStepException)9 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)9 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)9 PluginInterface (org.pentaho.di.core.plugins.PluginInterface)7 ValueMetaInteger (org.pentaho.di.core.row.value.ValueMetaInteger)6 Map (java.util.Map)4 KettleException (org.pentaho.di.core.exception.KettleException)4 PluginFolderInterface (org.pentaho.di.core.plugins.PluginFolderInterface)4 IOException (java.io.IOException)3 Annotation (java.lang.annotation.Annotation)3 URLClassLoader (java.net.URLClassLoader)3 HashMap (java.util.HashMap)3