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
;
117 public RemoteRepositoryLoginDialog(Shell parentShell
) {
121 protected Point
getInitialSize() {
122 return new Point(600, 400);
125 protected Control
createDialogArea(Composite parent
) {
126 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
127 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
129 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
130 composite
.setLayout(new GridLayout(2, false));
131 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
133 setMessage("Login to remote repository", IMessageProvider
.NONE
);
134 name
= createLT(composite
, "Name", "remoteRepository");
135 uri
= createLT(composite
, "URI",
136 "http://localhost:7070/org.argeo.jcr.webapp/remoting/node");
137 username
= createLT(composite
, "User", "");
138 password
= createLP(composite
, "Password");
144 protected void createButtonsForButtonBar(Composite parent
) {
145 super.createButtonsForButtonBar(parent
);
146 Button test
= createButton(parent
, 2, "Test", false);
147 test
.addSelectionListener(new SelectionAdapter() {
148 public void widgetSelected(SelectionEvent arg0
) {
154 void testConnection() {
155 Session session
= null;
157 URI checkedUri
= new URI(uri
.getText());
158 String checkedUriStr
= checkedUri
.toString();
160 Hashtable
<String
, String
> params
= new Hashtable
<String
, String
>();
161 params
.put(ArgeoJcrConstants
.JCR_REPOSITORY_URI
, checkedUriStr
);
162 // by default we use the URI as alias
163 params
.put(ArgeoJcrConstants
.JCR_REPOSITORY_ALIAS
,
165 Repository repository
= repositoryFactory
.getRepository(params
);
166 if (username
.getText().trim().equals("")) {// anonymous
167 session
= repository
.login();
169 // FIXME use getTextChars() when upgrading to 3.7
170 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
171 char[] pwd
= password
.getText().toCharArray();
172 SimpleCredentials sc
= new SimpleCredentials(
173 username
.getText(), pwd
);
174 session
= repository
.login(sc
);
175 MessageDialog
.openInformation(getParentShell(), "Success",
176 "Connection to '" + uri
.getText() + "' successful");
178 } catch (Exception e
) {
180 "Connection test failed for " + uri
.getText(), e
);
182 JcrUtils
.logoutQuietly(session
);
187 protected void okPressed() {
189 Session nodeSession
= keyring
.getSession();
190 Node home
= JcrUtils
.getUserHome(nodeSession
);
191 Node remote
= home
.hasNode(ARGEO_REMOTE
) ? home
192 .getNode(ARGEO_REMOTE
) : home
.addNode(ARGEO_REMOTE
);
193 if (remote
.hasNode(name
.getText()))
194 throw new ArgeoException(
195 "There is already a remote repository named "
197 Node remoteRepository
= remote
.addNode(name
.getText(),
198 ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
);
199 remoteRepository
.setProperty(ARGEO_URI
, uri
.getText());
200 remoteRepository
.setProperty(ARGEO_USER_ID
, username
.getText());
201 Node pwd
= remoteRepository
.addNode(ARGEO_PASSWORD
);
202 keyring
.set(pwd
.getPath(), password
.getText().toCharArray());
204 MessageDialog
.openInformation(
207 "Remote repository '" + username
.getText() + "@"
208 + uri
.getText() + "' added");
210 } catch (Exception e
) {
211 ErrorFeedback
.show("Cannot add remote repository", e
);
215 /** Creates label and text. */
216 protected Text
createLT(Composite parent
, String label
, String initial
) {
217 new Label(parent
, SWT
.NONE
).setText(label
);
218 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
);
219 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
220 text
.setText(initial
);
224 protected Text
createLP(Composite parent
, String label
) {
225 new Label(parent
, SWT
.NONE
).setText(label
);
226 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
228 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));