/
Utils.java
169 lines (147 loc) · 5.26 KB
/
Utils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright 2006-2014 University of Dundee. All rights reserved.
* Use is subject to license terms supplied in LICENSE.txt
*/
package ome.util;
import java.io.Closeable;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
import ome.model.IObject;
import ome.model.internal.Permissions;
/**
* various tools needed throughout Omero.
*
* @author Josh Moore
* <a href="mailto:josh.moore@gmx.de">josh.moore@gmx.de</a>
* @version 1.0
* @since 1.0
* TODO Grinder issues should be moved to test component to reduce deps.
*/
public class Utils {
private final static Logger log = Logger.getLogger(Utils.class.getName());
protected final static String CGLIB_IDENTIFIER = "$$EnhancerByCGLIB$$";
protected final static String JVST_IDENTIFIER = "_$$_jvst";
/**
* finds the "true" class identified by a given Class object. This is
* necessary because of possibly proxied instances.
*
* @param source
* Regular or CGLIB-based class.
* @return the regular Java class.
*/
public static <T extends IObject> Class<T> trueClass(Class<T> source) {
String s = source.getName();
if (s.contains(CGLIB_IDENTIFIER)) { // TODO any other test?
try {
return (Class<T>) Class.forName(s.substring(0, s
.indexOf(CGLIB_IDENTIFIER)));
} catch (ClassNotFoundException e) {
throw new RuntimeException( /* TODO */
"Classname contains " + CGLIB_IDENTIFIER
+ " but base class cannout be found.");
}
} else if (s.contains(JVST_IDENTIFIER)) {
try {
return (Class<T>) Class.forName(s.substring(0, s
.indexOf(JVST_IDENTIFIER)));
} catch (ClassNotFoundException e) {
throw new RuntimeException( /* TODO */
"Classname contains " + JVST_IDENTIFIER
+ " but base class cannout be found.");
}
}
return source;
}
static String msg = "Failed to instantiate %s. This may be caused by an "
+ "abstract class not being properly \"join fetch\"'d. Please review "
+ "your query or contact your server administrator.";
/**
* instantiates an object using the trueClass.
*
* @param source
* Regular or CGLIB-based class.
* @return the regular Java instance.
*/
public static <T extends IObject> T trueInstance(Class<T> source) {
final Class<T> trueClass = trueClass(source);
final T result;
try {
result = trueClass.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException(String.format(msg, trueClass), e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Not allowed to create class:"
+ trueClass, e);
}
return result;
}
/**
* primarily used in Grinder to discover what methods to call
*
* @param clazz
*/
public static <T> String[] getObjectVoidMethods(Class<T> clazz) {
final Set<String> set = new HashSet<String>();
Method[] methods = clazz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if (method.getReturnType().equals(Object.class)) {
if (method.getParameterTypes().length == 0) {
set.add(method.getName());
}
}
}
return set.toArray(new String[set.size()]);
}
/**
* Returns the internal representation of a {@link Permissions} object.
* Should be used with caution!
*/
public static Object internalForm(Permissions p) {
P pp = new P(p);
return Long.valueOf(pp.toLong());
}
/**
* Returns a {@link Permissions} instance from its internal representation.
* Should be used with caution!
*/
public static Permissions toPermissions(Object o) {
P pp = new P((Long) o);
return new Permissions(pp);
}
private static class P extends Permissions {
private static final long serialVersionUID = -18133057809465999L;
protected P(Permissions p) {
revokeAll(p);
grantAll(p);
}
protected P(Long l) {
this.setPerm1(l.longValue());
}
long toLong() {
return super.getPerm1();
}
}
/**
* Returns a {@link String} which can be used to correlate log messages.
*/
public static String getThreadIdentifier() {
return new StringBuilder(32).append(Runtime.getRuntime().hashCode())
.append("::").append(Thread.currentThread().getId()).toString();
}
// Helpers
// =========================================================================
public static void closeQuietly(Closeable is) {
if (is == null) {
log.fine("Closeable is null");
} else {
try {
is.close();
} catch (Exception e) {
log.info("Exception on closing closeable " + is + ":" + e);
}
}
}
}