Example usage for com.fasterxml.jackson.databind ObjectMapper writeValueAsBytes

List of usage examples for com.fasterxml.jackson.databind ObjectMapper writeValueAsBytes

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper writeValueAsBytes.

Prototype

@SuppressWarnings("resource")
public byte[] writeValueAsBytes(Object value) throws JsonProcessingException 

Source Link

Document

Method that can be used to serialize any Java value as a byte array.

Usage

From source file:org.commonjava.maven.galley.transport.htcli.internal.AbstractHttpJob.java

private void writeMetadata(final Transfer target, final ObjectMapper mapper) {
    if (target == null || request == null || response == null) {
        logger.trace("Cannot write HTTP exchange metadata. Request: {}. Response: {}. Transfer: {}", request,
                response, target);//from w  w w .  j  a v  a 2s. c om
        return;
    }

    logger.trace("Writing HTTP exchange metadata. Request: {}. Response: {}", request, response);
    Transfer metaTxfr = target.getSiblingMeta(HttpExchangeMetadata.FILE_EXTENSION);
    if (metaTxfr == null) {
        if (target.isDirectory()) {
            logger.trace("DIRECTORY. Using HTTP exchange metadata file INSIDE directory called: {}",
                    HttpExchangeMetadata.FILE_EXTENSION);
            metaTxfr = target.getChild(HttpExchangeMetadata.FILE_EXTENSION);
        } else {
            logger.trace("SKIP: Cannot retrieve HTTP exchange metadata Transfer instance for: {}", target);
            return;
        }
    }

    final HttpExchangeMetadata metadata = new HttpExchangeMetadata(request, response);
    OutputStream out = null;
    try {
        final Transfer finalMeta = metaTxfr;
        out = metaTxfr.openOutputStream(TransferOperation.GENERATE, false);
        logger.trace("Writing HTTP exchange metadata:\n\n{}\n\n", new Object() {
            @Override
            public String toString() {
                try {
                    return mapper.writeValueAsString(metadata);
                } catch (final JsonProcessingException e) {
                    logger.warn(String.format("Failed to write HTTP exchange metadata: %s. Reason: %s",
                            finalMeta, e.getMessage()), e);
                }

                return "ERROR RENDERING METADATA";
            }
        });

        out.write(mapper.writeValueAsBytes(metadata));
    } catch (final IOException e) {
        if (logger.isTraceEnabled()) {
            logger.trace(String.format("Failed to write metadata for HTTP exchange to: %s. Reason: %s",
                    metaTxfr, e.getMessage()), e);
        } else {
            logger.warn("Failed to write metadata for HTTP exchange to: {}. Reason: {}", metaTxfr,
                    e.getMessage());
        }
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:com.jivesoftware.os.amza.deployable.Main.java

public void run(String[] args) throws Exception {

    String hostname = args[0];// w  w  w  .j  a  va  2s.c  o  m
    String clusterName = (args.length > 1 ? args[1] : "unnamed");
    String hostPortPeers = (args.length > 2 ? args[2] : null);

    int port = Integer.parseInt(System.getProperty("amza.port", "1175"));
    String multicastGroup = System.getProperty("amza.discovery.group", "225.4.5.6");
    int multicastPort = Integer.parseInt(System.getProperty("amza.discovery.port", "1223"));

    String logicalName = System.getProperty("amza.logicalName", hostname + ":" + port);
    String datacenter = System.getProperty("host.datacenter", "unknownDatacenter");
    String rack = System.getProperty("host.rack", "unknownRack");

    RingMember ringMember = new RingMember(logicalName);
    RingHost ringHost = new RingHost(datacenter, rack, hostname, port);

    // todo need a better way to create writer id.
    int writerId = Integer.parseInt(System.getProperty("amza.id", String.valueOf(new Random().nextInt(512))));

    SnowflakeIdPacker idPacker = new SnowflakeIdPacker();
    JiveEpochTimestampProvider timestampProvider = new JiveEpochTimestampProvider();
    final TimestampedOrderIdProvider orderIdProvider = new OrderIdProviderImpl(
            new ConstantWriterIdProvider(writerId), idPacker, timestampProvider);

    final ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.INDENT_OUTPUT, false);

    final AmzaServiceConfig amzaServiceConfig = new AmzaServiceConfig();
    final AmzaStats amzaSystemStats = new AmzaStats();
    final AmzaStats amzaStats = new AmzaStats();
    final SickThreads sickThreads = new SickThreads();
    final SickPartitions sickPartitions = new SickPartitions();

    AtomicInteger systemRingSize = new AtomicInteger(-1);
    amzaServiceConfig.workingDirectories = System.getProperty("amza.working.dirs", "./data1,./data2,./data3")
            .split(",");
    amzaServiceConfig.systemRingSize = Integer.parseInt(System.getProperty("amza.system.ring.size", "-1"));
    if (amzaServiceConfig.systemRingSize > 0) {
        systemRingSize.set(amzaServiceConfig.systemRingSize);
    }

    AmzaInterner amzaInterner = new AmzaInterner();

    PartitionPropertyMarshaller partitionPropertyMarshaller = new PartitionPropertyMarshaller() {
        @Override
        public PartitionProperties fromBytes(byte[] bytes) {
            try {
                return mapper.readValue(bytes, PartitionProperties.class);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }

        @Override
        public byte[] toBytes(PartitionProperties partitionProperties) {
            try {
                return mapper.writeValueAsBytes(partitionProperties);
            } catch (JsonProcessingException ex) {
                throw new RuntimeException(ex);
            }
        }
    };

    //  hmmm
    LABPointerIndexConfig labConfig = BindInterfaceToConfiguration.bindDefault(LABPointerIndexConfig.class);
    labConfig.setLeapCacheMaxCapacity(
            Integer.parseInt(System.getProperty("amza.leap.cache.max.capacity", "1000000")));

    BinaryPrimaryRowMarshaller primaryRowMarshaller = new BinaryPrimaryRowMarshaller(); // hehe you cant change this :)
    BinaryHighwaterRowMarshaller highwaterRowMarshaller = new BinaryHighwaterRowMarshaller(amzaInterner);

    AtomicReference<Callable<RingTopology>> topologyProvider = new AtomicReference<>(); // bit of a hack

    InstanceDescriptor instanceDescriptor = new InstanceDescriptor(datacenter, rack, "", "", "", "", "", "", "",
            "", 0, "", "", "", 0L, true);
    ConnectionDescriptorsProvider connectionsProvider = (connectionDescriptorsRequest,
            expectedReleaseGroup) -> {
        try {
            RingTopology systemRing = topologyProvider.get().call();
            List<ConnectionDescriptor> descriptors = Lists.newArrayList(Iterables.transform(systemRing.entries,
                    input -> new ConnectionDescriptor(instanceDescriptor, false, false,
                            new HostPort(input.ringHost.getHost(), input.ringHost.getPort()),
                            Collections.emptyMap(), Collections.emptyMap())));
            return new ConnectionDescriptorsResponse(200, Collections.emptyList(), "", descriptors,
                    connectionDescriptorsRequest.getRequestUuid());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    };
    TenantsServiceConnectionDescriptorProvider<String> connectionPoolProvider = new TenantsServiceConnectionDescriptorProvider<>(
            Executors.newScheduledThreadPool(1), "", connectionsProvider, "", "", 10_000); // TODO config
    connectionPoolProvider.start();

    TenantAwareHttpClient<String> httpClient = new TenantRoutingHttpClientInitializer<String>(null)
            .builder(connectionPoolProvider, new HttpDeliveryClientHealthProvider("", null, "", 5000, 100))
            .deadAfterNErrors(10).checkDeadEveryNMillis(10_000).maxConnections(1_000)
            .socketTimeoutInMillis(60_000).build(); //TODO expose to conf

    AvailableRowsTaker availableRowsTaker = new HttpAvailableRowsTaker(httpClient, amzaInterner, mapper); // TODO config
    AquariumStats aquariumStats = new AquariumStats();

    AmzaService amzaService = new AmzaServiceInitializer().initialize(amzaServiceConfig, amzaInterner,
            aquariumStats, amzaSystemStats, amzaStats,
            new HealthTimer(CountersAndTimers.getOrCreate("quorumLatency"), "quorumLatency",
                    new NoOpHealthChecker<>("quorumLatency")),
            () -> amzaServiceConfig.systemRingSize, sickThreads, sickPartitions, primaryRowMarshaller,
            highwaterRowMarshaller, ringMember, ringHost, Collections.emptySet(), orderIdProvider, idPacker,
            partitionPropertyMarshaller, (workingIndexDirectories, indexProviderRegistry,
                    ephemeralRowIOProvider, persistentRowIOProvider, partitionStripeFunction) -> {
                indexProviderRegistry
                        .register(
                                new BerkeleyDBWALIndexProvider(BerkeleyDBWALIndexProvider.INDEX_CLASS_NAME,
                                        partitionStripeFunction, workingIndexDirectories),
                                persistentRowIOProvider);

                indexProviderRegistry.register(new LABPointerIndexWALIndexProvider(amzaInterner, labConfig,
                        Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
                        Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
                        LABPointerIndexWALIndexProvider.INDEX_CLASS_NAME, partitionStripeFunction,
                        workingIndexDirectories), persistentRowIOProvider);
            }, availableRowsTaker, () -> {
                return new HttpRowsTaker("system", amzaStats, httpClient, mapper, amzaInterner,
                        Executors.newSingleThreadExecutor(), Executors.newCachedThreadPool());
            }, () -> {
                return new HttpRowsTaker("striped", amzaStats, httpClient, mapper, amzaInterner,
                        Executors.newSingleThreadExecutor(), Executors.newCachedThreadPool());
            }, Optional.absent(), (changes) -> {
            }, (threadCount, name) -> {
                return Executors.newCachedThreadPool();
            });

    topologyProvider.set(() -> amzaService.getRingReader().getRing(AmzaRingReader.SYSTEM_RING, -1));

    TailAtScaleStrategy tailAtScaleStrategy = new TailAtScaleStrategy(
            BoundedExecutor.newBoundedExecutor(1024, "tas"), 100, // TODO config
            95, // TODO config
            1000 // TODO config
    );

    AmzaClientProvider<HttpClient, HttpClientException> clientProvider = new AmzaClientProvider<>(
            new HttpPartitionClientFactory(),
            new HttpPartitionHostsProvider(httpClient, tailAtScaleStrategy, mapper),
            new RingHostHttpClientProvider(httpClient), BoundedExecutor.newBoundedExecutor(1024, "amza-client"),
            10_000, //TODO expose to conf
            -1, -1);

    final JerseyEndpoints jerseyEndpoints = new JerseyEndpoints().addEndpoint(AmzaEndpoints.class)
            .addInjectable(AmzaService.class, amzaService).addEndpoint(AmzaReplicationRestEndpoints.class)
            .addInjectable(AmzaInstance.class, amzaService).addEndpoint(AmzaClientRestEndpoints.class)
            .addInjectable(AmzaInterner.class, amzaInterner).addInjectable(ObjectMapper.class, mapper)
            .addInjectable(AmzaClientService.class, new AmzaClientService(amzaService.getRingReader(),
                    amzaService.getRingWriter(), amzaService));

    new AmzaUIInitializer().initialize(clusterName, ringHost, amzaService, clientProvider, aquariumStats,
            amzaStats, timestampProvider, idPacker, amzaInterner, new AmzaUIInitializer.InjectionCallback() {
                @Override
                public void addEndpoint(Class clazz) {
                    System.out.println("Adding endpoint=" + clazz);
                    jerseyEndpoints.addEndpoint(clazz);
                }

                @Override
                public void addInjectable(Class clazz, Object instance) {
                    System.out.println("Injecting " + clazz + " " + instance);
                    jerseyEndpoints.addInjectable(clazz, instance);
                }

                @Override
                public void addSessionAuth(String... paths) throws Exception {
                    System.out.println("Ignoring session auth request for paths: " + Arrays.toString(paths));
                }
            });

    InitializeRestfulServer initializeRestfulServer = new InitializeRestfulServer(false, port, "AmzaNode",
            false, null, null, null, 128, 10000);
    initializeRestfulServer.addClasspathResource("/resources");
    initializeRestfulServer.addContextHandler("/", jerseyEndpoints);
    RestfulServer restfulServer = initializeRestfulServer.build();
    restfulServer.start();

    System.out.println("-----------------------------------------------------------------------");
    System.out.println("|      Jetty Service Online");
    System.out.println("-----------------------------------------------------------------------");

    amzaService.start(ringMember, ringHost);

    System.out.println("-----------------------------------------------------------------------");
    System.out.println("|      Amza Service Online");
    System.out.println("-----------------------------------------------------------------------");

    if (clusterName != null) {
        if (hostPortPeers != null) {
            System.out.println("-----------------------------------------------------------------------");
            System.out.println("|     Amza Service is in manual Discovery mode. Cluster Name:" + clusterName);
            String[] peers = hostPortPeers.split(",");
            for (String peer : peers) {
                String[] hostPort = peer.trim().split(":");
                if (hostPort.length != 2 && hostPort.length != 3) {
                    System.out.println("|     Malformed peer:" + peer
                            + " expected form: <host>:<port> or <logicalName>:<host>:<port>");
                } else {
                    String peerLogicalName = (hostPort.length == 2) ? hostPort[0] + ":" + hostPort[1]
                            : hostPort[0];
                    String peerHostname = (hostPort.length == 2) ? hostPort[0] : hostPort[1];
                    String peerPort = (hostPort.length == 2) ? hostPort[1] : hostPort[2];

                    RingMember peerRingMember = new RingMember(peerLogicalName);
                    RingHost peerRingHost = new RingHost("unknown", "unknown", peerHostname,
                            Integer.parseInt(peerPort));

                    System.out.println("|     Adding ringMember:" + peerRingMember + " on host:" + peerRingHost
                            + " to cluster: " + clusterName);
                    amzaService.getRingWriter().register(peerRingMember, peerRingHost, writerId, false);
                }
            }
            systemRingSize.set(1 + peers.length);
            System.out.println("-----------------------------------------------------------------------");
        } else {
            AmzaDiscovery amzaDiscovery = new AmzaDiscovery(amzaService.getRingReader(),
                    amzaService.getRingWriter(), clusterName, multicastGroup, multicastPort, systemRingSize);
            amzaDiscovery.start();
            System.out.println("-----------------------------------------------------------------------");
            System.out.println("|      Amza Service Discovery Online: Cluster Name:" + clusterName);
            System.out.println("-----------------------------------------------------------------------");
        }
    } else {
        System.out.println("-----------------------------------------------------------------------");
        System.out.println("|     Amza Service is in manual Discovery mode.  No cluster name was specified");
        System.out.println("-----------------------------------------------------------------------");
    }
}

From source file:gov.bnl.channelfinder.ChannelsResource.java

/**
 * POST method for merging properties and tags of the Channel identified by the
 * payload into an existing channel.//from w  w  w .  jav  a  2  s.  c o m
 *
 * @param chan name of channel to add
 * @param data new XmlChannel data (properties/tags) to be merged into channel <tt>chan</tt>
 * @return HTTP response
 */
@POST
@Path("{chName: " + chNameRegex + "}")
@Consumes({ "application/xml", "application/json" })
public Response update(@PathParam("chName") String chan, XmlChannel data) {
    long start = System.currentTimeMillis();
    UserManager um = UserManager.getInstance();
    um.setUser(securityContext.getUserPrincipal(), securityContext.isUserInRole("Administrator"));
    Client client = getNewClient();
    if (data.getName() == null || data.getName().isEmpty()) {
        return handleException(um.getUserName(), "PUT", Response.Status.BAD_REQUEST, "Specified channel name '"
                + chan + "' and payload channel name '" + data.getName() + "' do not match");
    }
    if (!validateChannelName(chan, data)) {
        return renameChannel(um, client, chan, data);
    }
    ObjectMapper mapper = new ObjectMapper();
    try {
        start = System.currentTimeMillis();
        data = validateChannel(data, client);
        audit.info(um.getUserName() + "|" + uriInfo.getPath() + "|POST|validation : "
                + (System.currentTimeMillis() - start));
        start = System.currentTimeMillis();
        GetResponse response = client.prepareGet("channelfinder", "channel", chan).execute().actionGet();
        if (response.isExists()) {
            XmlChannel channel = mapper.readValue(response.getSourceAsBytes(), XmlChannel.class);
            channel.setName(data.getName());
            channel.setOwner(data.getOwner());
            Collection<String> propNames = ChannelUtil.getPropertyNames(data);
            data.getProperties().addAll(channel.getProperties().stream().filter(p -> {
                return !propNames.contains(p.getName());
            }).collect(Collectors.toList()));
            channel.setProperties(data.getProperties());
            Collection<String> tagNames = ChannelUtil.getTagNames(data);
            data.getTags().addAll(channel.getTags().stream().filter(t -> {
                return !tagNames.contains(t.getName());
            }).collect(Collectors.toList()));
            channel.setTags(data.getTags());
            UpdateRequest updateRequest = new UpdateRequest("channelfinder", "channel", chan)
                    .doc(mapper.writeValueAsBytes(channel)).refresh(true);
            audit.info(um.getUserName() + "|" + uriInfo.getPath() + "|POST|prepare : "
                    + (System.currentTimeMillis() - start));
            start = System.currentTimeMillis();
            UpdateResponse result = client.update(updateRequest).actionGet();
            Response r = Response.noContent().build();
            audit.info(um.getUserName() + "|" + uriInfo.getPath() + "|POST|OK|" + r.getStatus() + "|data="
                    + XmlChannel.toLog(data));
            return r;
        } else {
            return handleException(um.getUserName(), "POST", Response.Status.NOT_FOUND,
                    "Specified channel '" + chan + "' does not exist");
        }
    } catch (IllegalArgumentException e) {
        return handleException(um.getUserName(), "POST", Response.Status.BAD_REQUEST, e);
    } catch (Exception e) {
        return handleException(um.getUserName(), "POST", Response.Status.INTERNAL_SERVER_ERROR, e);
    } finally {
        client.close();
    }
}

From source file:com.jivesoftware.os.upena.deployable.UpenaMain.java

public AmzaService startAmza(String workingDir, AmzaStats amzaStats, BAInterner baInterner, int writerId,
        RingHost ringHost, RingMember ringMember, OAuthSigner authSigner,
        TenantAwareHttpClient<String> systemTakeClient, TenantAwareHttpClient<String> stripedTakeClient,
        TenantAwareHttpClient<String> ringClient, AtomicReference<Callable<RingTopology>> topologyProvider,
        String clusterDiscoveryName, String multicastGroup, int multicastPort) throws Exception {

    //Deployable deployable = new Deployable(new String[0]);

    SnowflakeIdPacker idPacker = new SnowflakeIdPacker();
    JiveEpochTimestampProvider timestampProvider = new JiveEpochTimestampProvider();

    SickThreads sickThreads = new SickThreads();
    SickPartitions sickPartitions = new SickPartitions();
    //deployable.addHealthCheck(new SickThreadsHealthCheck(deployable.config(AmzaSickThreadsHealthConfig.class), sickThreads));
    //deployable.addHealthCheck(new SickPartitionsHealthCheck(sickPartitions));

    TimestampedOrderIdProvider orderIdProvider = new OrderIdProviderImpl(new ConstantWriterIdProvider(writerId),
            idPacker, timestampProvider);

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.INDENT_OUTPUT, false);

    PartitionPropertyMarshaller partitionPropertyMarshaller = new PartitionPropertyMarshaller() {

        @Override//w w  w  .  j  a  v a  2 s  . co m
        public PartitionProperties fromBytes(byte[] bytes) {
            try {
                return mapper.readValue(bytes, PartitionProperties.class);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public byte[] toBytes(PartitionProperties partitionProperties) {
            try {
                return mapper.writeValueAsBytes(partitionProperties);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }
    };

    BinaryPrimaryRowMarshaller primaryRowMarshaller = new BinaryPrimaryRowMarshaller(); // hehe you cant change this :)
    BinaryHighwaterRowMarshaller highwaterRowMarshaller = new BinaryHighwaterRowMarshaller(baInterner);

    RowsTakerFactory systemRowsTakerFactory = () -> new HttpRowsTaker(amzaStats, systemTakeClient, mapper,
            baInterner);
    RowsTakerFactory rowsTakerFactory = () -> new HttpRowsTaker(amzaStats, stripedTakeClient, mapper,
            baInterner);

    AvailableRowsTaker availableRowsTaker = new HttpAvailableRowsTaker(ringClient, baInterner, mapper);
    AquariumStats aquariumStats = new AquariumStats();

    QuorumTimeouts quorumTimeoutsConfig = bindDefault(QuorumTimeouts.class);
    HealthTimer quorumLatency = HealthFactory.getHealthTimer(QuorumLatency.class, TimerHealthChecker.FACTORY);

    TriggerTimeoutHealthCheck quorumTimeoutHealthCheck = new TriggerTimeoutHealthCheck(
            () -> amzaStats.getGrandTotal().quorumTimeouts.longValue(), quorumTimeoutsConfig);
    //deployable.addHealthCheck(quorumTimeoutHealthCheck);

    //LABPointerIndexConfig amzaLabConfig = deployable.config(LABPointerIndexConfig.class);
    LABPointerIndexConfig amzaLabConfig = bindDefault(UpenaLABPointerIndexConfig.class);

    AmzaServiceConfig amzaServiceConfig = new AmzaServiceConfig();
    amzaServiceConfig.systemRingSize = 1;
    amzaServiceConfig.workingDirectories = new String[] { new File(workingDir, "state").getAbsolutePath() };
    amzaServiceConfig.aquariumLivelinessFeedEveryMillis = 5_000;
    amzaServiceConfig.checkIfCompactionIsNeededIntervalInMillis = 30_000;
    amzaServiceConfig.deltaMergeThreads = 2;
    amzaServiceConfig.maxUpdatesBeforeDeltaStripeCompaction = 10_000;
    amzaServiceConfig.numberOfTakerThreads = 2;
    amzaServiceConfig.hardFsync = true;
    amzaServiceConfig.takeSlowThresholdInMillis = 1_000;
    amzaServiceConfig.rackDistributionEnabled = false;

    Set<RingMember> blacklistRingMembers = Sets.newHashSet();
    AmzaService amzaService = new AmzaServiceInitializer().initialize(amzaServiceConfig, baInterner,
            aquariumStats, amzaStats, quorumLatency, () -> {
                Callable<RingTopology> ringTopologyCallable = topologyProvider.get();
                if (ringTopologyCallable != null) {
                    try {
                        return ringTopologyCallable.call().entries.size();
                    } catch (Exception x) {
                        LOG.error("issue determining system ring size", x);
                    }
                }
                return -1;
            }, sickThreads, sickPartitions, primaryRowMarshaller, highwaterRowMarshaller, ringMember, ringHost,
            blacklistRingMembers, orderIdProvider, idPacker, partitionPropertyMarshaller,
            (workingIndexDirectories, indexProviderRegistry, ephemeralRowIOProvider, persistentRowIOProvider,
                    partitionStripeFunction) -> {

                indexProviderRegistry
                        .register(
                                new BerkeleyDBWALIndexProvider(BerkeleyDBWALIndexProvider.INDEX_CLASS_NAME,
                                        partitionStripeFunction, workingIndexDirectories),
                                persistentRowIOProvider);

                indexProviderRegistry.register(new LABPointerIndexWALIndexProvider(amzaLabConfig,
                        LABPointerIndexWALIndexProvider.INDEX_CLASS_NAME, partitionStripeFunction,
                        workingIndexDirectories), persistentRowIOProvider);
            }, availableRowsTaker, systemRowsTakerFactory, rowsTakerFactory,
            Optional.<TakeFailureListener>absent(), rowsChanged -> {
            });

    topologyProvider.set(() -> amzaService.getRingReader().getRing(AmzaRingReader.SYSTEM_RING, -1));

    amzaService.start(ringMember, ringHost);
    LOG.info("-----------------------------------------------------------------------");
    LOG.info("|      Amza Service Started");
    LOG.info("-----------------------------------------------------------------------");

    if (clusterDiscoveryName != null) {
        AmzaDiscovery amzaDiscovery = new AmzaDiscovery(amzaService.getRingReader(),
                amzaService.getRingWriter(), clusterDiscoveryName, multicastGroup, multicastPort,
                new AtomicInteger(amzaService.getRingReader().getRingSize(AmzaRingReader.SYSTEM_RING, -1)) // Grrr
        );
        amzaDiscovery.start();
        LOG.info("-----------------------------------------------------------------------");
        LOG.info("|      Amza Service Discovery Online");
        LOG.info("-----------------------------------------------------------------------");
    } else {
        LOG.info("-----------------------------------------------------------------------");
        LOG.info("|     Amza Service is in manual Discovery mode.  No cluster discovery name was specified");
        LOG.info("-----------------------------------------------------------------------");
    }

    return amzaService;
}

From source file:com.joyent.manta.client.MantaClient.java

/**
 * Submits a new job to be executed. This call is not idempotent, so calling
 * it twice will create two jobs./* w ww. ja  va  2  s.  c  o m*/
 *
 * @param job Object populated with details about the request job
 * @return id of the newly created job
 * @throws IOException thrown when there are problems creating the job over the network
 */
public UUID createJob(final MantaJob job) throws IOException {
    Validate.notNull(job, "Manta job must not be null");

    String path = formatPath(String.format("%s/jobs", config.getMantaHomeDirectory()));
    ObjectMapper mapper = MantaObjectMapper.INSTANCE;
    byte[] json = mapper.writeValueAsBytes(job);

    HttpEntity entity = new ExposedByteArrayEntity(json, ContentType.APPLICATION_JSON);

    HttpPost post = httpHelper.getRequestFactory().post(path);
    post.setEntity(entity);

    Function<CloseableHttpResponse, UUID> jobIdFunction = response -> {
        final String location = response.getFirstHeader(HttpHeaders.LOCATION).getValue();
        String id = MantaUtils.lastItemInPath(location);
        return UUID.fromString(id);
    };

    /* This endpoint has a propensity for failing to respond, so we retry on
     * failure. */

    final int retries;

    if (config.getRetries() == null) {
        retries = DefaultsConfigContext.DEFAULT_HTTP_RETRIES;
    } else {
        retries = config.getRetries();
    }

    IOException lastException = new IOException("Never thrown. Report me as a bug.");

    // if retries are set to zero, we always execute at least once
    for (int count = 0; count < retries || count == 0; count++) {
        try {
            return httpHelper.executeAndCloseRequest(post, jobIdFunction, "POST   {} response [{}] {} ", path);
        } catch (NoHttpResponseException | MantaNoHttpResponseException e) {
            lastException = e;
            LOG.warn("Error posting createJob. Retrying.", e);
        }
    }

    throw lastException;
}

From source file:org.bimserver.servlets.ServiceRunnerServlet.java

private void processServiceList(HttpServletRequest request, HttpServletResponse response) {
    BimDatabase database = getBimServer().getDatabase();
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode result = mapper.createObjectNode();
    ArrayNode array = mapper.createArrayNode();
    ArrayNode capabilities = mapper.createArrayNode();
    capabilities.add("WEBSOCKET");
    result.set("capabilities", capabilities);
    result.set("services", array);
    try (DatabaseSession session = database.createSession()) {
        for (PluginDescriptor pluginDescriptor : session.getAllOfType(
                StorePackage.eINSTANCE.getPluginDescriptor(), PluginDescriptor.class, OldQuery.getDefault())) {
            if (pluginDescriptor.getPluginInterfaceClassName().equals(ServicePlugin.class.getName())) {
                ServicePlugin servicePlugin = getBimServer().getPluginManager()
                        .getServicePlugin(pluginDescriptor.getPluginClassName(), true);
                if (servicePlugin instanceof BimBotsServiceInterface) {
                    try {
                        BimBotsServiceInterface bimBotsServiceInterface = (BimBotsServiceInterface) servicePlugin;

                        ObjectNode descriptorJson = mapper.createObjectNode();
                        descriptorJson.put("id", pluginDescriptor.getOid());
                        descriptorJson.put("name", pluginDescriptor.getName());
                        descriptorJson.put("description", pluginDescriptor.getDescription());
                        descriptorJson.put("provider",
                                getBimServer().getServerSettingsCache().getServerSettings().getName());
                        descriptorJson.put("providerIcon",
                                getBimServer().getServerSettingsCache().getServerSettings().getIcon());

                        ArrayNode inputs = mapper.createArrayNode();
                        ArrayNode outputs = mapper.createArrayNode();

                        for (String schemaName : bimBotsServiceInterface.getAvailableInputs()) {
                            inputs.add(schemaName);
                        }// w w w .j  a  v a  2  s.  c  o m
                        for (String schemaName : bimBotsServiceInterface.getAvailableOutputs()) {
                            outputs.add(schemaName);
                        }

                        descriptorJson.set("inputs", inputs);
                        descriptorJson.set("outputs", outputs);

                        ObjectNode oauth = mapper.createObjectNode();
                        String siteAddress = getBimServer().getServerSettingsCache().getServerSettings()
                                .getSiteAddress();
                        oauth.put("authorizationUrl", siteAddress + "/oauth/authorize");
                        oauth.put("registerUrl", siteAddress + "/oauth/register");
                        oauth.put("tokenUrl", siteAddress + "/oauth/access");

                        descriptorJson.set("oauth", oauth);
                        descriptorJson.put("resourceUrl", siteAddress + "/services");
                        array.add(descriptorJson);
                    } catch (Exception e) {
                        LOGGER.error("", e);
                    }
                }
            }
        }
        response.setContentType("application/json");
        response.getOutputStream().write(mapper.writeValueAsBytes(result));
    } catch (BimserverDatabaseException e) {
        LOGGER.error("", e);
    } catch (JsonProcessingException e) {
        LOGGER.error("", e);
    } catch (IOException e) {
        LOGGER.error("", e);
    }
}

From source file:com.jivesoftware.os.amza.sync.deployable.AmzaSyncMain.java

public void run(String[] args) throws Exception {
    ServiceStartupHealthCheck serviceStartupHealthCheck = new ServiceStartupHealthCheck();
    try {/* w  ww  .j a v  a2s .c  o m*/
        final Deployable deployable = new Deployable(args);
        InstanceConfig instanceConfig = deployable.config(InstanceConfig.class);
        HealthFactory.initialize(deployable::config, new DeployableHealthCheckRegistry(deployable));
        deployable.addManageInjectables(HasUI.class, new HasUI(Arrays.asList(new UI("Sync", "main", "/ui"))));
        deployable.addHealthCheck(new GCPauseHealthChecker(
                deployable.config(GCPauseHealthChecker.GCPauseHealthCheckerConfig.class)));
        deployable.addHealthCheck(new GCLoadHealthChecker(
                deployable.config(GCLoadHealthChecker.GCLoadHealthCheckerConfig.class)));
        deployable.addHealthCheck(new SystemCpuHealthChecker(
                deployable.config(SystemCpuHealthChecker.SystemCpuHealthCheckerConfig.class)));
        deployable.addHealthCheck(new LoadAverageHealthChecker(
                deployable.config(LoadAverageHealthChecker.LoadAverageHealthCheckerConfig.class)));
        deployable.addHealthCheck(new FileDescriptorCountHealthChecker(deployable
                .config(FileDescriptorCountHealthChecker.FileDescriptorCountHealthCheckerConfig.class)));
        deployable.addHealthCheck(serviceStartupHealthCheck);
        deployable.addErrorHealthChecks(deployable.config(ErrorHealthCheckConfig.class));
        deployable.addManageInjectables(FullyOnlineVersion.class, (FullyOnlineVersion) () -> {
            if (serviceStartupHealthCheck.startupHasSucceeded()) {
                return instanceConfig.getVersion();
            } else {
                return null;
            }
        });
        deployable.buildManageServer().start();

        HttpDeliveryClientHealthProvider clientHealthProvider = new HttpDeliveryClientHealthProvider(
                instanceConfig.getInstanceKey(), HttpRequestHelperUtils.buildRequestHelper(false, false, null,
                        instanceConfig.getRoutesHost(), instanceConfig.getRoutesPort()),
                instanceConfig.getConnectionsHealth(), 5_000, 100);

        TenantRoutingProvider tenantRoutingProvider = deployable.getTenantRoutingProvider();

        TenantsServiceConnectionDescriptorProvider syncDescriptorProvider = tenantRoutingProvider
                .getConnections(instanceConfig.getServiceName(), "main", 10_000); // TODO config

        TenantRoutingHttpClientInitializer<String> tenantRoutingHttpClientInitializer = deployable
                .getTenantRoutingHttpClientInitializer();

        TenantAwareHttpClient<String> amzaClient = tenantRoutingHttpClientInitializer
                .builder(deployable.getTenantRoutingProvider().getConnections("amza", "main", 10_000), // TODO config
                        clientHealthProvider)
                .deadAfterNErrors(10).checkDeadEveryNMillis(10_000).socketTimeoutInMillis(60_000).build(); // TODO expose to conf

        deployable.addHealthCheck(new TenantAwareHttpClientHealthCheck("amzaClient", amzaClient));

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

        AmzaSyncConfig syncConfig = deployable.config(AmzaSyncConfig.class);

        TailAtScaleStrategy tailAtScaleStrategy = new TailAtScaleStrategy(
                deployable.newBoundedExecutor(1024, "tas"), 100, // TODO config
                95, // TODO config
                1000 // TODO config
        );

        AmzaInterner amzaInterner = new AmzaInterner();
        AmzaClientProvider<HttpClient, HttpClientException> amzaClientProvider = new AmzaClientProvider<>(
                new HttpPartitionClientFactory(),
                new HttpPartitionHostsProvider(amzaClient, tailAtScaleStrategy, mapper),
                new RingHostHttpClientProvider(amzaClient),
                deployable.newBoundedExecutor(syncConfig.getAmzaCallerThreadPoolSize(), "amza-client"),
                syncConfig.getAmzaAwaitLeaderElectionForNMillis(), -1, -1);

        TimestampedOrderIdProvider orderIdProvider = new OrderIdProviderImpl(
                new ConstantWriterIdProvider(instanceConfig.getInstanceName()), new SnowflakeIdPacker(),
                new JiveEpochTimestampProvider());
        AmzaClientAquariumProvider amzaClientAquariumProvider = new AmzaClientAquariumProvider(
                new AquariumStats(), instanceConfig.getServiceName(), amzaClientProvider, orderIdProvider,
                new Member(instanceConfig.getInstanceKey().getBytes(StandardCharsets.UTF_8)), count -> {
                    ConnectionDescriptors descriptors = syncDescriptorProvider.getConnections("");
                    int ringSize = descriptors.getConnectionDescriptors().size();
                    return count > ringSize / 2;
                }, () -> {
                    Set<Member> members = Sets.newHashSet();
                    ConnectionDescriptors descriptors = syncDescriptorProvider.getConnections("");
                    for (ConnectionDescriptor connectionDescriptor : descriptors.getConnectionDescriptors()) {
                        members.add(new Member(connectionDescriptor.getInstanceDescriptor().instanceKey
                                .getBytes(StandardCharsets.UTF_8)));
                    }
                    return members;
                }, 128, //TODO config
                128, //TODO config
                5_000L, //TODO config
                100L, //TODO config
                60_000L, //TODO config
                10_000L, //TODO config
                Executors.newSingleThreadExecutor(), 100L, //TODO config
                1_000L, //TODO config
                10_000L, //TODO config
                syncConfig.getAquariumUseSolutionLog());

        ObjectMapper miruSyncMapper = new ObjectMapper();
        miruSyncMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        miruSyncMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        AmzaMarshaller<String> stringMarshaller = new AmzaMarshaller<String>() {
            @Override
            public String fromBytes(byte[] bytes) throws Exception {
                return new String(bytes, StandardCharsets.UTF_8);
            }

            @Override
            public byte[] toBytes(String value) throws Exception {
                return value == null ? null : value.getBytes(StandardCharsets.UTF_8);
            }
        };

        AmzaMarshaller<AmzaSyncSenderConfig> amzaSyncSenderConfigMarshaller = new AmzaMarshaller<AmzaSyncSenderConfig>() {
            @Override
            public AmzaSyncSenderConfig fromBytes(byte[] bytes) throws Exception {
                return mapper.readValue(bytes, AmzaSyncSenderConfig.class);
            }

            @Override
            public byte[] toBytes(AmzaSyncSenderConfig value) throws Exception {
                return mapper.writeValueAsBytes(value);
            }
        };

        AmzaSyncSenderMap senderConfigStorage = new AmzaSyncSenderMap(amzaClientProvider,
                "amza-sync-sender-config",
                new PartitionProperties(Durability.fsync_async, 0, 0, 0, 0, 0, 0, 0, 0, false,
                        Consistency.leader_quorum, true, true, false, RowType.snappy_primary, "lab", -1, null,
                        -1, -1),
                stringMarshaller, amzaSyncSenderConfigMarshaller);

        AmzaMarshaller<AmzaSyncPartitionTuple> tupleMarshaller = new AmzaMarshaller<AmzaSyncPartitionTuple>() {
            @Override
            public AmzaSyncPartitionTuple fromBytes(byte[] bytes) throws Exception {
                return AmzaSyncPartitionTuple.fromBytes(bytes, 0, amzaInterner);
            }

            @Override
            public byte[] toBytes(AmzaSyncPartitionTuple value) throws Exception {
                return AmzaSyncPartitionTuple.toBytes(value);
            }
        };

        AmzaMarshaller<AmzaSyncPartitionConfig> partitionConfigMarshaller = new AmzaMarshaller<AmzaSyncPartitionConfig>() {
            @Override
            public AmzaSyncPartitionConfig fromBytes(byte[] bytes) throws Exception {
                return mapper.readValue(bytes, AmzaSyncPartitionConfig.class);
            }

            @Override
            public byte[] toBytes(AmzaSyncPartitionConfig value) throws Exception {
                return mapper.writeValueAsBytes(value);
            }
        };

        AmzaSyncPartitionConfigStorage syncPartitionConfigStorage = new AmzaSyncPartitionConfigStorage(
                amzaClientProvider, "amza-sync-partitions-config-",
                new PartitionProperties(Durability.fsync_async, 0, 0, 0, 0, 0, 0, 0, 0, false,
                        Consistency.leader_quorum, true, true, false, RowType.snappy_primary, "lab", -1, null,
                        -1, -1),
                tupleMarshaller, partitionConfigMarshaller);

        AmzaSyncStats stats = new AmzaSyncStats();

        AmzaSyncReceiver syncReceiver = new AmzaSyncReceiver(amzaClientProvider,
                syncConfig.getSyncReceiverUseSolutionLog());

        AmzaSyncSenders syncSenders = null;
        if (syncConfig.getSyncSenderEnabled()) {
            ScheduledExecutorService executorService = Executors
                    .newScheduledThreadPool(syncConfig.getSyncSendersThreadCount());
            syncSenders = new AmzaSyncSenders(stats, syncConfig, syncReceiver, executorService,
                    amzaClientProvider, amzaClientAquariumProvider, amzaInterner, mapper, orderIdProvider,
                    senderConfigStorage, syncPartitionConfigStorage, 30_000); // TODO config
        }

        amzaClientAquariumProvider.start();
        if (syncSenders != null) {
            syncSenders.start();
        }

        SoyRendererConfig rendererConfig = deployable.config(SoyRendererConfig.class);

        File staticResourceDir = new File(System.getProperty("user.dir"));
        System.out.println("Static resources rooted at " + staticResourceDir.getAbsolutePath());
        Resource sourceTree = new Resource(staticResourceDir)
                .addResourcePath(rendererConfig.getPathToStaticResources()).setDirectoryListingAllowed(false)
                .setContext("/ui/static");
        deployable.addResource(sourceTree);

        SoyRenderer renderer = new SoyRendererInitializer().initialize(rendererConfig);

        AmzaSyncUIService amzaSyncUIService = new AmzaSyncUIServiceInitializer().initialize(renderer,
                syncSenders, stats, syncConfig.getSyncSenderEnabled(), syncConfig.getSyncReceiverEnabled(),
                mapper);

        deployable.addEndpoints(LoadBalancerHealthCheckEndpoints.class);
        deployable.addNoAuth("/health/check");
        if (instanceConfig.getMainServiceAuthEnabled()) {
            if (syncConfig.getSyncReceiverEnabled()) {
                AmzaSyncOAuthValidatorConfig oAuthValidatorConfig = deployable
                        .config(AmzaSyncOAuthValidatorConfig.class);
                AuthValidator<OAuth1Signature, OAuth1Request> syncOAuthValidator = new AmzaSyncOAuthValidatorInitializer()
                        .initialize(oAuthValidatorConfig);
                deployable.addCustomOAuth(syncOAuthValidator, "/api/*");
            }
            deployable.addRouteOAuth("/amza/*", "/api/*");
            deployable.addSessionAuth("/ui/*", "/amza/*", "/api/*");
        } else {
            deployable.addNoAuth("/amza/*", "/api/*");
            deployable.addSessionAuth("/ui/*");
        }

        deployable.addEndpoints(AmzaSyncEndpoints.class);
        deployable.addInjectables(AmzaInterner.class, amzaInterner);
        if (syncSenders != null) {
            deployable.addInjectables(AmzaSyncSenders.class, syncSenders);
        }

        deployable.addEndpoints(AmzaSyncUIEndpoints.class);
        deployable.addInjectables(AmzaSyncUIService.class, amzaSyncUIService);

        if (syncConfig.getSyncReceiverEnabled()) {
            deployable.addEndpoints(AmzaSyncApiEndpoints.class);
            deployable.addInjectables(AmzaSyncReceiver.class, syncReceiver);
        }
        deployable.addInjectables(ObjectMapper.class, mapper);
        deployable.addInjectables(AmzaSyncSenderMap.class, senderConfigStorage);
        deployable.addInjectables(AmzaSyncPartitionConfigStorage.class, syncPartitionConfigStorage);

        deployable.buildServer().start();
        clientHealthProvider.start();
        serviceStartupHealthCheck.success();

    } catch (Throwable t) {
        serviceStartupHealthCheck.info("Encountered the following failure during startup.", t);
    }
}

From source file:org.geoserver.notification.geonode.GeoNodeJsonEncoder.java

@Override
public byte[] encode(Notification notification) throws Exception {
    byte[] ret = null;

    ObjectMapper mapper = new ObjectMapper();
    mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"));
    mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    mapper.setSerializationInclusion(Include.NON_NULL);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

    KombuMessage message = new KombuMessage();

    message.setId(new UID().toString());
    message.setType(notification.getType() != null ? notification.getType().name() : null);
    message.setAction(notification.getAction() != null ? notification.getAction().name() : null);
    message.setTimestamp(new Date());
    message.setUser(notification.getUser());
    message.setOriginator(InetAddress.getLocalHost().getHostAddress());
    message.setProperties(notification.getProperties());
    if (notification.getObject() instanceof NamespaceInfo) {
        NamespaceInfo obj = (NamespaceInfo) notification.getObject();
        KombuNamespaceInfo source = new KombuNamespaceInfo();
        source.setId(obj.getId());/*from  w  w w. ja  v a2 s.co m*/
        source.setType("NamespaceInfo");
        source.setName(obj.getName());
        source.setNamespaceURI(obj.getURI());
        message.setSource(source);
    }
    if (notification.getObject() instanceof WorkspaceInfo) {
        WorkspaceInfo obj = (WorkspaceInfo) notification.getObject();
        KombuWorkspaceInfo source = new KombuWorkspaceInfo();
        source.setId(obj.getId());
        source.setType("WorkspaceInfo");
        source.setName(obj.getName());
        source.setNamespaceURI("");
        message.setSource(source);
    }
    if (notification.getObject() instanceof LayerInfo) {
        LayerInfo obj = (LayerInfo) notification.getObject();
        KombuLayerInfo source = new KombuLayerInfo();
        source.setId(obj.getId());
        source.setType("LayerInfo");
        source.setName(obj.getName());
        source.setResourceType(obj.getType() != null ? obj.getType().name() : "");
        BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer("name");
        Collection<String> styleNames = CollectionUtils.collect(obj.getStyles(), transformer);
        source.setStyles(StringUtils.join(styleNames.toArray()));
        source.setDefaultStyle(obj.getDefaultStyle() != null ? obj.getDefaultStyle().getName() : "");
        ResourceInfo res = obj.getResource();
        source.setWorkspace(res.getStore() != null
                ? res.getStore().getWorkspace() != null ? res.getStore().getWorkspace().getName() : ""
                : "");
        if (res.getNativeBoundingBox() != null) {
            source.setBounds(new Bounds(res.getNativeBoundingBox()));
        }
        if (res.getLatLonBoundingBox() != null) {
            source.setGeographicBunds(new Bounds(res.getLatLonBoundingBox()));
        }
        message.setSource(source);
    }
    if (notification.getObject() instanceof LayerGroupInfo) {
        LayerGroupInfo obj = (LayerGroupInfo) notification.getObject();
        KombuLayerGroupInfo source = new KombuLayerGroupInfo();
        source.setId(obj.getId());
        source.setType("LayerGroupInfo");
        source.setName(obj.getName());
        source.setWorkspace(obj.getWorkspace() != null ? obj.getWorkspace().getName() : "");
        source.setMode(obj.getType().name());
        String rootStyle = obj.getRootLayerStyle() != null ? obj.getRootLayerStyle().getName() : "";
        source.setRootLayerStyle(rootStyle);
        source.setRootLayer(obj.getRootLayer() != null ? obj.getRootLayer().getPath() : "");
        for (PublishedInfo pl : obj.getLayers()) {
            KombuLayerSimpleInfo kl = new KombuLayerSimpleInfo();
            if (pl instanceof LayerInfo) {
                LayerInfo li = (LayerInfo) pl;
                kl.setName(li.getName());
                String lstyle = li.getDefaultStyle() != null ? li.getDefaultStyle().getName() : "";
                if (!lstyle.equals(rootStyle)) {
                    kl.setStyle(lstyle);
                }
                source.addLayer(kl);
            }
        }
        message.setSource(source);
    }
    if (notification.getObject() instanceof ResourceInfo) {
        ResourceInfo obj = (ResourceInfo) notification.getObject();
        KombuResourceInfo source = null;
        if (notification.getObject() instanceof FeatureTypeInfo) {
            source = new KombuFeatureTypeInfo();
            source.setType("FeatureTypeInfo");
        }
        if (notification.getObject() instanceof CoverageInfo) {
            source = new KombuCoverageInfo();
            source.setType("CoverageInfo");
        }
        if (notification.getObject() instanceof WMSLayerInfo) {
            source = new KombuWMSLayerInfo();
            source.setType("WMSLayerInfo");
        }
        if (source != null) {
            source.setId(obj.getId());
            source.setName(obj.getName());
            source.setWorkspace(obj.getStore() != null
                    ? obj.getStore().getWorkspace() != null ? obj.getStore().getWorkspace().getName() : ""
                    : "");
            source.setNativeName(obj.getNativeName());
            source.setStore(obj.getStore() != null ? obj.getStore().getName() : "");
            if (obj.getNativeBoundingBox() != null) {
                source.setGeographicBunds(new Bounds(obj.getNativeBoundingBox()));
            }
            if (obj.boundingBox() != null) {
                source.setBounds(new Bounds(obj.boundingBox()));
            }
        }
        message.setSource(source);
    }
    if (notification.getObject() instanceof StoreInfo) {
        StoreInfo obj = (StoreInfo) notification.getObject();
        KombuStoreInfo source = new KombuStoreInfo();
        source.setId(obj.getId());
        source.setType("StoreInfo");
        source.setName(obj.getName());
        source.setWorkspace(obj.getWorkspace() != null ? obj.getWorkspace().getName() : "");
        message.setSource(source);
    }
    ret = mapper.writeValueAsBytes(message);
    return ret;

}