예제 #1
0
  /**
   * Inserts more entries in the random numbers pool. Numbers are obtained from the QRBG service.
   * The total amount of integers inserted can be calculated as ({@link #NUMBER_OF_BYTES}/4 * {@link
   * #NUMBER_OF_REQUESTS}.
   *
   * @throws IOException If there is no Internet connectivity.
   * @throws ServiceDeniedException If the QRBG account is not valid.
   * @see <a href="http://random.irb.hr/">QRBG Service</a>
   * @since 1.0
   */
  private void insertMoreNumbersInPool() throws IOException, ServiceDeniedException {
    QRBG source = new QRBG(SettingBusiness.getQRBGUsername(), SettingBusiness.getQRBGPassword());
    for (int request = 0; request < NUMBER_OF_REQUESTS; request++) {
      byte[] buffer = new byte[NUMBER_OF_BYTES];
      source.getBytes(buffer, NUMBER_OF_BYTES);

      Session session = HibernateUtil.getSession();
      try {
        session.beginTransaction();
        for (int i = 0; i < (NUMBER_OF_BYTES - 4); i += 4) {
          double randomNumber = QRBG.readInt(buffer, i);
          if (randomNumber < 0) {
            randomNumber *= -1;
          }
          randomNumber /= Integer.MAX_VALUE;

          if (randomNumber != 0) {
            session.save(new RandomNumber(randomNumber));
          }
        }
        session.getTransaction().commit();
      } catch (HibernateException ex) {
        session.getTransaction().rollback();
        Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
      } finally {
        HibernateUtil.closeSession(session);
      }
    }
  }
예제 #2
0
  /**
   * Gets the given amount of random numbers from the random numbers pool. The effectiveness of this
   * method depends on a proper configuration of a valid account on the QRBG service and an active
   * Internet connection. Also, the QRBG service uses the port 1227. Make sure your firewall is
   * configured properly.
   *
   * @param amount the amount of random numbers to get.
   * @throws IOException If there is no Internet connectivity.
   * @throws ServiceDeniedException If the QRBG account is not valid.
   * @see <a href="http://random.irb.hr/">QRBG Service</a>
   * @see RandomNumber
   * @since 1.0
   */
  public List<Double> getRandomNumbers(int amount) throws IOException, ServiceDeniedException {
    List<RandomNumber> numbersList = getRandomNumbersFromPool(amount);

    if (numbersList.size() < amount) {
      insertMoreNumbersInPool();
      numbersList = getRandomNumbersFromPool(amount);
    }

    List<Double> returnList = new ArrayList<Double>();
    Session session = HibernateUtil.getSession();
    try {
      session.beginTransaction();
      for (int i = 0; i < amount; i++) {
        RandomNumber randomNumber = numbersList.get(i);
        returnList.add(randomNumber.getValue());
        session.delete(randomNumber);
      }
      session.getTransaction().commit();
    } catch (HibernateException ex) {
      session.getTransaction().rollback();
      Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      HibernateUtil.closeSession(session);
    }

    return returnList;
  }
예제 #3
0
  /**
   * Gets a list of {@link RandomNumber} registries from the random numbers pool.
   *
   * @param amount the amount of random numbers to get from the pool.
   * @return a list of random numbers from the pool. The resulting list has, at most, the number of
   *     elements given by <code>amount</code>.
   * @see RandomNumber
   * @since 1.0
   */
  private List<RandomNumber> getRandomNumbersFromPool(int amount) {
    Session session = HibernateUtil.getSession();
    List<RandomNumber> numbersList = new ArrayList<RandomNumber>();
    try {
      numbersList =
          (List<RandomNumber>)
              session.createCriteria(RandomNumber.class).setMaxResults(amount).list();
    } catch (HibernateException ex) {
      Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      HibernateUtil.closeSession(session);
    }

    return numbersList;
  }