1 package org
.argeo
.cms
.internal
.runtime
;
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
.URIParameter
;
12 import java
.util
.Dictionary
;
13 import java
.util
.Hashtable
;
14 import java
.util
.Properties
;
15 import java
.util
.TreeMap
;
16 import java
.util
.TreeSet
;
18 import org
.argeo
.api
.cms
.CmsLog
;
19 import org
.argeo
.cms
.internal
.osgi
.CmsActivator
;
21 /** Package utilities */
22 class KernelUtils
implements KernelConstants
{
23 final static String OSGI_INSTANCE_AREA
= "osgi.instance.area";
24 final static String OSGI_CONFIGURATION_AREA
= "osgi.configuration.area";
26 static void setJaasConfiguration(URL jaasConfigurationUrl
) {
28 URIParameter uriParameter
= new URIParameter(jaasConfigurationUrl
.toURI());
29 javax
.security
.auth
.login
.Configuration jaasConfiguration
= javax
.security
.auth
.login
.Configuration
30 .getInstance("JavaLoginConfig", uriParameter
);
31 javax
.security
.auth
.login
.Configuration
.setConfiguration(jaasConfiguration
);
32 } catch (Exception e
) {
33 throw new IllegalArgumentException("Cannot set configuration " + jaasConfigurationUrl
, e
);
37 static Dictionary
<String
, ?
> asDictionary(Properties props
) {
38 Hashtable
<String
, Object
> hashtable
= new Hashtable
<String
, Object
>();
39 for (Object key
: props
.keySet()) {
40 hashtable
.put(key
.toString(), props
.get(key
));
45 static Dictionary
<String
, ?
> asDictionary(ClassLoader cl
, String resource
) {
46 Properties props
= new Properties();
48 props
.load(cl
.getResourceAsStream(resource
));
49 } catch (IOException e
) {
50 throw new IllegalArgumentException("Cannot load " + resource
+ " from classpath", e
);
52 return asDictionary(props
);
55 static File
getExecutionDir(String relativePath
) {
56 File executionDir
= new File(getFrameworkProp("user.dir"));
57 if (relativePath
== null)
60 return new File(executionDir
, relativePath
).getCanonicalFile();
61 } catch (IOException e
) {
62 throw new IllegalArgumentException("Cannot get canonical file", e
);
66 // static File getOsgiInstanceDir() {
67 // return new File(CmsActivator.getBundleContext().getProperty(OSGI_INSTANCE_AREA).substring("file:".length()))
68 // .getAbsoluteFile();
71 public static Path
getOsgiInstancePath(String relativePath
) {
72 return Paths
.get(getOsgiInstanceUri(relativePath
));
75 public static URI
getOsgiInstanceUri(String relativePath
) {
76 String osgiInstanceBaseUri
= getFrameworkProp(OSGI_INSTANCE_AREA
);
77 if (osgiInstanceBaseUri
!= null)
78 return safeUri(osgiInstanceBaseUri
+ (relativePath
!= null ? relativePath
: ""));
80 return Paths
.get(System
.getProperty("user.dir"), (relativePath
!= null ? relativePath
: "")).toUri();
83 static File
getOsgiConfigurationFile(String relativePath
) {
86 new URI(CmsActivator
.getBundleContext().getProperty(OSGI_CONFIGURATION_AREA
) + relativePath
))
88 } catch (Exception e
) {
89 throw new IllegalArgumentException("Cannot get configuration file for " + relativePath
, e
);
93 static String
getFrameworkProp(String key
, String def
) {
95 if (CmsActivator
.getBundleContext() != null)
96 value
= CmsActivator
.getBundleContext().getProperty(key
);
98 value
= System
.getProperty(key
);
104 static String
getFrameworkProp(String key
) {
105 return getFrameworkProp(key
, null);
109 // static Subject anonymousLogin() {
110 // Subject subject = new Subject();
113 // lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject);
116 // } catch (LoginException e) {
117 // throw new CmsException("Cannot login as anonymous", e);
121 static void logFrameworkProperties(CmsLog log
) {
122 for (Object sysProp
: new TreeSet
<Object
>(System
.getProperties().keySet())) {
123 log
.debug(sysProp
+ "=" + getFrameworkProp(sysProp
.toString()));
125 // String[] keys = { Constants.FRAMEWORK_STORAGE,
126 // Constants.FRAMEWORK_OS_NAME, Constants.FRAMEWORK_OS_VERSION,
127 // Constants.FRAMEWORK_PROCESSOR, Constants.FRAMEWORK_SECURITY,
128 // Constants.FRAMEWORK_TRUST_REPOSITORIES,
129 // Constants.FRAMEWORK_WINDOWSYSTEM, Constants.FRAMEWORK_VENDOR,
130 // Constants.FRAMEWORK_VERSION, Constants.FRAMEWORK_STORAGE_CLEAN,
131 // Constants.FRAMEWORK_LANGUAGE, Constants.FRAMEWORK_UUID };
132 // for (String key : keys)
133 // log.debug(key + "=" + bc.getProperty(key));
136 static void printSystemProperties(PrintStream out
) {
137 TreeMap
<String
, String
> display
= new TreeMap
<>();
138 for (Object key
: System
.getProperties().keySet())
139 display
.put(key
.toString(), System
.getProperty(key
.toString()));
140 for (String key
: display
.keySet())
141 out
.println(key
+ "=" + display
.get(key
));
144 // static Session openAdminSession(Repository repository) {
145 // return openAdminSession(repository, null);
148 // static Session openAdminSession(final Repository repository, final String workspaceName) {
149 // LoginContext loginContext = loginAsDataAdmin();
150 // return Subject.doAs(loginContext.getSubject(), new PrivilegedAction<Session>() {
153 // public Session run() {
155 // return repository.login(workspaceName);
156 // } catch (RepositoryException e) {
157 // throw new IllegalStateException("Cannot open admin session", e);
160 // loginContext.logout();
161 // } catch (LoginException e) {
162 // throw new IllegalStateException(e);
170 // static LoginContext loginAsDataAdmin() {
171 // ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
172 // Thread.currentThread().setContextClassLoader(KernelUtils.class.getClassLoader());
173 // LoginContext loginContext;
175 // loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_DATA_ADMIN);
176 // loginContext.login();
177 // } catch (LoginException e1) {
178 // throw new IllegalStateException("Could not login as data admin", e1);
180 // Thread.currentThread().setContextClassLoader(currentCl);
182 // return loginContext;
185 // static void doAsDataAdmin(Runnable action) {
186 // LoginContext loginContext = loginAsDataAdmin();
187 // Subject.doAs(loginContext.getSubject(), new PrivilegedAction<Void>() {
190 // public Void run() {
196 // loginContext.logout();
197 // } catch (LoginException e) {
198 // throw new IllegalStateException(e);
206 // public static void asyncOpen(ServiceTracker<?, ?> st) {
207 // Runnable run = new Runnable() {
210 // public void run() {
214 // Activator.getInternalExecutorService().execute(run);
215 //// new Thread(run, "Open service tracker " + st).start();
218 // static BundleContext getBundleContext() {
219 // return Activator.getBundleContext();
222 static boolean asBoolean(String value
) {
231 throw new IllegalArgumentException("Unsupported value for boolean attribute : " + value
);
235 private static URI
safeUri(String uri
) {
237 throw new IllegalArgumentException("URI cannot be null");
240 } catch (URISyntaxException e
) {
241 throw new IllegalArgumentException("Badly formatted URI " + uri
, e
);
245 private KernelUtils() {