Author: batosai
Date: 2008-09-03 21:02:32 +0000 (Wed, 03 Sep 2008)
New Revision: 22382
Modified:
trunk/apps/WoT/src/plugins/WoT/Identity.java
trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java
Log:
Refactored OwnIdentity to match Identity's changes.
Modified: trunk/apps/WoT/src/plugins/WoT/Identity.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/Identity.java 2008-09-03 20:51:54 UTC
(rev 22381)
+++ trunk/apps/WoT/src/plugins/WoT/Identity.java 2008-09-03 21:02:32 UTC
(rev 22382)
@@ -10,6 +10,7 @@
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
+import java.util.Map.Entry;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
@@ -151,6 +152,11 @@
updated();
}
+ public Iterator<Entry<String, String>> getProps() {
+ Iterator<Entry<String, String>> i = props.entrySet().iterator();
+ return i;
+ }
+
public void addContext(String context, ObjectContainer db) throws
InvalidParameterException {
String newContext = context.trim();
if(newContext.length() == 0) throw new
InvalidParameterException("Blank context");
@@ -166,10 +172,10 @@
updated();
}
- public Iterator<String> getContextsIterator() {
+ public Iterator<String> getContexts() {
return contexts.iterator();
}
-
+
private void updated() {
lastChange = new Date();
}
Modified: trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java 2008-09-03 20:51:54 UTC
(rev 22381)
+++ trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java 2008-09-03 21:02:32 UTC
(rev 22382)
@@ -12,7 +12,6 @@
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
-import java.util.Set;
import java.util.Map.Entry;
import javax.xml.parsers.DocumentBuilder;
@@ -46,49 +45,42 @@
*/
public class OwnIdentity extends Identity {
- private String insertURI;
+ private FreenetURI insertURI;
private Date lastInsert;
-
-
- public OwnIdentity(String insertURI, String requestURI, Date
lastInsert, Date lastChange, String nickName, String publishTrustList) throws
InvalidParameterException {
-
- super(requestURI, lastChange, nickName, publishTrustList);
+ public OwnIdentity (FreenetURI insertURI, FreenetURI requestURI, String
nickName, String publishTrustList, String context) throws
InvalidParameterException {
+ super(requestURI, nickName, publishTrustList, context);
setInsertURI(insertURI);
- setLastInsert(lastInsert);
}
+
+ public OwnIdentity (String insertURI, String requestURI, String
nickName, String publishTrustList, String context) throws
InvalidParameterException, MalformedURLException {
+ this(new FreenetURI(insertURI), new FreenetURI(requestURI),
nickName, publishTrustList, context);
+ }
+
+
@SuppressWarnings("unchecked")
- public static OwnIdentity getByURI (ObjectContainer db, FreenetURI uri)
throws UnknownIdentityException, DuplicateIdentityException {
-
+ public static OwnIdentity getById (ObjectContainer db, byte[] id)
throws UnknownIdentityException, DuplicateIdentityException {
Query query = db.query();
query.constrain(OwnIdentity.class);
-
query.descend("requestURI").constrain(uri.toString().substring(uri.toString().indexOf("@")
+ 1, uri.toString().indexOf("/")));
+ query.descend("id").constrain(id);
ObjectSet<OwnIdentity> result = query.execute();
- if(result.size() == 0) throw new UnknownIdentityException("No
identity has this request URI ("+uri.toString()+")");
- if(result.size() > 1) throw new
DuplicateIdentityException("There are two Identities matching this URI : " +
uri.toString());
+ if(result.size() == 0) throw new
UnknownIdentityException(id.toString());
+ if(result.size() > 1) throw new
DuplicateIdentityException(id.toString());
return result.next();
}
+ public static OwnIdentity getByURI (ObjectContainer db, FreenetURI uri)
throws UnknownIdentityException, DuplicateIdentityException {
+ return getById(db, uri.getRoutingKey());
+ }
+
public static OwnIdentity getByURI (ObjectContainer db, String uri)
throws UnknownIdentityException, DuplicateIdentityException,
MalformedURLException {
return getByURI(db, new FreenetURI(uri));
}
- /**
- * Generates a XML file describing the identity
- *
- * @param db Connection to the database
- * @throws ParserConfigurationException
- * @throws TransformerConfigurationException
- * @throws TransformerException
- * @throws FileNotFoundException
- * @throws IOException
- * @throws InvalidParameterException
- * @throws DatabaseClosedException
- * @throws Db4oIOException
- */
+
public void exportToXML(ObjectContainer db, OutputStream os) throws
ParserConfigurationException, TransformerConfigurationException,
TransformerException, FileNotFoundException, IOException, Db4oIOException,
DatabaseClosedException, InvalidParameterException {
// Create the output file
@@ -106,19 +98,19 @@
// NickName
Element nickNameTag = xmlDoc.createElement("nickName");
- nickNameTag.setAttribute("value", nickName);
+ nickNameTag.setAttribute("value", getNickName());
identity.appendChild(nickNameTag);
// PublishTrustList
Element publishTrustListTag =
xmlDoc.createElement("publishTrustList");
- publishTrustListTag.setAttribute("value", publishTrustList ?
"true" : "false");
+ publishTrustListTag.setAttribute("value",
doesPublishTrustList() ? "true" : "false");
identity.appendChild(publishTrustListTag);
// Properties
- Set<Entry<String, String>> set = props.entrySet();
- Iterator<Entry<String, String>> i = set.iterator();
- while(i.hasNext()){
- Map.Entry<String,String> prop = i.next();
+
+ Iterator<Entry<String, String>> props = getProps();
+ while(props.hasNext()){
+ Map.Entry<String,String> prop = props.next();
Element propTag = xmlDoc.createElement("prop");
propTag.setAttribute("key", prop.getKey());
propTag.setAttribute("value", prop.getValue());
@@ -126,9 +118,9 @@
}
// Contexts
- Iterator<String> i2 = contexts.iterator();
- while(i2.hasNext()) {
- String context = (String)i2.next();
+ Iterator<String> contexts = getContexts();
+ while(contexts.hasNext()) {
+ String context = (String)contexts.next();
Element contextTag = xmlDoc.createElement("context");
contextTag.setAttribute("value", context);
identity.appendChild(contextTag);
@@ -150,63 +142,25 @@
serializer.transform(domSource, resultStream);
}
- /**
- *
- * @return Whether the identity has been updated since the last insert
- */
public boolean needsInsert() {
- return (lastChange.after(lastInsert) || (new Date().getTime() -
lastInsert.getTime()) > 1000*60*60*24*7);
+ return (getLastChange().after(getLastInsert()) || (new
Date().getTime() - getLastInsert().getTime()) > 1000*60*60*24*7);
}
- /**
- * @return insertURI
- */
public FreenetURI getInsertURI() {
-
- FreenetURI key;
- try {
- key = new FreenetURI("USK@" + insertURI +
"/WoT/"+edition);
- } catch (MalformedURLException e) {
- // TODO Remove this as soon as the creation is checked
- e.printStackTrace();
- return null;
- // Can't happen, the key is tested on creation
- }
- return key;
+ return insertURI;
}
- /**
- * @param insertURI InsertURI of the Identity
- * @throws InvalidParameterException
- */
- public void setInsertURI(String insertURI) throws
InvalidParameterException {
-
- FreenetURI key;
- try {
- key = new FreenetURI(insertURI).setDocName("WoT");
- } catch (MalformedURLException e) {
- throw new InvalidParameterException("Invalid key :
"+e.getMessage());
- }
- setInsertURI(key);
- }
-
public void setInsertURI(FreenetURI key) throws
InvalidParameterException {
if(key.getKeyType().equals("SSK")) key = key.setKeyType("USK");
if(!key.getKeyType().equals("USK")) throw new
InvalidParameterException("Key type not supported");
- this.insertURI =
key.toString().substring(key.toString().indexOf("@") + 1,
key.toString().indexOf("/"));
+ this.insertURI = key;
}
- /**
- * @return lastInsert
- */
public Date getLastInsert() {
return lastInsert;
}
-
- /**
- * @param lastInsert Date of insertion in Freenet
- */
+
public void setLastInsert(Date lastInsert) {
this.lastInsert = lastInsert;
}