Beispiel #1
0
  /**
   * Sets the attribute name to value.
   *
   * @param name
   * @param value
   */
  public void __setattr__(String name, PyObject value) {

    if ("destinationDataHandler".equals(name)) {
      this.destDH = (Class) value.__tojava__(Class.class);
    } else if ("sourceDataHandler".equals(name)) {
      this.sourceDH = (Class) value.__tojava__(Class.class);
    } else if ("batchsize".equals(name)) {
      this.batchsize = ((Number) value.__tojava__(Number.class)).intValue();
    } else if ("queuesize".equals(name)) {
      this.queuesize = ((Number) value.__tojava__(Number.class)).intValue();
    } else {
      super.__setattr__(name, value);
    }
  }
Beispiel #2
0
 BigInteger get_ulong(PyObject value) {
   if (value instanceof PyLong) {
     BigInteger v = (BigInteger) value.__tojava__(BigInteger.class);
     if (v.compareTo(PyLong.MAX_ULONG) > 0) {
       throw StructError("unsigned long int too long to convert");
     }
     return v;
   } else return BigInteger.valueOf(get_int(value));
 }
Beispiel #3
0
 long get_long(PyObject value) {
   if (value instanceof PyLong) {
     Object v = value.__tojava__(Long.TYPE);
     if (v == Py.NoConversion) {
       throw StructError("long int too long to convert");
     }
     return ((Long) v).longValue();
   } else return get_int(value);
 }
Beispiel #4
0
  public PyObject __call__(PyObject arga, PyObject argb, PyObject argc) {

    BCP bcp = (BCP) __self__;

    switch (index) {
      case 0:
        String table = (String) arga.__tojava__(String.class);

        if (table == null) {
          throw Py.ValueError(zxJDBC.getString("invalidTableName"));
        }

        String where = (String) argb.__tojava__(String.class);
        PyObject count = bcp.bcp(table, where, argc, Py.None, Py.None, null, Py.None);

        return count;

      default:
        throw info.unexpectedCall(3, false);
    }
  }
  /** Provide functionality for Oracle specific types, such as ROWID. */
  public void setJDBCObject(PreparedStatement stmt, int index, PyObject object, int type)
      throws SQLException {

    if (DataHandler.checkNull(stmt, index, object, type)) {
      return;
    }

    switch (type) {
      case OracleTypes.ROWID:
        stmt.setString(index, (String) object.__tojava__(String.class));
        break;

      case Types.DECIMAL:

        // Oracle is annoying
        Object input = object.__tojava__(Double.class);

        if (input != Py.NoConversion) {
          stmt.setDouble(index, ((Double) input).doubleValue());

          break;
        }

        super.setJDBCObject(stmt, index, object, type);
        break;

      case Types.NUMERIC:
        super.setJDBCObject(stmt, index, object, Types.DOUBLE);
        break;

      case Types.BLOB:
      case Types.CLOB:
        Integer[] vals = {new Integer(index), new Integer(type)};
        String msg = zxJDBC.getString("errorSettingIndex", vals);

        throw new SQLException(msg);
      default:
        super.setJDBCObject(stmt, index, object, type);
    }
  }
  private void initFactory() {
    try {
      String theScript =
          Utils.readInputStream(FormatterFactory.class.getResourceAsStream("formatter.py"));
      itsInterpreter = new PythonInterpreter();
      itsInterpreter.setLocals(new PyStringMap());
      itsInterpreter.exec(theScript);

      PyObject f = itsInterpreter.get("factory");
      itsFactory = (IPyFormatterFactory) f.__tojava__(IPyFormatterFactory.class);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
 public Object __tojava__(Class c) {
   // If we are not being asked by the "default" conversion to java, then
   // we can provide this as the result, as long as it is a instance of the
   // specified class. Without this, derived.__tojava__(PyObject.class)
   // would broke. (And that's not pure speculation: PyReflectedFunction's
   // ReflectedArgs asks for things like that).
   if ((c != Object.class) && (c != Serializable.class) && (c.isInstance(this))) {
     return this;
   }
   // Otherwise, we call the derived __tojava__, if it exists:
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__tojava__");
   if (impl != null) {
     PyObject delegate = impl.__get__(this, self_type).__call__(Py.java2py(c));
     if (delegate != this) return delegate.__tojava__(Object.class);
   }
   return super.__tojava__(c);
 }
  /** {@inheritDoc} */
  public Object getScriptedObject(ScriptSource scriptSourceLocator, Class[] scriptInterfaces)
      throws IOException, ScriptCompilationException {
    // TODO: how to do this when running under Tomcat?
    ContextHandler handler = WebAppContext.getCurrentWebAppContext();
    String basePath = "";
    if (handler != null) {
      File root = handler.getBaseResource().getFile();
      if (root != null && root.exists()) {
        basePath = root.getAbsolutePath() + File.separator + "WEB-INF" + File.separator;
      }
    }

    String strScript = scriptSourceLocator.getScriptAsString();
    if (scriptInterfaces.length > 0) {
      try {
        PySystemState state = new PySystemState();
        if (!"".equals(basePath)) {
          // Add webapp paths that can contain classes and .jar files to python search path
          state.path.insert(0, Py.newString(basePath + "classes"));
          File jarRoot = new File(basePath + "lib");
          if (jarRoot.exists()) {
            for (String filename :
                jarRoot.list(
                    new FilenameFilter() {
                      public boolean accept(File dir, String name) {
                        return (name.endsWith(".jar"));
                      }
                    })) {
              state.path.insert(1, Py.newString(basePath + "lib" + File.separator + filename));
            }
          }
        }
        PythonInterpreter interp = new PythonInterpreter(null, state);
        interp.exec(strScript);
        PyObject getInstance = interp.get("getInstance");
        if (!(getInstance instanceof PyFunction)) {
          throw new ScriptCompilationException("\"getInstance\" is not a function.");
        }
        PyObject _this;
        if (arguments == null) {
          _this = ((PyFunction) getInstance).__call__();
        } else {
          PyObject[] args = new PyObject[arguments.length];
          for (int i = 0; i < arguments.length; i++) {
            args[i] = new PyJavaInstance(arguments[i]);
          }
          _this = ((PyFunction) getInstance).__call__(args);
        }
        return _this.__tojava__(scriptInterfaces[0]);
      } catch (Exception ex) {
        logger.error("Error while loading script.", ex);
        if (ex instanceof IOException) {
          // Raise to caller
          throw (IOException) ex;
        } else if (ex instanceof ScriptCompilationException) {
          // Raise to caller
          throw (ScriptCompilationException) ex;
        }

        throw new ScriptCompilationException(ex.getMessage());
      }
    }
    logger.error("No scriptInterfaces provided.");
    return null;
  }
 public static Object unwrap(PyObject arg) {
   return arg.__tojava__(Object.class);
 }