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