1 package org
.argeo
.cms
.jcr
.internal
;
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
.argeo
.cms
.jcr
.internal
.osgi
.CmsJcrActivator
;
30 import org
.osgi
.framework
.BundleContext
;
31 import org
.osgi
.util
.tracker
.ServiceTracker
;
33 /** Package utilities */
34 class KernelUtils
implements KernelConstants
{
35 final static String OSGI_INSTANCE_AREA
= "osgi.instance.area";
36 final static String OSGI_CONFIGURATION_AREA
= "osgi.configuration.area";
38 static void setJaasConfiguration(URL jaasConfigurationUrl
) {
40 URIParameter uriParameter
= new URIParameter(jaasConfigurationUrl
.toURI());
41 javax
.security
.auth
.login
.Configuration jaasConfiguration
= javax
.security
.auth
.login
.Configuration
42 .getInstance("JavaLoginConfig", uriParameter
);
43 javax
.security
.auth
.login
.Configuration
.setConfiguration(jaasConfiguration
);
44 } catch (Exception e
) {
45 throw new IllegalArgumentException("Cannot set configuration " + jaasConfigurationUrl
, e
);
49 static Dictionary
<String
, ?
> asDictionary(Properties props
) {
50 Hashtable
<String
, Object
> hashtable
= new Hashtable
<String
, Object
>();
51 for (Object key
: props
.keySet()) {
52 hashtable
.put(key
.toString(), props
.get(key
));
57 static Dictionary
<String
, ?
> asDictionary(ClassLoader cl
, String resource
) {
58 Properties props
= new Properties();
60 props
.load(cl
.getResourceAsStream(resource
));
61 } catch (IOException e
) {
62 throw new IllegalArgumentException("Cannot load " + resource
+ " from classpath", e
);
64 return asDictionary(props
);
67 static File
getExecutionDir(String relativePath
) {
68 File executionDir
= new File(getFrameworkProp("user.dir"));
69 if (relativePath
== null)
72 return new File(executionDir
, relativePath
).getCanonicalFile();
73 } catch (IOException e
) {
74 throw new IllegalArgumentException("Cannot get canonical file", e
);
78 static File
getOsgiInstanceDir() {
79 return new File(getBundleContext().getProperty(OSGI_INSTANCE_AREA
).substring("file:".length()))
83 static Path
getOsgiInstancePath(String relativePath
) {
84 return Paths
.get(getOsgiInstanceUri(relativePath
));
87 static URI
getOsgiInstanceUri(String relativePath
) {
88 String osgiInstanceBaseUri
= getFrameworkProp(OSGI_INSTANCE_AREA
);
89 if (osgiInstanceBaseUri
!= null)
90 return safeUri(osgiInstanceBaseUri
+ (relativePath
!= null ? relativePath
: ""));
92 return Paths
.get(System
.getProperty("user.dir")).toUri();
95 static File
getOsgiConfigurationFile(String relativePath
) {
97 return new File(new URI(getBundleContext().getProperty(OSGI_CONFIGURATION_AREA
) + relativePath
))
99 } catch (Exception e
) {
100 throw new IllegalArgumentException("Cannot get configuration file for " + relativePath
, e
);
104 static String
getFrameworkProp(String key
, String def
) {
105 BundleContext bundleContext
= CmsJcrActivator
.getBundleContext();
107 if (bundleContext
!= null)
108 value
= bundleContext
.getProperty(key
);
110 value
= System
.getProperty(key
);
116 static String
getFrameworkProp(String key
) {
117 return getFrameworkProp(key
, null);
121 // static Subject anonymousLogin() {
122 // Subject subject = new Subject();
125 // lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject);
128 // } catch (LoginException e) {
129 // throw new CmsException("Cannot login as anonymous", e);
133 static void logFrameworkProperties(Log log
) {
134 BundleContext bc
= getBundleContext();
135 for (Object sysProp
: new TreeSet
<Object
>(System
.getProperties().keySet())) {
136 log
.debug(sysProp
+ "=" + bc
.getProperty(sysProp
.toString()));
138 // String[] keys = { Constants.FRAMEWORK_STORAGE,
139 // Constants.FRAMEWORK_OS_NAME, Constants.FRAMEWORK_OS_VERSION,
140 // Constants.FRAMEWORK_PROCESSOR, Constants.FRAMEWORK_SECURITY,
141 // Constants.FRAMEWORK_TRUST_REPOSITORIES,
142 // Constants.FRAMEWORK_WINDOWSYSTEM, Constants.FRAMEWORK_VENDOR,
143 // Constants.FRAMEWORK_VERSION, Constants.FRAMEWORK_STORAGE_CLEAN,
144 // Constants.FRAMEWORK_LANGUAGE, Constants.FRAMEWORK_UUID };
145 // for (String key : keys)
146 // log.debug(key + "=" + bc.getProperty(key));
149 static void printSystemProperties(PrintStream out
) {
150 TreeMap
<String
, String
> display
= new TreeMap
<>();
151 for (Object key
: System
.getProperties().keySet())
152 display
.put(key
.toString(), System
.getProperty(key
.toString()));
153 for (String key
: display
.keySet())
154 out
.println(key
+ "=" + display
.get(key
));
157 static Session
openAdminSession(Repository repository
) {
158 return openAdminSession(repository
, null);
161 static Session
openAdminSession(final Repository repository
, final String workspaceName
) {
162 LoginContext loginContext
= loginAsDataAdmin();
163 return Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Session
>() {
166 public Session
run() {
168 return repository
.login(workspaceName
);
169 } catch (RepositoryException e
) {
170 throw new IllegalStateException("Cannot open admin session", e
);
173 loginContext
.logout();
174 } catch (LoginException e
) {
175 throw new IllegalStateException(e
);
183 static LoginContext
loginAsDataAdmin() {
184 ClassLoader currentCl
= Thread
.currentThread().getContextClassLoader();
185 Thread
.currentThread().setContextClassLoader(KernelUtils
.class.getClassLoader());
186 LoginContext loginContext
;
188 loginContext
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_DATA_ADMIN
);
189 loginContext
.login();
190 } catch (LoginException e1
) {
191 throw new IllegalStateException("Could not login as data admin", e1
);
193 Thread
.currentThread().setContextClassLoader(currentCl
);
198 static void doAsDataAdmin(Runnable action
) {
199 LoginContext loginContext
= loginAsDataAdmin();
200 Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Void
>() {
209 loginContext
.logout();
210 } catch (LoginException e
) {
211 throw new IllegalStateException(e
);
219 static void asyncOpen(ServiceTracker
<?
, ?
> st
) {
220 Runnable run
= new Runnable() {
227 // Activator.getInternalExecutorService().execute(run);
228 new Thread(run
, "Open service tracker " + st
).start();
231 static BundleContext
getBundleContext() {
232 return CmsJcrActivator
.getBundleContext();
235 static boolean asBoolean(String value
) {
244 throw new IllegalArgumentException(
245 "Unsupported value for attribute " + DataModelNamespace
.ABSTRACT
+ ": " + value
);
249 private static URI
safeUri(String uri
) {
251 throw new IllegalArgumentException("URI cannot be null");
254 } catch (URISyntaxException e
) {
255 throw new IllegalArgumentException("Badly formatted URI " + uri
, e
);
259 private KernelUtils() {