]> 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.nio.file.Path;
9 import java.nio.file.Paths;
10 import java.security.PrivilegedAction;
11 import java.util.Dictionary;
12 import java.util.Enumeration;
13 import java.util.Hashtable;
14 import java.util.Properties;
15 import java.util.TreeMap;
16 import java.util.TreeSet;
17
18 import javax.jcr.Repository;
19 import javax.jcr.RepositoryException;
20 import javax.jcr.Session;
21 import javax.security.auth.Subject;
22 import javax.security.auth.login.LoginContext;
23 import javax.security.auth.login.LoginException;
24 import javax.servlet.http.HttpServletRequest;
25
26 import org.apache.commons.logging.Log;
27 import org.argeo.cms.CmsException;
28 import org.argeo.node.NodeConstants;
29 import org.osgi.framework.Bundle;
30 import org.osgi.framework.BundleContext;
31 import org.osgi.framework.FrameworkUtil;
32
33 /** Package utilities */
34 class KernelUtils implements KernelConstants {
35 final static String OSGI_INSTANCE_AREA = "osgi.instance.area";
36 final static String OSGI_CONFIGURATION_AREA = "osgi.configuration.area";
37
38 static Dictionary<String, ?> asDictionary(Properties props) {
39 Hashtable<String, Object> hashtable = new Hashtable<String, Object>();
40 for (Object key : props.keySet()) {
41 hashtable.put(key.toString(), props.get(key));
42 }
43 return hashtable;
44 }
45
46 static Dictionary<String, ?> asDictionary(ClassLoader cl, String resource) {
47 Properties props = new Properties();
48 try {
49 props.load(cl.getResourceAsStream(resource));
50 } catch (IOException e) {
51 throw new CmsException("Cannot load " + resource + " from classpath", e);
52 }
53 return asDictionary(props);
54 }
55
56 static File getExecutionDir(String relativePath) {
57 File executionDir = new File(getFrameworkProp("user.dir"));
58 if (relativePath == null)
59 return executionDir;
60 try {
61 return new File(executionDir, relativePath).getCanonicalFile();
62 } catch (IOException e) {
63 throw new CmsException("Cannot get canonical file", e);
64 }
65 }
66
67 static File getOsgiInstanceDir() {
68 return new File(getBundleContext().getProperty(OSGI_INSTANCE_AREA).substring("file:".length()))
69 .getAbsoluteFile();
70 }
71
72 static Path getOsgiInstancePath(String relativePath) {
73 return Paths.get(getOsgiInstanceUri(relativePath));
74 }
75
76 static URI getOsgiInstanceUri(String relativePath) {
77 String osgiInstanceBaseUri = getFrameworkProp(OSGI_INSTANCE_AREA);
78 return safeUri(osgiInstanceBaseUri + (relativePath != null ? relativePath : ""));
79 }
80
81 // static String getOsgiInstancePath(String relativePath) {
82 // try {
83 // if (relativePath == null)
84 // return getOsgiInstanceDir().getCanonicalPath();
85 // else
86 // return new File(getOsgiInstanceDir(), relativePath).getCanonicalPath();
87 // } catch (IOException e) {
88 // throw new CmsException("Cannot get instance path for " + relativePath,
89 // e);
90 // }
91 // }
92
93 static File getOsgiConfigurationFile(String relativePath) {
94 try {
95 return new File(new URI(getBundleContext().getProperty(OSGI_CONFIGURATION_AREA) + relativePath))
96 .getCanonicalFile();
97 } catch (Exception e) {
98 throw new CmsException("Cannot get configuration file for " + relativePath, e);
99 }
100 }
101
102 static String getFrameworkProp(String key, String def) {
103 String value = getBundleContext().getProperty(key);
104 if (value == null)
105 return def;
106 return value;
107 }
108
109 static String getFrameworkProp(String key) {
110 return getFrameworkProp(key, null);
111 }
112
113 // Security
114 static Subject anonymousLogin() {
115 Subject subject = new Subject();
116 LoginContext lc;
117 try {
118 lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject);
119 lc.login();
120 return subject;
121 } catch (LoginException e) {
122 throw new CmsException("Cannot login as anonymous", e);
123 }
124 }
125
126 // HTTP
127 static void logRequestHeaders(Log log, HttpServletRequest request) {
128 if (!log.isDebugEnabled())
129 return;
130 for (Enumeration<String> headerNames = request.getHeaderNames(); headerNames.hasMoreElements();) {
131 String headerName = headerNames.nextElement();
132 Object headerValue = request.getHeader(headerName);
133 log.debug(headerName + ": " + headerValue);
134 }
135 log.debug(request.getRequestURI() + "\n");
136 }
137
138 static void logFrameworkProperties(Log log) {
139 BundleContext bc = getBundleContext();
140 for (Object sysProp : new TreeSet<Object>(System.getProperties().keySet())) {
141 log.debug(sysProp + "=" + bc.getProperty(sysProp.toString()));
142 }
143 // String[] keys = { Constants.FRAMEWORK_STORAGE,
144 // Constants.FRAMEWORK_OS_NAME, Constants.FRAMEWORK_OS_VERSION,
145 // Constants.FRAMEWORK_PROCESSOR, Constants.FRAMEWORK_SECURITY,
146 // Constants.FRAMEWORK_TRUST_REPOSITORIES,
147 // Constants.FRAMEWORK_WINDOWSYSTEM, Constants.FRAMEWORK_VENDOR,
148 // Constants.FRAMEWORK_VERSION, Constants.FRAMEWORK_STORAGE_CLEAN,
149 // Constants.FRAMEWORK_LANGUAGE, Constants.FRAMEWORK_UUID };
150 // for (String key : keys)
151 // log.debug(key + "=" + bc.getProperty(key));
152 }
153
154 static void printSystemProperties(PrintStream out){
155 TreeMap<String, String> display = new TreeMap<>();
156 for (Object key : System.getProperties().keySet())
157 display.put(key.toString(), System.getProperty(key.toString()));
158 for (String key : display.keySet())
159 out.println(key + "=" + display.get(key));
160 }
161
162 static Session openAdminSession(Repository repository) {
163 return openAdminSession(repository, null);
164 }
165
166 static Session openAdminSession(final Repository repository, final String workspaceName) {
167 ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
168 Thread.currentThread().setContextClassLoader(KernelUtils.class.getClassLoader());
169 LoginContext loginContext;
170 try {
171 loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_DATA_ADMIN);
172 loginContext.login();
173 } catch (LoginException e1) {
174 throw new CmsException("Could not login as data admin", e1);
175 } finally {
176 Thread.currentThread().setContextClassLoader(currentCl);
177 }
178 return Subject.doAs(loginContext.getSubject(), new PrivilegedAction<Session>() {
179
180 @Override
181 public Session run() {
182 try {
183 return repository.login(workspaceName);
184 } catch (RepositoryException e) {
185 throw new CmsException("Cannot open admin session", e);
186 }
187 }
188
189 });
190 }
191
192 /**
193 * @return the {@link BundleContext} of the {@link Bundle} which provided
194 * this class, never null.
195 * @throws CmsException
196 * if the related bundle is not active
197 */
198 public static BundleContext getBundleContext(Class<?> clzz) {
199 Bundle bundle = FrameworkUtil.getBundle(clzz);
200 BundleContext bc = bundle.getBundleContext();
201 if (bc == null)
202 throw new CmsException("Bundle " + bundle.getSymbolicName() + " is not active");
203 return bc;
204 }
205
206 private static BundleContext getBundleContext() {
207 return getBundleContext(KernelUtils.class);
208 }
209
210 private static URI safeUri(String uri) {
211 if (uri == null)
212 throw new CmsException("URI cannot be null");
213 try {
214 return new URI(uri);
215 } catch (URISyntaxException e) {
216 throw new CmsException("Dadly formatted URI " + uri, e);
217 }
218 }
219
220
221 private KernelUtils() {
222
223 }
224 }