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
.PrivilegedAction
;
12 import java
.security
.URIParameter
;
13 import java
.util
.Dictionary
;
14 import java
.util
.Hashtable
;
15 import java
.util
.Properties
;
16 import java
.util
.TreeMap
;
17 import java
.util
.TreeSet
;
19 import javax
.jcr
.Repository
;
20 import javax
.jcr
.RepositoryException
;
21 import javax
.jcr
.Session
;
22 import javax
.security
.auth
.Subject
;
23 import javax
.security
.auth
.login
.LoginContext
;
24 import javax
.security
.auth
.login
.LoginException
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.argeo
.api
.DataModelNamespace
;
28 import org
.argeo
.api
.NodeConstants
;
29 import org
.osgi
.framework
.BundleContext
;
30 import org
.osgi
.util
.tracker
.ServiceTracker
;
32 /** Package utilities */
33 class KernelUtils
implements KernelConstants
{
34 final static String OSGI_INSTANCE_AREA
= "osgi.instance.area";
35 final static String OSGI_CONFIGURATION_AREA
= "osgi.configuration.area";
37 static void setJaasConfiguration(URL jaasConfigurationUrl
) {
39 URIParameter uriParameter
= new URIParameter(jaasConfigurationUrl
.toURI());
40 javax
.security
.auth
.login
.Configuration jaasConfiguration
= javax
.security
.auth
.login
.Configuration
41 .getInstance("JavaLoginConfig", uriParameter
);
42 javax
.security
.auth
.login
.Configuration
.setConfiguration(jaasConfiguration
);
43 } catch (Exception e
) {
44 throw new IllegalArgumentException("Cannot set configuration " + jaasConfigurationUrl
, e
);
48 static Dictionary
<String
, ?
> asDictionary(Properties props
) {
49 Hashtable
<String
, Object
> hashtable
= new Hashtable
<String
, Object
>();
50 for (Object key
: props
.keySet()) {
51 hashtable
.put(key
.toString(), props
.get(key
));
56 static Dictionary
<String
, ?
> asDictionary(ClassLoader cl
, String resource
) {
57 Properties props
= new Properties();
59 props
.load(cl
.getResourceAsStream(resource
));
60 } catch (IOException e
) {
61 throw new IllegalArgumentException("Cannot load " + resource
+ " from classpath", e
);
63 return asDictionary(props
);
66 static File
getExecutionDir(String relativePath
) {
67 File executionDir
= new File(getFrameworkProp("user.dir"));
68 if (relativePath
== null)
71 return new File(executionDir
, relativePath
).getCanonicalFile();
72 } catch (IOException e
) {
73 throw new IllegalArgumentException("Cannot get canonical file", e
);
77 static File
getOsgiInstanceDir() {
78 return new File(getBundleContext().getProperty(OSGI_INSTANCE_AREA
).substring("file:".length()))
82 static Path
getOsgiInstancePath(String relativePath
) {
83 return Paths
.get(getOsgiInstanceUri(relativePath
));
86 static URI
getOsgiInstanceUri(String relativePath
) {
87 String osgiInstanceBaseUri
= getFrameworkProp(OSGI_INSTANCE_AREA
);
88 if (osgiInstanceBaseUri
!= null)
89 return safeUri(osgiInstanceBaseUri
+ (relativePath
!= null ? relativePath
: ""));
91 return Paths
.get(System
.getProperty("user.dir")).toUri();
94 static File
getOsgiConfigurationFile(String relativePath
) {
96 return new File(new URI(getBundleContext().getProperty(OSGI_CONFIGURATION_AREA
) + relativePath
))
98 } catch (Exception e
) {
99 throw new IllegalArgumentException("Cannot get configuration file for " + relativePath
, e
);
103 static String
getFrameworkProp(String key
, String def
) {
104 BundleContext bundleContext
= Activator
.getBundleContext();
106 if (bundleContext
!= null)
107 value
= bundleContext
.getProperty(key
);
109 value
= System
.getProperty(key
);
115 static String
getFrameworkProp(String key
) {
116 return getFrameworkProp(key
, null);
120 // static Subject anonymousLogin() {
121 // Subject subject = new Subject();
124 // lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject);
127 // } catch (LoginException e) {
128 // throw new CmsException("Cannot login as anonymous", e);
132 static void logFrameworkProperties(Log log
) {
133 BundleContext bc
= getBundleContext();
134 for (Object sysProp
: new TreeSet
<Object
>(System
.getProperties().keySet())) {
135 log
.debug(sysProp
+ "=" + bc
.getProperty(sysProp
.toString()));
137 // String[] keys = { Constants.FRAMEWORK_STORAGE,
138 // Constants.FRAMEWORK_OS_NAME, Constants.FRAMEWORK_OS_VERSION,
139 // Constants.FRAMEWORK_PROCESSOR, Constants.FRAMEWORK_SECURITY,
140 // Constants.FRAMEWORK_TRUST_REPOSITORIES,
141 // Constants.FRAMEWORK_WINDOWSYSTEM, Constants.FRAMEWORK_VENDOR,
142 // Constants.FRAMEWORK_VERSION, Constants.FRAMEWORK_STORAGE_CLEAN,
143 // Constants.FRAMEWORK_LANGUAGE, Constants.FRAMEWORK_UUID };
144 // for (String key : keys)
145 // log.debug(key + "=" + bc.getProperty(key));
148 static void printSystemProperties(PrintStream out
) {
149 TreeMap
<String
, String
> display
= new TreeMap
<>();
150 for (Object key
: System
.getProperties().keySet())
151 display
.put(key
.toString(), System
.getProperty(key
.toString()));
152 for (String key
: display
.keySet())
153 out
.println(key
+ "=" + display
.get(key
));
156 static Session
openAdminSession(Repository repository
) {
157 return openAdminSession(repository
, null);
160 static Session
openAdminSession(final Repository repository
, final String workspaceName
) {
161 LoginContext loginContext
= loginAsDataAdmin();
162 return Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Session
>() {
165 public Session
run() {
167 return repository
.login(workspaceName
);
168 } catch (RepositoryException e
) {
169 throw new IllegalStateException("Cannot open admin session", e
);
172 loginContext
.logout();
173 } catch (LoginException e
) {
174 throw new IllegalStateException(e
);
182 static LoginContext
loginAsDataAdmin() {
183 ClassLoader currentCl
= Thread
.currentThread().getContextClassLoader();
184 Thread
.currentThread().setContextClassLoader(KernelUtils
.class.getClassLoader());
185 LoginContext loginContext
;
187 loginContext
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_DATA_ADMIN
);
188 loginContext
.login();
189 } catch (LoginException e1
) {
190 throw new IllegalStateException("Could not login as data admin", e1
);
192 Thread
.currentThread().setContextClassLoader(currentCl
);
197 static void doAsDataAdmin(Runnable action
) {
198 LoginContext loginContext
= loginAsDataAdmin();
199 Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Void
>() {
208 loginContext
.logout();
209 } catch (LoginException e
) {
210 throw new IllegalStateException(e
);
218 static void asyncOpen(ServiceTracker
<?
, ?
> st
) {
219 Runnable run
= new Runnable() {
226 Activator
.getInternalExecutorService().execute(run
);
227 // new Thread(run, "Open service tracker " + st).start();
230 static BundleContext
getBundleContext() {
231 return Activator
.getBundleContext();
234 static boolean asBoolean(String value
) {
243 throw new IllegalArgumentException(
244 "Unsupported value for attribute " + DataModelNamespace
.ABSTRACT
+ ": " + value
);
248 private static URI
safeUri(String uri
) {
250 throw new IllegalArgumentException("URI cannot be null");
253 } catch (URISyntaxException e
) {
254 throw new IllegalArgumentException("Badly formatted URI " + uri
, e
);
258 private KernelUtils() {