]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/AddRemoteRepository.java
Add dep folder
[lgpl/argeo-commons.git] / server / plugins / org.argeo.jcr.ui.explorer / src / main / java / org / argeo / jcr / ui / explorer / commands / AddRemoteRepository.java
index f784bb6985af2d20a8931ee37c6e14189c02e6ed..e41edfca875163fa11fdd3353442e08f0e0fd1f2 100644 (file)
@@ -1,3 +1,18 @@
+/*
+ * Copyright (C) 2007-2012 Argeo GmbH
+ *
+ * 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,13 +24,16 @@ 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.eclipse.ui.utils.CommandUtils;
 import org.argeo.jcr.ArgeoJcrConstants;
 import org.argeo.jcr.ArgeoNames;
 import org.argeo.jcr.ArgeoTypes;
 import org.argeo.jcr.JcrUtils;
-import org.argeo.jcr.security.JcrKeyring;
+import org.argeo.jcr.UserJcrUtils;
 import org.argeo.jcr.ui.explorer.JcrExplorerConstants;
+import org.argeo.util.security.Keyring;
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
@@ -36,7 +54,6 @@ import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Text;
-import org.osgi.framework.BundleContext;
 
 /**
  * Connect to a remote repository and, if successful publish it as an OSGi
@@ -46,35 +63,14 @@ public class AddRemoteRepository extends AbstractHandler implements
                JcrExplorerConstants, ArgeoNames {
 
        private RepositoryFactory repositoryFactory;
-       private BundleContext bundleContext;
-
-       private JcrKeyring keyring;
+       private Repository nodeRepository;
+       private Keyring keyring;
 
        public Object execute(ExecutionEvent event) throws ExecutionException {
-               String uri = null;
-               if (event.getParameters().containsKey(PARAM_REPOSITORY_URI)) {
-                       uri = event.getParameter(PARAM_REPOSITORY_URI);
-               } else {
-                       RemoteRepositoryLoginDialog dlg = new RemoteRepositoryLoginDialog(
-                                       Display.getDefault().getActiveShell());
-                       if (dlg.open() == Dialog.OK) {
-                               uri = dlg.getUri();
-                       }
-               }
-
-               if (uri == null)
-                       return null;
-
-               try {
-                       Hashtable<String, String> params = new Hashtable<String, String>();
-                       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);
+               RemoteRepositoryLoginDialog dlg = new RemoteRepositoryLoginDialog(
+                               Display.getDefault().getActiveShell());
+               if (dlg.open() == Dialog.OK) {
+                       CommandUtils.callCommand(Refresh.ID);
                }
                return null;
        }
@@ -83,20 +79,20 @@ public class AddRemoteRepository extends AbstractHandler implements
                this.repositoryFactory = repositoryFactory;
        }
 
-       public void setBundleContext(BundleContext bundleContext) {
-               this.bundleContext = bundleContext;
+       public void setKeyring(Keyring keyring) {
+               this.keyring = keyring;
        }
 
-       public void setKeyring(JcrKeyring keyring) {
-               this.keyring = keyring;
+       public void setNodeRepository(Repository nodeRepository) {
+               this.nodeRepository = nodeRepository;
        }
 
        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 +112,12 @@ public class AddRemoteRepository extends AbstractHandler implements
                                        false));
                        setMessage("Login to remote repository", IMessageProvider.NONE);
                        name = createLT(composite, "Name", "remoteRepository");
-                       uriText = createLT(composite, "URI",
-                                       "http://localhost:7070/org.argeo.jcr.webapp/remoting/node");
+                       uri = createLT(composite, "URI",
+                                       "http://localhost:7070/data/jcr/node");
                        username = createLT(composite, "User", "");
                        password = createLP(composite, "Password");
+
+                       saveInKeyring = createLC(composite, "Remember password", false);
                        parent.pack();
                        return composite;
                }
@@ -138,14 +136,11 @@ 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<String, String> params = new Hashtable<String, String>();
                                params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI, checkedUriStr);
-                               // by default we use the URI as alias
-                               params.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS,
-                                               checkedUriStr);
                                Repository repository = repositoryFactory.getRepository(params);
                                if (username.getText().trim().equals("")) {// anonymous
                                        session = repository.login();
@@ -157,12 +152,11 @@ public class AddRemoteRepository extends AbstractHandler implements
                                                        username.getText(), pwd);
                                        session = repository.login(sc);
                                        MessageDialog.openInformation(getParentShell(), "Success",
-                                                       "Connection to '" + uriText.getText()
-                                                                       + "' 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);
                        }
@@ -170,24 +164,40 @@ public class AddRemoteRepository extends AbstractHandler implements
 
                @Override
                protected void okPressed() {
+                       Session nodeSession = null;
                        try {
-                               Session nodeSession = keyring.getSession();
-                               Node home = JcrUtils.getUserHome(nodeSession);
+                               nodeSession = nodeRepository.login();
+                               Node home = UserJcrUtils.getUserHome(nodeSession);
+
                                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();
+                               if (saveInKeyring.getSelection()) {
+                                       String pwdPath = remoteRepository.getPath() + '/'
+                                                       + ARGEO_PASSWORD;
+                                       keyring.set(pwdPath, password.getText().toCharArray());
+                               }
+                               nodeSession.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);
+                       } finally {
+                               JcrUtils.logoutQuietly(nodeSession);
                        }
-                       uri = uriText.getText();
-                       super.okPressed();
                }
 
                /** Creates label and text. */
@@ -199,6 +209,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
@@ -206,9 +226,5 @@ public class AddRemoteRepository extends AbstractHandler implements
                        text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
                        return text;
                }
-
-               public String getUri() {
-                       return uri;
-               }
        }
 }