From da731d2bb81666b9c697d9099da632e7dfcdc0f7 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Tue, 9 Sep 2014 12:13:21 +0200 Subject: Replaced int sysKey Item identifier with UUID, which is now portable. ItemPath objects are now used to identify Items throughout the kernel, replacing ints and Integers. --- src/main/java/com/c2kernel/lookup/AgentPath.java | 71 ++++++----- src/main/java/com/c2kernel/lookup/DomainPath.java | 20 +-- src/main/java/com/c2kernel/lookup/ItemPath.java | 143 +++++++--------------- src/main/java/com/c2kernel/lookup/Lookup.java | 3 +- src/main/java/com/c2kernel/lookup/Path.java | 30 +++-- 5 files changed, 110 insertions(+), 157 deletions(-) (limited to 'src/main/java/com/c2kernel/lookup') diff --git a/src/main/java/com/c2kernel/lookup/AgentPath.java b/src/main/java/com/c2kernel/lookup/AgentPath.java index a0bb7fd..fb4ce0e 100644 --- a/src/main/java/com/c2kernel/lookup/AgentPath.java +++ b/src/main/java/com/c2kernel/lookup/AgentPath.java @@ -12,10 +12,12 @@ package com.c2kernel.lookup; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.UUID; import org.apache.xerces.impl.dv.util.Base64; import com.c2kernel.common.ObjectNotFoundException; +import com.c2kernel.common.SystemKey; import com.c2kernel.process.Gateway; @@ -31,29 +33,40 @@ public class AgentPath extends ItemPath private String mAgentName=null; private String mPassword=null; - public AgentPath(int syskey, String agentName) - throws InvalidAgentPathException,InvalidItemPathException - { - super(syskey); - if (agentName!=null && agentName.length()>0) - setAgentName(agentName); - else + public AgentPath(SystemKey syskey) throws InvalidAgentPathException { + super(syskey); + try { + findAgentName(); + } catch (ObjectNotFoundException e) { + throw new InvalidAgentPathException(); + } + } + public AgentPath(UUID uuid) throws InvalidAgentPathException { + super(uuid); + try { + findAgentName(); + } catch (ObjectNotFoundException e) { throw new InvalidAgentPathException(); + } } - - public AgentPath(int syskey) - throws InvalidItemPathException - { - super(syskey); + + public AgentPath(ItemPath itemPath) throws InvalidAgentPathException { + super(itemPath.mUUID); + try { + findAgentName(); + } catch (ObjectNotFoundException e) { + throw new InvalidAgentPathException(); + } } - - public AgentPath(ItemPath entity) { - super(); - try { - setSysKey(entity.getSysKey()); - } catch (InvalidItemPathException ex) { - //won't happen as the entity path was valid - } + + public AgentPath(ItemPath itemPath, String agentName) { + super(itemPath.mUUID); + mAgentName = agentName; + } + + public AgentPath(String agentName) { + super(); + mAgentName = agentName; } public void setAgentName(String agentID) @@ -64,14 +77,16 @@ public class AgentPath extends ItemPath public String getAgentName() { if (mAgentName==null) - { - try { - mAgentName = Gateway.getLookup().getAgentName(this); - } catch (ObjectNotFoundException e) { - mAgentName = ""; - } - } - return mAgentName; + try { + findAgentName(); + } catch (ObjectNotFoundException e) { + return null; + } + return mAgentName; + } + + private void findAgentName() throws ObjectNotFoundException { + mAgentName = Gateway.getLookup().getAgentName(this); } public RolePath[] getRoles() diff --git a/src/main/java/com/c2kernel/lookup/DomainPath.java b/src/main/java/com/c2kernel/lookup/DomainPath.java index 706719e..c8ae751 100644 --- a/src/main/java/com/c2kernel/lookup/DomainPath.java +++ b/src/main/java/com/c2kernel/lookup/DomainPath.java @@ -12,7 +12,6 @@ package com.c2kernel.lookup; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.process.Gateway; -import com.c2kernel.utils.Logger; /** @@ -47,7 +46,7 @@ public class DomainPath extends Path super(path, Path.UNKNOWN); } - public DomainPath(String path, ItemPath entity) throws InvalidItemPathException + public DomainPath(String path, ItemPath entity) { super(path, Path.UNKNOWN); setEntity(entity); @@ -86,13 +85,13 @@ public class DomainPath extends Path } @Override - public ItemPath getEntity() throws ObjectNotFoundException { + public ItemPath getItemPath() throws ObjectNotFoundException { if (mType == UNKNOWN) { // must decide checkType(); } if (target == null) - throw new ObjectNotFoundException("Path is a context", ""); + throw new ObjectNotFoundException("Path "+toString()+" does not resolve to an Item", ""); return target; } @@ -108,7 +107,6 @@ public class DomainPath extends Path try { setEntity(Gateway.getLookup().resolvePath(this)); } catch (InvalidItemPathException ex) { - Logger.error(ex); mType = CONTEXT; } catch (ObjectNotFoundException ex) { mType = CONTEXT; @@ -123,17 +121,5 @@ public class DomainPath extends Path public String getName() { return mPath[mPath.length-1]; } - - @Override - public int getSysKey() { - if (mType == UNKNOWN) { // must decide - checkType(); - } - - if (mType == ENTITY) { - return target.getSysKey(); - } - else return INVALID; - } } diff --git a/src/main/java/com/c2kernel/lookup/ItemPath.java b/src/main/java/com/c2kernel/lookup/ItemPath.java index 17e5659..2219c8a 100644 --- a/src/main/java/com/c2kernel/lookup/ItemPath.java +++ b/src/main/java/com/c2kernel/lookup/ItemPath.java @@ -10,9 +10,11 @@ package com.c2kernel.lookup; -import java.util.ArrayList; +import java.nio.ByteBuffer; +import java.util.UUID; import com.c2kernel.common.ObjectNotFoundException; +import com.c2kernel.common.SystemKey; /** @@ -23,38 +25,29 @@ import com.c2kernel.common.ObjectNotFoundException; **/ public class ItemPath extends Path { - // no of components in a syskey - public static final int elementNo = 3; - // no of digits in a syskey component - public static final int elementLen = 3; - // maximum possible int syskey - public static final int maxSysKey = (int)Math.pow(10, elementNo*elementLen)-1; - protected static String mTypeRoot; - /* - * From syskey int - * Note no EntityPath constructors allow setting of CONTEXT or ENTITY: - * The object decides that for itself from the number of components - */ - - public ItemPath(int syskey) throws InvalidItemPathException { - super(); - setSysKey(syskey); + + public ItemPath() { + this(UUID.randomUUID()); } - /* - */ - public ItemPath() - { - super(); + public ItemPath(UUID uuid) { + setSysKey(uuid); + mPath = new String[1]; + mPath[0] = mUUID.toString(); } + public ItemPath(SystemKey syskey) { + setSysKey(syskey); + mPath = new String[1]; + mPath[0] = mUUID.toString(); + } /* */ public ItemPath(String[] path) throws InvalidItemPathException { - super(path, Path.CONTEXT); // dummy - it will get replaced in checkSysPath() - checkSysPath(); + super(path, Path.CONTEXT); + getSysKeyFromPath(); } /* @@ -62,16 +55,19 @@ public class ItemPath extends Path public ItemPath(String path) throws InvalidItemPathException { super(path, Path.CONTEXT); - checkSysPath(); + getSysKeyFromPath(); } - /* - */ - public ItemPath(ItemPath parent, String child) throws InvalidItemPathException { - super(parent, child); - checkSysPath(); + private void getSysKeyFromPath() throws InvalidItemPathException { + if (mPath.length > 0) { + try { + setSysKey(UUID.fromString(mPath[0])); + } catch (IllegalArgumentException ex) { + throw new InvalidItemPathException(mPath[0]+" is not a valid UUID"); + } + } } - + // EntityPaths root in /entity @Override public String getRoot() { @@ -79,81 +75,28 @@ public class ItemPath extends Path } @Override - public ItemPath getEntity() throws ObjectNotFoundException { + public ItemPath getItemPath() throws ObjectNotFoundException { return this; } public byte[] getOID() { - if (mSysKey == Path.INVALID) return null; - return String.valueOf(mSysKey).getBytes(); + if (mType == Path.CONTEXT) return null; + ByteBuffer bb = ByteBuffer.wrap(new byte[16]); + bb.putLong(mUUID.getMostSignificantBits()); + bb.putLong(mUUID.getLeastSignificantBits()); + return bb.array(); } - - /*************************************************************************/ - - /** Returns int form of syskey (if possible) - */ - @Override - public int getSysKey() { - if (mSysKey == Path.INVALID && mType == Path.ENTITY) - try { - if (mPath.length != elementNo) - throw new InvalidItemPathException("Incorrect number of components for a system key"); - mSysKey = 0; - for (String keypart : mPath) { - if (keypart.length()!=elementLen+1) - throw new InvalidItemPathException("Component '"+keypart+"' is not the correct size for a system key"); - for(int j=1; j= '0') && (c <='9')) - mSysKey = mSysKey*10 + c - '0'; - else - throw new InvalidItemPathException("Component '"+keypart+"' contained a non-numeric char (excluding first char 'd')."); - - } - } - } catch (InvalidItemPathException ex) { - mSysKey = Path.INVALID; - } - return mSysKey; - } - - /** Sets path from int syskey - */ - public void setSysKey(int sysKey) throws InvalidItemPathException - { - if (sysKey < 0 || sysKey > maxSysKey) - throw new InvalidItemPathException("System key "+sysKey+" out of range"); - String stringPath = Integer.toString(sysKey); - ArrayList newKey = new ArrayList(); - for (int i=0; i elementNo) - throw new InvalidItemPathException("EntityPath cannot have more than "+elementNo+" components: "+toString()); - if (mPath.length == elementNo) - mType = Path.ENTITY; - else - mType = Path.CONTEXT; + + protected void setSysKey(SystemKey sysKey) { + mSysKey = sysKey; + mUUID = new UUID(sysKey.msb, sysKey.lsb); + mType = Path.ENTITY; } } diff --git a/src/main/java/com/c2kernel/lookup/Lookup.java b/src/main/java/com/c2kernel/lookup/Lookup.java index 5384e7c..87dec1d 100644 --- a/src/main/java/com/c2kernel/lookup/Lookup.java +++ b/src/main/java/com/c2kernel/lookup/Lookup.java @@ -1,6 +1,7 @@ package com.c2kernel.lookup; import java.util.Iterator; +import java.util.UUID; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.process.auth.Authenticator; @@ -35,7 +36,7 @@ public interface Lookup { * @throws InvalidItemPathException When the system key is invalid/out-of-range * @throws ObjectNotFoundException When the Item does not exist in the directory. */ - public ItemPath getItemPath(int sysKey) throws InvalidItemPathException, ObjectNotFoundException; + public ItemPath getItemPath(UUID sysKey) throws InvalidItemPathException, ObjectNotFoundException; /** * Find the ItemPath for which a DomainPath is an alias. diff --git a/src/main/java/com/c2kernel/lookup/Path.java b/src/main/java/com/c2kernel/lookup/Path.java index d6be37f..f6993f7 100644 --- a/src/main/java/com/c2kernel/lookup/Path.java +++ b/src/main/java/com/c2kernel/lookup/Path.java @@ -13,8 +13,10 @@ package com.c2kernel.lookup; import java.io.Serializable; import java.util.ArrayList; import java.util.StringTokenizer; +import java.util.UUID; import com.c2kernel.common.ObjectNotFoundException; +import com.c2kernel.common.SystemKey; import com.c2kernel.process.Gateway; @@ -31,17 +33,16 @@ public abstract class Path implements Serializable public static final short CONTEXT = 1; public static final short ENTITY = 2; - // invalid int key - public static final int INVALID = -1; - protected String[] mPath = new String[0]; // slash delimited path protected String mStringPath = null; // entity or context protected short mType = CONTEXT; - // int syskey (only valid for entity SystemPaths) - protected int mSysKey = INVALID; + + // item UUID (only valid for ItemPaths and DomainPaths that are aliases for Items) + protected UUID mUUID; + protected SystemKey mSysKey; // ior is stored in here when it is resolved protected org.omg.CORBA.Object mIOR = null; @@ -102,7 +103,8 @@ public abstract class Path implements Serializable { mStringPath = null; mPath = path.clone(); - mSysKey = INVALID; + mUUID = null; + mSysKey = null; } /* string path e.g. /system/d000/d000/d001 @@ -122,7 +124,8 @@ public abstract class Path implements Serializable mPath = (newPath.toArray(mPath)); mStringPath = null; - mSysKey = INVALID; + mUUID = null; + mSysKey = null; } // lookup sets the IOR @@ -138,7 +141,8 @@ public abstract class Path implements Serializable { mStringPath = null; mPath = (path.getPath().clone()); - mSysKey = INVALID; + mUUID = null; + mSysKey = null; } /*************************************************************************/ @@ -196,11 +200,15 @@ public abstract class Path implements Serializable return mType; } - public int getSysKey() { + public SystemKey getSystemKey() { return mSysKey; } + + public UUID getUUID() { + return mUUID; + } - public abstract ItemPath getEntity() throws ObjectNotFoundException; + public abstract ItemPath getItemPath() throws ObjectNotFoundException; @Override public boolean equals( Object path ) @@ -217,7 +225,7 @@ public abstract class Path implements Serializable StringBuffer comp = new StringBuffer("Components: { "); for (String element : mPath) comp.append("'").append(element).append("' "); - return "Path - dump(): "+comp.toString()+"}\n string="+toString()+"\n int="+getSysKey()+"\n type="+mType; + return "Path - dump(): "+comp.toString()+"}\n string="+toString()+"\n uuid="+getUUID()+"\n type="+mType; } } -- cgit v1.2.3