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