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
;
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 CmsException("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 CmsException("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 CmsException("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 return safeUri(osgiInstanceBaseUri
+ (relativePath
!= null ? relativePath
: ""));
92 // static String getOsgiInstancePath(String relativePath) {
94 // if (relativePath == null)
95 // return getOsgiInstanceDir().getCanonicalPath();
97 // return new File(getOsgiInstanceDir(), relativePath).getCanonicalPath();
98 // } catch (IOException e) {
99 // throw new CmsException("Cannot get instance path for " + relativePath,
104 static File
getOsgiConfigurationFile(String relativePath
) {
106 return new File(new URI(getBundleContext().getProperty(OSGI_CONFIGURATION_AREA
) + relativePath
))
108 } catch (Exception e
) {
109 throw new CmsException("Cannot get configuration file for " + relativePath
, e
);
113 static String
getFrameworkProp(String key
, String def
) {
114 String value
= getBundleContext().getProperty(key
);
120 static String
getFrameworkProp(String key
) {
121 return getFrameworkProp(key
, null);
125 // static Subject anonymousLogin() {
126 // Subject subject = new Subject();
129 // lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject);
132 // } catch (LoginException e) {
133 // throw new CmsException("Cannot login as anonymous", e);
137 static void logFrameworkProperties(Log log
) {
138 BundleContext bc
= getBundleContext();
139 for (Object sysProp
: new TreeSet
<Object
>(System
.getProperties().keySet())) {
140 log
.debug(sysProp
+ "=" + bc
.getProperty(sysProp
.toString()));
142 // String[] keys = { Constants.FRAMEWORK_STORAGE,
143 // Constants.FRAMEWORK_OS_NAME, Constants.FRAMEWORK_OS_VERSION,
144 // Constants.FRAMEWORK_PROCESSOR, Constants.FRAMEWORK_SECURITY,
145 // Constants.FRAMEWORK_TRUST_REPOSITORIES,
146 // Constants.FRAMEWORK_WINDOWSYSTEM, Constants.FRAMEWORK_VENDOR,
147 // Constants.FRAMEWORK_VERSION, Constants.FRAMEWORK_STORAGE_CLEAN,
148 // Constants.FRAMEWORK_LANGUAGE, Constants.FRAMEWORK_UUID };
149 // for (String key : keys)
150 // log.debug(key + "=" + bc.getProperty(key));
153 static void printSystemProperties(PrintStream out
) {
154 TreeMap
<String
, String
> display
= new TreeMap
<>();
155 for (Object key
: System
.getProperties().keySet())
156 display
.put(key
.toString(), System
.getProperty(key
.toString()));
157 for (String key
: display
.keySet())
158 out
.println(key
+ "=" + display
.get(key
));
161 static Session
openAdminSession(Repository repository
) {
162 return openAdminSession(repository
, null);
165 static Session
openAdminSession(final Repository repository
, final String workspaceName
) {
166 ClassLoader currentCl
= Thread
.currentThread().getContextClassLoader();
167 Thread
.currentThread().setContextClassLoader(KernelUtils
.class.getClassLoader());
168 LoginContext loginContext
;
170 loginContext
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_DATA_ADMIN
);
171 loginContext
.login();
172 } catch (LoginException e1
) {
173 throw new CmsException("Could not login as data admin", e1
);
175 Thread
.currentThread().setContextClassLoader(currentCl
);
177 return Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Session
>() {
180 public Session
run() {
182 return repository
.login(workspaceName
);
183 } catch (RepositoryException e
) {
184 throw new CmsException("Cannot open admin session", e
);
192 * @return the {@link BundleContext} of the {@link Bundle} which provided
193 * this class, never null.
194 * @throws CmsException
195 * if the related bundle is not active
197 public static BundleContext
getBundleContext(Class
<?
> clzz
) {
198 Bundle bundle
= FrameworkUtil
.getBundle(clzz
);
199 BundleContext bc
= bundle
.getBundleContext();
201 throw new CmsException("Bundle " + bundle
.getSymbolicName() + " is not active");
205 private static BundleContext
getBundleContext() {
206 return getBundleContext(KernelUtils
.class);
209 private static URI
safeUri(String uri
) {
211 throw new CmsException("URI cannot be null");
214 } catch (URISyntaxException e
) {
215 throw new CmsException("Dadly formatted URI " + uri
, e
);
219 private KernelUtils() {