]> git.argeo.org Git - lgpl/argeo-commons.git/blob - KernelUtils.java
9b43044d446a0e638a8d17292387cfcf525642d8
[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.util.Dictionary;
7 import java.util.Enumeration;
8 import java.util.Hashtable;
9 import java.util.Properties;
10 import java.util.TreeSet;
11
12 import javax.security.auth.Subject;
13 import javax.security.auth.login.LoginContext;
14 import javax.security.auth.login.LoginException;
15 import javax.servlet.http.HttpServletRequest;
16
17 import org.apache.commons.logging.Log;
18 import org.argeo.cms.CmsException;
19 import org.argeo.cms.auth.AuthConstants;
20 import org.osgi.framework.BundleContext;
21
22 /** Package utilities */
23 class KernelUtils implements KernelConstants {
24 private final static String OSGI_INSTANCE_AREA = "osgi.instance.area";
25 private final static String OSGI_CONFIGURATION_AREA = "osgi.configuration.area";
26
27 static Dictionary<String, ?> asDictionary(Properties props) {
28 Hashtable<String, Object> hashtable = new Hashtable<String, Object>();
29 for (Object key : props.keySet()) {
30 hashtable.put(key.toString(), props.get(key));
31 }
32 return hashtable;
33 }
34
35 static Dictionary<String, ?> asDictionary(ClassLoader cl, String resource) {
36 Properties props = new Properties();
37 try {
38 props.load(cl.getResourceAsStream(resource));
39 } catch (IOException e) {
40 throw new CmsException("Cannot load " + resource
41 + " from classpath", e);
42 }
43 return asDictionary(props);
44 }
45
46 static File getExecutionDir(String relativePath) {
47 File executionDir = new File(getFrameworkProp("user.dir"));
48 if (relativePath == null)
49 return executionDir;
50 try {
51 return new File(executionDir, relativePath).getCanonicalFile();
52 } catch (IOException e) {
53 throw new CmsException("Cannot get canonical file", e);
54 }
55 }
56
57 static File getOsgiInstanceDir() {
58 return new File(Activator.getBundleContext()
59 .getProperty(OSGI_INSTANCE_AREA).substring("file:".length()))
60 .getAbsoluteFile();
61 }
62
63 static String getOsgiInstancePath(String relativePath) {
64 try {
65 if (relativePath == null)
66 return getOsgiInstanceDir().getCanonicalPath();
67 else
68 return new File(getOsgiInstanceDir(), relativePath)
69 .getCanonicalPath();
70 } catch (IOException e) {
71 throw new CmsException("Cannot get instance path for "
72 + relativePath, e);
73 }
74 }
75
76 static File getOsgiConfigurationFile(String relativePath) {
77 try {
78 return new File(new URI(Activator.getBundleContext().getProperty(
79 OSGI_CONFIGURATION_AREA)
80 + relativePath)).getCanonicalFile();
81 } catch (Exception e) {
82 throw new CmsException("Cannot get configuration file for "
83 + relativePath, e);
84 }
85 }
86
87 static String getFrameworkProp(String key, String def) {
88 String value = Activator.getBundleContext().getProperty(key);
89 if (value == null)
90 return def;
91 return value;
92 }
93
94 static String getFrameworkProp(String key) {
95 return getFrameworkProp(key, null);
96 }
97
98 // Security
99 static Subject anonymousLogin() {
100 Subject subject = new Subject();
101 LoginContext lc;
102 try {
103 lc = new LoginContext(AuthConstants.LOGIN_CONTEXT_ANONYMOUS,
104 subject);
105 lc.login();
106 return subject;
107 } catch (LoginException e) {
108 throw new CmsException("Cannot login as anonymous", e);
109 }
110 }
111
112 // @Deprecated
113 // static void anonymousLogin(AuthenticationManager authenticationManager) {
114 // try {
115 // List<GrantedAuthorityPrincipal> anonAuthorities = Collections
116 // .singletonList(new GrantedAuthorityPrincipal(
117 // KernelHeader.ROLE_ANONYMOUS));
118 // UserDetails anonUser = new User(KernelHeader.USERNAME_ANONYMOUS,
119 // "", true, true, true, true, anonAuthorities);
120 // AnonymousAuthenticationToken anonToken = new
121 // AnonymousAuthenticationToken(
122 // DEFAULT_SECURITY_KEY, anonUser, anonAuthorities);
123 // Authentication authentication = authenticationManager
124 // .authenticate(anonToken);
125 // SecurityContextHolder.getContext()
126 // .setAuthentication(authentication);
127 // } catch (Exception e) {
128 // throw new CmsException("Cannot authenticate", e);
129 // }
130 // }
131
132 // HTTP
133 static void logRequestHeaders(Log log, HttpServletRequest request) {
134 if (!log.isDebugEnabled())
135 return;
136 for (Enumeration<String> headerNames = request.getHeaderNames(); headerNames
137 .hasMoreElements();) {
138 String headerName = headerNames.nextElement();
139 Object headerValue = request.getHeader(headerName);
140 log.debug(headerName + ": " + headerValue);
141 }
142 log.debug("");
143 }
144
145 static void logFrameworkProperties(Log log) {
146 BundleContext bc = Activator.getBundleContext();
147 for (Object sysProp : new TreeSet<Object>(System.getProperties()
148 .keySet())) {
149 log.debug(sysProp + "=" + bc.getProperty(sysProp.toString()));
150 }
151 // String[] keys = { Constants.FRAMEWORK_STORAGE,
152 // Constants.FRAMEWORK_OS_NAME, Constants.FRAMEWORK_OS_VERSION,
153 // Constants.FRAMEWORK_PROCESSOR, Constants.FRAMEWORK_SECURITY,
154 // Constants.FRAMEWORK_TRUST_REPOSITORIES,
155 // Constants.FRAMEWORK_WINDOWSYSTEM, Constants.FRAMEWORK_VENDOR,
156 // Constants.FRAMEWORK_VERSION, Constants.FRAMEWORK_STORAGE_CLEAN,
157 // Constants.FRAMEWORK_LANGUAGE, Constants.FRAMEWORK_UUID };
158 // for (String key : keys)
159 // log.debug(key + "=" + bc.getProperty(key));
160 }
161
162 private KernelUtils() {
163
164 }
165 }