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