1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.core.util;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.PrintWriter;
22 import java.io.StringWriter;
23 import java.net.InetAddress;
24 import java.net.MalformedURLException;
25 import java.net.NetworkInterface;
26 import java.net.SocketException;
27 import java.net.UnknownHostException;
28 import java.util.Enumeration;
29
30 import org.apache.commons.lang.StringUtils;
31 import org.kuali.rice.core.config.Config;
32 import org.kuali.rice.core.config.ConfigContext;
33 import org.kuali.rice.core.exception.RiceRuntimeException;
34 import org.springframework.core.io.FileSystemResource;
35 import org.springframework.core.io.FileSystemResourceLoader;
36 import org.springframework.core.io.Resource;
37
38
39
40
41
42 public class RiceUtilities {
43 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RiceUtilities.class);
44 private static final String[] TRUE_VALUES = new String[] { "true", "yes", "t", "y" };
45
46 private static String instanceIpAddress = null;
47 private static String instanceHostName = null;
48
49 public static boolean getBooleanValueForString(String value, boolean defaultValue) {
50 if (!StringUtils.isBlank(value)) {
51 for (String trueValue : TRUE_VALUES) {
52 if (value.equalsIgnoreCase(trueValue)) {
53 return true;
54 }
55 }
56 return false;
57 }
58 return defaultValue;
59 }
60
61 public static String collectStackTrace(Throwable t) {
62 StringWriter sw = new StringWriter();
63 PrintWriter pw = new PrintWriter(sw);
64 t.printStackTrace(pw);
65 pw.close();
66 return sw.toString();
67 }
68
69 public static String getIpNumber() {
70 if ( instanceIpAddress == null ) {
71
72 if ( ConfigContext.getCurrentContextConfig() != null ) {
73
74 String ip = System.getProperty("host.ip");
75 if ( StringUtils.isBlank(ip) ) {
76 ip = ConfigContext.getCurrentContextConfig().getProperty("host.ip");
77 }
78
79 if ( StringUtils.isBlank(ip) ) {
80 return getCurrentEnvironmentNetworkIp();
81 } else {
82
83 instanceIpAddress = ip;
84 }
85 } else {
86
87 return getCurrentEnvironmentNetworkIp();
88 }
89 }
90 return instanceIpAddress;
91 }
92
93
94
95
96
97
98
99
100
101
102
103 public static String getCurrentEnvironmentNetworkIp() {
104 Enumeration<NetworkInterface> netInterfaces = null;
105 try {
106 netInterfaces = NetworkInterface.getNetworkInterfaces();
107 } catch (SocketException e) {
108 LOG.error("Somehow we have a socket error...",e);
109 return "127.0.0.1";
110 }
111
112 while (netInterfaces.hasMoreElements()) {
113 NetworkInterface ni = netInterfaces.nextElement();
114 Enumeration<InetAddress> address = ni.getInetAddresses();
115 while (address.hasMoreElements()) {
116 InetAddress addr = address.nextElement();
117 if (!addr.isLoopbackAddress() && !addr.isSiteLocalAddress()
118 && !(addr.getHostAddress().indexOf(":") > -1)) {
119 return addr.getHostAddress();
120 }
121 }
122 }
123 try {
124 return InetAddress.getLocalHost().getHostAddress();
125 } catch (UnknownHostException e) {
126 return "127.0.0.1";
127 }
128 }
129
130
131 public static String getHostName() {
132 if ( instanceHostName == null ) {
133 try {
134
135 if ( ConfigContext.getCurrentContextConfig() != null ) {
136 String host = System.getProperty("host.name");
137 if ( StringUtils.isBlank(host) ) {
138 host = ConfigContext.getCurrentContextConfig().getProperty("host.name");
139 }
140
141 if ( StringUtils.isBlank(host) ) {
142 return InetAddress.getByName( getCurrentEnvironmentNetworkIp() ).getHostName();
143 } else {
144
145 instanceHostName = host;
146 }
147 } else {
148
149 return InetAddress.getByName( getCurrentEnvironmentNetworkIp() ).getHostName();
150 }
151 } catch ( Exception ex ) {
152 return "localhost";
153 }
154 }
155 return instanceHostName;
156 }
157
158
159
160
161
162
163 private static class AbsoluteFileSystemResourceLoader extends FileSystemResourceLoader {
164 @Override
165 protected Resource getResourceByPath(String path) {
166 return new FileSystemResource(path);
167 }
168 }
169
170
171
172
173
174
175
176
177
178
179 public static InputStream getResourceAsStream(String resourceLoc) throws MalformedURLException, IOException {
180 AbsoluteFileSystemResourceLoader rl = new AbsoluteFileSystemResourceLoader();
181 rl.setClassLoader(Thread.currentThread().getContextClassLoader());
182 Resource r = rl.getResource(resourceLoc);
183 if (r.exists()) {
184 return r.getInputStream();
185 } else {
186 return null;
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207 }
208
209
210
211
212
213
214
215
216 public static <T extends Throwable> T findExceptionInStack(Throwable topLevelException, Class<T> exceptionClass) {
217 Throwable t = topLevelException;
218 while (t != null) {
219 if (exceptionClass.isAssignableFrom(t.getClass())) return (T) t;
220 t = t.getCause();
221 }
222 return null;
223 }
224 }