예제 #1
0
  private Object readValueFromDisk(InputStream is) throws IOException {
    BufferedReader fss = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    String className = fss.readLine();

    if (className == null) {
      return null;
    }

    String line = null;
    String content = "";
    while ((line = fss.readLine()) != null) {
      content += line;
    }

    fss.close();

    Class<?> clazz;
    try {
      if (className.contains("google")) {
        clazz = Class.forName(className);
        return mJsonFactory.createJsonParser(content).parseAndClose(clazz, null);
      } else {
        clazz = Class.forName(className);
        return mGson.fromJson(content, clazz);
      }
    } catch (IllegalArgumentException e) {
      Timber.e("Deserializing from disk failed", e);
      return null;
    } catch (ClassNotFoundException e) {
      throw new IOException(e.getMessage());
    }
  }
예제 #2
0
  private void writeValueToDisk(OutputStream os, Object o) throws IOException {

    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(os));

    String className = o.getClass().getCanonicalName();

    out.write(className + "\n");

    if (className.contains("google")) {
      mJsonFactory.createJsonGenerator(out).serialize(o);
    } else {
      String json = mGson.toJson(o);
      out.write(json);
    }

    out.close();
  }
  /**
   * Makes the payload for creating an instance.
   *
   * @param natIP external IP address
   * @param instanceName the name of the instance.
   * @return the payload for the POST request to create a new instance.
   */
  public Instance createPayload_instance(
      String instanceName, String bootDiskName, String machineType, String natIP)
      throws IOException {

    GoogleUrl googleUrl = new GoogleUrl(GOOGLE_PROJECT_NAME, ZONE);

    JsonObject json = new JsonObject();
    json.addProperty("kind", "compute#instance");
    json.addProperty("name", instanceName);
    json.addProperty("machineType", googleUrl.getMachineTypeUrl(machineType));

    JsonObject disksElem = new JsonObject();
    disksElem.addProperty("kind", "compute#attachedDisk");
    disksElem.addProperty("boot", true);
    disksElem.addProperty("type", "PERSISTENT");
    disksElem.addProperty("mode", "READ_WRITE");
    disksElem.addProperty("deviceName", bootDiskName);
    disksElem.addProperty("zone", googleUrl.getUrlPrefixWithProjectAndZone());
    disksElem.addProperty("source", googleUrl.getSource(bootDiskName));

    JsonArray jsonAr = new JsonArray();
    jsonAr.add(disksElem);
    json.add("disks", jsonAr);

    JsonObject networkInterfacesObj = new JsonObject();
    networkInterfacesObj.addProperty("kind", "compute#instanceNetworkInterface");
    networkInterfacesObj.addProperty("network", googleUrl.getDefaultNetwork());

    JsonObject accessConfigsObj = new JsonObject();
    accessConfigsObj.addProperty("name", "External NAT");
    accessConfigsObj.addProperty("type", "ONE_TO_ONE_NAT");
    if (natIP != null) {
      accessConfigsObj.addProperty("natIP", natIP);
    }

    JsonArray accessConfigsAr = new JsonArray();
    accessConfigsAr.add(accessConfigsObj);
    networkInterfacesObj.add("accessConfigs", accessConfigsAr);

    JsonArray networkInterfacesAr = new JsonArray();
    networkInterfacesAr.add(networkInterfacesObj);
    json.add("networkInterfaces", networkInterfacesAr);

    JsonObject serviceAccountsObj = new JsonObject();
    serviceAccountsObj.addProperty("kind", "compute#serviceAccount");
    serviceAccountsObj.addProperty("email", "default");
    JsonArray scopesAr = new JsonArray();
    scopesAr.add(new JsonPrimitive("https://www.googleapis.com/auth/userinfo.email"));
    scopesAr.add(new JsonPrimitive("https://www.googleapis.com/auth/compute"));
    scopesAr.add(new JsonPrimitive("https://www.googleapis.com/auth/devstorage.full_control"));
    serviceAccountsObj.add("scopes", scopesAr);
    JsonArray serviceAccountsAr = new JsonArray();
    serviceAccountsAr.add(serviceAccountsObj);
    json.add("serviceAccounts", serviceAccountsAr);

    JsonObject metadataObj = new JsonObject();

    JsonArray mdItemsAr = new JsonArray();
    JsonObject mdItemsObj = new JsonObject();
    mdItemsObj.addProperty("key", "startup-script-url");
    mdItemsObj.addProperty("value", "");
    mdItemsAr.add(mdItemsObj);
    metadataObj.add("items", mdItemsAr);
    json.add("metadata", metadataObj);
    String payload = json.toString();

    JsonFactory factory = JacksonFactory.getDefaultInstance();
    Instance instance = factory.fromString(payload, Instance.class);

    return instance;
  }