View Javadoc

1   /*
2    * $Id: HostNameFactory.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.config.factories;
12  
13  import org.mule.api.config.PropertyFactory;
14  
15  import java.net.InetAddress;
16  import java.util.Map;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  
21  /**
22   * Extracts the local hostname from the local system
23   */
24  public class HostNameFactory implements PropertyFactory
25  {
26      protected static final Log logger = LogFactory.getLog(HostNameFactory.class);
27  
28      public Object create(Map<?, ?> props) throws Exception
29      {
30          // we could use getCanonicalHostName here.  however, on machines behind
31          // NAT firewalls it seems that is often the NAT address, which corresponds
32          // to an interface on the firewall, not on the local machine.
33          try
34          {
35              return InetAddress.getLocalHost().getHostName();
36          }
37          catch (Exception e)
38          {
39              logger.warn("Unable to resolve hostname, defaulting to 'localhost': " + e.getMessage(), e);
40              return "localhost";
41          }
42      }
43  
44  }