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