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 final static String OSGI_FRAMEWORK_UUID
= "org.osgi.framework.uuid";
27 static void setJaasConfiguration(URL jaasConfigurationUrl
) {
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
);
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
));
46 static Dictionary
<String
, ?
> asDictionary(ClassLoader cl
, String resource
) {
47 Properties props
= new Properties();
49 props
.load(cl
.getResourceAsStream(resource
));
50 } catch (IOException e
) {
51 throw new IllegalArgumentException("Cannot load " + resource
+ " from classpath", e
);
53 return asDictionary(props
);
56 static Path
getExecutionDir(String relativePath
) {
57 Path executionDir
= Paths
.get(getFrameworkProp("user.dir"));
58 if (relativePath
== null)
60 return executionDir
.resolve(relativePath
);
63 public static Path
getOsgiInstancePath(String relativePath
) {
64 URI uri
= getOsgiInstanceUri(relativePath
);
65 if (uri
== null) // no data area available
67 return Paths
.get(uri
);
70 public static Path
getOsgiConfigurationPath(String relativePath
) {
71 URI uri
= getOsgiConfigurationUri(relativePath
);
72 if (uri
== null) // no data area available
74 return Paths
.get(uri
);
77 public static URI
getOsgiInstanceUri(String relativePath
) {
78 String osgiInstanceBaseUri
= getFrameworkProp(OSGI_INSTANCE_AREA
);
79 if (osgiInstanceBaseUri
== null) // no data area available
82 if (!osgiInstanceBaseUri
.endsWith("/"))
83 osgiInstanceBaseUri
= osgiInstanceBaseUri
+ "/";
84 return safeUri(osgiInstanceBaseUri
+ (relativePath
!= null ? relativePath
: ""));
87 public static URI
getOsgiConfigurationUri(String relativePath
) {
88 String osgiInstanceBaseUri
= getFrameworkProp(OSGI_CONFIGURATION_AREA
);
89 if (osgiInstanceBaseUri
== null) // no data area available
92 if (!osgiInstanceBaseUri
.endsWith("/"))
93 osgiInstanceBaseUri
= osgiInstanceBaseUri
+ "/";
94 return safeUri(osgiInstanceBaseUri
+ (relativePath
!= null ? relativePath
: ""));
97 static String
getFrameworkProp(String key
, String def
) {
99 value
= CmsActivator
.getFrameworkProperty(key
);
101 value
= System
.getProperty(key
);
107 static String
getFrameworkProp(String key
) {
108 return getFrameworkProp(key
, null);
111 static void logFrameworkProperties(CmsLog log
) {
112 for (Object sysProp
: new TreeSet
<Object
>(System
.getProperties().keySet())) {
113 log
.debug(sysProp
+ "=" + getFrameworkProp(sysProp
.toString()));
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
));
125 static boolean asBoolean(String value
) {
134 throw new IllegalArgumentException("Unsupported value for boolean attribute : " + value
);
138 private static URI
safeUri(String uri
) {
140 throw new IllegalArgumentException("URI cannot be null");
142 // FIXME does not work if URI contains illegal characters (such as spaces, etc.)
144 } catch (URISyntaxException e
) {
145 throw new IllegalArgumentException("Badly formatted URI " + uri
, e
);
149 private KernelUtils() {