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
.slc
.client
.ui
.dist
.wizards
;
19 import java
.util
.Hashtable
;
21 import javax
.jcr
.Node
;
22 import javax
.jcr
.Property
;
23 import javax
.jcr
.Repository
;
24 import javax
.jcr
.RepositoryFactory
;
25 import javax
.jcr
.Session
;
26 import javax
.jcr
.SimpleCredentials
;
27 import javax
.jcr
.nodetype
.NodeType
;
29 import org
.argeo
.ArgeoException
;
30 import org
.argeo
.eclipse
.ui
.ErrorFeedback
;
31 import org
.argeo
.jcr
.ArgeoJcrConstants
;
32 import org
.argeo
.jcr
.ArgeoJcrUtils
;
33 import org
.argeo
.jcr
.ArgeoNames
;
34 import org
.argeo
.jcr
.ArgeoTypes
;
35 import org
.argeo
.jcr
.JcrUtils
;
36 import org
.argeo
.jcr
.UserJcrUtils
;
37 import org
.argeo
.slc
.SlcException
;
38 import org
.argeo
.slc
.repo
.RepoConstants
;
39 import org
.argeo
.util
.security
.Keyring
;
40 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
41 import org
.eclipse
.jface
.resource
.JFaceResources
;
42 import org
.eclipse
.jface
.wizard
.Wizard
;
43 import org
.eclipse
.jface
.wizard
.WizardPage
;
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
.events
.SelectionListener
;
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
.Label
;
53 import org
.eclipse
.swt
.widgets
.Text
;
57 * Registers a new remote repository in the current Node.
60 public class RegisterRepoWizard
extends Wizard
{
63 private Keyring keyring
;
64 private RepositoryFactory repositoryFactory
;
65 private Repository nodeRepository
;
68 private DefineModelPage page
;
70 // Widgets of model page
73 private Text username
;
74 private Text password
;
75 private Button saveInKeyring
;
78 private final static String DEFAULT_NAME
= "Argeo Public Repository";
79 private final static String DEFAULT_URI
= "http://repo.argeo.org/data/pub/java";
80 private final static String DEFAULT_USER_NAME
= "anonymous";
81 private final static boolean DEFAULT_ANONYMOUS
= true;
83 public RegisterRepoWizard(Keyring keyring
,
84 RepositoryFactory repositoryFactory
, Repository nodeRepository
) {
86 this.keyring
= keyring
;
87 this.repositoryFactory
= repositoryFactory
;
88 this.nodeRepository
= nodeRepository
;
92 public void addPages() {
94 page
= new DefineModelPage();
96 setWindowTitle("Register a new remote repository");
97 } catch (Exception e
) {
98 throw new SlcException("Cannot add page to wizard ", e
);
103 public boolean performFinish() {
107 Session nodeSession
= null;
109 nodeSession
= nodeRepository
.login();
110 String reposPath
= UserJcrUtils
.getUserHome(nodeSession
).getPath()
111 + RepoConstants
.REPOSITORIES_BASE_PATH
;
113 Node repos
= nodeSession
.getNode(reposPath
);
114 String repoNodeName
= JcrUtils
.replaceInvalidChars(name
.getText());
115 if (repos
.hasNode(repoNodeName
))
116 throw new ArgeoException(
117 "There is already a remote repository named "
119 Node repoNode
= repos
.addNode(repoNodeName
,
120 ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
);
121 repoNode
.setProperty(ArgeoNames
.ARGEO_URI
, uri
.getText());
122 repoNode
.setProperty(ArgeoNames
.ARGEO_USER_ID
, username
.getText());
123 repoNode
.addMixin(NodeType
.MIX_TITLE
);
124 repoNode
.setProperty(Property
.JCR_TITLE
, name
.getText());
126 if (saveInKeyring
.getSelection()) {
127 String pwdPath
= repoNode
.getPath() + '/'
128 + ArgeoNames
.ARGEO_PASSWORD
;
129 keyring
.set(pwdPath
, password
.getText().toCharArray());
132 MessageDialog
.openInformation(getShell(), "Repository Added",
133 "Remote repository " + uri
.getText() + "' added");
134 } catch (Exception e
) {
135 ErrorFeedback
.show("Cannot add remote repository", e
);
137 JcrUtils
.logoutQuietly(nodeSession
);
142 private class DefineModelPage
extends WizardPage
{
144 public DefineModelPage() {
146 setTitle("Fill information to register a repository");
149 public void createControl(Composite parent
) {
152 Composite composite
= new Composite(parent
, SWT
.NONE
);
153 composite
.setLayout(new GridLayout(2, false));
154 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
157 // Create various fields
158 // setMessage("Login to remote repository", IMessageProvider.NONE);
159 name
= createLT(composite
, "Name", DEFAULT_NAME
);
160 uri
= createLT(composite
, "URI", DEFAULT_URI
);
162 final Button anonymousLogin
= createLC(composite
,
163 "log as anonymous", true);
164 anonymousLogin
.addSelectionListener(new SelectionListener() {
165 public void widgetSelected(SelectionEvent e
) {
166 if (anonymousLogin
.getSelection()) {
167 username
.setText(DEFAULT_USER_NAME
);
168 password
.setText("");
169 username
.setEnabled(false);
170 password
.setEnabled(false);
172 username
.setText("");
173 password
.setText("");
174 username
.setEnabled(true);
175 password
.setEnabled(true);
179 public void widgetDefaultSelected(SelectionEvent e
) {
183 username
= createLT(composite
, "User", DEFAULT_USER_NAME
);
184 password
= createLP(composite
, "Password");
185 saveInKeyring
= createLC(composite
, "Remember password", false);
187 if (DEFAULT_ANONYMOUS
) {
188 username
.setEnabled(false);
189 password
.setEnabled(false);
192 Button test
= createButton(composite
, "Test");
193 GridData gd
= new GridData(SWT
.CENTER
, SWT
.CENTER
, false, false, 2,
196 test
.setLayoutData(gd
);
198 test
.addSelectionListener(new SelectionAdapter() {
199 public void widgetSelected(SelectionEvent arg0
) {
205 setControl(composite
);
208 /** Creates label and text. */
209 protected Text
createLT(Composite parent
, String label
, String initial
) {
210 new Label(parent
, SWT
.NONE
).setText(label
);
211 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
);
212 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true, true));
213 text
.setText(initial
);
217 /** Creates label and check. */
218 protected Button
createLC(Composite parent
, String label
,
220 new Label(parent
, SWT
.NONE
).setText(label
);
221 Button check
= new Button(parent
, SWT
.CHECK
);
222 check
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
223 check
.setSelection(initial
);
227 /** Creates a button with a label. */
228 protected Button
createButton(Composite parent
, String label
) {
229 Button button
= new Button(parent
, SWT
.PUSH
);
230 button
.setText(label
);
231 button
.setFont(JFaceResources
.getDialogFont());
232 setButtonLayoutData(button
);
236 /** Creates label and password field */
237 protected Text
createLP(Composite parent
, String label
) {
238 new Label(parent
, SWT
.NONE
).setText(label
);
239 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
241 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
247 void testConnection() {
248 Session session
= null;
250 if (uri
.getText().startsWith("http")) {// http, https
251 URI checkedUri
= new URI(uri
.getText());
252 String checkedUriStr
= checkedUri
.toString();
253 Hashtable
<String
, String
> params
= new Hashtable
<String
, String
>();
254 params
.put(ArgeoJcrConstants
.JCR_REPOSITORY_URI
, checkedUriStr
);
255 Repository repository
= ArgeoJcrUtils
.getRepositoryByUri(
256 repositoryFactory
, checkedUriStr
);
257 if (username
.getText().trim().equals("")) {// anonymous
258 session
= repository
.login();
260 // FIXME use getTextChars() when upgrading to 3.7
262 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
263 char[] pwd
= password
.getText().toCharArray();
264 SimpleCredentials sc
= new SimpleCredentials(
265 username
.getText(), pwd
);
266 session
= repository
.login(sc
);
269 Repository repository
= ArgeoJcrUtils
.getRepositoryByAlias(
270 repositoryFactory
, uri
.getText());
271 session
= repository
.login();
273 MessageDialog
.openInformation(getShell(), "Success",
274 "Connection to '" + uri
.getText() + "' successful");
275 } catch (Exception e
) {
277 .show("Connection test failed for " + uri
.getText(), e
);
279 JcrUtils
.logoutQuietly(session
);