Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RiceUtilities |
|
| 4.454545454545454;4.455 | ||||
RiceUtilities$1 |
|
| 4.454545454545454;4.455 | ||||
RiceUtilities$AbsoluteFileSystemResourceLoader |
|
| 4.454545454545454;4.455 |
1 | /* | |
2 | * Copyright 2006-2011 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.opensource.org/licenses/ecl2.php | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | package org.kuali.rice.core.util; | |
18 | ||
19 | import org.apache.commons.lang.StringUtils; | |
20 | import org.kuali.rice.core.api.config.property.ConfigContext; | |
21 | import org.springframework.core.io.DefaultResourceLoader; | |
22 | import org.springframework.core.io.FileSystemResource; | |
23 | import org.springframework.core.io.FileSystemResourceLoader; | |
24 | import org.springframework.core.io.Resource; | |
25 | ||
26 | import java.io.IOException; | |
27 | import java.io.InputStream; | |
28 | import java.io.PrintWriter; | |
29 | import java.io.StringWriter; | |
30 | import java.net.InetAddress; | |
31 | import java.net.MalformedURLException; | |
32 | import java.net.NetworkInterface; | |
33 | import java.net.SocketException; | |
34 | import java.net.UnknownHostException; | |
35 | import java.util.Enumeration; | |
36 | ||
37 | /** | |
38 | * | |
39 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
40 | */ | |
41 | public final class RiceUtilities { | |
42 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RiceUtilities.class); |
43 | 0 | private static final String[] TRUE_VALUES = new String[] { "true", "yes", "t", "y" }; |
44 | ||
45 | 0 | private static String instanceIpAddress = null; |
46 | 0 | private static String instanceHostName = null; |
47 | ||
48 | 0 | private RiceUtilities() { |
49 | 0 | throw new UnsupportedOperationException("do not call"); |
50 | } | |
51 | ||
52 | public static boolean getBooleanValueForString(String value, boolean defaultValue) { | |
53 | 0 | if (!StringUtils.isBlank(value)) { |
54 | 0 | for (String trueValue : TRUE_VALUES) { |
55 | 0 | if (value.equalsIgnoreCase(trueValue)) { |
56 | 0 | return true; |
57 | } | |
58 | } | |
59 | 0 | return false; |
60 | } | |
61 | 0 | return defaultValue; |
62 | } | |
63 | ||
64 | public static String collectStackTrace(Throwable t) { | |
65 | 0 | StringWriter sw = new StringWriter(); |
66 | 0 | PrintWriter pw = new PrintWriter(sw); |
67 | 0 | t.printStackTrace(pw); |
68 | 0 | pw.close(); |
69 | 0 | return sw.toString(); |
70 | } | |
71 | ||
72 | public static String getIpNumber() { | |
73 | 0 | if ( instanceIpAddress == null ) { |
74 | // protect from running upon startup | |
75 | 0 | if ( ConfigContext.getCurrentContextConfig() != null ) { |
76 | // attempt to load from environment | |
77 | 0 | String ip = System.getProperty("host.ip"); |
78 | 0 | if ( StringUtils.isBlank(ip) ) { |
79 | 0 | ip = ConfigContext.getCurrentContextConfig().getProperty("host.ip"); |
80 | } | |
81 | // if not set at all just return | |
82 | 0 | if ( StringUtils.isBlank(ip) ) { |
83 | 0 | return getCurrentEnvironmentNetworkIp(); |
84 | } else { | |
85 | // ok - it was set in configuration or by this method, set it permanently for this instance | |
86 | 0 | instanceIpAddress = ip; |
87 | } | |
88 | 0 | } else { |
89 | // prior to startup, just return it | |
90 | 0 | return getCurrentEnvironmentNetworkIp(); |
91 | } | |
92 | } | |
93 | 0 | return instanceIpAddress; |
94 | } | |
95 | ||
96 | /** * @return the current environment's IP address, taking into account the Internet connection to any of the available | |
97 | * machine's Network interfaces. Examples of the outputs can be in octatos or in IPV6 format. | |
98 | * | |
99 | * fec0:0:0:9:213:e8ff:fef1:b717%4 siteLocal: true isLoopback: false isIPV6: true | |
100 | * ============================================ 130.212.150.216 <<<<<<<<<<<------------- This is the one we | |
101 | * want to grab so that we can. siteLocal: false address the DSP on the network. isLoopback: false isIPV6: | |
102 | * false ==> lo ============================================ 0:0:0:0:0:0:0:1%1 siteLocal: false isLoopback: | |
103 | * true isIPV6: true ============================================ 127.0.0.1 siteLocal: false isLoopback: | |
104 | * true isIPV6: false | |
105 | */ | |
106 | public static String getCurrentEnvironmentNetworkIp() { | |
107 | 0 | Enumeration<NetworkInterface> netInterfaces = null; |
108 | try { | |
109 | 0 | netInterfaces = NetworkInterface.getNetworkInterfaces(); |
110 | 0 | } catch (SocketException e) { |
111 | 0 | LOG.error("Somehow we have a socket error...",e); |
112 | 0 | return "127.0.0.1"; |
113 | 0 | } |
114 | ||
115 | 0 | while (netInterfaces.hasMoreElements()) { |
116 | 0 | NetworkInterface ni = netInterfaces.nextElement(); |
117 | 0 | Enumeration<InetAddress> address = ni.getInetAddresses(); |
118 | 0 | while (address.hasMoreElements()) { |
119 | 0 | InetAddress addr = address.nextElement(); |
120 | 0 | if (!addr.isLoopbackAddress() && !addr.isSiteLocalAddress() |
121 | && !(addr.getHostAddress().indexOf(":") > -1)) { | |
122 | 0 | return addr.getHostAddress(); |
123 | } | |
124 | 0 | } |
125 | 0 | } |
126 | try { | |
127 | 0 | return InetAddress.getLocalHost().getHostAddress(); |
128 | 0 | } catch (UnknownHostException e) { |
129 | 0 | return "127.0.0.1"; |
130 | } | |
131 | } | |
132 | ||
133 | ||
134 | public static String getHostName() { | |
135 | 0 | if ( instanceHostName == null ) { |
136 | try { | |
137 | // protect from running upon startup | |
138 | 0 | if ( ConfigContext.getCurrentContextConfig() != null ) { |
139 | 0 | String host = System.getProperty("host.name"); |
140 | 0 | if ( StringUtils.isBlank(host) ) { |
141 | 0 | host = ConfigContext.getCurrentContextConfig().getProperty("host.name"); |
142 | } | |
143 | // if not set at all just return | |
144 | 0 | if ( StringUtils.isBlank(host) ) { |
145 | 0 | return InetAddress.getByName( getCurrentEnvironmentNetworkIp() ).getHostName(); |
146 | } else { | |
147 | // ok - it was set in configuration or by this method, set it permanently for this instance | |
148 | 0 | instanceHostName = host; |
149 | } | |
150 | 0 | } else { |
151 | // prior to startup, just return it | |
152 | 0 | return InetAddress.getByName( getCurrentEnvironmentNetworkIp() ).getHostName(); |
153 | } | |
154 | 0 | } catch ( Exception ex ) { |
155 | 0 | return "localhost"; |
156 | 0 | } |
157 | } | |
158 | 0 | return instanceHostName; |
159 | } | |
160 | ||
161 | /** | |
162 | * The standard Spring FileSystemResourceLoader does not support normal absolute file paths | |
163 | * for historical backwards-compatibility reasons. This class simply circumvents that behavior | |
164 | * to allow proper interpretation of absolute paths (i.e. not stripping a leading slash) | |
165 | */ | |
166 | 0 | private static class AbsoluteFileSystemResourceLoader extends FileSystemResourceLoader { |
167 | @Override | |
168 | protected Resource getResourceByPath(String path) { | |
169 | 0 | return new FileSystemResource(path); |
170 | } | |
171 | } | |
172 | ||
173 | /** | |
174 | * Attempts to retrieve the resource stream. | |
175 | * | |
176 | * @param resourceLoc resource location; syntax supported by {@link DefaultResourceLoader} | |
177 | * @return the resource stream or null if the resource could not be obtained | |
178 | * @throws MalformedURLException | |
179 | * @throws IOException | |
180 | * @see DefaultResourceLoader | |
181 | */ | |
182 | public static InputStream getResourceAsStream(String resourceLoc) throws MalformedURLException, IOException { | |
183 | 0 | Resource resource = getResource(resourceLoc); |
184 | 0 | if (resource.exists()) { |
185 | 0 | return resource.getInputStream(); |
186 | } | |
187 | 0 | return null; |
188 | } | |
189 | ||
190 | /** | |
191 | * Returns a handle to the requested Resource. | |
192 | * | |
193 | * @param resourceLoc resource location; syntax supported by {@link DefaultResourceLoader} | |
194 | * @return a handle to the Resource | |
195 | */ | |
196 | public static Resource getResource(String resourceLoc) { | |
197 | 0 | AbsoluteFileSystemResourceLoader resourceLoader = new AbsoluteFileSystemResourceLoader(); |
198 | 0 | resourceLoader.setClassLoader(ClassLoaderUtils.getDefaultClassLoader()); |
199 | 0 | return resourceLoader.getResource(resourceLoc); |
200 | } | |
201 | ||
202 | /** | |
203 | * This method searches for an exception of the specified type in the stack trace of the given | |
204 | * exception. | |
205 | * @param topLevelException the exception whose stack to traverse | |
206 | * @param exceptionClass the exception class to look for | |
207 | * @return the first instance of an exception of the specified class if found, or null otherwise | |
208 | */ | |
209 | public static <T extends Throwable> T findExceptionInStack(Throwable topLevelException, Class<T> exceptionClass) { | |
210 | 0 | Throwable t = topLevelException; |
211 | 0 | while (t != null) { |
212 | 0 | if (exceptionClass.isAssignableFrom(t.getClass())) return (T) t; |
213 | 0 | t = t.getCause(); |
214 | } | |
215 | 0 | return null; |
216 | } | |
217 | ||
218 | /** | |
219 | * Converts a given file size in bytes to a unit of bytes, kilobyte, | |
220 | * megabyte, or gigabyte | |
221 | * | |
222 | * @param fileSize | |
223 | * - size in bytes | |
224 | * @return String with format 'size units' | |
225 | */ | |
226 | public static String getFileSizeUnits(Long fileSize) { | |
227 | 0 | Long newFileSize = fileSize; |
228 | 0 | String fileSizeUnits = "bytes"; |
229 | ||
230 | 0 | if (fileSize > 1024) { |
231 | 0 | Long kiloSize = fileSize / 1024; |
232 | ||
233 | 0 | if (kiloSize < 1024) { |
234 | 0 | newFileSize = kiloSize; |
235 | 0 | fileSizeUnits = "KB"; |
236 | } else { | |
237 | 0 | Long megaSize = kiloSize / 1024; |
238 | ||
239 | 0 | if (megaSize < 1024) { |
240 | 0 | newFileSize = megaSize; |
241 | 0 | fileSizeUnits = "MB"; |
242 | } else { | |
243 | 0 | Long gigaSize = megaSize / 1024; |
244 | ||
245 | 0 | newFileSize = gigaSize; |
246 | 0 | fileSizeUnits = "GB"; |
247 | } | |
248 | } | |
249 | } | |
250 | ||
251 | 0 | return newFileSize + fileSizeUnits; |
252 | } | |
253 | } |