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