]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/kernel/FirstInitProperties.java
Don't use context user to generate path.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / FirstInitProperties.java
1 package org.argeo.cms.internal.kernel;
2
3 import static org.argeo.cms.internal.kernel.KernelUtils.getFrameworkProp;
4
5 import java.io.File;
6 import java.io.FileFilter;
7 import java.io.IOException;
8 import java.net.URI;
9 import java.util.ArrayList;
10 import java.util.Dictionary;
11 import java.util.Hashtable;
12 import java.util.List;
13
14 import org.apache.commons.io.FileUtils;
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.argeo.cms.CmsException;
18 import org.argeo.cms.auth.AuthConstants;
19 import org.argeo.jcr.ArgeoJcrConstants;
20 import org.argeo.node.NodeConstants;
21 import org.argeo.node.RepoConf;
22 import org.argeo.osgi.useradmin.UserAdminConf;
23 import org.eclipse.equinox.http.jetty.JettyConstants;
24
25 /**
26 * Interprets framework properties in order to generate the initial deploy
27 * configuration.
28 */
29 class FirstInitProperties {
30 private final static Log log = LogFactory.getLog(FirstInitProperties.class);
31
32 public FirstInitProperties() {
33 log.info("## FIRST INIT ##");
34 }
35
36 /** Override the provided config with the framework properties */
37 Dictionary<String, Object> getNodeRepositoryConfig(Dictionary<String, Object> provided) {
38 Dictionary<String, Object> props = provided != null ? provided : new Hashtable<String, Object>();
39 for (RepoConf repoConf : RepoConf.values()) {
40 Object value = getFrameworkProp(NodeConstants.NODE_REPO_PROP_PREFIX + repoConf.name());
41 if (value != null)
42 props.put(repoConf.name(), value);
43 }
44 props.put(NodeConstants.CN, ArgeoJcrConstants.ALIAS_NODE);
45 props.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS, ArgeoJcrConstants.ALIAS_NODE);
46 return props;
47 }
48
49 /** Override the provided config with the framework properties */
50 Dictionary<String, Object> getHttpServerConfig(Dictionary<String, Object> provided) {
51 String httpPort = getFrameworkProp("org.osgi.service.http.port");
52 String httpsPort = getFrameworkProp("org.osgi.service.http.port.secure");
53 /// TODO make it more generic
54 String httpHost = getFrameworkProp("org.eclipse.equinox.http.jetty.http.host");
55
56 final Hashtable<String, Object> props = new Hashtable<String, Object>();
57 // try {
58 if (httpPort != null || httpsPort != null) {
59 if (httpPort != null) {
60 props.put(JettyConstants.HTTP_PORT, httpPort);
61 props.put(JettyConstants.HTTP_ENABLED, true);
62 }
63 if (httpsPort != null) {
64 props.put(JettyConstants.HTTPS_PORT, httpsPort);
65 props.put(JettyConstants.HTTPS_ENABLED, true);
66 props.put(JettyConstants.SSL_KEYSTORETYPE, "PKCS12");
67 // jettyProps.put(JettyConstants.SSL_KEYSTORE,
68 // nodeSecurity.getHttpServerKeyStore().getCanonicalPath());
69 props.put(JettyConstants.SSL_PASSWORD, "changeit");
70 props.put(JettyConstants.SSL_WANTCLIENTAUTH, true);
71 }
72 if (httpHost != null) {
73 props.put(JettyConstants.HTTP_HOST, httpHost);
74 }
75 props.put(NodeConstants.CN, NodeConstants.DEFAULT);
76 }
77 return props;
78 }
79
80 List<Dictionary<String, Object>> getUserDirectoryConfigs() {
81 List<Dictionary<String, Object>> res = new ArrayList<>();
82 File nodeBaseDir = KernelUtils.getOsgiInstancePath(KernelConstants.DIR_NODE).toFile();
83 List<String> uris = new ArrayList<>();
84
85 // node roles
86 String nodeRolesUri = getFrameworkProp(NodeConstants.ROLES_URI);
87 String baseNodeRoleDn = AuthConstants.ROLES_BASEDN;
88 if (nodeRolesUri == null) {
89 File nodeRolesFile = new File(nodeBaseDir, baseNodeRoleDn + ".ldif");
90 if (!nodeRolesFile.exists())
91 try {
92 FileUtils.copyInputStreamToFile(getClass().getResourceAsStream(baseNodeRoleDn + ".ldif"),
93 nodeRolesFile);
94 } catch (IOException e) {
95 throw new CmsException("Cannot copy demo resource", e);
96 }
97 nodeRolesUri = nodeRolesFile.toURI().toString();
98 }
99 uris.add(nodeRolesUri);
100
101 // Business roles
102 String userAdminUris = getFrameworkProp(NodeConstants.USERADMIN_URIS);
103 if (userAdminUris == null) {
104 String demoBaseDn = "dc=example,dc=com";
105 File businessRolesFile = new File(nodeBaseDir, demoBaseDn + ".ldif");
106 if (!businessRolesFile.exists())
107 try {
108 FileUtils.copyInputStreamToFile(getClass().getResourceAsStream(demoBaseDn + ".ldif"),
109 businessRolesFile);
110 } catch (IOException e) {
111 throw new CmsException("Cannot copy demo resource", e);
112 }
113 userAdminUris = businessRolesFile.toURI().toString();
114 }
115 for (String userAdminUri : userAdminUris.split(" "))
116 uris.add(userAdminUri);
117
118 // Interprets URIs
119 for (String uri : uris) {
120 URI u;
121 try {
122 u = new URI(uri);
123 if (u.getPath() == null)
124 throw new CmsException("URI " + uri + " must have a path in order to determine base DN");
125 if (u.getScheme() == null) {
126 if (uri.startsWith("/") || uri.startsWith("./") || uri.startsWith("../"))
127 u = new File(uri).getCanonicalFile().toURI();
128 else if (!uri.contains("/")) {
129 u = KernelUtils.getOsgiInstanceUri(KernelConstants.DIR_NODE + '/' + uri);
130 // u = new URI(nodeBaseDir.toURI() + uri);
131 } else
132 throw new CmsException("Cannot interpret " + uri + " as an uri");
133 } else if (u.getScheme().equals("file")) {
134 u = new File(u).getCanonicalFile().toURI();
135 }
136 } catch (Exception e) {
137 throw new CmsException("Cannot interpret " + uri + " as an uri", e);
138 }
139 Dictionary<String, Object> properties = UserAdminConf.uriAsProperties(u.toString());
140 res.add(properties);
141 }
142
143 return res;
144 }
145
146 /**
147 * Called before node initialisation, in order populate OSGi instance are
148 * with some files (typically LDIF, etc).
149 */
150 void prepareInstanceArea() {
151 String nodeInit = getFrameworkProp(NodeConstants.NODE_INIT);
152 if (nodeInit == null)
153 nodeInit = "../../init";
154 if (nodeInit.startsWith("http")) {
155 // remoteFirstInit(nodeInit);
156 return;
157 }
158
159 // TODO use java.nio.file
160 File initDir;
161 if (nodeInit.startsWith("."))
162 initDir = KernelUtils.getExecutionDir(nodeInit);
163 else
164 initDir = new File(nodeInit);
165 // TODO also uncompress archives
166 if (initDir.exists())
167 try {
168 FileUtils.copyDirectory(initDir, KernelUtils.getOsgiInstanceDir(), new FileFilter() {
169
170 @Override
171 public boolean accept(File pathname) {
172 if (pathname.getName().equals(".svn") || pathname.getName().equals(".git"))
173 return false;
174 return true;
175 }
176 });
177 log.info("CMS initialized from " + initDir.getCanonicalPath());
178 } catch (IOException e) {
179 throw new CmsException("Cannot initialize from " + initDir, e);
180 }
181 }
182
183 }