X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=server%2Fplugins%2Forg.argeo.jcr.ui.explorer%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fjcr%2Fui%2Fexplorer%2Fcommands%2FAddRemoteRepository.java;h=6b5dfa8b0c490bafaa37059d6b38098b7e9e962b;hb=a093e5c849e70bbc754fa4462dbb6c2b59979973;hp=4e120ac478a88afab8946ba058c490afe2f5a769;hpb=ac74aa4ed8f0a3ff2c56ea98533ab1d0a5e6ca33;p=lgpl%2Fargeo-commons.git diff --git a/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/AddRemoteRepository.java b/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/AddRemoteRepository.java index 4e120ac47..6b5dfa8b0 100644 --- a/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/AddRemoteRepository.java +++ b/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/AddRemoteRepository.java @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2007-2012 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.argeo.jcr.ui.explorer.commands; import java.net.URI; @@ -9,6 +24,7 @@ import javax.jcr.RepositoryFactory; import javax.jcr.Session; import javax.jcr.SimpleCredentials; +import org.argeo.ArgeoException; import org.argeo.eclipse.ui.ErrorFeedback; import org.argeo.jcr.ArgeoJcrConstants; import org.argeo.jcr.ArgeoNames; @@ -53,29 +69,30 @@ public class AddRemoteRepository extends AbstractHandler implements public Object execute(ExecutionEvent event) throws ExecutionException { String uri = null; if (event.getParameters().containsKey(PARAM_REPOSITORY_URI)) { + // FIXME remove this uri = event.getParameter(PARAM_REPOSITORY_URI); + if (uri == null) + return null; + + try { + Hashtable params = new Hashtable(); + params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI, uri); + // by default we use the URI as alias + params.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS, uri); + Repository repository = repositoryFactory.getRepository(params); + bundleContext.registerService(Repository.class.getName(), + repository, params); + } catch (Exception e) { + ErrorFeedback.show("Cannot add remote repository " + uri, e); + } } else { RemoteRepositoryLoginDialog dlg = new RemoteRepositoryLoginDialog( Display.getDefault().getActiveShell()); if (dlg.open() == Dialog.OK) { - uri = dlg.getUri(); + // uri = dlg.getUri(); } } - if (uri == null) - return null; - - try { - Hashtable params = new Hashtable(); - params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI, uri); - // by default we use the URI as alias - params.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS, uri); - Repository repository = repositoryFactory.getRepository(params); - bundleContext.registerService(Repository.class.getName(), - repository, params); - } catch (Exception e) { - ErrorFeedback.show("Cannot add remote repository " + uri, e); - } return null; } @@ -92,11 +109,11 @@ public class AddRemoteRepository extends AbstractHandler implements } class RemoteRepositoryLoginDialog extends TitleAreaDialog { - private String uri; private Text name; - private Text uriText; + private Text uri; private Text username; private Text password; + private Button saveInKeyring; public RemoteRepositoryLoginDialog(Shell parentShell) { super(parentShell); @@ -116,10 +133,12 @@ public class AddRemoteRepository extends AbstractHandler implements false)); setMessage("Login to remote repository", IMessageProvider.NONE); name = createLT(composite, "Name", "remoteRepository"); - uriText = createLT(composite, "URI", + uri = createLT(composite, "URI", "http://localhost:7070/org.argeo.jcr.webapp/remoting/node"); username = createLT(composite, "User", ""); password = createLP(composite, "Password"); + + saveInKeyring = createLC(composite, "Remember password", false); parent.pack(); return composite; } @@ -138,7 +157,7 @@ public class AddRemoteRepository extends AbstractHandler implements void testConnection() { Session session = null; try { - URI checkedUri = new URI(uriText.getText()); + URI checkedUri = new URI(uri.getText()); String checkedUriStr = checkedUri.toString(); Hashtable params = new Hashtable(); @@ -157,11 +176,11 @@ public class AddRemoteRepository extends AbstractHandler implements username.getText(), pwd); session = repository.login(sc); MessageDialog.openInformation(getParentShell(), "Success", - "Connection to " + uri + "successful"); + "Connection to '" + uri.getText() + "' successful"); } } catch (Exception e) { ErrorFeedback.show( - "Connection test failed for " + uriText.getText(), e); + "Connection test failed for " + uri.getText(), e); } finally { JcrUtils.logoutQuietly(session); } @@ -172,21 +191,37 @@ public class AddRemoteRepository extends AbstractHandler implements try { Session nodeSession = keyring.getSession(); Node home = JcrUtils.getUserHome(nodeSession); + + // FIXME better deal with non existing home dir + if (home == null) + home = JcrUtils.createUserHomeIfNeeded(nodeSession, + nodeSession.getUserID()); + Node remote = home.hasNode(ARGEO_REMOTE) ? home .getNode(ARGEO_REMOTE) : home.addNode(ARGEO_REMOTE); + if (remote.hasNode(name.getText())) + throw new ArgeoException( + "There is already a remote repository named " + + name.getText()); Node remoteRepository = remote.addNode(name.getText(), ArgeoTypes.ARGEO_REMOTE_REPOSITORY); - remoteRepository.setProperty(ARGEO_URI, uriText.getText()); + remoteRepository.setProperty(ARGEO_URI, uri.getText()); remoteRepository.setProperty(ARGEO_USER_ID, username.getText()); Node pwd = remoteRepository.addNode(ARGEO_PASSWORD); - keyring.set(pwd.getPath(), password.getText().toCharArray()); - nodeSession.save(); + pwd.getSession().save(); + if (saveInKeyring.getSelection()) + keyring.set(pwd.getPath(), password.getText().toCharArray()); + keyring.getSession().save(); + MessageDialog.openInformation( + getParentShell(), + "Repository Added", + "Remote repository '" + username.getText() + "@" + + uri.getText() + "' added"); + + super.okPressed(); } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + ErrorFeedback.show("Cannot add remote repository", e); } - uri = uriText.getText(); - super.okPressed(); } /** Creates label and text. */ @@ -198,6 +233,16 @@ public class AddRemoteRepository extends AbstractHandler implements return text; } + /** Creates label and check. */ + protected Button createLC(Composite parent, String label, + Boolean initial) { + new Label(parent, SWT.NONE).setText(label); + Button check = new Button(parent, SWT.CHECK); + check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); + check.setSelection(initial); + return check; + } + protected Text createLP(Composite parent, String label) { new Label(parent, SWT.NONE).setText(label); Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER @@ -205,9 +250,5 @@ public class AddRemoteRepository extends AbstractHandler implements text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); return text; } - - public String getUri() { - return uri; - } } }