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