]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/util/LangUtils.java
8810d646335056a604791cec8a545b27931be1b9
1 package org
.argeo
.util
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.io
.OutputStream
;
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
;
15 import javax
.naming
.InvalidNameException
;
16 import javax
.naming
.ldap
.LdapName
;
18 public class LangUtils
{
23 * Returns an array with the names of the provided classes. Useful when
24 * registering services with multiple interfaces in OSGi.
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();
38 * Creates a new {@link Dictionary} with one key-value pair (neither key not
39 * value should be null)
41 public static Dictionary
<String
, Object
> init(String key
, Object value
) {
44 Hashtable
<String
, Object
> props
= new Hashtable
<>();
45 props
.put(key
, value
);
50 * Wraps the keys of the provided {@link Dictionary} as an {@link Iterable}.
52 public static Iterable
<String
> keys(Dictionary
<String
, ?
> props
) {
54 return new DictionaryKeys(props
);
57 public static String
toJson(Dictionary
<String
, ?
> props
) {
58 return toJson(props
, false);
61 public static String
toJson(Dictionary
<String
, ?
> props
, boolean pretty
) {
62 StringBuilder sb
= new StringBuilder();
66 Enumeration
<String
> keys
= props
.keys();
67 while (keys
.hasMoreElements()) {
68 String key
= keys
.nextElement();
71 sb
.append('\"').append(key
).append('\"');
76 sb
.append('\"').append(props
.get(key
)).append('\"');
77 if (keys
.hasMoreElements())
86 public static void storeAsProperties(Dictionary
<String
, Object
> props
, Path path
) throws IOException
{
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());
94 try (OutputStream out
= Files
.newOutputStream(path
)) {
95 toStore
.store(out
, null);
99 public static void appendAsLdif(String dnBase
, String dnKey
, Dictionary
<String
, Object
> props
, Path path
)
102 throw new IllegalArgumentException("Props cannot be null");
103 Object dnValue
= props
.get(dnKey
);
104 String dnStr
= dnKey
+ '=' + dnValue
+ ',' + dnBase
;
107 dn
= new LdapName(dnStr
);
108 } catch (InvalidNameException e
) {
109 throw new IllegalArgumentException("Cannot interpret DN " + dnStr
, e
);
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());
117 for (Enumeration
<String
> keys
= props
.keys(); keys
.hasMoreElements();) {
118 String key
= keys
.nextElement();
119 Object value
= props
.get(key
);
122 // FIXME deal with binary and multiple values
123 writer
.append(value
.toString());
129 public static Dictionary
<String
, Object
> loadFromProperties(Path path
) throws IOException
{
130 Properties toLoad
= new Properties();
131 try (InputStream in
= Files
.newInputStream(path
)) {
134 Dictionary
<String
, Object
> res
= new Hashtable
<String
, Object
>();
135 for (Object key
: toLoad
.keySet())
136 res
.put(key
.toString(), toLoad
.get(key
));
144 * Chain the messages of all causes (one per line, <b>starts with a line
145 * return</b>) without all the stack
147 public static String
chainCausesMessages(Throwable t
) {
148 StringBuffer buf
= new StringBuffer();
149 chainCauseMessage(buf
, t
);
150 return buf
.toString();
153 /** Recursive chaining of messages */
154 private static void chainCauseMessage(StringBuffer buf
, Throwable t
) {
155 buf
.append('\n').append(' ').append(t
.getClass().getCanonicalName())
156 .append(": ").append(t
.getMessage());
157 if (t
.getCause() != null)
158 chainCauseMessage(buf
, t
.getCause());
161 /** Singleton constructor. */
162 private LangUtils() {