Author: xor
Date: 2008-11-10 02:23:00 +0000 (Mon, 10 Nov 2008)
New Revision: 23459

Modified:
   trunk/plugins/WoT/Identity.java
   trunk/plugins/WoT/IdentityFetcher.java
   trunk/plugins/WoT/IdentityParser.java
   trunk/plugins/WoT/OwnIdentity.java
   trunk/plugins/WoT/Trust.java
   trunk/plugins/WoT/WoT.java
Log:


Modified: trunk/plugins/WoT/Identity.java
===================================================================
--- trunk/plugins/WoT/Identity.java     2008-11-09 23:12:34 UTC (rev 23458)
+++ trunk/plugins/WoT/Identity.java     2008-11-10 02:23:00 UTC (rev 23459)
@@ -43,7 +43,7 @@
  * @param contexts An ArrayList containing contexts (eg. client apps) an 
Identity is used for
  */
 public class Identity {
-       
+       /* FIXME: Where do these values come from? They should be tweaked 
before release. */
        /** Capacity is the maximum amount of points an identity can give to an 
other by trusting it. */
        public final static int capacities[] = {
                        100,// Rank 0 : Own identities
@@ -58,7 +58,7 @@
          * A unique identifier used to query this Identity from the database.
          * In fact, it is simply a String representing its routing key.
          */
-       private String id;
+       private final String id;
         /** The requestURI used to fetch this identity from Freenet */
        private FreenetURI requestURI;
        /** Date of this identity's last modification (last time we fetched it 
from Freenet) */
@@ -80,18 +80,18 @@
         * @param publishTrustList Whether this identity publishes its 
trustList or not
         * @throws InvalidParameterException if a supplied parameter is invalid
         */
-       public Identity (FreenetURI requestURI, String nickName, String 
publishTrustList) throws InvalidParameterException {
-               
-               setRequestURI(requestURI);
-               setNickName(nickName);
+       public Identity (FreenetURI newRequestURI, String newNickName, boolean 
publishTrustList) throws InvalidParameterException {
+               setRequestURI(newRequestURI);
+               id = getIdFromURI(getRequestURI());
+               setNickName(newNickName);
                setPublishTrustList(publishTrustList);
                props = new HashMap<String, String>();
                contexts = new ArrayList<String>();
-               id = getIdFromURI(getRequestURI());

                Logger.debug(this, "New identity : " + getNickName());
-       }
+       }       

+
        /**
         * Creates an Identity
         * 
@@ -101,11 +101,10 @@
         * @throws InvalidParameterException if a supplied parameter is invalid
         * @throws MalformedURLException if the supplied requestURI isn't a 
valid FreenetURI
         */
-       public Identity (String requestURI, String nickName, String 
publishTrustList) throws InvalidParameterException, MalformedURLException {
+       public Identity (String requestURI, String nickName, boolean 
publishTrustList) throws InvalidParameterException, MalformedURLException {
                this(new FreenetURI(requestURI), nickName, publishTrustList);
        }

-
        /**
         * Loads an identity from the database, querying on its id
         * 
@@ -116,15 +115,15 @@
         * @throws UnknownIdentityException if there is no identity with this 
id in the database
         */
        @SuppressWarnings("unchecked")
-       public static Identity getById (ObjectContainer db, String id) throws 
DuplicateIdentityException, UnknownIdentityException {
+       public static Identity getById (ObjectContainer db, String id) throws 
UnknownIdentityException {

                Query query = db.query();
                query.constrain(Identity.class);
                query.descend("id").constrain(id);
                ObjectSet<Identity> result = query.execute();

+               assert(result.size() <= 1);
                if(result.size() == 0) throw new UnknownIdentityException(id);
-               if(result.size() > 1) throw new DuplicateIdentityException(id);
                return result.next();
        }

@@ -138,7 +137,7 @@
         * @throws DuplicateIdentityException if there are more than one 
identity with this id in the database
         * @throws MalformedURLException if the requestURI isn't a valid 
FreenetURI
         */
-       public static Identity getByURI (ObjectContainer db, String uri) throws 
UnknownIdentityException, DuplicateIdentityException, MalformedURLException {
+       public static Identity getByURI (ObjectContainer db, String uri) throws 
UnknownIdentityException, MalformedURLException {
                return getByURI(db, new FreenetURI(uri));
        }

@@ -151,7 +150,7 @@
         * @throws UnknownIdentityException if there is no identity with this 
id in the database
         * @throws DuplicateIdentityException if there are more than one 
identity with this id in the database
         */
-       public static Identity getByURI (ObjectContainer db, FreenetURI uri) 
throws UnknownIdentityException, DuplicateIdentityException {
+       public static Identity getByURI (ObjectContainer db, FreenetURI uri) 
throws UnknownIdentityException {
                return getById(db, getIdFromURI(uri));
        }

@@ -263,6 +262,7 @@
                else return result.next();
        }

+       /* FIXME: move this where it belongs */
        public HTMLNode getReceivedTrustForm (ObjectContainer db, 
PluginRespirator pr, String SELF_URI, Identity truster) throws 
DuplicateTrustException {

                String trustValue = "";
@@ -369,7 +369,7 @@
         * @throws InvalidParameterException if a given parameter isn't valid, 
{@see Trust} for details on accepted values.
         * @throws DuplicateScoreException if there already exist more than one 
{@link Score} objects for the trustee (should never happen)
         */
-       public void setTrust(ObjectContainer db, Identity trustee, int value, 
String comment) throws DuplicateTrustException, InvalidParameterException, 
DuplicateScoreException {
+       public void setTrust(ObjectContainer db, Identity trustee, byte value, 
String comment) throws DuplicateTrustException, InvalidParameterException, 
DuplicateScoreException {
                // Check if we are updating an existing trust value
                Trust trust;
                try {
@@ -378,17 +378,20 @@
                        if(!trust.getComment().equals(comment)) {
                                trust.setComment(comment);
                                db.store(trust);
+                               db.commit(); /* TODO: this commit was not here 
until I added it, is there a reason for that? */
                        }

                        if(trust.getValue() != value) {
                                trust.setValue(value);
                                db.store(trust);
+                               db.commit(); /* TODO: this commit was not here 
until I added it, is there a reason for that? */
                                Logger.debug(this, "Updated trust value ("+ 
trust +"), now updating Score.");
                                trustee.updateScore(db);
                        }
                } catch (NotTrustedException e) {
                        trust = new Trust(this, trustee, value, comment);
                        db.store(trust);
+                       db.commit(); /* TODO: this commit was not here until I 
added it, is there a reason for that? */
                        Logger.debug(this, "New trust value ("+ trust +"), now 
updating Score.");
                        trustee.updateScore(db);
                } 
@@ -401,9 +404,9 @@
         * @throws DuplicateScoreException if there already exist more than one 
{@link Score} objects for the trustee (should never happen)
         * @throws DuplicateTrustException if there already exist more than one 
{@link Trust} objects between these identities (should never happen)
         */
-       public void updateScore (ObjectContainer db) throws 
DuplicateScoreException, DuplicateTrustException {
+       public synchronized void updateScore (ObjectContainer db) throws 
DuplicateScoreException, DuplicateTrustException {
                ObjectSet<OwnIdentity> treeOwners = 
OwnIdentity.getAllOwnIdentities(db);
-               if(treeOwners.size() == 0) Logger.error(this, "Can't update "+ 
getNickName()+"'s score : there is no own identity yet");
+               if(treeOwners.size() == 0) Logger.debug(this, "Can't update "+ 
getNickName()+"'s score : there is no own identity yet");
                while(treeOwners.hasNext())
                        updateScore (db, treeOwners.next());
        }
@@ -491,7 +494,7 @@
                                value += trust.getValue() * 
trust.getTruster().getScore(treeOwner, db).getCapacity() / 100;
                        } catch (NotInTrustTreeException e) {}
                }
-               return value;
+               return value; /* FIXME: wouldn't it make sense to divide by 100 
here?? */
        }

        /**
@@ -526,10 +529,13 @@
         * @param requestURI The FreenetURI used to fetch this identity 
         * @throws InvalidParameterException if the given FreenetURI is neither 
a SSK nor a USK
         */
-       public void setRequestURI(FreenetURI requestURI) throws 
InvalidParameterException {
-               if(requestURI.getKeyType().equals("SSK")) requestURI = 
requestURI.setKeyType("USK");
-               if(!requestURI.getKeyType().equals("USK")) throw new 
InvalidParameterException("Key type not supported");
-               this.requestURI = 
requestURI.setKeyType("USK").setDocName("WoT");
+       protected synchronized void setRequestURI(FreenetURI newRequestURI) 
throws InvalidParameterException {
+               if(requestURI != null && 
newRequestURI.getRoutingKey().equals(requestURI.getRoutingKey()))
+                       throw new InvalidParameterException("Cannot change the 
request URI of an existing identity");
+               
+               if(newRequestURI.getKeyType().equals("SSK")) newRequestURI = 
newRequestURI.setKeyType("USK");
+               if(!newRequestURI.getKeyType().equals("USK")) throw new 
InvalidParameterException("Key type not supported");
+               requestURI = newRequestURI.setKeyType("USK").setDocName("WoT");
                updated();
        }

@@ -540,7 +546,7 @@
         * @param edition A long representing the last fetched version of this 
identity.
         * @throws InvalidParameterException
         */
-       public void setEdition(long edition) throws InvalidParameterException {
+       public synchronized void setEdition(long edition) throws 
InvalidParameterException {
                setRequestURI(getRequestURI().setSuggestedEdition(edition));
        }

@@ -550,11 +556,15 @@
         * @param nickName A String containing this Identity's NickName
         * @throws InvalidParameterException if the nickName's length is bigger 
than 50, or if it empty
         */
-       public void setNickName(String nickName) throws 
InvalidParameterException {
-               String nick = nickName.trim();
+       public synchronized void setNickName(String newNickname) throws 
InvalidParameterException {
+               String nick = newNickname.trim();
                if(nick.length() == 0) throw new 
InvalidParameterException("Blank nickName");
-               if(nick.length() > 50) throw new 
InvalidParameterException("NickName is too long (50 chars max)");
-               this.nickName = nick;
+               if(nick.length() > 50) throw new 
InvalidParameterException("Nickname is too long (50 chars max)");
+               
+               if(nickName != null && !nickName.equals("") && 
!nickName.equals(newNickname))
+                       throw new InvalidParameterException("Changing the 
nickname of an identity is not allowed.");
+               
+               nickName = nick;
                updated();
        }

@@ -563,20 +573,10 @@
         * 
         * @param publishTrustList 
         */
-       public void setPublishTrustList(boolean publishTrustList) {
+       public synchronized void setPublishTrustList(boolean publishTrustList) {
                this.publishTrustList = publishTrustList;
                updated();
        }
-
-       /**
-        * Sets if this Identity publishes its trustList or not.
-        * The given string is converted to a boolean.
-        * 
-        * @param publishTrustList 
-        */
-       public void setPublishTrustList(String publishTrustList) {
-               setPublishTrustList(publishTrustList.equals("true"));
-       }

        /**
         * Sets a custom property on this Identity. Custom properties keys have 
to be unique.
@@ -587,7 +587,7 @@
         * @param db A reference to the database 
         * @throws InvalidParameterException if the key or the value is empty
         */
-       public void setProp(String key, String value, ObjectContainer db) 
throws InvalidParameterException {
+       public synchronized void setProp(String key, String value, 
ObjectContainer db) throws InvalidParameterException {
                if(key.trim().length() == 0 || value.trim().length() == 0) 
throw new InvalidParameterException("Blank key or value in this property");
                props.put(key.trim(), value.trim());
                db.store(props);
@@ -601,7 +601,7 @@
         * @param db A reference to the database 
         * @throws InvalidParameterException if this Identity doesn't have the 
given property
         */
-       public void removeProp(String key, ObjectContainer db) throws 
InvalidParameterException {
+       public synchronized void removeProp(String key, ObjectContainer db) 
throws InvalidParameterException {
                if(!props.containsKey(key)) throw new 
InvalidParameterException("Property '"+key+"' isn't set on this identity");
                props.remove(key.trim());
                db.store(props);
@@ -613,7 +613,7 @@
         * 
         * @return An Iterator referencing all this Identity's custom properties
         */
-       public Iterator<Entry<String, String>> getProps() {
+       public synchronized Iterator<Entry<String, String>> getProps() {
                Iterator<Entry<String, String>> i = props.entrySet().iterator();
                return i;
        }
@@ -632,7 +632,7 @@
                String newContext = context.trim();
                if(newContext.length() == 0) throw new 
InvalidParameterException("Blank context");
                if(!contexts.contains(newContext)) contexts.add(newContext);
-               db.store(contexts);
+               db.store(contexts); /* FIXME: shouldn't we store the whole 
identity with cascading enabled???? */
                updated();
        }

@@ -645,10 +645,10 @@
         * @param db A reference to the database
         * @throws InvalidParameterException if the client tries to remove the 
last context of this Identity (an identity with no context is useless)
         */
-       public void removeContext(String context, ObjectContainer db) throws 
InvalidParameterException {
+       public synchronized void removeContext(String context, ObjectContainer 
db) throws InvalidParameterException {
                if(contexts.size() == 1) throw new 
InvalidParameterException("Only one context left");
                contexts.remove(context);
-               db.store(contexts);
+               db.store(contexts); /* FIXME: shouldn't we store the whole 
identity with cascading enabled???? */
                updated();
        }

@@ -657,14 +657,14 @@
         * 
         * @return An Iterator referencing all this identity's contexts
         */
-       public Iterator<String> getContexts() {
+       public synchronized Iterator<String> getContexts() {
                return contexts.iterator();
        }

        /**
         * Tell that this Identity has been updated.
         */
-       public void updated() {
+       public synchronized void updated() {
                lastChange = new Date();
        }

@@ -679,14 +679,14 @@
        /**
         * @return The requestURI ({@link FreenetURI}) to fetch this Identity 
         */
-       public FreenetURI getRequestURI() {
+       public synchronized FreenetURI getRequestURI() {
                return requestURI.setMetaString(new String[] {"identity.xml"} );
        }

        /**
         * @return The date of this Identity's last modification
         */
-       public Date getLastChange() {
+       public synchronized Date getLastChange() {
                return lastChange;
        }

@@ -694,7 +694,7 @@
         * @return A string representing the date of this Identity's last 
         * modification. Or "Fetching..." if it has not been fetched yet.
         */
-       public String getReadableLastChange() {
+       public synchronized String getReadableLastChange() {
                if (lastChange.equals(new Date(0))) return "Fetching...";
                else return lastChange.toString();
        }
@@ -702,7 +702,7 @@
        /**
         * @return this Identity's nickName
         */
-       public String getNickName() {
+       public synchronized String getNickName() {
                return nickName;
        }

@@ -716,7 +716,7 @@
        /**
         * @return A String listing all this Identity's contexts
         */
-       public String getContextsAsString() {
+       public synchronized String getContextsAsString() {
                return contexts.toString();
        }

@@ -727,7 +727,7 @@
         * @return The value of the requested custom property
         * @throws InvalidParameterException if this Identity doesn't have the 
required property
         */
-       public String getProp(String key) throws InvalidParameterException {
+       public synchronized String getProp(String key) throws 
InvalidParameterException {
                if(!props.containsKey(key)) throw new 
InvalidParameterException("Property '"+key+"' isn't set on this identity");
                return props.get(key);
        }
@@ -735,7 +735,7 @@
        /**
         * @return A String listing all this Identities custom properties
         */
-       public String getPropsAsString() {
+       public synchronized String getPropsAsString() {
                return props.toString();
        }

@@ -743,7 +743,7 @@
         * @param context The context we want to know if this Identity has it 
or not
         * @return Whether this Identity has that context or not
         */
-       public boolean hasContext(String context) {
+       public synchronized boolean hasContext(String context) {
                return contexts.contains(context.trim());
        }
 }

Modified: trunk/plugins/WoT/IdentityFetcher.java
===================================================================
--- trunk/plugins/WoT/IdentityFetcher.java      2008-11-09 23:12:34 UTC (rev 
23458)
+++ trunk/plugins/WoT/IdentityFetcher.java      2008-11-10 02:23:00 UTC (rev 
23459)
@@ -8,8 +8,12 @@

 import java.util.ArrayList;
 import java.util.Date;
+import java.util.HashSet;
+import java.util.Hashtable;
 import java.util.Iterator;

+import plugins.WoT.exceptions.UnknownIdentityException;
+
 import com.db4o.ObjectContainer;

 import freenet.client.FetchContext;
@@ -36,9 +40,12 @@
        private ObjectContainer db;
         /** A reference to the HighLevelSimpleClient used to talk with the 
node */
        private HighLevelSimpleClient client;
-        /** A list of all current requests */
-       private ArrayList<ClientGetter> requests;
+
+       /** All current requests */
+       private HashSet<Identity> identities;
+       private HashSet<ClientGetter> requests;

+       
        /**
         * Creates a new IdentityFetcher.
         * 
@@ -49,7 +56,8 @@

                this.db = db;
                this.client = client;
-               requests = new ArrayList<ClientGetter>();
+               identities = new HashSet<Identity>(128); /* TODO: profile & 
tweak */
+               requests = new HashSet<ClientGetter>(128); /* TODO: profile & 
tweak */
        }

        /**
@@ -70,17 +78,18 @@
         * @param identity the Identity to fetch
         * @param nextEdition whether we want to check current edition or the 
next one
         */
-       public void fetch(Identity identity, boolean nextEdition) {
+       public synchronized void fetch(Identity identity, boolean nextEdition) {

-               /* FIXME Keep a track on identities being fetched and don't
-                * try to download one twice. When done, remove the ugly hack 
-                * on seed identity creation and start the fetch at that 
moment. 
-                */ 
+               if(identities.contains(identity))
+                       return;
+
                try {
                        if(nextEdition && !identity.getLastChange().equals(new 
Date(0)))
                                
fetch(identity.getRequestURI().setSuggestedEdition(identity.getRequestURI().getSuggestedEdition()
 + 1));
                        else
                                fetch(identity.getRequestURI());
+                       
+                       identities.add(identity);
                } catch (FetchException e) {
                        Logger.error(this, "Request restart failed: "+e, e);
                }
@@ -92,14 +101,14 @@
         * @param uri the {@link FreenetURI} we want to fetch
         * @throws FetchException if the node encounters a problem
         */
-       public void fetch(FreenetURI uri) throws FetchException {
+       public synchronized void fetch(FreenetURI uri) throws FetchException {
                FetchContext fetchContext = client.getFetchContext();
                fetchContext.maxSplitfileBlockRetries = -1; // retry forever
                fetchContext.maxNonSplitfileRetries = -1; // retry forever
                ClientGetter g = client.fetch(uri, -1, this, this, 
fetchContext);
                g.setPriorityClass(RequestStarter.UPDATE_PRIORITY_CLASS); /* 
FIXME: decide which one to use */
                requests.add(g);
-               Logger.debug(this, "Start fetching identity "+uri.toString());
+               Logger.debug(this, "Start fetching uri "+uri.toString());
        }

        /**
@@ -107,9 +116,10 @@
         */
        public void stop() {
                Iterator<ClientGetter> i = requests.iterator();
-               Logger.debug(this, "Trying to stop "+requests.size()+" 
requests");
-               while (i.hasNext()) i.next().cancel();
-               Logger.debug(this, "Stopped all current requests");
+               int counter = 0;
+               Logger.debug(this, "Trying to stop all requests"); 
+               while (i.hasNext()) { i.next().cancel(); ++counter; }
+               Logger.debug(this, "Stopped " + counter + " current requests");
        }

        /**
@@ -130,22 +140,14 @@
                }
                // Errors we can't/want deal with
                Logger.error(this, "Fetch failed for "+ state.getURI(), e);
-               requests.remove(state); 
+               requests.remove(state);
+               try {
+                       Identity id = Identity.getByURI(db, state.getURI());
+                       identities.remove(id);
+               } catch(UnknownIdentityException ex) {}
+               
        }

-       // Only called by inserts
-       public void onFailure(InsertException e, BaseClientPutter state) {}
-
-       // Only called by inserts
-       public void onFetchable(BaseClientPutter state) {}
-
-       // Only called by inserts
-       public void onGeneratedURI(FreenetURI uri, BaseClientPutter state) {}
-
-       /** Called when freenet.async thinks that the request should be 
serialized to
-        * disk, if it is a persistent request. */
-       public void onMajorProgress() {}
-
        /**
         * Called when a file is successfully fetched. We then create an
         * {@link IdentityParser} and give it the file content. 
@@ -156,8 +158,7 @@

                try {
                        Logger.debug(this, "Sucessfully fetched identity "+ 
state.getURI().toString());
-                       new IdentityParser(db, client, 
this).parse(result.asBucket().getInputStream(), state.getURI());
-                       db.commit();            
+                       new IdentityParser(db, client, 
this).parse(result.asBucket().getInputStream(), state.getURI()); 
                        
state.restart(state.getURI().setSuggestedEdition(state.getURI().getSuggestedEdition()
 + 1));
                } catch (Exception e) {
                        Logger.error(this, "Parsing failed for "+ 
state.getURI(), e);
@@ -166,4 +167,17 @@

        // Only called by inserts
        public void onSuccess(BaseClientPutter state) {}
+       
+       // Only called by inserts
+       public void onFailure(InsertException e, BaseClientPutter state) {}
+
+       // Only called by inserts
+       public void onFetchable(BaseClientPutter state) {}
+
+       // Only called by inserts
+       public void onGeneratedURI(FreenetURI uri, BaseClientPutter state) {}
+
+       /** Called when freenet.async thinks that the request should be 
serialized to
+        * disk, if it is a persistent request. */
+       public void onMajorProgress() {}
 }

Modified: trunk/plugins/WoT/IdentityParser.java
===================================================================
--- trunk/plugins/WoT/IdentityParser.java       2008-11-09 23:12:34 UTC (rev 
23458)
+++ trunk/plugins/WoT/IdentityParser.java       2008-11-10 02:23:00 UTC (rev 
23459)
@@ -77,6 +77,7 @@

                saxParser.parse(is, new IdentityHandler() );
                db.store(identity);
+               db.commit();

                Logger.debug(this, "Successfuly parsed identity '" + 
identity.getNickName() + "'");
        }
@@ -110,7 +111,7 @@
                                        
identity.setNickName(attrs.getValue("value"));
                                }
                                if (elt_name.equals("publishTrustList")) {
-                                       
identity.setPublishTrustList(attrs.getValue("value"));
+                                       
identity.setPublishTrustList(attrs.getValue("value").equals("true"));
                                }
                                else if (elt_name.equals("prop")) {
                                        identity.setProp(attrs.getValue("key"), 
attrs.getValue("value"), db);
@@ -121,7 +122,7 @@
                                else if (elt_name.equals("trust")) {

                                        Identity trustee;
-                                       int value = 
Integer.parseInt(attrs.getValue("value"));
+                                       byte value = 
Byte.parseByte(attrs.getValue("value"));
                                        String comment = 
attrs.getValue("comment");

                                        try{
@@ -133,8 +134,9 @@
                                                // Create trustee only if the 
truster has a positive score.
                                                // This is to avoid Identity 
spam when announcements will be here.
                                                if(identity.getBestScore(db) > 
0) {
-                                                       trustee = new 
Identity(attrs.getValue("uri"), "Not found yet...", "false");
+                                                       trustee = new 
Identity(new FreenetURI(attrs.getValue("uri")), "Not found yet...", false);
                                                        db.store(trustee);
+                                                       db.commit(); /* TODO: 
this commit() was not here until I added it, is there a reason for that? */
                                                        identity.setTrust(db, 
trustee, value, comment);
                                                        fetcher.fetch(trustee); 
                                                }

Modified: trunk/plugins/WoT/OwnIdentity.java
===================================================================
--- trunk/plugins/WoT/OwnIdentity.java  2008-11-09 23:12:34 UTC (rev 23458)
+++ trunk/plugins/WoT/OwnIdentity.java  2008-11-10 02:23:00 UTC (rev 23459)
@@ -63,12 +63,12 @@
         * @param publishTrustList Whether this OwnIdentity publishes its 
trustList or not 
         * @throws InvalidParameterException If a given parameter is invalid
         */
-       public OwnIdentity (FreenetURI insertURI, FreenetURI requestURI, String 
nickName, String publishTrustList) throws InvalidParameterException {   
+       public OwnIdentity (FreenetURI insertURI, FreenetURI requestURI, String 
nickName, boolean publishTrustList) throws InvalidParameterException {  
                super(requestURI, nickName, publishTrustList);
                setInsertURI(insertURI);
                setLastInsert(new Date(0));
        }
-
+       
        /**
         * Creates a new OwnIdentity with the given parameters.
         * insertURI and requestURI are converted from String to {@link 
FreenetURI}
@@ -80,7 +80,7 @@
         * @throws InvalidParameterException If a given parameter is invalid
         * @throws MalformedURLException If either requestURI or insertURI is 
not a valid FreenetURI
         */
-       public OwnIdentity (String insertURI, String requestURI, String 
nickName, String publishTrustList) throws InvalidParameterException, 
MalformedURLException {
+       public OwnIdentity (String insertURI, String requestURI, String 
nickName, boolean publishTrustList) throws InvalidParameterException, 
MalformedURLException {
                this(new FreenetURI(insertURI), new FreenetURI(requestURI), 
nickName, publishTrustList);
        }

@@ -111,14 +111,14 @@
         * @throws DuplicateIdentityException If there is more than one 
identity with that id (should never happen)
         */
        @SuppressWarnings("unchecked")
-       public static OwnIdentity getById (ObjectContainer db, String id) 
throws UnknownIdentityException, DuplicateIdentityException {
+       public static OwnIdentity getById (ObjectContainer db, String id) 
throws UnknownIdentityException {
                Query query = db.query();
                query.constrain(OwnIdentity.class);
                query.descend("id").constrain(id);
                ObjectSet<OwnIdentity> result = query.execute();

+               assert(result.size() <= 1);
                if(result.size() == 0) throw new 
UnknownIdentityException(id.toString());
-               if(result.size() > 1) throw new 
DuplicateIdentityException(id.toString());
                return result.next();
        }

@@ -133,7 +133,7 @@
         * @throws DuplicateIdentityException if the OwnIdentity is present 
more that once in the database (should never happen)
         * @throws MalformedURLException if the supplied requestURI is not a 
valid FreenetURI
         */
-       public static OwnIdentity getByURI (ObjectContainer db, String uri) 
throws UnknownIdentityException, DuplicateIdentityException, 
MalformedURLException {
+       public static OwnIdentity getByURI (ObjectContainer db, String uri) 
throws UnknownIdentityException, MalformedURLException {
                return getByURI(db, new FreenetURI(uri));
        }

@@ -147,7 +147,7 @@
         * @throws UnknownIdentityException if the OwnIdentity isn't in the 
database
         * @throws DuplicateIdentityException if the OwnIdentity is present 
more that once in the database (should never happen)
         */
-       public static OwnIdentity getByURI (ObjectContainer db, FreenetURI uri) 
throws UnknownIdentityException, DuplicateIdentityException {
+       public static OwnIdentity getByURI (ObjectContainer db, FreenetURI uri) 
throws UnknownIdentityException {
                return getById(db, getIdFromURI(uri));
        }


Modified: trunk/plugins/WoT/Trust.java
===================================================================
--- trunk/plugins/WoT/Trust.java        2008-11-09 23:12:34 UTC (rev 23458)
+++ trunk/plugins/WoT/Trust.java        2008-11-10 02:23:00 UTC (rev 23459)
@@ -27,7 +27,7 @@
         */
        private final Identity truster;
        private final Identity trustee;
-       private int value;
+       private byte value;
        private String comment;

        /**
@@ -39,7 +39,7 @@
         * @param comment A comment to explain the numeric trust value
         * @throws InvalidParameterException if the trust value is not between 
-100 and +100
         */
-       public Trust(Identity truster, Identity trustee, int value, String 
comment) throws InvalidParameterException {
+       public Trust(Identity truster, Identity trustee, byte value, String 
comment) throws InvalidParameterException {
                this.truster = truster;
                this.trustee = trustee;
                setValue(value);
@@ -93,7 +93,7 @@
        /**
         * @return value Numeric value of this trust relationship
         */
-       public int getValue() {
+       public synchronized byte getValue() {
                return value;
        }

@@ -101,24 +101,29 @@
         * @param value Numeric value of this trust relationship [-100;+100] 
         * @throws InvalidParameterException if value isn't in the range
         */
-       public void setValue(int value) throws InvalidParameterException {
-               if(value < -100 || value > 100) 
+       public synchronized void setValue(byte newValue) throws 
InvalidParameterException {
+               if(newValue < -100 || newValue > 100) 
                        throw new InvalidParameterException("Invalid trust 
value ("+value+")");

-               this.value = value;
+               value = newValue;
        }

        /**
         * @return comment The comment associated to this Trust relationship
         */
-       public String getComment() {
+       public synchronized String getComment() {
                return comment;
        }

        /**
         * @param comment Comment on this trust relationship
         */
-       public void setComment(String comment) {
-               this.comment = comment;
+       public synchronized void setComment(String newComment) throws 
InvalidParameterException {
+               assert(newComment != null);
+               
+               if(newComment != null && newComment.length() > 256)
+                       throw new InvalidParameterException("Comment is too 
long (maximum is 256 characters).");
+               
+               comment = newComment != null ? newComment : "";
        }
 }

Modified: trunk/plugins/WoT/WoT.java
===================================================================
--- trunk/plugins/WoT/WoT.java  2008-11-09 23:12:34 UTC (rev 23458)
+++ trunk/plugins/WoT/WoT.java  2008-11-10 02:23:00 UTC (rev 23459)
@@ -96,6 +96,15 @@

                // Should disappear soon.
                web = new WebInterface(pr, db, config, client, SELF_URI);
+
+               // Create a default OwnIdentity if none exists. Should speed up 
plugin usability for newbies
+               if(OwnIdentity.getNbOwnIdentities(db) == 0) {
+                       try {
+                               createIdentity("Anonymous", true, "freetalk");
+                       } catch (Exception e) {
+                               Logger.error(this, "Error creating default 
identity : ", e);
+                       }
+               }

                // Start the inserter thread
                inserter = new IdentityInserter(db, client, 
pr.getNode().clientCore.tempBucketFactory);
@@ -222,7 +231,7 @@
                        Identity old = Identity.getByURI(db, requestURI);

                        // We already have fetched this identity as a 
stranger's one. We need to update the database.
-                       id = new OwnIdentity(insertURI, requestURI, 
old.getNickName(), old.doesPublishTrustList() ? "true" : "false");
+                       id = new OwnIdentity(new FreenetURI(insertURI), new 
FreenetURI(requestURI), old.getNickName(), old.doesPublishTrustList());

                        Iterator<String> i1 = old.getContexts();
                        while (i1.hasNext()) id.addContext(i1.next(), db);
@@ -269,7 +278,7 @@
                        Logger.debug(this, "Successfully restored an already 
known identity from Freenet (" + id.getNickName() + ")");

                } catch (UnknownIdentityException e) {
-                       id = new OwnIdentity(insertURI, requestURI, "Restore in 
progress...", "false");
+                       id = new OwnIdentity(new FreenetURI(insertURI), new 
FreenetURI(requestURI), "Restore in progress...", false);

                        // Store the new identity
                        db.store(id);
@@ -287,8 +296,8 @@

                setTrust(       request.getPartAsString("truster", 1024),
                                        request.getPartAsString("trustee", 
1024),
-                                       request.getPartAsString("value", 1024),
-                                       request.getPartAsString("comment", 
1024));
+                                       request.getPartAsString("value", 4),
+                                       request.getPartAsString("comment", 
256));
        }

        private void setTrust(String truster, String trustee, String value, 
String comment) throws InvalidParameterException, UnknownIdentityException, 
NumberFormatException, TransformerConfigurationException, 
FileNotFoundException, ParserConfigurationException, TransformerException, 
IOException, InsertException, Db4oIOException, DatabaseClosedException, 
DuplicateScoreException, DuplicateIdentityException, NotTrustedException, 
DuplicateTrustException  {
@@ -296,11 +305,10 @@
                OwnIdentity trusterId = OwnIdentity.getByURI(db, truster);
                Identity trusteeId = Identity.getByURI(db, trustee);

-               setTrust((OwnIdentity)trusterId, trusteeId, 
Integer.parseInt(value), comment);
-}      
+               setTrust((OwnIdentity)trusterId, trusteeId, 
Byte.parseByte(value), comment);
+       }

-       private void setTrust(OwnIdentity truster, Identity trustee, int value, 
String comment) throws TransformerConfigurationException, 
FileNotFoundException, ParserConfigurationException, TransformerException, 
IOException, InsertException, Db4oIOException, DatabaseClosedException, 
InvalidParameterException, DuplicateScoreException, NotTrustedException, 
DuplicateTrustException {
-
+       private void setTrust(OwnIdentity truster, Identity trustee, byte 
value, String comment) throws TransformerConfigurationException, 
FileNotFoundException, ParserConfigurationException, TransformerException, 
IOException, InsertException, Db4oIOException, DatabaseClosedException, 
InvalidParameterException, DuplicateScoreException, NotTrustedException, 
DuplicateTrustException {
                truster.setTrust(db, trustee, value, comment);
                truster.updated();
                db.store(truster);
@@ -319,7 +327,7 @@
                        throw new InvalidParameterException("We already have 
this identity");
                }
                catch (UnknownIdentityException e) {
-                       identity = new Identity(requestURI, "Not found yet...", 
"false");
+                       identity = new Identity(new FreenetURI(requestURI), 
"Not found yet...", false);
                        db.store(identity);
                        db.commit();
                        Logger.debug(this, "Trying to fetch manually added 
identity (" + identity.getRequestURI() + ")");
@@ -333,24 +341,25 @@
                return createIdentity(  
request.getPartAsString("insertURI",1024),
                                                                
request.getPartAsString("requestURI",1024),
                                                                
request.getPartAsString("nickName", 1024),
-                                                               
request.getPartAsString("publishTrustList", 1024),
-                                                               "testing");     
+                                                               
request.getPartAsString("publishTrustList", 5).equals(true),
+                                                               "freetalk");    
        }

-       private OwnIdentity createIdentity(String nickName, String 
publishTrustList, String context) throws TransformerConfigurationException, 
FileNotFoundException, InvalidParameterException, ParserConfigurationException, 
TransformerException, IOException, InsertException, Db4oIOException, 
DatabaseClosedException, DuplicateScoreException, NotTrustedException, 
DuplicateTrustException {
+       private OwnIdentity createIdentity(String nickName, boolean 
publishTrustList, String context) throws TransformerConfigurationException, 
FileNotFoundException, InvalidParameterException, ParserConfigurationException, 
TransformerException, IOException, InsertException, Db4oIOException, 
DatabaseClosedException, DuplicateScoreException, NotTrustedException, 
DuplicateTrustException {

                FreenetURI[] keypair = client.generateKeyPair("WoT");
                return createIdentity(keypair[0].toString(), 
keypair[1].toString(), nickName, publishTrustList, context);
        }

-       private OwnIdentity createIdentity(String insertURI, String requestURI, 
String nickName, String publishTrustList, String context) throws 
InvalidParameterException, TransformerConfigurationException, 
FileNotFoundException, ParserConfigurationException, TransformerException, 
IOException, InsertException, Db4oIOException, DatabaseClosedException, 
DuplicateScoreException, NotTrustedException, DuplicateTrustException {
+       private OwnIdentity createIdentity(String insertURI, String requestURI, 
String nickName, boolean publishTrustList, String context) throws 
InvalidParameterException, TransformerConfigurationException, 
FileNotFoundException, ParserConfigurationException, TransformerException, 
IOException, InsertException, Db4oIOException, DatabaseClosedException, 
DuplicateScoreException, NotTrustedException, DuplicateTrustException {

-               OwnIdentity identity = new OwnIdentity(insertURI, requestURI, 
nickName, publishTrustList);
+               OwnIdentity identity = new OwnIdentity(new 
FreenetURI(insertURI), new FreenetURI(requestURI), nickName, publishTrustList);
+               identity.addContext(context, db);
                db.store(identity);
                identity.initTrustTree(db);             
-               
+
                // This identity trusts the seed identity
-               identity.setTrust(db, seed, 100, "I trust the WoT plugin");
+               identity.setTrust(db, seed, (byte)100, "I trust the WoT 
plugin");

                Logger.debug(this, "Successfully created a new OwnIdentity (" + 
identity.getNickName() + ")");

@@ -458,13 +467,13 @@
                if(params.get("NickName")==null || 
params.get("PublishTrustList")==null || params.get("Context")==null) throw new 
InvalidParameterException("Missing mandatory parameter");

                if(params.get("RequestURI")==null || 
params.get("InsertURI")==null) {
-                       identity = createIdentity(params.get("NickName"), 
params.get("PublishTrustList"), params.get("Context"));
+                       identity = createIdentity(params.get("NickName"), 
params.get("PublishTrustList").equals("true"), params.get("Context"));
                }
                else {
                        identity = createIdentity(      params.get("InsertURI"),
                                                                                
params.get("RequestURI"),
                                                                                
params.get("NickName"), 
-                                                                               
params.get("PublishTrustList"),
+                                                                               
params.get("PublishTrustList").equals("true"),
                                                                                
params.get("Context"));
                }
                sfs.putAppend("Message", "IdentityCreated");
@@ -778,7 +787,7 @@
                        } catch (UnknownIdentityException e) { // Create it.
                                try {
                                        // Create the seed identity
-                                       seed = new Identity(seedURI, "Fetching 
seed identity...", "true");
+                                       seed = new Identity(new 
FreenetURI(seedURI), "Fetching seed identity...", true);
                                        // Step down to previous edition as the 
Fetcher is gonna try to fetch next edition
                                        
seed.setEdition(seed.getRequestURI().getSuggestedEdition() - 1);
                                } catch (Exception e1) { // Should never happen


Reply via email to