From c6204281c819ea4514952ed7623e282babcb5f8c Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Wed, 27 Feb 2013 21:36:23 +0100 Subject: More --- .../java/com/c2kernel/lookup/ldap/LDAPLookup.java | 41 ++++++++++++---------- .../com/c2kernel/lookup/ldap/LDAPRoleManager.java | 40 ++++++++++++++------- 2 files changed, 49 insertions(+), 32 deletions(-) (limited to 'src/main/java/com/c2kernel/lookup/ldap') diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java index 3ca1749..d52371c 100644 --- a/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java @@ -53,6 +53,9 @@ public class LDAPLookup implements Lookup private LDAPPropertyManager mPropManager; private final LDAPRoleManager mRoleManager; + protected static String mGlobalPath; //cern.ch + protected static String mRootPath; //cristal2 + protected static String mLocalPath; //lab27 /** @@ -69,14 +72,14 @@ public class LDAPLookup implements Lookup mLDAPConn = createConnection(mLDAPProps); - Path.mGlobalPath=props.mGlobalPath; - Path.mRootPath=props.mRootPath; - Path.mLocalPath=props.mLocalPath; + mGlobalPath=props.mGlobalPath; + mRootPath=props.mRootPath; + mLocalPath=props.mLocalPath; - EntityPath.mTypeRoot = "cn=entity,"+props.mLocalPath; - DomainPath.mTypeRoot = "cn=domain,"+props.mLocalPath; + EntityPath.setTypeRoot("cn=entity,"+props.mLocalPath); + DomainPath.setTypeRoot("cn=domain,"+props.mLocalPath); - mNextKeyManager = new NextKeyManager(this, "cn=last,"+EntityPath.mTypeRoot); + mNextKeyManager = new NextKeyManager(this, "cn=last,"+EntityPath.getTypeRoot()); Logger.msg(7, "LDAP.useOldProps="+Gateway.getProperty("LDAP.useOldProps", "false")); if (Gateway.getProperty("LDAP.useOldProps", "false").equals("true")) { Logger.debug(1, "Using Kernel 2.1 LDAP Property Format"); @@ -86,7 +89,7 @@ public class LDAPLookup implements Lookup Logger.debug(1, "Using Kernel 2.2 LDAP Property Format"); mPropManager = new LDAPPropertyManager(this); } - mRoleManager = new LDAPRoleManager(this, "cn=agent,"+DomainPath.mTypeRoot, EntityPath.mTypeRoot); + mRoleManager = new LDAPRoleManager(this, "cn=agent,"+DomainPath.getTypeRoot(), EntityPath.getTypeRoot()); } @@ -159,7 +162,7 @@ public class LDAPLookup implements Lookup * Disconnects the connection with the LDAP server during shutdown */ @Override - public void disconnect() { + public void close() { Logger.msg(1, "LDAP Lookup: Shutting down LDAP connection."); if (mLDAPConn != null) { try { @@ -265,7 +268,7 @@ public class LDAPLookup implements Lookup public void delete(Path path) throws ObjectCannotBeUpdated { try { - LDAPLookupUtils.delete(getConnection(),path.getDN()+Path.mLocalPath); + LDAPLookupUtils.delete(getConnection(),path.getDN()+mLocalPath); } catch (LDAPException ex) { throw new ObjectCannotBeUpdated(ex.getLDAPErrorMessage(), ""); } @@ -281,7 +284,7 @@ public class LDAPLookup implements Lookup if (!LDAPLookupUtils.exists(getConnection(),dn)) { String listDN[] = path.getPath(); - String name = "cn="+ path.getRoot() + "," + Path.mLocalPath; + String name = "cn="+ path.getRoot() + "," + mLocalPath; int i=0; while (i getEntityClass(Path path) throws ObjectNotFoundException { String[] attr = { LDAPConnection.ALL_USER_ATTRS }; try { - LDAPEntry anEntry=getConnection().read(path.getDN()+Path.mLocalPath,attr); + LDAPEntry anEntry=getConnection().read(path.getDN()+mLocalPath,attr); String type = LDAPLookupUtils.getFirstAttributeValue(anEntry, "objectClass"); if (type.equals("cristalentity")) return TraceableEntity.class; @@ -460,16 +463,16 @@ public class LDAPLookup implements Lookup LDAPLookupUtils.getFirstAttributeValue(entry, "jobList").equals("TRUE")); } else if (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","aliasObject") || - (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalcontext") && dn.endsWith(DomainPath.mTypeRoot))) + (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalcontext") && dn.endsWith(DomainPath.getTypeRoot()))) { DomainPath domainPath = new DomainPath(); domainPath.setDN(dn); thisPath = domainPath; } else if (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalentity") || - (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalcontext") && dn.endsWith(EntityPath.mTypeRoot))) + (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalcontext") && dn.endsWith(EntityPath.getTypeRoot()))) { - if(dn.endsWith(EntityPath.mTypeRoot)) { + if(dn.endsWith(EntityPath.getTypeRoot())) { EntityPath entityPath; if (entityKey != -1) entityPath = new EntityPath(entityKey); diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPRoleManager.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPRoleManager.java index 816d1c4..42032f3 100644 --- a/src/main/java/com/c2kernel/lookup/ldap/LDAPRoleManager.java +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPRoleManager.java @@ -1,7 +1,7 @@ package com.c2kernel.lookup.ldap; import java.util.ArrayList; -import java.util.Enumeration; +import java.util.Iterator; import com.c2kernel.common.ObjectAlreadyExistsException; import com.c2kernel.common.ObjectCannotBeUpdated; @@ -11,6 +11,7 @@ import com.c2kernel.lookup.InvalidEntityPathException; import com.c2kernel.lookup.Path; import com.c2kernel.lookup.RoleManager; import com.c2kernel.lookup.RolePath; +import com.c2kernel.process.Gateway; import com.c2kernel.utils.Logger; import com.novell.ldap.LDAPConnection; import com.novell.ldap.LDAPEntry; @@ -92,7 +93,8 @@ public class LDAPRoleManager implements RoleManager { throw new ObjectCannotBeUpdated("Agent " + agent.getAgentName() + " already has role " + role.getName()); } - public void removeRole(AgentPath agent, RolePath role) + @Override + public void removeRole(AgentPath agent, RolePath role) throws ObjectCannotBeUpdated, ObjectNotFoundException { LDAPEntry roleEntry = LDAPLookupUtils.getEntry(mLdap.getConnection(), role.getFullDN()); @@ -102,12 +104,13 @@ public class LDAPRoleManager implements RoleManager { throw new ObjectCannotBeUpdated("Agent did not have that role"); } - public boolean hasRole(AgentPath agent, RolePath role) { + @Override + public boolean hasRole(AgentPath agent, RolePath role) { String filter = "(&(objectclass=cristalrole)(uniqueMember="+agent.getFullDN()+")(cn="+role.getName()+"))"; LDAPSearchConstraints searchCons = new LDAPSearchConstraints(); searchCons.setBatchSize(0); searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER ); - return mLdap.search(mRolePath,LDAPConnection.SCOPE_SUB,filter,searchCons).hasMoreElements(); + return mLdap.search(mRolePath,LDAPConnection.SCOPE_SUB,filter,searchCons).hasNext(); } @Override @@ -150,12 +153,12 @@ public class LDAPRoleManager implements RoleManager { LDAPSearchConstraints searchCons = new LDAPSearchConstraints(); searchCons.setBatchSize(0); searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER ); - Enumeration roles = mLdap.search(mRolePath,LDAPConnection.SCOPE_SUB,filter,searchCons); + Iterator roles = mLdap.search(mRolePath,LDAPConnection.SCOPE_SUB,filter,searchCons); ArrayList roleList = new ArrayList(); - while(roles.hasMoreElements()) + while(roles.hasNext()) { - RolePath path = (RolePath) roles.nextElement(); + RolePath path = (RolePath) roles.next(); roleList.add(path); } RolePath[] roleArr = new RolePath[roleList.size()]; @@ -180,10 +183,10 @@ public class LDAPRoleManager implements RoleManager { searchCons.setBatchSize(0); searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER ); String filter = "(&(objectclass=cristalagent)(uid="+agentName+"))"; - Enumeration res = mLdap.search(mEntityPath,LDAPConnection.SCOPE_SUB,filter,searchCons); - if (!res.hasMoreElements()) + Iterator res = mLdap.search(mEntityPath,LDAPConnection.SCOPE_SUB,filter,searchCons); + if (!res.hasNext()) throw new ObjectNotFoundException("Agent not found"); - Path result = res.nextElement(); + Path result = res.next(); if (result instanceof AgentPath) return (AgentPath)result; else @@ -197,14 +200,25 @@ public class LDAPRoleManager implements RoleManager { searchCons.setBatchSize(0); searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER ); String filter = "(&(objectclass=cristalrole)(cn="+roleName+"))"; - Enumeration res = mLdap.search(mRolePath,LDAPConnection.SCOPE_SUB,filter,searchCons); - if (!res.hasMoreElements()) + Iterator res = mLdap.search(mRolePath,LDAPConnection.SCOPE_SUB,filter,searchCons); + if (!res.hasNext()) throw new ObjectNotFoundException("Role not found"); - Path result = res.nextElement(); + Path result = res.next(); if (result instanceof RolePath) return (RolePath)result; else throw new ObjectNotFoundException("Entry was not a Role"); } + /* (non-Javadoc) + * @see com.c2kernel.lookup.RoleManager#getAgentName(com.c2kernel.lookup.AgentPath) + */ + @Override + public String getAgentName(AgentPath agentPath) throws ObjectNotFoundException { + LDAPLookup ldap = (LDAPLookup)Gateway.getLookup(); + LDAPEntry agentEntry = LDAPLookupUtils.getEntry(ldap.getConnection(), agentPath.getDN() + ldap.mLocalPath); + + return LDAPLookupUtils.getFirstAttributeValue(agentEntry,"uid"); + } + } -- cgit v1.2.3