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
.ArgeoJcrUtils
;
33 import org
.argeo
.jcr
.ArgeoNames
;
34 import org
.argeo
.jcr
.RepositoryRegister
;
35 import org
.argeo
.jcr
.security
.JcrKeyring
;
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
;
52 private final JcrKeyring jcrKeyring
;
54 public RepositoriesNode(String name
, RepositoryRegister repositoryRegister
,
55 RepositoryFactory repositoryFactory
, TreeParent parent
,
56 JcrKeyring jcrKeyring
) {
58 this.repositoryRegister
= repositoryRegister
;
59 this.repositoryFactory
= repositoryFactory
;
60 this.jcrKeyring
= jcrKeyring
;
64 * Override normal behavior to initialize the various repositories only at
68 public synchronized Object
[] getChildren() {
70 return super.getChildren();
72 // initialize current object
73 Map
<String
, Repository
> refRepos
= repositoryRegister
75 for (String name
: refRepos
.keySet()) {
76 super.addChild(new RepositoryNode(name
, refRepos
.get(name
),
81 if (jcrKeyring
!= null) {
83 addRemoteRepositories(jcrKeyring
);
84 } catch (RepositoryException e
) {
85 throw new ArgeoException(
86 "Cannot browse remote repositories", e
);
89 return super.getChildren();
93 protected void addRemoteRepositories(JcrKeyring jcrKeyring
)
94 throws RepositoryException
{
95 Session userSession
= jcrKeyring
.getSession();
96 Node userHome
= ArgeoJcrUtils
.getUserHome(userSession
);
97 if (userHome
!= null && userHome
.hasNode(ARGEO_REMOTE
)) {
98 NodeIterator it
= userHome
.getNode(ARGEO_REMOTE
).getNodes();
99 while (it
.hasNext()) {
100 Node remoteNode
= it
.nextNode();
101 String uri
= remoteNode
.getProperty(ARGEO_URI
).getString();
103 Hashtable
<String
, String
> params
= new Hashtable
<String
, String
>();
104 params
.put(ArgeoJcrConstants
.JCR_REPOSITORY_URI
, uri
);
105 params
.put(ArgeoJcrConstants
.JCR_REPOSITORY_ALIAS
,
106 remoteNode
.getName());
107 Repository repository
= repositoryFactory
108 .getRepository(params
);
109 RemoteRepositoryNode remoteRepositoryNode
= new RemoteRepositoryNode(
110 remoteNode
.getName(), repository
, this, jcrKeyring
,
111 remoteNode
.getPath());
112 super.addChild(remoteRepositoryNode
);
113 } catch (Exception e
) {
114 ErrorFeedback
.show("Cannot add remote repository "
121 public void registerNewRepository(String alias
, Repository repository
) {
122 // TODO: implement this
123 // Create a new RepositoryNode Object
125 // super.addChild(new RepositoriesNode(...));
128 /** Returns the {@link RepositoryRegister} wrapped by this object. */
129 public RepositoryRegister
getRepositoryRegister() {
130 return repositoryRegister
;