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