From 3a66087e65f71effdeb4feef83db4fc6910f550c Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Tue, 9 Sep 2014 14:06:46 +0200 Subject: Support new ItemPath based Item id. NextKeyManager removed. --- .../com/c2kernel/lookup/ldap/LDAPClientReader.java | 5 +- .../c2kernel/lookup/ldap/LDAPClusterStorage.java | 80 ++++++---------- .../java/com/c2kernel/lookup/ldap/LDAPLookup.java | 72 +++++++------- .../c2kernel/lookup/ldap/LDAPNextKeyManager.java | 103 --------------------- src/main/resources/LDAPboot.txt | 1 - 5 files changed, 69 insertions(+), 192 deletions(-) delete mode 100644 src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPClientReader.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPClientReader.java index a02d4fd..1d41ed7 100644 --- a/src/main/java/com/c2kernel/lookup/ldap/LDAPClientReader.java +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPClientReader.java @@ -1,6 +1,7 @@ package com.c2kernel.lookup.ldap; import com.c2kernel.entity.C2KLocalObject; +import com.c2kernel.lookup.ItemPath; import com.c2kernel.persistency.ClusterStorageException; /** Allows clients to directly load properties and collections from the LDAP @@ -19,7 +20,7 @@ public class LDAPClientReader extends LDAPClusterStorage { * @see com.c2kernel.persistency.ClusterStorage#delete(Integer, String) */ @Override - public void delete(Integer sysKey, String path) + public void delete(ItemPath itemPath, String path) throws ClusterStorageException { throw new ClusterStorageException("Writing not supported in ClientReader"); } @@ -36,7 +37,7 @@ public class LDAPClientReader extends LDAPClusterStorage { * @see com.c2kernel.persistency.ClusterStorage#put(Integer, String, C2KLocalObject) */ - public void put(Integer sysKey, String path, C2KLocalObject obj) + public void put(ItemPath itemPath, String path, C2KLocalObject obj) throws ClusterStorageException { throw new ClusterStorageException("Writing not supported in ClientReader"); } diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPClusterStorage.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPClusterStorage.java index f309378..3de9aad 100644 --- a/src/main/java/com/c2kernel/lookup/ldap/LDAPClusterStorage.java +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPClusterStorage.java @@ -52,30 +52,23 @@ public class LDAPClusterStorage extends ClusterStorage { // retrieve object by path @Override - public C2KLocalObject get(Integer sysKey, String path) throws ClusterStorageException { - Logger.msg(6, "LDAPClusterStorage.get() - "+sysKey+"/"+path); + public C2KLocalObject get(ItemPath thisItem, String path) throws ClusterStorageException { + Logger.msg(6, "LDAPClusterStorage.get() - "+thisItem+"/"+path); StringTokenizer tok = new StringTokenizer(path, "/"); int pathLength = tok.countTokens(); if (pathLength != 2) throw new ClusterStorageException("Path length was invalid: "+path); String type = tok.nextToken(); - ItemPath thisEntity; - try { - thisEntity = new ItemPath(sysKey.intValue()); - } catch (InvalidItemPathException e) { - throw new ClusterStorageException("Invalid Syskey:"+sysKey); - } - String objName = tok.nextToken(); C2KLocalObject newObj; if (type.equals(PROPERTY)) { try { - Property newProperty = ldapStore.getProperty(thisEntity, objName); + Property newProperty = ldapStore.getProperty(thisItem, objName); newObj = newProperty; } catch (ObjectNotFoundException ex) { - throw new ClusterStorageException("Property "+objName+" not found in "+sysKey); + throw new ClusterStorageException("Property "+objName+" not found in "+thisItem); } } @@ -86,21 +79,14 @@ public class LDAPClusterStorage extends ClusterStorage { } // store object by path @Override - public void put(Integer sysKey, C2KLocalObject obj) throws ClusterStorageException { - Logger.msg(6, "LDAPClusterStorage.put() - "+sysKey+"/"+ClusterStorage.getPath(obj)); + public void put(ItemPath thisItem, C2KLocalObject obj) throws ClusterStorageException { + Logger.msg(6, "LDAPClusterStorage.put() - "+thisItem+"/"+ClusterStorage.getPath(obj)); String type = obj.getClusterType(); - ItemPath thisEntity; - try { - thisEntity = new ItemPath(sysKey.intValue()); - } catch (InvalidItemPathException e) { - throw new ClusterStorageException("Invalid Syskey:"+sysKey); - } - if (type.equals(PROPERTY)) { try { - ldapStore.setProperty(thisEntity, (Property)obj); + ldapStore.setProperty(thisItem, (Property)obj); } catch (Exception e1) { Logger.error(e1); throw new ClusterStorageException("LDAPClusterStorage - could not write property"); @@ -112,23 +98,16 @@ public class LDAPClusterStorage extends ClusterStorage { } // delete cluster @Override - public void delete(Integer sysKey, String path) throws ClusterStorageException { + public void delete(ItemPath thisItem, String path) throws ClusterStorageException { StringTokenizer tok = new StringTokenizer(path, "/"); int pathLength = tok.countTokens(); if (pathLength != 2) throw new ClusterStorageException("Path length was invalid: "+path); String type = tok.nextToken(); - ItemPath thisEntity; - try { - thisEntity = new ItemPath(sysKey.intValue()); - } catch (InvalidItemPathException e) { - throw new ClusterStorageException("Invalid Syskey:"+sysKey); - } - if (type.equals(PROPERTY)) { try { - ldapStore.deleteProperty(thisEntity, tok.nextToken()); + ldapStore.deleteProperty(thisItem, tok.nextToken()); } catch (Exception e1) { Logger.error(e1); throw new ClusterStorageException("LDAPClusterStorage - could not delete property"); @@ -143,34 +122,31 @@ public class LDAPClusterStorage extends ClusterStorage { // directory listing @Override - public String[] getClusterContents(Integer sysKey, String path) throws ClusterStorageException { - Logger.msg(6, "LDAPClusterStorage.getClusterContents() - "+sysKey+"/"+path); + public String[] getClusterContents(ItemPath thisItem, String path) throws ClusterStorageException { + Logger.msg(6, "LDAPClusterStorage.getClusterContents() - "+thisItem+"/"+path); StringTokenizer tok = new StringTokenizer(path, "/"); int pathLength = tok.countTokens(); if (pathLength > 1) return new String[0]; String type = getClusterType(path); - try - { - ItemPath thisEntity = new ItemPath(sysKey.intValue()); - if (type.equals(PROPERTY)) - return ldapStore.getPropertyNames(thisEntity); - else - if (type.equals("")) { // root query - String[] allClusters = new String[0]; - ArrayList clusterList = new ArrayList(); - if (ldapStore.hasProperties(thisEntity)) - clusterList.add(PROPERTY); - allClusters = clusterList.toArray(allClusters); - return allClusters; - } - else - throw new ClusterStorageException("Cluster type "+type+" not supported."); - } catch (InvalidItemPathException e) { - throw new ClusterStorageException("Invalid Syskey:"+sysKey); - } catch (ObjectNotFoundException e) { - throw new ClusterStorageException("Entity "+sysKey+" does not exist"); + + try { + if (type.equals(PROPERTY)) + return ldapStore.getPropertyNames(thisItem); + else + if (type.equals("")) { // root query + String[] allClusters = new String[0]; + ArrayList clusterList = new ArrayList(); + if (ldapStore.hasProperties(thisItem)) + clusterList.add(PROPERTY); + allClusters = clusterList.toArray(allClusters); + return allClusters; + } + else + throw new ClusterStorageException("Cluster type "+type+" not supported."); + } catch (ObjectNotFoundException e) { + throw new ClusterStorageException("Item "+thisItem+" does not exist"); } } } diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java index 372a91c..96fe82b 100644 --- a/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java @@ -9,6 +9,7 @@ import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Iterator; import java.util.StringTokenizer; +import java.util.UUID; import org.omg.CORBA.Object; @@ -166,21 +167,25 @@ public class LDAPLookup implements LookupManager @Override public ItemPath resolvePath(DomainPath domPath) throws InvalidItemPathException, ObjectNotFoundException { - ItemPath referencedPath = null; + LDAPEntry domEntry = LDAPLookupUtils.getEntry(mLDAPAuth.getAuthObject(), getFullDN(domPath), LDAPSearchConstraints.DEREF_ALWAYS); String entityKey = LDAPLookupUtils.getFirstAttributeValue(domEntry, - "intsyskey"); + "cn"); Logger.msg(7, "DomainPath " + domPath + " is a reference to " + entityKey); String objClass = LDAPLookupUtils.getFirstAttributeValue(domEntry, "objectClass"); - if (objClass.equals("cristalagent")) - referencedPath = new AgentPath(Integer.parseInt(entityKey)); - else - referencedPath = new ItemPath(Integer.parseInt(entityKey)); + try { + ItemPath referencedPath = new ItemPath(UUID.fromString(entityKey)); + if (objClass.equals("cristalagent")) + return new AgentPath(referencedPath); + + return referencedPath; + } catch (IllegalArgumentException ex) { + throw new InvalidItemPathException(entityKey); + } - return referencedPath; } @@ -194,7 +199,7 @@ public class LDAPLookup implements LookupManager LDAPEntry newEntry = new LDAPEntry(getFullDN(path),attrSet); LDAPLookupUtils.addEntry(mLDAPAuth.getAuthObject(),newEntry); if (path instanceof DomainPath) - Gateway.getProxyServer().sendProxyEvent(new ProxyMessage(ProxyMessage.NA, path.toString(), ProxyMessage.ADDED)); + Gateway.getProxyServer().sendProxyEvent(new ProxyMessage(null, path.toString(), ProxyMessage.ADDED)); } catch (LDAPException ex) { if (ex.getResultCode() == LDAPException.ENTRY_ALREADY_EXISTS) throw new ObjectAlreadyExistsException(ex.getLDAPErrorMessage(), ""); @@ -214,7 +219,7 @@ public class LDAPLookup implements LookupManager throw new ObjectCannotBeUpdated(ex.getLDAPErrorMessage(), ""); } if (path instanceof DomainPath) { - Gateway.getProxyServer().sendProxyEvent(new ProxyMessage(ProxyMessage.NA, path.toString(), ProxyMessage.DELETED)); + Gateway.getProxyServer().sendProxyEvent(new ProxyMessage(null, path.toString(), ProxyMessage.DELETED)); } } @@ -364,16 +369,16 @@ public class LDAPLookup implements LookupManager } @Override - public ItemPath getItemPath(int sysKey) throws ObjectNotFoundException, InvalidItemPathException { + public ItemPath getItemPath(UUID uuid) throws ObjectNotFoundException, InvalidItemPathException { String[] attr = { LDAPConnection.ALL_USER_ATTRS }; try { - ItemPath item = new ItemPath(sysKey); + ItemPath item = new ItemPath(uuid); LDAPEntry anEntry=mLDAPAuth.getAuthObject().read(getDN(item)+mLocalPath,attr); String type = LDAPLookupUtils.getFirstAttributeValue(anEntry, "objectClass"); if (type.equals("cristalentity")) return item; else if (type.equals("cristalagent")) - return new AgentPath(sysKey); + return new AgentPath(item); else throw new ObjectNotFoundException("Not an entity", ""); @@ -394,29 +399,35 @@ public class LDAPLookup implements LookupManager protected Path nodeToPath(LDAPEntry entry) throws InvalidItemPathException, ObjectNotFoundException { String dn = entry.getDN(); + UUID entityKey; + org.omg.CORBA.Object ior; // extract syskey - int entityKey = -1; try { - String entityKeyStr = LDAPLookupUtils.getFirstAttributeValue(entry,"intsyskey"); - entityKey = Integer.parseInt(entityKeyStr); - } catch (Exception e) { } + String entityKeyStr = LDAPLookupUtils.getFirstAttributeValue(entry,"cn"); + entityKey = UUID.fromString(entityKeyStr); + } catch (ObjectNotFoundException ex) { + entityKey = null; + } catch (IllegalArgumentException ex) { + entityKey = null; + } + // extract IOR - org.omg.CORBA.Object ior = null; try { - String stringIOR = LDAPLookupUtils.getFirstAttributeValue(entry,"ior"); - ior = Gateway.getORB().string_to_object(stringIOR); - } catch (ObjectNotFoundException e2) { } - + String stringIOR = LDAPLookupUtils.getFirstAttributeValue(entry,"ior"); + ior = Gateway.getORB().string_to_object(stringIOR); + } catch (ObjectNotFoundException ex) { + ior = null; + } + /* Find the right path class */ Path thisPath; if (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalagent")) { //cristalagent String agentID = LDAPLookupUtils.getFirstAttributeValue(entry,"uid"); - thisPath = new AgentPath(entityKey, agentID); + thisPath = new AgentPath(new ItemPath(entityKey), agentID); } - else if (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalrole")) { //cristalrole thisPath = new RolePath(LDAPDN.explodeDN(dn,true)[0], @@ -433,14 +444,8 @@ public class LDAPLookup implements LookupManager (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalcontext") && dn.endsWith(mItemTypeRoot))) { if(dn.endsWith(mItemTypeRoot)) { - ItemPath entityPath; - if (entityKey != -1) - entityPath = new ItemPath(entityKey); - else { - entityPath = new ItemPath(); - entityPath.setPath(getPathComponents(dn.substring(0, dn.lastIndexOf(mItemTypeRoot)))); - } - thisPath = entityPath; + ItemPath entityPath = new ItemPath(entityKey); + thisPath = entityPath; } else throw new ObjectNotFoundException("Entity found outside entity tree"); @@ -511,7 +516,7 @@ public class LDAPLookup implements LookupManager DomainPath domPath = (DomainPath)path; attrs.add(new LDAPAttribute("cn",domPath.getName())); try { - attrs.add(new LDAPAttribute("aliasedObjectName",getFullDN(domPath.getEntity()))); + attrs.add(new LDAPAttribute("aliasedObjectName",getFullDN(domPath.getItemPath()))); String objectclass_values[] = { "alias", "aliasObject" }; attrs.add(new LDAPAttribute("objectclass",objectclass_values)); } catch (ObjectNotFoundException e) { // no entity - is a context @@ -521,8 +526,7 @@ public class LDAPLookup implements LookupManager else if (path instanceof ItemPath) { ItemPath itemPath = (ItemPath)path; - attrs.add(new LDAPAttribute("intsyskey",Integer.toString(itemPath.getSysKey()))); - attrs.add(new LDAPAttribute("cn", itemPath.getPath()[itemPath.getPath().length-1])); + attrs.add(new LDAPAttribute("cn", itemPath.getUUID().toString())); if (itemPath.getIOR() != null) attrs.add(new LDAPAttribute("ior", Gateway.getORB().object_to_string(itemPath.getIOR()))); diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java deleted file mode 100644 index 93fcf5c..0000000 --- a/src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java +++ /dev/null @@ -1,103 +0,0 @@ -package com.c2kernel.lookup.ldap; - -import com.c2kernel.common.ObjectCannotBeUpdated; -import com.c2kernel.common.ObjectNotFoundException; -import com.c2kernel.lookup.AgentPath; -import com.c2kernel.lookup.InvalidItemPathException; -import com.c2kernel.lookup.ItemPath; -import com.c2kernel.persistency.ClusterStorageException; -import com.c2kernel.persistency.NextKeyManager; -import com.c2kernel.process.Gateway; -import com.c2kernel.process.auth.Authenticator; -import com.c2kernel.utils.Logger; -import com.novell.ldap.LDAPEntry; - -/************************************************************************** - * - * $Revision: 1.2 $ - * $Date: 2005/04/27 13:47:24 $ - * - * Copyright (C) 2003 CERN - European Organization for Nuclear Research - * All rights reserved. - **************************************************************************/ - -// public static final String codeRevision = "$Revision: 1.2 $ $Date: 2005/04/27 13:47:24 $ $Author: abranson $"; -public class LDAPNextKeyManager implements NextKeyManager { - - protected LDAPAuthManager ldap; - protected String lastKeyPath; - - public LDAPNextKeyManager() { - } - - @Override - public void open(Authenticator auth) { - this.ldap = (LDAPAuthManager)auth; - LDAPProperties props = new LDAPProperties(Gateway.getProperties()); - this.lastKeyPath = "cn=last,cn=entity,"+props.mLocalPath; - } - - @Override - public synchronized ItemPath generateNextEntityKey() - throws ObjectCannotBeUpdated, ObjectNotFoundException - { - ItemPath lastKey = getLastEntityPath(); - - try { - lastKey.setSysKey(lastKey.getSysKey()+1); - } catch (InvalidItemPathException ex) { - throw new ObjectCannotBeUpdated("Invalid syskey "+(lastKey.getSysKey()+1)+". Maybe centre is full."); - } - //test that storage is empty for that key - try { - if (Gateway.getStorage().getClusterContents(lastKey.getSysKey(), "").length > 0) - throw new ObjectCannotBeUpdated("NextKeyManager: Storage already contains data for syskey "+lastKey.getSysKey()+ - ". Storage is out of sync with nextkey. Please contact an administrator", ""); - } catch (ClusterStorageException e) { - Logger.error(e); - throw new ObjectCannotBeUpdated("Could not check storage for prior data for the next generated systemKey: "+e.getMessage()); - } - - //set the last key - writeLastEntityKey(lastKey.getSysKey()); - - return lastKey; - } - - @Override - public synchronized AgentPath generateNextAgentKey() - throws ObjectCannotBeUpdated, ObjectNotFoundException { - ItemPath newEntity = generateNextEntityKey(); - return new AgentPath(newEntity); - } - - @Override - public void writeLastEntityKey(int sysKey) throws ObjectCannotBeUpdated, ObjectNotFoundException { - LDAPEntry lastKeyEntry = LDAPLookupUtils.getEntry(ldap.getAuthObject(),lastKeyPath); - LDAPLookupUtils.setAttributeValue(ldap.getAuthObject(), lastKeyEntry,"intsyskey",Integer.toString(sysKey)); - } - - @Override - public ItemPath getLastEntityPath() throws ObjectNotFoundException - { - LDAPEntry lastKeyEntry = LDAPLookupUtils.getEntry(ldap.getAuthObject(),lastKeyPath); - String lastKey = LDAPLookupUtils.getFirstAttributeValue(lastKeyEntry,"intsyskey"); - try { - int sysKey = Integer.parseInt(lastKey); - ItemPath sysPath = new ItemPath(sysKey); - return sysPath; - } catch (InvalidItemPathException ex) { - throw new ObjectNotFoundException("Invalid syskey. Maybe centre is full."); - } catch (NumberFormatException ex) { - throw new ObjectNotFoundException("Invalid syskey in lastkey."); - } - - } - - @Override - public void close() { - ldap = null; - lastKeyPath = null; - } - -} diff --git a/src/main/resources/LDAPboot.txt b/src/main/resources/LDAPboot.txt index 995d47d..da74bfd 100644 --- a/src/main/resources/LDAPboot.txt +++ b/src/main/resources/LDAPboot.txt @@ -1,4 +1,3 @@ cn=entity, -cn=last,cn=entity, cn=domain, cn=agent,cn=domain, \ No newline at end of file -- cgit v1.2.3