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