예제 #1
0
  private static void putVMFiles(String remoteFilePath, String localFilePath) throws Exception {
    String serviceUrl = url.substring(0, url.lastIndexOf("sdk") - 1);
    String httpUrl =
        serviceUrl + "/folder" + remoteFilePath + "?dcPath=" + datacenter + "&dsName=" + datastore;
    httpUrl = httpUrl.replaceAll("\\ ", "%20");
    System.out.println("Putting VM File " + httpUrl);
    URL fileURL = new URL(httpUrl);
    HttpURLConnection conn = (HttpURLConnection) fileURL.openConnection();
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setAllowUserInteraction(true);

    // Maintain session
    List cookies = (List) headers.get("Set-cookie");
    cookieValue = (String) cookies.get(0);
    StringTokenizer tokenizer = new StringTokenizer(cookieValue, ";");
    cookieValue = tokenizer.nextToken();
    String path = "$" + tokenizer.nextToken();
    String cookie = "$Version=\"1\"; " + cookieValue + "; " + path;

    // set the cookie in the new request header
    Map map = new HashMap();
    map.put("Cookie", Collections.singletonList(cookie));
    ((BindingProvider) vimPort).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, map);

    conn.setRequestProperty("Cookie", cookie);
    conn.setRequestProperty("Content-Type", "application/octet-stream");
    conn.setRequestMethod("PUT");
    conn.setRequestProperty("Content-Length", "1024");
    long fileLen = new File(localFilePath).length();
    System.out.println("File size is: " + fileLen);
    conn.setChunkedStreamingMode((int) fileLen);
    OutputStream out = conn.getOutputStream();
    InputStream in = new BufferedInputStream(new FileInputStream(localFilePath));
    int bufLen = 9 * 1024;
    byte[] buf = new byte[bufLen];
    byte[] tmp = null;
    int len = 0;
    while ((len = in.read(buf, 0, bufLen)) != -1) {
      tmp = new byte[len];
      System.arraycopy(buf, 0, tmp, 0, len);
      out.write(tmp, 0, len);
    }
    in.close();
    out.close();
    conn.getResponseMessage();
    conn.disconnect();
  }
예제 #2
0
파일: VITop.java 프로젝트: mikelopez/jvm
  /** @throws Exception */
  private static void displayStats() throws Exception {
    String[][] statsList = getCounters();
    ManagedObjectReference hostmor = getHostByHostName(hostname);
    if (hostmor == null) {
      System.out.println("Host Not Found");
      return;
    }

    Object property;
    ArrayList props = (ArrayList) getDynamicProperty(perfManager, "perfCounter");

    if (props != null && props.size() > 0) {
      property = props.get(0);
    } else {
      property = null;
    }

    // ArrayOfPerfCounterInfo arrayCounter = (ArrayOfPerfCounterInfo) property;
    ArrayList counterInfoList = new ArrayList();
    counterInfoList.add(property);

    List<PerfMetricId> midVector = new ArrayList<PerfMetricId>();
    List<String> statNames = new ArrayList<String>();
    for (int i = 0; i < statsList.length; i++) {
      PerfCounterInfo counterInfo =
          getCounterInfo(counterInfoList, statsList[i][0], statsList[i][1]);
      if (counterInfo == null) {
        System.out.println(
            "Warning: Unable to find stat " + statsList[i][0] + " " + statsList[i][1]);
        continue;
      }
      String counterName = counterInfo.getNameInfo().getLabel();
      statNames.add(counterName);

      PerfMetricId pmid = new PerfMetricId();
      pmid.setCounterId(counterInfo.getKey());
      pmid.setInstance("");
      midVector.add(pmid);
    }
    List<PerfMetricId> midList = new ArrayList<PerfMetricId>(midVector);
    Collections.copy(midList, midVector);

    PerfQuerySpec spec = new PerfQuerySpec();
    spec.setEntity(hostmor);

    GregorianCalendar startTime = new GregorianCalendar();
    startTime.add(Calendar.SECOND, -60);

    XMLGregorianCalendar starttimexmlgc =
        DatatypeFactory.newInstance().newXMLGregorianCalendar(startTime);

    // spec.setStartTime(starttimexmlgc);
    spec.getMetricId().addAll(midList);
    spec.setIntervalId(new Integer(20));
    querySpec = spec;

    final List<String> statNames2 = statNames;
    javax.swing.SwingUtilities.invokeLater(
        new Runnable() {

          public void run() {
            createAndShowGUI("VM Name", statNames2);
          }
        });

    Timer timer = new Timer(true);
    timer.schedule(
        new TimerTask() {

          public void run() {
            refreshStats();
          }
        },
        1000,
        21000);
  }