]> git.argeo.org Git - lgpl/argeo-commons.git/blob - internal/kernel/FirstInit.java
Prepare next development cycle
[lgpl/argeo-commons.git] / internal / kernel / FirstInit.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 FirstInit {
27 private final static Log log = LogFactory.getLog(FirstInit.class);
28
29 public FirstInit() {
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 props.put(JettyConstants.SSL_KEYSTORE, "../../ssl/server.p12");
65 // jettyProps.put(JettyConstants.SSL_KEYSTORE,
66 // nodeSecurity.getHttpServerKeyStore().getCanonicalPath());
67 props.put(JettyConstants.SSL_PASSWORD, "changeit");
68 props.put(JettyConstants.SSL_WANTCLIENTAUTH, true);
69 }
70 if (httpHost != null) {
71 props.put(JettyConstants.HTTP_HOST, httpHost);
72 }
73 props.put(NodeConstants.CN, NodeConstants.DEFAULT);
74 }
75 return props;
76 }
77
78 List<Dictionary<String, Object>> getUserDirectoryConfigs() {
79 List<Dictionary<String, Object>> res = new ArrayList<>();
80 File nodeBaseDir = KernelUtils.getOsgiInstancePath(KernelConstants.DIR_NODE).toFile();
81 List<String> uris = new ArrayList<>();
82
83 // node roles
84 String nodeRolesUri = getFrameworkProp(NodeConstants.ROLES_URI);
85 String baseNodeRoleDn = NodeConstants.ROLES_BASEDN;
86 if (nodeRolesUri == null) {
87 File nodeRolesFile = new File(nodeBaseDir, baseNodeRoleDn + ".ldif");
88 if (!nodeRolesFile.exists())
89 try {
90 FileUtils.copyInputStreamToFile(getClass().getResourceAsStream(baseNodeRoleDn + ".ldif"),
91 nodeRolesFile);
92 } catch (IOException e) {
93 throw new CmsException("Cannot copy demo resource", e);
94 }
95 nodeRolesUri = nodeRolesFile.toURI().toString();
96 }
97 uris.add(nodeRolesUri);
98
99 // Business roles
100 String userAdminUris = getFrameworkProp(NodeConstants.USERADMIN_URIS);
101 if (userAdminUris == null) {
102 String kerberosDomain = Activator.getCmsSecurity().getKerberosDomain();
103 if (kerberosDomain != null) {
104 userAdminUris = "ipa:///" + kerberosDomain;
105 } else {
106 String demoBaseDn = "dc=example,dc=com";
107 File businessRolesFile = new File(nodeBaseDir, demoBaseDn + ".ldif");
108 if (!businessRolesFile.exists())
109 try {
110 FileUtils.copyInputStreamToFile(getClass().getResourceAsStream(demoBaseDn + ".ldif"),
111 businessRolesFile);
112 } catch (IOException e) {
113 throw new CmsException("Cannot copy demo resource", e);
114 }
115 userAdminUris = businessRolesFile.toURI().toString();
116 log.warn("## DEV Using dummy base DN " + demoBaseDn);
117 // TODO downgrade security level
118 }
119 }
120 for (String userAdminUri : userAdminUris.split(" "))
121 uris.add(userAdminUri);
122
123 // Interprets URIs
124 for (String uri : uris) {
125 URI u;
126 try {
127 u = new URI(uri);
128 if (u.getPath() == null)
129 throw new CmsException("URI " + uri + " must have a path in order to determine base DN");
130 if (u.getScheme() == null) {
131 if (uri.startsWith("/") || uri.startsWith("./") || uri.startsWith("../"))
132 u = new File(uri).getCanonicalFile().toURI();
133 else if (!uri.contains("/")) {
134 u = KernelUtils.getOsgiInstanceUri(KernelConstants.DIR_NODE + '/' + uri);
135 // u = new URI(nodeBaseDir.toURI() + uri);
136 } else
137 throw new CmsException("Cannot interpret " + uri + " as an uri");
138 } else if (u.getScheme().equals("file")) {
139 u = new File(u).getCanonicalFile().toURI();
140 }
141 } catch (Exception e) {
142 throw new CmsException("Cannot interpret " + uri + " as an uri", e);
143 }
144 Dictionary<String, Object> properties = UserAdminConf.uriAsProperties(u.toString());
145 res.add(properties);
146 }
147
148 return res;
149 }
150
151 /**
152 * Called before node initialisation, in order populate OSGi instance are
153 * with some files (typically LDIF, etc).
154 */
155 static void prepareInstanceArea() {
156 String nodeInit = getFrameworkProp(NodeConstants.NODE_INIT);
157 if (nodeInit == null)
158 nodeInit = "../../init";
159 if (nodeInit.startsWith("http")) {
160 // remoteFirstInit(nodeInit);
161 return;
162 }
163
164 // TODO use java.nio.file
165 File initDir;
166 if (nodeInit.startsWith("."))
167 initDir = KernelUtils.getExecutionDir(nodeInit);
168 else
169 initDir = new File(nodeInit);
170 // TODO also uncompress archives
171 if (initDir.exists())
172 try {
173 FileUtils.copyDirectory(initDir, KernelUtils.getOsgiInstanceDir(), new FileFilter() {
174
175 @Override
176 public boolean accept(File pathname) {
177 if (pathname.getName().equals(".svn") || pathname.getName().equals(".git"))
178 return false;
179 return true;
180 }
181 });
182 log.info("CMS initialized from " + initDir.getCanonicalPath());
183 } catch (IOException e) {
184 throw new CmsException("Cannot initialize from " + initDir, e);
185 }
186 }
187
188 }