1 package org
.argeo
.cms
.internal
.kernel
;
4 import java
.io
.IOException
;
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
;
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
;
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
;
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";
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
));
42 static Dictionary
<String
, ?
> asDictionary(ClassLoader cl
, String resource
) {
43 Properties props
= new Properties();
45 props
.load(cl
.getResourceAsStream(resource
));
46 } catch (IOException e
) {
47 throw new CmsException("Cannot load " + resource
+ " from classpath", e
);
49 return asDictionary(props
);
52 static File
getExecutionDir(String relativePath
) {
53 File executionDir
= new File(getFrameworkProp("user.dir"));
54 if (relativePath
== null)
57 return new File(executionDir
, relativePath
).getCanonicalFile();
58 } catch (IOException e
) {
59 throw new CmsException("Cannot get canonical file", e
);
63 static File
getOsgiInstanceDir() {
64 return new File(getBundleContext().getProperty(OSGI_INSTANCE_AREA
).substring("file:".length()))
68 static URI
getOsgiInstanceUri(String relativePath
) {
69 String osgiInstanceBaseUri
= getFrameworkProp(OSGI_INSTANCE_AREA
);
71 return new URI(osgiInstanceBaseUri
+ (relativePath
!= null ? relativePath
: ""));
72 } catch (URISyntaxException e
) {
73 throw new CmsException("Cannot get OSGi instance URI for " + relativePath
, e
);
77 static String
getOsgiInstancePath(String relativePath
) {
79 if (relativePath
== null)
80 return getOsgiInstanceDir().getCanonicalPath();
82 return new File(getOsgiInstanceDir(), relativePath
).getCanonicalPath();
83 } catch (IOException e
) {
84 throw new CmsException("Cannot get instance path for " + relativePath
, e
);
88 static File
getOsgiConfigurationFile(String relativePath
) {
90 return new File(new URI(getBundleContext().getProperty(OSGI_CONFIGURATION_AREA
) + relativePath
))
92 } catch (Exception e
) {
93 throw new CmsException("Cannot get configuration file for " + relativePath
, e
);
97 static String
getFrameworkProp(String key
, String def
) {
98 String value
= getBundleContext().getProperty(key
);
104 static String
getFrameworkProp(String key
) {
105 return getFrameworkProp(key
, null);
109 static Subject
anonymousLogin() {
110 Subject subject
= new Subject();
113 lc
= new LoginContext(AuthConstants
.LOGIN_CONTEXT_ANONYMOUS
, subject
);
116 } catch (LoginException e
) {
117 throw new CmsException("Cannot login as anonymous", e
);
122 // static void anonymousLogin(AuthenticationManager authenticationManager) {
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);
142 static void logRequestHeaders(Log log
, HttpServletRequest request
) {
143 if (!log
.isDebugEnabled())
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
);
150 log
.debug(request
.getRequestURI() + "\n");
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()));
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));
169 static Session
openAdminSession(Repository repository
) {
170 return openAdminSession(repository
, null);
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
;
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
);
183 Thread
.currentThread().setContextClassLoader(currentCl
);
185 return Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Session
>() {
188 public Session
run() {
190 return repository
.login(workspaceName
);
191 } catch (RepositoryException e
) {
192 throw new CmsException("Cannot open admin session", e
);
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
205 public static BundleContext
getBundleContext(Class
<?
> clzz
) {
206 Bundle bundle
= FrameworkUtil
.getBundle(clzz
);
207 BundleContext bc
= bundle
.getBundleContext();
209 throw new CmsException("Bundle " + bundle
.getSymbolicName() + " is not active");
213 private static BundleContext
getBundleContext() {
214 return getBundleContext(KernelUtils
.class);
217 private KernelUtils() {