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