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