]>
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
.node
.NodeConstants
;
20 import org
.argeo
.node
.RepoConf
;
21 import org
.argeo
.osgi
.useradmin
.UserAdminConf
;
22 import org
.eclipse
.equinox
.http
.jetty
.JettyConstants
;
25 * Interprets framework properties in order to generate the initial deploy
28 class FirstInitProperties
{
29 private final static Log log
= LogFactory
.getLog(FirstInitProperties
.class);
31 public FirstInitProperties() {
32 log
.info("## FIRST INIT ##");
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());
41 props
.put(repoConf
.name(), value
);
43 props
.put(NodeConstants
.CN
, NodeConstants
.ALIAS_NODE
);
44 props
.put(NodeConstants
.JCR_REPOSITORY_ALIAS
, NodeConstants
.ALIAS_NODE
);
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");
55 final Hashtable
<String
, Object
> props
= new Hashtable
<String
, Object
>();
57 if (httpPort
!= null || httpsPort
!= null) {
58 if (httpPort
!= null) {
59 props
.put(JettyConstants
.HTTP_PORT
, httpPort
);
60 props
.put(JettyConstants
.HTTP_ENABLED
, true);
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);
71 if (httpHost
!= null) {
72 props
.put(JettyConstants
.HTTP_HOST
, httpHost
);
74 props
.put(NodeConstants
.CN
, NodeConstants
.DEFAULT
);
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
<>();
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())
91 FileUtils
.copyInputStreamToFile(getClass().getResourceAsStream(baseNodeRoleDn
+ ".ldif"),
93 } catch (IOException e
) {
94 throw new CmsException("Cannot copy demo resource", e
);
96 nodeRolesUri
= nodeRolesFile
.toURI().toString();
98 uris
.add(nodeRolesUri
);
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())
107 FileUtils
.copyInputStreamToFile(getClass().getResourceAsStream(demoBaseDn
+ ".ldif"),
109 } catch (IOException e
) {
110 throw new CmsException("Cannot copy demo resource", e
);
112 userAdminUris
= businessRolesFile
.toURI().toString();
114 for (String userAdminUri
: userAdminUris
.split(" "))
115 uris
.add(userAdminUri
);
118 for (String uri
: uris
) {
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);
131 throw new CmsException("Cannot interpret " + uri
+ " as an uri");
132 } else if (u
.getScheme().equals("file")) {
133 u
= new File(u
).getCanonicalFile().toURI();
135 } catch (Exception e
) {
136 throw new CmsException("Cannot interpret " + uri
+ " as an uri", e
);
138 Dictionary
<String
, Object
> properties
= UserAdminConf
.uriAsProperties(u
.toString());
146 * Called before node initialisation, in order populate OSGi instance are
147 * with some files (typically LDIF, etc).
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);
158 // TODO use java.nio.file
160 if (nodeInit
.startsWith("."))
161 initDir
= KernelUtils
.getExecutionDir(nodeInit
);
163 initDir
= new File(nodeInit
);
164 // TODO also uncompress archives
165 if (initDir
.exists())
167 FileUtils
.copyDirectory(initDir
, KernelUtils
.getOsgiInstanceDir(), new FileFilter() {
170 public boolean accept(File pathname
) {
171 if (pathname
.getName().equals(".svn") || pathname
.getName().equals(".git"))
176 log
.info("CMS initialized from " + initDir
.getCanonicalPath());
177 } catch (IOException e
) {
178 throw new CmsException("Cannot initialize from " + initDir
, e
);