1 package org
.argeo
.cms
.internal
.kernel
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
6 import java
.nio
.file
.Files
;
7 import java
.nio
.file
.Path
;
8 import java
.util
.ArrayList
;
9 import java
.util
.Dictionary
;
10 import java
.util
.List
;
11 import java
.util
.SortedMap
;
12 import java
.util
.TreeMap
;
14 import javax
.naming
.InvalidNameException
;
15 import javax
.naming
.directory
.Attributes
;
16 import javax
.naming
.directory
.BasicAttributes
;
17 import javax
.naming
.ldap
.LdapName
;
18 import javax
.naming
.ldap
.Rdn
;
20 import org
.apache
.commons
.logging
.Log
;
21 import org
.apache
.commons
.logging
.LogFactory
;
22 import org
.argeo
.cms
.CmsException
;
23 import org
.argeo
.naming
.AttributesDictionary
;
24 import org
.argeo
.naming
.LdifParser
;
25 import org
.argeo
.naming
.LdifWriter
;
26 import org
.argeo
.node
.NodeConstants
;
27 import org
.argeo
.osgi
.useradmin
.UserAdminConf
;
28 import org
.eclipse
.equinox
.http
.jetty
.JettyConfigurator
;
29 import org
.osgi
.framework
.BundleContext
;
30 import org
.osgi
.framework
.FrameworkUtil
;
31 import org
.osgi
.service
.cm
.Configuration
;
32 import org
.osgi
.service
.cm
.ConfigurationAdmin
;
33 import org
.osgi
.service
.cm
.ConfigurationEvent
;
34 import org
.osgi
.service
.cm
.ConfigurationListener
;
36 /** Manages the LDIF-based deployment configuration. */
37 class DeployConfig
implements ConfigurationListener
{
38 private final Log log
= LogFactory
.getLog(getClass());
39 private final BundleContext bc
= FrameworkUtil
.getBundle(getClass()).getBundleContext();
41 private static Path deployConfigPath
= KernelUtils
.getOsgiInstancePath(KernelConstants
.DEPLOY_CONFIG_PATH
);
42 private SortedMap
<LdapName
, Attributes
> deployConfigs
= new TreeMap
<>();
43 private final DataModels dataModels
;
45 public DeployConfig(ConfigurationAdmin configurationAdmin
, DataModels dataModels
, boolean isClean
) {
46 this.dataModels
= dataModels
;
47 // ConfigurationAdmin configurationAdmin =
48 // bc.getService(bc.getServiceReference(ConfigurationAdmin.class));
50 boolean isFirstInit
= false;
51 if (!isInitialized()) { // first init
55 init(configurationAdmin
, isClean
, isFirstInit
);
56 } catch (IOException e
) {
57 throw new CmsException("Could not init deploy configs", e
);
59 // FIXME check race conditions during initialization
60 // bc.registerService(ConfigurationListener.class, this, null);
63 private void firstInit() throws IOException
{
64 log
.info("## FIRST INIT ##");
65 Files
.createDirectories(deployConfigPath
.getParent());
67 // FirstInit firstInit = new FirstInit();
68 InitUtils
.prepareFirstInitInstanceArea();
70 if (!Files
.exists(deployConfigPath
))
71 deployConfigs
= new TreeMap
<>();
72 else// config file could have juste been copied by preparation
73 try (InputStream in
= Files
.newInputStream(deployConfigPath
)) {
74 deployConfigs
= new LdifParser().read(in
);
79 private void setFromFrameworkProperties(boolean isFirstInit
) {
81 Dictionary
<String
, Object
> nodeConfig
= InitUtils
82 .getNodeRepositoryConfig(getProps(NodeConstants
.NODE_REPOS_FACTORY_PID
, NodeConstants
.NODE
));
83 // node repository is mandatory
84 putFactoryDeployConfig(NodeConstants
.NODE_REPOS_FACTORY_PID
, nodeConfig
);
86 // additional repositories
87 dataModels
: for (DataModels
.DataModel dataModel
: dataModels
.getNonAbstractDataModels()) {
88 if (NodeConstants
.NODE
.equals(dataModel
.getName()))
90 Dictionary
<String
, Object
> config
= InitUtils
.getRepositoryConfig(dataModel
.getName(),
91 getProps(NodeConstants
.NODE_REPOS_FACTORY_PID
, dataModel
.getName()));
92 if (config
.size() != 0)
93 putFactoryDeployConfig(NodeConstants
.NODE_REPOS_FACTORY_PID
, config
);
97 List
<Dictionary
<String
, Object
>> userDirectoryConfigs
= InitUtils
.getUserDirectoryConfigs();
98 if (userDirectoryConfigs
.size() != 0) {
99 List
<String
> activeCns
= new ArrayList
<>();
100 for (int i
= 0; i
< userDirectoryConfigs
.size(); i
++) {
101 Dictionary
<String
, Object
> userDirectoryConfig
= userDirectoryConfigs
.get(i
);
102 String cn
= UserAdminConf
.baseDnHash(userDirectoryConfig
);
104 userDirectoryConfig
.put(NodeConstants
.CN
, cn
);
105 putFactoryDeployConfig(NodeConstants
.NODE_USER_ADMIN_PID
, userDirectoryConfig
);
108 LdapName userAdminFactoryName
= serviceFactoryDn(NodeConstants
.NODE_USER_ADMIN_PID
);
109 for (LdapName name
: deployConfigs
.keySet()) {
110 if (name
.startsWith(userAdminFactoryName
) && !name
.equals(userAdminFactoryName
)) {
112 Attributes attrs
= deployConfigs
.get(name
);
113 String cn
= name
.getRdn(name
.size() - 1).getValue().toString();
114 if (!activeCns
.contains(cn
)) {
115 attrs
.put(UserAdminConf
.disabled
.name(), "true");
117 } catch (Exception e
) {
118 throw new CmsException("Cannot disable user directory " + name
, e
);
125 // Dictionary<String, Object> webServerConfig = InitUtils
126 // .getHttpServerConfig(getProps(KernelConstants.JETTY_FACTORY_PID, NodeConstants.DEFAULT));
127 // if (!webServerConfig.isEmpty()) {
128 // // TODO check for other customizers
129 // webServerConfig.put("customizer.class", "org.argeo.equinox.jetty.CmsJettyCustomizer");
130 // putFactoryDeployConfig(KernelConstants.JETTY_FACTORY_PID, webServerConfig);
132 LdapName defaultHttpServiceDn
= serviceDn(KernelConstants
.JETTY_FACTORY_PID
, NodeConstants
.DEFAULT
);
133 if (deployConfigs
.containsKey(defaultHttpServiceDn
)) {
134 // remove old default configs since we have now to start Jetty servlet bridge
136 deployConfigs
.remove(defaultHttpServiceDn
);
143 // Explicitly configures Jetty so that the default server is not started by the
144 // activator of the Equinox Jetty bundle.
145 Dictionary
<String
, Object
> webServerConfig
= InitUtils
146 .getHttpServerConfig(getProps(KernelConstants
.JETTY_FACTORY_PID
, NodeConstants
.DEFAULT
));
147 if (!webServerConfig
.isEmpty()) {
148 webServerConfig
.put("customizer.class", KernelConstants
.CMS_JETTY_CUSTOMIZER_CLASS
);
153 tryGettyJetty
: while (tryCount
> 0) {
155 JettyConfigurator
.startServer(KernelConstants
.DEFAULT_JETTY_SERVER
, webServerConfig
);
157 } catch (IllegalStateException e
) {
158 // Jetty may not be ready
161 } catch (Exception e1
) {
166 } catch (Exception e
) {
167 log
.error("Cannot start default Jetty server with config " + webServerConfig
, e
);
172 private void init(ConfigurationAdmin configurationAdmin
, boolean isClean
, boolean isFirstInit
) throws IOException
{
174 try (InputStream in
= Files
.newInputStream(deployConfigPath
)) {
175 deployConfigs
= new LdifParser().read(in
);
178 setFromFrameworkProperties(isFirstInit
);
179 for (LdapName dn
: deployConfigs
.keySet()) {
180 Rdn lastRdn
= dn
.getRdn(dn
.size() - 1);
181 LdapName prefix
= (LdapName
) dn
.getPrefix(dn
.size() - 1);
182 if (prefix
.toString().equals(NodeConstants
.DEPLOY_BASEDN
)) {
183 if (lastRdn
.getType().equals(NodeConstants
.CN
)) {
185 String pid
= lastRdn
.getValue().toString();
186 Configuration conf
= configurationAdmin
.getConfiguration(pid
);
187 AttributesDictionary dico
= new AttributesDictionary(deployConfigs
.get(dn
));
190 // service factory definition
193 // service factory service
194 Rdn beforeLastRdn
= dn
.getRdn(dn
.size() - 2);
195 assert beforeLastRdn
.getType().equals(NodeConstants
.OU
);
196 String factoryPid
= beforeLastRdn
.getValue().toString();
197 Configuration conf
= configurationAdmin
.createFactoryConfiguration(factoryPid
.toString(), null);
198 AttributesDictionary dico
= new AttributesDictionary(deployConfigs
.get(dn
));
203 // TODO check consistency if not clean
207 public void configurationEvent(ConfigurationEvent event
) {
209 if (ConfigurationEvent
.CM_UPDATED
== event
.getType()) {
210 ConfigurationAdmin configurationAdmin
= bc
.getService(event
.getReference());
211 Configuration conf
= configurationAdmin
.getConfiguration(event
.getPid(), null);
212 LdapName serviceDn
= null;
213 String factoryPid
= conf
.getFactoryPid();
214 if (factoryPid
!= null) {
215 LdapName serviceFactoryDn
= serviceFactoryDn(factoryPid
);
216 if (deployConfigs
.containsKey(serviceFactoryDn
)) {
217 for (LdapName dn
: deployConfigs
.keySet()) {
218 if (dn
.startsWith(serviceFactoryDn
)) {
219 Rdn lastRdn
= dn
.getRdn(dn
.size() - 1);
220 assert lastRdn
.getType().equals(NodeConstants
.CN
);
221 Object value
= conf
.getProperties().get(lastRdn
.getType());
222 assert value
!= null;
223 if (value
.equals(lastRdn
.getValue())) {
230 Object cn
= conf
.getProperties().get(NodeConstants
.CN
);
232 throw new IllegalArgumentException("Properties must contain cn");
233 if (serviceDn
== null) {
234 putFactoryDeployConfig(factoryPid
, conf
.getProperties());
236 Attributes attrs
= deployConfigs
.get(serviceDn
);
237 assert attrs
!= null;
238 AttributesDictionary
.copy(conf
.getProperties(), attrs
);
241 if (log
.isDebugEnabled())
242 log
.debug("Updated deploy config " + serviceDn(factoryPid
, cn
.toString()));
244 // ignore non config-registered service factories
247 serviceDn
= serviceDn(event
.getPid());
248 if (deployConfigs
.containsKey(serviceDn
)) {
249 Attributes attrs
= deployConfigs
.get(serviceDn
);
250 assert attrs
!= null;
251 AttributesDictionary
.copy(conf
.getProperties(), attrs
);
253 if (log
.isDebugEnabled())
254 log
.debug("Updated deploy config " + serviceDn
);
256 // ignore non config-registered services
260 } catch (Exception e
) {
261 log
.error("Could not handle configuration event", e
);
265 void putFactoryDeployConfig(String factoryPid
, Dictionary
<String
, Object
> props
) {
266 Object cn
= props
.get(NodeConstants
.CN
);
268 throw new IllegalArgumentException("cn must be set in properties");
269 LdapName serviceFactoryDn
= serviceFactoryDn(factoryPid
);
270 if (!deployConfigs
.containsKey(serviceFactoryDn
))
271 deployConfigs
.put(serviceFactoryDn
, new BasicAttributes(NodeConstants
.OU
, factoryPid
));
272 LdapName serviceDn
= serviceDn(factoryPid
, cn
.toString());
273 Attributes attrs
= new BasicAttributes();
274 AttributesDictionary
.copy(props
, attrs
);
275 deployConfigs
.put(serviceDn
, attrs
);
278 void putDeployConfig(String servicePid
, Dictionary
<String
, Object
> props
) {
279 LdapName serviceDn
= serviceDn(servicePid
);
280 Attributes attrs
= new BasicAttributes(NodeConstants
.CN
, servicePid
);
281 AttributesDictionary
.copy(props
, attrs
);
282 deployConfigs
.put(serviceDn
, attrs
);
286 try (Writer writer
= Files
.newBufferedWriter(deployConfigPath
)) {
287 new LdifWriter(writer
).write(deployConfigs
);
288 } catch (IOException e
) {
289 // throw new CmsException("Cannot save deploy configs", e);
290 log
.error("Cannot save deploy configs", e
);
294 boolean isStandalone(String dataModelName
) {
295 return getProps(NodeConstants
.NODE_REPOS_FACTORY_PID
, dataModelName
) != null;
301 private LdapName
serviceFactoryDn(String factoryPid
) {
303 return new LdapName(NodeConstants
.OU
+ "=" + factoryPid
+ "," + NodeConstants
.DEPLOY_BASEDN
);
304 } catch (InvalidNameException e
) {
305 throw new IllegalArgumentException("Cannot generate DN from " + factoryPid
, e
);
309 private LdapName
serviceDn(String servicePid
) {
311 return new LdapName(NodeConstants
.CN
+ "=" + servicePid
+ "," + NodeConstants
.DEPLOY_BASEDN
);
312 } catch (InvalidNameException e
) {
313 throw new IllegalArgumentException("Cannot generate DN from " + servicePid
, e
);
317 private LdapName
serviceDn(String factoryPid
, String cn
) {
319 return (LdapName
) serviceFactoryDn(factoryPid
).add(new Rdn(NodeConstants
.CN
, cn
));
320 } catch (InvalidNameException e
) {
321 throw new IllegalArgumentException("Cannot generate DN from " + factoryPid
+ " and " + cn
, e
);
325 Dictionary
<String
, Object
> getProps(String factoryPid
, String cn
) {
326 Attributes attrs
= deployConfigs
.get(serviceDn(factoryPid
, cn
));
328 return new AttributesDictionary(attrs
);
333 static boolean isInitialized() {
334 return Files
.exists(deployConfigPath
);