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
.commands
;
18 import javax
.jcr
.Repository
;
19 import javax
.jcr
.RepositoryFactory
;
21 import org
.argeo
.jcr
.ArgeoNames
;
22 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
23 import org
.argeo
.slc
.client
.ui
.dist
.utils
.CommandHelpers
;
24 import org
.argeo
.slc
.client
.ui
.dist
.wizards
.RegisterRepoWizard
;
25 import org
.argeo
.slc
.jcr
.SlcNames
;
26 import org
.argeo
.util
.security
.Keyring
;
27 import org
.eclipse
.core
.commands
.AbstractHandler
;
28 import org
.eclipse
.core
.commands
.ExecutionEvent
;
29 import org
.eclipse
.core
.commands
.ExecutionException
;
30 import org
.eclipse
.jface
.dialogs
.Dialog
;
31 import org
.eclipse
.jface
.wizard
.WizardDialog
;
32 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
35 * Connect to a remote repository.
37 public class RegisterRepository
extends AbstractHandler
implements ArgeoNames
,
40 public final static String ID
= DistPlugin
.ID
+ ".registerRepository";
41 public final static String DEFAULT_LABEL
= "Register a repository...";
42 public final static String DEFAULT_ICON_PATH
= "icons/addRepo.gif";
44 /* DEPENDENCY INJECTION */
45 private RepositoryFactory repositoryFactory
;
46 private Repository nodeRepository
;
47 private Keyring keyring
;
49 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
50 RegisterRepoWizard wizard
= new RegisterRepoWizard(keyring
,
51 repositoryFactory
, nodeRepository
);
52 WizardDialog dialog
= new WizardDialog(
53 HandlerUtil
.getActiveShell(event
), wizard
);
54 int result
= dialog
.open();
55 if (result
== Dialog
.OK
)
56 CommandHelpers
.callCommand(RefreshDistributionsView
.ID
);
58 // RemoteRepositoryLoginDialog dlg = new RemoteRepositoryLoginDialog(
59 // Display.getDefault().getActiveShell());
60 // if (dlg.open() == Dialog.OK) {
65 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
66 this.repositoryFactory
= repositoryFactory
;
69 public void setKeyring(Keyring keyring
) {
70 this.keyring
= keyring
;
73 public void setNodeRepository(Repository nodeRepository
) {
74 this.nodeRepository
= nodeRepository
;
77 // class RemoteRepositoryLoginDialog extends TitleAreaDialog {
80 // private Text username;
81 // private Text password;
82 // private Button saveInKeyring;
84 // public RemoteRepositoryLoginDialog(Shell parentShell) {
85 // super(parentShell);
88 // protected Point getInitialSize() {
89 // return new Point(600, 400);
92 // protected Control createDialogArea(Composite parent) {
93 // Composite dialogarea = (Composite) super.createDialogArea(parent);
94 // dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
96 // Composite composite = new Composite(dialogarea, SWT.NONE);
97 // composite.setLayout(new GridLayout(2, false));
98 // composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
100 // setMessage("Login to remote repository", IMessageProvider.NONE);
101 // name = createLT(composite, "Name", "Example Java Repository");
102 // uri = createLT(composite, "URI",
103 // "https://example.com/data/jcr/java");
104 // username = createLT(composite, "User", "");
105 // password = createLP(composite, "Password");
107 // saveInKeyring = createLC(composite, "Remember password", false);
113 // protected void createButtonsForButtonBar(Composite parent) {
114 // super.createButtonsForButtonBar(parent);
115 // Button test = createButton(parent, 2, "Test", false);
116 // test.addSelectionListener(new SelectionAdapter() {
117 // public void widgetSelected(SelectionEvent arg0) {
123 // void testConnection() {
124 // Session session = null;
126 // if (uri.getText().startsWith("http")) {// http, https
127 // URI checkedUri = new URI(uri.getText());
128 // String checkedUriStr = checkedUri.toString();
129 // Hashtable<String, String> params = new Hashtable<String, String>();
130 // params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI,
132 // Repository repository = ArgeoJcrUtils.getRepositoryByUri(
133 // repositoryFactory, checkedUriStr);
134 // if (username.getText().trim().equals("")) {// anonymous
135 // session = repository.login();
137 // // FIXME use getTextChars() when upgrading to 3.7
139 // // https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
140 // char[] pwd = password.getText().toCharArray();
141 // SimpleCredentials sc = new SimpleCredentials(
142 // username.getText(), pwd);
143 // session = repository.login(sc);
146 // Repository repository = ArgeoJcrUtils.getRepositoryByAlias(
147 // repositoryFactory, uri.getText());
148 // session = repository.login();
150 // MessageDialog.openInformation(getParentShell(), "Success",
151 // "Connection to '" + uri.getText() + "' successful");
152 // } catch (Exception e) {
153 // ErrorFeedback.show(
154 // "Connection test failed for " + uri.getText(), e);
156 // JcrUtils.logoutQuietly(session);
161 // protected void okPressed() {
162 // Session nodeSession = null;
164 // nodeSession = nodeRepository.login();
165 // String reposPath = UserJcrUtils.getUserHome(nodeSession)
166 // .getPath() + RepoConstants.REPOSITORIES_BASE_PATH;
168 // Node repos = nodeSession.getNode(reposPath);
169 // String repoNodeName = JcrUtils.replaceInvalidChars(name
171 // if (repos.hasNode(repoNodeName))
172 // throw new ArgeoException(
173 // "There is already a remote repository named "
175 // Node repoNode = repos.addNode(repoNodeName,
176 // ArgeoTypes.ARGEO_REMOTE_REPOSITORY);
177 // repoNode.setProperty(ARGEO_URI, uri.getText());
178 // repoNode.setProperty(ARGEO_USER_ID, username.getText());
179 // repoNode.addMixin(NodeType.MIX_TITLE);
180 // repoNode.setProperty(Property.JCR_TITLE, name.getText());
181 // nodeSession.save();
182 // if (saveInKeyring.getSelection()) {
183 // String pwdPath = repoNode.getPath() + '/' + ARGEO_PASSWORD;
184 // keyring.set(pwdPath, password.getText().toCharArray());
185 // nodeSession.save();
187 // MessageDialog.openInformation(getParentShell(),
188 // "Repository Added",
189 // "Remote repository " + uri.getText() + "' added");
191 // super.okPressed();
192 // } catch (Exception e) {
193 // ErrorFeedback.show("Cannot add remote repository", e);
195 // JcrUtils.logoutQuietly(nodeSession);
199 // /** Creates label and text. */
200 // protected Text createLT(Composite parent, String label, String initial) {
201 // new Label(parent, SWT.NONE).setText(label);
202 // Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
203 // text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
204 // text.setText(initial);
208 // /** Creates label and check. */
209 // protected Button createLC(Composite parent, String label,
210 // Boolean initial) {
211 // new Label(parent, SWT.NONE).setText(label);
212 // Button check = new Button(parent, SWT.CHECK);
213 // check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
214 // check.setSelection(initial);
218 // protected Text createLP(Composite parent, String label) {
219 // new Label(parent, SWT.NONE).setText(label);
220 // Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
222 // text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));