2 * Copyright (C) 2007-2012 Argeo GmbH
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
.cms
.ui
.workbench
.internal
.jcr
.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
.cms
.ui
.workbench
.internal
.WorkbenchConstants
;
28 import org
.argeo
.cms
.ui
.workbench
.util
.CommandUtils
;
29 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
30 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
31 import org
.argeo
.jcr
.JcrUtils
;
32 import org
.argeo
.node
.ArgeoNames
;
33 import org
.argeo
.node
.ArgeoTypes
;
34 import org
.argeo
.node
.NodeConstants
;
35 import org
.argeo
.node
.NodeUtils
;
36 import org
.argeo
.node
.security
.Keyring
;
37 import org
.eclipse
.core
.commands
.AbstractHandler
;
38 import org
.eclipse
.core
.commands
.ExecutionEvent
;
39 import org
.eclipse
.core
.commands
.ExecutionException
;
40 import org
.eclipse
.jface
.dialogs
.Dialog
;
41 import org
.eclipse
.jface
.dialogs
.IMessageProvider
;
42 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
43 import org
.eclipse
.jface
.dialogs
.TitleAreaDialog
;
44 import org
.eclipse
.swt
.SWT
;
45 import org
.eclipse
.swt
.events
.SelectionAdapter
;
46 import org
.eclipse
.swt
.events
.SelectionEvent
;
47 import org
.eclipse
.swt
.graphics
.Point
;
48 import org
.eclipse
.swt
.layout
.GridData
;
49 import org
.eclipse
.swt
.layout
.GridLayout
;
50 import org
.eclipse
.swt
.widgets
.Button
;
51 import org
.eclipse
.swt
.widgets
.Composite
;
52 import org
.eclipse
.swt
.widgets
.Control
;
53 import org
.eclipse
.swt
.widgets
.Display
;
54 import org
.eclipse
.swt
.widgets
.Label
;
55 import org
.eclipse
.swt
.widgets
.Shell
;
56 import org
.eclipse
.swt
.widgets
.Text
;
59 * Connect to a remote repository and, if successful publish it as an OSGi
62 public class AddRemoteRepository
extends AbstractHandler
implements
63 WorkbenchConstants
, ArgeoNames
{
65 private RepositoryFactory repositoryFactory
;
66 private Repository nodeRepository
;
67 private Keyring keyring
;
69 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
70 RemoteRepositoryLoginDialog dlg
= new RemoteRepositoryLoginDialog(
71 Display
.getDefault().getActiveShell());
72 if (dlg
.open() == Dialog
.OK
) {
73 CommandUtils
.callCommand(Refresh
.ID
);
78 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
79 this.repositoryFactory
= repositoryFactory
;
82 public void setKeyring(Keyring keyring
) {
83 this.keyring
= keyring
;
86 public void setNodeRepository(Repository nodeRepository
) {
87 this.nodeRepository
= nodeRepository
;
90 class RemoteRepositoryLoginDialog
extends TitleAreaDialog
{
91 private static final long serialVersionUID
= 2234006887750103399L;
94 private Text username
;
95 private Text password
;
96 private Button saveInKeyring
;
98 public RemoteRepositoryLoginDialog(Shell parentShell
) {
102 protected Point
getInitialSize() {
103 return new Point(600, 400);
106 protected Control
createDialogArea(Composite parent
) {
107 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
108 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
110 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
111 composite
.setLayout(new GridLayout(2, false));
112 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
114 setMessage("Login to remote repository", IMessageProvider
.NONE
);
115 name
= createLT(composite
, "Name", "remoteRepository");
116 uri
= createLT(composite
, "URI",
117 "http://localhost:7070/jcr/node");
118 username
= createLT(composite
, "User", "");
119 password
= createLP(composite
, "Password");
121 saveInKeyring
= createLC(composite
, "Remember password", false);
127 protected void createButtonsForButtonBar(Composite parent
) {
128 super.createButtonsForButtonBar(parent
);
129 Button test
= createButton(parent
, 2, "Test", false);
130 test
.addSelectionListener(new SelectionAdapter() {
131 private static final long serialVersionUID
= -1829962269440419560L;
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(NodeConstants
.LABELED_URI
, checkedUriStr
);
147 Repository repository
= repositoryFactory
.getRepository(params
);
148 if (username
.getText().trim().equals("")) {// anonymous
149 session
= repository
.login();
151 // FIXME use getTextChars() when upgrading to 3.7
152 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
153 char[] pwd
= password
.getText().toCharArray();
154 SimpleCredentials sc
= new SimpleCredentials(
155 username
.getText(), pwd
);
156 session
= repository
.login(sc
, "main");
157 MessageDialog
.openInformation(getParentShell(), "Success",
158 "Connection to '" + uri
.getText() + "' successful");
160 } catch (Exception e
) {
162 "Connection test failed for " + uri
.getText(), e
);
164 JcrUtils
.logoutQuietly(session
);
169 protected void okPressed() {
170 Session nodeSession
= null;
172 nodeSession
= nodeRepository
.login();
173 Node home
= NodeUtils
.getUserHome(nodeSession
);
175 Node remote
= home
.hasNode(ARGEO_REMOTE
) ? home
176 .getNode(ARGEO_REMOTE
) : home
.addNode(ARGEO_REMOTE
);
177 if (remote
.hasNode(name
.getText()))
178 throw new EclipseUiException(
179 "There is already a remote repository named "
181 Node remoteRepository
= remote
.addNode(name
.getText(),
182 ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
);
183 remoteRepository
.setProperty(ARGEO_URI
, uri
.getText());
184 remoteRepository
.setProperty(ARGEO_USER_ID
, username
.getText());
186 if (saveInKeyring
.getSelection()) {
187 String pwdPath
= remoteRepository
.getPath() + '/'
189 keyring
.set(pwdPath
, password
.getText().toCharArray());
192 MessageDialog
.openInformation(
195 "Remote repository '" + username
.getText() + "@"
196 + uri
.getText() + "' added");
199 } catch (Exception e
) {
200 ErrorFeedback
.show("Cannot add remote repository", e
);
202 JcrUtils
.logoutQuietly(nodeSession
);
206 /** Creates label and text. */
207 protected Text
createLT(Composite parent
, String label
, String initial
) {
208 new Label(parent
, SWT
.NONE
).setText(label
);
209 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
);
210 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
211 text
.setText(initial
);
215 /** Creates label and check. */
216 protected Button
createLC(Composite parent
, String label
,
218 new Label(parent
, SWT
.NONE
).setText(label
);
219 Button check
= new Button(parent
, SWT
.CHECK
);
220 check
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
221 check
.setSelection(initial
);
225 protected Text
createLP(Composite parent
, String label
) {
226 new Label(parent
, SWT
.NONE
).setText(label
);
227 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
229 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));