protected static void storeData(
      IOObject ioobject, String location, RemoteRepository repository, ProgressListener l)
      throws RepositoryException {
    if (l != null) {
      l.setTotal(100);
      l.setCompleted(0);
    }

    HttpURLConnection conn;
    try {
      conn = repository.getResourceHTTPConnection(location, EntryStreamType.IOOBJECT, false);
    } catch (IOException e1) {
      throw new RepositoryException("Cannot store object at " + location + ": " + e1, e1);
    }
    OutputStream out = null;
    try {
      conn.setRequestProperty("Content-Type", RMContentType.IOOBJECT.getContentTypeString());
      conn.setRequestMethod("POST");
      conn.setDoOutput(true);
      conn.setDoInput(true);
      try {
        out = conn.getOutputStream();
      } catch (IOException e) {
        throw new RepositoryException(
            "Cannot upload object: " + conn.getResponseCode() + ": " + conn.getResponseMessage(),
            e);
      }
      IOObjectSerializer.getInstance().serialize(out, ioobject);
      out.close();
      BufferedReader in = null;
      try {
        try {
          in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
          LogService.getRoot().fine("BEGIN Reply of server:");
          String line;
          while ((line = in.readLine()) != null) {
            LogService.getRoot().fine(line);
          }
          LogService.getRoot().fine("END Reply of server.");
        } catch (IOException e) {
        }
      } finally {
        try {
          if (in != null) {
            in.close();
          }
        } catch (IOException e) {
        }
      }
    } catch (IOException e) {
      try {
        throw new RepositoryException(
            "Cannot store object at "
                + location
                + ": "
                + conn.getResponseCode()
                + ": "
                + conn.getResponseMessage(),
            e);
      } catch (IOException e1) {
        throw new RepositoryException("Cannot store object at " + location + ": " + e, e);
      }
    } finally {
      if (out != null) {
        try {
          out.close();
        } catch (IOException e) {
        }
      }
    }

    if (l != null) {
      l.setCompleted(100);
      l.complete();
    }
  }
  public void run() throws IllegalArgumentException, MalformedURLException, RepositoryException {
    final String url = getArgument("url", "http://localhost:8080");
    final String user = getArgument("user", "admin");
    final String password = getArgument("password", "changeit");

    System.err.println("Using RapidAnalytics server at " + url + "...");
    RemoteRepository repository =
        new RemoteRepository(new URL(url), "Temp", user, password.toCharArray(), true);
    RepositoryManager.getInstance(null).addRepository(repository);
    //
    //		GlobalAuthenticator.registerServerAuthenticator(new URLAuthenticator() {
    //			@Override
    //			public String getName() {
    //				return "Dummy command line authenticator";
    //			}
    //
    //			@Override
    //			public PasswordAuthentication getAuthentication(URL url) {
    //				return new PasswordAuthentication(user, password.toCharArray());
    //			}
    //		});

    if (isArgumentSet("check-state")) {
      long startTime;
      try {
        final RepositoryService repoService = repository.getRepositoryService();
        startTime = System.currentTimeMillis();
        repoService.getFolderContents("/");
      } catch (Exception e) {
        System.err.println("Error checking state of RapidAnalytics server at " + url + ": " + e);
        System.exit(1);
        return;
      }
      long responseTime = System.currentTimeMillis() - startTime;
      System.err.println(
          "RapidAnalytics server at " + url + " is up. Response time was " + responseTime + " ms.");
      System.exit(0);
      return;
    }

    if (isArgumentSet("wait-for-start")) {
      int waitForStart = getArgumentInt("wait-for-start", 120);
      long startTime = System.currentTimeMillis();
      long waitUntil = startTime + 1000 * waitForStart;
      Exception lastException;
      do {
        System.err.println("Checking server " + url);
        try {
          getManagementService(url, user, password);

          System.err.println("Server " + url + " is up.");
          System.exit(0);
          return;
        } catch (Exception e) {
          lastException = e;
          System.err.println("Server is not yet up: " + e);
          try {
            Thread.sleep(2000);
          } catch (InterruptedException e1) {
          }
        }
      } while (System.currentTimeMillis() < waitUntil);
      System.err.println("Server at " + url + " did not come up within " + waitForStart + "s.");
      if (lastException != null) {
        System.err.println("Last exception was: " + lastException);
        lastException.printStackTrace();
      }
      System.exit(1);
      return;
    }

    if (isArgumentSet("set-property")) {
      String property = getArgument("set-property", null);
      if (property == null) {
        System.err.println("Argument of --set-property must be of the form KEY=VALUE");
        System.exit(1);
        return;
      }
      String[] split = property.split("=", 2);
      if (split.length != 2) {
        System.err.println("Argument of --set-property must be of the form KEY=VALUE");
        System.exit(1);
        return;
      }
      String key = split[0];
      String value = split[1];
      try {
        ManagementService managementService = getManagementService(url, user, password);
        System.err.println("Setting " + key + " to value '" + value + "'");
        managementService.setGlobalProperty(key, value);
        System.exit(0);
      } catch (Exception e) {
        System.err.println("Failed to set property: " + e);
        e.printStackTrace();
        System.exit(1);
      }
      return;
    }

    if (isArgumentSet("check-setup")) {
      try {
        ManagementService managementService = getManagementService(url, user, password);
        System.err.println("Checking RapidAnalytics setup.");
        if (managementService.checkSetup()) {
          System.err.println("Setup is ok.");
          System.exit(0);
        } else {
          System.err.println("Setup is incomplete.");
          System.exit(1);
        }
      } catch (Exception e) {
        System.err.println("Failed to set property: " + e);
        e.printStackTrace();
        System.exit(1);
      }

      return;
    }

    delay = getArgumentInt("delay", 1000);
    if ("true".equals(getArgument("watch", "false"))) {
      watch = true;
      dumpStatus = true;
    }

    int processId = -1;
    String executeProcess = getArgument("execute-process", null);
    if (executeProcess != null) {
      System.err.println("Scheduling process execution for process " + executeProcess);
      ExecutionResponse result =
          repository.getProcessService().executeProcessSimple(executeProcess, null, null);
      if (result.getStatus() != 0) {
        System.err.println(
            "ERROR. Server responded with code "
                + result.getStatus()
                + ": "
                + result.getErrorMessage());
        System.exit(result.getStatus());
      } else {
        System.out.println("Process scheduled for " + result.getFirstExecution());
        int jobId = result.getJobId();
        if (dumpStatus) {
          processId = getJobId(jobId, repository.getProcessService());
        } else {
          processId = -1;
        }
      }
    } else {
      processId = getArgumentInt("process-id", -1);
      if (processId != -1) {
        dumpStatus = true;
      }
    }

    if (dumpStatus) {
      if (processId == -1) {
        throw new IllegalArgumentException(
            "You must use --process-id or --execute-service if --watch=true.");
      }
      RemoteProcessState state;
      do {
        ProcessResponse pInfo = repository.getProcessService().getRunningProcessesInfo(processId);
        if (pInfo == null) {
          throw new IllegalArgumentException("Process with id " + processId + " does not exist.");
        }
        dump(pInfo, System.out);
        if (watch) {
          try {
            Thread.sleep(delay);
          } catch (InterruptedException e) {
          }
        }
        state = RemoteProcessState.valueOf(pInfo.getState());
      } while (watch && !state.isTerminated());
      if (!state.isSuccessful()) {
        System.exit(1);
      } else {
        System.exit(0);
      }
    }
  }