|
From: <ha...@us...> - 2008-04-07 07:54:33
|
Revision: 1957
http://cogkit.svn.sourceforge.net/cogkit/?rev=1957&view=rev
Author: hategan
Date: 2008-04-07 00:54:31 -0700 (Mon, 07 Apr 2008)
Log Message:
-----------
properly implement equals and hashCode
Modified Paths:
--------------
trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/common/task/SecurityContextImpl.java
trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/common/task/ServiceContactImpl.java
trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/common/task/ServiceImpl.java
trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/interfaces/SecurityContext.java
trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/interfaces/ServiceContact.java
Modified: trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/common/task/SecurityContextImpl.java
===================================================================
--- trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/common/task/SecurityContextImpl.java 2008-04-05 13:59:25 UTC (rev 1956)
+++ trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/common/task/SecurityContextImpl.java 2008-04-07 07:54:31 UTC (rev 1957)
@@ -38,7 +38,6 @@
}
public String getAlias() {
-
return this.alias;
}
@@ -54,4 +53,29 @@
public Object getAttribute(String name) {
return this.attributes.get(name);
}
+
+ public int hashCode() {
+ return (credentials == null ? 0 : credentials.hashCode()) + attributes.hashCode();
+ }
+
+ public boolean equals(Object o) {
+ if (o instanceof SecurityContext) {
+ SecurityContext sc = (SecurityContext) o;
+ if ((credentials == null && sc.getCredentials() == null) || credentials.equals(sc.getCredentials())) {
+ if (o instanceof SecurityContextImpl) {
+ SecurityContextImpl sci = (SecurityContextImpl) o;
+ return attributes.equals(sci.attributes);
+ }
+ else {
+ return true;
+ }
+ }
+ else {
+ return false;
+ }
+ }
+ else {
+ return false;
+ }
+ }
}
\ No newline at end of file
Modified: trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/common/task/ServiceContactImpl.java
===================================================================
--- trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/common/task/ServiceContactImpl.java 2008-04-05 13:59:25 UTC (rev 1956)
+++ trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/common/task/ServiceContactImpl.java 2008-04-07 07:54:31 UTC (rev 1957)
@@ -6,10 +6,6 @@
package org.globus.cog.abstraction.impl.common.task;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.StringTokenizer;
-
import org.apache.log4j.Logger;
import org.globus.cog.abstraction.interfaces.ServiceContact;
@@ -17,26 +13,17 @@
static Logger logger = Logger.getLogger(ServiceContactImpl.class.getName());
- private static final byte HOST = 1;
-
- private static final byte PORT = 2;
-
- private static final byte CONTACT = 3;
-
public static final ServiceContact LOCALHOST = new ServiceContactImpl(
"localhost");
- private String host = null;
+ private String host, path;
+ private int port;
- private int port = -1;
-
- private String contact = null;
-
public ServiceContactImpl() {
}
public ServiceContactImpl(String contact) {
- this.contact = contact;
+ parse(contact);
}
public ServiceContactImpl(String host, int port) {
@@ -46,10 +33,11 @@
public void setHost(String host) {
this.host = host;
+ port = -1;
}
public String getHost() {
- return get(HOST);
+ return host;
}
public void setPort(int port) {
@@ -57,117 +45,53 @@
}
public int getPort() {
- String p = get(PORT);
- if (p == null) {
- return -1;
- }
- return Integer.parseInt(p);
-
+ return port;
}
public void setContact(String contact) {
- this.contact = contact;
+ parse(contact);
}
public String getContact() {
- return get(CONTACT);
+ return host + (port == -1 ? "" : ":" + port) + (path == null ? "" : path);
}
- public boolean equals(ServiceContact serviceContact) {
- return this.getContact().equalsIgnoreCase(serviceContact.getContact());
- }
-
- public boolean equals(Object object) {
- if (!(object instanceof ServiceContact)) {
- return false;
+ public boolean equals(Object o) {
+ if (o instanceof ServiceContact) {
+ ServiceContact sc = (ServiceContact) o;
+ return getContact().equals(sc.getContact());
}
- return this.toString().equalsIgnoreCase(
- ((ServiceContact) object).toString());
+ return false;
}
public int hashCode() {
- return this.getContact().toLowerCase().hashCode();
+ return this.getContact().hashCode();
}
- private String get(byte element) {
- switch (element) {
- case CONTACT:
- // if the service contact url is already set
- if (this.contact != null) {
- return this.contact;
+ private void parse(String contact) {
+ int portsep = contact.indexOf(':');
+ int pathsep = contact.indexOf('/');
+ if (portsep != -1 && (pathsep == -1 || portsep < pathsep)) {
+ host = contact.substring(0, portsep);
+ if (pathsep == -1) {
+ port = Integer.parseInt(contact.substring(portsep + 1));
+ path = null;
}
- // if not try to generate one
- else if (this.host != null) {
- if (this.port != -1) {
- return this.host + ":" + Integer.toString(this.port);
- } else {
- return this.host;
- }
+ else {
+ port = Integer.parseInt(contact.substring(portsep + 1, pathsep));
+ path = contact.substring(pathsep);
}
- return null;
- case HOST:
- if (this.host == null && this.contact != null) {
- // try to cast the contact into a URI and then get the
- // host and port from it
- if (this.contact.indexOf("://") != -1) {
- try {
- URI uri = new URI(this.contact);
- logger.debug("Host from URI: " + uri.getHost());
- return uri.getHost();
- } catch (URISyntaxException e) {
- logger
- .debug("Cannot retreive host information from the URI");
- return null;
- }
- } else {
- String c = this.contact;
- StringTokenizer st = new StringTokenizer(c, ":");
- try {
- String h = st.nextToken();
- logger.debug("Host from contact: " + h);
- return h;
- } catch (Exception ex) {
- logger
- .debug("Cannot retreive port information from the contact");
- return null;
- }
- }
- }
- return this.host;
- case PORT:
- if (this.port == -1 && this.contact != null) {
- // try to cast the contact into a URI and then get the
- // port from it
- if (this.contact.indexOf("://") != -1) {
- try {
- URI uri = new URI(this.contact);
- logger.debug("Port from URI: " + uri.getPort());
- return Integer.toString(uri.getPort());
- } catch (URISyntaxException e) {
- logger
- .debug("Cannot retreive port information from the URI");
- return null;
- }
- } else {
- String c = this.contact;
- StringTokenizer st = new StringTokenizer(c, ":");
- try {
- st.nextToken();
- String p = st.nextToken();
- logger.debug("Port from contact: " + p);
- return p;
- } catch (Exception ex) {
- logger
- .debug("Cannot retreive port information from the contact");
- return null;
- }
- }
- }
- return Integer.toString(this.port);
- default:
- break;
}
- return null;
+ else if (pathsep != -1) {
+ host = contact.substring(0, pathsep);
+ port = -1;
+ path = contact.substring(pathsep);
+ }
+ else {
+ host = contact;
+ port = -1;
+ path = null;
+ }
}
public String toString() {
Modified: trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/common/task/ServiceImpl.java
===================================================================
--- trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/common/task/ServiceImpl.java 2008-04-05 13:59:25 UTC (rev 1956)
+++ trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/common/task/ServiceImpl.java 2008-04-07 07:54:31 UTC (rev 1957)
@@ -142,7 +142,7 @@
public Enumeration getAllAttributes() {
return new Vector(getAttributeNames()).elements();
}
-
+
public Collection getAttributeNames() {
if (attributes != null) {
return attributes.keySet();
@@ -155,4 +155,23 @@
public String toString() {
return this.serviceContact.toString() + "(" + this.provider + ")";
}
+
+ public int hashCode() {
+ return serviceContact.hashCode() + provider.hashCode()
+ + (securityContext == null ? 0 : securityContext.hashCode());
+ }
+
+ public boolean equals(Object o) {
+ if (o instanceof Service) {
+ Service s = (Service) o;
+ return serviceContact.equals(s.getServiceContact())
+ && provider.equals(s.getProvider())
+ && equals(securityContext, s.getSecurityContext());
+ }
+ return false;
+ }
+
+ private boolean equals(Object o1, Object o2) {
+ return o1 == null ? o2 == null : o1.equals(o2);
+ }
}
Modified: trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/interfaces/SecurityContext.java
===================================================================
--- trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/interfaces/SecurityContext.java 2008-04-05 13:59:25 UTC (rev 1956)
+++ trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/interfaces/SecurityContext.java 2008-04-07 07:54:31 UTC (rev 1957)
@@ -6,7 +6,6 @@
package org.globus.cog.abstraction.interfaces;
-import org.globus.cog.abstraction.impl.common.task.InvalidSecurityContextException;
import org.ietf.jgss.GSSCredential;
/**
@@ -24,7 +23,7 @@
/**
* Returns the credentials for this <code>SecurityContext</code>
*/
- public Object getCredentials() throws InvalidSecurityContextException;
+ public Object getCredentials();
public void setAlias(String alias);
Modified: trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/interfaces/ServiceContact.java
===================================================================
--- trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/interfaces/ServiceContact.java 2008-04-05 13:59:25 UTC (rev 1956)
+++ trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/interfaces/ServiceContact.java 2008-04-07 07:54:31 UTC (rev 1957)
@@ -42,10 +42,4 @@
* Returns the entire contact string of this <code>ServiceContact</code>
*/
public String getContact();
-
- /**
- * Checks if the given <code>ServiceContact</code> is equal to this
- * <code>ServiceContact</code>.
- */
- public boolean equals(ServiceContact serviceContact);
}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|