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
.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
.eclipse
.ui
.utils
.CommandUtils
;
30 import org
.argeo
.jcr
.ArgeoJcrConstants
;
31 import org
.argeo
.jcr
.ArgeoNames
;
32 import org
.argeo
.jcr
.ArgeoTypes
;
33 import org
.argeo
.jcr
.JcrUtils
;
34 import org
.argeo
.jcr
.UserJcrUtils
;
35 import org
.argeo
.jcr
.ui
.explorer
.JcrExplorerConstants
;
36 import org
.argeo
.util
.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 JcrExplorerConstants
, 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
{
93 private Text username
;
94 private Text password
;
95 private Button saveInKeyring
;
97 public RemoteRepositoryLoginDialog(Shell parentShell
) {
101 protected Point
getInitialSize() {
102 return new Point(600, 400);
105 protected Control
createDialogArea(Composite parent
) {
106 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
107 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
109 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
110 composite
.setLayout(new GridLayout(2, false));
111 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
113 setMessage("Login to remote repository", IMessageProvider
.NONE
);
114 name
= createLT(composite
, "Name", "remoteRepository");
115 uri
= createLT(composite
, "URI",
116 "http://localhost:7070/data/jcr/node");
117 username
= createLT(composite
, "User", "");
118 password
= createLP(composite
, "Password");
120 saveInKeyring
= createLC(composite
, "Remember password", false);
126 protected void createButtonsForButtonBar(Composite parent
) {
127 super.createButtonsForButtonBar(parent
);
128 Button test
= createButton(parent
, 2, "Test", false);
129 test
.addSelectionListener(new SelectionAdapter() {
130 public void widgetSelected(SelectionEvent arg0
) {
136 void testConnection() {
137 Session session
= null;
139 URI checkedUri
= new URI(uri
.getText());
140 String checkedUriStr
= checkedUri
.toString();
142 Hashtable
<String
, String
> params
= new Hashtable
<String
, String
>();
143 params
.put(ArgeoJcrConstants
.JCR_REPOSITORY_URI
, checkedUriStr
);
144 Repository repository
= repositoryFactory
.getRepository(params
);
145 if (username
.getText().trim().equals("")) {// anonymous
146 session
= repository
.login();
148 // FIXME use getTextChars() when upgrading to 3.7
149 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
150 char[] pwd
= password
.getText().toCharArray();
151 SimpleCredentials sc
= new SimpleCredentials(
152 username
.getText(), pwd
);
153 session
= repository
.login(sc
);
154 MessageDialog
.openInformation(getParentShell(), "Success",
155 "Connection to '" + uri
.getText() + "' successful");
157 } catch (Exception e
) {
159 "Connection test failed for " + uri
.getText(), e
);
161 JcrUtils
.logoutQuietly(session
);
166 protected void okPressed() {
167 Session nodeSession
= null;
169 nodeSession
= nodeRepository
.login();
170 Node home
= UserJcrUtils
.getUserHome(nodeSession
);
172 Node remote
= home
.hasNode(ARGEO_REMOTE
) ? home
173 .getNode(ARGEO_REMOTE
) : home
.addNode(ARGEO_REMOTE
);
174 if (remote
.hasNode(name
.getText()))
175 throw new ArgeoException(
176 "There is already a remote repository named "
178 Node remoteRepository
= remote
.addNode(name
.getText(),
179 ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
);
180 remoteRepository
.setProperty(ARGEO_URI
, uri
.getText());
181 remoteRepository
.setProperty(ARGEO_USER_ID
, username
.getText());
183 if (saveInKeyring
.getSelection()) {
184 String pwdPath
= remoteRepository
.getPath() + '/'
186 keyring
.set(pwdPath
, password
.getText().toCharArray());
189 MessageDialog
.openInformation(
192 "Remote repository '" + username
.getText() + "@"
193 + uri
.getText() + "' added");
196 } catch (Exception e
) {
197 ErrorFeedback
.show("Cannot add remote repository", e
);
199 JcrUtils
.logoutQuietly(nodeSession
);
203 /** Creates label and text. */
204 protected Text
createLT(Composite parent
, String label
, String initial
) {
205 new Label(parent
, SWT
.NONE
).setText(label
);
206 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
);
207 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
208 text
.setText(initial
);
212 /** Creates label and check. */
213 protected Button
createLC(Composite parent
, String label
,
215 new Label(parent
, SWT
.NONE
).setText(label
);
216 Button check
= new Button(parent
, SWT
.CHECK
);
217 check
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
218 check
.setSelection(initial
);
222 protected Text
createLP(Composite parent
, String label
) {
223 new Label(parent
, SWT
.NONE
).setText(label
);
224 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
226 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));