| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| RiceUtilities |
|
| 4.666666666666667;4.667 | ||||
| RiceUtilities$1 |
|
| 4.666666666666667;4.667 | ||||
| RiceUtilities$AbsoluteFileSystemResourceLoader |
|
| 4.666666666666667;4.667 |
| 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.net.InetAddress; | |
| 29 | import java.net.MalformedURLException; | |
| 30 | import java.net.NetworkInterface; | |
| 31 | import java.net.SocketException; | |
| 32 | import java.net.UnknownHostException; | |
| 33 | import java.util.Enumeration; | |
| 34 | ||
| 35 | /** | |
| 36 | * | |
| 37 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
| 38 | */ | |
| 39 | public final class RiceUtilities { | |
| 40 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RiceUtilities.class); |
| 41 | ||
| 42 | 0 | private static String instanceIpAddress = null; |
| 43 | 0 | private static String instanceHostName = null; |
| 44 | ||
| 45 | 0 | private RiceUtilities() { |
| 46 | 0 | throw new UnsupportedOperationException("do not call"); |
| 47 | } | |
| 48 | ||
| 49 | public static String getIpNumber() { | |
| 50 | 0 | if ( instanceIpAddress == null ) { |
| 51 | // protect from running upon startup | |
| 52 | 0 | if ( ConfigContext.getCurrentContextConfig() != null ) { |
| 53 | // attempt to load from environment | |
| 54 | 0 | String ip = System.getProperty("host.ip"); |
| 55 | 0 | if ( StringUtils.isBlank(ip) ) { |
| 56 | 0 | ip = ConfigContext.getCurrentContextConfig().getProperty("host.ip"); |
| 57 | } | |
| 58 | // if not set at all just return | |
| 59 | 0 | if ( StringUtils.isBlank(ip) ) { |
| 60 | 0 | return getCurrentEnvironmentNetworkIp(); |
| 61 | } else { | |
| 62 | // ok - it was set in configuration or by this method, set it permanently for this instance | |
| 63 | 0 | instanceIpAddress = ip; |
| 64 | } | |
| 65 | 0 | } else { |
| 66 | // prior to startup, just return it | |
| 67 | 0 | return getCurrentEnvironmentNetworkIp(); |
| 68 | } | |
| 69 | } | |
| 70 | 0 | return instanceIpAddress; |
| 71 | } | |
| 72 | ||
| 73 | /** * @return the current environment's IP address, taking into account the Internet connection to any of the available | |
| 74 | * machine's Network interfaces. Examples of the outputs can be in octatos or in IPV6 format. | |
| 75 | * | |
| 76 | * fec0:0:0:9:213:e8ff:fef1:b717%4 siteLocal: true isLoopback: false isIPV6: true | |
| 77 | * ============================================ 130.212.150.216 <<<<<<<<<<<------------- This is the one we | |
| 78 | * want to grab so that we can. siteLocal: false address the DSP on the network. isLoopback: false isIPV6: | |
| 79 | * false ==> lo ============================================ 0:0:0:0:0:0:0:1%1 siteLocal: false isLoopback: | |
| 80 | * true isIPV6: true ============================================ 127.0.0.1 siteLocal: false isLoopback: | |
| 81 | * true isIPV6: false | |
| 82 | */ | |
| 83 | private static String getCurrentEnvironmentNetworkIp() { | |
| 84 | 0 | Enumeration<NetworkInterface> netInterfaces = null; |
| 85 | try { | |
| 86 | 0 | netInterfaces = NetworkInterface.getNetworkInterfaces(); |
| 87 | 0 | } catch (SocketException e) { |
| 88 | 0 | LOG.error("Somehow we have a socket error...",e); |
| 89 | 0 | return "127.0.0.1"; |
| 90 | 0 | } |
| 91 | ||
| 92 | 0 | while (netInterfaces.hasMoreElements()) { |
| 93 | 0 | NetworkInterface ni = netInterfaces.nextElement(); |
| 94 | 0 | Enumeration<InetAddress> address = ni.getInetAddresses(); |
| 95 | 0 | while (address.hasMoreElements()) { |
| 96 | 0 | InetAddress addr = address.nextElement(); |
| 97 | 0 | if (!addr.isLoopbackAddress() && !addr.isSiteLocalAddress() |
| 98 | && !(addr.getHostAddress().indexOf(":") > -1)) { | |
| 99 | 0 | return addr.getHostAddress(); |
| 100 | } | |
| 101 | 0 | } |
| 102 | 0 | } |
| 103 | try { | |
| 104 | 0 | return InetAddress.getLocalHost().getHostAddress(); |
| 105 | 0 | } catch (UnknownHostException e) { |
| 106 | 0 | return "127.0.0.1"; |
| 107 | } | |
| 108 | } | |
| 109 | ||
| 110 | ||
| 111 | public static String getHostName() { | |
| 112 | 0 | if ( instanceHostName == null ) { |
| 113 | try { | |
| 114 | // protect from running upon startup | |
| 115 | 0 | if ( ConfigContext.getCurrentContextConfig() != null ) { |
| 116 | 0 | String host = System.getProperty("host.name"); |
| 117 | 0 | if ( StringUtils.isBlank(host) ) { |
| 118 | 0 | host = ConfigContext.getCurrentContextConfig().getProperty("host.name"); |
| 119 | } | |
| 120 | // if not set at all just return | |
| 121 | 0 | if ( StringUtils.isBlank(host) ) { |
| 122 | 0 | return InetAddress.getByName( getCurrentEnvironmentNetworkIp() ).getHostName(); |
| 123 | } else { | |
| 124 | // ok - it was set in configuration or by this method, set it permanently for this instance | |
| 125 | 0 | instanceHostName = host; |
| 126 | } | |
| 127 | 0 | } else { |
| 128 | // prior to startup, just return it | |
| 129 | 0 | return InetAddress.getByName( getCurrentEnvironmentNetworkIp() ).getHostName(); |
| 130 | } | |
| 131 | 0 | } catch ( Exception ex ) { |
| 132 | 0 | return "localhost"; |
| 133 | 0 | } |
| 134 | } | |
| 135 | 0 | return instanceHostName; |
| 136 | } | |
| 137 | ||
| 138 | /** | |
| 139 | * The standard Spring FileSystemResourceLoader does not support normal absolute file paths | |
| 140 | * for historical backwards-compatibility reasons. This class simply circumvents that behavior | |
| 141 | * to allow proper interpretation of absolute paths (i.e. not stripping a leading slash) | |
| 142 | */ | |
| 143 | 0 | private static class AbsoluteFileSystemResourceLoader extends FileSystemResourceLoader { |
| 144 | @Override | |
| 145 | protected Resource getResourceByPath(String path) { | |
| 146 | 0 | return new FileSystemResource(path); |
| 147 | } | |
| 148 | } | |
| 149 | ||
| 150 | /** | |
| 151 | * Attempts to retrieve the resource stream. | |
| 152 | * | |
| 153 | * @param resourceLoc resource location; syntax supported by {@link DefaultResourceLoader} | |
| 154 | * @return the resource stream or null if the resource could not be obtained | |
| 155 | * @throws MalformedURLException | |
| 156 | * @throws IOException | |
| 157 | * @see DefaultResourceLoader | |
| 158 | */ | |
| 159 | public static InputStream getResourceAsStream(String resourceLoc) throws MalformedURLException, IOException { | |
| 160 | 0 | Resource resource = getResource(resourceLoc); |
| 161 | 0 | if (resource.exists()) { |
| 162 | 0 | return resource.getInputStream(); |
| 163 | } | |
| 164 | 0 | return null; |
| 165 | } | |
| 166 | ||
| 167 | /** | |
| 168 | * Returns a handle to the requested Resource. | |
| 169 | * | |
| 170 | * @param resourceLoc resource location; syntax supported by {@link DefaultResourceLoader} | |
| 171 | * @return a handle to the Resource | |
| 172 | */ | |
| 173 | private static Resource getResource(String resourceLoc) { | |
| 174 | 0 | AbsoluteFileSystemResourceLoader resourceLoader = new AbsoluteFileSystemResourceLoader(); |
| 175 | 0 | resourceLoader.setClassLoader(ClassLoaderUtils.getDefaultClassLoader()); |
| 176 | 0 | return resourceLoader.getResource(resourceLoc); |
| 177 | } | |
| 178 | ||
| 179 | /** | |
| 180 | * This method searches for an exception of the specified type in the stack trace of the given | |
| 181 | * exception. | |
| 182 | * @param topLevelException the exception whose stack to traverse | |
| 183 | * @param exceptionClass the exception class to look for | |
| 184 | * @return the first instance of an exception of the specified class if found, or null otherwise | |
| 185 | */ | |
| 186 | public static <T extends Throwable> T findExceptionInStack(Throwable topLevelException, Class<T> exceptionClass) { | |
| 187 | 0 | Throwable t = topLevelException; |
| 188 | 0 | while (t != null) { |
| 189 | 0 | if (exceptionClass.isAssignableFrom(t.getClass())) return (T) t; |
| 190 | 0 | t = t.getCause(); |
| 191 | } | |
| 192 | 0 | return null; |
| 193 | } | |
| 194 | ||
| 195 | /** | |
| 196 | * Converts a given file size in bytes to a unit of bytes, kilobyte, | |
| 197 | * megabyte, or gigabyte | |
| 198 | * | |
| 199 | * @param fileSize | |
| 200 | * - size in bytes | |
| 201 | * @return String with format 'size units' | |
| 202 | */ | |
| 203 | public static String getFileSizeUnits(Long fileSize) { | |
| 204 | 0 | Long newFileSize = fileSize; |
| 205 | 0 | String fileSizeUnits = "bytes"; |
| 206 | ||
| 207 | 0 | if (fileSize > 1024) { |
| 208 | 0 | Long kiloSize = fileSize / 1024; |
| 209 | ||
| 210 | 0 | if (kiloSize < 1024) { |
| 211 | 0 | newFileSize = kiloSize; |
| 212 | 0 | fileSizeUnits = "KB"; |
| 213 | } else { | |
| 214 | 0 | Long megaSize = kiloSize / 1024; |
| 215 | ||
| 216 | 0 | if (megaSize < 1024) { |
| 217 | 0 | newFileSize = megaSize; |
| 218 | 0 | fileSizeUnits = "MB"; |
| 219 | } else { | |
| 220 | 0 | Long gigaSize = megaSize / 1024; |
| 221 | ||
| 222 | 0 | newFileSize = gigaSize; |
| 223 | 0 | fileSizeUnits = "GB"; |
| 224 | } | |
| 225 | } | |
| 226 | } | |
| 227 | ||
| 228 | 0 | return newFileSize + fileSizeUnits; |
| 229 | } | |
| 230 | } |