]> git.argeo.org Git - lgpl/argeo-commons.git/blob - KernelUtils.java
60c796af73280639bc8f03868ec5ec67ed03cbb9
[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 public 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 != null)
78 return safeUri(osgiInstanceBaseUri + (relativePath != null ? relativePath : ""));
79 else
80 return Paths.get(System.getProperty("user.dir"), (relativePath != null ? relativePath : "")).toUri();
81 }
82
83 static File getOsgiConfigurationFile(String relativePath) {
84 try {
85 return new File(
86 new URI(CmsActivator.getBundleContext().getProperty(OSGI_CONFIGURATION_AREA) + relativePath))
87 .getCanonicalFile();
88 } catch (Exception e) {
89 throw new IllegalArgumentException("Cannot get configuration file for " + relativePath, e);
90 }
91 }
92
93 static String getFrameworkProp(String key, String def) {
94 String value;
95 if (CmsActivator.getBundleContext() != null)
96 value = CmsActivator.getBundleContext().getProperty(key);
97 else
98 value = System.getProperty(key);
99 if (value == null)
100 return def;
101 return value;
102 }
103
104 public static String getFrameworkProp(String key) {
105 return getFrameworkProp(key, null);
106 }
107
108 // Security
109 // static Subject anonymousLogin() {
110 // Subject subject = new Subject();
111 // LoginContext lc;
112 // try {
113 // lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject);
114 // lc.login();
115 // return subject;
116 // } catch (LoginException e) {
117 // throw new CmsException("Cannot login as anonymous", e);
118 // }
119 // }
120
121 static void logFrameworkProperties(CmsLog log) {
122 for (Object sysProp : new TreeSet<Object>(System.getProperties().keySet())) {
123 log.debug(sysProp + "=" + getFrameworkProp(sysProp.toString()));
124 }
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));
134 }
135
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));
142 }
143
144 // static Session openAdminSession(Repository repository) {
145 // return openAdminSession(repository, null);
146 // }
147 //
148 // static Session openAdminSession(final Repository repository, final String workspaceName) {
149 // LoginContext loginContext = loginAsDataAdmin();
150 // return Subject.doAs(loginContext.getSubject(), new PrivilegedAction<Session>() {
151 //
152 // @Override
153 // public Session run() {
154 // try {
155 // return repository.login(workspaceName);
156 // } catch (RepositoryException e) {
157 // throw new IllegalStateException("Cannot open admin session", e);
158 // } finally {
159 // try {
160 // loginContext.logout();
161 // } catch (LoginException e) {
162 // throw new IllegalStateException(e);
163 // }
164 // }
165 // }
166 //
167 // });
168 // }
169 //
170 // static LoginContext loginAsDataAdmin() {
171 // ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
172 // Thread.currentThread().setContextClassLoader(KernelUtils.class.getClassLoader());
173 // LoginContext loginContext;
174 // try {
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);
179 // } finally {
180 // Thread.currentThread().setContextClassLoader(currentCl);
181 // }
182 // return loginContext;
183 // }
184
185 // static void doAsDataAdmin(Runnable action) {
186 // LoginContext loginContext = loginAsDataAdmin();
187 // Subject.doAs(loginContext.getSubject(), new PrivilegedAction<Void>() {
188 //
189 // @Override
190 // public Void run() {
191 // try {
192 // action.run();
193 // return null;
194 // } finally {
195 // try {
196 // loginContext.logout();
197 // } catch (LoginException e) {
198 // throw new IllegalStateException(e);
199 // }
200 // }
201 // }
202 //
203 // });
204 // }
205
206 // public static void asyncOpen(ServiceTracker<?, ?> st) {
207 // Runnable run = new Runnable() {
208 //
209 // @Override
210 // public void run() {
211 // st.open();
212 // }
213 // };
214 // Activator.getInternalExecutorService().execute(run);
215 //// new Thread(run, "Open service tracker " + st).start();
216 // }
217
218 // static BundleContext getBundleContext() {
219 // return Activator.getBundleContext();
220 // }
221
222 static boolean asBoolean(String value) {
223 if (value == null)
224 return false;
225 switch (value) {
226 case "true":
227 return true;
228 case "false":
229 return false;
230 default:
231 throw new IllegalArgumentException("Unsupported value for boolean attribute : " + value);
232 }
233 }
234
235 private static URI safeUri(String uri) {
236 if (uri == null)
237 throw new IllegalArgumentException("URI cannot be null");
238 try {
239 return new URI(uri);
240 } catch (URISyntaxException e) {
241 throw new IllegalArgumentException("Badly formatted URI " + uri, e);
242 }
243 }
244
245 private KernelUtils() {
246
247 }
248 }