From 6be0c4b69491e7929c65ba941b03130a6fbb45a3 Mon Sep 17 00:00:00 2001
From: ragnarok <ragnarok>
Date: Sun, 18 Sep 2005 05:28:51 +0000
Subject: [PATCH] * Moved PetName and PetNameDB to core. * Implement
 PetNameNamingService.

---
 .../java/src/net/i2p/syndie/BlogManager.java  |  2 +
 apps/syndie/java/src/net/i2p/syndie/User.java |  1 +
 .../src/net/i2p/syndie/sml/HTMLRenderer.java  |  1 +
 .../net/i2p/syndie/web/ArchiveViewerBean.java |  2 +
 .../java/src/net/i2p/syndie/web/PostBean.java |  1 +
 .../net/i2p/syndie/web/RemoteArchiveBean.java |  1 +
 apps/syndie/jsp/_bodyindex.jsp                |  2 +-
 apps/syndie/jsp/addresses.jsp                 |  2 +-
 apps/syndie/jsp/post.jsp                      |  2 +-
 apps/syndie/jsp/remote.jsp                    |  2 +-
 .../src/net/i2p/client/naming}/PetName.java   |  2 +-
 .../src/net/i2p/client/naming}/PetNameDB.java |  3 +-
 .../client/naming/PetNameNamingService.java   | 65 +++++++++++++++++++
 13 files changed, 80 insertions(+), 6 deletions(-)
 rename {apps/syndie/java/src/net/i2p/syndie => core/java/src/net/i2p/client/naming}/PetName.java (99%)
 rename {apps/syndie/java/src/net/i2p/syndie => core/java/src/net/i2p/client/naming}/PetNameDB.java (98%)
 create mode 100644 core/java/src/net/i2p/client/naming/PetNameNamingService.java

diff --git a/apps/syndie/java/src/net/i2p/syndie/BlogManager.java b/apps/syndie/java/src/net/i2p/syndie/BlogManager.java
index 8c3fe86347..010172c0f1 100644
--- a/apps/syndie/java/src/net/i2p/syndie/BlogManager.java
+++ b/apps/syndie/java/src/net/i2p/syndie/BlogManager.java
@@ -4,6 +4,8 @@ import java.io.*;
 import java.text.*;
 import java.util.*;
 import net.i2p.I2PAppContext;
+import net.i2p.client.naming.PetName;
+import net.i2p.client.naming.PetNameDB;
 import net.i2p.data.*;
 import net.i2p.syndie.data.*;
 import net.i2p.syndie.sml.*;
diff --git a/apps/syndie/java/src/net/i2p/syndie/User.java b/apps/syndie/java/src/net/i2p/syndie/User.java
index badb40c332..d11856c62c 100644
--- a/apps/syndie/java/src/net/i2p/syndie/User.java
+++ b/apps/syndie/java/src/net/i2p/syndie/User.java
@@ -4,6 +4,7 @@ import java.io.UnsupportedEncodingException;
 import java.io.IOException;
 import java.util.*;
 import net.i2p.I2PAppContext;
