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
.Dictionary
;
10 import java
.util
.SortedMap
;
11 import java
.util
.TreeMap
;
13 import javax
.naming
.InvalidNameException
;
14 import javax
.naming
.directory
.Attributes
;
15 import javax
.naming
.directory
.BasicAttributes
;
16 import javax
.naming
.ldap
.LdapName
;
17 import javax
.naming
.ldap
.Rdn
;
19 import org
.apache
.commons
.logging
.Log
;
20 import org
.apache
.commons
.logging
.LogFactory
;
21 import org
.argeo
.cms
.CmsException
;
22 import org
.argeo
.naming
.AttributesDictionary
;
23 import org
.argeo
.naming
.LdifParser
;
24 import org
.argeo
.naming
.LdifWriter
;
25 import org
.argeo
.node
.NodeConstants
;
26 import org
.osgi
.framework
.BundleContext
;
27 import org
.osgi
.framework
.FrameworkUtil
;
28 import org
.osgi
.service
.cm
.Configuration
;
29 import org
.osgi
.service
.cm
.ConfigurationAdmin
;
30 import org
.osgi
.service
.cm
.ConfigurationEvent
;
31 import org
.osgi
.service
.cm
.ConfigurationListener
;
33 class DeployConfig
implements ConfigurationListener
{
34 private final Log log
= LogFactory
.getLog(getClass());
35 private final BundleContext bc
= FrameworkUtil
.getBundle(getClass()).getBundleContext();
37 private Path deployConfigPath
= KernelUtils
.getOsgiInstancePath(KernelConstants
.DEPLOY_CONFIG_PATH
);
38 private SortedMap
<LdapName
, Attributes
> deployConfigs
= new TreeMap
<>();
40 public DeployConfig(ConfigurationAdmin configurationAdmin
,boolean isClean
) {
41 // ConfigurationAdmin configurationAdmin =
42 // bc.getService(bc.getServiceReference(ConfigurationAdmin.class));
44 if (!Files
.exists(deployConfigPath
)) { // first init
47 init(configurationAdmin
, isClean
);
48 } catch (IOException e
) {
49 throw new CmsException("Could not init deploy configs", e
);
51 // FIXME check race conditions during initialization
52 // bc.registerService(ConfigurationListener.class, this, null);
55 private void firstInit() throws IOException
{
56 Files
.createDirectories(deployConfigPath
.getParent());
58 FirstInitProperties firstInit
= new FirstInitProperties();
59 firstInit
.prepareInstanceArea();
61 if (!Files
.exists(deployConfigPath
))// could have juste been copied
62 Files
.createFile(deployConfigPath
);
64 try (InputStream in
= Files
.newInputStream(deployConfigPath
)) {
65 deployConfigs
= new LdifParser().read(in
);
69 Dictionary
<String
, Object
> nodeConfig
= firstInit
70 .getNodeRepositoryConfig(getProps(NodeConstants
.NODE_REPOS_FACTORY_PID
, NodeConstants
.NODE
));
71 // node repository is mandatory
72 putFactoryDeployConfig(NodeConstants
.NODE_REPOS_FACTORY_PID
, nodeConfig
);
76 List
<Dictionary
<String
, Object
>> userDirectoryConfigs
= firstInit
.getUserDirectoryConfigs();
77 for (int i
= 0; i
< userDirectoryConfigs
.size(); i
++) {
78 Dictionary
<String
, Object
> userDirectoryConfig
= userDirectoryConfigs
.get(i
);
79 String cn
= Integer
.toString(i
);
80 userDirectoryConfig
.put(NodeConstants
.CN
, cn
);
81 putFactoryDeployConfig(NodeConstants
.NODE_USER_ADMIN_PID
, userDirectoryConfig
);
85 Dictionary
<String
, Object
> webServerConfig
= firstInit
86 .getHttpServerConfig(getProps(KernelConstants
.JETTY_FACTORY_PID
, NodeConstants
.DEFAULT
));
87 if (!webServerConfig
.isEmpty())
88 putFactoryDeployConfig(KernelConstants
.JETTY_FACTORY_PID
, webServerConfig
);
93 private void init(ConfigurationAdmin configurationAdmin
, boolean isClean
) throws IOException
{
95 try (InputStream in
= Files
.newInputStream(deployConfigPath
)) {
96 deployConfigs
= new LdifParser().read(in
);
99 for (LdapName dn
: deployConfigs
.keySet()) {
100 Rdn lastRdn
= dn
.getRdn(dn
.size() - 1);
101 LdapName prefix
= (LdapName
) dn
.getPrefix(dn
.size() - 1);
102 if (prefix
.toString().equals(NodeConstants
.DEPLOY_BASEDN
)) {
103 if (lastRdn
.getType().equals(NodeConstants
.CN
)) {
105 String pid
= lastRdn
.getValue().toString();
106 Configuration conf
= configurationAdmin
.getConfiguration(pid
);
107 AttributesDictionary dico
= new AttributesDictionary(deployConfigs
.get(dn
));
110 // service factory definition
113 // service factory service
114 Rdn beforeLastRdn
= dn
.getRdn(dn
.size() - 2);
115 assert beforeLastRdn
.getType().equals(NodeConstants
.OU
);
116 String factoryPid
= beforeLastRdn
.getValue().toString();
117 Configuration conf
= configurationAdmin
.createFactoryConfiguration(factoryPid
.toString(), null);
118 AttributesDictionary dico
= new AttributesDictionary(deployConfigs
.get(dn
));
123 // TODO check consistency if not clean
127 public void configurationEvent(ConfigurationEvent event
) {
129 if (ConfigurationEvent
.CM_UPDATED
== event
.getType()) {
130 ConfigurationAdmin configurationAdmin
= bc
.getService(event
.getReference());
131 Configuration conf
= configurationAdmin
.getConfiguration(event
.getPid(), null);
132 LdapName serviceDn
= null;
133 String factoryPid
= conf
.getFactoryPid();
134 if (factoryPid
!= null) {
135 LdapName serviceFactoryDn
= serviceFactoryDn(factoryPid
);
136 if (deployConfigs
.containsKey(serviceFactoryDn
)) {
137 for (LdapName dn
: deployConfigs
.keySet()) {
138 if (dn
.startsWith(serviceFactoryDn
)) {
139 Rdn lastRdn
= dn
.getRdn(dn
.size() - 1);
140 assert lastRdn
.getType().equals(NodeConstants
.CN
);
141 Object value
= conf
.getProperties().get(lastRdn
.getType());
142 assert value
!= null;
143 if (value
.equals(lastRdn
.getValue())) {
150 Object cn
= conf
.getProperties().get(NodeConstants
.CN
);
152 throw new IllegalArgumentException("Properties must contain cn");
153 if (serviceDn
== null) {
154 putFactoryDeployConfig(factoryPid
, conf
.getProperties());
156 Attributes attrs
= deployConfigs
.get(serviceDn
);
157 assert attrs
!= null;
158 AttributesDictionary
.copy(conf
.getProperties(), attrs
);
161 if (log
.isDebugEnabled())
162 log
.debug("Updated deploy config " + serviceDn(factoryPid
, cn
.toString()));
164 // ignore non config-registered service factories
167 serviceDn
= serviceDn(event
.getPid());
168 if (deployConfigs
.containsKey(serviceDn
)) {
169 Attributes attrs
= deployConfigs
.get(serviceDn
);
170 assert attrs
!= null;
171 AttributesDictionary
.copy(conf
.getProperties(), attrs
);
173 if (log
.isDebugEnabled())
174 log
.debug("Updated deploy config " + serviceDn
);
176 // ignore non config-registered services
180 } catch (Exception e
) {
181 log
.error("Could not handle configuration event", e
);
185 void putFactoryDeployConfig(String factoryPid
, Dictionary
<String
, Object
> props
) {
186 Object cn
= props
.get(NodeConstants
.CN
);
188 throw new IllegalArgumentException("cn must be set in properties");
189 LdapName serviceFactoryDn
= serviceFactoryDn(factoryPid
);
190 if (!deployConfigs
.containsKey(serviceFactoryDn
))
191 deployConfigs
.put(serviceFactoryDn
, new BasicAttributes(NodeConstants
.OU
, factoryPid
));
192 LdapName serviceDn
= serviceDn(factoryPid
, cn
.toString());
193 Attributes attrs
= new BasicAttributes();
194 AttributesDictionary
.copy(props
, attrs
);
195 deployConfigs
.put(serviceDn
, attrs
);
198 void putDeployConfig(String servicePid
, Dictionary
<String
, Object
> props
) {
199 LdapName serviceDn
= serviceDn(servicePid
);
200 Attributes attrs
= new BasicAttributes(NodeConstants
.CN
, servicePid
);
201 AttributesDictionary
.copy(props
, attrs
);
202 deployConfigs
.put(serviceDn
, attrs
);
206 try (Writer writer
= Files
.newBufferedWriter(deployConfigPath
)) {
207 new LdifWriter(writer
).write(deployConfigs
);
208 } catch (IOException e
) {
209 throw new CmsException("Cannot save deploy configs", e
);
216 private LdapName
serviceFactoryDn(String factoryPid
) {
218 return new LdapName(NodeConstants
.OU
+ "=" + factoryPid
+ "," + NodeConstants
.DEPLOY_BASEDN
);
219 } catch (InvalidNameException e
) {
220 throw new IllegalArgumentException("Cannot generate DN from " + factoryPid
, e
);
224 private LdapName
serviceDn(String servicePid
) {
226 return new LdapName(NodeConstants
.CN
+ "=" + servicePid
+ "," + NodeConstants
.DEPLOY_BASEDN
);
227 } catch (InvalidNameException e
) {
228 throw new IllegalArgumentException("Cannot generate DN from " + servicePid
, e
);
232 private LdapName
serviceDn(String factoryPid
, String cn
) {
234 return (LdapName
) serviceFactoryDn(factoryPid
).add(new Rdn(NodeConstants
.CN
, cn
));
235 } catch (InvalidNameException e
) {
236 throw new IllegalArgumentException("Cannot generate DN from " + factoryPid
+ " and " + cn
, e
);
240 Dictionary
<String
, Object
> getProps(String factoryPid
, String cn
) {
241 Attributes attrs
= deployConfigs
.get(serviceDn(factoryPid
, cn
));
243 return new AttributesDictionary(attrs
);