]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/runtime/KernelUtils.java
Improve ACR attribute typing.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / 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 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 Path getOsgiConfigurationPath(String relativePath) {
69 URI uri = getOsgiConfigurationUri(relativePath);
70 if (uri == null) // no data area available
71 return null;
72 return Paths.get(uri);
73 }
74
75 public static URI getOsgiInstanceUri(String relativePath) {
76 String osgiInstanceBaseUri = getFrameworkProp(OSGI_INSTANCE_AREA);
77 if (osgiInstanceBaseUri == null) // no data area available
78 return null;
79
80 if (!osgiInstanceBaseUri.endsWith("/"))
81 osgiInstanceBaseUri = osgiInstanceBaseUri + "/";
82 return safeUri(osgiInstanceBaseUri + (relativePath != null ? relativePath : ""));
83 }
84
85 public static URI getOsgiConfigurationUri(String relativePath) {
86 String osgiInstanceBaseUri = getFrameworkProp(OSGI_CONFIGURATION_AREA);
87 if (osgiInstanceBaseUri == null) // no data area available
88 return null;
89
90 if (!osgiInstanceBaseUri.endsWith("/"))
91 osgiInstanceBaseUri = osgiInstanceBaseUri + "/";
92 return safeUri(osgiInstanceBaseUri + (relativePath != null ? relativePath : ""));
93 }
94
95 static String getFrameworkProp(String key, String def) {
96 String value;
97 if (CmsActivator.getBundleContext() != null)
98 value = CmsActivator.getBundleContext().getProperty(key);
99 else
100 value = System.getProperty(key);
101 if (value == null)
102 return def;
103 return value;
104 }
105
106 static String getFrameworkProp(String key) {
107 return getFrameworkProp(key, null);
108 }
109
110 static void logFrameworkProperties(CmsLog log) {
111 for (Object sysProp : new TreeSet<Object>(System.getProperties().keySet())) {
112 log.debug(sysProp + "=" + getFrameworkProp(sysProp.toString()));
113 }
114 }
115
116 static void printSystemProperties(PrintStream out) {
117 TreeMap<String, String> display = new TreeMap<>();
118 for (Object key : System.getProperties().keySet())
119 display.put(key.toString(), System.getProperty(key.toString()));
120 for (String key : display.keySet())
121 out.println(key + "=" + display.get(key));
122 }
123
124 static boolean asBoolean(String value) {
125 if (value == null)
126 return false;
127 switch (value) {
128 case "true":
129 return true;
130 case "false":
131 return false;
132 default:
133 throw new IllegalArgumentException("Unsupported value for boolean attribute : " + value);
134 }
135 }
136
137 private static URI safeUri(String uri) {
138 if (uri == null)
139 throw new IllegalArgumentException("URI cannot be null");
140 try {
141 return new URI(uri);
142 } catch (URISyntaxException e) {
143 throw new IllegalArgumentException("Badly formatted URI " + uri, e);
144 }
145 }
146
147 private KernelUtils() {
148
149 }
150 }