]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/kernel/FirstInitProperties.java
1 package org
.argeo
.cms
.internal
.kernel
;
3 import static org
.argeo
.cms
.internal
.kernel
.KernelUtils
.getFrameworkProp
;
6 import java
.io
.FileFilter
;
7 import java
.io
.IOException
;
9 import java
.util
.ArrayList
;
10 import java
.util
.Dictionary
;
11 import java
.util
.Hashtable
;
12 import java
.util
.List
;
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
;
26 * Interprets framework properties in order to generate the initial deploy
29 class FirstInitProperties
{
30 private final static Log log
= LogFactory
.getLog(FirstInitProperties
.class);
32 public FirstInitProperties() {
33 log
.info("## FIRST INIT ##");
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());
42 props
.put(repoConf
.name(), value
);
44 props
.put(NodeConstants
.CN
, ArgeoJcrConstants
.ALIAS_NODE
);
45 props
.put(ArgeoJcrConstants
.JCR_REPOSITORY_ALIAS
, ArgeoJcrConstants
.ALIAS_NODE
);
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");
56 final Hashtable
<String
, Object
> props
= new Hashtable
<String
, Object
>();
58 if (httpPort
!= null || httpsPort
!= null) {
59 if (httpPort
!= null) {
60 props
.put(JettyConstants
.HTTP_PORT
, httpPort
);
61 props
.put(JettyConstants
.HTTP_ENABLED
, true);
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);
72 if (httpHost
!= null) {
73 props
.put(JettyConstants
.HTTP_HOST
, httpHost
);
75 props
.put(NodeConstants
.CN
, NodeConstants
.DEFAULT
);
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
<>();
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())
92 FileUtils
.copyInputStreamToFile(getClass().getResourceAsStream(baseNodeRoleDn
+ ".ldif"),
94 } catch (IOException e
) {
95 throw new CmsException("Cannot copy demo resource", e
);
97 nodeRolesUri
= nodeRolesFile
.toURI().toString();
99 uris
.add(nodeRolesUri
);
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())
108 FileUtils
.copyInputStreamToFile(getClass().getResourceAsStream(demoBaseDn
+ ".ldif"),
110 } catch (IOException e
) {
111 throw new CmsException("Cannot copy demo resource", e
);
113 userAdminUris
= businessRolesFile
.toURI().toString();
115 for (String userAdminUri
: userAdminUris
.split(" "))
116 uris
.add(userAdminUri
);
119 for (String uri
: uris
) {
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);
132 throw new CmsException("Cannot interpret " + uri
+ " as an uri");
133 } else if (u
.getScheme().equals("file")) {
134 u
= new File(u
).getCanonicalFile().toURI();
136 } catch (Exception e
) {
137 throw new CmsException("Cannot interpret " + uri
+ " as an uri", e
);
139 Dictionary
<String
, Object
> properties
= UserAdminConf
.uriAsProperties(u
.toString());
147 * Called before node initialisation, in order populate OSGi instance are
148 * with some files (typically LDIF, etc).
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);
159 // TODO use java.nio.file
161 if (nodeInit
.startsWith("."))
162 initDir
= KernelUtils
.getExecutionDir(nodeInit
);
164 initDir
= new File(nodeInit
);
165 // TODO also uncompress archives
166 if (initDir
.exists())
168 FileUtils
.copyDirectory(initDir
, KernelUtils
.getOsgiInstanceDir(), new FileFilter() {
171 public boolean accept(File pathname
) {
172 if (pathname
.getName().equals(".svn") || pathname
.getName().equals(".git"))
177 log
.info("CMS initialized from " + initDir
.getCanonicalPath());
178 } catch (IOException e
) {
179 throw new CmsException("Cannot initialize from " + initDir
, e
);