1 package org
.argeo
.cms
.internal
.kernel
;
4 import java
.io
.IOException
;
5 import java
.io
.PrintStream
;
7 import java
.net
.URISyntaxException
;
9 import java
.nio
.file
.Path
;
10 import java
.nio
.file
.Paths
;
11 import java
.security
.URIParameter
;
12 import java
.util
.Dictionary
;
13 import java
.util
.Hashtable
;
14 import java
.util
.Properties
;
15 import java
.util
.TreeMap
;
16 import java
.util
.TreeSet
;
18 import org
.apache
.commons
.logging
.Log
;
19 import org
.argeo
.cms
.osgi
.DataModelNamespace
;
20 import org
.osgi
.framework
.BundleContext
;
21 import org
.osgi
.util
.tracker
.ServiceTracker
;
23 /** Package utilities */
24 class KernelUtils
implements KernelConstants
{
25 final static String OSGI_INSTANCE_AREA
= "osgi.instance.area";
26 final static String OSGI_CONFIGURATION_AREA
= "osgi.configuration.area";
28 static void setJaasConfiguration(URL jaasConfigurationUrl
) {
30 URIParameter uriParameter
= new URIParameter(jaasConfigurationUrl
.toURI());
31 javax
.security
.auth
.login
.Configuration jaasConfiguration
= javax
.security
.auth
.login
.Configuration
32 .getInstance("JavaLoginConfig", uriParameter
);
33 javax
.security
.auth
.login
.Configuration
.setConfiguration(jaasConfiguration
);
34 } catch (Exception e
) {
35 throw new IllegalArgumentException("Cannot set configuration " + jaasConfigurationUrl
, e
);
39 static Dictionary
<String
, ?
> asDictionary(Properties props
) {
40 Hashtable
<String
, Object
> hashtable
= new Hashtable
<String
, Object
>();
41 for (Object key
: props
.keySet()) {
42 hashtable
.put(key
.toString(), props
.get(key
));
47 static Dictionary
<String
, ?
> asDictionary(ClassLoader cl
, String resource
) {
48 Properties props
= new Properties();
50 props
.load(cl
.getResourceAsStream(resource
));
51 } catch (IOException e
) {
52 throw new IllegalArgumentException("Cannot load " + resource
+ " from classpath", e
);
54 return asDictionary(props
);
57 static File
getExecutionDir(String relativePath
) {
58 File executionDir
= new File(getFrameworkProp("user.dir"));
59 if (relativePath
== null)
62 return new File(executionDir
, relativePath
).getCanonicalFile();
63 } catch (IOException e
) {
64 throw new IllegalArgumentException("Cannot get canonical file", e
);
68 static File
getOsgiInstanceDir() {
69 return new File(getBundleContext().getProperty(OSGI_INSTANCE_AREA
).substring("file:".length()))
73 static Path
getOsgiInstancePath(String relativePath
) {
74 return Paths
.get(getOsgiInstanceUri(relativePath
));
77 static URI
getOsgiInstanceUri(String relativePath
) {
78 String osgiInstanceBaseUri
= getFrameworkProp(OSGI_INSTANCE_AREA
);
79 if (osgiInstanceBaseUri
!= null)
80 return safeUri(osgiInstanceBaseUri
+ (relativePath
!= null ? relativePath
: ""));
82 return Paths
.get(System
.getProperty("user.dir")).toUri();
85 static File
getOsgiConfigurationFile(String relativePath
) {
87 return new File(new URI(getBundleContext().getProperty(OSGI_CONFIGURATION_AREA
) + relativePath
))
89 } catch (Exception e
) {
90 throw new IllegalArgumentException("Cannot get configuration file for " + relativePath
, e
);
94 static String
getFrameworkProp(String key
, String def
) {
95 BundleContext bundleContext
= Activator
.getBundleContext();
97 if (bundleContext
!= null)
98 value
= bundleContext
.getProperty(key
);
100 value
= System
.getProperty(key
);
106 static String
getFrameworkProp(String key
) {
107 return getFrameworkProp(key
, null);
111 // static Subject anonymousLogin() {
112 // Subject subject = new Subject();
115 // lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject);
118 // } catch (LoginException e) {
119 // throw new CmsException("Cannot login as anonymous", e);
123 static void logFrameworkProperties(Log log
) {
124 BundleContext bc
= getBundleContext();
125 for (Object sysProp
: new TreeSet
<Object
>(System
.getProperties().keySet())) {
126 log
.debug(sysProp
+ "=" + bc
.getProperty(sysProp
.toString()));
128 // String[] keys = { Constants.FRAMEWORK_STORAGE,
129 // Constants.FRAMEWORK_OS_NAME, Constants.FRAMEWORK_OS_VERSION,
130 // Constants.FRAMEWORK_PROCESSOR, Constants.FRAMEWORK_SECURITY,
131 // Constants.FRAMEWORK_TRUST_REPOSITORIES,
132 // Constants.FRAMEWORK_WINDOWSYSTEM, Constants.FRAMEWORK_VENDOR,
133 // Constants.FRAMEWORK_VERSION, Constants.FRAMEWORK_STORAGE_CLEAN,
134 // Constants.FRAMEWORK_LANGUAGE, Constants.FRAMEWORK_UUID };
135 // for (String key : keys)
136 // log.debug(key + "=" + bc.getProperty(key));
139 static void printSystemProperties(PrintStream out
) {
140 TreeMap
<String
, String
> display
= new TreeMap
<>();
141 for (Object key
: System
.getProperties().keySet())
142 display
.put(key
.toString(), System
.getProperty(key
.toString()));
143 for (String key
: display
.keySet())
144 out
.println(key
+ "=" + display
.get(key
));
147 // static Session openAdminSession(Repository repository) {
148 // return openAdminSession(repository, null);
151 // static Session openAdminSession(final Repository repository, final String workspaceName) {
152 // LoginContext loginContext = loginAsDataAdmin();
153 // return Subject.doAs(loginContext.getSubject(), new PrivilegedAction<Session>() {
156 // public Session run() {
158 // return repository.login(workspaceName);
159 // } catch (RepositoryException e) {
160 // throw new IllegalStateException("Cannot open admin session", e);
163 // loginContext.logout();
164 // } catch (LoginException e) {
165 // throw new IllegalStateException(e);
173 // static LoginContext loginAsDataAdmin() {
174 // ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
175 // Thread.currentThread().setContextClassLoader(KernelUtils.class.getClassLoader());
176 // LoginContext loginContext;
178 // loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_DATA_ADMIN);
179 // loginContext.login();
180 // } catch (LoginException e1) {
181 // throw new IllegalStateException("Could not login as data admin", e1);
183 // Thread.currentThread().setContextClassLoader(currentCl);
185 // return loginContext;
188 // static void doAsDataAdmin(Runnable action) {
189 // LoginContext loginContext = loginAsDataAdmin();
190 // Subject.doAs(loginContext.getSubject(), new PrivilegedAction<Void>() {
193 // public Void run() {
199 // loginContext.logout();
200 // } catch (LoginException e) {
201 // throw new IllegalStateException(e);
209 static void asyncOpen(ServiceTracker
<?
, ?
> st
) {
210 Runnable run
= new Runnable() {
217 Activator
.getInternalExecutorService().execute(run
);
218 // new Thread(run, "Open service tracker " + st).start();
221 static BundleContext
getBundleContext() {
222 return Activator
.getBundleContext();
225 static boolean asBoolean(String value
) {
234 throw new IllegalArgumentException(
235 "Unsupported value for attribute " + DataModelNamespace
.ABSTRACT
+ ": " + value
);
239 private static URI
safeUri(String uri
) {
241 throw new IllegalArgumentException("URI cannot be null");
244 } catch (URISyntaxException e
) {
245 throw new IllegalArgumentException("Badly formatted URI " + uri
, e
);
249 private KernelUtils() {