org/objectweb/proactive/core/util/HostsInfos.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.util;
00032 
00033 import java.net.InetAddress;
00034 import java.net.UnknownHostException;
00035 import java.util.Hashtable;
00036 
00037 import org.apache.log4j.Logger;
00038 import org.objectweb.proactive.core.config.ProActiveConfiguration;
00039 import org.objectweb.proactive.core.util.log.Loggers;
00040 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00041 
00042 
00049 //the Unique instance of HostInfos
00050 public class HostsInfos {
00051         private static HostsInfos hostsInfos = new HostsInfos();
00052     static Logger logger = ProActiveLogger.getLogger(Loggers.UTIL);
00053 
00054     private static Hashtable<String, Hashtable<String, String>> hostsTable;
00055 
00056     private HostsInfos() {
00057         ProActiveConfiguration.load();
00058         hostsTable = new Hashtable<String, Hashtable<String, String>>();
00059         hostsTable.put("all", new Hashtable<String, String>());
00060         loadProperties();
00061     }
00062 
00063     public static Hashtable getHostsInfos() {
00064         return hostsTable;
00065     }
00066 
00067     public static String hostnameToIP(String hostname) {
00068         if (hostname == null || hostname.equals("all"))
00069                 return hostname;
00070         
00071         try {
00072                 return InetAddress.getByName(hostname).getHostAddress();
00073         } catch (UnknownHostException e) {
00074                         return hostname;
00075                 }
00076     }
00077     
00078     public static Hashtable getHostInfos(String _hostname) {
00079         String hostname = hostnameToIP(_hostname);
00080         return getHostInfos(hostname, true);
00081     }
00082 
00083     public static String getUserName(String _hostname) {
00084         String hostname = hostnameToIP(_hostname);
00085         Hashtable host_infos = getHostInfos(hostname, true);
00086         return (String) host_infos.get("username");
00087     }
00088 
00089     public static String getSecondaryName(String _hostname) {
00090         String hostname = hostnameToIP(_hostname);
00091         Hashtable host_infos = getHostInfos(hostname, true);
00092         String secondary = (String) host_infos.get("secondary");
00093         if (secondary != null) {
00094             return secondary;
00095         } else {
00096             return hostname;
00097         }
00098     }
00099 
00100     public static void setUserName(String _hostname, String username) {
00101         String hostname = hostnameToIP(_hostname);
00102         Hashtable<String, String> host_infos = getHostInfos(hostname, false);
00103         if (host_infos.get("username") == null) {
00104             host_infos.put("username", username);
00105             //          try {
00106             //                  System.out.println(hostname+" --> "+username+ " on host "+InetAddress.getLocalHost().getHostName());
00107             //          } catch (UnknownHostException e) {
00108             //                  // TODO Auto-generated catch block
00109             //                  e.printStackTrace();
00110             //          }
00111         }
00112     }
00113 
00114     public static void setSecondaryName(String _hostname, String secondary_name) {
00115         String hostname = hostnameToIP(_hostname);
00116         Hashtable<String, String> host_infos = getHostInfos(hostname, false);
00117         if (host_infos.get("secondary") == null) {
00118             host_infos.put("secondary", secondary_name);
00119         }
00120     }
00121 
00122     protected static Hashtable<String, String> getHostInfos(String _hostname, boolean common) {
00123         String hostname = hostnameToIP(_hostname);
00124         Hashtable<String, String> host_infos = findHostInfos(hostname);
00125         if (host_infos == null) {
00126             if (common) {
00127                 return hostsTable.get("all");
00128             } else {
00129                 host_infos = new Hashtable<String, String>();
00130                 hostsTable.put(hostname, host_infos);
00131                 return host_infos;
00132             }
00133         }
00134         return host_infos;
00135     }
00136 
00140     final static String REGEXP_KEYVAL="(([0-9]{1,3}\\.){3}[0-9]{1,3}:([0-9]{1,3}\\.){3}[0-9]{1,3})";
00141         
00142     private void loadProperties() {
00143         String userNames = System.getProperty("proactive.ssh.username");
00144 
00145         if (userNames == null) {
00146             userNames = System.getProperty("user.name");
00147         }
00148         parseUserNames(userNames);
00149         
00150         String secondaryNames = System.getProperty("proactive.secondaryNames");
00151         if (secondaryNames != null) {
00152                 
00153                 if (secondaryNames.matches(REGEXP_KEYVAL + "(," + REGEXP_KEYVAL + ")?")) {
00154                         for (String keyval : secondaryNames.split(",")) {
00155                                 String [] tmp = keyval.split(":");
00156                                 setSecondaryName(tmp[0], tmp[1]);
00157                         }       
00158                 } else {
00159                         logger.error("Invalid value for proactive.secondaryNames: " + secondaryNames);
00160                 }
00161         }
00162     }
00163 
00167     private void parseUserNames(String userNames) {
00168         String[] unames = userNames.split(";");
00169         for (int i = 0; i < unames.length; i++) {
00170             int index = unames[i].indexOf("@");
00171             if (index < 0) {
00172                 if (unames.length > 1) {
00173                     logger.error(
00174                         "ERROR: malformed usernames. Should be username1@host1;username2@host2");
00175                 } else {
00176                     setUserName("all", unames[0]);
00177                 }
00178             } else {
00179                 String username = unames[i].substring(0, index);
00180                 String hostname = unames[i].substring(index + 1,
00181                         unames[i].length());
00182                 setUserName(hostname, username);
00183             }
00184         }
00185     }
00186 
00187     private static Hashtable<String, String> findHostInfos(String _hostname) {
00188         String hostname = hostnameToIP(_hostname);
00189         Hashtable<String, String> host_infos = hostsTable.get(hostname);
00190         if (host_infos == null) {
00191             try {
00192                 //we have to identify the mapping between host and IP
00193                 //we try with the canonical hostname
00194                 String host = InetAddress.getByName(hostname)
00195                                          .getCanonicalHostName();
00196                 if (!hostsTable.containsKey(host)) {
00197                     //we try with the IP
00198                     host = InetAddress.getByName(hostname).getHostAddress();
00199                 }
00200                 return hostsTable.get(host);
00201             } catch (UnknownHostException e) {
00202                 //same as return null
00203                 return host_infos;
00204             }
00205         }
00206         return host_infos;
00207     }
00208 
00209     public static void main(String[] args) {
00210         System.out.println(HostsInfos.getUserName("138.96.90.12"));
00211     }
00212 }

Generated on Mon Jan 22 15:16:10 2007 for ProActive by  doxygen 1.5.1