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
.jcr
.model
;
18 import java
.util
.Arrays
;
20 import javax
.jcr
.Node
;
21 import javax
.jcr
.Repository
;
22 import javax
.jcr
.RepositoryException
;
23 import javax
.jcr
.RepositoryFactory
;
24 import javax
.jcr
.Session
;
25 import javax
.jcr
.SimpleCredentials
;
27 import org
.argeo
.cms
.ArgeoNames
;
28 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
29 import org
.argeo
.eclipse
.ui
.TreeParent
;
30 import org
.argeo
.node
.NodeUtils
;
31 import org
.argeo
.node
.security
.Keyring
;
33 /** Root of a remote repository */
34 public class RemoteRepositoryElem
extends RepositoryElem
{
35 private final Keyring keyring
;
37 * A session of the logged in user on the default workspace of the node
40 private final Session userSession
;
41 private final String remoteNodePath
;
43 private final RepositoryFactory repositoryFactory
;
44 private final String uri
;
46 public RemoteRepositoryElem(String alias
, RepositoryFactory repositoryFactory
, String uri
, TreeParent parent
,
47 Session userSession
, Keyring keyring
, String remoteNodePath
) {
48 super(alias
, null, parent
);
49 this.repositoryFactory
= repositoryFactory
;
51 this.keyring
= keyring
;
52 this.userSession
= userSession
;
53 this.remoteNodePath
= remoteNodePath
;
57 protected Session
repositoryLogin(String workspaceName
) throws RepositoryException
{
58 Node remoteRepository
= userSession
.getNode(remoteNodePath
);
59 String userID
= remoteRepository
.getProperty(ArgeoNames
.ARGEO_USER_ID
).getString();
60 if (userID
.trim().equals("")) {
61 return getRepository().login(workspaceName
);
63 String pwdPath
= remoteRepository
.getPath() + '/' + ArgeoNames
.ARGEO_PASSWORD
;
64 char[] password
= keyring
.getAsChars(pwdPath
);
66 SimpleCredentials credentials
= new SimpleCredentials(userID
, password
);
67 return getRepository().login(credentials
, workspaceName
);
69 Arrays
.fill(password
, 0, password
.length
, ' ');
75 public Repository
getRepository() {
76 if (repository
== null)
77 repository
= NodeUtils
.getRepositoryByUri(repositoryFactory
, uri
);
78 return super.getRepository();
81 public void remove() {
83 Node remoteNode
= userSession
.getNode(remoteNodePath
);
85 remoteNode
.getSession().save();
86 } catch (RepositoryException e
) {
87 throw new EclipseUiException("Cannot remove " + remoteNodePath
, e
);