Exemplo n.º 1
0
  private static void installJar(String urlString, String jarName, boolean deploy, byte[] image)
      throws SQLException {
    assertJarName(jarName);

    if (getJarId(jarName, null) >= 0)
      throw new SQLException("A jar named '" + jarName + "' already exists", "46002");

    PreparedStatement stmt =
        SQLUtils.getDefaultConnection()
            .prepareStatement(
                "INSERT INTO sqlj.jar_repository(jarName, jarOrigin, jarOwner) VALUES(?, ?, ?)");
    try {
      stmt.setString(1, jarName);
      stmt.setString(2, urlString);
      stmt.setString(3, AclId.getSessionUser().getName());
      if (stmt.executeUpdate() != 1)
        throw new SQLException("Jar repository insert did not insert 1 row");
    } finally {
      SQLUtils.close(stmt);
    }

    AclId[] ownerRet = new AclId[1];
    int jarId = getJarId(jarName, ownerRet);
    if (jarId < 0) throw new SQLException("Unable to obtain id of '" + jarName + "'");

    if (image == null) Backend.addClassImages(jarId, urlString);
    else {
      InputStream imageStream = new ByteArrayInputStream(image);
      addClassImages(jarId, imageStream, image.length);
    }
    Loader.clearSchemaLoaders();
    if (deploy) deployInstall(jarId, jarName);
  }
Exemplo n.º 2
0
  /**
   * Removes the jar named <code>jarName</code> from the database jar repository. Class path entries
   * that references this jar will also be removed (just the entry, not the whole path). This method
   * is exposed in SQL as <code>sqlj.remove_jar(VARCHAR, BOOLEAN)</code>.
   *
   * @param jarName The name by which the system referes this jar.
   * @param undeploy If set, execute remove commands found in the deployment descriptor of the jar.
   * @throws SQLException if the named jar cannot be found in the repository.
   */
  public static void removeJar(String jarName, boolean undeploy) throws SQLException {
    assertJarName(jarName);
    AclId[] ownerRet = new AclId[1];
    int jarId = getJarId(jarName, ownerRet);
    if (jarId < 0)
      throw new SQLException("No Jar named '" + jarName + "' is known to the system", "4600B");

    AclId user = AclId.getSessionUser();
    if (!(user.isSuperuser() || user.equals(ownerRet[0])))
      throw new SecurityException("Only super user or owner can remove a jar");

    if (undeploy) deployRemove(jarId, jarName);

    PreparedStatement stmt =
        SQLUtils.getDefaultConnection()
            .prepareStatement("DELETE FROM sqlj.jar_repository WHERE jarId = ?");
    try {
      stmt.setInt(1, jarId);
      if (stmt.executeUpdate() != 1)
        throw new SQLException("Jar repository update did not update 1 row");
    } finally {
      SQLUtils.close(stmt);
    }
    Loader.clearSchemaLoaders();
  }
Exemplo n.º 3
0
  /**
   * Defines the mapping between an SQL type and a Java class.
   *
   * @param sqlTypeName The name of the SQL type. The name can be qualified with a schema
   *     (namespace). If the schema is omitted, it will be resolved according to the current setting
   *     of the <code>search_path</code>.
   * @param javaClassName The name of the class. The class must be found in the classpath in effect
   *     for the current schema
   * @throws SQLException
   */
  public static void addTypeMapping(String sqlTypeName, String javaClassName) throws SQLException {
    PreparedStatement stmt = null;
    try {
      ClassLoader loader = Loader.getCurrentLoader();
      Class cls = loader.loadClass(javaClassName);
      if (!SQLData.class.isAssignableFrom(cls))
        throw new SQLException("Class " + javaClassName + " does not implement java.sql.SQLData");

      sqlTypeName = getFullSqlName(sqlTypeName);
      stmt =
          SQLUtils.getDefaultConnection()
              .prepareStatement("INSERT INTO sqlj.typemap_entry(javaName, sqlName) VALUES(?,?)");
      stmt.setString(1, javaClassName);
      stmt.setString(2, sqlTypeName);
      stmt.executeUpdate();
    } catch (ClassNotFoundException e) {
      throw new SQLException("No such class: " + javaClassName);
    } finally {
      SQLUtils.close(stmt);
    }
    Loader.clearSchemaLoaders();
  }
Exemplo n.º 4
0
 /**
  * Drops the mapping between an SQL type and a Java class.
  *
  * @param sqlTypeName The name of the SQL type. The name can be qualified with a schema
  *     (namespace). If the schema is omitted, it will be resolved according to the current setting
  *     of the <code>search_path</code>.
  * @throws SQLException
  */
 public static void dropTypeMapping(String sqlTypeName) throws SQLException {
   PreparedStatement stmt = null;
   try {
     sqlTypeName = getFullSqlName(sqlTypeName);
     stmt =
         SQLUtils.getDefaultConnection()
             .prepareStatement("DELETE FROM sqlj.typemap_entry WHERE sqlName = ?");
     stmt.setString(1, sqlTypeName);
     stmt.executeUpdate();
   } finally {
     SQLUtils.close(stmt);
   }
   Loader.clearSchemaLoaders();
 }
