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
.cms
.CmsException
;
28 import org
.argeo
.node
.DataModelNamespace
;
29 import org
.argeo
.node
.NodeConstants
;
30 import org
.osgi
.framework
.Bundle
;
31 import org
.osgi
.framework
.BundleContext
;
32 import org
.osgi
.framework
.FrameworkUtil
;
33 import org
.osgi
.util
.tracker
.ServiceTracker
;
35 /** Package utilities */
36 class KernelUtils
implements KernelConstants
{
37 final static String OSGI_INSTANCE_AREA
= "osgi.instance.area";
38 final static String OSGI_CONFIGURATION_AREA
= "osgi.configuration.area";
40 static void setJaasConfiguration(URL jaasConfigurationUrl
) {
42 URIParameter uriParameter
= new URIParameter(jaasConfigurationUrl
.toURI());
43 javax
.security
.auth
.login
.Configuration jaasConfiguration
= javax
.security
.auth
.login
.Configuration
44 .getInstance("JavaLoginConfig", uriParameter
);
45 javax
.security
.auth
.login
.Configuration
.setConfiguration(jaasConfiguration
);
46 } catch (Exception e
) {
47 throw new CmsException("Cannot set configuration " + jaasConfigurationUrl
, e
);
51 static Dictionary
<String
, ?
> asDictionary(Properties props
) {
52 Hashtable
<String
, Object
> hashtable
= new Hashtable
<String
, Object
>();
53 for (Object key
: props
.keySet()) {
54 hashtable
.put(key
.toString(), props
.get(key
));
59 static Dictionary
<String
, ?
> asDictionary(ClassLoader cl
, String resource
) {
60 Properties props
= new Properties();
62 props
.load(cl
.getResourceAsStream(resource
));
63 } catch (IOException e
) {
64 throw new CmsException("Cannot load " + resource
+ " from classpath", e
);
66 return asDictionary(props
);
69 static File
getExecutionDir(String relativePath
) {
70 File executionDir
= new File(getFrameworkProp("user.dir"));
71 if (relativePath
== null)
74 return new File(executionDir
, relativePath
).getCanonicalFile();
75 } catch (IOException e
) {
76 throw new CmsException("Cannot get canonical file", e
);
80 static File
getOsgiInstanceDir() {
81 return new File(getBundleContext().getProperty(OSGI_INSTANCE_AREA
).substring("file:".length()))
85 static Path
getOsgiInstancePath(String relativePath
) {
86 return Paths
.get(getOsgiInstanceUri(relativePath
));
89 static URI
getOsgiInstanceUri(String relativePath
) {
90 String osgiInstanceBaseUri
= getFrameworkProp(OSGI_INSTANCE_AREA
);
91 return safeUri(osgiInstanceBaseUri
+ (relativePath
!= null ? relativePath
: ""));
94 // static String getOsgiInstancePath(String relativePath) {
96 // if (relativePath == null)
97 // return getOsgiInstanceDir().getCanonicalPath();
99 // return new File(getOsgiInstanceDir(), relativePath).getCanonicalPath();
100 // } catch (IOException e) {
101 // throw new CmsException("Cannot get instance path for " + relativePath,
106 static File
getOsgiConfigurationFile(String relativePath
) {
108 return new File(new URI(getBundleContext().getProperty(OSGI_CONFIGURATION_AREA
) + relativePath
))
110 } catch (Exception e
) {
111 throw new CmsException("Cannot get configuration file for " + relativePath
, e
);
115 static String
getFrameworkProp(String key
, String def
) {
116 String value
= getBundleContext().getProperty(key
);
122 static String
getFrameworkProp(String key
) {
123 return getFrameworkProp(key
, null);
127 // static Subject anonymousLogin() {
128 // Subject subject = new Subject();
131 // lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject);
134 // } catch (LoginException e) {
135 // throw new CmsException("Cannot login as anonymous", e);
139 static void logFrameworkProperties(Log log
) {
140 BundleContext bc
= getBundleContext();
141 for (Object sysProp
: new TreeSet
<Object
>(System
.getProperties().keySet())) {
142 log
.debug(sysProp
+ "=" + bc
.getProperty(sysProp
.toString()));
144 // String[] keys = { Constants.FRAMEWORK_STORAGE,
145 // Constants.FRAMEWORK_OS_NAME, Constants.FRAMEWORK_OS_VERSION,
146 // Constants.FRAMEWORK_PROCESSOR, Constants.FRAMEWORK_SECURITY,
147 // Constants.FRAMEWORK_TRUST_REPOSITORIES,
148 // Constants.FRAMEWORK_WINDOWSYSTEM, Constants.FRAMEWORK_VENDOR,
149 // Constants.FRAMEWORK_VERSION, Constants.FRAMEWORK_STORAGE_CLEAN,
150 // Constants.FRAMEWORK_LANGUAGE, Constants.FRAMEWORK_UUID };
151 // for (String key : keys)
152 // log.debug(key + "=" + bc.getProperty(key));
155 static void printSystemProperties(PrintStream out
) {
156 TreeMap
<String
, String
> display
= new TreeMap
<>();
157 for (Object key
: System
.getProperties().keySet())
158 display
.put(key
.toString(), System
.getProperty(key
.toString()));
159 for (String key
: display
.keySet())
160 out
.println(key
+ "=" + display
.get(key
));
163 static Session
openAdminSession(Repository repository
) {
164 return openAdminSession(repository
, null);
167 static Session
openAdminSession(final Repository repository
, final String workspaceName
) {
168 ClassLoader currentCl
= Thread
.currentThread().getContextClassLoader();
169 Thread
.currentThread().setContextClassLoader(KernelUtils
.class.getClassLoader());
170 LoginContext loginContext
;
172 loginContext
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_DATA_ADMIN
);
173 loginContext
.login();
174 } catch (LoginException e1
) {
175 throw new CmsException("Could not login as data admin", e1
);
177 Thread
.currentThread().setContextClassLoader(currentCl
);
179 return Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Session
>() {
182 public Session
run() {
184 return repository
.login(workspaceName
);
185 } catch (RepositoryException e
) {
186 throw new CmsException("Cannot open admin session", e
);
193 static void asyncOpen(ServiceTracker
<?
, ?
> st
) {
194 Runnable run
= new Runnable() {
201 new Thread(run
, "Open service tracker " + st
).start();
205 * @return the {@link BundleContext} of the {@link Bundle} which provided this
207 * @throws CmsException
208 * if the related bundle is not active
210 static BundleContext
getBundleContext(Class
<?
> clzz
) {
211 Bundle bundle
= FrameworkUtil
.getBundle(clzz
);
212 BundleContext bc
= bundle
.getBundleContext();
214 throw new CmsException("Bundle " + bundle
.getSymbolicName() + " is not active");
218 static BundleContext
getBundleContext() {
219 return getBundleContext(KernelUtils
.class);
222 static boolean asBoolean(String value
) {
231 throw new CmsException("Unsupported value for attribute " + DataModelNamespace
.ABSTRACT
236 private static URI
safeUri(String uri
) {
238 throw new CmsException("URI cannot be null");
241 } catch (URISyntaxException e
) {
242 throw new CmsException("Dadly formatted URI " + uri
, e
);
246 private KernelUtils() {