1 package org
.argeo
.cms
.internal
.runtime
;
3 import java
.io
.IOException
;
4 import java
.io
.PrintStream
;
6 import java
.net
.URISyntaxException
;
8 import java
.nio
.file
.Path
;
9 import java
.nio
.file
.Paths
;
10 import java
.security
.URIParameter
;
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 org
.argeo
.api
.cms
.CmsLog
;
18 import org
.argeo
.cms
.internal
.osgi
.CmsActivator
;
20 /** Package utilities */
21 class KernelUtils
implements KernelConstants
{
22 final static String OSGI_INSTANCE_AREA
= "osgi.instance.area";
23 final static String OSGI_CONFIGURATION_AREA
= "osgi.configuration.area";
25 static void setJaasConfiguration(URL jaasConfigurationUrl
) {
27 URIParameter uriParameter
= new URIParameter(jaasConfigurationUrl
.toURI());
28 javax
.security
.auth
.login
.Configuration jaasConfiguration
= javax
.security
.auth
.login
.Configuration
29 .getInstance("JavaLoginConfig", uriParameter
);
30 javax
.security
.auth
.login
.Configuration
.setConfiguration(jaasConfiguration
);
31 } catch (Exception e
) {
32 throw new IllegalArgumentException("Cannot set configuration " + jaasConfigurationUrl
, e
);
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 IllegalArgumentException("Cannot load " + resource
+ " from classpath", e
);
51 return asDictionary(props
);
54 static Path
getExecutionDir(String relativePath
) {
55 Path executionDir
= Paths
.get(getFrameworkProp("user.dir"));
56 if (relativePath
== null)
59 return executionDir
.resolve(relativePath
).toRealPath();
60 } catch (IOException e
) {
61 throw new IllegalArgumentException("Cannot get canonical file", e
);
65 // static File getOsgiInstanceDir() {
66 // return new File(CmsActivator.getBundleContext().getProperty(OSGI_INSTANCE_AREA).substring("file:".length()))
67 // .getAbsoluteFile();
70 public static Path
getOsgiInstancePath(String relativePath
) {
71 URI uri
= getOsgiInstanceUri(relativePath
);
72 if (uri
== null) // no data area available
74 return Paths
.get(uri
);
77 public static URI
getOsgiInstanceUri(String relativePath
) {
78 String osgiInstanceBaseUri
= getFrameworkProp(OSGI_INSTANCE_AREA
);
79 if (osgiInstanceBaseUri
== null) // no data area available
82 if (!osgiInstanceBaseUri
.endsWith("/"))
83 osgiInstanceBaseUri
= osgiInstanceBaseUri
+ "/";
84 // if (osgiInstanceBaseUri != null)
85 return safeUri(osgiInstanceBaseUri
+ (relativePath
!= null ? relativePath
: ""));
87 // return Paths.get(System.getProperty("user.dir"), (relativePath != null ? relativePath : "")).toUri();
90 // static File getOsgiConfigurationFile(String relativePath) {
93 // new URI(CmsActivator.getBundleContext().getProperty(OSGI_CONFIGURATION_AREA) + relativePath))
94 // .getCanonicalFile();
95 // } catch (Exception e) {
96 // throw new IllegalArgumentException("Cannot get configuration file for " + relativePath, e);
100 static String
getFrameworkProp(String key
, String def
) {
102 if (CmsActivator
.getBundleContext() != null)
103 value
= CmsActivator
.getBundleContext().getProperty(key
);
105 value
= System
.getProperty(key
);
111 static String
getFrameworkProp(String key
) {
112 return getFrameworkProp(key
, null);
116 // static Subject anonymousLogin() {
117 // Subject subject = new Subject();
120 // lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject);
123 // } catch (LoginException e) {
124 // throw new CmsException("Cannot login as anonymous", e);
128 static void logFrameworkProperties(CmsLog log
) {
129 for (Object sysProp
: new TreeSet
<Object
>(System
.getProperties().keySet())) {
130 log
.debug(sysProp
+ "=" + getFrameworkProp(sysProp
.toString()));
132 // String[] keys = { Constants.FRAMEWORK_STORAGE,
133 // Constants.FRAMEWORK_OS_NAME, Constants.FRAMEWORK_OS_VERSION,
134 // Constants.FRAMEWORK_PROCESSOR, Constants.FRAMEWORK_SECURITY,
135 // Constants.FRAMEWORK_TRUST_REPOSITORIES,
136 // Constants.FRAMEWORK_WINDOWSYSTEM, Constants.FRAMEWORK_VENDOR,
137 // Constants.FRAMEWORK_VERSION, Constants.FRAMEWORK_STORAGE_CLEAN,
138 // Constants.FRAMEWORK_LANGUAGE, Constants.FRAMEWORK_UUID };
139 // for (String key : keys)
140 // log.debug(key + "=" + bc.getProperty(key));
143 static void printSystemProperties(PrintStream out
) {
144 TreeMap
<String
, String
> display
= new TreeMap
<>();
145 for (Object key
: System
.getProperties().keySet())
146 display
.put(key
.toString(), System
.getProperty(key
.toString()));
147 for (String key
: display
.keySet())
148 out
.println(key
+ "=" + display
.get(key
));
151 // static Session openAdminSession(Repository repository) {
152 // return openAdminSession(repository, null);
155 // static Session openAdminSession(final Repository repository, final String workspaceName) {
156 // LoginContext loginContext = loginAsDataAdmin();
157 // return Subject.doAs(loginContext.getSubject(), new PrivilegedAction<Session>() {
160 // public Session run() {
162 // return repository.login(workspaceName);
163 // } catch (RepositoryException e) {
164 // throw new IllegalStateException("Cannot open admin session", e);
167 // loginContext.logout();
168 // } catch (LoginException e) {
169 // throw new IllegalStateException(e);
177 // static LoginContext loginAsDataAdmin() {
178 // ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
179 // Thread.currentThread().setContextClassLoader(KernelUtils.class.getClassLoader());
180 // LoginContext loginContext;
182 // loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_DATA_ADMIN);
183 // loginContext.login();
184 // } catch (LoginException e1) {
185 // throw new IllegalStateException("Could not login as data admin", e1);
187 // Thread.currentThread().setContextClassLoader(currentCl);
189 // return loginContext;
192 // static void doAsDataAdmin(Runnable action) {
193 // LoginContext loginContext = loginAsDataAdmin();
194 // Subject.doAs(loginContext.getSubject(), new PrivilegedAction<Void>() {
197 // public Void run() {
203 // loginContext.logout();
204 // } catch (LoginException e) {
205 // throw new IllegalStateException(e);
213 // public static void asyncOpen(ServiceTracker<?, ?> st) {
214 // Runnable run = new Runnable() {
217 // public void run() {
221 // Activator.getInternalExecutorService().execute(run);
222 //// new Thread(run, "Open service tracker " + st).start();
225 // static BundleContext getBundleContext() {
226 // return Activator.getBundleContext();
229 static boolean asBoolean(String value
) {
238 throw new IllegalArgumentException("Unsupported value for boolean attribute : " + value
);
242 private static URI
safeUri(String uri
) {
244 throw new IllegalArgumentException("URI cannot be null");
247 } catch (URISyntaxException e
) {
248 throw new IllegalArgumentException("Badly formatted URI " + uri
, e
);
252 private KernelUtils() {