import java.io.File;
import java.io.IOException;
+import java.net.URI;
import java.util.Dictionary;
+import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
+import java.util.TreeSet;
+import javax.security.auth.Subject;
+import javax.security.auth.login.LoginContext;
+import javax.security.auth.login.LoginException;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.commons.logging.Log;
import org.argeo.cms.CmsException;
+import org.argeo.cms.auth.AuthConstants;
import org.osgi.framework.BundleContext;
-class KernelUtils {
- final static String OSGI_INSTANCE_AREA = "osgi.instance.area";
+/** Package utilities */
+class KernelUtils implements KernelConstants {
+ private final static String OSGI_INSTANCE_AREA = "osgi.instance.area";
+ private final static String OSGI_CONFIGURATION_AREA = "osgi.configuration.area";
static Dictionary<String, ?> asDictionary(Properties props) {
Hashtable<String, Object> hashtable = new Hashtable<String, Object>();
return hashtable;
}
- static Dictionary<String, ?> asDictionary(ClassLoader cl,
- String resource) {
+ static Dictionary<String, ?> asDictionary(ClassLoader cl, String resource) {
Properties props = new Properties();
try {
props.load(cl.getResourceAsStream(resource));
return asDictionary(props);
}
- static File getOsgiInstanceDir(BundleContext bundleContext) {
- return new File(bundleContext.getProperty(OSGI_INSTANCE_AREA)
- .substring("file:".length())).getAbsoluteFile();
+ static File getExecutionDir(String relativePath) {
+ File executionDir = new File(getFrameworkProp("user.dir"));
+ if (relativePath == null)
+ return executionDir;
+ try {
+ return new File(executionDir, relativePath).getCanonicalFile();
+ } catch (IOException e) {
+ throw new CmsException("Cannot get canonical file", e);
+ }
+ }
+
+ static File getOsgiInstanceDir() {
+ return new File(Activator.getBundleContext()
+ .getProperty(OSGI_INSTANCE_AREA).substring("file:".length()))
+ .getAbsoluteFile();
+ }
+
+ static String getOsgiInstancePath(String relativePath) {
+ try {
+ if (relativePath == null)
+ return getOsgiInstanceDir().getCanonicalPath();
+ else
+ return new File(getOsgiInstanceDir(), relativePath)
+ .getCanonicalPath();
+ } catch (IOException e) {
+ throw new CmsException("Cannot get instance path for "
+ + relativePath, e);
+ }
+ }
+
+ static File getOsgiConfigurationFile(String relativePath) {
+ try {
+ return new File(new URI(Activator.getBundleContext().getProperty(
+ OSGI_CONFIGURATION_AREA)
+ + relativePath)).getCanonicalFile();
+ } catch (Exception e) {
+ throw new CmsException("Cannot get configuration file for "
+ + relativePath, e);
+ }
+ }
+
+ static String getFrameworkProp(String key, String def) {
+ String value = Activator.getBundleContext().getProperty(key);
+ if (value == null)
+ return def;
+ return value;
+ }
+
+ static String getFrameworkProp(String key) {
+ return getFrameworkProp(key, null);
+ }
+
+ // Security
+ static Subject anonymousLogin() {
+ Subject subject = new Subject();
+ LoginContext lc;
+ try {
+ lc = new LoginContext(AuthConstants.LOGIN_CONTEXT_ANONYMOUS,
+ subject);
+ lc.login();
+ return subject;
+ } catch (LoginException e) {
+ throw new CmsException("Cannot login as anonymous", e);
+ }
+ }
+
+ // @Deprecated
+ // static void anonymousLogin(AuthenticationManager authenticationManager) {
+ // try {
+ // List<GrantedAuthorityPrincipal> anonAuthorities = Collections
+ // .singletonList(new GrantedAuthorityPrincipal(
+ // KernelHeader.ROLE_ANONYMOUS));
+ // UserDetails anonUser = new User(KernelHeader.USERNAME_ANONYMOUS,
+ // "", true, true, true, true, anonAuthorities);
+ // AnonymousAuthenticationToken anonToken = new
+ // AnonymousAuthenticationToken(
+ // DEFAULT_SECURITY_KEY, anonUser, anonAuthorities);
+ // Authentication authentication = authenticationManager
+ // .authenticate(anonToken);
+ // SecurityContextHolder.getContext()
+ // .setAuthentication(authentication);
+ // } catch (Exception e) {
+ // throw new CmsException("Cannot authenticate", e);
+ // }
+ // }
+
+ // HTTP
+ static void logRequestHeaders(Log log, HttpServletRequest request) {
+ if (!log.isDebugEnabled())
+ return;
+ for (Enumeration<String> headerNames = request.getHeaderNames(); headerNames
+ .hasMoreElements();) {
+ String headerName = headerNames.nextElement();
+ Object headerValue = request.getHeader(headerName);
+ log.debug(headerName + ": " + headerValue);
+ }
+ log.debug("");
+ }
+
+ static void logFrameworkProperties(Log log) {
+ BundleContext bc = Activator.getBundleContext();
+ for (Object sysProp : new TreeSet<Object>(System.getProperties()
+ .keySet())) {
+ log.debug(sysProp + "=" + bc.getProperty(sysProp.toString()));
+ }
+ // String[] keys = { Constants.FRAMEWORK_STORAGE,
+ // Constants.FRAMEWORK_OS_NAME, Constants.FRAMEWORK_OS_VERSION,
+ // Constants.FRAMEWORK_PROCESSOR, Constants.FRAMEWORK_SECURITY,
+ // Constants.FRAMEWORK_TRUST_REPOSITORIES,
+ // Constants.FRAMEWORK_WINDOWSYSTEM, Constants.FRAMEWORK_VENDOR,
+ // Constants.FRAMEWORK_VERSION, Constants.FRAMEWORK_STORAGE_CLEAN,
+ // Constants.FRAMEWORK_LANGUAGE, Constants.FRAMEWORK_UUID };
+ // for (String key : keys)
+ // log.debug(key + "=" + bc.getProperty(key));
}
private KernelUtils() {