]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/kernel/KernelUtils.java
Rather use DataAdmin login context
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / 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.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.node.NodeConstants;
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 final static String OSGI_INSTANCE_AREA = "osgi.instance.area";
34 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,
87 // e);
88 // }
89 // }
90
91 static File getOsgiConfigurationFile(String relativePath) {
92 try {
93 return new File(new URI(getBundleContext().getProperty(OSGI_CONFIGURATION_AREA) + relativePath))
94 .getCanonicalFile();
95 } catch (Exception e) {
96 throw new CmsException("Cannot get configuration file for " + relativePath, e);
97 }
98 }
99
100 static String getFrameworkProp(String key, String def) {
101 String value = getBundleContext().getProperty(key);
102 if (value == null)
103 return def;
104 return value;
105 }
106
107 static String getFrameworkProp(String key) {
108 return getFrameworkProp(key, null);
109 }
110
111 // Security
112 @Deprecated
113 static Subject anonymousLogin() {
114 Subject subject = new Subject();
115 LoginContext lc;
116 try {
117 lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject);
118 lc.login();
119 return subject;
120 } catch (LoginException e) {
121 throw new CmsException("Cannot login as anonymous", e);
122 }
123 }
124
125 // HTTP
126 static void logRequestHeaders(Log log, HttpServletRequest request) {
127 if (!log.isDebugEnabled())
128 return;
129 for (Enumeration<String> headerNames = request.getHeaderNames(); headerNames.hasMoreElements();) {
130 String headerName = headerNames.nextElement();
131 Object headerValue = request.getHeader(headerName);
132 log.debug(headerName + ": " + headerValue);
133 }
134 log.debug(request.getRequestURI() + "\n");
135 }
136
137 static void logFrameworkProperties(Log log) {
138 BundleContext bc = getBundleContext();
139 for (Object sysProp : new TreeSet<Object>(System.getProperties().keySet())) {
140 log.debug(sysProp + "=" + bc.getProperty(sysProp.toString()));
141 }
142 // String[] keys = { Constants.FRAMEWORK_STORAGE,
143 // Constants.FRAMEWORK_OS_NAME, Constants.FRAMEWORK_OS_VERSION,
144 // Constants.FRAMEWORK_PROCESSOR, Constants.FRAMEWORK_SECURITY,
145 // Constants.FRAMEWORK_TRUST_REPOSITORIES,
146 // Constants.FRAMEWORK_WINDOWSYSTEM, Constants.FRAMEWORK_VENDOR,
147 // Constants.FRAMEWORK_VERSION, Constants.FRAMEWORK_STORAGE_CLEAN,
148 // Constants.FRAMEWORK_LANGUAGE, Constants.FRAMEWORK_UUID };
149 // for (String key : keys)
150 // log.debug(key + "=" + bc.getProperty(key));
151 }
152
153 static Session openAdminSession(Repository repository) {
154 return openAdminSession(repository, null);
155 }
156
157 static Session openAdminSession(final Repository repository, final String workspaceName) {
158 ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
159 Thread.currentThread().setContextClassLoader(KernelUtils.class.getClassLoader());
160 LoginContext loginContext;
161 try {
162 loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_DATA_ADMIN);
163 loginContext.login();
164 } catch (LoginException e1) {
165 throw new CmsException("Could not login as data admin", e1);
166 } finally {
167 Thread.currentThread().setContextClassLoader(currentCl);
168 }
169 return Subject.doAs(loginContext.getSubject(), new PrivilegedAction<Session>() {
170
171 @Override
172 public Session run() {
173 try {
174 return repository.login(workspaceName);
175 } catch (RepositoryException e) {
176 throw new CmsException("Cannot open admin session", e);
177 }
178 }
179
180 });
181 }
182
183 /**
184 * @return the {@link BundleContext} of the {@link Bundle} which provided
185 * this class, never null.
186 * @throws CmsException
187 * if the related bundle is not active
188 */
189 public static BundleContext getBundleContext(Class<?> clzz) {
190 Bundle bundle = FrameworkUtil.getBundle(clzz);
191 BundleContext bc = bundle.getBundleContext();
192 if (bc == null)
193 throw new CmsException("Bundle " + bundle.getSymbolicName() + " is not active");
194 return bc;
195 }
196
197 private static BundleContext getBundleContext() {
198 return getBundleContext(KernelUtils.class);
199 }
200
201 private static URI safeUri(String uri) {
202 if (uri == null)
203 throw new CmsException("URI cannot be null");
204 try {
205 return new URI(uri);
206 } catch (URISyntaxException e) {
207 throw new CmsException("Dadly formatted URI " + uri, e);
208 }
209 }
210
211
212 private KernelUtils() {
213
214 }
215 }