Example usage for java.util UUID toString

List of usage examples for java.util UUID toString

Introduction

In this page you can find the example usage for java.util UUID toString.

Prototype

public String toString() 

Source Link

Document

Returns a String object representing this UUID .

Usage

From source file:com.ngdata.sep.impl.SepModelImpl.java

@Override
public boolean addSubscriptionSilent(String name) throws InterruptedException, KeeperException, IOException {
    ReplicationAdmin replicationAdmin = new ReplicationAdmin(hbaseConf);
    try {//  w w w . j a v  a 2s .  co m
        String internalName = toInternalSubscriptionName(name);
        if (replicationAdmin.listPeers().containsKey(internalName)) {
            return false;
        }

        String basePath = baseZkPath + "/" + internalName;
        UUID uuid = UUID.nameUUIDFromBytes(Bytes.toBytes(internalName)); // always gives the same uuid for the same name
        ZkUtil.createPath(zk, basePath + "/hbaseid", Bytes.toBytes(uuid.toString()));
        ZkUtil.createPath(zk, basePath + "/rs");

        try {
            replicationAdmin.addPeer(internalName, zkQuorumString + ":" + zkClientPort + ":" + basePath);
        } catch (IllegalArgumentException e) {
            if (e.getMessage().equals("Cannot add existing peer")) {
                return false;
            }
            throw e;
        } catch (Exception e) {
            // HBase 0.95+ throws at least one extra exception: ReplicationException which we convert into IOException
            if (e instanceof InterruptedException) {
                throw (InterruptedException) e;
            } else if (e instanceof KeeperException) {
                throw (KeeperException) e;
            } else {
                throw new IOException(e);
            }
        }

        return true;
    } finally {
        Closer.close(replicationAdmin);
    }
}

From source file:com.stratio.ingestion.sink.cassandra.EventParserTest.java

