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