]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/internal/KernelUtils.java
Admin session to the proper content provider workspace
[lgpl/argeo-commons.git] / jcr / org.argeo.cms.jcr / src / org / argeo / cms / jcr / internal / KernelUtils.java
1 package org.argeo.cms.jcr.internal;
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.argeo.api.cms.CmsAuth;
27 import org.argeo.api.cms.CmsLog;
28 import org.argeo.cms.jcr.internal.osgi.CmsJcrActivator;
29 import org.argeo.cms.osgi.DataModelNamespace;
30 import org.osgi.framework.BundleContext;
31 import org.osgi.util.tracker.ServiceTracker;
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 IllegalArgumentException("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 IllegalArgumentException("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 IllegalArgumentException("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 if (osgiInstanceBaseUri != null)
90 return safeUri(osgiInstanceBaseUri + (relativePath != null ? relativePath : ""));
91 else
92 return Paths.get(System.getProperty("user.dir")).toUri();
93 }
94
95 static File getOsgiConfigurationFile(String relativePath) {
96 try {
97 return new File(new URI(getBundleContext().getProperty(OSGI_CONFIGURATION_AREA) + relativePath))
98 .getCanonicalFile();
99 } catch (Exception e) {
100 throw new IllegalArgumentException("Cannot get configuration file for " + relativePath, e);
101 }
102 }
103
104 static String getFrameworkProp(String key, String def) {
105 BundleContext bundleContext = CmsJcrActivator.getBundleContext();
106 String value;
107 if (bundleContext != null)
108 value = bundleContext.getProperty(key);
109 else
110 value = System.getProperty(key);
111 if (value == null)
112 return def;
113 return value;
114 }
115
116 static String getFrameworkProp(String key) {
117 return getFrameworkProp(key, null);
118 }
119
120 // Security
121 // static Subject anonymousLogin() {
122 // Subject subject = new Subject();
123 // LoginContext lc;
124 // try {
125 // lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject);
126 // lc.login();
127 // return subject;
128 // } catch (LoginException e) {
129 // throw new CmsException("Cannot login as anonymous", e);
130 // }
131 // }
132
133 static void logFrameworkProperties(CmsLog log) {
134 BundleContext bc = getBundleContext();
135 for (Object sysProp : new TreeSet<Object>(System.getProperties().keySet())) {
136 log.debug(sysProp + "=" + bc.getProperty(sysProp.toString()));
137 }
138 // String[] keys = { Constants.FRAMEWORK_STORAGE,
139 // Constants.FRAMEWORK_OS_NAME, Constants.FRAMEWORK_OS_VERSION,
140 // Constants.FRAMEWORK_PROCESSOR, Constants.FRAMEWORK_SECURITY,
141 // Constants.FRAMEWORK_TRUST_REPOSITORIES,
142 // Constants.FRAMEWORK_WINDOWSYSTEM, Constants.FRAMEWORK_VENDOR,
143 // Constants.FRAMEWORK_VERSION, Constants.FRAMEWORK_STORAGE_CLEAN,
144 // Constants.FRAMEWORK_LANGUAGE, Constants.FRAMEWORK_UUID };
145 // for (String key : keys)
146 // log.debug(key + "=" + bc.getProperty(key));
147 }
148
149 static void printSystemProperties(PrintStream out) {
150 TreeMap<String, String> display = new TreeMap<>();
151 for (Object key : System.getProperties().keySet())
152 display.put(key.toString(), System.getProperty(key.toString()));
153 for (String key : display.keySet())
154 out.println(key + "=" + display.get(key));
155 }
156
157 static Session openAdminSession(Repository repository) {
158 return openAdminSession(repository, null);
159 }
160
161 static Session openAdminSession(final Repository repository, final String workspaceName) {
162 LoginContext loginContext = loginAsDataAdmin();
163 return Subject.doAs(loginContext.getSubject(), new PrivilegedAction<Session>() {
164
165 @Override
166 public Session run() {
167 try {
168 return repository.login(workspaceName);
169 } catch (RepositoryException e) {
170 throw new IllegalStateException("Cannot open admin session", e);
171 } finally {
172 try {
173 loginContext.logout();
174 } catch (LoginException e) {
175 throw new IllegalStateException(e);
176 }
177 }
178 }
179
180 });
181 }
182
183 static LoginContext loginAsDataAdmin() {
184 ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
185 Thread.currentThread().setContextClassLoader(KernelUtils.class.getClassLoader());
186 LoginContext loginContext;
187 try {
188 loginContext = new LoginContext(CmsAuth.LOGIN_CONTEXT_DATA_ADMIN);
189 loginContext.login();
190 } catch (LoginException e1) {
191 throw new IllegalStateException("Could not login as data admin", e1);
192 } finally {
193 Thread.currentThread().setContextClassLoader(currentCl);
194 }
195 return loginContext;
196 }
197
198 static void doAsDataAdmin(Runnable action) {
199 LoginContext loginContext = loginAsDataAdmin();
200 Subject.doAs(loginContext.getSubject(), new PrivilegedAction<Void>() {
201
202 @Override
203 public Void run() {
204 try {
205 action.run();
206 return null;
207 } finally {
208 try {
209 loginContext.logout();
210 } catch (LoginException e) {
211 throw new IllegalStateException(e);
212 }
213 }
214 }
215
216 });
217 }
218
219 static void asyncOpen(ServiceTracker<?, ?> st) {
220 Runnable run = new Runnable() {
221
222 @Override
223 public void run() {
224 st.open();
225 }
226 };
227 // Activator.getInternalExecutorService().execute(run);
228 new Thread(run, "Open service tracker " + st).start();
229 }
230
231 static BundleContext getBundleContext() {
232 return CmsJcrActivator.getBundleContext();
233 }
234
235 static boolean asBoolean(String value) {
236 if (value == null)
237 return false;
238 switch (value) {
239 case "true":
240 return true;
241 case "false":
242 return false;
243 default:
244 throw new IllegalArgumentException(
245 "Unsupported value for attribute " + DataModelNamespace.ABSTRACT + ": " + value);
246 }
247 }
248
249 private static URI safeUri(String uri) {
250 if (uri == null)
251 throw new IllegalArgumentException("URI cannot be null");
252 try {
253 return new URI(uri);
254 } catch (URISyntaxException e) {
255 throw new IllegalArgumentException("Badly formatted URI " + uri, e);
256 }
257 }
258
259 private KernelUtils() {
260
261 }
262 }