]> 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
fix bug 85 : JCR explorer refresh was not implemented for TreeParent objects of type...
[lgpl/argeo-commons.git] / server / plugins / org.argeo.jcr.ui.explorer / src / main / java / org / argeo / jcr / ui / explorer / commands / AddRemoteRepository.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.jcr.ui.explorer.commands;
17
18 import java.net.URI;
19 import java.util.Hashtable;
20
21 import javax.jcr.Node;
22 import javax.jcr.Repository;
23 import javax.jcr.RepositoryFactory;
24 import javax.jcr.Session;
25 import javax.jcr.SimpleCredentials;
26
27 import org.argeo.ArgeoException;
28 import org.argeo.eclipse.ui.ErrorFeedback;
29 import org.argeo.jcr.ArgeoJcrConstants;
30 import org.argeo.jcr.ArgeoNames;
31 import org.argeo.jcr.ArgeoTypes;
32 import org.argeo.jcr.JcrUtils;
33 import org.argeo.jcr.security.JcrKeyring;
34 import org.argeo.jcr.ui.explorer.JcrExplorerConstants;
35 import org.eclipse.core.commands.AbstractHandler;
36 import org.eclipse.core.commands.ExecutionEvent;
37 import org.eclipse.core.commands.ExecutionException;
38 import org.eclipse.jface.dialogs.Dialog;
39 import org.eclipse.jface.dialogs.IMessageProvider;
40 import org.eclipse.jface.dialogs.MessageDialog;
41 import org.eclipse.jface.dialogs.TitleAreaDialog;
42 import org.eclipse.swt.SWT;
43 import org.eclipse.swt.events.SelectionAdapter;
44 import org.eclipse.swt.events.SelectionEvent;
45 import org.eclipse.swt.graphics.Point;
46 import org.eclipse.swt.layout.GridData;
47 import org.eclipse.swt.layout.GridLayout;
48 import org.eclipse.swt.widgets.Button;
49 import org.eclipse.swt.widgets.Composite;
50 import org.eclipse.swt.widgets.Control;
51 import org.eclipse.swt.widgets.Display;
52 import org.eclipse.swt.widgets.Label;
53 import org.eclipse.swt.widgets.Shell;
54 import org.eclipse.swt.widgets.Text;
55 import org.osgi.framework.BundleContext;
56
57 /**
58 * Connect to a remote repository and, if successful publish it as an OSGi
59 * service.
60 */
61 public class AddRemoteRepository extends AbstractHandler implements
62 JcrExplorerConstants, ArgeoNames {
63
64 private RepositoryFactory repositoryFactory;
65 private BundleContext bundleContext;
66
67 private JcrKeyring keyring;
68
69 public Object execute(ExecutionEvent event) throws ExecutionException {
70 String uri = null;
71 if (event.getParameters().containsKey(PARAM_REPOSITORY_URI)) {
72 // FIXME remove this
73 uri = event.getParameter(PARAM_REPOSITORY_URI);
74 if (uri == null)
75 return null;
76
77 try {
78 Hashtable<String, String> params = new Hashtable<String, String>();
79 params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI, uri);
80 // by default we use the URI as alias
81 params.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS, uri);
82 Repository repository = repositoryFactory.getRepository(params);
83 bundleContext.registerService(Repository.class.getName(),
84 repository, params);
85 } catch (Exception e) {
86 ErrorFeedback.show("Cannot add remote repository " + uri, e);
87 }
88 } else {
89 RemoteRepositoryLoginDialog dlg = new RemoteRepositoryLoginDialog(
90 Display.getDefault().getActiveShell());
91 if (dlg.open() == Dialog.OK) {
92 // uri = dlg.getUri();
93 }
94 }
95
96 return null;
97 }
98
99 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
100 this.repositoryFactory = repositoryFactory;
101 }
102
103 public void setBundleContext(BundleContext bundleContext) {
104 this.bundleContext = bundleContext;
105 }
106
107 public void setKeyring(JcrKeyring keyring) {
108 this.keyring = keyring;
109 }
110
111 class RemoteRepositoryLoginDialog extends TitleAreaDialog {
112 private Text name;
113 private Text uri;
114 private Text username;
115 private Text password;
116 private Button saveInKeyring;
117
118 public RemoteRepositoryLoginDialog(Shell parentShell) {
119 super(parentShell);
120 }
121
122 protected Point getInitialSize() {
123 return new Point(600, 400);
124 }
125
126 protected Control createDialogArea(Composite parent) {
127 Composite dialogarea = (Composite) super.createDialogArea(parent);
128 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
129 true));
130 Composite composite = new Composite(dialogarea, SWT.NONE);
131 composite.setLayout(new GridLayout(2, false));
132 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
133 false));
134 setMessage("Login to remote repository", IMessageProvider.NONE);
135 name = createLT(composite, "Name", "remoteRepository");
136 uri = createLT(composite, "URI",
137 "http://localhost:7070/org.argeo.jcr.webapp/remoting/node");
138 username = createLT(composite, "User", "");
139 password = createLP(composite, "Password");
140
141 saveInKeyring = createLC(composite, "Remember password", false);
142 parent.pack();
143 return composite;
144 }
145
146 @Override
147 protected void createButtonsForButtonBar(Composite parent) {
148 super.createButtonsForButtonBar(parent);
149 Button test = createButton(parent, 2, "Test", false);
150 test.addSelectionListener(new SelectionAdapter() {
151 public void widgetSelected(SelectionEvent arg0) {
152 testConnection();
153 }
154 });
155 }
156
157 void testConnection() {
158 Session session = null;
159 try {
160 URI checkedUri = new URI(uri.getText());
161 String checkedUriStr = checkedUri.toString();
162
163 Hashtable<String, String> params = new Hashtable<String, String>();
164 params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI, checkedUriStr);
165 // by default we use the URI as alias
166 params.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS,
167 checkedUriStr);
168 Repository repository = repositoryFactory.getRepository(params);
169 if (username.getText().trim().equals("")) {// anonymous
170 session = repository.login();
171 } else {
172 // FIXME use getTextChars() when upgrading to 3.7
173 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
174 char[] pwd = password.getText().toCharArray();
175 SimpleCredentials sc = new SimpleCredentials(
176 username.getText(), pwd);
177 session = repository.login(sc);
178 MessageDialog.openInformation(getParentShell(), "Success",
179 "Connection to '" + uri.getText() + "' successful");
180 }
181 } catch (Exception e) {
182 ErrorFeedback.show(
183 "Connection test failed for " + uri.getText(), e);
184 } finally {
185 JcrUtils.logoutQuietly(session);
186 }
187 }
188
189 @Override
190 protected void okPressed() {
191 try {
192 Session nodeSession = keyring.getSession();
193 Node home = JcrUtils.getUserHome(nodeSession);
194
195 // FIXME better deal with non existing home dir
196 if (home == null)
197 home = JcrUtils.createUserHomeIfNeeded(nodeSession,
198 nodeSession.getUserID());
199
200 Node remote = home.hasNode(ARGEO_REMOTE) ? home
201 .getNode(ARGEO_REMOTE) : home.addNode(ARGEO_REMOTE);
202 if (remote.hasNode(name.getText()))
203 throw new ArgeoException(
204 "There is already a remote repository named "
205 + name.getText());
206 Node remoteRepository = remote.addNode(name.getText(),
207 ArgeoTypes.ARGEO_REMOTE_REPOSITORY);
208 remoteRepository.setProperty(ARGEO_URI, uri.getText());
209 remoteRepository.setProperty(ARGEO_USER_ID, username.getText());
210 Node pwd = remoteRepository.addNode(ARGEO_PASSWORD);
211 pwd.getSession().save();
212 if (saveInKeyring.getSelection())
213 keyring.set(pwd.getPath(), password.getText().toCharArray());
214 keyring.getSession().save();
215 MessageDialog.openInformation(
216 getParentShell(),
217 "Repository Added",
218 "Remote repository '" + username.getText() + "@"
219 + uri.getText() + "' added");
220
221 super.okPressed();
222 } catch (Exception e) {
223 ErrorFeedback.show("Cannot add remote repository", e);
224 }
225 }
226
227 /** Creates label and text. */
228 protected Text createLT(Composite parent, String label, String initial) {
229 new Label(parent, SWT.NONE).setText(label);
230 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
231 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
232 text.setText(initial);
233 return text;
234 }
235
236 /** Creates label and check. */
237 protected Button createLC(Composite parent, String label,
238 Boolean initial) {
239 new Label(parent, SWT.NONE).setText(label);
240 Button check = new Button(parent, SWT.CHECK);
241 check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
242 check.setSelection(initial);
243 return check;
244 }
245
246 protected Text createLP(Composite parent, String label) {
247 new Label(parent, SWT.NONE).setText(label);
248 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
249 | SWT.PASSWORD);
250 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
251 return text;
252 }
253 }
254 }