Improve launch config
[gpl/argeo-suite.git] / org.argeo.suite.core / src / org / argeo / suite / core / DefaultSuiteMaintenanceService.java
1 package org.argeo.suite.core;
2
3 import java.util.List;
4
5 import javax.jcr.Repository;
6 import javax.jcr.RepositoryException;
7 import javax.jcr.Session;
8 import javax.jcr.nodetype.NodeType;
9 import javax.jcr.security.Privilege;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.argeo.connect.AppMaintenanceService;
14 import org.argeo.connect.SystemMaintenanceService;
15 import org.argeo.jcr.JcrUtils;
16 import org.argeo.node.NodeConstants;
17 import org.argeo.suite.ArgeoSuiteRole;
18 import org.argeo.suite.SuiteException;
19
20 /** Make the DJay-ing to provide a full running Suite platform */
21 public class DefaultSuiteMaintenanceService implements SystemMaintenanceService {
22 private final static Log log = LogFactory.getLog(DefaultSuiteMaintenanceService.class);
23
24 /* DEPENDENCY INJECTION */
25 private Repository repository;
26 private String workspaceName;
27 private List<AppMaintenanceService> maintenanceServices;
28
29 public void init() {
30 Session adminSession = null;
31 try {
32 adminSession = repository.login(workspaceName);
33 if (prepareJcrTree(adminSession)) {
34 configurePrivileges(adminSession);
35 }
36 } catch (Exception e) {
37 throw new SuiteException("Cannot initialise model", e);
38 } finally {
39 JcrUtils.logoutQuietly(adminSession);
40 }
41 }
42
43 // To be cleaned once first init and config mechanisms have been implemented
44 // private final static String publicPath = "/public";
45 // FIXME Users must have read access on the jcr:system/jcr:versionStorage
46 // node under JackRabbit to be able to manage versions
47 private final static String jackRabbitVersionSystemPath = "/jcr:system";
48
49 @Override
50 public boolean prepareJcrTree(Session session) {
51 boolean hasCHanged = false;
52 try {
53 // JcrUtils.mkdirs(session, publicPath, NodeType.NT_UNSTRUCTURED);
54 if (session.hasPendingChanges()) {
55 session.save();
56 hasCHanged = true;
57 }
58 } catch (RepositoryException e) {
59 throw new SuiteException("Cannot build model", e);
60 }
61 for (AppMaintenanceService service : maintenanceServices)
62 hasCHanged |= service.prepareJcrTree(session);
63 if (hasCHanged)
64 log.info("Repository has been initialised with Argeo Suite model");
65 return hasCHanged;
66 }
67
68 @Override
69 public void configurePrivileges(Session session) {
70 try {
71 // Remove unused default JCR rights
72 JcrUtils.clearAccessControList(session, "/", "everyone");
73
74 JcrUtils.addPrivilege(session, jackRabbitVersionSystemPath, ArgeoSuiteRole.coworker.dn(),
75 Privilege.JCR_READ);
76 // Default configuration of the workspace
77 JcrUtils.addPrivilege(session, "/", NodeConstants.ROLE_ADMIN, Privilege.JCR_ALL);
78 // JcrUtils.addPrivilege(session, publicPath, NodeConstants.ROLE_USER, Privilege.JCR_READ);
79 // JcrUtils.addPrivilege(session, publicPath, "anonymous", Privilege.JCR_READ);
80 // JcrUtils.addPrivilege(session, publicPath, NodeConstants.ROLE_ANONYMOUS, Privilege.JCR_READ);
81
82 session.save();
83 } catch (RepositoryException e) {
84 throw new SuiteException("Cannot build model", e);
85 }
86 for (AppMaintenanceService service : maintenanceServices)
87 service.configurePrivileges(session);
88 log.info("Access control configured");
89 }
90
91 public void destroy() {
92 }
93
94 /* DEPENDENCY INJECTION */
95 public void setRepository(Repository repository) {
96 this.repository = repository;
97 }
98
99 public void setWorkspaceName(String workspaceName) {
100 this.workspaceName = workspaceName;
101 }
102
103 public void setMaintenanceServices(List<AppMaintenanceService> maintenanceServices) {
104 this.maintenanceServices = maintenanceServices;
105 }
106 }