Source Code Cross Referenced for ExternalGeoLocationDecideRule.java in  » Web-Crawler » heritrix » org » archive » crawler » deciderules » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Crawler » heritrix » org.archive.crawler.deciderules 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* ExternalGeoLocationDecideRule
002:         * 
003:         * Created on May 25, 2005
004:         *
005:         * Copyright (C) 2005 Internet Archive.
006:         * 
007:         * This file is part of the Heritrix web crawler (crawler.archive.org).
008:         * 
009:         * Heritrix is free software; you can redistribute it and/or modify
010:         * it under the terms of the GNU Lesser Public License as published by
011:         * the Free Software Foundation; either version 2.1 of the License, or
012:         * any later version.
013:         * 
014:         * Heritrix is distributed in the hope that it will be useful, 
015:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
016:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017:         * GNU Lesser Public License for more details.
018:         * 
019:         * You should have received a copy of the GNU Lesser Public License
020:         * along with Heritrix; if not, write to the Free Software
021:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
022:         */
023:        package org.archive.crawler.deciderules;
024:
025:        import java.net.InetAddress;
026:        import java.net.UnknownHostException;
027:        import java.util.logging.Level;
028:        import java.util.logging.Logger;
029:
030:        import org.apache.commons.httpclient.URIException;
031:        import org.archive.crawler.datamodel.CandidateURI;
032:        import org.archive.crawler.datamodel.CrawlHost;
033:        import org.archive.crawler.settings.SimpleType;
034:        import org.xbill.DNS.Address;
035:
036:        /**
037:         * A rule that can be configured to take alternate implementations
038:         * of the ExternalGeoLocationInterface.
039:         * If no implementation specified, or none found, returns configured decision.
040:         * If host in URI has been resolved checks CrawlHost for the country code
041:         * determination.
042:         * If country code is not present, does country lookup, and saves the country
043:         * code to <code>CrawlHost</code> for future consultation.
044:         * If country code is present in <code>CrawlHost</code>, compares it against
045:         * the configured code.
046:         * Note that if a host's IP address changes during the crawl, we still consider
047:         * the associated hostname to be in the country of its original IP address.
048:         * 
049:         * @author Igor Ranitovic
050:         */
051:        public class ExternalGeoLocationDecideRule extends PredicatedDecideRule {
052:
053:            private static final long serialVersionUID = -32974116429860725L;
054:
055:            private static final Logger LOGGER = Logger
056:                    .getLogger(ExternalGeoLocationDecideRule.class.getName());
057:            static final String ATTR_IMPLEMENTATION = "implementation-class";
058:            static final String ATTR_COUNTRY_CODE = "country-code";
059:            static final String DEFAULT_COUNTRY_CODE = "--";
060:            private String countryCode;
061:            private ExternalGeoLookupInterface implementation = null;
062:
063:            /**
064:             * @param name Name of this rule.
065:             */
066:            public ExternalGeoLocationDecideRule(String name) {
067:                super (name);
068:                setDescription("ExternalGeoLocationDecideRule. Rule that "
069:                        + "instantiates implementations of the ExternalGeoLookupInterface. "
070:                        + "The implementation needs to be present on the classpath. "
071:                        + "On initialization, the implementation is instantiated ("
072:                        + "assumption is that there is public constructor that takes +"
073:                        + "country code).");
074:                addElementToDefinition(new SimpleType(ATTR_IMPLEMENTATION,
075:                        "Name of implementation of ExternalGeoLookupInterface class to "
076:                                + "instantiate.", ""));
077:                addElementToDefinition(new SimpleType(ATTR_COUNTRY_CODE,
078:                        "Country code name.", ""));
079:
080:            }
081:
082:            protected boolean evaluate(Object obj) {
083:                ExternalGeoLookupInterface impl = getConfiguredImplementation(obj);
084:                if (impl == null) {
085:                    return false;
086:                }
087:                CrawlHost crawlHost = null;
088:                String host;
089:                InetAddress address;
090:                try {
091:                    if (obj instanceof  CandidateURI) {
092:                        host = ((CandidateURI) obj).getUURI().getHost();
093:                        crawlHost = getSettingsHandler().getOrder()
094:                                .getController().getServerCache().getHostFor(
095:                                        host);
096:                        if (crawlHost.getCountryCode() != null) {
097:                            return (crawlHost.getCountryCode()
098:                                    .equals(countryCode)) ? true : false;
099:                        }
100:                        address = crawlHost.getIP();
101:                        if (address == null) {
102:                            address = Address.getByName(host);
103:                        }
104:                        crawlHost.setCountryCode((String) impl.lookup(address));
105:                        if (crawlHost.getCountryCode().equals(countryCode)) {
106:                            LOGGER.fine("Country Code Lookup: " + " " + host
107:                                    + crawlHost.getCountryCode());
108:                            return true;
109:                        }
110:                    }
111:                } catch (UnknownHostException e) {
112:                    LOGGER.log(Level.FINE, "Failed dns lookup " + obj, e);
113:                    if (crawlHost != null) {
114:                        crawlHost.setCountryCode(DEFAULT_COUNTRY_CODE);
115:                    }
116:                } catch (URIException e) {
117:                    LOGGER
118:                            .log(Level.FINE, "Failed to parse hostname " + obj,
119:                                    e);
120:                }
121:
122:                return false;
123:            }
124:
125:            /**
126:             * Get implementation, if one specified. If none specified, will keep trying
127:             * to find one. Will be messy if the provided class is not-instantiable
128:             * 
129:             * @param o A context object.
130:             * @return Instance of <code>ExternalGeoLookupInterface</code> or null.
131:             */
132:            protected synchronized ExternalGeoLookupInterface getConfiguredImplementation(
133:                    Object o) {
134:                if (this .implementation != null) {
135:                    return this .implementation;
136:                }
137:                ExternalGeoLookupInterface result = null;
138:                try {
139:                    String className = (String) getAttribute(o,
140:                            ATTR_IMPLEMENTATION);
141:                    countryCode = (String) getAttribute(o, ATTR_COUNTRY_CODE);
142:                    if (className != null && className.length() != 0) {
143:                        Object obj = Class.forName(className).getConstructor(
144:                                new Class[] { String.class }).newInstance(
145:                                new Object[] { countryCode });
146:                        if (!(obj instanceof  ExternalGeoLookupInterface)) {
147:                            LOGGER
148:                                    .severe("Implementation "
149:                                            + className
150:                                            + " does not implement ExternalGeoLookupInterface");
151:                        }
152:                        result = (ExternalGeoLookupInterface) obj;
153:                        this .implementation = result;
154:                    }
155:                } catch (Exception e) {
156:                    LOGGER.severe(e.getMessage());
157:                }
158:                return result;
159:            }
160:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.