]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/AddRemoteRepository.java
Improve logging
[lgpl/argeo-commons.git] / server / plugins / org.argeo.jcr.ui.explorer / src / main / java / org / argeo / jcr / ui / explorer / commands / AddRemoteRepository.java
1 package org.argeo.jcr.ui.explorer.commands;
2
3 import java.net.URI;
4 import java.util.Hashtable;
5
6 import javax.jcr.Node;
7 import javax.jcr.Repository;
8 import javax.jcr.RepositoryFactory;
9 import javax.jcr.Session;
10 import javax.jcr.SimpleCredentials;
11
12 import org.argeo.ArgeoException;
13 import org.argeo.eclipse.ui.ErrorFeedback;
14 import org.argeo.jcr.ArgeoJcrConstants;
15 import org.argeo.jcr.ArgeoNames;
16 import org.argeo.jcr.ArgeoTypes;
17 import org.argeo.jcr.JcrUtils;
18 import org.argeo.jcr.security.JcrKeyring;
19 import org.argeo.jcr.ui.explorer.JcrExplorerConstants;
20 import org.eclipse.core.commands.AbstractHandler;
21 import org.eclipse.core.commands.ExecutionEvent;
22 import org.eclipse.core.commands.ExecutionException;
23 import org.eclipse.jface.dialogs.Dialog;
24 import org.eclipse.jface.dialogs.IMessageProvider;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.dialogs.TitleAreaDialog;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.graphics.Point;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Display;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Shell;
39 import org.eclipse.swt.widgets.Text;
40 import org.osgi.framework.BundleContext;
41
42 /**
43 * Connect to a remote repository and, if successful publish it as an OSGi
44 * service.
45 */
46 public class AddRemoteRepository extends AbstractHandler implements
47 JcrExplorerConstants, ArgeoNames {
48
49 private RepositoryFactory repositoryFactory;
50 private BundleContext bundleContext;
51
52 private JcrKeyring keyring;
53
54 public Object execute(ExecutionEvent event) throws ExecutionException {
55 String uri = null;
56 if (event.getParameters().containsKey(PARAM_REPOSITORY_URI)) {
57 // FIXME remove this
58 uri = event.getParameter(PARAM_REPOSITORY_URI);
59 if (uri == null)
60 return null;
61
62 try {
63 Hashtable<String, String> params = new Hashtable<String, String>();
64 params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI, uri);
65 // by default we use the URI as alias
66 params.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS, uri);
67 Repository repository = repositoryFactory.getRepository(params);
68 bundleContext.registerService(Repository.class.getName(),
69 repository, params);
70 } catch (Exception e) {
71 ErrorFeedback.show("Cannot add remote repository " + uri, e);
72 }
73 } else {
74 RemoteRepositoryLoginDialog dlg = new RemoteRepositoryLoginDialog(
75 Display.getDefault().getActiveShell());
76 if (dlg.open() == Dialog.OK) {
77 // uri = dlg.getUri();
78 }
79 }
80
81 return null;
82 }
83
84 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
85 this.repositoryFactory = repositoryFactory;
86 }
87
88 public void setBundleContext(BundleContext bundleContext) {
89 this.bundleContext = bundleContext;
90 }
91
92 public void setKeyring(JcrKeyring keyring) {
93 this.keyring = keyring;
94 }
95
96 class RemoteRepositoryLoginDialog extends TitleAreaDialog {
97 private Text name;
98 private Text uri;
99 private Text username;
100 private Text password;
101
102 public RemoteRepositoryLoginDialog(Shell parentShell) {
103 super(parentShell);
104 }
105
106 protected Point getInitialSize() {
107 return new Point(600, 400);
108 }
109
110 protected Control createDialogArea(Composite parent) {
111 Composite dialogarea = (Composite) super.createDialogArea(parent);
112 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
113 true));
114 Composite composite = new Composite(dialogarea, SWT.NONE);
115 composite.setLayout(new GridLayout(2, false));
116 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
117 false));
118 setMessage("Login to remote repository", IMessageProvider.NONE);
119 name = createLT(composite, "Name", "remoteRepository");
120 uri = createLT(composite, "URI",
121 "http://localhost:7070/org.argeo.jcr.webapp/remoting/node");
122 username = createLT(composite, "User", "");
123 password = createLP(composite, "Password");
124 parent.pack();
125 return composite;
126 }
127
128 @Override
129 protected void createButtonsForButtonBar(Composite parent) {
130 super.createButtonsForButtonBar(parent);
131 Button test = createButton(parent, 2, "Test", false);
132 test.addSelectionListener(new SelectionAdapter() {
133 public void widgetSelected(SelectionEvent arg0) {
134 testConnection();
135 }
136 });
137 }
138
139 void testConnection() {
140 Session session = null;
141 try {
142 URI checkedUri = new URI(uri.getText());
143 String checkedUriStr = checkedUri.toString();
144
145 Hashtable<String, String> params = new Hashtable<String, String>();
146 params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI, checkedUriStr);
147 // by default we use the URI as alias
148 params.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS,
149 checkedUriStr);
150 Repository repository = repositoryFactory.getRepository(params);
151 if (username.getText().trim().equals("")) {// anonymous
152 session = repository.login();
153 } else {
154 // FIXME use getTextChars() when upgrading to 3.7
155 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
156 char[] pwd = password.getText().toCharArray();
157 SimpleCredentials sc = new SimpleCredentials(
158 username.getText(), pwd);
159 session = repository.login(sc);
160 MessageDialog.openInformation(getParentShell(), "Success",
161 "Connection to '" + uri.getText() + "' successful");
162 }
163 } catch (Exception e) {
164 ErrorFeedback.show(
165 "Connection test failed for " + uri.getText(), e);
166 } finally {
167 JcrUtils.logoutQuietly(session);
168 }
169 }
170
171 @Override
172 protected void okPressed() {
173 try {
174 Session nodeSession = keyring.getSession();
175 Node home = JcrUtils.getUserHome(nodeSession);
176 Node remote = home.hasNode(ARGEO_REMOTE) ? home
177 .getNode(ARGEO_REMOTE) : home.addNode(ARGEO_REMOTE);
178 if (remote.hasNode(name.getText()))
179 throw new ArgeoException(
180 "There is already a remote repository named "
181 + name.getText());
182 Node remoteRepository = remote.addNode(name.getText(),
183 ArgeoTypes.ARGEO_REMOTE_REPOSITORY);
184 remoteRepository.setProperty(ARGEO_URI, uri.getText());
185 remoteRepository.setProperty(ARGEO_USER_ID, username.getText());
186 Node pwd = remoteRepository.addNode(ARGEO_PASSWORD);
187 keyring.set(pwd.getPath(), password.getText().toCharArray());
188 nodeSession.save();
189 MessageDialog.openInformation(
190 getParentShell(),
191 "Repository Added",
192 "Remote repository '" + username.getText() + "@"
193 + uri.getText() + "' added");
194 } catch (Exception e) {
195 // TODO Auto-generated catch block
196 e.printStackTrace();
197 }
198 super.okPressed();
199 }
200
201 /** Creates label and text. */
202 protected Text createLT(Composite parent, String label, String initial) {
203 new Label(parent, SWT.NONE).setText(label);
204 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
205 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
206 text.setText(initial);
207 return text;
208 }
209
210 protected Text createLP(Composite parent, String label) {
211 new Label(parent, SWT.NONE).setText(label);
212 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
213 | SWT.PASSWORD);
214 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
215 return text;
216 }
217 }
218 }