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