From fdaf02db3616b7b36168e97462b7d55adc47fee2 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Thu, 24 Jul 2014 09:23:15 +0200 Subject: Move cluster storage implementation into lookup.ldap package, leave stubs in the old persistency package for backwards compatibility. --- .../com/c2kernel/lookup/ldap/LDAPClientReader.java | 44 +++++ .../c2kernel/lookup/ldap/LDAPClusterStorage.java | 176 ++++++++++++++++++++ .../com/c2kernel/persistency/LDAPClientReader.java | 50 ++---- .../c2kernel/persistency/LDAPClusterStorage.java | 184 ++------------------- 4 files changed, 242 insertions(+), 212 deletions(-) create mode 100644 src/main/java/com/c2kernel/lookup/ldap/LDAPClientReader.java create mode 100644 src/main/java/com/c2kernel/lookup/ldap/LDAPClusterStorage.java (limited to 'src') diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPClientReader.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPClientReader.java new file mode 100644 index 0000000..a02d4fd --- /dev/null +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPClientReader.java @@ -0,0 +1,44 @@ +package com.c2kernel.lookup.ldap; + +import com.c2kernel.entity.C2KLocalObject; +import com.c2kernel.persistency.ClusterStorageException; + +/** Allows clients to directly load properties and collections from the LDAP +* so no CORBA calls need to be made during normal browsing +*/ + +public class LDAPClientReader extends LDAPClusterStorage { + // return all readwrite support as readonly + @Override + public short queryClusterSupport(String clusterType) { + return (short)(super.queryClusterSupport(clusterType) & READ); + } + + + /** + * @see com.c2kernel.persistency.ClusterStorage#delete(Integer, String) + */ + @Override + public void delete(Integer sysKey, String path) + throws ClusterStorageException { + throw new ClusterStorageException("Writing not supported in ClientReader"); + } + + /** + * @see com.c2kernel.persistency.ClusterStorage#getName() + */ + @Override + public String getName() { + return "LDAP Client Cluster Reader"; + } + + /** + * @see com.c2kernel.persistency.ClusterStorage#put(Integer, String, C2KLocalObject) + */ + + public void put(Integer sysKey, 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 new file mode 100644 index 0000000..f309378 --- /dev/null +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPClusterStorage.java @@ -0,0 +1,176 @@ +package com.c2kernel.lookup.ldap; +import java.util.ArrayList; +import java.util.StringTokenizer; + +import com.c2kernel.common.ObjectNotFoundException; +import com.c2kernel.entity.C2KLocalObject; +import com.c2kernel.lookup.InvalidItemPathException; +import com.c2kernel.lookup.ItemPath; +import com.c2kernel.lookup.Lookup; +import com.c2kernel.persistency.ClusterStorage; +import com.c2kernel.persistency.ClusterStorageException; +import com.c2kernel.process.Gateway; +import com.c2kernel.process.auth.Authenticator; +import com.c2kernel.property.Property; +import com.c2kernel.utils.Logger; + +public class LDAPClusterStorage extends ClusterStorage { + LDAPPropertyManager ldapStore; + + @Override + public void open(Authenticator auth) throws ClusterStorageException { + Lookup lookup = Gateway.getLookup(); + if (lookup instanceof LDAPLookup) + ldapStore = ((LDAPLookup)lookup).getPropManager(); + else + throw new ClusterStorageException("Cannot use LDAP cluster storage without LDAP Lookup"); + + } + + @Override + public void close() throws ClusterStorageException { + } + + // introspection + @Override + public short queryClusterSupport(String clusterType) { + if (clusterType.equals(PROPERTY)) + return READWRITE; + else + return NONE; + } + + @Override + public String getName() { + return "LDAP Cluster Storage"; + } + + @Override + public String getId() { + return "LDAP"; + } + + // retrieve object by path + @Override + public C2KLocalObject get(Integer sysKey, String path) throws ClusterStorageException { + Logger.msg(6, "LDAPClusterStorage.get() - "+sysKey+"/"+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); + newObj = newProperty; + } catch (ObjectNotFoundException ex) { + throw new ClusterStorageException("Property "+objName+" not found in "+sysKey); + } + + } + else + throw new ClusterStorageException("Cluster type "+type+" not supported."); + + return newObj; + } + // store object by path + @Override + public void put(Integer sysKey, C2KLocalObject obj) throws ClusterStorageException { + Logger.msg(6, "LDAPClusterStorage.put() - "+sysKey+"/"+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); + } catch (Exception e1) { + Logger.error(e1); + throw new ClusterStorageException("LDAPClusterStorage - could not write property"); + } + } + else + throw new ClusterStorageException("Cluster type "+type+" not supported."); + + } + // delete cluster + @Override + public void delete(Integer sysKey, 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()); + } catch (Exception e1) { + Logger.error(e1); + throw new ClusterStorageException("LDAPClusterStorage - could not delete property"); + } + } + else + throw new ClusterStorageException("Cluster type "+type+" not supported."); + + } + + /* navigation */ + + // directory listing + @Override + public String[] getClusterContents(Integer sysKey, String path) throws ClusterStorageException { + Logger.msg(6, "LDAPClusterStorage.getClusterContents() - "+sysKey+"/"+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"); + } + } +} diff --git a/src/main/java/com/c2kernel/persistency/LDAPClientReader.java b/src/main/java/com/c2kernel/persistency/LDAPClientReader.java index ac9215c..4458ac6 100644 --- a/src/main/java/com/c2kernel/persistency/LDAPClientReader.java +++ b/src/main/java/com/c2kernel/persistency/LDAPClientReader.java @@ -1,43 +1,15 @@ package com.c2kernel.persistency; -import com.c2kernel.entity.C2KLocalObject; - -/** Allows clients to directly load properties and collections from the LDAP -* so no CORBA calls need to be made during normal browsing -*/ - -public class LDAPClientReader extends LDAPClusterStorage { - // return all readwrite support as readonly - @Override - public short queryClusterSupport(String clusterType) { - return (short)(super.queryClusterSupport(clusterType) & READ); - } - - - /** - * @see com.c2kernel.persistency.ClusterStorage#delete(Integer, String) - */ - @Override - public void delete(Integer sysKey, String path) - throws ClusterStorageException { - throw new ClusterStorageException("Writing not supported in ClientReader"); - } - - /** - * @see com.c2kernel.persistency.ClusterStorage#getName() - */ - @Override - public String getName() { - return "LDAP Client Cluster Reader"; +/** + * Provided for easier loading (may be referenced without package in ClusterStorage property) + * + * @author abranson + * + */ +public class LDAPClientReader extends com.c2kernel.lookup.ldap.LDAPClientReader { + + public LDAPClientReader() { + super(); } - - /** - * @see com.c2kernel.persistency.ClusterStorage#put(Integer, String, C2KLocalObject) - */ - - public void put(Integer sysKey, String path, C2KLocalObject obj) - throws ClusterStorageException { - throw new ClusterStorageException("Writing not supported in ClientReader"); - } - + } diff --git a/src/main/java/com/c2kernel/persistency/LDAPClusterStorage.java b/src/main/java/com/c2kernel/persistency/LDAPClusterStorage.java index 4762a33..005c6e8 100644 --- a/src/main/java/com/c2kernel/persistency/LDAPClusterStorage.java +++ b/src/main/java/com/c2kernel/persistency/LDAPClusterStorage.java @@ -1,176 +1,14 @@ package com.c2kernel.persistency; -import java.util.ArrayList; -import java.util.StringTokenizer; -import com.c2kernel.common.ObjectNotFoundException; -import com.c2kernel.entity.C2KLocalObject; -import com.c2kernel.lookup.InvalidItemPathException; -import com.c2kernel.lookup.ItemPath; -import com.c2kernel.lookup.Lookup; -import com.c2kernel.lookup.ldap.LDAPLookup; -import com.c2kernel.lookup.ldap.LDAPPropertyManager; -import com.c2kernel.process.Gateway; -import com.c2kernel.process.auth.Authenticator; -import com.c2kernel.property.Property; -import com.c2kernel.utils.Logger; - -public class LDAPClusterStorage extends ClusterStorage { - LDAPPropertyManager ldapStore; - - @Override - public void open(Authenticator auth) throws ClusterStorageException { - Lookup lookup = Gateway.getLookup(); - if (lookup instanceof LDAPLookup) - ldapStore = ((LDAPLookup)lookup).getPropManager(); - else - throw new ClusterStorageException("Cannot use LDAP cluster storage without LDAP Lookup"); - - } - - @Override - public void close() throws ClusterStorageException { - } - - // introspection - @Override - public short queryClusterSupport(String clusterType) { - if (clusterType.equals(PROPERTY)) - return READWRITE; - else - return NONE; - } - - @Override - public String getName() { - return "LDAP Cluster Storage"; - } - - @Override - public String getId() { - return "LDAP"; - } - - // retrieve object by path - @Override - public C2KLocalObject get(Integer sysKey, String path) throws ClusterStorageException { - Logger.msg(6, "LDAPClusterStorage.get() - "+sysKey+"/"+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); - newObj = newProperty; - } catch (ObjectNotFoundException ex) { - throw new ClusterStorageException("Property "+objName+" not found in "+sysKey); - } - - } - else - throw new ClusterStorageException("Cluster type "+type+" not supported."); - - return newObj; - } - // store object by path - @Override - public void put(Integer sysKey, C2KLocalObject obj) throws ClusterStorageException { - Logger.msg(6, "LDAPClusterStorage.put() - "+sysKey+"/"+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); - } catch (Exception e1) { - Logger.error(e1); - throw new ClusterStorageException("LDAPClusterStorage - could not write property"); - } - } - else - throw new ClusterStorageException("Cluster type "+type+" not supported."); - - } - // delete cluster - @Override - public void delete(Integer sysKey, 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()); - } catch (Exception e1) { - Logger.error(e1); - throw new ClusterStorageException("LDAPClusterStorage - could not delete property"); - } - } - else - throw new ClusterStorageException("Cluster type "+type+" not supported."); - - } - - /* navigation */ - - // directory listing - @Override - public String[] getClusterContents(Integer sysKey, String path) throws ClusterStorageException { - Logger.msg(6, "LDAPClusterStorage.getClusterContents() - "+sysKey+"/"+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"); - } - } +/** + * Provided for easier loading (may be referenced without package in ClusterStorage property) + * + * @author abranson + * + */ +public class LDAPClusterStorage extends com.c2kernel.lookup.ldap.LDAPClusterStorage { + + public LDAPClusterStorage() { + super(); + } } -- cgit v1.2.3 From 7ed322e896143cc687e0f3ada8a659473aea0567 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Thu, 24 Jul 2014 09:31:20 +0200 Subject: Don't depend on gateway properties in the constructor of LDAPLookup, as it may be instantiated before the Gateway is initialized. --- .../com/c2kernel/lookup/ldap/LDAPAuthManager.java | 3 ++- .../java/com/c2kernel/lookup/ldap/LDAPLookup.java | 31 +++++++++++++--------- 2 files changed, 21 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPAuthManager.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPAuthManager.java index 6737192..1ddb452 100644 --- a/src/main/java/com/c2kernel/lookup/ldap/LDAPAuthManager.java +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPAuthManager.java @@ -26,7 +26,8 @@ public class LDAPAuthManager implements Authenticator { ldapProps.mUser = ""; ldapProps.mPassword = ""; mLDAPConn = LDAPLookupUtils.createConnection(ldapProps); - LDAPLookup anonLookup = new LDAPLookup(ldapProps); + LDAPLookup anonLookup = new LDAPLookup(); + anonLookup.initPaths(ldapProps); anonLookup.open(this); String agentDN = anonLookup.getFullDN(anonLookup.getAgentPath(agentName)); diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java index 0a27a0d..372a91c 100644 --- a/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java @@ -56,17 +56,21 @@ public class LDAPLookup implements LookupManager protected LDAPAuthManager mLDAPAuth; protected LDAPPropertyManager mPropManager; - final String mItemTypeRoot, mDomainTypeRoot, mGlobalPath, mRootPath, mLocalPath, mRolePath; + private String mGlobalPath, mRootPath, mLocalPath, mRolePath, mItemTypeRoot, mDomainTypeRoot; + LDAPProperties ldapProps; + public LDAPLookup() { + } + + /** - * Creates a new LDAPLookup manager with the properties supplied. - * This should be only done by the Gateway during initialisation. - * - * @param props The LDAP properties object that extracts LDAP connection properties from the global c2kprops + * Initializes the DN paths from the Root, global and local paths supplied by the LDAP properties. + * @param props */ - public LDAPLookup(LDAPProperties props) + protected void initPaths(LDAPProperties props) { Logger.msg(8,"LDAPLookup - initialising."); + ldapProps = props; mGlobalPath=props.mGlobalPath; mRootPath=props.mRootPath; @@ -75,17 +79,20 @@ public class LDAPLookup implements LookupManager mItemTypeRoot = "cn=entity,"+props.mLocalPath; mDomainTypeRoot = "cn=domain,"+props.mLocalPath; mRolePath = "cn=agent,"+mDomainTypeRoot; - - } - - public LDAPLookup() { - this(new LDAPProperties(Gateway.getProperties())); } + /** + * Initializes the LDAPLookup manager with the Gateway properties. + * This should be only done by the Gateway during initialisation. + * + * @param auth A LDAPAuthManager authenticator + */ @Override public void open(Authenticator auth) { + if (ldapProps == null) + initPaths(new LDAPProperties(Gateway.getProperties())); + mLDAPAuth = (LDAPAuthManager)auth; - Logger.msg(7, "LDAP.useOldProps="+Gateway.getProperties().getBoolean("LDAP.useOldProps", false)); mPropManager = new LDAPPropertyManager(this, mLDAPAuth); } -- cgit v1.2.3