]> git.argeo.org Git - lgpl/argeo-commons.git/blob - NodeDeployConfig.java
39ca0b34cd246141e40cf183a03517bbed4caf9a
[lgpl/argeo-commons.git] / NodeDeployConfig.java
1 package org.argeo.cms.internal.kernel;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.Writer;
6 import java.net.URI;
7 import java.net.URISyntaxException;
8 import java.nio.file.Files;
9 import java.nio.file.Path;
10 import java.nio.file.Paths;
11 import java.util.Dictionary;
12 import java.util.Hashtable;
13 import java.util.SortedMap;
14 import java.util.function.Function;
15
16 import javax.naming.InvalidNameException;
17 import javax.naming.directory.Attributes;
18 import javax.naming.ldap.LdapName;
19
20 import org.argeo.cms.CmsException;
21 import org.argeo.naming.AttributesDictionary;
22 import org.argeo.naming.LdifParser;
23 import org.argeo.naming.LdifWriter;
24 import org.argeo.node.NodeConstants;
25
26 @Deprecated
27 class NodeDeployConfig {
28 private final String BASE = "ou=deploy,ou=node";
29 private final Path path;
30 private final Function<String, String> getter;
31
32 private final SortedMap<LdapName, Attributes> configurations;
33
34 public NodeDeployConfig(Function<String, String> getter) {
35 String osgiConfigurationArea = getter.apply(KernelUtils.OSGI_CONFIGURATION_AREA);
36 try {
37 this.path = Paths.get(new URI(osgiConfigurationArea));
38 } catch (URISyntaxException e) {
39 throw new IllegalArgumentException("Cannot parse " + getter.apply(KernelUtils.OSGI_CONFIGURATION_AREA), e);
40 }
41 this.getter = getter;
42
43 if (!Files.exists(path))
44 try (Writer writer = Files.newBufferedWriter(path)) {
45 Files.createFile(path);
46 LdifWriter ldifWriter = new LdifWriter(writer);
47 } catch (IOException e) {
48 throw new CmsException("Cannot create " + path, e);
49 }
50
51 try (InputStream in = Files.newInputStream(path)) {
52 configurations = new LdifParser().read(in);
53 } catch (IOException e) {
54 throw new CmsException("Cannot read " + path, e);
55 }
56 }
57
58 public Dictionary<String, Object> getConfiguration(String servicePid) {
59 LdapName dn;
60 try {
61 dn = new LdapName("ou=" + servicePid + "," + BASE);
62 } catch (InvalidNameException e) {
63 throw new IllegalArgumentException("Cannot parse DN", e);
64 }
65 if (configurations.containsKey(dn))
66 return new AttributesDictionary(configurations.get(dn));
67 else
68 return null;
69 }
70
71 static Dictionary<String, Object> getStatePropertiesFromEnvironment(Function<String, String> getter) {
72 Hashtable<String, Object> props = new Hashtable<>();
73 // i18n
74 copyFrameworkProp(getter, NodeConstants.I18N_DEFAULT_LOCALE, props);
75 copyFrameworkProp(getter, NodeConstants.I18N_LOCALES, props);
76 // user admin
77 copyFrameworkProp(getter, NodeConstants.ROLES_URI, props);
78 copyFrameworkProp(getter, NodeConstants.USERADMIN_URIS, props);
79 // data
80 for (RepoConf repoConf : RepoConf.values())
81 copyFrameworkProp(getter, NodeConstants.NODE_REPO_PROP_PREFIX + repoConf.name(), props);
82 // TODO add other environment sources
83 return props;
84 }
85
86 static Dictionary<String, Object> getUserAdminPropertiesFromEnvironment(Function<String, String> getter) {
87 Hashtable<String, Object> props = new Hashtable<>();
88 copyFrameworkProp(getter, NodeConstants.ROLES_URI, props);
89 copyFrameworkProp(getter, NodeConstants.USERADMIN_URIS, props);
90 return props;
91 }
92
93 private static void copyFrameworkProp(Function<String, String> getter, String key,
94 Dictionary<String, Object> props) {
95 String value = getter.apply(key);
96 if (value != null)
97 props.put(key, value);
98 }
99
100 }