@Test
public void shouldParsePrimitiveTypes() throws Exception {
    Object integer = EventParser.parseValue("1", DataType.Name.INT);
    assertThat(integer).isInstanceOf(Integer.class).isEqualTo(1);
    integer = EventParser.parseValue(Integer.toString(Integer.MAX_VALUE), DataType.Name.INT);
    assertThat(integer).isInstanceOf(Integer.class).isEqualTo(Integer.MAX_VALUE);
    integer = EventParser.parseValue(Integer.toString(Integer.MIN_VALUE), DataType.Name.INT);
    assertThat(integer).isInstanceOf(Integer.class).isEqualTo(Integer.MIN_VALUE);
    integer = EventParser.parseValue(" 1 2 ", DataType.Name.INT);
    assertThat(integer).isInstanceOf(Integer.class).isEqualTo(12);

    Object counter = EventParser.parseValue("1", DataType.Name.COUNTER);
    assertThat(counter).isEqualTo(1L);//from ww w  .  j  a  v  a  2 s  . com
    counter = EventParser.parseValue(Long.toString(Long.MAX_VALUE), DataType.Name.COUNTER);
    assertThat(counter).isEqualTo(Long.MAX_VALUE);
    counter = EventParser.parseValue(Long.toString(Long.MIN_VALUE), DataType.Name.COUNTER);
    assertThat(counter).isEqualTo(Long.MIN_VALUE);
    counter = EventParser.parseValue(" 1 2 ", DataType.Name.COUNTER);
    assertThat(counter).isEqualTo(12L);

    Object _float = EventParser.parseValue("1", DataType.Name.FLOAT);
    assertThat(_float).isInstanceOf(Float.class).isEqualTo(1f);
    _float = EventParser.parseValue("1.0", DataType.Name.FLOAT);
    assertThat(_float).isInstanceOf(Float.class).isEqualTo(1f);
    _float = EventParser.parseValue(Float.toString(Float.MAX_VALUE), DataType.Name.FLOAT);
    assertThat(_float).isInstanceOf(Float.class).isEqualTo(Float.MAX_VALUE);
    _float = EventParser.parseValue(Float.toString(Float.MIN_VALUE), DataType.Name.FLOAT);
    assertThat(_float).isInstanceOf(Float.class).isEqualTo(Float.MIN_VALUE);
    _float = EventParser.parseValue(" 1 . 0 ", DataType.Name.FLOAT);
    assertThat(_float).isInstanceOf(Float.class).isEqualTo(1f);

    Object _double = EventParser.parseValue("1", DataType.Name.DOUBLE);
    assertThat(_double).isInstanceOf(Double.class).isEqualTo(1.0);
    _double = EventParser.parseValue("0", DataType.Name.DOUBLE);
    assertThat(_double).isInstanceOf(Double.class).isEqualTo(0.0);
    _double = EventParser.parseValue(Double.toString(Double.MAX_VALUE), DataType.Name.DOUBLE);
    assertThat(_double).isInstanceOf(Double.class).isEqualTo(Double.MAX_VALUE);
    _double = EventParser.parseValue(Double.toString(Double.MIN_VALUE), DataType.Name.DOUBLE);
    assertThat(_double).isInstanceOf(Double.class).isEqualTo(Double.MIN_VALUE);
    _double = EventParser.parseValue(" 1 . 0 ", DataType.Name.DOUBLE);
    assertThat(_double).isInstanceOf(Double.class).isEqualTo(1.0);

    for (DataType.Name type : Arrays.asList(DataType.Name.BIGINT)) {
        Object bigInteger = EventParser.parseValue("1", type);
        assertThat(bigInteger).isInstanceOf(Long.class).isEqualTo(1L);
        bigInteger = EventParser.parseValue("0", type);
        assertThat(bigInteger).isInstanceOf(Long.class).isEqualTo(0L);
        bigInteger = EventParser.parseValue(Long.toString(Long.MAX_VALUE), type);
        assertThat(bigInteger).isInstanceOf(Long.class).isEqualTo(Long.MAX_VALUE);
        bigInteger = EventParser.parseValue(Long.toString(Long.MIN_VALUE), type);
        assertThat(bigInteger).isInstanceOf(Long.class).isEqualTo(Long.MIN_VALUE);
    }

    for (DataType.Name type : Arrays.asList(DataType.Name.VARINT)) {
        Object bigInteger = EventParser.parseValue("1", type);
        assertThat(bigInteger).isInstanceOf(BigInteger.class).isEqualTo(BigInteger.ONE);
        bigInteger = EventParser.parseValue("0", type);
        assertThat(bigInteger).isInstanceOf(BigInteger.class).isEqualTo(BigInteger.ZERO);
        bigInteger = EventParser.parseValue(
                BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.valueOf(2L)).toString(), type);
        assertThat(bigInteger).isInstanceOf(BigInteger.class)
                .isEqualTo(BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.valueOf(2L)));
        bigInteger = EventParser.parseValue(
                BigInteger.valueOf(Long.MIN_VALUE).multiply(BigInteger.valueOf(2L)).toString(), type);
        assertThat(bigInteger).isInstanceOf(BigInteger.class)
                .isEqualTo(BigInteger.valueOf(Long.MIN_VALUE).multiply(BigInteger.valueOf(2L)));
    }

    Object bigDecimal = EventParser.parseValue("1", DataType.Name.DECIMAL);
    assertThat(bigDecimal).isInstanceOf(BigDecimal.class).isEqualTo(BigDecimal.valueOf(1));
    bigDecimal = EventParser.parseValue("0", DataType.Name.DECIMAL);
    assertThat(bigDecimal).isInstanceOf(BigDecimal.class).isEqualTo(BigDecimal.valueOf(0));
    bigDecimal = EventParser.parseValue(
            BigDecimal.valueOf(Double.MAX_VALUE).multiply(BigDecimal.valueOf(2)).toString(),
            DataType.Name.DECIMAL);
    assertThat(bigDecimal).isInstanceOf(BigDecimal.class)
            .isEqualTo(BigDecimal.valueOf(Double.MAX_VALUE).multiply(BigDecimal.valueOf(2)));
    bigDecimal = EventParser.parseValue(
            BigDecimal.valueOf(Double.MIN_VALUE).multiply(BigDecimal.valueOf(2)).toString(),
            DataType.Name.DECIMAL);
    assertThat(bigDecimal).isInstanceOf(BigDecimal.class)
            .isEqualTo(BigDecimal.valueOf(Double.MIN_VALUE).multiply(BigDecimal.valueOf(2)));
    bigDecimal = EventParser.parseValue(" 1 2 ", DataType.Name.DECIMAL);
    assertThat(bigDecimal).isInstanceOf(BigDecimal.class).isEqualTo(BigDecimal.valueOf(12));

    Object string = EventParser.parseValue("string", DataType.Name.TEXT);
    assertThat(string).isInstanceOf(String.class).isEqualTo("string");

    Object bool = EventParser.parseValue("true", DataType.Name.BOOLEAN);
    assertThat(bool).isInstanceOf(Boolean.class).isEqualTo(true);

    Object addr = EventParser.parseValue("192.168.1.1", DataType.Name.INET);
    assertThat(addr).isInstanceOf(InetAddress.class).isEqualTo(InetAddress.getByName("192.168.1.1"));

    UUID randomUUID = UUID.randomUUID();
    Object uuid = EventParser.parseValue(randomUUID.toString(), DataType.Name.UUID);
    assertThat(uuid).isInstanceOf(UUID.class).isEqualTo(randomUUID);
}

