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
.NodeConstants
;
29 import org
.osgi
.framework
.Bundle
;
30 import org
.osgi
.framework
.BundleContext
;
31 import org
.osgi
.framework
.FrameworkUtil
;
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 CmsException("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 CmsException("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 CmsException("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 return safeUri(osgiInstanceBaseUri
+ (relativePath
!= null ? relativePath
: ""));
93 // static String getOsgiInstancePath(String relativePath) {
95 // if (relativePath == null)
96 // return getOsgiInstanceDir().getCanonicalPath();
98 // return new File(getOsgiInstanceDir(), relativePath).getCanonicalPath();
99 // } catch (IOException e) {
100 // throw new CmsException("Cannot get instance path for " + relativePath,
105 static File
getOsgiConfigurationFile(String relativePath
) {
107 return new File(new URI(getBundleContext().getProperty(OSGI_CONFIGURATION_AREA
) + relativePath
))
109 } catch (Exception e
) {
110 throw new CmsException("Cannot get configuration file for " + relativePath
, e
);
114 static String
getFrameworkProp(String key
, String def
) {
115 String value
= getBundleContext().getProperty(key
);
121 static String
getFrameworkProp(String key
) {
122 return getFrameworkProp(key
, null);
126 // static Subject anonymousLogin() {
127 // Subject subject = new Subject();
130 // lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject);
133 // } catch (LoginException e) {
134 // throw new CmsException("Cannot login as anonymous", e);
138 static void logFrameworkProperties(Log log
) {
139 BundleContext bc
= getBundleContext();
140 for (Object sysProp
: new TreeSet
<Object
>(System
.getProperties().keySet())) {
141 log
.debug(sysProp
+ "=" + bc
.getProperty(sysProp
.toString()));
143 // String[] keys = { Constants.FRAMEWORK_STORAGE,
144 // Constants.FRAMEWORK_OS_NAME, Constants.FRAMEWORK_OS_VERSION,
145 // Constants.FRAMEWORK_PROCESSOR, Constants.FRAMEWORK_SECURITY,
146 // Constants.FRAMEWORK_TRUST_REPOSITORIES,
147 // Constants.FRAMEWORK_WINDOWSYSTEM, Constants.FRAMEWORK_VENDOR,
148 // Constants.FRAMEWORK_VERSION, Constants.FRAMEWORK_STORAGE_CLEAN,
149 // Constants.FRAMEWORK_LANGUAGE, Constants.FRAMEWORK_UUID };
150 // for (String key : keys)
151 // log.debug(key + "=" + bc.getProperty(key));
154 static void printSystemProperties(PrintStream out
) {
155 TreeMap
<String
, String
> display
= new TreeMap
<>();
156 for (Object key
: System
.getProperties().keySet())
157 display
.put(key
.toString(), System
.getProperty(key
.toString()));
158 for (String key
: display
.keySet())
159 out
.println(key
+ "=" + display
.get(key
));
162 static Session
openAdminSession(Repository repository
) {
163 return openAdminSession(repository
, null);
166 static Session
openAdminSession(final Repository repository
, final String workspaceName
) {
167 ClassLoader currentCl
= Thread
.currentThread().getContextClassLoader();
168 Thread
.currentThread().setContextClassLoader(KernelUtils
.class.getClassLoader());
169 LoginContext loginContext
;
171 loginContext
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_DATA_ADMIN
);
172 loginContext
.login();
173 } catch (LoginException e1
) {
174 throw new CmsException("Could not login as data admin", e1
);
176 Thread
.currentThread().setContextClassLoader(currentCl
);
178 return Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Session
>() {
181 public Session
run() {
183 return repository
.login(workspaceName
);
184 } catch (RepositoryException e
) {
185 throw new CmsException("Cannot open admin session", e
);
192 static void asyncOpen(ServiceTracker
<?
, ?
> st
) {
193 Runnable run
= new Runnable() {
200 new Thread(run
, "Open service tracker " + st
).start();
204 * @return the {@link BundleContext} of the {@link Bundle} which provided this
206 * @throws CmsException
207 * if the related bundle is not active
209 public static BundleContext
getBundleContext(Class
<?
> clzz
) {
210 Bundle bundle
= FrameworkUtil
.getBundle(clzz
);
211 BundleContext bc
= bundle
.getBundleContext();
213 throw new CmsException("Bundle " + bundle
.getSymbolicName() + " is not active");
217 private static BundleContext
getBundleContext() {
218 return getBundleContext(KernelUtils
.class);
221 private static URI
safeUri(String uri
) {
223 throw new CmsException("URI cannot be null");
226 } catch (URISyntaxException e
) {
227 throw new CmsException("Dadly formatted URI " + uri
, e
);
231 private KernelUtils() {