]> git.argeo.org Git - lgpl/argeo-commons.git/blob - internal/runtime/KernelUtils.java
Prepare next development cycle
[lgpl/argeo-commons.git] / 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 static void setJaasConfiguration(URL jaasConfigurationUrl) {
26 try {
27 URIParameter uriParameter = new URIParameter(jaasConfigurationUrl.toURI());
28 javax.security.auth.login.Configuration jaasConfiguration = javax.security.auth.login.Configuration
29 .getInstance("JavaLoginConfig", uriParameter);
30 javax.security.auth.login.Configuration.setConfiguration(jaasConfiguration);
31 } catch (Exception e) {
32 throw new IllegalArgumentException("Cannot set configuration " + jaasConfigurationUrl, e);
33 }
34 }
35
36 static Dictionary<String, ?> asDictionary(Properties props) {
37 Hashtable<String, Object> hashtable = new Hashtable<String, Object>();
38 for (Object key : props.keySet()) {
39 hashtable.put(key.toString(), props.get(key));
40 }
41 return hashtable;
42 }
43
44 static Dictionary<String, ?> asDictionary(ClassLoader cl, String resource) {
45 Properties props = new Properties();
46 try {
47 props.load(cl.getResourceAsStream(resource));
48 } catch (IOException e) {
49 throw new IllegalArgumentException("Cannot load " + resource + " from classpath", e);
50 }
51 return asDictionary(props);
52 }
53
54 static Path getExecutionDir(String relativePath) {
55 Path executionDir = Paths.get(getFrameworkProp("user.dir"));
56 if (relativePath == null)
57 return executionDir;
58 return executionDir.resolve(relativePath);
59 }
60
61 public static Path getOsgiInstancePath(String relativePath) {
62 URI uri = getOsgiInstanceUri(relativePath);
63 if (uri == null) // no data area available
64 return null;
65 return Paths.get(uri);
66 }
67
68 public static URI getOsgiInstanceUri(String relativePath) {
69 String osgiInstanceBaseUri = getFrameworkProp(OSGI_INSTANCE_AREA);
70 if (osgiInstanceBaseUri == null) // no data area available
71 return null;
72
73 if (!osgiInstanceBaseUri.endsWith("/"))
74 osgiInstanceBaseUri = osgiInstanceBaseUri + "/";
75 return safeUri(osgiInstanceBaseUri + (relativePath != null ? relativePath : ""));
76 }
77
78 static String getFrameworkProp(String key, String def) {
79 String value;
80 if (CmsActivator.getBundleContext() != null)
81 value = CmsActivator.getBundleContext().getProperty(key);
82 else
83 value = System.getProperty(key);
84 if (value == null)
85 return def;
86 return value;
87 }
88
89 static String getFrameworkProp(String key) {
90 return getFrameworkProp(key, null);
91 }
92
93 static void logFrameworkProperties(CmsLog log) {
94 for (Object sysProp : new TreeSet<Object>(System.getProperties().keySet())) {
95 log.debug(sysProp + "=" + getFrameworkProp(sysProp.toString()));
96 }
97 }
98
99 static void printSystemProperties(PrintStream out) {
100 TreeMap<String, String> display = new TreeMap<>();
101 for (Object key : System.getProperties().keySet())
102 display.put(key.toString(), System.getProperty(key.toString()));
103 for (String key : display.keySet())
104 out.println(key + "=" + display.get(key));
105 }
106
107 static boolean asBoolean(String value) {
108 if (value == null)
109 return false;
110 switch (value) {
111 case "true":
112 return true;
113 case "false":
114 return false;
115 default:
116 throw new IllegalArgumentException("Unsupported value for boolean attribute : " + value);
117 }
118 }
119
120 private static URI safeUri(String uri) {
121 if (uri == null)
122 throw new IllegalArgumentException("URI cannot be null");
123 try {
124 return new URI(uri);
125 } catch (URISyntaxException e) {
126 throw new IllegalArgumentException("Badly formatted URI " + uri, e);
127 }
128 }
129
130 private KernelUtils() {
131
132 }
133 }