From source file:de.matzefratze123.heavyspleef.core.uuid.UUIDManager.java

private GameProfile fetchGameProfile(UUID uuid) throws IOException, ParseException {
    String uuidString = uuid.toString().replace("-", "");
    URL url = new URL(UUID_BASE_URL + uuidString);

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    InputStream inputStream = connection.getInputStream();

    JSONObject result = (JSONObject) parser.parse(new InputStreamReader(inputStream));
    String name = (String) result.get("name");
    if (name == null) {
        return null;
    }/*from w w  w .  j  a v a2 s.c o  m*/

    GameProfile profile = new GameProfile(uuid, name);
    return profile;
}

From source file:com.esri.geoportal.harvester.beans.TriggerManagerBean.java

@Override
public TaskUuidTriggerDefinitionPair read(UUID id) throws CrudlException {
    try (Connection connection = dataSource.getConnection();
            PreparedStatement st = connection.prepareStatement("SELECT * FROM TRIGGERS WHERE ID = ?");) {
        st.setString(1, id.toString());
        ResultSet rs = st.executeQuery();
        if (rs.next()) {
            try {
                return deserialize(rs.getString("definition"), TaskUuidTriggerDefinitionPair.class);
            } catch (IOException | SQLException ex) {
                LOG.warn("Error reading broker definition", ex);
            }//w  w w  .  j ava  2  s . c  om
        }
    } catch (SQLException ex) {
        throw new CrudlException("Error selecting broker definition", ex);
    }

    return null;
}

From source file:org.ohmage.query.impl.ImageQueries.java

@Override
public URL getImageUrl(UUID imageId) throws DataAccessException {
    try {/*w  w  w. j  a v a 2 s .co  m*/
        return new URL(getJdbcTemplate().queryForObject(SQL_GET_IMAGE_URL, new Object[] { imageId.toString() },
                String.class));
    } catch (org.springframework.dao.IncorrectResultSizeDataAccessException e) {
        if (e.getActualSize() > 1) {
            throw new DataAccessException("Multiple images have the same unique identifier.", e);
        }

        return null;
    } catch (org.springframework.dao.DataAccessException e) {
        throw new DataAccessException(
                "Error executing SQL '" + SQL_GET_IMAGE_URL + "' with parameter: " + imageId, e);
    } catch (MalformedURLException e) {
        throw new DataAccessException("The URL was not a valid URL.", e);
    }
}

From source file:org.ovirt.engine.sdk.decorators.Users.java

/**
 * Fetches User object by id./*from   w  w w .ja va  2  s  . c om*/
 *
 * @return {@link User }
 *
 * @throws ClientProtocolException
 *             Signals that HTTP/S protocol error has occurred.
 * @throws ServerException
 *             Signals that an oVirt api error has occurred.
 * @throws IOException
 *             Signals that an I/O exception of some sort has occurred.
 */
@Override
public User get(UUID id) throws ClientProtocolException, ServerException, IOException {
    String url = SLASH + getName() + SLASH + id.toString();
    return getProxy().get(url, org.ovirt.engine.sdk.entities.User.class, User.class);
}

