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