Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   * 
  *   http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.    
  */
 
 package org.apache.tuscany.sca.host.corba;
 
 import java.util.Map;
 import java.util.Set;

Version:
$Rev: 951651 $ $Date: 2010-06-05 08:17:47 +0300 (Sat, 05 Jun 2010) $ Class partially copied from eclipse wst project (http://repo1.maven.org/maven2/org/eclipse/wst/server/core/1.0.205-v20070829b/). Finally we should use jar from maven dependency. Problem described in http://www.fornax-platform.org/cp/display/fornax/2.+Hello+World+Tutorial+(CSC)?replyToComment=2098.comment-2098 needs to be fixed.
 
 public class SocketUtil {
 
     protected static final Object lock = new Object();
 
     private static Set<StringlocalHostCache;
     private static Set<StringnotLocalHostCache = new HashSet<String>();
     private static Map<StringCacheThreadthreadMap = new HashMap<StringCacheThread>();
 
     private static Set<InetAddressaddressCache;
 
     static class CacheThread extends Thread {
         private Set<InetAddresscurrentAddresses;
         private Set<StringaddressList;
         private String host;
         private Set<StringnonAddressList;
         private Map<StringCacheThreadthreadMap2;
 
         public CacheThread(String host,
                            Set<InetAddresscurrentAddresses,
                            Set<StringaddressList,
                            Set<StringnonAddressList,
                            Map<StringCacheThreadthreadMap2) {
             super("Caching localhost information");
             this. = host;
             this. = currentAddresses;
             this. = addressList;
             this. = nonAddressList;
             this. = threadMap2;
         }
 
         public void run() {
             if ( != null) {
                 Iterator<InetAddressiter2 = .iterator();
                 while (iter2.hasNext()) {
                     InetAddress addr = iter2.next();
                     String hostname = addr.getHostName();
                     String hostname2 = addr.getCanonicalHostName();
                     synchronized () {
                         if (hostname != null && !.contains(hostname))
                             .add(hostname);
                         if (hostname2 != null && !.contains(hostname2))
                             .add(hostname2);
                     }
                 }
             }
 
             try {
                 InetAddress[] addrs = InetAddress.getAllByName();
                 int length = addrs.length;
                 for (int j = 0; j < lengthj++) {
                     InetAddress addr = addrs[0];
                     String hostname = addr.getHostName();
                     String hostname2 = addr.getCanonicalHostName();
                     synchronized () {
                         if (addr.isLoopbackAddress()) {
                             if (hostname != null && !.contains(hostname))
                                 .add(hostname);
                             if (hostname2 != null && !.contains(hostname2))
                                 .add(hostname2);
                         } else {
                            if (hostname != null && !.contains(hostname))
                                .add(hostname);
                            if (hostname2 != null && !.contains(hostname2))
                                .add(hostname2);
                        }
                    }
                }
            } catch (UnknownHostException e) {
                synchronized () {
                    if ( != null && !.contains())
                        .add();
                }
            }
            synchronized () {
                .remove();
            }
        }
    }
    public static boolean isLocalhost(final String host) {
        if (host == null || host.equals(""))
            return false;
        if ("localhost".equals(host) || "127.0.0.1".equals(host))
            return true;
        // check simple cases
        try {
            InetAddress localHostaddr = InetAddress.getLocalHost();
            if (localHostaddr.getHostName().equals(host) || host.equals(localHostaddr.getCanonicalHostName())
                || localHostaddr.getHostAddress().equals(host))
                return true;
        } catch (Exception e) {
        }
        // check for current thread and wait if necessary
        boolean currentThread = false;
        try {
            Thread t = null;
            synchronized () {
                t = .get(host);
            }
            if (t != null && t.isAlive()) {
                currentThread = true;
                t.join(30);
            }
        } catch (Exception e) {
        }
        // check if cache is still ok
        boolean refreshedCache = false;
        try {
            // get network interfaces
            final Set<InetAddresscurrentAddresses = new HashSet<InetAddress>();
            currentAddresses.add(InetAddress.getLocalHost());
            Enumeration<?> nis = NetworkInterface.getNetworkInterfaces();
            while (nis.hasMoreElements()) {
                NetworkInterface inter = (NetworkInterface)nis.nextElement();
                Enumeration<InetAddressias = inter.getInetAddresses();
                while (ias.hasMoreElements())
                    currentAddresses.add(ias.nextElement());
            }
            // check if cache is empty or old and refill it if necessary
            if ( == null || !.containsAll(currentAddresses)
                || !currentAddresses.containsAll()) {
                CacheThread cacheThread = null;
                refreshedCache = true;
                synchronized () {
                     = currentAddresses;
                     = new HashSet<String>();
                     = new HashSet<String>(currentAddresses.size() * 3);
                    Iterator<InetAddressiter = currentAddresses.iterator();
                    while (iter.hasNext()) {
                        InetAddress addr = iter.next();
                        String a = addr.getHostAddress();
                        if (a != null && !.contains(a))
                            .add(a);
                    }
                    cacheThread = new CacheThread(hostcurrentAddresses);
                    .put(hostcacheThread);
                    cacheThread.setDaemon(true);
                    cacheThread.setPriority(. - 1);
                    cacheThread.start();
                }
                cacheThread.join(200);
            }
        } catch (Exception e) {
        }
        synchronized () {
            if (.contains(host))
                return true;
            if (.contains(host))
                return false;
        }
        // if the cache hasn't been cleared, maybe we still need to lookup the
        // host
        if (!refreshedCache && !currentThread) {
            try {
                CacheThread cacheThread = null;
                synchronized () {
                    cacheThread = new CacheThread(hostnull);
                    .put(hostcacheThread);
                    cacheThread.setDaemon(true);
                    cacheThread.setPriority(. - 1);
                    cacheThread.start();
                }
                cacheThread.join(75);
                synchronized () {
                    if (.contains(host))
                        return true;
                }
            } catch (Exception e) {
            }
        }
        return false;
    }
New to GrepCode? Check out our FAQ X