From source file:com.esri.geoportal.harvester.beans.TaskManagerBean.java

@Override
public TaskDefinition read(UUID id) throws CrudlException {
    try (Connection connection = dataSource.getConnection();
            PreparedStatement st = connection.prepareStatement("SELECT * FROM TASKS WHERE ID = ?");) {
        st.setString(1, id.toString());
        ResultSet rs = st.executeQuery();
        if (rs.next()) {
            try {
                TaskDefinition taskDef = deserialize(rs.getString("taskDefinition"), TaskDefinition.class);
                taskDef.setRef(id.toString());
                return taskDef;
            } catch (IOException | SQLException ex) {
                LOG.warn("Error reading task definition", ex);
            }//from   w  w  w.j  a  v  a 2 s .c om
        }
    } catch (SQLException ex) {
        throw new CrudlException("Error selecting task", ex);
    }

    return null;
}

From source file:com.smartitengineering.event.hub.spi.db.DBHubPersistorITCase.java

public void testGetEventByUUID() {
    final String content = "<xml>some xml</xml>";
    final String contentType = "application/xml";
    final HubPersistentStorer storer = HubPersistentStorerSPI.getInstance().getStorer();
    UUID uuid = UUID.randomUUID();
    String uuidStr = uuid.toString();
    Event event = APIFactory.getEventBuilder()
            .eventContent(APIFactory.getContent(contentType, IOUtils.toInputStream(content))).uuid(uuidStr)
            .build();/*from   w  w  w . ja  v  a 2 s .  c  o  m*/
    event = storer.create(event);
    String placeholderId = event.getPlaceholderId();
    assertEquals(uuidStr, event.getUniversallyUniqueID());
    event = storer.getEvent(event.getPlaceholderId());
    assertEquals(uuidStr, event.getUniversallyUniqueID());
    event = storer.getEventByUUID(uuidStr);
    assertEquals(uuidStr, event.getUniversallyUniqueID());
    assertEquals(placeholderId, event.getPlaceholderId());
    assertNull(storer.getEventByUUID(null));
    assertNull(storer.getEventByUUID(""));
    assertNull(storer.getEventByUUID("aab"));
}

From source file:com.ning.arecibo.collector.TestEventCollectorServer.java

