1 package org
.argeo
.cms
.internal
.kernel
;
4 import java
.io
.IOException
;
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
;
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
;
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
;
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";
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
));
35 static Dictionary
<String
, ?
> asDictionary(ClassLoader cl
, String resource
) {
36 Properties props
= new Properties();
38 props
.load(cl
.getResourceAsStream(resource
));
39 } catch (IOException e
) {
40 throw new CmsException("Cannot load " + resource
41 + " from classpath", e
);
43 return asDictionary(props
);
46 static File
getExecutionDir(String relativePath
) {
47 File executionDir
= new File(getFrameworkProp("user.dir"));
48 if (relativePath
== null)
51 return new File(executionDir
, relativePath
).getCanonicalFile();
52 } catch (IOException e
) {
53 throw new CmsException("Cannot get canonical file", e
);
57 static File
getOsgiInstanceDir() {
58 return new File(Activator
.getBundleContext()
59 .getProperty(OSGI_INSTANCE_AREA
).substring("file:".length()))
63 static String
getOsgiInstancePath(String relativePath
) {
65 if (relativePath
== null)
66 return getOsgiInstanceDir().getCanonicalPath();
68 return new File(getOsgiInstanceDir(), relativePath
)
70 } catch (IOException e
) {
71 throw new CmsException("Cannot get instance path for "
76 static File
getOsgiConfigurationFile(String relativePath
) {
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 "
87 static String
getFrameworkProp(String key
, String def
) {
88 String value
= Activator
.getBundleContext().getProperty(key
);
94 static String
getFrameworkProp(String key
) {
95 return getFrameworkProp(key
, null);
99 static Subject
anonymousLogin() {
100 Subject subject
= new Subject();
103 lc
= new LoginContext(AuthConstants
.LOGIN_CONTEXT_ANONYMOUS
,
107 } catch (LoginException e
) {
108 throw new CmsException("Cannot login as anonymous", e
);
113 // static void anonymousLogin(AuthenticationManager authenticationManager) {
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);
133 static void logRequestHeaders(Log log
, HttpServletRequest request
) {
134 if (!log
.isDebugEnabled())
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
);
145 static void logFrameworkProperties(Log log
) {
146 BundleContext bc
= Activator
.getBundleContext();
147 for (Object sysProp
: new TreeSet
<Object
>(System
.getProperties()
149 log
.debug(sysProp
+ "=" + bc
.getProperty(sysProp
.toString()));
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));
162 private KernelUtils() {