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