@Test(groups = "slow")
public void testJsonClientIntegration() throws Exception {
    final RESTEventService service = createService(new JsonEventSerializer());

    Assert.assertEquals(processor.getEventsReceived(), 0);
    Assert.assertEquals(timelineEventHandler.getEventsDiscarded(), 0);

    final UUID hostUUID = UUID.randomUUID();
    final String hostName = hostUUID.toString();
    final int eventTypeId = timelineDAO.getOrAddEventCategory(EVENT_TYPE);
    final Integer hostId = timelineDAO.getOrAddHost(hostName);
    Assert.assertNotNull(hostId);// w  w w  .j  a v  a  2  s .c  om

    final Integer minHeapUserKindId = timelineDAO.getOrAddSampleKind(hostId, eventTypeId, MIN_HEAPUSED_KIND);
    Assert.assertNotNull(minHeapUserKindId);
    final Integer maxHeapUserKindId = timelineDAO.getOrAddSampleKind(hostId, eventTypeId, MAX_HEAPUSED_KIND);
    Assert.assertNotNull(maxHeapUserKindId);

    final long now = System.currentTimeMillis();
    final DateTime startTime = new DateTime(now - (now % 1000), DateTimeZone.UTC);
    DateTime endTime = startTime;
    final int sampleCount = 10;
    for (int i = 0; i < sampleCount; i++) {
        endTime = startTime.plusSeconds(i * 10);
        final MapEvent event = createEvent(hostUUID, endTime.getMillis());
        service.sendREST(event);

        Assert.assertEquals(processor.getEventsReceived(), 1 + i);
        Assert.assertEquals(timelineEventHandler.getEventsDiscarded(), 0);
        Assert.assertEquals(timelineEventHandler.getAccumulators().size(), 1);

        // Make sure we don't create dups
        final BiMap<Integer, String> hosts = timelineDAO.getHosts();
        Assert.assertEquals(hosts.values().size(), 1);
        Assert.assertEquals(hosts.values().toArray()[0], hostName);

        // Make sure we saw all sample kinds
        final BiMap<Integer, CategoryIdAndSampleKind> categoryIdsAndSampleKinds = timelineDAO.getSampleKinds();
        Assert.assertEquals(categoryIdsAndSampleKinds.values().size(), event.getKeys().size());
        final List<String> sampleKinds = CategoryIdAndSampleKind
                .extractSampleKinds(categoryIdsAndSampleKinds.values());
        Assert.assertTrue(sampleKinds.contains(MIN_HEAPUSED_KIND));
        Assert.assertTrue(sampleKinds.contains(MAX_HEAPUSED_KIND));
    }

    timelineEventHandler.forceCommit();
    backgroundWriter.initiateShutdown();

    final AccumulatorConsumer consumer = new AccumulatorConsumer();
    timelineDAO.getSamplesByHostIdsAndSampleKindIds(ImmutableList.<Integer>of(hostId),
            ImmutableList.<Integer>of(minHeapUserKindId, maxHeapUserKindId), startTime, endTime, consumer);
    final List<TimelineChunk> chunks = consumer.getAccumulator();
    // 1 host x 2 sample kinds
    Assert.assertEquals(chunks.size(), 2);
    // Only one
    Assert.assertEquals((Integer) chunks.get(0).getHostId(), hostId);
    Assert.assertEquals((Integer) chunks.get(1).getHostId(), hostId);
    // Two types
    Assert.assertTrue((chunks.get(0).getSampleKindId() == minHeapUserKindId)
            || chunks.get(0).getSampleKindId() == maxHeapUserKindId);
    Assert.assertTrue((chunks.get(1).getSampleKindId() == minHeapUserKindId)
            || chunks.get(1).getSampleKindId() == maxHeapUserKindId);

    // Only one
    Assert.assertEquals(chunks.get(0).getHostId(), (int) hostId);
    Assert.assertEquals(chunks.get(1).getHostId(), (int) hostId);
    // Two types
    Assert.assertTrue(chunks.get(0).getSampleKindId() == minHeapUserKindId
            || chunks.get(0).getSampleKindId() == maxHeapUserKindId);
    Assert.assertTrue(chunks.get(1).getSampleKindId() == minHeapUserKindId
            || chunks.get(1).getSampleKindId() == maxHeapUserKindId);

    // Number of events sent
    Assert.assertEquals(chunks.get(0).getSampleCount(), sampleCount);
    Assert.assertEquals(chunks.get(1).getSampleCount(), sampleCount);

    // Only one
    Assert.assertEquals(chunks.get(0).getHostId(), (int) hostId);
    Assert.assertEquals(chunks.get(1).getHostId(), (int) hostId);
    // When we started sending events (we store seconds granularity)
    Assert.assertEquals(chunks.get(0).getStartTime().getMillis() / 1000, startTime.getMillis() / 1000);
    Assert.assertEquals(chunks.get(1).getStartTime().getMillis() / 1000, startTime.getMillis() / 1000);
    // When we finished sending events (we store seconds granularity)
    Assert.assertEquals(chunks.get(0).getEndTime().getMillis() / 1000, endTime.getMillis() / 1000);
    Assert.assertEquals(chunks.get(1).getEndTime().getMillis() / 1000, endTime.getMillis() / 1000);
    // Each event was sent at a separate time
    Assert.assertEquals(chunks.get(0).getSampleCount(), sampleCount);
    Assert.assertEquals(chunks.get(1).getSampleCount(), sampleCount);
    // Check all the timelines events
    final TimelineCursor timeCursor0 = new TimelineCursorImpl(chunks.get(0).getTimes(),
            chunks.get(0).getSampleCount());
    final TimelineCursor timeCursor1 = new TimelineCursorImpl(chunks.get(1).getTimes(),
            chunks.get(1).getSampleCount());
    for (int i = 0; i < sampleCount; i++) {
        Assert.assertEquals(timeCursor0.getNextTime(), startTime.plusSeconds(i * 10));
        Assert.assertEquals(timeCursor1.getNextTime(), startTime.plusSeconds(i * 10));
    }
}