Exemplo n.º 5
0
  private static void replaceJar(String urlString, String jarName, boolean redeploy, byte[] image)
      throws SQLException {
    AclId[] ownerRet = new AclId[1];
    int jarId = getJarId(jarName, ownerRet);
    if (jarId < 0)
      throw new SQLException("No Jar named '" + jarName + "' is known to the system", "4600A");

    AclId user = AclId.getSessionUser();
    if (!(user.isSuperuser() || user.equals(ownerRet[0])))
      throw new SecurityException("Only super user or owner can replace a jar");

    if (redeploy) deployRemove(jarId, jarName);

    PreparedStatement stmt =
        SQLUtils.getDefaultConnection()
            .prepareStatement(
                "UPDATE sqlj.jar_repository "
                    + "SET jarOrigin = ?, jarOwner = ?, jarManifest = NULL "
                    + "WHERE jarId = ?");
    try {
      stmt.setString(1, urlString);
      stmt.setString(2, user.getName());
      stmt.setInt(3, jarId);
      if (stmt.executeUpdate() != 1)
        throw new SQLException("Jar repository update did not update 1 row");
    } finally {
      SQLUtils.close(stmt);
    }

    stmt =
        SQLUtils.getDefaultConnection()
            .prepareStatement("DELETE FROM sqlj.jar_entry WHERE jarId = ?");
    try {
      stmt.setInt(1, jarId);
      stmt.executeUpdate();
    } finally {
      SQLUtils.close(stmt);
    }
    if (image == null) Backend.addClassImages(jarId, urlString);
    else {
      InputStream imageStream = new ByteArrayInputStream(image);
      addClassImages(jarId, imageStream, image.length);
    }
    Loader.clearSchemaLoaders();
    if (redeploy) deployInstall(jarId, jarName);
  }
Exemplo n.º 6
0
  /**
   * Define the class path to use for Java functions, triggers, and procedures that are created in
   * the schema named <code>schemaName</code> This method is exposed in SQL as <code>
   * sqlj.set_classpath(VARCHAR, VARCHAR)</code>.
   *
   * @param schemaName Name of the schema for which this path is valid.
   * @param path Colon separated list of names. Each name must denote the name of a jar that is
   *     present in the jar repository.
   * @throws SQLException If no schema can be found with the givene name, or if one or several names
   *     of the path denotes a nonexistant jar file.
   */
  public static void setClassPath(String schemaName, String path) throws SQLException {
    if (schemaName == null || schemaName.length() == 0) schemaName = "public";

    if ("public".equals(schemaName)) {
      if (!AclId.getSessionUser().isSuperuser())
        throw new SQLException(
            "Permission denied. Only a super user can set the classpath of the public schema");
    } else {
      schemaName = schemaName.toLowerCase();
      Oid schemaId = getSchemaId(schemaName);
      if (schemaId == null) throw new SQLException("No such schema: " + schemaName);
      if (!AclId.getSessionUser().hasSchemaCreatePermission(schemaId))
        throw new SQLException(
            "Permission denied. User must have create permission on the target schema in order to set the classpath");
    }

    PreparedStatement stmt;
    ArrayList entries = null;
    if (path != null && path.length() > 0) {
      // Collect and verify that all entries in the path represents a
      // valid jar
      //
      entries = new ArrayList();
      stmt =
          SQLUtils.getDefaultConnection()
              .prepareStatement("SELECT jarId FROM sqlj.jar_repository WHERE jarName = ?");
      try {
        for (; ; ) {
          int colon = path.indexOf(':');
          String jarName;
          if (colon >= 0) {
            jarName = path.substring(0, colon);
            path = path.substring(colon + 1);
          } else jarName = path;

          int jarId = getJarId(stmt, jarName, null);
          if (jarId < 0) throw new SQLException("No such jar: " + jarName);

          entries.add(new Integer(jarId));
          if (colon < 0) break;
        }
      } finally {
        SQLUtils.close(stmt);
      }
    }

    // Delete the old classpath
    //
    stmt =
        SQLUtils.getDefaultConnection()
            .prepareStatement("DELETE FROM sqlj.classpath_entry WHERE schemaName = ?");
    try {
      stmt.setString(1, schemaName);
      stmt.executeUpdate();
    } finally {
      SQLUtils.close(stmt);
    }

    if (entries != null) {
      // Insert the new path.
      //
      stmt =
          SQLUtils.getDefaultConnection()
              .prepareStatement(
                  "INSERT INTO sqlj.classpath_entry(schemaName, ordinal, jarId) VALUES(?, ?, ?)");
      try {
        int top = entries.size();
        for (int idx = 0; idx < top; ++idx) {
          int jarId = ((Integer) entries.get(idx)).intValue();
          stmt.setString(1, schemaName);
          stmt.setInt(2, idx + 1);
          stmt.setInt(3, jarId);
          stmt.executeUpdate();
        }
      } finally {
        SQLUtils.close(stmt);
      }
    }
    Loader.clearSchemaLoaders();
  }