+import net.i2p.client.naming.PetNameDB;
 import net.i2p.data.*;
 
 /**
diff --git a/apps/syndie/java/src/net/i2p/syndie/sml/HTMLRenderer.java b/apps/syndie/java/src/net/i2p/syndie/sml/HTMLRenderer.java
index ff26a507d6..ada257d2bb 100644
--- a/apps/syndie/java/src/net/i2p/syndie/sml/HTMLRenderer.java
+++ b/apps/syndie/java/src/net/i2p/syndie/sml/HTMLRenderer.java
@@ -4,6 +4,7 @@ import java.io.*;
 import java.text.*;
 import java.util.*;
 import net.i2p.I2PAppContext;
+import net.i2p.client.naming.PetName;
 import net.i2p.data.*;
 import net.i2p.syndie.*;
 import net.i2p.syndie.data.*;
diff --git a/apps/syndie/java/src/net/i2p/syndie/web/ArchiveViewerBean.java b/apps/syndie/java/src/net/i2p/syndie/web/ArchiveViewerBean.java
index 5df8d08068..f5ce3394cb 100644
--- a/apps/syndie/java/src/net/i2p/syndie/web/ArchiveViewerBean.java
+++ b/apps/syndie/java/src/net/i2p/syndie/web/ArchiveViewerBean.java
@@ -4,6 +4,8 @@ import java.io.*;
 import java.text.*;
 import java.util.*;
 import net.i2p.I2PAppContext;
+import net.i2p.client.naming.PetName;
+import net.i2p.client.naming.PetNameDB;
 import net.i2p.data.*;
 import net.i2p.syndie.*;
 import net.i2p.syndie.data.*;
diff --git a/apps/syndie/java/src/net/i2p/syndie/web/PostBean.java b/apps/syndie/java/src/net/i2p/syndie/web/PostBean.java
index fbc3d03587..a058a26f49 100644
--- a/apps/syndie/java/src/net/i2p/syndie/web/PostBean.java
+++ b/apps/syndie/java/src/net/i2p/syndie/web/PostBean.java
@@ -3,6 +3,7 @@ package net.i2p.syndie.web;
 import java.io.*;
 import java.util.*;
 import net.i2p.I2PAppContext;
+import net.i2p.client.naming.PetName;
 import net.i2p.syndie.*;
 import net.i2p.syndie.data.BlogURI;
 import net.i2p.syndie.sml.HTMLPreviewRenderer;
diff --git a/apps/syndie/java/src/net/i2p/syndie/web/RemoteArchiveBean.java b/apps/syndie/java/src/net/i2p/syndie/web/RemoteArchiveBean.java
index 427aa4d033..619b7ec29b 100644
--- a/apps/syndie/java/src/net/i2p/syndie/web/RemoteArchiveBean.java
+++ b/apps/syndie/java/src/net/i2p/syndie/web/RemoteArchiveBean.java
@@ -5,6 +5,7 @@ import java.text.*;
 import java.util.*;
 import java.util.zip.*;
 import net.i2p.I2PAppContext;
+import net.i2p.client.naming.PetNameDB;
 import net.i2p.data.*;
 import net.i2p.util.EepGet;
 import net.i2p.util.EepGetScheduler;
diff --git a/apps/syndie/jsp/_bodyindex.jsp b/apps/syndie/jsp/_bodyindex.jsp
index d9fc9c51a2..fe80cd8369 100644
--- a/apps/syndie/jsp/_bodyindex.jsp
+++ b/apps/syndie/jsp/_bodyindex.jsp
@@ -1,4 +1,4 @@
-<%@page contentType="text/html; charset=UTF-8" import="net.i2p.syndie.web.ArchiveViewerBean, net.i2p.syndie.*" %>
+<%@page contentType="text/html; charset=UTF-8" import="net.i2p.syndie.web.ArchiveViewerBean, net.i2p.syndie.*, net.i2p.client.naming.PetName" %>
 <% request.setCharacterEncoding("UTF-8"); %>
 <jsp:useBean scope="session" class="net.i2p.syndie.User" id="user" /><%
 if (user.getAuthenticated() && (null != request.getParameter("action")) ) {
diff --git a/apps/syndie/jsp/addresses.jsp b/apps/syndie/jsp/addresses.jsp
index cb51fd6b80..c152499064 100644
--- a/apps/syndie/jsp/addresses.jsp
+++ b/apps/syndie/jsp/addresses.jsp
@@ -1,4 +1,4 @@
-<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="net.i2p.data.Base64, net.i2p.syndie.web.*, net.i2p.syndie.sml.*, net.i2p.syndie.data.*, net.i2p.syndie.*, org.mortbay.servlet.MultiPartRequest, java.util.*, java.io.*" %><%
+<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="net.i2p.data.Base64, net.i2p.syndie.web.*, net.i2p.syndie.sml.*, net.i2p.syndie.data.*, net.i2p.syndie.*, net.i2p.client.naming.PetName, net.i2p.client.naming.PetNameDB, org.mortbay.servlet.MultiPartRequest, java.util.*, java.io.*" %><%
  request.setCharacterEncoding("UTF-8"); %><jsp:useBean scope="session" class="net.i2p.syndie.User" id="user" 
 /><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
 <html>
diff --git a/apps/syndie/jsp/post.jsp b/apps/syndie/jsp/post.jsp
index 0afc1cc315..a22be401fe 100644
--- a/apps/syndie/jsp/post.jsp
+++ b/apps/syndie/jsp/post.jsp
@@ -1,4 +1,4 @@
-<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="net.i2p.data.Base64, net.i2p.syndie.web.*, net.i2p.syndie.sml.*, net.i2p.syndie.data.*, net.i2p.syndie.*, org.mortbay.servlet.MultiPartRequest, java.util.*" %><% 
+<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="net.i2p.data.Base64, net.i2p.client.naming.PetName, net.i2p.client.naming.PetNameDB, net.i2p.syndie.web.*, net.i2p.syndie.sml.*, net.i2p.syndie.data.*, net.i2p.syndie.*, org.mortbay.servlet.MultiPartRequest, java.util.*" %><% 
 request.setCharacterEncoding("UTF-8"); 
 %><jsp:useBean scope="session" class="net.i2p.syndie.User" id="user" 
 /><jsp:useBean scope="session" class="net.i2p.syndie.web.PostBean" id="post" 
diff --git a/apps/syndie/jsp/remote.jsp b/apps/syndie/jsp/remote.jsp
index bc47b1a8f3..0bdf684ccc 100644
--- a/apps/syndie/jsp/remote.jsp
+++ b/apps/syndie/jsp/remote.jsp
@@ -1,4 +1,4 @@
-<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="net.i2p.syndie.web.*, net.i2p.syndie.*, net.i2p.syndie.sml.*, java.util.*" %><% 
+<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="net.i2p.client.naming.PetName, net.i2p.syndie.web.*, net.i2p.syndie.*, net.i2p.syndie.sml.*, java.util.*" %><% 
 request.setCharacterEncoding("UTF-8"); 
 %><jsp:useBean scope="session" class="net.i2p.syndie.web.RemoteArchiveBean" id="remote" 
 /><jsp:useBean scope="session" class="net.i2p.syndie.User" id="user" 
diff --git a/apps/syndie/java/src/net/i2p/syndie/PetName.java b/core/java/src/net/i2p/client/naming/PetName.java
similarity index 99%
rename from apps/syndie/java/src/net/i2p/syndie/PetName.java
rename to core/java/src/net/i2p/client/naming/PetName.java
index c63d149879..73ee4edf54 100644
--- a/apps/syndie/java/src/net/i2p/syndie/PetName.java
+++ b/core/java/src/net/i2p/client/naming/PetName.java
@@ -1,4 +1,4 @@
-package net.i2p.syndie;
+package net.i2p.client.naming;
 
 import java.util.*;
 import net.i2p.data.DataHelper;
diff --git a/apps/syndie/java/src/net/i2p/syndie/PetNameDB.java b/core/java/src/net/i2p/client/naming/PetNameDB.java
similarity index 98%
rename from apps/syndie/java/src/net/i2p/syndie/PetNameDB.java
rename to core/java/src/net/i2p/client/naming/PetNameDB.java
index 700464924c..730cc4e6b4 100644
--- a/apps/syndie/java/src/net/i2p/syndie/PetNameDB.java
+++ b/core/java/src/net/i2p/client/naming/PetNameDB.java
@@ -1,8 +1,9 @@
-package net.i2p.syndie;
+package net.i2p.client.naming;
 
 import java.io.*;
 import java.util.*;
 
+
 /**
  *
  */
