2 * Copyright (C) 2007-2012 Mathieu Baudier
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.jcr
.ui
.explorer
.commands
;
19 import java
.util
.Hashtable
;
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
;
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
;
58 * Connect to a remote repository and, if successful publish it as an OSGi
61 public class AddRemoteRepository
extends AbstractHandler
implements
62 JcrExplorerConstants
, ArgeoNames
{
64 private RepositoryFactory repositoryFactory
;
65 private BundleContext bundleContext
;
67 private JcrKeyring keyring
;
69 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
71 if (event
.getParameters().containsKey(PARAM_REPOSITORY_URI
)) {
73 uri
= event
.getParameter(PARAM_REPOSITORY_URI
);
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(),
85 } catch (Exception e
) {
86 ErrorFeedback
.show("Cannot add remote repository " + uri
, e
);
89 RemoteRepositoryLoginDialog dlg
= new RemoteRepositoryLoginDialog(
90 Display
.getDefault().getActiveShell());
91 if (dlg
.open() == Dialog
.OK
) {
92 // uri = dlg.getUri();
99 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
100 this.repositoryFactory
= repositoryFactory
;
103 public void setBundleContext(BundleContext bundleContext
) {
104 this.bundleContext
= bundleContext
;
107 public void setKeyring(JcrKeyring keyring
) {
108 this.keyring
= keyring
;
111 class RemoteRepositoryLoginDialog
extends TitleAreaDialog
{
114 private Text username
;
115 private Text password
;
116 private Button saveInKeyring
;
118 public RemoteRepositoryLoginDialog(Shell parentShell
) {
122 protected Point
getInitialSize() {
123 return new Point(600, 400);
126 protected Control
createDialogArea(Composite parent
) {
127 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
128 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, 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,
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");
141 saveInKeyring
= createLC(composite
, "Remember password", false);
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
) {
157 void testConnection() {
158 Session session
= null;
160 URI checkedUri
= new URI(uri
.getText());
161 String checkedUriStr
= checkedUri
.toString();
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
,
168 Repository repository
= repositoryFactory
.getRepository(params
);
169 if (username
.getText().trim().equals("")) {// anonymous
170 session
= repository
.login();
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");
181 } catch (Exception e
) {
183 "Connection test failed for " + uri
.getText(), e
);
185 JcrUtils
.logoutQuietly(session
);
190 protected void okPressed() {
192 Session nodeSession
= keyring
.getSession();
193 Node home
= JcrUtils
.getUserHome(nodeSession
);
195 // FIXME better deal with non existing home dir
197 home
= JcrUtils
.createUserHomeIfNeeded(nodeSession
,
198 nodeSession
.getUserID());
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 "
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(
218 "Remote repository '" + username
.getText() + "@"
219 + uri
.getText() + "' added");
222 } catch (Exception e
) {
223 ErrorFeedback
.show("Cannot add remote repository", e
);
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
);
236 /** Creates label and check. */
237 protected Button
createLC(Composite parent
, String label
,
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
);
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
250 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));