public RepositoryTreeModel(
      final RepositoryManager root,
      final boolean onlyFolders,
      final boolean onlyWritableRepositories) {
    this.root = root;
    this.onlyFolders = onlyFolders;
    this.onlyWriteableRepositories = onlyWritableRepositories;
    for (Repository repository : root.getRepositories()) {
      repository.addRepositoryListener(repositoryListener);
    }
    root.addObserver(
        new Observer<Repository>() {

          @Override
          public void update(Observable<Repository> observable, Repository arg) {
            for (Repository repository : root.getRepositories()) {
              // if (onlyWritableRepositories) {
              repository.removeRepositoryListener(repositoryListener);
              repository.addRepositoryListener(repositoryListener);
              // }
            }
            TreeModelEvent e = new TreeModelEvent(this, new TreePath(root));
            for (TreeModelListener l : listeners.getListeners(TreeModelListener.class)) {
              l.treeStructureChanged(e);
            }
          }
        },
        true);
  }
  public RemoteProcessesTreeModel() {
    updateTimer.schedule(new UpdateTask(), UPDATE_PERIOD, UPDATE_PERIOD);

    RepositoryManager.getInstance(null).addObserver(repositoryManagerObserver, false);

    // initialize model
    for (RemoteRepository repo : RepositoryManager.getInstance(null).getRemoteRepositories()) {
      addRepository(repo);
    }
  }
 @Override
 public void actionPerformed(ActionEvent e) {
   String loc =
       RepositoryLocationChooser.selectLocation(
           lastLocation, "", RapidMinerGUI.getMainFrame(), true, false, true, true, true);
   if (loc != null) {
     RepositoryLocation location;
     try {
       location = new RepositoryLocation(loc);
     } catch (Exception ex) {
       SwingTools.showSimpleErrorMessage("malformed_rep_location", ex, loc);
       return;
     }
     try {
       if (location.locateEntry() != null) {
         // overwrite?
         if (SwingTools.showConfirmDialog("overwrite", ConfirmDialog.YES_NO_OPTION, location)
             != ConfirmDialog.YES_OPTION) {
           return;
         }
       }
       RepositoryManager.getInstance(null).store(object, location, null);
       lastLocation = location;
     } catch (RepositoryException ex) {
       SwingTools.showSimpleErrorMessage("cannot_store_obj_at_location", ex, loc);
     }
   }
 }
 private List<Repository> getWritableRepositories(RepositoryManager manager) {
   List<Repository> repositories = manager.getRepositories();
   List<Repository> writeableRepositories = new LinkedList<Repository>();
   for (Repository repository : repositories) {
     if (!repository.isReadOnly()) {
       writeableRepositories.add(repository);
     }
   }
   return writeableRepositories;
 }
 private TreeModelEvent makeChangeEvent(Entry entry) {
   TreePath path = getPathTo(entry.getContainingFolder());
   int index;
   if (entry instanceof Repository) {
     index = RepositoryManager.getInstance(null).getRepositories().indexOf(entry);
   } else {
     index = getIndexOfChild(entry.getContainingFolder(), entry);
   }
   return new TreeModelEvent(
       RepositoryTreeModel.this, path, new int[] {index}, new Object[] {entry});
 }
        @Override
        public void update(Observable<Repository> observable, final Repository arg) {
          // repository has been removed
          if (arg == null) {

            // rebuild repositories, observerRepositories and processes lists
            synchronized (repositories) {

              // remember old observed repositories
              Set<RemoteRepository> oldObserverRepositories =
                  new HashSet<RemoteRepository>(observedRepositories);

              // remove all current repositories
              List<RemoteRepository> oldRemoteRepositories =
                  new LinkedList<RemoteRepository>(repositories);
              for (RemoteRepository repo : oldRemoteRepositories) {
                removeRepository(repo);
              }

              // add all remote repositories that still exists
              List<RemoteRepository> newRemoteRepositories =
                  new LinkedList<RemoteRepository>(
                      RepositoryManager.getInstance(null).getRemoteRepositories());
              for (RemoteRepository repo : newRemoteRepositories) {
                addRepository(repo);
              }

              // start observing all repositories again that still exists
              for (RemoteRepository repo : oldObserverRepositories) {
                if (repositories.contains(repo)) {
                  observe(repo);
                }
              }
            }
          } else {
            // repository has been added
            if (arg instanceof RemoteRepository) {
              RemoteRepository addedRepo = (RemoteRepository) arg;
              addRepository(addedRepo);
            }
          }
        }
  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);
      }
    }
  }