1 package org
.argeo
.cms
.internal
.kernel
;
4 import java
.io
.IOException
;
5 import java
.io
.PrintStream
;
7 import java
.net
.URISyntaxException
;
8 import java
.nio
.file
.Path
;
9 import java
.nio
.file
.Paths
;
10 import java
.security
.PrivilegedAction
;
11 import java
.util
.Dictionary
;
12 import java
.util
.Enumeration
;
13 import java
.util
.Hashtable
;
14 import java
.util
.Properties
;
15 import java
.util
.TreeMap
;
16 import java
.util
.TreeSet
;
18 import javax
.jcr
.Repository
;
19 import javax
.jcr
.RepositoryException
;
20 import javax
.jcr
.Session
;
21 import javax
.security
.auth
.Subject
;
22 import javax
.security
.auth
.login
.LoginContext
;
23 import javax
.security
.auth
.login
.LoginException
;
24 import javax
.servlet
.http
.HttpServletRequest
;
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 Dictionary
<String
, ?
> asDictionary(Properties props
) {
39 Hashtable
<String
, Object
> hashtable
= new Hashtable
<String
, Object
>();
40 for (Object key
: props
.keySet()) {
41 hashtable
.put(key
.toString(), props
.get(key
));
46 static Dictionary
<String
, ?
> asDictionary(ClassLoader cl
, String resource
) {
47 Properties props
= new Properties();
49 props
.load(cl
.getResourceAsStream(resource
));
50 } catch (IOException e
) {
51 throw new CmsException("Cannot load " + resource
+ " from classpath", e
);
53 return asDictionary(props
);
56 static File
getExecutionDir(String relativePath
) {
57 File executionDir
= new File(getFrameworkProp("user.dir"));
58 if (relativePath
== null)
61 return new File(executionDir
, relativePath
).getCanonicalFile();
62 } catch (IOException e
) {
63 throw new CmsException("Cannot get canonical file", e
);
67 static File
getOsgiInstanceDir() {
68 return new File(getBundleContext().getProperty(OSGI_INSTANCE_AREA
).substring("file:".length()))
72 static Path
getOsgiInstancePath(String relativePath
) {
73 return Paths
.get(getOsgiInstanceUri(relativePath
));
76 static URI
getOsgiInstanceUri(String relativePath
) {
77 String osgiInstanceBaseUri
= getFrameworkProp(OSGI_INSTANCE_AREA
);
78 return safeUri(osgiInstanceBaseUri
+ (relativePath
!= null ? relativePath
: ""));
81 // static String getOsgiInstancePath(String relativePath) {
83 // if (relativePath == null)
84 // return getOsgiInstanceDir().getCanonicalPath();
86 // return new File(getOsgiInstanceDir(), relativePath).getCanonicalPath();
87 // } catch (IOException e) {
88 // throw new CmsException("Cannot get instance path for " + relativePath,
93 static File
getOsgiConfigurationFile(String relativePath
) {
95 return new File(new URI(getBundleContext().getProperty(OSGI_CONFIGURATION_AREA
) + relativePath
))
97 } catch (Exception e
) {
98 throw new CmsException("Cannot get configuration file for " + relativePath
, e
);
102 static String
getFrameworkProp(String key
, String def
) {
103 String value
= getBundleContext().getProperty(key
);
109 static String
getFrameworkProp(String key
) {
110 return getFrameworkProp(key
, null);
114 static Subject
anonymousLogin() {
115 Subject subject
= new Subject();
118 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
, subject
);
121 } catch (LoginException e
) {
122 throw new CmsException("Cannot login as anonymous", e
);
127 static void logRequestHeaders(Log log
, HttpServletRequest request
) {
128 if (!log
.isDebugEnabled())
130 for (Enumeration
<String
> headerNames
= request
.getHeaderNames(); headerNames
.hasMoreElements();) {
131 String headerName
= headerNames
.nextElement();
132 Object headerValue
= request
.getHeader(headerName
);
133 log
.debug(headerName
+ ": " + headerValue
);
135 log
.debug(request
.getRequestURI() + "\n");
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
);
193 * @return the {@link BundleContext} of the {@link Bundle} which provided
194 * this class, never null.
195 * @throws CmsException
196 * if the related bundle is not active
198 public static BundleContext
getBundleContext(Class
<?
> clzz
) {
199 Bundle bundle
= FrameworkUtil
.getBundle(clzz
);
200 BundleContext bc
= bundle
.getBundleContext();
202 throw new CmsException("Bundle " + bundle
.getSymbolicName() + " is not active");
206 private static BundleContext
getBundleContext() {
207 return getBundleContext(KernelUtils
.class);
210 private static URI
safeUri(String uri
) {
212 throw new CmsException("URI cannot be null");
215 } catch (URISyntaxException e
) {
216 throw new CmsException("Dadly formatted URI " + uri
, e
);
221 private KernelUtils() {