]> git.argeo.org Git - lgpl/argeo-commons.git/blob - KernelUtils.java
0e84af62af45d8c5b2d22b0261cb3e1dcc2170b9
[lgpl/argeo-commons.git] / KernelUtils.java
1 package org.argeo.cms.internal.runtime;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.PrintStream;
6 import java.net.URI;
7 import java.net.URISyntaxException;
8 import java.net.URL;
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;
17
18 import org.argeo.api.cms.CmsLog;
19 import org.argeo.cms.internal.osgi.CmsActivator;
20
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";
25
26 static void setJaasConfiguration(URL jaasConfigurationUrl) {
27 try {
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);
34 }
35 }
36
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));
41 }
42 return hashtable;
43 }
44
45 static Dictionary<String, ?> asDictionary(ClassLoader cl, String resource) {
46 Properties props = new Properties();
47 try {
48 props.load(cl.getResourceAsStream(resource));
49 } catch (IOException e) {
50 throw new IllegalArgumentException("Cannot load " + resource + " from classpath", e);
51 }
52 return asDictionary(props);
53 }
54
55 static File getExecutionDir(String relativePath) {
56 File executionDir = new File(getFrameworkProp("user.dir"));
57 if (relativePath == null)
58 return executionDir;
59 try {
60 return new File(executionDir, relativePath).getCanonicalFile();
61 } catch (IOException e) {
62 throw new IllegalArgumentException("Cannot get canonical file", e);
63 }
64 }
65
66 // static File getOsgiInstanceDir() {
67 // return new File(CmsActivator.getBundleContext().getProperty(OSGI_INSTANCE_AREA).substring("file:".length()))
68 // .getAbsoluteFile();
69 // }
70
71 public static Path getOsgiInstancePath(String relativePath) {
72 return Paths.get(getOsgiInstanceUri(relativePath));
73 }
74
75 public static URI getOsgiInstanceUri(String relativePath) {
76 String osgiInstanceBaseUri = getFrameworkProp(OSGI_INSTANCE_AREA);
77 if (!osgiInstanceBaseUri.endsWith("/"))
78 osgiInstanceBaseUri = osgiInstanceBaseUri + "/";
79 if (osgiInstanceBaseUri != null)
80 return safeUri(osgiInstanceBaseUri + (relativePath != null ? relativePath : ""));
81 else
82 return Paths.get(System.getProperty("user.dir"), (relativePath != null ? relativePath : "")).toUri();
83 }
84
85 static File getOsgiConfigurationFile(String relativePath) {
86 try {
87 return new File(
88 new URI(CmsActivator.getBundleContext().getProperty(OSGI_CONFIGURATION_AREA) + relativePath))
89 .getCanonicalFile();
90 } catch (Exception e) {
91 throw new IllegalArgumentException("Cannot get configuration file for " + relativePath, e);
92 }
93 }
94
95 static String getFrameworkProp(String key, String def) {
96 String value;
97 if (CmsActivator.getBundleContext() != null)
98 value = CmsActivator.getBundleContext().getProperty(key);
99 else
100 value = System.getProperty(key);
101 if (value == null)
102 return def;
103 return value;
104 }
105
106 static String getFrameworkProp(String key) {
107 return getFrameworkProp(key, null);
108 }
109
110 // Security
111 // static Subject anonymousLogin() {
112 // Subject subject = new Subject();
113 // LoginContext lc;
114 // try {
115 // lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject);
116 // lc.login();
117 // return subject;
118 // } catch (LoginException e) {
119 // throw new CmsException("Cannot login as anonymous", e);
120 // }
121 // }
122
123 static void logFrameworkProperties(CmsLog log) {
124 for (Object sysProp : new TreeSet<Object>(System.getProperties().keySet())) {
125 log.debug(sysProp + "=" + getFrameworkProp(sysProp.toString()));
126 }
127 // String[] keys = { Constants.FRAMEWORK_STORAGE,
128 // Constants.FRAMEWORK_OS_NAME, Constants.FRAMEWORK_OS_VERSION,
129 // Constants.FRAMEWORK_PROCESSOR, Constants.FRAMEWORK_SECURITY,
130 // Constants.FRAMEWORK_TRUST_REPOSITORIES,
131 // Constants.FRAMEWORK_WINDOWSYSTEM, Constants.FRAMEWORK_VENDOR,
132 // Constants.FRAMEWORK_VERSION, Constants.FRAMEWORK_STORAGE_CLEAN,
133 // Constants.FRAMEWORK_LANGUAGE, Constants.FRAMEWORK_UUID };
134 // for (String key : keys)
135 // log.debug(key + "=" + bc.getProperty(key));
136 }
137
138 static void printSystemProperties(PrintStream out) {
139 TreeMap<String, String> display = new TreeMap<>();
140 for (Object key : System.getProperties().keySet())
141 display.put(key.toString(), System.getProperty(key.toString()));
142 for (String key : display.keySet())
143 out.println(key + "=" + display.get(key));
144 }
145
146 // static Session openAdminSession(Repository repository) {
147 // return openAdminSession(repository, null);
148 // }
149 //
150 // static Session openAdminSession(final Repository repository, final String workspaceName) {
151 // LoginContext loginContext = loginAsDataAdmin();
152 // return Subject.doAs(loginContext.getSubject(), new PrivilegedAction<Session>() {
153 //
154 // @Override
155 // public Session run() {
156 // try {
157 // return repository.login(workspaceName);
158 // } catch (RepositoryException e) {
159 // throw new IllegalStateException("Cannot open admin session", e);
160 // } finally {
161 // try {
162 // loginContext.logout();
163 // } catch (LoginException e) {
164 // throw new IllegalStateException(e);
165 // }
166 // }
167 // }
168 //
169 // });
170 // }
171 //
172 // static LoginContext loginAsDataAdmin() {
173 // ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
174 // Thread.currentThread().setContextClassLoader(KernelUtils.class.getClassLoader());
175 // LoginContext loginContext;
176 // try {
177 // loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_DATA_ADMIN);
178 // loginContext.login();
179 // } catch (LoginException e1) {
180 // throw new IllegalStateException("Could not login as data admin", e1);
181 // } finally {
182 // Thread.currentThread().setContextClassLoader(currentCl);
183 // }
184 // return loginContext;
185 // }
186
187 // static void doAsDataAdmin(Runnable action) {
188 // LoginContext loginContext = loginAsDataAdmin();
189 // Subject.doAs(loginContext.getSubject(), new PrivilegedAction<Void>() {
190 //
191 // @Override
192 // public Void run() {
193 // try {
194 // action.run();
195 // return null;
196 // } finally {
197 // try {
198 // loginContext.logout();
199 // } catch (LoginException e) {
200 // throw new IllegalStateException(e);
201 // }
202 // }
203 // }
204 //
205 // });
206 // }
207
208 // public static void asyncOpen(ServiceTracker<?, ?> st) {
209 // Runnable run = new Runnable() {
210 //
211 // @Override
212 // public void run() {
213 // st.open();
214 // }
215 // };
216 // Activator.getInternalExecutorService().execute(run);
217 //// new Thread(run, "Open service tracker " + st).start();
218 // }
219
220 // static BundleContext getBundleContext() {
221 // return Activator.getBundleContext();
222 // }
223
224 static boolean asBoolean(String value) {
225 if (value == null)
226 return false;
227 switch (value) {
228 case "true":
229 return true;
230 case "false":
231 return false;
232 default:
233 throw new IllegalArgumentException("Unsupported value for boolean attribute : " + value);
234 }
235 }
236
237 private static URI safeUri(String uri) {
238 if (uri == null)
239 throw new IllegalArgumentException("URI cannot be null");
240 try {
241 return new URI(uri);
242 } catch (URISyntaxException e) {
243 throw new IllegalArgumentException("Badly formatted URI " + uri, e);
244 }
245 }
246
247 private KernelUtils() {
248
249 }
250 }