1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.cms.ui.jcr.model;
17
18 import javax.jcr.Repository;
19 import javax.jcr.RepositoryException;
20 import javax.jcr.Session;
21
22 import org.argeo.eclipse.ui.EclipseUiException;
23 import org.argeo.eclipse.ui.TreeParent;
24 import org.argeo.jcr.JcrUtils;
25
26
27
28
29
30
31
32
33 public class RepositoryElem extends TreeParent {
34 private String alias;
35 protected Repository repository;
36 private Session defaultSession = null;
37
38
39 public RepositoryElem(String alias, Repository repository, TreeParent parent) {
40 super(alias);
41 this.repository = repository;
42 setParent(parent);
43 this.alias = alias;
44 }
45
46 public void login() {
47 try {
48 defaultSession = repositoryLogin("main");
49 String[] wkpNames = defaultSession.getWorkspace().getAccessibleWorkspaceNames();
50 for (String wkpName : wkpNames) {
51 if (wkpName.equals(defaultSession.getWorkspace().getName()))
52 addChild(new WorkspaceElem(this, wkpName, defaultSession));
53 else
54 addChild(new WorkspaceElem(this, wkpName));
55 }
56 } catch (RepositoryException e) {
57 throw new EclipseUiException("Cannot connect to repository " + alias, e);
58 }
59 }
60
61 public synchronized void logout() {
62 for (Object child : getChildren()) {
63 if (child instanceof WorkspaceElem)
64 ((WorkspaceElem) child).logout();
65 }
66 clearChildren();
67 JcrUtils.logoutQuietly(defaultSession);
68 defaultSession = null;
69 }
70
71
72
73
74
75 protected Session repositoryLogin(String workspaceName) throws RepositoryException {
76 return repository.login(workspaceName);
77 }
78
79 public String[] getAccessibleWorkspaceNames() {
80 try {
81 return defaultSession.getWorkspace().getAccessibleWorkspaceNames();
82 } catch (RepositoryException e) {
83 throw new EclipseUiException("Cannot retrieve workspace names", e);
84 }
85 }
86
87 public void createWorkspace(String workspaceName) {
88 if (!isConnected())
89 login();
90 try {
91 defaultSession.getWorkspace().createWorkspace(workspaceName);
92 } catch (RepositoryException e) {
93 throw new EclipseUiException("Cannot create workspace", e);
94 }
95 }
96
97
98 public Repository getRepository() {
99 return repository;
100 }
101
102 public String getAlias() {
103 return alias;
104 }
105
106 public Boolean isConnected() {
107 if (defaultSession != null && defaultSession.isLive())
108 return true;
109 else
110 return false;
111 }
112 }