1 64 65 package com.jcorporate.expresso.ext.dbobj; 66 67 import com.jcorporate.expresso.core.db.DBConnection; 68 import com.jcorporate.expresso.core.db.DBException; 69 import com.jcorporate.expresso.core.dbobj.DBObject; 70 import com.jcorporate.expresso.core.dbobj.RequestContext; 71 72 import java.lang.ref.Reference ; 73 import java.lang.ref.SoftReference ; 74 import java.net.InetAddress ; 75 import java.util.Vector ; 76 77 78 92 public class ReverseLookupDomains 93 extends ISOCountryCodes { 94 public static final String FLD_DOMAINCODE = "DomainCode"; 95 public static final String FLD_DESCRIPTION = "DomainDescrip"; 96 public static final String FLD_NOTES = "DomainNotes"; 97 98 103 public ReverseLookupDomains() 104 throws DBException { 105 super(); 106 } 107 108 114 public ReverseLookupDomains(DBConnection myConnection) 115 throws DBException { 116 super(myConnection); 117 } 118 119 126 public ReverseLookupDomains(int uid) 127 throws DBException { 128 super(uid); 129 } 130 131 138 public ReverseLookupDomains(RequestContext request) 139 throws DBException { 140 super(request); 141 } 142 143 149 public synchronized void populateDefaultValues() 150 throws DBException { 151 int i; 152 String countryCodes[][] = super.getCountryCodes(); 153 int len = countryCodes.length; 154 155 for (i = 0; i < len; i++) { 156 this.clear(); 157 this.setField(FLD_DOMAINCODE, 158 countryCodes[i][1].toLowerCase()); 159 160 if (!this.find()) { 161 setField(FLD_DESCRIPTION, countryCodes[i][0]); 162 this.add(); 163 } 164 } 165 166 String internetCodes[][] = this.getInternetCodes(); 167 len = internetCodes.length; 168 169 for (i = 0; i < len; i++) { 170 this.clear(); 171 this.setField(FLD_DOMAINCODE, internetCodes[i][1].toLowerCase()); 172 173 if (!this.find()) { 174 setField(FLD_DESCRIPTION, internetCodes[i][0]); 175 this.add(); 176 } 177 } 178 } 179 180 181 187 public int getExpectedDefaultPopulation() { 188 String internetCodes[][] = this.getInternetCodes(); 189 return internetCodes.length + super.getExpectedDefaultPopulation(); 190 } 191 192 196 public Vector getValues() 197 throws DBException { 198 return getValuesDefault(FLD_DOMAINCODE, FLD_DESCRIPTION); 199 } 200 201 202 207 public synchronized DBObject getThisDBObj() 208 throws DBException { 209 return new ReverseLookupDomains(); 210 } 211 212 213 218 protected void setupFields() 219 throws DBException { 220 setTargetTable("REVERSEDOMAINS"); 221 setDescription("Reverse Domain Lookup Descriptions"); 222 setCharset("ISO-8859-1"); 223 addField(FLD_DOMAINCODE, "varchar", 20, false, "Domain Code"); 224 addField(FLD_DESCRIPTION, "varchar", 128, false, "Domain Description"); 225 addField(FLD_NOTES, "varchar", 128, true, "Misc. Notes"); 226 setStringFilter(FLD_DOMAINCODE, "stripFilter"); 227 setStringFilter(FLD_DESCRIPTION, "rawFilter"); 228 addKey(FLD_DOMAINCODE); 229 setReadOnly(FLD_DOMAINCODE); 230 } 231 232 233 protected synchronized String [][] getInternetCodes() { 234 synchronized (internetCodesLock) { 235 if (internetCodesRef == null || internetCodesRef.get() == null) { 236 internetCodesRef = new SoftReference (new String [][]{ 237 {"Commercial Company", "com"}, {"Educational Institution", "edu"}, 238 {"Non-profit Organization", "org"}, {"Networking Company", "net"}, 239 {"Old Internet Backbone", "arpa"}, 240 {"Air Transportation Industry", "aero"}, 241 {"Commercial Business", "biz"}, {"Cooperative Association", "coop"}, 242 {"Information", "info"}, {"Museum", "museum"}, {"Individual", "name"}, 243 {"US Government", "gov"}, {"US Military", "mil"}, 244 {"International Treaty Organization", "int"}, 245 {"Commercial Company", "cc"}, {"United Kingdom", "uk"} 246 }); 247 248 } 249 250 return (String [][]) internetCodesRef.get(); 251 } 252 } 253 254 255 private static final Object internetCodesLock = new Object (); 256 private static Reference internetCodesRef = null; 257 258 259 266 public String getDomainId(String ipAddress) 267 throws DBException { 268 try { 269 InetAddress addr = InetAddress.getByName(ipAddress); 270 String hostName = addr.getHostName(); 271 String domain = hostName.substring(hostName.lastIndexOf(".") + 1); 272 ReverseLookupDomains rld = new ReverseLookupDomains(this.getRequestingUid()); 273 rld.setDataContext(this.getDataContext()); 274 rld.setField(FLD_DOMAINCODE, domain); 275 276 if (!rld.find()) { 277 throw new DBException("Unable to find domain entry for domain=" + domain + 278 " ipaddress= " + ipAddress + " hostname= " + hostName); 279 } else { 280 return rld.getField(FLD_DOMAINCODE); 281 } 282 } catch (java.net.UnknownHostException uhe) { 283 throw new DBException("Unable to lookup host: " + ipAddress, uhe); 284 } catch (SecurityException se) { 285 throw new DBException("Unable to lookup host: " + ipAddress + 286 ". You need to have 'socket.checkConnect' permissions enabled" + 287 " in your security manager", se); 288 } 289 } 290 } | Popular Tags |