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
.argeo
.cms
.CmsException
;
30 import org
.osgi
.framework
.Bundle
;
31 import org
.osgi
.framework
.BundleContext
;
32 import org
.osgi
.util
.tracker
.ServiceTracker
;
34 /** Package utilities */
35 class KernelUtils
implements KernelConstants
{
36 final static String OSGI_INSTANCE_AREA
= "osgi.instance.area";
37 final static String OSGI_CONFIGURATION_AREA
= "osgi.configuration.area";
39 static void setJaasConfiguration(URL jaasConfigurationUrl
) {
41 URIParameter uriParameter
= new URIParameter(jaasConfigurationUrl
.toURI());
42 javax
.security
.auth
.login
.Configuration jaasConfiguration
= javax
.security
.auth
.login
.Configuration
43 .getInstance("JavaLoginConfig", uriParameter
);
44 javax
.security
.auth
.login
.Configuration
.setConfiguration(jaasConfiguration
);
45 } catch (Exception e
) {
46 throw new IllegalArgumentException("Cannot set configuration " + jaasConfigurationUrl
, e
);
50 static Dictionary
<String
, ?
> asDictionary(Properties props
) {
51 Hashtable
<String
, Object
> hashtable
= new Hashtable
<String
, Object
>();
52 for (Object key
: props
.keySet()) {
53 hashtable
.put(key
.toString(), props
.get(key
));
58 static Dictionary
<String
, ?
> asDictionary(ClassLoader cl
, String resource
) {
59 Properties props
= new Properties();
61 props
.load(cl
.getResourceAsStream(resource
));
62 } catch (IOException e
) {
63 throw new IllegalArgumentException("Cannot load " + resource
+ " from classpath", e
);
65 return asDictionary(props
);
68 static File
getExecutionDir(String relativePath
) {
69 File executionDir
= new File(getFrameworkProp("user.dir"));
70 if (relativePath
== null)
73 return new File(executionDir
, relativePath
).getCanonicalFile();
74 } catch (IOException e
) {
75 throw new IllegalArgumentException("Cannot get canonical file", e
);
79 static File
getOsgiInstanceDir() {
80 return new File(getBundleContext().getProperty(OSGI_INSTANCE_AREA
).substring("file:".length()))
84 static Path
getOsgiInstancePath(String relativePath
) {
85 return Paths
.get(getOsgiInstanceUri(relativePath
));
88 static URI
getOsgiInstanceUri(String relativePath
) {
89 String osgiInstanceBaseUri
= getFrameworkProp(OSGI_INSTANCE_AREA
);
90 if (osgiInstanceBaseUri
!= null)
91 return safeUri(osgiInstanceBaseUri
+ (relativePath
!= null ? relativePath
: ""));
93 return Paths
.get(System
.getProperty("user.dir")).toUri();
96 static File
getOsgiConfigurationFile(String relativePath
) {
98 return new File(new URI(getBundleContext().getProperty(OSGI_CONFIGURATION_AREA
) + relativePath
))
100 } catch (Exception e
) {
101 throw new IllegalArgumentException("Cannot get configuration file for " + relativePath
, e
);
105 static String
getFrameworkProp(String key
, String def
) {
106 BundleContext bundleContext
= Activator
.getBundleContext();
108 if (bundleContext
!= null)
109 value
= bundleContext
.getProperty(key
);
111 value
= System
.getProperty(key
);
117 static String
getFrameworkProp(String key
) {
118 return getFrameworkProp(key
, null);
122 // static Subject anonymousLogin() {
123 // Subject subject = new Subject();
126 // lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject);
129 // } catch (LoginException e) {
130 // throw new CmsException("Cannot login as anonymous", e);
134 static void logFrameworkProperties(Log log
) {
135 BundleContext bc
= getBundleContext();
136 for (Object sysProp
: new TreeSet
<Object
>(System
.getProperties().keySet())) {
137 log
.debug(sysProp
+ "=" + bc
.getProperty(sysProp
.toString()));
139 // String[] keys = { Constants.FRAMEWORK_STORAGE,
140 // Constants.FRAMEWORK_OS_NAME, Constants.FRAMEWORK_OS_VERSION,
141 // Constants.FRAMEWORK_PROCESSOR, Constants.FRAMEWORK_SECURITY,
142 // Constants.FRAMEWORK_TRUST_REPOSITORIES,
143 // Constants.FRAMEWORK_WINDOWSYSTEM, Constants.FRAMEWORK_VENDOR,
144 // Constants.FRAMEWORK_VERSION, Constants.FRAMEWORK_STORAGE_CLEAN,
145 // Constants.FRAMEWORK_LANGUAGE, Constants.FRAMEWORK_UUID };
146 // for (String key : keys)
147 // log.debug(key + "=" + bc.getProperty(key));
150 static void printSystemProperties(PrintStream out
) {
151 TreeMap
<String
, String
> display
= new TreeMap
<>();
152 for (Object key
: System
.getProperties().keySet())
153 display
.put(key
.toString(), System
.getProperty(key
.toString()));
154 for (String key
: display
.keySet())
155 out
.println(key
+ "=" + display
.get(key
));
158 static Session
openAdminSession(Repository repository
) {
159 return openAdminSession(repository
, null);
162 static Session
openAdminSession(final Repository repository
, final String workspaceName
) {
163 ClassLoader currentCl
= Thread
.currentThread().getContextClassLoader();
164 Thread
.currentThread().setContextClassLoader(KernelUtils
.class.getClassLoader());
165 LoginContext loginContext
;
167 loginContext
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_DATA_ADMIN
);
168 loginContext
.login();
169 } catch (LoginException e1
) {
170 throw new IllegalStateException("Could not login as data admin", e1
);
172 Thread
.currentThread().setContextClassLoader(currentCl
);
174 return Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Session
>() {
177 public Session
run() {
179 return repository
.login(workspaceName
);
180 } catch (RepositoryException e
) {
181 throw new IllegalStateException("Cannot open admin session", e
);
188 static void asyncOpen(ServiceTracker
<?
, ?
> st
) {
189 Runnable run
= new Runnable() {
196 Activator
.getInternalExecutorService().execute(run
);
197 // new Thread(run, "Open service tracker " + st).start();
201 * @return the {@link BundleContext} of the {@link Bundle} which provided this
203 * @throws CmsException if the related bundle is not active
205 // static BundleContext getBundleContext(Class<?> clzz) {
206 //// Bundle bundle = FrameworkUtil.getBundle(clzz);
207 // BundleContext bc = Activator.getBundleContext();
209 // throw new CmsException("Bundle " + bundle.getSymbolicName() + " is not active");
213 static BundleContext
getBundleContext() {
214 return Activator
.getBundleContext();
217 static boolean asBoolean(String value
) {
226 throw new IllegalArgumentException(
227 "Unsupported value for attribute " + DataModelNamespace
.ABSTRACT
+ ": " + value
);
231 private static URI
safeUri(String uri
) {
233 throw new IllegalArgumentException("URI cannot be null");
236 } catch (URISyntaxException e
) {
237 throw new IllegalArgumentException("Badly formatted URI " + uri
, e
);
241 private KernelUtils() {