1 package org
.argeo
.jcr
.ui
.explorer
.commands
;
4 import java
.util
.Hashtable
;
7 import javax
.jcr
.Repository
;
8 import javax
.jcr
.RepositoryFactory
;
9 import javax
.jcr
.Session
;
10 import javax
.jcr
.SimpleCredentials
;
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
;
43 * Connect to a remote repository and, if successful publish it as an OSGi
46 public class AddRemoteRepository
extends AbstractHandler
implements
47 JcrExplorerConstants
, ArgeoNames
{
49 private RepositoryFactory repositoryFactory
;
50 private BundleContext bundleContext
;
52 private JcrKeyring keyring
;
54 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
56 if (event
.getParameters().containsKey(PARAM_REPOSITORY_URI
)) {
58 uri
= event
.getParameter(PARAM_REPOSITORY_URI
);
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(),
70 } catch (Exception e
) {
71 ErrorFeedback
.show("Cannot add remote repository " + uri
, e
);
74 RemoteRepositoryLoginDialog dlg
= new RemoteRepositoryLoginDialog(
75 Display
.getDefault().getActiveShell());
76 if (dlg
.open() == Dialog
.OK
) {
77 // uri = dlg.getUri();
84 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
85 this.repositoryFactory
= repositoryFactory
;
88 public void setBundleContext(BundleContext bundleContext
) {
89 this.bundleContext
= bundleContext
;
92 public void setKeyring(JcrKeyring keyring
) {
93 this.keyring
= keyring
;
96 class RemoteRepositoryLoginDialog
extends TitleAreaDialog
{
99 private Text username
;
100 private Text password
;
102 public RemoteRepositoryLoginDialog(Shell parentShell
) {
106 protected Point
getInitialSize() {
107 return new Point(600, 400);
110 protected Control
createDialogArea(Composite parent
) {
111 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
112 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, 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,
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");
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
) {
139 void testConnection() {
140 Session session
= null;
142 URI checkedUri
= new URI(uri
.getText());
143 String checkedUriStr
= checkedUri
.toString();
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
,
150 Repository repository
= repositoryFactory
.getRepository(params
);
151 if (username
.getText().trim().equals("")) {// anonymous
152 session
= repository
.login();
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");
163 } catch (Exception e
) {
165 "Connection test failed for " + uri
.getText(), e
);
167 JcrUtils
.logoutQuietly(session
);
172 protected void okPressed() {
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 "
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());
189 MessageDialog
.openInformation(
192 "Remote repository '" + username
.getText() + "@"
193 + uri
.getText() + "' added");
194 } catch (Exception e
) {
195 // TODO Auto-generated catch block
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
);
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
214 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));