Ejemplo n.º 1
1
 /*
  * create method, that read from a txt file and read the MySQL language and write to the database.
  */
 public void createDB() {
   try {
     BufferedReader br = new BufferedReader(new FileReader("db_schema.txt"));
     String line = null;
     StringBuilder sb = new StringBuilder();
     while ((line = br.readLine()) != null) {
       sb.append(line);
       if (sb.length() > 0
           && sb.charAt(sb.length() - 1) == ';') { // see if it is the end of one line of command
         statement.executeUpdate(sb.toString());
         sb = new StringBuilder();
       }
     }
     br.close();
   } catch (FileNotFoundException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   } catch (SQLException e) {
     e.printStackTrace();
   }
 }
  /** Returns true if this VM has acquired the right to run this TestTask */
  private static DUnitRun getDUnitRun() {
    if (dunitRun != null) {
      System.out.println("BBB dunitRun not null returning");
      return dunitRun;
    }

    try {
      SwarmBB.getBB().getSharedLock().lock();
      Integer runId = (Integer) SwarmBB.getBB().getSharedMap().get(SwarmBB.RUN_ID);
      System.out.println("BBB runID=" + runId);
      if (runId != null) {
        dunitRun = Swarm.getDUnitRun(runId);
        System.out.println("BBB lookedUp RUN:" + dunitRun);
      } else {
        dunitRun = Swarm.generateNewDUnitRun();
        System.out.println(
            "BBB GENNED UP A RUN:" + dunitRun + " mapping to id:" + dunitRun.getId());
        SwarmBB.getBB().getSharedMap().put(SwarmBB.RUN_ID, dunitRun.getId());
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      SwarmBB.getBB().getSharedLock().unlock();
    }
    return dunitRun;
  }
Ejemplo n.º 3
0
  /**
   * Create an Archival Unit
   *
   * @return true If successful
   */
  private boolean createAu() {

    Configuration config = getAuConfigFromForm();

    AuProxy au;
    Element element;

    try {
      au = getRemoteApi().createAndSaveAuConfiguration(getPlugin(), config);

    } catch (ArchivalUnit.ConfigurationException exception) {
      return error("Configuration failed: " + exception.getMessage());

    } catch (IOException exception) {
      return error("Unable to save configuration: " + exception.getMessage());
    }
    /*
     * Successful creation - add the AU name and ID to the response document
     */
    element = getXmlUtils().createElement(getResponseRoot(), AP_E_AU);
    XmlUtils.addText(element, au.getName());

    element = getXmlUtils().createElement(getResponseRoot(), AP_E_AUID);
    XmlUtils.addText(element, au.getAuId());

    return true;
  }
  // execute and get results
  private void execute(Connection conn, String text, Writer writer, boolean commaSeparator)
      throws SQLException {

    BufferedWriter buffer = new BufferedWriter(writer);
    Statement stmt = conn.createStatement();
    stmt.execute(text);
    ResultSet rs = stmt.getResultSet();
    ResultSetMetaData metadata = rs.getMetaData();
    int nbCols = metadata.getColumnCount();
    String[] labels = new String[nbCols];
    int[] colwidths = new int[nbCols];
    int[] colpos = new int[nbCols];
    int linewidth = 1;

    // read each occurrence
    try {
      while (rs.next()) {
        for (int i = 0; i < nbCols; i++) {
          Object value = rs.getObject(i + 1);
          if (value != null) {
            buffer.write(value.toString());
            if (commaSeparator) buffer.write(",");
          }
        }
      }
      buffer.flush();
      rs.close();
    } catch (IOException ex) {
      if (Debug.isDebug()) ex.printStackTrace();
      // ok, exit from the loop
    } catch (SQLException ex) {
      if (Debug.isDebug()) ex.printStackTrace();
    }
  }
 public void initialize() {
   try {
     PipedWriter src = new PipedWriter();
     reader = new PipedReader(src);
     writer = new PrintWriter(src);
   } catch (IOException ex) {
     if (Debug.isDebug()) ex.printStackTrace();
   }
 }
Ejemplo n.º 6
0
  public static void addToResultsFile(
      String executingDatabaseName,
      long runTime,
      long rowsLoaded,
      long rowsLoadedPerSecond,
      File resultsFile) {

    boolean resultsFileCreated = false;

    try {
      if (!resultsFile.exists()) {

        resultsFileCreated = resultsFile.createNewFile();
      }

      OutputStream resultsFileStream = new FileOutputStream(resultsFile, true);

      if (resultsFileCreated) {
        // Write header lines.

        String header =
            "timeOfTest, runTime, databaseBeingTested, rowsLoaded, rowsLoadedPerSecond, hotStart, config, replicas"
                + "\n";

        resultsFileStream.write(header.getBytes());
      }

      String results =
          startDate.getTime()
              + ", "
              + runTime
              + ", "
              + executingDatabaseName
              + ", "
              + rowsLoaded
              + ", "
              + rowsLoadedPerSecond
              + ", "
              + hotStart
              + ", "
              + configInfo
              + ", "
              + numberOfReplicas
              + "\n";
      resultsFileStream.write(results.getBytes());

      resultsFileStream.flush();

      resultsFileStream.close();

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 7
0
  // Logs a new entry in the library RSS file
  public static synchronized void addRSSEntry(
      String title, String link, String description, File rssFile) {
    // File rssFile=new File("nofile.xml");

    try {
      System.out.println("Looking for RSS file: " + rssFile.getCanonicalPath());
      if (rssFile.exists()) {

        SAXReader reader = new SAXReader();
        Document document = reader.read(rssFile);
        Element root = document.getRootElement();
        Element channel = root.element("channel");
        List items = channel.elements("item");
        int numItems = items.size();
        items = null;
        if (numItems > 9) {
          Element removeThisItem = channel.element("item");
          channel.remove(removeThisItem);
        }

        Element newItem = channel.addElement("item");
        Element newTitle = newItem.addElement("title");
        Element newLink = newItem.addElement("link");
        Element newDescription = newItem.addElement("description");
        newTitle.setText(title);
        newDescription.setText(description);
        newLink.setText(link);

        Element pubDate = channel.element("pubDate");
        pubDate.setText((new java.util.Date()).toString());

        // now save changes
        FileWriter mywriter = new FileWriter(rssFile);
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setLineSeparator(System.getProperty("line.separator"));
        XMLWriter writer = new XMLWriter(mywriter, format);
        writer.write(document);
        writer.close();
      }
    } catch (IOException ioe) {
      System.out.println("ERROR: Could not find the RSS file.");
      ioe.printStackTrace();
    } catch (DocumentException de) {
      System.out.println("ERROR: Could not read the RSS file.");
      de.printStackTrace();
    } catch (Exception e) {
      System.out.println("Unknown exception trying to add an entry to the RSS file.");
      e.printStackTrace();
    }
  }
  public BackupHdfs() {
    try {
      m_wrMkdirs = new PrintWriter(new BufferedWriter(new FileWriter("hdfs-mkdirs.sh")));
      m_wrMkdirs.println("hadoop fs -mkdir /mapred");
      m_wrMkdirs.println("hadoop fs -mkdir /mapred/system");

      m_wrChmods = new PrintWriter(new BufferedWriter(new FileWriter("hdfs-chmods.sh")));
      m_wrChmods.println("hadoop fs -chmod 775 /");
      m_wrChmods.println("hadoop fs -chown hdfs:hadoop /");
      m_wrChmods.println("hadoop fs -chown mapred:hadoop /mapred/system");
      m_wrChmods.println("hadoop fs -chmod 700 /mapred/system");
    } catch (IOException e) {
      System.err.println("ERROR: failed to open files for writing: " + e.toString());
    }
  }
Ejemplo n.º 9
0
  // Parse configuration data from given database
  // Read info and write appropriate .ini and .dat files
  // Given classname of JDBC driver, URL of database and user/password
  protected void parseConfigData(
      String driver_classname,
      String datasource_url,
      String username,
      String password,
      String community,
      String node,
      String path) {

    System.out.println("Loading driver " + driver_classname);

    try {
      Class driver = Class.forName(driver_classname);
    } catch (ClassNotFoundException e) {
      System.out.println("Could not load driver : " + driver_classname);
      e.printStackTrace();
    }

    System.out.println("Connecting to the datasource : " + datasource_url);
    try {
      Connection conn = DriverManager.getConnection(datasource_url, username, password);

      Statement stmt = conn.createStatement();

      // Maintain a hashtable of NodeName => Vector of ClusterNames
      Hashtable all_nodes = new Hashtable();
      parseNodeInfo(all_nodes, stmt, community, node);
      dumpNodeInfo(all_nodes, path);

      // Maintain a hashtable of ClusterName => Vector of Plugins
      Hashtable all_clusters = new Hashtable();
      parseClusterInfo(all_clusters, stmt, community, node);
      dumpClusterInfo(all_clusters, path);

      // Maintain a hashtable of OrganizationName => OrganizationData
      Hashtable all_organizations = new Hashtable();
      parseOrganizationInfo(all_organizations, stmt, community, node);
      dumpOrganizationInfo(all_organizations, path);

      conn.close();
    } catch (IOException ioe) {
      System.out.println("IO Exception");
      ioe.printStackTrace();
    } catch (SQLException sqle) {
      System.out.println("SQL Exception");
      sqle.printStackTrace();
    }
  }
Ejemplo n.º 10
0
  /** Display the file in the text area */
  private void showFile() {
    Scanner input = null;
    try {
      // Use a Scanner to read text from the file
      input = new Scanner(new File(jtfFilename.getText().trim()));

      // Read a line and append the line to the text area
      while (input.hasNext()) jtaFile.append(input.nextLine() + '\n');
    } catch (FileNotFoundException ex) {
      System.out.println("File not found: " + jtfFilename.getText());
    } catch (IOException ex) {
      ex.printStackTrace();
    } finally {
      if (input != null) input.close();
    }
  }
Ejemplo n.º 11
0
  @Override
  public byte[] getFollowRequests(UserPublicKey owner) {
    byte[] dummy = null;
    FollowRequestData request = new FollowRequestData(owner, dummy);
    RowData[] requests = request.select();
    if (requests == null) return new byte[4];

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DataOutput dout = new DataOutputStream(bout);
    try {
      dout.writeInt(requests.length);
      for (RowData req : requests) Serialize.serialize(req.data, dout);
      return bout.toByteArray();
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
  }
Ejemplo n.º 12
0
  public static void loadSettings() {

    if (!new File("/plugins/config/WorldTools/MySQL.properties").exists()) {
      try {
        File f = new File("plugins/config/WorldTools/MySQL.properties");
        BufferedWriter out = new BufferedWriter(new FileWriter(f));
        out.write("############################");
        out.newLine();
        out.write("# THIS IS CURRENTLY UNUSED #");
        out.newLine();
        out.write("############################");
        out.newLine();
        out.write("useCanarySQL=true");
        out.newLine();
        out.newLine();
        out.write("SQLdriver=com.mysql.jdbc.Driver");
        out.newLine();
        out.newLine();
        out.write("SQLuser=root");
        out.newLine();
        out.newLine();
        out.write("SQLpass=root");
        out.newLine();
        out.newLine();
        out.write("SQLdb=jdbc:mysql://localhost:3306/minecraft");
        out.newLine();
        out.newLine();
      } catch (IOException e) {
        log.info("[WorldTools] - Error during creating SQL propertiesfile!");
        e.printStackTrace();
      }
    }
    PropertiesFile properties = new PropertiesFile("/plugins/config/WorldTools/MySQL.properties");
    try {
      SQLdriver = properties.getProperty("SQLdriver");
      SQLusername = properties.getProperty("SQLuser");
      SQLpassword = properties.getProperty("SQLpass");
      SQLdb = properties.getProperty("SQLdb");
      useSql = Boolean.parseBoolean(properties.getProperty("UseCanarySQL"));
    } catch (Exception e) {
      log.log(Level.SEVERE, "Exception while reading from the mysql properties", e);
    }
    getConnection();
  }
Ejemplo n.º 13
0
  public String execute() {
    HttpSession session = ServletActionContext.getRequest().getSession();
    if (photo == null) return "setinfo";
    String uploadPath = "/var/www/uploadres";
    String photoName = session.getAttribute("username") + this.getPhotoFileName();
    File toPhotoFile = new File(new File(uploadPath), photoName);

    if (!toPhotoFile.getParentFile().exists()) toPhotoFile.getParentFile().mkdirs();
    try {
      FileUtils.copyFile(photo, toPhotoFile);
      try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        try {
          Connection conn = DriverManager.getConnection(url, user, pwd);
          Statement stmt = conn.createStatement();
          String sql =
              "UPDATE Users SET photo = '"
                  + photoName
                  + "' WHERE username='******'";
          stmt.execute(sql);
        } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return "setinfo";
  }
Ejemplo n.º 14
0
  @Override
  public void run() {
    // TODO Auto-generated method stub
    try {
      Socket s = new Socket(hostin, portin);
      InputStream ins = s.getInputStream();
      OutputStream os = s.getOutputStream();
      ir = new BufferedReader(new InputStreamReader(ins));
      pw = new PrintWriter(new OutputStreamWriter(os), true);

      pw.print(nick);
      while (true) {
        String line = ir.readLine();
        jta.append(line + "\n");
        jsp.getVerticalScrollBar().setValue(jsp.getVerticalScrollBar().getMaximum());
      }

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 15
0
  public static void main(String[] args) {
    File inputFile =
        args.length > 0
            ? new File(args[0])
            : new File("C:\\Documents and Settings\\tdanford\\Desktop\\sacCer1.gff");

    try {
      SGDGFFParser parser = new SGDGFFParser();
      BinCalculator bincalc = new BinCalculator();
      parser.parseInputFile(inputFile);

      for (String k : parser.geneFeatures.keySet()) {
        try {
          GeneFeatures gf = parser.geneFeatures.get(k);
          StringBuffer line = new StringBuffer();
          ArrayList<Integer> starts = new ArrayList<Integer>();
          ArrayList<Integer> ends = new ArrayList<Integer>();
          for (GFFRecord cds : gf.cds) {
            starts.add(cds.getStart());
            ends.add(cds.getEnd());
          }
          if (starts.size() == 0) {
            starts.add(gf.gene.getStart());
            ends.add(gf.gene.getEnd());
          }

          Collections.sort(starts);
          Collections.sort(ends);
          String strand = "";
          if (gf.gene.getStrand() == StrandedFeature.NEGATIVE) {
            strand = "-";
          } else if (gf.gene.getStrand() == StrandedFeature.POSITIVE) {
            strand = "+";
          }
          /* dumb that we have to put this back on, but that's the UCSC standard format */
          String chrom = "chr" + Genome.fixYeastChrom(gf.gene.getSeqName());

          line.append(gf.id + "\t");
          line.append(chrom + "\t"); // do we need to get rid of roman numerals here ?
          line.append(strand + "\t");
          line.append(gf.gene.getStart() + "\t");
          line.append(gf.gene.getEnd() + "\t");
          line.append(starts.get(0) + "\t");
          line.append(ends.get(ends.size() - 1) + "\t");
          line.append(starts.size() + "\t");
          for (int i = 0; i < starts.size(); i++) {
            if (i == 0) {
              line.append(starts.get(i));
            } else {
              line.append("," + starts.get(i));
            }
          }
          line.append("\t");
          for (int i = 0; i < ends.size(); i++) {
            if (i == 0) {
              line.append(ends.get(i));
            } else {
              line.append("," + ends.get(i));
            }
          }
          line.append("\t");

          // no protein ID
          System.out.println(line);
        } catch (Exception e) {
          System.err.println(e.toString());
          e.printStackTrace();
        }
      }
      for (GFFRecord record : parser.otherRecords) {
        if (record.getFeature().equals("intron")) {
          continue;
        }

        try {
          StringBuffer line = new StringBuffer();
          line.append(bincalc.getBinFromRange(record.getStart(), record.getEnd()) + "\t");
          String strand = "";
          if (record.getStrand() == StrandedFeature.NEGATIVE) {
            strand = "-";
          } else if (record.getStrand() == StrandedFeature.POSITIVE) {
            strand = "+";
          }
          /* dumb that we have to put this back on, but that's the UCSC standard format */
          String chrom = "chr" + Genome.fixYeastChrom(record.getSeqName());
          Map<String, List<String>> attrs = decodeAttrMap(record);
          String name = "";
          if (attrs != null && attrs.containsKey("Name") && attrs.get("Name").size() > 0) {
            name = attrs.get("Name").get(0);
          } else if (attrs != null && attrs.containsKey("ID") && attrs.get("ID").size() > 0) {
            name = attrs.get("ID").get(0);
          }

          line.append(chrom + "\t");
          line.append(record.getStart() + "\t" + record.getEnd() + "\t");
          line.append(name + "\t");
          line.append(record.getScore() + "\t");
          line.append(strand + "\t");
          line.append(record.getFeature());
          System.out.println(line);
        } catch (Exception e) {
          System.err.println(e.toString());
          e.printStackTrace();
        }
      }

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 16
0
  // Logs a new ATOM entry
  public static synchronized void addATOMEntry(
      String title, String link, String description, File atomFile, String context) {
    try {

      if (atomFile.exists()) {

        // System.out.println("ATOM file found!");
        /** Namespace URI for content:encoded elements */
        String CONTENT_NS = "http://www.w3.org/2005/Atom";

        /** Parses RSS or Atom to instantiate a SyndFeed. */
        SyndFeedInput input = new SyndFeedInput();

        /** Transforms SyndFeed to RSS or Atom XML. */
        SyndFeedOutput output = new SyndFeedOutput();

        // Load the feed, regardless of RSS or Atom type
        SyndFeed feed = input.build(new XmlReader(atomFile));

        // Set the output format of the feed
        feed.setFeedType("atom_1.0");

        List<SyndEntry> items = feed.getEntries();
        int numItems = items.size();
        if (numItems > 9) {
          items.remove(0);
          feed.setEntries(items);
        }

        SyndEntry newItem = new SyndEntryImpl();
        newItem.setTitle(title);
        newItem.setLink(link);
        newItem.setUri(link);
        SyndContent desc = new SyndContentImpl();
        desc.setType("text/html");
        desc.setValue(description);
        newItem.setDescription(desc);
        desc.setType("text/html");
        newItem.setPublishedDate(new java.util.Date());

        List<SyndCategory> categories = new ArrayList<SyndCategory>();
        if (CommonConfiguration.getProperty("htmlTitle", context) != null) {
          SyndCategory category2 = new SyndCategoryImpl();
          category2.setName(CommonConfiguration.getProperty("htmlTitle", context));
          categories.add(category2);
        }
        newItem.setCategories(categories);
        if (CommonConfiguration.getProperty("htmlAuthor", context) != null) {
          newItem.setAuthor(CommonConfiguration.getProperty("htmlAuthor", context));
        }
        items.add(newItem);
        feed.setEntries(items);

        feed.setPublishedDate(new java.util.Date());

        FileWriter writer = new FileWriter(atomFile);
        output.output(feed, writer);
        writer.toString();
      }
    } catch (IOException ioe) {
      System.out.println("ERROR: Could not find the ATOM file.");
      ioe.printStackTrace();
    } catch (Exception e) {
      System.out.println("Unknown exception trying to add an entry to the ATOM file.");
      e.printStackTrace();
    }
  }
Ejemplo n.º 17
0
  /**
   * Convert an existing data object to the specified JDBC type.
   *
   * @param callerReference an object reference to the caller of this method; must be a <code>
   *     Connection</code>, <code>Statement</code> or <code>ResultSet</code>
   * @param x the data object to convert
   * @param jdbcType the required type constant from <code>java.sql.Types</code>
   * @return the converted data object
   * @throws SQLException if the conversion is not supported or fails
   */
  static Object convert(Object callerReference, Object x, int jdbcType, String charSet)
      throws SQLException {
    // handle null value
    if (x == null) {
      switch (jdbcType) {
        case java.sql.Types.BIT:
        case JtdsStatement.BOOLEAN:
          return Boolean.FALSE;

        case java.sql.Types.TINYINT:
        case java.sql.Types.SMALLINT:
        case java.sql.Types.INTEGER:
          return INTEGER_ZERO;

        case java.sql.Types.BIGINT:
          return LONG_ZERO;

        case java.sql.Types.REAL:
          return FLOAT_ZERO;

        case java.sql.Types.FLOAT:
        case java.sql.Types.DOUBLE:
          return DOUBLE_ZERO;

        default:
          return null;
      }
    }

    try {
      switch (jdbcType) {
        case java.sql.Types.TINYINT:
          if (x instanceof Boolean) {
            return ((Boolean) x).booleanValue() ? INTEGER_ONE : INTEGER_ZERO;
          } else if (x instanceof Byte) {
            return new Integer(((Byte) x).byteValue() & 0xFF);
          } else {
            long val;
            if (x instanceof Number) {
              val = ((Number) x).longValue();
            } else if (x instanceof String) {
              val = new Long(((String) x).trim()).longValue();
            } else {
              break;
            }
            if (val < Byte.MIN_VALUE || val > Byte.MAX_VALUE) {
              throw new SQLException(
                  Messages.get("error.convert.numericoverflow", x, getJdbcTypeName(jdbcType)),
                  "22003");
            } else {
              return new Integer(new Long(val).intValue());
            }
          }

        case java.sql.Types.SMALLINT:
          if (x instanceof Boolean) {
            return ((Boolean) x).booleanValue() ? INTEGER_ONE : INTEGER_ZERO;
          } else if (x instanceof Short) {
            return new Integer(((Short) x).shortValue());
          } else if (x instanceof Byte) {
            return new Integer(((Byte) x).byteValue() & 0xFF);
          } else {
            long val;
            if (x instanceof Number) {
              val = ((Number) x).longValue();
            } else if (x instanceof String) {
              val = new Long(((String) x).trim()).longValue();
            } else {
              break;
            }
            if (val < Short.MIN_VALUE || val > Short.MAX_VALUE) {
              throw new SQLException(
                  Messages.get("error.convert.numericoverflow", x, getJdbcTypeName(jdbcType)),
                  "22003");
            } else {
              return new Integer(new Long(val).intValue());
            }
          }

        case java.sql.Types.INTEGER:
          if (x instanceof Integer) {
            return x;
          } else if (x instanceof Boolean) {
            return ((Boolean) x).booleanValue() ? INTEGER_ONE : INTEGER_ZERO;
          } else if (x instanceof Short) {
            return new Integer(((Short) x).shortValue());
          } else if (x instanceof Byte) {
            return new Integer(((Byte) x).byteValue() & 0xFF);
          } else {
            long val;
            if (x instanceof Number) {
              val = ((Number) x).longValue();
            } else if (x instanceof String) {
              val = new Long(((String) x).trim()).longValue();
            } else {
              break;
            }
            if (val < Integer.MIN_VALUE || val > Integer.MAX_VALUE) {
              throw new SQLException(
                  Messages.get("error.convert.numericoverflow", x, getJdbcTypeName(jdbcType)),
                  "22003");
            } else {
              return new Integer(new Long(val).intValue());
            }
          }

        case java.sql.Types.BIGINT:
          if (x instanceof BigDecimal) {
            BigDecimal val = (BigDecimal) x;
            if (val.compareTo(MIN_VALUE_LONG_BD) < 0 || val.compareTo(MAX_VALUE_LONG_BD) > 0) {
              throw new SQLException(
                  Messages.get("error.convert.numericoverflow", x, getJdbcTypeName(jdbcType)),
                  "22003");
            } else {
              return new Long(val.longValue());
            }
          } else if (x instanceof Long) {
            return x;
          } else if (x instanceof Boolean) {
            return ((Boolean) x).booleanValue() ? LONG_ONE : LONG_ZERO;
          } else if (x instanceof Byte) {
            return new Long(((Byte) x).byteValue() & 0xFF);
          } else if (x instanceof BigInteger) {
            BigInteger val = (BigInteger) x;
            if (val.compareTo(MIN_VALUE_LONG_BI) < 0 || val.compareTo(MAX_VALUE_LONG_BI) > 0) {
              throw new SQLException(
                  Messages.get("error.convert.numericoverflow", x, getJdbcTypeName(jdbcType)),
                  "22003");
            } else {
              return new Long(val.longValue());
            }
          } else if (x instanceof Number) {
            return new Long(((Number) x).longValue());
          } else if (x instanceof String) {
            return new Long(((String) x).trim());
          } else {
            break;
          }

        case java.sql.Types.REAL:
          if (x instanceof Float) {
            return x;
          } else if (x instanceof Byte) {
            return new Float(((Byte) x).byteValue() & 0xFF);
          } else if (x instanceof Number) {
            return new Float(((Number) x).floatValue());
          } else if (x instanceof String) {
            return new Float(((String) x).trim());
          } else if (x instanceof Boolean) {
            return ((Boolean) x).booleanValue() ? FLOAT_ONE : FLOAT_ZERO;
          }

          break;

        case java.sql.Types.FLOAT:
        case java.sql.Types.DOUBLE:
          if (x instanceof Double) {
            return x;
          } else if (x instanceof Byte) {
            return new Double(((Byte) x).byteValue() & 0xFF);
          } else if (x instanceof Number) {
            return new Double(((Number) x).doubleValue());
          } else if (x instanceof String) {
            return new Double(((String) x).trim());
          } else if (x instanceof Boolean) {
            return ((Boolean) x).booleanValue() ? DOUBLE_ONE : DOUBLE_ZERO;
          }

          break;

        case java.sql.Types.NUMERIC:
        case java.sql.Types.DECIMAL:
          if (x instanceof BigDecimal) {
            return x;
          } else if (x instanceof Number) {
            return new BigDecimal(x.toString());
          } else if (x instanceof String) {
            return new BigDecimal((String) x);
          } else if (x instanceof Boolean) {
            return ((Boolean) x).booleanValue() ? BIG_DECIMAL_ONE : BIG_DECIMAL_ZERO;
          }

          break;

        case java.sql.Types.VARCHAR:
        case java.sql.Types.CHAR:
          if (x instanceof String) {
            return x;
          } else if (x instanceof Number) {
            return x.toString();
          } else if (x instanceof Boolean) {
            return ((Boolean) x).booleanValue() ? "1" : "0";
          } else if (x instanceof Clob) {
            Clob clob = (Clob) x;
            long length = clob.length();

            if (length > Integer.MAX_VALUE) {
              throw new SQLException(Messages.get("error.normalize.lobtoobig"), "22000");
            }

            return clob.getSubString(1, (int) length);
          } else if (x instanceof Blob) {
            Blob blob = (Blob) x;
            long length = blob.length();

            if (length > Integer.MAX_VALUE) {
              throw new SQLException(Messages.get("error.normalize.lobtoobig"), "22000");
            }

            x = blob.getBytes(1, (int) length);
          }

          if (x instanceof byte[]) {
            return toHex((byte[]) x);
          }

          return x.toString(); // Last hope!

        case java.sql.Types.BIT:
        case JtdsStatement.BOOLEAN:
          if (x instanceof Boolean) {
            return x;
          } else if (x instanceof Number) {
            return (((Number) x).intValue() == 0) ? Boolean.FALSE : Boolean.TRUE;
          } else if (x instanceof String) {
            String tmp = ((String) x).trim();

            return ("1".equals(tmp) || "true".equalsIgnoreCase(tmp)) ? Boolean.TRUE : Boolean.FALSE;
          }

          break;

        case java.sql.Types.VARBINARY:
        case java.sql.Types.BINARY:
          if (x instanceof byte[]) {
            return x;
          } else if (x instanceof Blob) {
            Blob blob = (Blob) x;

            return blob.getBytes(1, (int) blob.length());
          } else if (x instanceof Clob) {
            Clob clob = (Clob) x;
            long length = clob.length();

            if (length > Integer.MAX_VALUE) {
              throw new SQLException(Messages.get("error.normalize.lobtoobig"), "22000");
            }

            x = clob.getSubString(1, (int) length);
          }

          if (x instanceof String) {
            //
            // Strictly speaking this conversion is not required by
            // the JDBC standard but jTDS has always supported it.
            //
            if (charSet == null) {
              charSet = "ISO-8859-1";
            }

            try {
              return ((String) x).getBytes(charSet);
            } catch (UnsupportedEncodingException e) {
              return ((String) x).getBytes();
            }
          } else if (x instanceof UniqueIdentifier) {
            return ((UniqueIdentifier) x).getBytes();
          }

          break;

        case java.sql.Types.TIMESTAMP:
          if (x instanceof DateTime) {
            return ((DateTime) x).toTimestamp();
          } else if (x instanceof java.sql.Timestamp) {
            return x;
          } else if (x instanceof java.sql.Date) {
            return new java.sql.Timestamp(((java.sql.Date) x).getTime());
          } else if (x instanceof java.sql.Time) {
            return new java.sql.Timestamp(((java.sql.Time) x).getTime());
          } else if (x instanceof java.lang.String) {
            return java.sql.Timestamp.valueOf(((String) x).trim());
          }

          break;

        case java.sql.Types.DATE:
          if (x instanceof DateTime) {
            return ((DateTime) x).toDate();
          } else if (x instanceof java.sql.Date) {
            return x;
          } else if (x instanceof java.sql.Time) {
            return DATE_ZERO;
          } else if (x instanceof java.sql.Timestamp) {
            GregorianCalendar cal = (GregorianCalendar) calendar.get();
            cal.setTime((java.util.Date) x);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            // VM1.4+ only              return new java.sql.Date(cal.getTimeInMillis());
            return new java.sql.Date(cal.getTime().getTime());
          } else if (x instanceof java.lang.String) {
            return java.sql.Date.valueOf(((String) x).trim());
          }

          break;

        case java.sql.Types.TIME:
          if (x instanceof DateTime) {
            return ((DateTime) x).toTime();
          } else if (x instanceof java.sql.Time) {
            return x;
          } else if (x instanceof java.sql.Date) {
            return TIME_ZERO;
          } else if (x instanceof java.sql.Timestamp) {
            GregorianCalendar cal = (GregorianCalendar) calendar.get();
            // VM 1.4+ only             cal.setTimeInMillis(((java.sql.Timestamp)x).getTime());
            cal.setTime((java.util.Date) x);
            cal.set(Calendar.YEAR, 1970);
            cal.set(Calendar.MONTH, 0);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            // VM 1.4+ only             return new java.sql.Time(cal.getTimeInMillis());*/
            return new java.sql.Time(cal.getTime().getTime());
          } else if (x instanceof java.lang.String) {
            return java.sql.Time.valueOf(((String) x).trim());
          }

          break;

        case java.sql.Types.OTHER:
          return x;

        case java.sql.Types.JAVA_OBJECT:
          throw new SQLException(
              Messages.get(
                  "error.convert.badtypes", x.getClass().getName(), getJdbcTypeName(jdbcType)),
              "22005");

        case java.sql.Types.LONGVARBINARY:
        case java.sql.Types.BLOB:
          if (x instanceof Blob) {
            return x;
          } else if (x instanceof byte[]) {
            return new BlobImpl(getConnection(callerReference), (byte[]) x);
          } else if (x instanceof Clob) {
            //
            // Convert CLOB to BLOB. Not required by the standard but we will
            // do it anyway.
            //
            Clob clob = (Clob) x;
            try {
              if (charSet == null) {
                charSet = "ISO-8859-1";
              }
              Reader rdr = clob.getCharacterStream();
              BlobImpl blob = new BlobImpl(getConnection(callerReference));
              BufferedWriter out =
                  new BufferedWriter(new OutputStreamWriter(blob.setBinaryStream(1), charSet));
              // TODO Use a buffer to improve performance
              int c;
              while ((c = rdr.read()) >= 0) {
                out.write(c);
              }
              out.close();
              rdr.close();
              return blob;
            } catch (UnsupportedEncodingException e) {
              // Unlikely to happen but fall back on in memory copy
              x = clob.getSubString(1, (int) clob.length());
            } catch (IOException e) {
              throw new SQLException(
                  Messages.get("error.generic.ioerror", e.getMessage()), "HY000");
            }
          }

          if (x instanceof String) {
            //
            // Strictly speaking this conversion is also not required by
            // the JDBC standard but jTDS has always supported it.
            //
            BlobImpl blob = new BlobImpl(getConnection(callerReference));
            String data = (String) x;

            if (charSet == null) {
              charSet = "ISO-8859-1";
            }

            try {
              blob.setBytes(1, data.getBytes(charSet));
            } catch (UnsupportedEncodingException e) {
              blob.setBytes(1, data.getBytes());
            }

            return blob;
          }

          break;

        case java.sql.Types.LONGVARCHAR:
        case java.sql.Types.CLOB:
          if (x instanceof Clob) {
            return x;
          } else if (x instanceof Blob) {
            //
            // Convert BLOB to CLOB
            //
            Blob blob = (Blob) x;
            try {
              InputStream is = blob.getBinaryStream();
              ClobImpl clob = new ClobImpl(getConnection(callerReference));
              Writer out = clob.setCharacterStream(1);
              // TODO Use a buffer to improve performance
              int b;
              // These reads/writes are buffered by the underlying blob buffers
              while ((b = is.read()) >= 0) {
                out.write(hex[b >> 4]);
                out.write(hex[b & 0x0F]);
              }
              out.close();
              is.close();
              return clob;
            } catch (IOException e) {
              throw new SQLException(
                  Messages.get("error.generic.ioerror", e.getMessage()), "HY000");
            }
          } else if (x instanceof Boolean) {
            x = ((Boolean) x).booleanValue() ? "1" : "0";
          } else if (!(x instanceof byte[])) {
            x = x.toString();
          }

          if (x instanceof byte[]) {
            ClobImpl clob = new ClobImpl(getConnection(callerReference));
            clob.setString(1, toHex((byte[]) x));

            return clob;
          } else if (x instanceof String) {
            return new ClobImpl(getConnection(callerReference), (String) x);
          }

          break;

        default:
          throw new SQLException(
              Messages.get("error.convert.badtypeconst", getJdbcTypeName(jdbcType)), "HY004");
      }

      throw new SQLException(
          Messages.get("error.convert.badtypes", x.getClass().getName(), getJdbcTypeName(jdbcType)),
          "22005");
    } catch (NumberFormatException nfe) {
      throw new SQLException(
          Messages.get("error.convert.badnumber", getJdbcTypeName(jdbcType)), "22000");
    }
  }
Ejemplo n.º 18
0
  /**
   * Read command line input and load config file, set config value to the instance of DotsConfig.
   */
  public static void loadConfig(String[] args) {
    int i;
    String configFileName = "./config.ini";
    String tmpString;
    String value;
    File configFile = null;
    StringTokenizer stk = null;
    Properties props = new Properties();
    String propString;

    /* Read command line input */
    for (i = 0; i < args.length; ) {
      String option = args[i++];

      if (option.equals("-config")) {
        configFileName = args[i++];
      } else if (option.equals("-case")) {
        TESTCASENAME = args[i++];
      } else if (option.equals("-?") || option.equals("-help")) {
        System.out.println("Usage:Dots [-option]");
        System.out.println("\t\t-config				Config file name");
        System.out.println("\t\t-case				Test case name");
        System.out.println("\t\t-help				Print this message");
        System.exit(0);
      } else {
        System.out.println("Expected Option Is Not Found!");
        System.out.println("Usage:	Dots -config [config file] -case [case name]");
        System.exit(0);
      }
    }

    TESTCASENAME = TESTCASENAME.toUpperCase();

    /* Check test case name */
    if (TESTCASENAME.equals("")) {
      System.out.println("Testcase Name Must Be Specified!");
      System.out.println("Usage:	Dots -config [config file] -case [case name]");
      System.exit(0);
    }
    if (!(TESTCASENAME.equals("BTCJ1")
        || TESTCASENAME.equals("BTCJ2")
        || TESTCASENAME.equals("BTCJ3")
        || TESTCASENAME.equals("BTCJ4")
        || TESTCASENAME.equals("BTCJ5")
        || TESTCASENAME.equals("BTCJ6")
        || TESTCASENAME.equals("BTCJ7")
        || TESTCASENAME.equals("BTCJ8")
        || TESTCASENAME.equals("ATCJ1")
        || TESTCASENAME.equals("ATCJ2"))) {
      System.out.println("Testcase Name Is Not Correct!");
      System.exit(0);
    }

    /* Load config file and set DotsConfig value */
    try {
      props.load(new FileInputStream(configFileName));

      if ((propString = props.getProperty("DURATION")) != null) {
        if (propString.trim().length() <= 0) {
          System.out.println("Expected Config File Option DURATION Is Not Found!");
          System.exit(1);
        }
        stk = new StringTokenizer(propString, ":");
        if (stk.countTokens() > 1) {
          value = stk.nextToken().trim();
          tmpString = stk.nextToken().trim();
          DotsConfig.DURATION = Integer.parseInt(value) * 60 + Integer.parseInt(tmpString);
        } else DotsConfig.DURATION = Integer.parseInt(propString.trim()) * 60;
        if (DotsConfig.DURATION <= 1) {
          System.out.println("DURATION value is too small(<=1)!");
          System.exit(1);
        }
      } else {
        System.out.println("Expected Config File Option DURATION Is Not Found!");
        System.exit(1);
      }

      if ((propString = props.getProperty("CONCURRENT_CONNECTIONS")) != null) {
        if (propString.trim().length() <= 0) {
          System.out.println("Expected Config File Option CONCURRENT_CONNECTIONS Is Not Found!");
          System.exit(1);
        }
        DotsConfig.CONNECTIONS = Integer.parseInt(propString.trim());
        if (DotsConfig.CONNECTIONS < 1) {
          System.out.println("CONCURRENT_CONNECTIONS value is too small(<1)!");
          System.exit(1);
        }
      } else {
        System.out.println("Expected Config File Option CONCURRENT_CONNECTIONS Is Not Found!");
        System.exit(1);
      }

      if ((propString = props.getProperty("CPU_TARGET")) != null) {
        if (propString.trim().length() <= 0) {
          System.out.println("Expected Config File Option CPU_TARGET Is Not Found!");
          System.exit(1);
        }
        DotsConfig.CPU_TARGET = Integer.parseInt(propString.trim());
        if (DotsConfig.CPU_TARGET <= 10) {
          System.out.println("CPU_TARGET value is too small(<10)!");
          System.exit(1);
        }
        if (DotsConfig.CPU_TARGET > 100) {
          System.out.println("CPU_TARGET value is too large(>100)!");
          System.exit(1);
        }
      } else {
        System.out.println("Expected Config File Option CPU_TARGET Is Not Found!");
        System.exit(1);
      }

      if ((propString = props.getProperty("LOG_DIR")) != null) {
        if (propString.trim().length() <= 0) {
          System.out.println("Expected Config File Option LOG_DIR Is Not Found!");
          System.exit(1);
        }
        DotsConfig.LOG_DIR = propString.trim();
      } else {
        System.out.println("Expected Config File Option LOG_DIR Is Not Found!");
        System.exit(1);
      }

      if ((propString = props.getProperty("AUTO_MODE")) != null) {
        if (propString.trim().equalsIgnoreCase("yes")) DotsConfig.AUTO_MODE = true;
        else if (propString.trim().equalsIgnoreCase("no")) DotsConfig.AUTO_MODE = false;
        else {
          System.out.println("AUTO_MODE format is not correct!");
          System.exit(1);
        }
      } else {
        System.out.println("Expected Config File Option AUTO_MODE Is Not Found!");
        System.exit(1);
      }

      if ((propString = props.getProperty("SUMMARY_INTERVAL")) != null) {
        if (propString.trim().length() <= 0) {
          System.out.println("Expected Config File Option SUMMARY_INTERVAL Is Not Found!");
          System.exit(1);
        }
        DotsConfig.SUMMARY_INTERVAL = Integer.parseInt(propString.trim());
        if (DotsConfig.SUMMARY_INTERVAL < 1) {
          System.out.println("SUMMARY_INTERVAL value is too small(<1)!");
          System.exit(1);
        }
      } else {
        System.out.println("Expected Config File Option SUMMARY_INTERVAL Is Not Found!");
        System.exit(1);
      }

      if ((propString = props.getProperty("UserID")) != null) {
        if (propString.trim().length() <= 0) {
          System.out.println("Expected Config File Option UserID Is Not Found!");
          System.exit(1);
        }
        DotsConfig.DB_UID = propString.trim();
      } else {
        System.out.println("Expected Config File Option UserID Is Not Found!");
        System.exit(1);
      }

      if ((propString = props.getProperty("Password")) != null) {
        if (propString.trim().length() <= 0) {
          System.out.println("Expected Config File Option Password Is Not Found!");
          System.exit(1);
        }
        DotsConfig.DB_PASSWD = propString.trim();
      } else {
        System.out.println("Expected Config File Option Password Is Not Found!");
        System.exit(1);
      }

      if ((propString = props.getProperty("DriverClass")) != null) {
        if (propString.trim().length() <= 0) {
          System.out.println("Expected Config File Option DriverClass Is Not Found!");
          System.exit(1);
        }
        DotsConfig.DRIVER_CLASS_NAME = propString.trim();
      } else {
        System.out.println("Expected Config File Option DriverClass Is Not Found!");
        System.exit(1);
      }

      if ((propString = props.getProperty("URL")) != null) {
        if (propString.trim().length() <= 0) {
          System.out.println("Expected Config File Option URL Is Not Found!");
          System.exit(1);
        }
        DotsConfig.URL = propString.trim();
      } else {
        System.out.println("Expected Config File Option URL Is Not Found!");
        System.exit(1);
      }

      if ((propString = props.getProperty("SERVER_IP")) != null) {
        if (propString.trim().length() <= 0) {
          System.out.println("Expected Config File Option SERVER_IP Is Not Found!");
          System.exit(1);
        }
        DotsConfig.SERVER_IP = propString.trim();
      } else {
        System.out.println("Expected Config File Option SERVER_IP Is Not Found!");
        System.exit(1);
      }

      if ((propString = props.getProperty("SERVER_PORT")) != null) {
        if (propString.trim().length() <= 0) {
          System.out.println("Expected Config File Option SERVER_PORT Is Not Found!");
          System.exit(1);
        }
        DotsConfig.PERF_SVR_PORT = propString.trim();
      } else {
        System.out.println("Expected Config File Option SERVER_PORT Is Not Found!");
        System.exit(1);
      }

      if ((propString = props.getProperty("MAX_ROWS")) != null) {
        if (propString.trim().length() <= 0) {
          System.out.println("Expected Config File Option MAX_ROWS Is Not Found!");
          System.exit(1);
        }
        DotsConfig.MAX_ROWS = Integer.parseInt(propString.trim());
        if (DotsConfig.MAX_ROWS <= 1) {
          System.out.println("MAX_ROWS value is too small(<=1)!");
          System.exit(1);
        }
      } else {
        System.out.println("Expected Config File Option MAX_ROWS Is Not Found!");
        System.exit(1);
      }

      if ((propString = props.getProperty("MAX_LOGFILESIZE")) != null) {
        if (propString.trim().length() <= 0) {
          System.out.println("Excepted Config File Option MAX_LOGFILESIZE Is Not Found!");
          System.exit(1);
        }
        DotsConfig.MAX_LOGFILESIZE = Integer.parseInt(propString.trim());
        if (DotsConfig.MAX_LOGFILESIZE <= 102400) {
          System.out.println("MAX_LOGFILESIZE value is too small(<=100K)");
          System.exit(1);
        }
        if (DotsConfig.MAX_LOGFILESIZE > 1073741824) { // 1G
          System.out.println("MAX_LOGFILESIZE value is too large(>1G)");
          System.exit(1);
        }
      } else {
        System.out.println("Excepted Config File Option MAX_LOGFILESIZE Is Not Found!");
        System.exit(1);
      }

      if ((propString = props.getProperty("CREATIONINTERVAL")) != null) {
        DotsConfig.INTERVAL = Integer.parseInt(propString.trim());
        if (DotsConfig.INTERVAL > 5) {
          System.out.println("CREATIONINTERVAL value is too large(>5min)");
          System.exit(1);
        }
        if (DotsConfig.INTERVAL < 1) {
          System.out.println("CREATIONPINTERVAL value is too small(<1min");
          System.exit(1);
        }
      } else {
        System.out.println("Excepted Config File Option CREATIONINTERVAL Is Not Found!");
        System.exit(1);
      }

      if ((propString = props.getProperty("RUN_AUTO")) != null) {
        if (propString.trim().equalsIgnoreCase("yes")) DotsConfig.RUN_AUTO = true;
        else if (propString.trim().equalsIgnoreCase("no")) DotsConfig.RUN_AUTO = false;
        else {
          System.out.println("RUN_AUTO format is not correct!");
          System.exit(1);
        }
      } else {
        DotsConfig.RUN_AUTO = false;
      }

      DotsConfig.THRDGRP = new ThreadGroup("DBAccess");
      DotsConfig.CPU_USAGE = 0;
      DotsConfig.QUERYCOUNT = 0;
      DotsConfig.INSERTCOUNT = 0;
      DotsConfig.DELETECOUNT = 0;
      DotsConfig.UPDATECOUNT = 0;
      DotsConfig.FAILEDCOUNT = 0;
    } catch (NumberFormatException e) {
      System.out.println("Config File Number Format Error!");
      e.printStackTrace();
      System.exit(1);
    } catch (IOException e) {
      System.out.println("Read Config File Error!");
      e.printStackTrace();
      System.exit(1);
    }
  }
Ejemplo n.º 19
0
  private void insertRows(Connection connection) {
    // Build the SQL INSERT statement
    String sqlInsert = "insert into " + jtfTableName.getText() + " values (";

    // Use a Scanner to read text from the file
    Scanner input = null;

    // Get file name from the text field
    String filename = jtfFilename.getText().trim();

    try {
      // Create a scanner
      input = new Scanner(new File(filename));

      // Create a statement
      Statement statement = connection.createStatement();

      System.out.println(
          "Driver major version? " + connection.getMetaData().getDriverMajorVersion());

      // Determine if batchUpdatesSupported is supported
      boolean batchUpdatesSupported = false;

      try {
        if (connection.getMetaData().supportsBatchUpdates()) {
          batchUpdatesSupported = true;
          System.out.println("batch updates supported");
        } else {
          System.out.println(
              "The driver is of JDBC 2 type, but " + "does not support batch updates");
        }
      } catch (UnsupportedOperationException ex) {
        System.out.println("The driver does not support JDBC 2");
      }

      // Determine if the driver is capable of batch updates
      if (batchUpdatesSupported) {
        // Read a line and add the insert table command to the batch
        while (input.hasNext()) {
          statement.addBatch(sqlInsert + input.nextLine() + ")");
        }

        statement.executeBatch();

        jlblStatus.setText("Batch updates completed");
      } else {
        // Read a line and execute insert table command
        while (input.hasNext()) {
          statement.executeUpdate(sqlInsert + input.nextLine() + ")");
        }

        jlblStatus.setText("Single row update completed");
      }
    } catch (SQLException ex) {
      System.out.println(ex);
    } catch (FileNotFoundException ex) {
      System.out.println("File not found: " + filename);
    } catch (IOException ex) {
      ex.printStackTrace();
    } finally {
      if (input != null) input.close();
    }
  }
  /**
   * Compare the checksums of the hdfs file as well as the local copied file.
   *
   * @author [email protected]
   * @date Fri Jan 27 06:06:00 2012
   */
  boolean compareChecksums(FileSystem fs, Path p, String sFsPath) {
    try {
      // get hdfs file info
      FileStatus stat = fs.getFileStatus(p);

      // get HDFS checksum
      FileChecksum ck = fs.getFileChecksum(p);
      String sCk, sCkShort;
      if (ck == null) {
        sCk = sCkShort = "<null>";
      } else {
        sCk = ck.toString();
        sCkShort = sCk.replaceAll("^.*:", "");
      }

      // System.out.println(p.toUri().getPath() + " len=" + stat.getLen()
      // + " " + stat.getOwner() + "/" + stat.getGroup()
      // + " checksum=" + sCk);

      // find the local file
      File fLocal = new File(sFsPath);
      if (!fLocal.exists()) {
        System.out.println("CHECKSUM-ERROR: file does not exist: " + sFsPath);
        return false;
      }
      if (!fLocal.isFile()) {
        System.out.println("CHECKSUM-ERROR: path is not a file: " + sFsPath);
        return false;
      }
      if (stat.getLen() != fLocal.length()) {
        System.out.println(
            "CHECKSUM-ERROR: length mismatch: "
                + sFsPath
                + " hdfslen="
                + stat.getLen()
                + " fslen="
                + fLocal.length());
        return false;
      }

      // get local fs checksum
      FileChecksum ckLocal = getLocalFileChecksum(sFsPath);
      if (ckLocal == null) {
        System.out.println("ERROR Failed to get checksum for local file " + sFsPath);
        return false;
      }

      // compare checksums as a string, after stripping the
      // algorithm name from the beginning
      String sCkLocal = ckLocal.toString();
      String sCkLocalShort = sCkLocal.replaceAll("^.*:", "");

      if (false == sCkShort.equals(sCkLocalShort)) {
        System.out.println(
            "CHECKSUM-ERROR: checksum mismatch: "
                + sFsPath
                + "\nhdfs = "
                + sCk
                + "\nlocal= "
                + sCkLocal);
        return false;
      }

      return true;
    } catch (IOException e) {
      System.out.println("CHECKSUM-ERROR: " + sFsPath + " exception " + e.toString());
    }

    return false;
  }
Ejemplo n.º 21
0
  public boolean parserCpYieldTxt(File txtFile) throws Exception {
    FileInputStream fIn = null;
    String fileNameUid = "";
    try {
      Calendar calendar = Calendar.getInstance();
      SimpleDateFormat df2 = new SimpleDateFormat("yyyyMM");

      // Using Find Target "|=124" or ",=44" Count
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

      logger.debug(
          "File "
              + txtFile.getAbsolutePath()
              + "  "
              + txtFile.getName()
              + "  "
              + new MimetypesFileTypeMap().getContentType(txtFile));

      // 1.0 讀入檔案, 建立資料流
      fIn = new FileInputStream(txtFile);
      // FileInputStream fIn2 = new FileInputStream(csvFile);
      InputStreamReader isr = null;
      BufferedReader br = null;
      isr = new InputStreamReader(fIn, "UTF-8");
      br = new BufferedReader(isr);

      // byte[] byteArray = new byte[new Long(csvFile.length()).intValue()];
      // 讀入File Data Byte.....
      // fIn2.read(byteArray);
      logger.debug(txtFile.getName() + "<--讀入資料檔...成功");
      // Using get sign  "\n" 抓行數...

      // 1.1 讀入行數資料, 略過行數
      int l = -1;
      List<String> lineDataList = new ArrayList<String>();
      for (int i = 0; i <= l; i++) {
        br.readLine();
      }
      while (br.ready()) {
        String line = br.readLine();
        line = null != line ? line : "";
        // logger.debug(line);
        if (!line.trim().equals("")) {
          lineDataList.add(line);
        }
      }

      // 1.2 確認有資料開使處理
      if (lineDataList != null) {
        CpYieldParserDao cpYieldParserDao = new CpYieldParserDao();

        CpYieldLotTo cpYieldLotTo = new CpYieldLotTo();

        String cpYieldUuid = UUID.randomUUID().toString().toUpperCase();
        logger.debug("lineDataList.size() " + lineDataList.size());

        fileNameUid =
            FilenameUtils.getBaseName(txtFile.getName())
                + "_"
                + cpYieldUuid
                + "."
                + FilenameUtils.getExtension(txtFile.getName());

        File bkFolder =
            new File(fileOutUrl + File.separator + df2.format(calendar.getTime()).toString());

        bkFolder.mkdir();

        File bkFile = new File(bkFolder.getPath().toString() + File.separator + fileNameUid);
        // 1.2.1 處理每行資料
        String tmpWaferID = lineDataList.get(1);
        String arrayWafer[] = tmpWaferID.split("=")[1].trim().split("-");

        // logger.debug("arrayWafer[] " + arrayWafer.length);

        // 1.3 Prepare Data
        String cpLot = arrayWafer[0].trim();
        String waferId = arrayWafer[1].trim();
        String machineId = arrayWafer[2].trim();
        Integer cpTestTimes = cpYieldParserDao.getMaxCpTestTimes(cpLot, waferId);

        String xMaxCoor = lineDataList.get(2).split("=")[1].trim();
        String yMaxCoor = lineDataList.get(3).split("=")[1].trim();
        String flat = lineDataList.get(4).split("=")[1].trim();

        logger.debug("xMaxCoor " + xMaxCoor);
        logger.debug("yMaxCoor " + yMaxCoor);
        logger.debug("flat " + flat);

        // 1.3 Find Bin Data
        int sb = 0, eb = 0;
        for (int i = 0; i < lineDataList.size(); i++) {
          if (lineDataList.get(i).indexOf("Wafer Bin Summary") >= 0) {
            sb = i + 1;
            break;
          }
        }
        for (int i = sb; i < lineDataList.size(); i++) {
          if (lineDataList.get(i).indexOf("bin") < 0) {
            eb = i - 1;
            break;
          }
        }

        logger.debug("sb " + sb);
        logger.debug(lineDataList.get(sb).trim());
        logger.debug("eb " + eb);
        logger.debug(lineDataList.get(eb).trim());
        // 1.3.1 Get Bin Data
        List<CpYieldLotBinTo> cpYieldLotBins = new ArrayList<CpYieldLotBinTo>();
        String cpYieldBinUuid;

        String bin;
        Integer die;
        String percentage;
        String binString;

        for (int j = sb; j <= eb; j++) {
          cpYieldBinUuid = UUID.randomUUID().toString().toUpperCase();
          CpYieldLotBinTo cpYieldLotBinTo = new CpYieldLotBinTo();

          cpYieldLotBinTo.setCpYieldBinUuid(cpYieldBinUuid);
          cpYieldLotBinTo.setCpYieldUuid(cpYieldUuid);

          binString = lineDataList.get(j).trim();
          binString = binString.replaceAll("bin", "").trim();
          // Get Bin
          bin = binString.substring(0, binString.indexOf(" "));
          logger.debug("bin " + bin);
          // Get Die
          bin = binString.substring(0, binString.indexOf(" "));
          binString = binString.replaceAll(bin, "").trim();
          die = Integer.parseInt(binString.substring(0, binString.indexOf(" ")));
          logger.debug("die " + die);
          // Get Percentage
          binString = binString.replaceAll(die.toString(), "").trim();
          percentage = binString.substring(0, binString.length() - 1);
          logger.debug("percentage " + percentage);

          cpYieldLotBinTo.setBin(bin);
          cpYieldLotBinTo.setDie(die);
          cpYieldLotBinTo.setPercentage(percentage);

          cpYieldLotBins.add(cpYieldLotBinTo);
        }

        // 1.4 Die Data
        Integer passDie;
        Integer failDie;
        Integer totelDie;
        for (int i = eb + 1; i < lineDataList.size(); i++) {
          // pass die
          if (lineDataList.get(i).trim().indexOf("pass die") >= 0) {
            passDie = Integer.parseInt(lineDataList.get(i).trim().split(":")[1].trim());
            logger.debug("passDie " + passDie);
            cpYieldLotTo.setPassDie(passDie);
            continue;
          }
          // fail die
          if (lineDataList.get(i).trim().indexOf("fail die") >= 0) {
            failDie = Integer.parseInt(lineDataList.get(i).trim().split(":")[1].trim());
            logger.debug("failDie " + failDie);
            cpYieldLotTo.setFailDie(failDie);
            continue;
          }
          // totel die
          if (lineDataList.get(i).trim().indexOf("totel die") >= 0) {
            totelDie = Integer.parseInt(lineDataList.get(i).trim().split(":")[1].trim());
            logger.debug("totelDie " + totelDie);
            cpYieldLotTo.setTotelDie(totelDie);
            continue;
          }
        }

        // 1.5 Set data in To
        cpYieldLotTo.setCpYieldUuid(cpYieldUuid);
        cpYieldLotTo.setCpTestTimes(cpTestTimes);
        cpYieldLotTo.setCpLot(cpLot);
        cpYieldLotTo.setWaferId(waferId);
        cpYieldLotTo.setMachineId(machineId);
        cpYieldLotTo.setxMaxCoor(xMaxCoor);
        cpYieldLotTo.setyMaxCoor(yMaxCoor);
        cpYieldLotTo.setFlat(flat);

        String fileMimeType = new MimetypesFileTypeMap().getContentType(txtFile);
        cpYieldLotTo.setFileName(
            df2.format(calendar.getTime()).toString() + File.separator + fileNameUid);
        cpYieldLotTo.setFileMimeType(fileMimeType);
        cpYieldLotTo.setFtpFlag("N");

        fIn.close();
        br.close();

        Methods.copyFile(txtFile, bkFile);
        txtFile.delete();
        // 1.6 DataBasse
        // 1.6.1 Insert CP Lot Table
        cpYieldParserDao.insertCpYieldLot(cpYieldLotTo);
        cpYieldParserDao.insertCpYieldLotBin(cpYieldLotBins);
      }

      fIn.close();
      br.close();

      logger.info(txtFile.getName() + " is Parser complete");
      logger.info(fileNameUid + " is Parser complete");

      // logger.debug(tapeList.size());
      logger.info("---------------------------------");
    } catch (Exception e) {
      if (fIn != null) {
        fIn.close();
      }
      logger.info("ERROR MOVE FILE");
      Methods.copyFile(txtFile, new File(fileErrorUrl + "\\" + txtFile.getName()));
      txtFile.delete();

      StackTraceElement[] messages = e.getStackTrace();
      Exception ex = new Exception(txtFile.getName());
      ex.setStackTrace(messages);
      ex.printStackTrace();
      throw ex;
    } finally {
      try {
        if (fIn != null) {
          fIn.close();
        }
      } catch (IOException ie) {
        StackTraceElement[] messages = ie.getStackTrace();
        int length = messages.length;
        String error = "";
        for (int i = 0; i < length; i++) {
          error = error + "toString:" + messages[i].toString() + "\r\n";
        }
        ie.printStackTrace();
        logger.error(error);
        return false;
      }
    }
    return true;
  }
  private void executeParsedFile(NameListMessage nameListMessage, ArrayList cmds) {
    BufferedWriter buffer = null;
    int connectionId = nameListMessage.connectionId;
    boolean commaSeparated = nameListMessage.commaSeparated;

    // get java.sql.Connection from connectionId
    Connection connection = ConnectionService.getConnection(connectionId);

    if (connection == null) {
      nameListMessage.errorMessage = kConnectionClosed;
    } else {

      try {
        Thread th = null;
        buffer = new BufferedWriter(writer);

        // if reader exists, create reading thread
        if (reader != null) {
          final ObjectNameReader objectReader = new ObjectNameReader(nameListMessage.nameList);

          // This thread will read stream until meets EOL (ie. when
          // writer is closed)
          th =
              new Thread() {
                public void run() {
                  objectReader.readInputFile(reader);
                }
              };
        } // end if

        // if no reader (offline reverse), never start the reading
        // thread
        // (because output is written down directly to the disk without
        // processing in counterpart).
        if (reader != null) th.start();

        // execute each SQL statement
        // we skip objects that don't need to be reversed
        Iterator iter = cmds.iterator();
        int index = -1;
        while (iter.hasNext()) {
          String cmd = (String) iter.next();
          index++;
          // check if this statement id is marked as <to skip>
          if (nameListMessage.ignoredStatementIds != null
              && nameListMessage.ignoredStatementIds.contains(new Integer(index))) {
            if (commaSeparated) {
              buffer.flush();
              buffer.newLine();
            }
            continue;
          }
          if (writer != null) {
            if (cmd != null) {
              try {
                execute(connection, cmd, buffer, commaSeparated);
              } catch (SQLException e) {
                if (Debug.isDebug()) e.printStackTrace();
                nameListMessage.errorMessage =
                    nameListMessage.errorMessage == null
                        ? e.toString()
                        : nameListMessage.errorMessage.concat("\n" + e.toString()); // NOT
                // LOCALIZABLE,
                // escape
                // code
              }
            }
            if (commaSeparated) {
              buffer.flush();
              buffer.newLine();
            }
          }
        }
      }
      /*
       * catch (SQLException ex) { if (Debug.isDebug()) ex.printStackTrace();
       * nameListMessage.errorMessage = nameListMessage.errorMessage == null ? ex.toString() :
       * nameListMessage.errorMessage.concat("\n" + ex.toString()); //NOT LOCALIZABLE, escape
       * code }
       */
      catch (IOException ex) {
        if (Debug.isDebug()) ex.printStackTrace();
        nameListMessage.errorMessage =
            nameListMessage.errorMessage == null
                ? ex.toString()
                : nameListMessage.errorMessage.concat(
                    "\n" + ex.toString()); // NOT LOCALIZABLE, escape
        // code
      }

      // close writer: the reading thread terminates.
      try {
        // WARNING: we must make a pause after closing the buffer
        // otherwise
        // the nameList variable won't be well initialized...strange!
        // [FG]
        buffer.close();
        Thread.sleep(400);
      } catch (IOException ex) {
      } catch (InterruptedException ex) {
      }
    } // end if
  } // end parseSqlFile()
Ejemplo n.º 23
0
  public static void main(String[] args) {

    try {
      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
      ;
      Connection conn =
          DriverManager.getConnection(
              "jdbc:oracle:thin:@192.168.117.110:1521:WFZF", "qzkj", "qzkj");
      conn.setAutoCommit(false);
      ;

      BLOB blob = null;

      PreparedStatement pstmt =
          conn.prepareStatement("insert into sys_file(uuid,content,name) values(?,empty_blob(),?)");
      String uuid = Tool.getStringUUid();
      pstmt.setString(1, uuid);
      pstmt.setString(2, "test.flv");
      pstmt.executeUpdate();
      pstmt.close();

      pstmt = conn.prepareStatement("select content from sys_file where uuid= ? for update");
      pstmt.setString(1, uuid);
      ResultSet rset = pstmt.executeQuery();
      ;
      if (rset.next()) blob = (BLOB) rset.getBlob(1);
      ;

      String fileName = "test.flv";
      File f =
          new File(
              "C://Documents and Settings//Administrator//桌面//FlvPlayer201002//FlvPlayer201002//"
                  + fileName);
      FileInputStream fin = new FileInputStream(f);
      ;
      System.out.println("file size = " + fin.available());

      pstmt = conn.prepareStatement("update sys_file set content=? where uuid=?");
      ;

      OutputStream out = blob.getBinaryOutputStream();
      ;

      int count = -1, total = 0;
      byte[] data = new byte[(int) fin.available()];
      fin.read(data);
      ;
      out.write(data);
      ;
      /*
      byte[] data = new byte[blob.getBufferSize();];  另一种实现方法,节省内存
      while ((count = fin.read(data);); != -1); {
        total += count;
        out.write(data, 0, count);;
      }
      */

      fin.close();
      ;
      out.close();
      ;

      pstmt.setBlob(1, blob);
      ;
      pstmt.setString(2, uuid);

      pstmt.executeUpdate();
      ;
      pstmt.close();
      ;

      conn.commit();
      ;
      conn.close();
      ;
    } catch (SQLException e) {
      System.err.println(e.getMessage());
      ;
      e.printStackTrace();
    } catch (IOException e) {
      System.err.println(e.getMessage());
      ;
    }
  }
Ejemplo n.º 24
0
  public void run() {
    ObjectInputStream din = null;
    try {
      din = new ObjectInputStream(socket.getInputStream());
      Packet packet = (Packet) din.readObject();
      username = packet.getSender();

      server.usernameSocketMapping.put(username, socket);
      server.isAlive.add(username);
      // Sh: Login validation message
      if (packet.getType() == 0) {
        try {
          String password = packet.getMessage();
          Statement st = Server.conn.createStatement();
          ResultSet rs =
              st.executeQuery(
                  "SELECT * FROM user_info where username='******' and password='******'");
          if (!rs.next()) { // User does not exist
            server.forwardToReceiver(new Packet(0, "Server", username, "Invalid"));
            synchronized (server.usernameSocketMapping) {
              server.usernameSocketMapping.remove(username);
            }
            synchronized (server.isAlive) {
              server.isAlive.remove(username);
            }
            return;
          } else { // Authentic user
            server.forwardToReceiver(new Packet(0, "Server", username, "Authentic"));
          }
        } catch (SQLException e1) {
          e1.printStackTrace();
        }
      }
      // Sh: Signup message
      if (packet.getType() == 2) {
        try {
          String[] message = packet.getMessage().split(":");
          String name = message[0];
          String password = message[1];
          // String sex=message[2];
          String username = message[2];
          Statement st = Server.conn.createStatement();
          ResultSet rs =
              st.executeQuery("SELECT * FROM user_info where username='******'");
          if (rs.next()) { // Username already present
            server.forwardToReceiver(new Packet(2, "Server", username, "Username present"));
            synchronized (server.usernameSocketMapping) {
              server.usernameSocketMapping.remove(username);
            }
            synchronized (server.isAlive) {
              server.isAlive.remove(username);
            }
            return;
          } else { // Register user
            String query =
                "INSERT INTO user_info VALUES('"
                    + name
                    + "','"
                    + password
                    + "',"
                    + "NULL,'"
                    + username
                    + "')";
            System.out.println(query);
            st.executeUpdate(query);
            server.forwardToReceiver(new Packet(2, "Server", username, "User Registererd"));
          }

        } catch (SQLException e1) {
          e1.printStackTrace();
        }
      }
      System.out.println("State of isAlive map is: " + server.isAlive);

      try {
        Statement st = Server.conn.createStatement();
        ResultSet rs =
            st.executeQuery(
                "SELECT sender,message,status FROM msg_buffer where receiver='"
                    + username
                    + "' order by sender,sno");
        String sendflag = null;
        String sender = null;
        while (rs.next()) {

          sender = rs.getString("sender");
          int status = rs.getInt("status");
          String message = rs.getString("message");
          System.out.println("sender: " + sender);
          System.out.println("receiver: " + username);
          System.out.println("message: " + message);
          server.forwardToReceiver(new Packet(status, sender, username, message));
          System.out.println("Sending " + packet);
          if (!sender.equals(sendflag)) {
            server.forwardToReceiver(new Packet(6, username, sender, "Message Received"));
            sendflag = sender;
          }
        }

        st.executeUpdate("DELETE FROM msg_buffer where receiver='" + username + "'");
      } catch (SQLException e1) {
        e1.printStackTrace();
      }

      while (true) {
        packet = (Packet) din.readObject();

        if (packet.getType() == 3) // is an OK message
        {
          try {
            packet.setSender(username);
            System.out.println("Is OK message");
            Statement st = Server.conn.createStatement();
            ResultSet rs =
                st.executeQuery(
                    "SELECT * FROM user_info where username='******'");
            if (rs.next()) {

              if (server.addFriend(packet.getSender(), packet.getReceiver(), true) == 1) {
                // packet.setMessage("Already in friend list");
                server.forwardToReceiver(
                    new Packet(1, "Server", packet.getSender(), "Already in friend list"));
                System.out.println("ALready in list");
              } else {
                System.out.println("Sending " + packet);
                server.forwardToReceiver(
                    new Packet(1, "Server", packet.getReceiver(), packet.getMessage()));
                server.forwardToReceiver(
                    new Packet(1, "Server", packet.getSender(), packet.getMessage()));
              }
            } else {
              server.forwardToReceiver(
                  new Packet(1, "Server", packet.getSender(), "User does not exist"));
            }
          } catch (SQLException e1) {
            e1.printStackTrace();
          }
        } else if ((packet.getType() == 8)) // is a getFriends message
        {
          packet.setSender(username);
          System.out.println("Type 8");
          /*
           * try { //Thread.sleep(3000); } catch (InterruptedException
           * e) { // TODO Auto-generated catch block
           * e.printStackTrace(); }
           */
          String FriendsMessage = getFriendsMessage(packet);
          server.forwardToReceiver(
              new Packet(8, "Server", packet.getSender(), "Friends|" + FriendsMessage));
        } else if (packet.getType() == 16) // Random conversation.
        {
          packet.setSender(username);
          String receiver = server.getRandomPerson(packet.getSender());
          if (!receiver.equals("No One Online")) {
            server.forwardToReceiver(
                new Packet(1, "Server", packet.getSender(), receiver + "| will talk to you now."));
            server.forwardToReceiver(
                new Packet(1, "Server", receiver, packet.getSender() + "| will talk to you now."));
          } else {
            System.out.println("No one online");
            server.forwardToReceiver(new Packet(16, "Server", packet.getSender(), "No One Online"));
            server.forwardToReceiver(new Packet(1, "Server", packet.getSender(), "No One Online"));
          }
        } else if (packet.getType() == 13) { // Webcam of live is started
          synchronized (server.webcamAliveMapping) {
            server.webcamAliveMapping.put(packet.getSender(), packet.getMessage());
          }
          System.out.println("State of webcamAliveMapping is: " + server.webcamAliveMapping);

        } else if (packet.getType() == 14) { // Webcam of client is
          // closed
          if (server.webcamAliveMapping.containsKey(packet.getSender())) {
            synchronized (server.webcamAliveMapping) {
              server.webcamAliveMapping.remove(packet.getSender());
            }
          }
        } else if (packet.getType() == 15) { // Client requested for
          // another client's webcam
          if (server.webcamAliveMapping.containsKey(
              packet.getReceiver())) { // User Webcam is available
            server.forwardToReceiver(
                new Packet(
                    15,
                    "Server",
                    packet.getSender(),
                    server.webcamAliveMapping.get(packet.getReceiver())));
          } else { // User Webcam not available
            server.forwardToReceiver(
                new Packet(15, "Server", packet.getSender(), "Webcam Not Found"));
            server.forwardToReceiver(
                new Packet(
                    1,
                    "Server",
                    packet.getSender(),
                    "Webcam for " + packet.getReceiver() + " is not available"));
          }
        } else if (packet.getType() == 17) {
          ArrayList<String> friends = new ArrayList<String>();
          friends = server.getfriendlist(packet.getSender());
          if (friends.isEmpty()) {
            server.forwardToReceiver(
                new Packet(1, "Server", packet.getSender(), "Your friendlist is empty."));
          } else {
            for (int i = 0; i < friends.size(); i++) {
              server.forwardToReceiver(
                  new Packet(1, packet.getSender(), friends.get(i), packet.getMessage()));
            }
          }

        } else {
          packet.setSender(username);
          System.out.println("Sending " + packet);
          server.forwardToReceiver(packet);
        }
      }
    } catch (EOFException ie) {
    } catch (IOException ie) {
      ie.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } finally {
      try {
        din.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
      synchronized (server.isAlive) {
        server.isAlive.remove(username);
      }
      synchronized (server.usernameSocketMapping) {
        server.usernameSocketMapping.remove(username);
      }
      System.out.println("State of isAlive map is: " + server.isAlive);
      server.removeConnection(socket);
    }
  }