]> git.argeo.org Git - lgpl/argeo-commons.git/blob - cms/internal/runtime/KernelUtils.java
Prepare next development cycle
[lgpl/argeo-commons.git] / cms / internal / runtime / KernelUtils.java
1 package org.argeo.cms.internal.runtime;
2
3 import java.io.IOException;
4 import java.io.PrintStream;
5 import java.net.URI;
6 import java.net.URISyntaxException;
7 import java.net.URL;
8 import java.nio.file.Path;
9 import java.nio.file.Paths;
10 import java.security.URIParameter;
11 import java.util.Dictionary;
12 import java.util.Hashtable;
13 import java.util.Properties;
14 import java.util.TreeMap;
15 import java.util.TreeSet;
16
17 import org.argeo.api.cms.CmsLog;
18 import org.argeo.cms.internal.osgi.CmsActivator;
19
20 /** Package utilities */
21 class KernelUtils implements KernelConstants {
22 final static String OSGI_INSTANCE_AREA = "osgi.instance.area";
23 final static String OSGI_CONFIGURATION_AREA = "osgi.configuration.area";
24
25 final static String OSGI_FRAMEWORK_UUID = "org.osgi.framework.uuid";
26
27 static void setJaasConfiguration(URL jaasConfigurationUrl) {
28 try {
29 URIParameter uriParameter = new URIParameter(jaasConfigurationUrl.toURI());
30 javax.security.auth.login.Configuration jaasConfiguration = javax.security.auth.login.Configuration
31 .getInstance("JavaLoginConfig", uriParameter);
32 javax.security.auth.login.Configuration.setConfiguration(jaasConfiguration);
33 } catch (Exception e) {
34 throw new IllegalArgumentException("Cannot set configuration " + jaasConfigurationUrl, e);
35 }
36 }
37
38 static Dictionary<String, ?> asDictionary(Properties props) {
39 Hashtable<String, Object> hashtable = new Hashtable<String, Object>();
40 for (Object key : props.keySet()) {
41 hashtable.put(key.toString(), props.get(key));
42 }
43 return hashtable;
44 }
45
46 static Dictionary<String, ?> asDictionary(ClassLoader cl, String resource) {
47 Properties props = new Properties();
48 try {
49 props.load(cl.getResourceAsStream(resource));
50 } catch (IOException e) {
51 throw new IllegalArgumentException("Cannot load " + resource + " from classpath", e);
52 }
53 return asDictionary(props);
54 }
55
56 static Path getExecutionDir(String relativePath) {
57 Path executionDir = Paths.get(getFrameworkProp("user.dir"));
58 if (relativePath == null)
59 return executionDir;
60 return executionDir.resolve(relativePath);
61 }
62
63 public static Path getOsgiInstancePath(String relativePath) {
64 URI uri = getOsgiInstanceUri(relativePath);
65 if (uri == null) // no data area available
66 return null;
67 return Paths.get(uri);
68 }
69
70 public static Path getOsgiConfigurationPath(String relativePath) {
71 URI uri = getOsgiConfigurationUri(relativePath);
72 if (uri == null) // no data area available
73 return null;
74 return Paths.get(uri);
75 }
76
77 public static URI getOsgiInstanceUri(String relativePath) {
78 String osgiInstanceBaseUri = getFrameworkProp(OSGI_INSTANCE_AREA);
79 if (osgiInstanceBaseUri == null) // no data area available
80 return null;
81
82 if (!osgiInstanceBaseUri.endsWith("/"))
83 osgiInstanceBaseUri = osgiInstanceBaseUri + "/";
84 return safeUri(osgiInstanceBaseUri + (relativePath != null ? relativePath : ""));
85 }
86
87 public static URI getOsgiConfigurationUri(String relativePath) {
88 String osgiInstanceBaseUri = getFrameworkProp(OSGI_CONFIGURATION_AREA);
89 if (osgiInstanceBaseUri == null) // no data area available
90 return null;
91
92 if (!osgiInstanceBaseUri.endsWith("/"))
93 osgiInstanceBaseUri = osgiInstanceBaseUri + "/";
94 return safeUri(osgiInstanceBaseUri + (relativePath != null ? relativePath : ""));
95 }
96
97 static String getFrameworkProp(String key, String def) {
98 String value;
99 value = CmsActivator.getFrameworkProperty(key);
100 if (value == null)
101 value = System.getProperty(key);
102 if (value == null)
103 return def;
104 return value;
105 }
106
107 static String getFrameworkProp(String key) {
108 return getFrameworkProp(key, null);
109 }
110
111 static void logFrameworkProperties(CmsLog log) {
112 for (Object sysProp : new TreeSet<Object>(System.getProperties().keySet())) {
113 log.debug(sysProp + "=" + getFrameworkProp(sysProp.toString()));
114 }
115 }
116
117 static void printSystemProperties(PrintStream out) {
118 TreeMap<String, String> display = new TreeMap<>();
119 for (Object key : System.getProperties().keySet())
120 display.put(key.toString(), System.getProperty(key.toString()));
121 for (String key : display.keySet())
122 out.println(key + "=" + display.get(key));
123 }
124
125 static boolean asBoolean(String value) {
126 if (value == null)
127 return false;
128 switch (value) {
129 case "true":
130 return true;
131 case "false":
132 return false;
133 default:
134 throw new IllegalArgumentException("Unsupported value for boolean attribute : " + value);
135 }
136 }
137
138 private static URI safeUri(String uri) {
139 if (uri == null)
140 throw new IllegalArgumentException("URI cannot be null");
141 try {
142 // FIXME does not work if URI contains illegal characters (such as spaces, etc.)
143 return new URI(uri);
144 } catch (URISyntaxException e) {
145 throw new IllegalArgumentException("Badly formatted URI " + uri, e);
146 }
147 }
148
149 private KernelUtils() {
150
151 }
152 }