Search in sources :

Example 1 with SimpleCredentialsStorage

use of org.archicontribs.modelrepository.authentication.SimpleCredentialsStorage in project archi-modelrepository-plugin by archi-contribs.

the class ModelRepositoryPreferencePage method performOk.

@Override
public boolean performOk() {
    String name = fUserNameTextField.getText();
    String email = fUserEmailTextField.getText();
    try {
        GraficoUtils.saveGitConfigUserDetails(name, email);
    } catch (IOException | ConfigInvalidException ex) {
        ex.printStackTrace();
    }
    getPreferenceStore().setValue(PREFS_REPOSITORY_FOLDER, fUserRepoFolderTextField.getText());
    getPreferenceStore().setValue(PREFS_STORE_REPO_CREDENTIALS, fStoreCredentialsButton.getSelection());
    getPreferenceStore().setValue(PREFS_PROXY_USE, fUseProxyButton.getSelection());
    getPreferenceStore().setValue(PREFS_PROXY_HOST, fProxyHostTextField.getText());
    getPreferenceStore().setValue(PREFS_PROXY_PORT, fProxyPortTextField.getText());
    getPreferenceStore().setValue(PREFS_PROXY_REQUIRES_AUTHENTICATION, fRequiresProxyAuthenticationButton.getSelection());
    try {
        SimpleCredentialsStorage sc = new SimpleCredentialsStorage(new File(ModelRepositoryPlugin.INSTANCE.getUserModelRepositoryFolder(), IGraficoConstants.PROXY_CREDENTIALS_FILE));
        sc.store(fProxyUserNameTextField.getText(), fProxyUserPasswordTextField.getText());
    } catch (NoSuchAlgorithmException | IOException ex) {
        ex.printStackTrace();
    }
    return true;
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) SimpleCredentialsStorage(org.archicontribs.modelrepository.authentication.SimpleCredentialsStorage) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) File(java.io.File)

Example 2 with SimpleCredentialsStorage

use of org.archicontribs.modelrepository.authentication.SimpleCredentialsStorage in project archi-modelrepository-plugin by archi-contribs.

the class ModelRepositoryTreeViewer method startBackgroundJobs.

/**
 * Set up and start background jobs
 */
