|  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 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 |     | 
     | 
  |  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 |     | 
                 | 
  |  75 |    0 |                if ( ConfigContext.getCurrentContextConfig() != null ) { | 
  |  76 |     | 
                     | 
  |  77 |    0 |                    String ip = System.getProperty("host.ip"); | 
  |  78 |    0 |                    if ( StringUtils.isBlank(ip) ) { | 
  |  79 |    0 |                        ip = ConfigContext.getCurrentContextConfig().getProperty("host.ip"); | 
  |  80 |     | 
                   }  | 
  |  81 |     | 
                     | 
  |  82 |    0 |                    if ( StringUtils.isBlank(ip) ) {                     | 
  |  83 |    0 |                        return getCurrentEnvironmentNetworkIp();  | 
  |  84 |     | 
                   } else {  | 
  |  85 |     | 
                         | 
  |  86 |    0 |                        instanceIpAddress = ip;  | 
  |  87 |     | 
                   }  | 
  |  88 |    0 |                } else { | 
  |  89 |     | 
                     | 
  |  90 |    0 |                    return getCurrentEnvironmentNetworkIp();  | 
  |  91 |     | 
               }  | 
  |  92 |     | 
           }  | 
  |  93 |    0 |            return instanceIpAddress;  | 
  |  94 |     | 
       }  | 
  |  95 |     | 
     | 
  |  96 |     | 
         | 
  |  97 |     | 
     | 
  |  98 |     | 
     | 
  |  99 |     | 
     | 
  |  100 |     | 
     | 
  |  101 |     | 
     | 
  |  102 |     | 
     | 
  |  103 |     | 
     | 
  |  104 |     | 
     | 
  |  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 |     | 
                     | 
  |  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 |     | 
                         | 
  |  144 |    0 |                        if ( StringUtils.isBlank(host) ) { | 
  |  145 |    0 |                            return InetAddress.getByName( getCurrentEnvironmentNetworkIp() ).getHostName();  | 
  |  146 |     | 
                       } else {  | 
  |  147 |     | 
                             | 
  |  148 |    0 |                            instanceHostName = host;  | 
  |  149 |     | 
                       }  | 
  |  150 |    0 |                    } else { | 
  |  151 |     | 
                         | 
  |  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 |     | 
     | 
  |  163 |     | 
     | 
  |  164 |     | 
     | 
  |  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 |     | 
     | 
  |  175 |     | 
     | 
  |  176 |     | 
     | 
  |  177 |     | 
     | 
  |  178 |     | 
     | 
  |  179 |     | 
     | 
  |  180 |     | 
     | 
  |  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 |     | 
     | 
  |  192 |     | 
     | 
  |  193 |     | 
     | 
  |  194 |     | 
     | 
  |  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 |     | 
     | 
  |  204 |     | 
     | 
  |  205 |     | 
     | 
  |  206 |     | 
     | 
  |  207 |     | 
     | 
  |  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 |     | 
   }  |