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
.api
.NodeConstants
;
23 import org
.argeo
.naming
.AttributesDictionary
;
24 import org
.argeo
.naming
.LdifParser
;
25 import org
.argeo
.naming
.LdifWriter
;
26 import org
.argeo
.osgi
.useradmin
.UserAdminConf
;
27 import org
.eclipse
.equinox
.http
.jetty
.JettyConfigurator
;
28 import org
.osgi
.framework
.BundleContext
;
29 import org
.osgi
.framework
.FrameworkUtil
;
30 import org
.osgi
.service
.cm
.Configuration
;
31 import org
.osgi
.service
.cm
.ConfigurationAdmin
;
32 import org
.osgi
.service
.cm
.ConfigurationEvent
;
33 import org
.osgi
.service
.cm
.ConfigurationListener
;
35 /** Manages the LDIF-based deployment configuration. */
36 class DeployConfig
implements ConfigurationListener
{
37 private final Log log
= LogFactory
.getLog(getClass());
38 private final BundleContext bc
= FrameworkUtil
.getBundle(getClass()).getBundleContext();
40 private static Path deployConfigPath
= KernelUtils
.getOsgiInstancePath(KernelConstants
.DEPLOY_CONFIG_PATH
);
41 private SortedMap
<LdapName
, Attributes
> deployConfigs
= new TreeMap
<>();
42 private final DataModels dataModels
;
44 private boolean isFirstInit
= false;
46 public DeployConfig(ConfigurationAdmin configurationAdmin
, DataModels dataModels
, boolean isClean
) {
47 this.dataModels
= dataModels
;
48 // ConfigurationAdmin configurationAdmin =
49 // bc.getService(bc.getServiceReference(ConfigurationAdmin.class));
51 if (!isInitialized()) { // first init
55 init(configurationAdmin
, isClean
, isFirstInit
);
56 } catch (IOException e
) {
57 throw new RuntimeException("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_REPOSITORY
.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);
150 // // TODO centralise with Jetty extender
151 // Object webSocketEnabled = webServerConfig.get(InternalHttpConstants.WEBSOCKET_ENABLED);
152 // if (webSocketEnabled != null && webSocketEnabled.toString().equals("true")) {
153 // bc.registerService(ServerEndpointConfig.Configurator.class, new CmsWebSocketConfigurator(), null);
154 // webServerConfig.put(InternalHttpConstants.WEBSOCKET_ENABLED, "true");
160 tryGettyJetty
: while (tryCount
> 0) {
162 JettyConfigurator
.startServer(KernelConstants
.DEFAULT_JETTY_SERVER
, webServerConfig
);
163 // Explicitly starts Jetty OSGi HTTP bundle, so that it gets triggered if OSGi
164 // configuration is not cleaned
165 FrameworkUtil
.getBundle(JettyConfigurator
.class).start();
167 } catch (IllegalStateException e
) {
168 // Jetty may not be ready
171 } catch (Exception e1
) {
177 } catch (Exception e
) {
178 log
.error("Cannot start default Jetty server with config " + webServerConfig
, e
);
183 private void init(ConfigurationAdmin configurationAdmin
, boolean isClean
, boolean isFirstInit
) throws IOException
{
185 try (InputStream in
= Files
.newInputStream(deployConfigPath
)) {
186 deployConfigs
= new LdifParser().read(in
);
189 if (log
.isDebugEnabled())
190 log
.debug("Clean state, loading from framework properties...");
191 setFromFrameworkProperties(isFirstInit
);
192 for (LdapName dn
: deployConfigs
.keySet()) {
193 Rdn lastRdn
= dn
.getRdn(dn
.size() - 1);
194 LdapName prefix
= (LdapName
) dn
.getPrefix(dn
.size() - 1);
195 if (prefix
.toString().equals(NodeConstants
.DEPLOY_BASEDN
)) {
196 if (lastRdn
.getType().equals(NodeConstants
.CN
)) {
198 String pid
= lastRdn
.getValue().toString();
199 Configuration conf
= configurationAdmin
.getConfiguration(pid
);
200 AttributesDictionary dico
= new AttributesDictionary(deployConfigs
.get(dn
));
203 // service factory definition
206 // service factory service
207 Rdn beforeLastRdn
= dn
.getRdn(dn
.size() - 2);
208 assert beforeLastRdn
.getType().equals(NodeConstants
.OU
);
209 String factoryPid
= beforeLastRdn
.getValue().toString();
210 Configuration conf
= configurationAdmin
.createFactoryConfiguration(factoryPid
.toString(), null);
211 AttributesDictionary dico
= new AttributesDictionary(deployConfigs
.get(dn
));
216 // TODO check consistency if not clean
220 public void configurationEvent(ConfigurationEvent event
) {
222 if (ConfigurationEvent
.CM_UPDATED
== event
.getType()) {
223 ConfigurationAdmin configurationAdmin
= bc
.getService(event
.getReference());
224 Configuration conf
= configurationAdmin
.getConfiguration(event
.getPid(), null);
225 LdapName serviceDn
= null;
226 String factoryPid
= conf
.getFactoryPid();
227 if (factoryPid
!= null) {
228 LdapName serviceFactoryDn
= serviceFactoryDn(factoryPid
);
229 if (deployConfigs
.containsKey(serviceFactoryDn
)) {
230 for (LdapName dn
: deployConfigs
.keySet()) {
231 if (dn
.startsWith(serviceFactoryDn
)) {
232 Rdn lastRdn
= dn
.getRdn(dn
.size() - 1);
233 assert lastRdn
.getType().equals(NodeConstants
.CN
);
234 Object value
= conf
.getProperties().get(lastRdn
.getType());
235 assert value
!= null;
236 if (value
.equals(lastRdn
.getValue())) {
243 Object cn
= conf
.getProperties().get(NodeConstants
.CN
);
245 throw new IllegalArgumentException("Properties must contain cn");
246 if (serviceDn
== null) {
247 putFactoryDeployConfig(factoryPid
, conf
.getProperties());
249 Attributes attrs
= deployConfigs
.get(serviceDn
);
250 assert attrs
!= null;
251 AttributesDictionary
.copy(conf
.getProperties(), attrs
);
254 if (log
.isDebugEnabled())
255 log
.debug("Updated deploy config " + serviceDn(factoryPid
, cn
.toString()));
257 // ignore non config-registered service factories
260 serviceDn
= serviceDn(event
.getPid());
261 if (deployConfigs
.containsKey(serviceDn
)) {
262 Attributes attrs
= deployConfigs
.get(serviceDn
);
263 assert attrs
!= null;
264 AttributesDictionary
.copy(conf
.getProperties(), attrs
);
266 if (log
.isDebugEnabled())
267 log
.debug("Updated deploy config " + serviceDn
);
269 // ignore non config-registered services
273 } catch (Exception e
) {
274 log
.error("Could not handle configuration event", e
);
278 void putFactoryDeployConfig(String factoryPid
, Dictionary
<String
, Object
> props
) {
279 Object cn
= props
.get(NodeConstants
.CN
);
281 throw new IllegalArgumentException("cn must be set in properties");
282 LdapName serviceFactoryDn
= serviceFactoryDn(factoryPid
);
283 if (!deployConfigs
.containsKey(serviceFactoryDn
))
284 deployConfigs
.put(serviceFactoryDn
, new BasicAttributes(NodeConstants
.OU
, factoryPid
));
285 LdapName serviceDn
= serviceDn(factoryPid
, cn
.toString());
286 Attributes attrs
= new BasicAttributes();
287 AttributesDictionary
.copy(props
, attrs
);
288 deployConfigs
.put(serviceDn
, attrs
);
291 void putDeployConfig(String servicePid
, Dictionary
<String
, Object
> props
) {
292 LdapName serviceDn
= serviceDn(servicePid
);
293 Attributes attrs
= new BasicAttributes(NodeConstants
.CN
, servicePid
);
294 AttributesDictionary
.copy(props
, attrs
);
295 deployConfigs
.put(serviceDn
, attrs
);
299 try (Writer writer
= Files
.newBufferedWriter(deployConfigPath
)) {
300 new LdifWriter(writer
).write(deployConfigs
);
301 } catch (IOException e
) {
302 // throw new CmsException("Cannot save deploy configs", e);
303 log
.error("Cannot save deploy configs", e
);
307 boolean isStandalone(String dataModelName
) {
308 return getProps(NodeConstants
.NODE_REPOS_FACTORY_PID
, dataModelName
) != null;
314 private LdapName
serviceFactoryDn(String factoryPid
) {
316 return new LdapName(NodeConstants
.OU
+ "=" + factoryPid
+ "," + NodeConstants
.DEPLOY_BASEDN
);
317 } catch (InvalidNameException e
) {
318 throw new IllegalArgumentException("Cannot generate DN from " + factoryPid
, e
);
322 private LdapName
serviceDn(String servicePid
) {
324 return new LdapName(NodeConstants
.CN
+ "=" + servicePid
+ "," + NodeConstants
.DEPLOY_BASEDN
);
325 } catch (InvalidNameException e
) {
326 throw new IllegalArgumentException("Cannot generate DN from " + servicePid
, e
);
330 private LdapName
serviceDn(String factoryPid
, String cn
) {
332 return (LdapName
) serviceFactoryDn(factoryPid
).add(new Rdn(NodeConstants
.CN
, cn
));
333 } catch (InvalidNameException e
) {
334 throw new IllegalArgumentException("Cannot generate DN from " + factoryPid
+ " and " + cn
, e
);
338 Dictionary
<String
, Object
> getProps(String factoryPid
, String cn
) {
339 Attributes attrs
= deployConfigs
.get(serviceDn(factoryPid
, cn
));
341 return new AttributesDictionary(attrs
);
346 private static boolean isInitialized() {
347 return Files
.exists(deployConfigPath
);
350 public boolean isFirstInit() {