protected void startBackgroundJobs() {
    // Refresh file system job
    Job refreshFileSystemJob = new // $NON-NLS-1$
    Job(// $NON-NLS-1$
    "Refresh File System Job") {

        // last modified
        long lastModified = 0L;

        @Override
        public IStatus run(IProgressMonitor monitor) {
            if (getControl().isDisposed()) {
                return Status.OK_STATUS;
            }
            // If rootFolder has been modifed (child folder added/deleted/renamed) refresh
            File rootFolder = getRootFolder();
            if (lastModified != 0L && rootFolder.lastModified() != lastModified) {
                refreshInBackground();
            }
            lastModified = rootFolder.lastModified();
            if (!getControl().isDisposed()) {
                // Schedule again in 5 seconds
                schedule(5000);
            }
            return Status.OK_STATUS;
        }
    };
    refreshFileSystemJob.schedule(5000);
    // Fetch Job
    Job fetchJob = new // $NON-NLS-1$
    Job(// $NON-NLS-1$
    "Fetch Job") {

        @Override
        public IStatus run(IProgressMonitor monitor) {
            boolean needsRefresh = false;
            for (IArchiRepository repo : getRepositories(getRootFolder())) {
                if (getControl().isDisposed()) {
                    return Status.OK_STATUS;
                }
                // If the user name and password are stored
                SimpleCredentialsStorage scs = new SimpleCredentialsStorage(new File(repo.getLocalGitFolder(), IGraficoConstants.REPO_CREDENTIALS_FILE));
                try {
                    String userName = scs.getUsername();
                    String userPassword = scs.getPassword();
                    if (userName != null && userPassword != null) {
                        repo.fetchFromRemote(userName, userPassword, null, false);
                        needsRefresh = true;
                    }
                } catch (IOException | GitAPIException ex) {
                // silence is golden
                }
            }
            if (needsRefresh) {
                refreshInBackground();
            }
            if (!getControl().isDisposed()) {
                // Schedule again in 20 seconds
                schedule(20000);
            }
            return Status.OK_STATUS;
        }

        @Override
        protected void canceling() {
            /*
                 * Because the Git Fetch process doesn't respond to cancel requests we can't cancel it when it is running.
                 * So, if the user closes the app this job might be running. So we will wait for this job to finish
                 */
            int timeout = 0;
            final int delay = 100;
            try {
                while (getState() == Job.RUNNING) {
                    Thread.sleep(delay);
                    timeout += delay;
                    if (timeout > 30000) {
                        // don't wait longer than this
                        break;
                    }
                }
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    };
    fetchJob.schedule(1000);
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SimpleCredentialsStorage(org.archicontribs.modelrepository.authentication.SimpleCredentialsStorage) IArchiRepository(org.archicontribs.modelrepository.grafico.IArchiRepository) IOException(java.io.IOException) Job(org.eclipse.core.runtime.jobs.Job) File(java.io.File)

Example 3 with SimpleCredentialsStorage

use of org.archicontribs.modelrepository.authentication.SimpleCredentialsStorage in project archi-modelrepository-plugin by archi-contribs.

the class AbstractModelAction method getUserNameAndPasswordFromCredentialsFileOrDialog.

/**
 * Get user name and password from credentials file if prefs set or from dialog
 * @param storageFileName
 * @param shell
 * @return the username and password, or null
 */
protected UsernamePassword getUserNameAndPasswordFromCredentialsFileOrDialog(Shell shell) {
    boolean doStoreInCredentialsFile = ModelRepositoryPlugin.INSTANCE.getPreferenceStore().getBoolean(IPreferenceConstants.PREFS_STORE_REPO_CREDENTIALS);
    SimpleCredentialsStorage scs = new SimpleCredentialsStorage(new File(getRepository().getLocalGitFolder(), IGraficoConstants.REPO_CREDENTIALS_FILE));
    // Is it stored?
    if (doStoreInCredentialsFile && scs.hasCredentialsFile()) {
        try {
            return new UsernamePassword(scs.getUsername(), scs.getPassword());
        } catch (IOException ex) {
            displayErrorDialog(Messages.AbstractModelAction_9, ex);
        }
    }
    // Else ask the user
    UserNamePasswordDialog dialog = new UserNamePasswordDialog(shell, scs);
    if (dialog.open() == Window.OK) {
        return new UsernamePassword(dialog.getUsername(), dialog.getPassword());
    }
    return null;
}
Also used : SimpleCredentialsStorage(org.archicontribs.modelrepository.authentication.SimpleCredentialsStorage) IOException(java.io.IOException) File(java.io.File) UserNamePasswordDialog(org.archicontribs.modelrepository.dialogs.UserNamePasswordDialog) UsernamePassword(org.archicontribs.modelrepository.authentication.UsernamePassword)

Example 4 with SimpleCredentialsStorage

use of org.archicontribs.modelrepository.authentication.SimpleCredentialsStorage in project archi-modelrepository-plugin by archi-contribs.

the class CloneModelAction method run.

@Override
public void run() {
    CloneInputDialog dialog = new CloneInputDialog(fWindow.getShell());
    if (dialog.open() != Window.OK) {
        return;
    }
    final String repoURL = dialog.getURL();
    final String userName = dialog.getUsername();
    final String userPassword = dialog.getPassword();
    if (!StringUtils.isSet(repoURL) && !StringUtils.isSet(userName) && !StringUtils.isSet(userPassword)) {
        return;
    }
    File localRepoFolder = new File(ModelRepositoryPlugin.INSTANCE.getUserModelRepositoryFolder(), GraficoUtils.getLocalGitFolderName(repoURL));
    // Folder is not empty
    if (localRepoFolder.exists() && localRepoFolder.isDirectory() && localRepoFolder.list().length > 0) {
        MessageDialog.openError(fWindow.getShell(), Messages.CloneModelAction_0, // $NON-NLS-1$
        Messages.CloneModelAction_2 + " " + localRepoFolder.getAbsolutePath());
        return;
    }
    setRepository(new ArchiRepository(localRepoFolder));
    /**
     * Wrapper class to handle progress monitor
     */
    class CloneProgressHandler extends ProgressHandler {

        @Override
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            super.run(monitor);
            try {
                monitor.beginTask(Messages.CloneModelAction_4, IProgressMonitor.UNKNOWN);
                // Proxy check
                ProxyAuthenticater.update(repoURL);
                // Clone
                getRepository().cloneModel(repoURL, userName, userPassword, this);
                monitor.subTask(Messages.CloneModelAction_5);
                // Load it from the Grafico files if we can
                IArchimateModel graficoModel = new GraficoModelLoader(getRepository()).loadModel();
                // We couldn't load it from Grafico so create a new blank model
                if (graficoModel == null) {
                    // New one. This will open in the tree
                    IArchimateModel model = IEditorModelManager.INSTANCE.createNewModel();
                    model.setFile(getRepository().getTempModelFile());
                    // And Save it
                    IEditorModelManager.INSTANCE.saveModel(model);
                    // Export to Grafico
                    getRepository().exportModelToGraficoFiles();
                    // And do a first commit
                    getRepository().commitChanges(Messages.CloneModelAction_6, false);
                    // Save the checksum
                    getRepository().saveChecksum();
                }
                // Store repo credentials if option is set
                if (ModelRepositoryPlugin.INSTANCE.getPreferenceStore().getBoolean(IPreferenceConstants.PREFS_STORE_REPO_CREDENTIALS)) {
                    SimpleCredentialsStorage scs = new SimpleCredentialsStorage(new File(getRepository().getLocalGitFolder(), IGraficoConstants.REPO_CREDENTIALS_FILE));
                    scs.store(userName, userPassword);
                }
                // Notify listeners
                notifyChangeListeners(IRepositoryListener.REPOSITORY_ADDED);
            } catch (GitAPIException | IOException | NoSuchAlgorithmException ex) {
                displayErrorDialog(Messages.CloneModelAction_0, ex);
            } finally {
                monitor.done();
            }
        }
    }
    Display.getCurrent().asyncExec(new Runnable() {

        @Override
        public void run() {
            try {
                ProgressMonitorDialog pmDialog = new ProgressMonitorDialog(fWindow.getShell());
                pmDialog.run(false, true, new CloneProgressHandler());
            } catch (InvocationTargetException | InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    });
}
Also used : ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) CloneInputDialog(org.archicontribs.modelrepository.dialogs.CloneInputDialog) ArchiRepository(org.archicontribs.modelrepository.grafico.ArchiRepository) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) GraficoModelLoader(org.archicontribs.modelrepository.grafico.GraficoModelLoader) SimpleCredentialsStorage(org.archicontribs.modelrepository.authentication.SimpleCredentialsStorage) File(java.io.File) IArchimateModel(com.archimatetool.model.IArchimateModel)

Example 5 with SimpleCredentialsStorage

use of org.archicontribs.modelrepository.authentication.SimpleCredentialsStorage in project archi-modelrepository-plugin by archi-contribs.

the class CreateRepoFromModelAction method run.

@Override
public void run() {
    NewModelRepoDialog dialog = new NewModelRepoDialog(fWindow.getShell());
    if (dialog.open() != Window.OK) {
        return;
    }
    final String repoURL = dialog.getURL();
    final String userName = dialog.getUsername();
    final String userPassword = dialog.getPassword();
    if (!StringUtils.isSet(repoURL) && !StringUtils.isSet(userName) && !StringUtils.isSet(userPassword)) {
        return;
    }
    File localRepoFolder = new File(ModelRepositoryPlugin.INSTANCE.getUserModelRepositoryFolder(), GraficoUtils.getLocalGitFolderName(repoURL));
    // Folder is not empty
    if (localRepoFolder.exists() && localRepoFolder.isDirectory() && localRepoFolder.list().length > 0) {
        MessageDialog.openError(fWindow.getShell(), Messages.CreateRepoFromModelAction_1, Messages.CreateRepoFromModelAction_2 + " " + // $NON-NLS-1$
        localRepoFolder.getAbsolutePath());
        return;
    }
    setRepository(new ArchiRepository(localRepoFolder));
    /**
     * Wrapper class to handle progress monitor
     */
    class CreateRepoProgressHandler extends ProgressHandler {

        @Override
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            super.run(monitor);
            try {
                monitor.beginTask(Messages.CreateRepoFromModelAction_3, IProgressMonitor.UNKNOWN);
                // Proxy check
                ProxyAuthenticater.update(repoURL);
                // Create a new repo
                try (Git git = getRepository().createNewLocalGitRepository(repoURL)) {
                }
                // TODO: If the model has not been saved yet this is fine but if the model already exists
                // We should tell the user this is the case
                // Set new file location
                fModel.setFile(getRepository().getTempModelFile());
                // And Save it
                IEditorModelManager.INSTANCE.saveModel(fModel);
                // Export to Grafico
                getRepository().exportModelToGraficoFiles();
                monitor.subTask(Messages.CreateRepoFromModelAction_4);
                // Commit changes
                getRepository().commitChanges(Messages.CreateRepoFromModelAction_5, false);
                monitor.subTask(Messages.CreateRepoFromModelAction_6);
                // Push
                getRepository().pushToRemote(userName, userPassword, null);
                // Store repo credentials if option is set
                if (ModelRepositoryPlugin.INSTANCE.getPreferenceStore().getBoolean(IPreferenceConstants.PREFS_STORE_REPO_CREDENTIALS)) {
                    SimpleCredentialsStorage sc = new SimpleCredentialsStorage(new File(getRepository().getLocalGitFolder(), IGraficoConstants.REPO_CREDENTIALS_FILE));
                    sc.store(userName, userPassword);
                }
                // Save the checksum
                getRepository().saveChecksum();
            } catch (GitAPIException | IOException | NoSuchAlgorithmException | URISyntaxException ex) {
                displayErrorDialog(Messages.CreateRepoFromModelAction_7, ex);
            } finally {
                monitor.done();
            }
        }
    }
    Display.getCurrent().asyncExec(new Runnable() {

        @Override
        public void run() {
            try {
                ProgressMonitorDialog pmDialog = new ProgressMonitorDialog(fWindow.getShell());
                pmDialog.run(false, true, new CreateRepoProgressHandler());
            } catch (InvocationTargetException | InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    });
}
Also used : ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) NewModelRepoDialog(org.archicontribs.modelrepository.dialogs.NewModelRepoDialog) ArchiRepository(org.archicontribs.modelrepository.grafico.ArchiRepository) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URISyntaxException(java.net.URISyntaxException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Git(org.eclipse.jgit.api.Git) SimpleCredentialsStorage(org.archicontribs.modelrepository.authentication.SimpleCredentialsStorage) File(java.io.File)

Aggregations

File (java.io.File)6 IOException (java.io.IOException)6 SimpleCredentialsStorage (org.archicontribs.modelrepository.authentication.SimpleCredentialsStorage)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 ArchiRepository (org.archicontribs.modelrepository.grafico.ArchiRepository)2 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)2 IArchimateModel (com.archimatetool.model.IArchimateModel)1 URISyntaxException (java.net.URISyntaxException)1 UsernamePassword (org.archicontribs.modelrepository.authentication.UsernamePassword)1 CloneInputDialog (org.archicontribs.modelrepository.dialogs.CloneInputDialog)1 NewModelRepoDialog (org.archicontribs.modelrepository.dialogs.NewModelRepoDialog)1 UserNamePasswordDialog (org.archicontribs.modelrepository.dialogs.UserNamePasswordDialog)1 GraficoModelLoader (org.archicontribs.modelrepository.grafico.GraficoModelLoader)1 IArchiRepository (org.archicontribs.modelrepository.grafico.IArchiRepository)1 Job (org.eclipse.core.runtime.jobs.Job)1 Git (org.eclipse.jgit.api.Git)1 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)1 PersonIdent (org.eclipse.jgit.lib.PersonIdent)1