]> git.argeo.org Git - lgpl/argeo-commons.git/blob - util/LangUtils.java
Prepare next development cycle
[lgpl/argeo-commons.git] / util / LangUtils.java
1 package org.argeo.util;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.io.Writer;
7 import java.nio.file.Files;
8 import java.nio.file.Path;
9 import java.nio.file.StandardOpenOption;
10 import java.util.Dictionary;
11 import java.util.Enumeration;
12 import java.util.Hashtable;
13 import java.util.Properties;
14
15 import javax.naming.InvalidNameException;
16 import javax.naming.ldap.LdapName;
17
18 public class LangUtils {
19 /*
20 * NON-API OSGi
21 */
22 /**
23 * Returns an array with the names of the provided classes. Useful when
24 * registering services with multiple interfaces in OSGi.
25 */
26 public static String[] names(Class<?>... clzz) {
27 String[] res = new String[clzz.length];
28 for (int i = 0; i < clzz.length; i++)
29 res[i] = clzz[i].getName();
30 return res;
31 }
32
33 /*
34 * DICTIONARY
35 */
36
37 /**
38 * Creates a new {@link Dictionary} with one key-value pair (neither key not
39 * value should be null)
40 */
41 public static Dictionary<String, Object> dico(String key, Object value) {
42 assert key != null;
43 assert value != null;
44 Hashtable<String, Object> props = new Hashtable<>();
45 props.put(key, value);
46 return props;
47 }
48
49 /**
50 * Wraps the keys of the provided {@link Dictionary} as an {@link Iterable}.
51 */
52 public static Iterable<String> keys(Dictionary<String, ?> props) {
53 assert props != null;
54 return new DictionaryKeys(props);
55 }
56
57 static String toJson(Dictionary<String, ?> props) {
58 return toJson(props, false);
59 }
60
61 static String toJson(Dictionary<String, ?> props, boolean pretty) {
62 StringBuilder sb = new StringBuilder();
63 sb.append('{');
64 if (pretty)
65 sb.append('\n');
66 Enumeration<String> keys = props.keys();
67 while (keys.hasMoreElements()) {
68 String key = keys.nextElement();
69 if (pretty)
70 sb.append(' ');
71 sb.append('\"').append(key).append('\"');
72 if (pretty)
73 sb.append(" : ");
74 else
75 sb.append(':');
76 sb.append('\"').append(props.get(key)).append('\"');
77 if (keys.hasMoreElements())
78 sb.append(", ");
79 if (pretty)
80 sb.append('\n');
81 }
82 sb.append('}');
83 return sb.toString();
84 }
85
86 static void storeAsProperties(Dictionary<String, Object> props, Path path) throws IOException {
87 if (props == null)
88 throw new IllegalArgumentException("Props cannot be null");
89 Properties toStore = new Properties();
90 for (Enumeration<String> keys = props.keys(); keys.hasMoreElements();) {
91 String key = keys.nextElement();
92 toStore.setProperty(key, props.get(key).toString());
93 }
94 try (OutputStream out = Files.newOutputStream(path)) {
95 toStore.store(out, null);
96 }
97 }
98
99 static void appendAsLdif(String dnBase, String dnKey, Dictionary<String, Object> props, Path path)
100 throws IOException {
101 if (props == null)
102 throw new IllegalArgumentException("Props cannot be null");
103 Object dnValue = props.get(dnKey);
104 String dnStr = dnKey + '=' + dnValue + ',' + dnBase;
105 LdapName dn;
106 try {
107 dn = new LdapName(dnStr);
108 } catch (InvalidNameException e) {
109 throw new IllegalArgumentException("Cannot interpret DN " + dnStr, e);
110 }
111 if (dnValue == null)
112 throw new IllegalArgumentException("DN key " + dnKey + " must have a value");
113 try (Writer writer = Files.newBufferedWriter(path, StandardOpenOption.APPEND, StandardOpenOption.CREATE)) {
114 writer.append("\ndn: ");
115 writer.append(dn.toString());
116 writer.append('\n');
117 for (Enumeration<String> keys = props.keys(); keys.hasMoreElements();) {
118 String key = keys.nextElement();
119 Object value = props.get(key);
120 writer.append(key);
121 writer.append(": ");
122 // FIXME deal with binary and multiple values
123 writer.append(value.toString());
124 writer.append('\n');
125 }
126 }
127 }
128
129 static Dictionary<String, Object> loadFromProperties(Path path) throws IOException {
130 Properties toLoad = new Properties();
131 try (InputStream in = Files.newInputStream(path)) {
132 toLoad.load(in);
133 }
134 Dictionary<String, Object> res = new Hashtable<String, Object>();
135 for (Object key : toLoad.keySet())
136 res.put(key.toString(), toLoad.get(key));
137 return res;
138 }
139
140 /*
141 * EXCEPTIONS
142 */
143 /**
144 * Chain the messages of all causes (one per line, <b>starts with a line
145 * return</b>) without all the stack
146 */
147 public static String chainCausesMessages(Throwable t) {
148 StringBuffer buf = new StringBuffer();
149 chainCauseMessage(buf, t);
150 return buf.toString();
151 }
152
153 /** Recursive chaining of messages */
154 private static void chainCauseMessage(StringBuffer buf, Throwable t) {
155 buf.append('\n').append(' ').append(t.getClass().getCanonicalName()).append(": ").append(t.getMessage());
156 if (t.getCause() != null)
157 chainCauseMessage(buf, t.getCause());
158 }
159
160 /** Singleton constructor. */
161 private LangUtils() {
162
163 }
164
165 }