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
.NodeIterator
;
23 import javax
.jcr
.Property
;
24 import javax
.jcr
.Repository
;
25 import javax
.jcr
.RepositoryFactory
;
26 import javax
.jcr
.Session
;
27 import javax
.jcr
.SimpleCredentials
;
28 import javax
.jcr
.nodetype
.NodeType
;
30 import org
.argeo
.eclipse
.ui
.dialogs
.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
, RepositoryFactory repositoryFactory
, Repository nodeRepository
) {
85 this.keyring
= keyring
;
86 this.repositoryFactory
= repositoryFactory
;
87 this.nodeRepository
= nodeRepository
;
91 public void addPages() {
93 page
= new DefineModelPage();
95 setWindowTitle("Register a new remote repository");
96 } catch (Exception e
) {
97 throw new SlcException("Cannot add page to wizard ", e
);
102 public boolean performFinish() {
106 Session nodeSession
= null;
108 nodeSession
= nodeRepository
.login();
109 String reposPath
= UserJcrUtils
.getUserHome(nodeSession
).getPath() + RepoConstants
.REPOSITORIES_BASE_PATH
;
111 Node repos
= nodeSession
.getNode(reposPath
);
112 String repoNodeName
= JcrUtils
.replaceInvalidChars(name
.getText());
113 if (repos
.hasNode(repoNodeName
))
114 throw new SlcException("There is already a remote repository named " + repoNodeName
);
116 // check if the same URI has already been registered
117 NodeIterator ni
= repos
.getNodes();
118 while (ni
.hasNext()) {
119 Node node
= ni
.nextNode();
120 if (node
.isNodeType(ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
) && node
.hasProperty(ArgeoNames
.ARGEO_URI
)
121 && node
.getProperty(ArgeoNames
.ARGEO_URI
).getString().equals(uri
.getText()))
122 throw new SlcException("This URI " + uri
.getText() + " is already registered, "
123 + "for the time being, only one instance of a single "
124 + "repository at a time is implemented.");
127 Node repoNode
= repos
.addNode(repoNodeName
, ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
);
128 repoNode
.setProperty(ArgeoNames
.ARGEO_URI
, uri
.getText());
129 repoNode
.setProperty(ArgeoNames
.ARGEO_USER_ID
, username
.getText());
130 repoNode
.addMixin(NodeType
.MIX_TITLE
);
131 repoNode
.setProperty(Property
.JCR_TITLE
, name
.getText());
133 if (saveInKeyring
.getSelection()) {
134 String pwdPath
= repoNode
.getPath() + '/' + ArgeoNames
.ARGEO_PASSWORD
;
135 keyring
.set(pwdPath
, password
.getText().toCharArray());
138 MessageDialog
.openInformation(getShell(), "Repository Added",
139 "Remote repository " + uri
.getText() + "' added");
140 } catch (Exception e
) {
141 ErrorFeedback
.show("Cannot add remote repository", e
);
143 JcrUtils
.logoutQuietly(nodeSession
);
148 private class DefineModelPage
extends WizardPage
{
149 private static final long serialVersionUID
= 874386824101995060L;
151 public DefineModelPage() {
153 setTitle("Fill information to register a repository");
156 public void createControl(Composite parent
) {
159 Composite composite
= new Composite(parent
, SWT
.NONE
);
160 composite
.setLayout(new GridLayout(2, false));
161 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
163 // Create various fields
164 // setMessage("Login to remote repository", IMessageProvider.NONE);
165 name
= createLT(composite
, "Name", DEFAULT_NAME
);
166 uri
= createLT(composite
, "URI", DEFAULT_URI
);
168 final Button anonymousLogin
= createLC(composite
, "Log as anonymous", true);
169 anonymousLogin
.addSelectionListener(new SelectionListener() {
170 private static final long serialVersionUID
= 4874716406036981039L;
172 public void widgetSelected(SelectionEvent e
) {
173 if (anonymousLogin
.getSelection()) {
174 username
.setText(DEFAULT_USER_NAME
);
175 password
.setText("");
176 username
.setEnabled(false);
177 password
.setEnabled(false);
179 username
.setText("");
180 password
.setText("");
181 username
.setEnabled(true);
182 password
.setEnabled(true);
186 public void widgetDefaultSelected(SelectionEvent e
) {
190 username
= createLT(composite
, "User", DEFAULT_USER_NAME
);
191 password
= createLP(composite
, "Password");
192 saveInKeyring
= createLC(composite
, "Remember password", false);
194 if (DEFAULT_ANONYMOUS
) {
195 username
.setEnabled(false);
196 password
.setEnabled(false);
199 Button test
= createButton(composite
, "Test");
200 GridData gd
= new GridData(SWT
.CENTER
, SWT
.CENTER
, false, false, 2, 1);
202 test
.setLayoutData(gd
);
204 test
.addSelectionListener(new SelectionAdapter() {
205 private static final long serialVersionUID
= -4034851916548656293L;
207 public void widgetSelected(SelectionEvent arg0
) {
213 setControl(composite
);
216 /** Creates label and text. */
217 protected Text
createLT(Composite parent
, String label
, String initial
) {
218 new Label(parent
, SWT
.RIGHT
).setText(label
);
219 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
);
220 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true, true));
221 text
.setText(initial
);
225 /** Creates label and check. */
226 protected Button
createLC(Composite parent
, String label
, Boolean initial
) {
227 new Label(parent
, SWT
.RIGHT
).setText(label
);
228 Button check
= new Button(parent
, SWT
.CHECK
);
229 check
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
230 check
.setSelection(initial
);
234 /** Creates a button with a label. */
235 protected Button
createButton(Composite parent
, String label
) {
236 Button button
= new Button(parent
, SWT
.PUSH
);
237 button
.setText(label
);
238 button
.setFont(JFaceResources
.getDialogFont());
239 setButtonLayoutData(button
);
243 /** Creates label and password field */
244 protected Text
createLP(Composite parent
, String label
) {
245 new Label(parent
, SWT
.NONE
).setText(label
);
246 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
| SWT
.PASSWORD
);
247 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
253 void testConnection() {
254 Session session
= null;
256 if (uri
.getText().startsWith("http")) {// http, https
257 URI checkedUri
= new URI(uri
.getText());
258 String checkedUriStr
= checkedUri
.toString();
259 Hashtable
<String
, String
> params
= new Hashtable
<String
, String
>();
260 params
.put(ArgeoJcrConstants
.JCR_REPOSITORY_URI
, checkedUriStr
);
261 Repository repository
= ArgeoJcrUtils
.getRepositoryByUri(repositoryFactory
, checkedUriStr
);
262 // FIXME make it more generic
263 String defaultWorkspace
= "main";
264 if (username
.getText().trim().equals("")) {// anonymous
265 session
= repository
.login(defaultWorkspace
);
267 char[] pwd
= password
.getTextChars();
268 SimpleCredentials sc
= new SimpleCredentials(username
.getText(), pwd
);
269 session
= repository
.login(sc
, defaultWorkspace
);
272 Repository repository
= ArgeoJcrUtils
.getRepositoryByAlias(repositoryFactory
, uri
.getText());
273 session
= repository
.login();
275 MessageDialog
.openInformation(getShell(), "Success", "Connection to '" + uri
.getText() + "' successful");
276 } catch (Exception e
) {
277 ErrorFeedback
.show("Connection test failed for " + uri
.getText(), e
);
279 JcrUtils
.logoutQuietly(session
);