2 * Copyright (C) 2007-2012 Mathieu Baudier
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
.model
;
18 import java
.util
.Hashtable
;
21 import javax
.jcr
.Node
;
22 import javax
.jcr
.NodeIterator
;
23 import javax
.jcr
.Repository
;
24 import javax
.jcr
.RepositoryException
;
25 import javax
.jcr
.RepositoryFactory
;
26 import javax
.jcr
.Session
;
28 import org
.argeo
.ArgeoException
;
29 import org
.argeo
.eclipse
.ui
.ErrorFeedback
;
30 import org
.argeo
.eclipse
.ui
.TreeParent
;
31 import org
.argeo
.jcr
.ArgeoJcrConstants
;
32 import org
.argeo
.jcr
.ArgeoNames
;
33 import org
.argeo
.jcr
.RepositoryRegister
;
34 import org
.argeo
.jcr
.UserJcrUtils
;
35 import org
.argeo
.util
.security
.Keyring
;
38 * UI Tree component. Implements the Argeo abstraction of a
39 * {@link RepositoryFactory} that enable a user to "mount" various repositories
40 * in a single Tree like View. It is usually meant to be at the root of the UI
41 * Tree and thus {@link getParent()} method will return null.
43 * The {@link RepositoryFactory} is injected at instantiation time and must be
44 * use get or register new {@link Repository} objects upon which a reference is
48 public class RepositoriesNode
extends TreeParent
implements ArgeoNames
{
49 private final RepositoryRegister repositoryRegister
;
50 private final RepositoryFactory repositoryFactory
;
53 * A session of the logged in user on the default workspace of the node
56 private final Session userSession
;
57 private final Keyring keyring
;
59 public RepositoriesNode(String name
, RepositoryRegister repositoryRegister
,
60 RepositoryFactory repositoryFactory
, TreeParent parent
,
61 Session userSession
, Keyring keyring
) {
63 this.repositoryRegister
= repositoryRegister
;
64 this.repositoryFactory
= repositoryFactory
;
65 this.userSession
= userSession
;
66 this.keyring
= keyring
;
70 * Override normal behavior to initialize the various repositories only at
74 public synchronized Object
[] getChildren() {
76 return super.getChildren();
78 // initialize current object
79 Map
<String
, Repository
> refRepos
= repositoryRegister
81 for (String name
: refRepos
.keySet()) {
82 super.addChild(new RepositoryNode(name
, refRepos
.get(name
),
87 if (keyring
!= null) {
89 addRemoteRepositories(keyring
);
90 } catch (RepositoryException e
) {
91 throw new ArgeoException(
92 "Cannot browse remote repositories", e
);
95 return super.getChildren();
99 protected void addRemoteRepositories(Keyring jcrKeyring
)
100 throws RepositoryException
{
101 Node userHome
= UserJcrUtils
.getUserHome(userSession
);
102 if (userHome
!= null && userHome
.hasNode(ARGEO_REMOTE
)) {
103 NodeIterator it
= userHome
.getNode(ARGEO_REMOTE
).getNodes();
104 while (it
.hasNext()) {
105 Node remoteNode
= it
.nextNode();
106 String uri
= remoteNode
.getProperty(ARGEO_URI
).getString();
108 Hashtable
<String
, String
> params
= new Hashtable
<String
, String
>();
109 params
.put(ArgeoJcrConstants
.JCR_REPOSITORY_URI
, uri
);
110 params
.put(ArgeoJcrConstants
.JCR_REPOSITORY_ALIAS
,
111 remoteNode
.getName());
112 Repository repository
= repositoryFactory
113 .getRepository(params
);
114 RemoteRepositoryNode remoteRepositoryNode
= new RemoteRepositoryNode(
115 remoteNode
.getName(), repository
, this,
116 userSession
, jcrKeyring
, remoteNode
.getPath());
117 super.addChild(remoteRepositoryNode
);
118 } catch (Exception e
) {
119 ErrorFeedback
.show("Cannot add remote repository "
126 public void registerNewRepository(String alias
, Repository repository
) {
127 // TODO: implement this
128 // Create a new RepositoryNode Object
130 // super.addChild(new RepositoriesNode(...));
133 /** Returns the {@link RepositoryRegister} wrapped by this object. */
134 public RepositoryRegister
getRepositoryRegister() {
135 return repositoryRegister
;