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
.eclipse
.ui
.ErrorFeedback
;
13 import org
.argeo
.jcr
.ArgeoJcrConstants
;
14 import org
.argeo
.jcr
.ArgeoNames
;
15 import org
.argeo
.jcr
.ArgeoTypes
;
16 import org
.argeo
.jcr
.JcrUtils
;
17 import org
.argeo
.jcr
.security
.JcrKeyring
;
18 import org
.argeo
.jcr
.ui
.explorer
.JcrExplorerConstants
;
19 import org
.eclipse
.core
.commands
.AbstractHandler
;
20 import org
.eclipse
.core
.commands
.ExecutionEvent
;
21 import org
.eclipse
.core
.commands
.ExecutionException
;
22 import org
.eclipse
.jface
.dialogs
.Dialog
;
23 import org
.eclipse
.jface
.dialogs
.IMessageProvider
;
24 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
25 import org
.eclipse
.jface
.dialogs
.TitleAreaDialog
;
26 import org
.eclipse
.swt
.SWT
;
27 import org
.eclipse
.swt
.events
.SelectionAdapter
;
28 import org
.eclipse
.swt
.events
.SelectionEvent
;
29 import org
.eclipse
.swt
.graphics
.Point
;
30 import org
.eclipse
.swt
.layout
.GridData
;
31 import org
.eclipse
.swt
.layout
.GridLayout
;
32 import org
.eclipse
.swt
.widgets
.Button
;
33 import org
.eclipse
.swt
.widgets
.Composite
;
34 import org
.eclipse
.swt
.widgets
.Control
;
35 import org
.eclipse
.swt
.widgets
.Display
;
36 import org
.eclipse
.swt
.widgets
.Label
;
37 import org
.eclipse
.swt
.widgets
.Shell
;
38 import org
.eclipse
.swt
.widgets
.Text
;
39 import org
.osgi
.framework
.BundleContext
;
42 * Connect to a remote repository and, if successful publish it as an OSGi
45 public class AddRemoteRepository
extends AbstractHandler
implements
46 JcrExplorerConstants
, ArgeoNames
{
48 private RepositoryFactory repositoryFactory
;
49 private BundleContext bundleContext
;
51 private JcrKeyring keyring
;
53 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
55 if (event
.getParameters().containsKey(PARAM_REPOSITORY_URI
)) {
56 uri
= event
.getParameter(PARAM_REPOSITORY_URI
);
58 RemoteRepositoryLoginDialog dlg
= new RemoteRepositoryLoginDialog(
59 Display
.getDefault().getActiveShell());
60 if (dlg
.open() == Dialog
.OK
) {
69 Hashtable
<String
, String
> params
= new Hashtable
<String
, String
>();
70 params
.put(ArgeoJcrConstants
.JCR_REPOSITORY_URI
, uri
);
71 // by default we use the URI as alias
72 params
.put(ArgeoJcrConstants
.JCR_REPOSITORY_ALIAS
, uri
);
73 Repository repository
= repositoryFactory
.getRepository(params
);
74 bundleContext
.registerService(Repository
.class.getName(),
76 } catch (Exception e
) {
77 ErrorFeedback
.show("Cannot add remote repository " + uri
, e
);
82 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
83 this.repositoryFactory
= repositoryFactory
;
86 public void setBundleContext(BundleContext bundleContext
) {
87 this.bundleContext
= bundleContext
;
90 public void setKeyring(JcrKeyring keyring
) {
91 this.keyring
= keyring
;
94 class RemoteRepositoryLoginDialog
extends TitleAreaDialog
{
98 private Text username
;
99 private Text password
;
101 public RemoteRepositoryLoginDialog(Shell parentShell
) {
105 protected Point
getInitialSize() {
106 return new Point(600, 400);
109 protected Control
createDialogArea(Composite parent
) {
110 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
111 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
113 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
114 composite
.setLayout(new GridLayout(2, false));
115 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
117 setMessage("Login to remote repository", IMessageProvider
.NONE
);
118 name
= createLT(composite
, "Name", "remoteRepository");
119 uriText
= createLT(composite
, "URI",
120 "http://localhost:7070/org.argeo.jcr.webapp/remoting/node");
121 username
= createLT(composite
, "User", "");
122 password
= createLP(composite
, "Password");
128 protected void createButtonsForButtonBar(Composite parent
) {
129 super.createButtonsForButtonBar(parent
);
130 Button test
= createButton(parent
, 2, "Test", false);
131 test
.addSelectionListener(new SelectionAdapter() {
132 public void widgetSelected(SelectionEvent arg0
) {
138 void testConnection() {
139 Session session
= null;
141 URI checkedUri
= new URI(uriText
.getText());
142 String checkedUriStr
= checkedUri
.toString();
144 Hashtable
<String
, String
> params
= new Hashtable
<String
, String
>();
145 params
.put(ArgeoJcrConstants
.JCR_REPOSITORY_URI
, checkedUriStr
);
146 // by default we use the URI as alias
147 params
.put(ArgeoJcrConstants
.JCR_REPOSITORY_ALIAS
,
149 Repository repository
= repositoryFactory
.getRepository(params
);
150 if (username
.getText().trim().equals("")) {// anonymous
151 session
= repository
.login();
153 // FIXME use getTextChars() when upgrading to 3.7
154 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
155 char[] pwd
= password
.getText().toCharArray();
156 SimpleCredentials sc
= new SimpleCredentials(
157 username
.getText(), pwd
);
158 session
= repository
.login(sc
);
159 MessageDialog
.openInformation(getParentShell(), "Success",
160 "Connection to '" + uriText
.getText()
163 } catch (Exception e
) {
165 "Connection test failed for " + uriText
.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 Node remoteRepository
= remote
.addNode(name
.getText(),
179 ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
);
180 remoteRepository
.setProperty(ARGEO_URI
, uriText
.getText());
181 remoteRepository
.setProperty(ARGEO_USER_ID
, username
.getText());
182 Node pwd
= remoteRepository
.addNode(ARGEO_PASSWORD
);
183 keyring
.set(pwd
.getPath(), password
.getText().toCharArray());
185 } catch (Exception e
) {
186 // TODO Auto-generated catch block
189 uri
= uriText
.getText();
193 /** Creates label and text. */
194 protected Text
createLT(Composite parent
, String label
, String initial
) {
195 new Label(parent
, SWT
.NONE
).setText(label
);
196 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
);
197 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
198 text
.setText(initial
);
202 protected Text
createLP(Composite parent
, String label
) {
203 new Label(parent
, SWT
.NONE
).setText(label
);
204 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
206 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
210 public String
getUri() {