From 6e35118970c7af70eb0ac938859d794f7348d367 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Fri, 22 Feb 2013 14:04:33 +0100 Subject: Extracted LDAP specifics into subpackage --- .../com/c2kernel/lookup/ldap/NextKeyManager.java | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/main/java/com/c2kernel/lookup/ldap/NextKeyManager.java (limited to 'src/main/java/com/c2kernel/lookup/ldap/NextKeyManager.java') diff --git a/src/main/java/com/c2kernel/lookup/ldap/NextKeyManager.java b/src/main/java/com/c2kernel/lookup/ldap/NextKeyManager.java new file mode 100644 index 0000000..305fe65 --- /dev/null +++ b/src/main/java/com/c2kernel/lookup/ldap/NextKeyManager.java @@ -0,0 +1,74 @@ +package com.c2kernel.lookup.ldap; + +import com.c2kernel.common.ObjectCannotBeUpdated; +import com.c2kernel.common.ObjectNotFoundException; +import com.c2kernel.lookup.AgentPath; +import com.c2kernel.lookup.EntityPath; +import com.c2kernel.lookup.InvalidEntityPathException; +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 NextKeyManager { + + LDAPLookup ldap; + String lastKeyPath; + + public NextKeyManager(LDAPLookup ldap, String lastKeyPath) { + super(); + this.ldap = ldap; + this.lastKeyPath = lastKeyPath; + } + + public synchronized EntityPath generateNextEntityKey() + throws ObjectCannotBeUpdated, ObjectNotFoundException + { + EntityPath lastKey = getLastEntityPath(); + + try { + lastKey.setSysKey(lastKey.getSysKey()+1); + } catch (InvalidEntityPathException ex) { + throw new ObjectCannotBeUpdated("Invalid syskey "+(lastKey.getSysKey()+1)+". Maybe centre is full."); + } + //set the last key + writeLastEntityKey(lastKey.getSysKey()); + + return lastKey; + } + + public synchronized AgentPath generateNextAgentKey() + throws ObjectCannotBeUpdated, ObjectNotFoundException { + EntityPath newEntity = generateNextEntityKey(); + return new AgentPath(newEntity); + } + + public void writeLastEntityKey(int sysKey) throws ObjectCannotBeUpdated, ObjectNotFoundException { + LDAPEntry lastKeyEntry = LDAPLookupUtils.getEntry(ldap.getConnection(),lastKeyPath); + LDAPLookupUtils.setAttributeValue(ldap.getConnection(), lastKeyEntry,"intsyskey",Integer.toString(sysKey)); + } + + public EntityPath getLastEntityPath() throws ObjectNotFoundException + { + LDAPEntry lastKeyEntry = LDAPLookupUtils.getEntry(ldap.getConnection(),lastKeyPath); + String lastKey = LDAPLookupUtils.getFirstAttributeValue(lastKeyEntry,"intsyskey"); + try { + int sysKey = Integer.parseInt(lastKey); + EntityPath sysPath = new EntityPath(sysKey); + return sysPath; + } catch (InvalidEntityPathException ex) { + throw new ObjectNotFoundException("Invalid syskey. Maybe centre is full."); + } catch (NumberFormatException ex) { + throw new ObjectNotFoundException("Invalid syskey in lastkey."); + } + + } + +} -- cgit v1.2.3