1 package org
.argeo
.cms
.internal
.runtime
;
3 import java
.io
.IOException
;
4 import java
.io
.PrintStream
;
6 import java
.net
.URISyntaxException
;
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
;
17 import org
.argeo
.api
.cms
.CmsLog
;
18 import org
.argeo
.cms
.internal
.osgi
.CmsActivator
;
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";
25 static void setJaasConfiguration(URL jaasConfigurationUrl
) {
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
);
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
));
44 static Dictionary
<String
, ?
> asDictionary(ClassLoader cl
, String resource
) {
45 Properties props
= new Properties();
47 props
.load(cl
.getResourceAsStream(resource
));
48 } catch (IOException e
) {
49 throw new IllegalArgumentException("Cannot load " + resource
+ " from classpath", e
);
51 return asDictionary(props
);
54 static Path
getExecutionDir(String relativePath
) {
55 Path executionDir
= Paths
.get(getFrameworkProp("user.dir"));
56 if (relativePath
== null)
58 return executionDir
.resolve(relativePath
);
61 public static Path
getOsgiInstancePath(String relativePath
) {
62 URI uri
= getOsgiInstanceUri(relativePath
);
63 if (uri
== null) // no data area available
65 return Paths
.get(uri
);
68 public static URI
getOsgiInstanceUri(String relativePath
) {
69 String osgiInstanceBaseUri
= getFrameworkProp(OSGI_INSTANCE_AREA
);
70 if (osgiInstanceBaseUri
== null) // no data area available
73 if (!osgiInstanceBaseUri
.endsWith("/"))
74 osgiInstanceBaseUri
= osgiInstanceBaseUri
+ "/";
75 return safeUri(osgiInstanceBaseUri
+ (relativePath
!= null ? relativePath
: ""));
78 static String
getFrameworkProp(String key
, String def
) {
80 if (CmsActivator
.getBundleContext() != null)
81 value
= CmsActivator
.getBundleContext().getProperty(key
);
83 value
= System
.getProperty(key
);
89 static String
getFrameworkProp(String key
) {
90 return getFrameworkProp(key
, null);
93 static void logFrameworkProperties(CmsLog log
) {
94 for (Object sysProp
: new TreeSet
<Object
>(System
.getProperties().keySet())) {
95 log
.debug(sysProp
+ "=" + getFrameworkProp(sysProp
.toString()));
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
));
107 static boolean asBoolean(String value
) {
116 throw new IllegalArgumentException("Unsupported value for boolean attribute : " + value
);
120 private static URI
safeUri(String uri
) {
122 throw new IllegalArgumentException("URI cannot be null");
125 } catch (URISyntaxException e
) {
126 throw new IllegalArgumentException("Badly formatted URI " + uri
, e
);
130 private KernelUtils() {