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
;
16 import org
.argeo
.cms
.internal
.osgi
.CmsActivator
;
18 /** Package utilities */
19 class KernelUtils
implements KernelConstants
{
20 final static String OSGI_INSTANCE_AREA
= "osgi.instance.area";
21 final static String OSGI_CONFIGURATION_AREA
= "osgi.configuration.area";
23 final static String OSGI_FRAMEWORK_UUID
= "org.osgi.framework.uuid";
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 Path
getOsgiConfigurationPath(String relativePath
) {
69 URI uri
= getOsgiConfigurationUri(relativePath
);
70 if (uri
== null) // no data area available
72 return Paths
.get(uri
);
75 public static URI
getOsgiInstanceUri(String relativePath
) {
76 String osgiInstanceBaseUri
= getFrameworkProp(OSGI_INSTANCE_AREA
);
77 if (osgiInstanceBaseUri
== null) // no data area available
80 if (!osgiInstanceBaseUri
.endsWith("/"))
81 osgiInstanceBaseUri
= osgiInstanceBaseUri
+ "/";
82 return safeUri(osgiInstanceBaseUri
+ (relativePath
!= null ? relativePath
: ""));
85 public static URI
getOsgiConfigurationUri(String relativePath
) {
86 String osgiInstanceBaseUri
= getFrameworkProp(OSGI_CONFIGURATION_AREA
);
87 if (osgiInstanceBaseUri
== null) // no data area available
90 if (!osgiInstanceBaseUri
.endsWith("/"))
91 osgiInstanceBaseUri
= osgiInstanceBaseUri
+ "/";
92 return safeUri(osgiInstanceBaseUri
+ (relativePath
!= null ? relativePath
: ""));
95 static String
getFrameworkProp(String key
, String def
) {
97 value
= CmsActivator
.getFrameworkProperty(key
);
99 // value = System.getProperty(key);
105 static String
getFrameworkProp(String key
) {
106 return getFrameworkProp(key
, null);
109 // static void logFrameworkProperties(CmsLog log) {
110 // for (Object sysProp : new TreeSet<Object>(System.getProperties().keySet())) {
111 // log.debug(sysProp + "=" + getFrameworkProp(sysProp.toString()));
115 static void printSystemProperties(PrintStream out
) {
116 TreeMap
<String
, String
> display
= new TreeMap
<>();
117 for (Object key
: System
.getProperties().keySet())
118 display
.put(key
.toString(), System
.getProperty(key
.toString()));
119 for (String key
: display
.keySet())
120 out
.println(key
+ "=" + display
.get(key
));
123 static boolean asBoolean(String value
) {
132 throw new IllegalArgumentException("Unsupported value for boolean attribute : " + value
);
136 private static URI
safeUri(String uri
) {
138 throw new IllegalArgumentException("URI cannot be null");
140 // FIXME does not work if URI contains illegal characters (such as spaces, etc.)
142 } catch (URISyntaxException e
) {
143 throw new IllegalArgumentException("Badly formatted URI " + uri
, e
);
147 private KernelUtils() {