<commit_before>/*
 * Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, without warranties or
 * conditions of any kind, EITHER EXPRESS OR IMPLIED.  See the License for the
 * specific language governing permissions and limitations under the License.
 */

package com.vmware.workshop.labs.answer;

import java.util.function.Consumer;

import com.vmware.workshop.labs.BookService;
import com.vmware.workshop.labs.Top10BookRankingService;
import com.vmware.xenon.common.Operation;
import com.vmware.xenon.common.Service;
import com.vmware.xenon.common.ServiceHost;
import com.vmware.xenon.common.UriUtils;
import com.vmware.xenon.services.common.RootNamespaceService;

/**
 * Hands on lab answer implementation.
 */
public class BookstoreDemoAnswer {

    public static void main(String[] args) throws Throwable {

        // create adhoc ServiceHost.
        // In real application, you should create own ServiceHost implementation.
        ServiceHost host = ServiceHost.create();
        host.start();
        host.startDefaultCoreServicesSynchronously();
        host.startService(new RootNamespaceService());
        host.startFactory(new BookService());
        host.startService(new Top10BookRankingService());


        // Create a consumer of subscription callbacks

        // subscription callback
        Consumer<Operation> target = (notifyOp) -> {
            System.out.println("notification: " + notifyOp);
            if (notifyOp.getAction() == Service.Action.POST) {
                BookService.BookState state = notifyOp.getBody(BookService.BookState.class);
                System.out.println("sold=" + state.sold);
            }
            notifyOp.complete();
        };


        // start subscription when factory became available
        // For reliable subscription, use "startReliableSubscriptionService()" instead of "startSubscriptionService()"
        host.registerForServiceAvailability((o, e) -> {

            // Register subscription to the factory of BookService
            Operation createSub = Operation.createPost(
                    UriUtils.buildUri(host, BookService.FACTORY_LINK)).setReferer(host.getUri());
            host.startSubscriptionService(createSub, target);<commit_msg>Use log instead of system out<commit_after>/*
 * Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, without warranties or
 * conditions of any kind, EITHER EXPRESS OR IMPLIED.  See the License for the
 * specific language governing permissions and limitations under the License.
 */

package com.vmware.workshop.labs.answer;

import java.util.function.Consumer;
import java.util.logging.Level;

import com.vmware.workshop.labs.BookService;
import com.vmware.workshop.labs.Top10BookRankingService;
import com.vmware.xenon.common.Operation;
import com.vmware.xenon.common.Service;
import com.vmware.xenon.common.ServiceHost;
import com.vmware.xenon.common.UriUtils;
import com.vmware.xenon.services.common.RootNamespaceService;

/**
 * Hands on lab answer implementation.
 */
public class BookstoreDemoAnswer {

    public static void main(String[] args) throws Throwable {

        // create adhoc ServiceHost.
        // In real application, you should create own ServiceHost implementation.
        ServiceHost host = ServiceHost.create();
        host.start();
        host.startDefaultCoreServicesSynchronously();
        host.startService(new RootNamespaceService());
        host.startFactory(new BookService());
        host.startService(new Top10BookRankingService());


        // Create a consumer of subscription callbacks

        // subscription callback
        Consumer<Operation> target = (notifyOp) -> {
            host.log(Level.INFO, "notification: %s", notifyOp);
            if (notifyOp.getAction() == Service.Action.POST) {
                BookService.BookState state = notifyOp.getBody(BookService.BookState.class);
                host.log(Level.INFO, "title=%s, sold=%d", state.title, state.sold);
            }
            notifyOp.complete();
        };


        // start subscription when factory became available
        // For reliable subscription, use "startReliableSubscriptionService()" instead of "startSubscriptionService()"
        host.registerForServiceAvailability((o, e) -> {

            // Register subscription to the factory of BookService
            Operation createSub = Operation.createPost(
                    UriUtils.buildUri(host, BookService.FACTORY_LINK)).setReferer(host.getUri());
            host.startSubscriptionService(createSub, target);
{'fraction_non_alphanumeric': 0.09456130377108206, 'fraction_numerical': 0.006064051544438127, 'mean_word_length': 3.2156549520766773, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7396608', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1343, 'n_words': 401}