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