diff --git a/core/java/src/net/i2p/client/naming/PetNameNamingService.java b/core/java/src/net/i2p/client/naming/PetNameNamingService.java
new file mode 100644
index 0000000000..7c197cb63a
--- /dev/null
+++ b/core/java/src/net/i2p/client/naming/PetNameNamingService.java
@@ -0,0 +1,65 @@
+package net.i2p.client.naming;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Properties;
+
+import net.i2p.I2PAppContext;
+import net.i2p.data.DataHelper;
+import net.i2p.data.Destination;
+
+public class PetNameNamingService extends NamingService {
+
+    private PetNameDB _petnameDb;
+    public final static String PROP_PETNAME_FILE = "i2p.petnamefile";
+    public final static String DEFAULT_PETNAME_FILE = "petnames.txt";
+    
+    public PetNameNamingService(I2PAppContext context) {
+        super(context);
+        _petnameDb = new PetNameDB();
+        String file = _context.getProperty(PROP_PETNAME_FILE, DEFAULT_PETNAME_FILE);
+
+        //If the petnamedb file doesn't exist, create it, using the 
+        //contents of hosts.txt.
+        File nameFile = new File(file);
+        if (!nameFile.exists()) {
+            Properties hosts = new Properties();
+            File hostsFile = new File("hosts.txt");
+            if (hostsFile.exists() && hostsFile.canRead()) {
+                try {
+                    DataHelper.loadProps(hosts, hostsFile);
+                } catch (IOException ioe) {
+                }
+            }
+            Iterator iter = hosts.keySet().iterator();
+            while (iter.hasNext()) {
+                String hostname = (String)iter.next();
+                PetName pn = new PetName(hostname, "i2p", "http", hosts.getProperty(hostname));
+                _petnameDb.set(hostname, pn);
+            }
+            try {
+                _petnameDb.store(file);
+            } catch (IOException ioe) {
+            }
+        }
+        
+        try {
+            _petnameDb.load(file);
+        } catch (IOException ioe) {
+        }
+    }
+
+    public Destination lookup(String hostname) {
+        PetName name = _petnameDb.get(hostname);
+        if (name != null && name.getNetwork().equals("i2p")) {
+            return lookupBase64(name.getLocation());
+        } else {
+            return lookupBase64(hostname);
+        }
+    }
+
+    public String reverseLookup(Destination dest) {
+        return _petnameDb.getNameByLocation(dest.toBase64());
+    }
+}
-- 
GitLab