/**
   * Establishes a connection to a registry.
   *
   * @param queryUrl the URL of the query registry
   * @param publishUrl the URL of the publish registry
   */
  public void makeConnection(String queryUrl, String publishUrl) {

    Context context = null;
    ConnectionFactory factory = null;

    /*
     * Define connection configuration properties.
     * To publish, you need both the query URL and the
     * publish URL.
     */
    Properties props = new Properties();
    props.setProperty("javax.xml.registry.queryManagerURL", queryUrl);
    props.setProperty("javax.xml.registry.lifeCycleManagerURL", publishUrl);

    try {
      // Create the connection, passing it the
      // configuration properties
      context = new InitialContext();
      factory = (ConnectionFactory) context.lookup("java:comp/env/eis/JAXR");
      factory.setProperties(props);
      connection = factory.createConnection();
      System.out.println("Created connection to registry");
    } catch (Exception e) {
      e.printStackTrace();
      if (connection != null) {
        try {
          connection.close();
        } catch (JAXRException je) {
        }
      }
    }
  }
  int querySkuDetails(Inventory inv, List<String> moreSkus) throws RemoteException, JSONException {
    ArrayList<String> skuList = new ArrayList<String>();
    skuList.addAll(inv.getAllOwnedSkus(ITEM_TYPE_INAPP));
    if (moreSkus != null) skuList.addAll(moreSkus);

    if (skuList.size() == 0) {
      return BILLING_RESPONSE_RESULT_OK;
    }

    Bundle querySkus = new Bundle();
    querySkus.putStringArrayList(GET_SKU_DETAILS_ITEM_LIST, skuList);
    Bundle skuDetails = connection.getSkuDetails(querySkus);

    if (!skuDetails.containsKey(RESPONSE_GET_SKU_DETAILS_LIST)) {
      int response = getResponseCodeFromBundle(skuDetails);
      if (response != BILLING_RESPONSE_RESULT_OK) {
        return response;
      } else {
        return HELPER_BAD_RESPONSE;
      }
    }

    ArrayList<String> responseList = skuDetails.getStringArrayList(RESPONSE_GET_SKU_DETAILS_LIST);

    for (String thisResponse : responseList) {
      SkuDetails d = new SkuDetails(thisResponse);
      inv.addSkuDetails(d);
    }
    return BILLING_RESPONSE_RESULT_OK;
  }
  public void launchPurchaseFlow(
      Activity activity,
      String sku,
      int requestCode,
      OnPurchaseFinishedListener listener,
      String extraData) {
    checkSetupDone("launchPurchaseFlow");
    asyncFlag.start("launchPurchaseFlow");
    Result result;

    try {
      Bundle buyIntentBundle = connection.getBuyIntent(sku, extraData);
      int response = getResponseCodeFromBundle(buyIntentBundle);
      if (response != BILLING_RESPONSE_RESULT_OK) {
        result = new Result(response, "Unable to buy item");
        if (listener != null) listener.onPurchaseFinished(result, null);
      } else {
        PendingIntent intent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT);
        this.requestCode = requestCode;
        purchaseListener = listener;
        activity.startIntentSenderForResult(
            intent.getIntentSender(), requestCode, new Intent(), 0, 0, 0);
      }
    } catch (IntentSender.SendIntentException e) {
      result = new Result(HELPER_SEND_INTENT_FAILED, "Failed to send intent.");
      if (listener != null) listener.onPurchaseFinished(result, null);
    } catch (RemoteException e) {
      result = new Result(HELPER_REMOTE_EXCEPTION, "Remote exception while starting purchase flow");
      if (listener != null) listener.onPurchaseFinished(result, null);
    }
  }
  int queryPurchases(Inventory inv) throws JSONException, RemoteException {
    boolean verificationFailed = false;
    String continueToken = null;

    do {
      Bundle ownedItems = connection.getPurchases(continueToken);

      int response = getResponseCodeFromBundle(ownedItems);
      if (response != BILLING_RESPONSE_RESULT_OK) {
        return response;
      }
      if (!ownedItems.containsKey(RESPONSE_INAPP_ITEM_LIST)
          || !ownedItems.containsKey(RESPONSE_INAPP_PURCHASE_DATA_LIST)
          || !ownedItems.containsKey(RESPONSE_INAPP_SIGNATURE_LIST)) {
        return HELPER_BAD_RESPONSE;
      }

      ArrayList<String> purchaseDataList =
          ownedItems.getStringArrayList(RESPONSE_INAPP_PURCHASE_DATA_LIST);
      ArrayList<String> signatureList =
          ownedItems.getStringArrayList(RESPONSE_INAPP_SIGNATURE_LIST);

      for (int i = 0; i < purchaseDataList.size(); ++i) {
        String purchaseData = purchaseDataList.get(i);
        String signature = signatureList.get(i);
        if (security.verifyPurchase(purchaseData, signature)) {
          Purchase purchase = new Purchase(purchaseData, signature);
          inv.addPurchase(purchase);
        } else {
          verificationFailed = true;
        }
      }

      continueToken = ownedItems.getString(INAPP_CONTINUATION_TOKEN);
    } while (!TextUtils.isEmpty(continueToken));

    return verificationFailed ? HELPER_VERIFICATION_FAILED : BILLING_RESPONSE_RESULT_OK;
  }
  /**
   * Creates an organization, its classification, and its services, and saves it to the registry.
   */
  public String executePublish(String username, String password, String endpoint) {

    String id = null;
    RegistryService rs = null;
    BusinessLifeCycleManager blcm = null;
    BusinessQueryManager bqm = null;

    try {
      rs = connection.getRegistryService();
      blcm = rs.getBusinessLifeCycleManager();
      bqm = rs.getBusinessQueryManager();
      System.out.println("Got registry service, query " + "manager, and life cycle manager");

      // Get authorization from the registry
      PasswordAuthentication passwdAuth =
          new PasswordAuthentication(username, password.toCharArray());

      Set creds = new HashSet();
      creds.add(passwdAuth);
      connection.setCredentials(creds);
      System.out.println("Established security credentials");

      // Get hardcoded strings from a ResourceBundle
      ResourceBundle bundle = ResourceBundle.getBundle("com.sun.cb.CoffeeRegistry");

      // Create organization name and description
      Organization org = blcm.createOrganization(bundle.getString("org.name"));
      InternationalString s = blcm.createInternationalString(bundle.getString("org.description"));
      org.setDescription(s);

      // Create primary contact, set name
      User primaryContact = blcm.createUser();
      PersonName pName = blcm.createPersonName(bundle.getString("person.name"));
      primaryContact.setPersonName(pName);

      // Set primary contact phone number
      TelephoneNumber tNum = blcm.createTelephoneNumber();
      tNum.setNumber(bundle.getString("phone.number"));
      Collection phoneNums = new ArrayList();
      phoneNums.add(tNum);
      primaryContact.setTelephoneNumbers(phoneNums);

      // Set primary contact email address
      EmailAddress emailAddress = blcm.createEmailAddress(bundle.getString("email.address"));
      Collection emailAddresses = new ArrayList();
      emailAddresses.add(emailAddress);
      primaryContact.setEmailAddresses(emailAddresses);

      // Set primary contact for organization
      org.setPrimaryContact(primaryContact);

      // Set classification scheme to NAICS
      ClassificationScheme cScheme =
          bqm.findClassificationSchemeByName(null, bundle.getString("classification.scheme"));

      // Create and add classification
      Classification classification =
          (Classification)
              blcm.createClassification(
                  cScheme,
                  bundle.getString("classification.name"),
                  bundle.getString("classification.value"));
      Collection classifications = new ArrayList();
      classifications.add(classification);
      org.addClassifications(classifications);

      // Create services and service
      Collection services = new ArrayList();
      Service service = blcm.createService(bundle.getString("service.name"));
      InternationalString is =
          blcm.createInternationalString(bundle.getString("service.description"));
      service.setDescription(is);

      // Create service bindings
      Collection serviceBindings = new ArrayList();
      ServiceBinding binding = blcm.createServiceBinding();
      is = blcm.createInternationalString(bundle.getString("service.binding"));
      binding.setDescription(is);
      binding.setValidateURI(false);
      binding.setAccessURI(endpoint);
      serviceBindings.add(binding);

      // Add service bindings to service
      service.addServiceBindings(serviceBindings);

      // Add service to services, then add services to organization
      services.add(service);
      org.addServices(services);

      // Add organization and submit to registry
      // Retrieve key if successful
      Collection orgs = new ArrayList();
      orgs.add(org);
      BulkResponse response = blcm.saveOrganizations(orgs);
      Collection exceptions = response.getExceptions();
      if (exceptions == null) {
        System.out.println("Organization saved");

        Collection keys = response.getCollection();
        Iterator keyIter = keys.iterator();
        if (keyIter.hasNext()) {
          javax.xml.registry.infomodel.Key orgKey =
              (javax.xml.registry.infomodel.Key) keyIter.next();
          id = orgKey.getId();
          System.out.println("Organization key is " + id);
        }
      } else {
        Iterator excIter = exceptions.iterator();
        Exception exception = null;
        while (excIter.hasNext()) {
          exception = (Exception) excIter.next();
          System.err.println("Exception on save: " + exception.toString());
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      if (connection != null) {
        try {
          connection.close();
        } catch (JAXRException je) {
          System.err.println("Connection close failed");
        }
      }
    }
    return id;
  }