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
.Hashtable
;
13 import java
.util
.Properties
;
14 import java
.util
.TreeMap
;
15 import java
.util
.TreeSet
;
17 import javax
.jcr
.Repository
;
18 import javax
.jcr
.RepositoryException
;
19 import javax
.jcr
.Session
;
20 import javax
.security
.auth
.Subject
;
21 import javax
.security
.auth
.login
.LoginContext
;
22 import javax
.security
.auth
.login
.LoginException
;
24 import org
.apache
.commons
.logging
.Log
;
25 import org
.argeo
.cms
.CmsException
;
26 import org
.argeo
.node
.NodeConstants
;
27 import org
.osgi
.framework
.Bundle
;
28 import org
.osgi
.framework
.BundleContext
;
29 import org
.osgi
.framework
.FrameworkUtil
;
31 /** Package utilities */
32 class KernelUtils
implements KernelConstants
{
33 final static String OSGI_INSTANCE_AREA
= "osgi.instance.area";
34 final static String OSGI_CONFIGURATION_AREA
= "osgi.configuration.area";
36 static Dictionary
<String
, ?
> asDictionary(Properties props
) {
37 Hashtable
<String
, Object
> hashtable
= new Hashtable
<String
, Object
>();
38 for (Object key
: props
.keySet()) {
39 hashtable
.put(key
.toString(), props
.get(key
));
44 static Dictionary
<String
, ?
> asDictionary(ClassLoader cl
, String resource
) {
45 Properties props
= new Properties();
47 props
.load(cl
.getResourceAsStream(resource
));
48 } catch (IOException e
) {
49 throw new CmsException("Cannot load " + resource
+ " from classpath", e
);
51 return asDictionary(props
);
54 static File
getExecutionDir(String relativePath
) {
55 File executionDir
= new File(getFrameworkProp("user.dir"));
56 if (relativePath
== null)
59 return new File(executionDir
, relativePath
).getCanonicalFile();
60 } catch (IOException e
) {
61 throw new CmsException("Cannot get canonical file", e
);
65 static File
getOsgiInstanceDir() {
66 return new File(getBundleContext().getProperty(OSGI_INSTANCE_AREA
).substring("file:".length()))
70 static Path
getOsgiInstancePath(String relativePath
) {
71 return Paths
.get(getOsgiInstanceUri(relativePath
));
74 static URI
getOsgiInstanceUri(String relativePath
) {
75 String osgiInstanceBaseUri
= getFrameworkProp(OSGI_INSTANCE_AREA
);
76 return safeUri(osgiInstanceBaseUri
+ (relativePath
!= null ? relativePath
: ""));
79 // static String getOsgiInstancePath(String relativePath) {
81 // if (relativePath == null)
82 // return getOsgiInstanceDir().getCanonicalPath();
84 // return new File(getOsgiInstanceDir(), relativePath).getCanonicalPath();
85 // } catch (IOException e) {
86 // throw new CmsException("Cannot get instance path for " + relativePath,
91 static File
getOsgiConfigurationFile(String relativePath
) {
93 return new File(new URI(getBundleContext().getProperty(OSGI_CONFIGURATION_AREA
) + relativePath
))
95 } catch (Exception e
) {
96 throw new CmsException("Cannot get configuration file for " + relativePath
, e
);
100 static String
getFrameworkProp(String key
, String def
) {
101 String value
= getBundleContext().getProperty(key
);
107 static String
getFrameworkProp(String key
) {
108 return getFrameworkProp(key
, null);
112 // static Subject anonymousLogin() {
113 // Subject subject = new Subject();
116 // lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject);
119 // } catch (LoginException e) {
120 // throw new CmsException("Cannot login as anonymous", e);
124 static void logFrameworkProperties(Log log
) {
125 BundleContext bc
= getBundleContext();
126 for (Object sysProp
: new TreeSet
<Object
>(System
.getProperties().keySet())) {
127 log
.debug(sysProp
+ "=" + bc
.getProperty(sysProp
.toString()));
129 // String[] keys = { Constants.FRAMEWORK_STORAGE,
130 // Constants.FRAMEWORK_OS_NAME, Constants.FRAMEWORK_OS_VERSION,
131 // Constants.FRAMEWORK_PROCESSOR, Constants.FRAMEWORK_SECURITY,
132 // Constants.FRAMEWORK_TRUST_REPOSITORIES,
133 // Constants.FRAMEWORK_WINDOWSYSTEM, Constants.FRAMEWORK_VENDOR,
134 // Constants.FRAMEWORK_VERSION, Constants.FRAMEWORK_STORAGE_CLEAN,
135 // Constants.FRAMEWORK_LANGUAGE, Constants.FRAMEWORK_UUID };
136 // for (String key : keys)
137 // log.debug(key + "=" + bc.getProperty(key));
140 static void printSystemProperties(PrintStream out
) {
141 TreeMap
<String
, String
> display
= new TreeMap
<>();
142 for (Object key
: System
.getProperties().keySet())
143 display
.put(key
.toString(), System
.getProperty(key
.toString()));
144 for (String key
: display
.keySet())
145 out
.println(key
+ "=" + display
.get(key
));
148 static Session
openAdminSession(Repository repository
) {
149 return openAdminSession(repository
, null);
152 static Session
openAdminSession(final Repository repository
, final String workspaceName
) {
153 ClassLoader currentCl
= Thread
.currentThread().getContextClassLoader();
154 Thread
.currentThread().setContextClassLoader(KernelUtils
.class.getClassLoader());
155 LoginContext loginContext
;
157 loginContext
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_DATA_ADMIN
);
158 loginContext
.login();
159 } catch (LoginException e1
) {
160 throw new CmsException("Could not login as data admin", e1
);
162 Thread
.currentThread().setContextClassLoader(currentCl
);
164 return Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Session
>() {
167 public Session
run() {
169 return repository
.login(workspaceName
);
170 } catch (RepositoryException e
) {
171 throw new CmsException("Cannot open admin session", e
);
179 * @return the {@link BundleContext} of the {@link Bundle} which provided
180 * this class, never null.
181 * @throws CmsException
182 * if the related bundle is not active
184 public static BundleContext
getBundleContext(Class
<?
> clzz
) {
185 Bundle bundle
= FrameworkUtil
.getBundle(clzz
);
186 BundleContext bc
= bundle
.getBundleContext();
188 throw new CmsException("Bundle " + bundle
.getSymbolicName() + " is not active");
192 private static BundleContext
getBundleContext() {
193 return getBundleContext(KernelUtils
.class);
196 private static URI
safeUri(String uri
) {
198 throw new CmsException("URI cannot be null");
201 } catch (URISyntaxException e
) {
202 throw new CmsException("Dadly formatted URI " + uri
, e
);
206 private KernelUtils() {