00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
00106
00107
00108
00109
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
00193
00194 String host = InetAddress.getByName(hostname)
00195 .getCanonicalHostName();
00196 if (!hostsTable.containsKey(host)) {
00197
00198 host = InetAddress.getByName(hostname).getHostAddress();
00199 }
00200 return hostsTable.get(host);
00201 } catch (UnknownHostException e) {
00202
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 }