From c6204281c819ea4514952ed7623e282babcb5f8c Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Wed, 27 Feb 2013 21:36:23 +0100 Subject: More --- src/main/java/com/c2kernel/lookup/AgentPath.java | 15 ++++---- src/main/java/com/c2kernel/lookup/DomainPath.java | 10 ++++-- src/main/java/com/c2kernel/lookup/EntityPath.java | 8 +++++ src/main/java/com/c2kernel/lookup/Lookup.java | 2 +- src/main/java/com/c2kernel/lookup/Path.java | 15 +++----- src/main/java/com/c2kernel/lookup/RoleManager.java | 6 ++++ src/main/java/com/c2kernel/lookup/RolePath.java | 2 -- .../java/com/c2kernel/lookup/ldap/LDAPLookup.java | 41 ++++++++++++---------- .../com/c2kernel/lookup/ldap/LDAPRoleManager.java | 40 ++++++++++++++------- 9 files changed, 84 insertions(+), 55 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 c200d75..5dd177b 100644 --- a/src/main/java/com/c2kernel/lookup/AgentPath.java +++ b/src/main/java/com/c2kernel/lookup/AgentPath.java @@ -16,9 +16,8 @@ import java.security.NoSuchAlgorithmException; import org.apache.xerces.impl.dv.util.Base64; import com.c2kernel.common.ObjectNotFoundException; -import com.c2kernel.lookup.ldap.LDAPLookupUtils; import com.c2kernel.process.Gateway; -import com.novell.ldap.LDAPEntry; +import com.c2kernel.utils.Logger; /** @@ -68,11 +67,11 @@ public class AgentPath extends EntityPath if (mAgentName==null) { try { - LDAPEntry agentEntry = LDAPLookupUtils.getEntry(Gateway.getLookup().getConnection(), this.getDN() + mLocalPath); - mAgentName = LDAPLookupUtils.getFirstAttributeValue(agentEntry,"uid"); - } catch (ObjectNotFoundException e) { - mAgentName = ""; - } + mAgentName = Gateway.getLookup().getRoleManager().getAgentName(this); + } catch (ObjectNotFoundException e) { + Logger.error(e); + mAgentName=""; + } } return mAgentName; } @@ -111,7 +110,7 @@ public class AgentPath extends EntityPath mAgentName; } - static String generateUserPassword(String pass, String algo) throws NoSuchAlgorithmException { + public static String generateUserPassword(String pass, String algo) throws NoSuchAlgorithmException { MessageDigest sha = MessageDigest.getInstance(algo); sha.reset(); sha.update(pass.getBytes()); diff --git a/src/main/java/com/c2kernel/lookup/DomainPath.java b/src/main/java/com/c2kernel/lookup/DomainPath.java index 523ac49..a1c2275 100644 --- a/src/main/java/com/c2kernel/lookup/DomainPath.java +++ b/src/main/java/com/c2kernel/lookup/DomainPath.java @@ -13,8 +13,6 @@ package com.c2kernel.lookup; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.process.Gateway; import com.c2kernel.utils.Logger; -import com.novell.ldap.LDAPAttribute; -import com.novell.ldap.LDAPAttributeSet; /** @@ -66,6 +64,14 @@ public class DomainPath extends Path public String getRoot() { return "domain"; } + + public static void setTypeRoot(String root) { + mTypeRoot = root; + } + + public static String getTypeRoot() { + return mTypeRoot; + } public DomainPath getParent() { if (mPath.length == 0) diff --git a/src/main/java/com/c2kernel/lookup/EntityPath.java b/src/main/java/com/c2kernel/lookup/EntityPath.java index 8e93f8f..deb44a3 100644 --- a/src/main/java/com/c2kernel/lookup/EntityPath.java +++ b/src/main/java/com/c2kernel/lookup/EntityPath.java @@ -81,6 +81,14 @@ public class EntityPath extends Path return "entity"; } + public static void setTypeRoot(String root) { + mTypeRoot = root; + } + + public static String getTypeRoot() { + return mTypeRoot; + } + @Override public EntityPath getEntity() throws ObjectNotFoundException { return this; diff --git a/src/main/java/com/c2kernel/lookup/Lookup.java b/src/main/java/com/c2kernel/lookup/Lookup.java index 060b5de..4c79c8b 100644 --- a/src/main/java/com/c2kernel/lookup/Lookup.java +++ b/src/main/java/com/c2kernel/lookup/Lookup.java @@ -31,7 +31,7 @@ public interface Lookup { public void install() throws ObjectNotFoundException; - public void disconnect(); + public void close(); public NextKeyManager getNextKeyManager(); diff --git a/src/main/java/com/c2kernel/lookup/Path.java b/src/main/java/com/c2kernel/lookup/Path.java index 3390007..8196d4d 100644 --- a/src/main/java/com/c2kernel/lookup/Path.java +++ b/src/main/java/com/c2kernel/lookup/Path.java @@ -13,12 +13,11 @@ package com.c2kernel.lookup; import java.io.Serializable; import java.util.ArrayList; import java.util.Enumeration; +import java.util.Iterator; import java.util.StringTokenizer; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.process.Gateway; -import com.novell.ldap.LDAPConnection; -import com.novell.ldap.LDAPSearchConstraints; /** @@ -51,10 +50,6 @@ public abstract class Path implements Serializable // // needed for unusual subclass constructors - protected static String mGlobalPath; //cern.ch - protected static String mRootPath; //cristal2 - protected static String mLocalPath; //lab27 - public Path() { } @@ -241,10 +236,10 @@ public abstract class Path implements Serializable } public Path find(String name) throws ObjectNotFoundException { - Enumeration e = Gateway.getLookup().search(this, name); - if (e.hasMoreElements()) { - Path thisPath = e.nextElement(); - if (e.hasMoreElements()) + Iterator e = Gateway.getLookup().search(this, name); + if (e.hasNext()) { + Path thisPath = e.next(); + if (e.hasNext()) throw new ObjectNotFoundException("More than one match for "+name, ""); return thisPath; } diff --git a/src/main/java/com/c2kernel/lookup/RoleManager.java b/src/main/java/com/c2kernel/lookup/RoleManager.java index a483559..0caa215 100644 --- a/src/main/java/com/c2kernel/lookup/RoleManager.java +++ b/src/main/java/com/c2kernel/lookup/RoleManager.java @@ -76,4 +76,10 @@ public interface RoleManager { */ public void removeRole(AgentPath agent, RolePath role) throws ObjectCannotBeUpdated, ObjectNotFoundException; + /** + * @param agentPath + * @return + */ + public String getAgentName(AgentPath agentPath) throws ObjectNotFoundException; + } diff --git a/src/main/java/com/c2kernel/lookup/RolePath.java b/src/main/java/com/c2kernel/lookup/RolePath.java index cdda034..ee24d63 100644 --- a/src/main/java/com/c2kernel/lookup/RolePath.java +++ b/src/main/java/com/c2kernel/lookup/RolePath.java @@ -17,8 +17,6 @@ import com.c2kernel.common.ObjectCannotBeUpdated; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.process.Gateway; import com.c2kernel.utils.Logger; -import com.novell.ldap.LDAPAttribute; -import com.novell.ldap.LDAPAttributeSet; 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