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