From 428d828ca640d1348979f9982d1c0bc0a489a3b4 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Thu, 12 Dec 2013 14:13:36 +0100 Subject: Properties preserve and respect the PropertyDescription 'isMutable' property. This setting prevents the WriteProperty predefined step from changing the property value when isMutable is false. WriteProperty also requires the selected property to already exist - they should be created either during Item instantiation or using AddC2KObject. LDAPPropertyManager prepends the Property name in its entries with ! if they are non mutable. Various places around the kernel that create properties now set the mutable field. Fixes #150 --- .../java/com/c2kernel/entity/proxy/ItemProxy.java | 6 +- .../predefined/CreateItemFromDescription.java | 4 +- .../instance/predefined/WriteProperty.java | 41 +++++++----- .../predefined/entitycreation/NewAgent.java | 4 +- .../predefined/entitycreation/NewItem.java | 2 +- src/main/java/com/c2kernel/lookup/LDAPLookup.java | 11 +--- .../com/c2kernel/lookup/LDAPPropertyManager.java | 52 ++++++++++----- .../c2kernel/lookup/LegacyLDAPPropertyManager.java | 75 ---------------------- .../c2kernel/persistency/LDAPClusterStorage.java | 5 +- src/main/java/com/c2kernel/process/Bootstrap.java | 16 ++--- .../java/com/c2kernel/process/module/Module.java | 8 +-- src/main/java/com/c2kernel/property/Property.java | 18 ++++-- .../com/c2kernel/property/PropertyDescription.java | 2 +- .../c2kernel/property/PropertyDescriptionList.java | 3 +- 14 files changed, 102 insertions(+), 145 deletions(-) delete mode 100644 src/main/java/com/c2kernel/lookup/LegacyLDAPPropertyManager.java (limited to 'src/main/java/com') diff --git a/src/main/java/com/c2kernel/entity/proxy/ItemProxy.java b/src/main/java/com/c2kernel/entity/proxy/ItemProxy.java index ceea6c3..0e6859d 100644 --- a/src/main/java/com/c2kernel/entity/proxy/ItemProxy.java +++ b/src/main/java/com/c2kernel/entity/proxy/ItemProxy.java @@ -79,7 +79,7 @@ public class ItemProxy extends EntityProxy public void setProperty(AgentProxy agent, String name, String value) throws AccessRightsException, - PersistencyException + PersistencyException, InvalidDataException { String[] params = new String[2]; params[0] = name; @@ -90,9 +90,11 @@ public class ItemProxy extends EntityProxy throw (e); } catch (PersistencyException e) { throw (e); + } catch (InvalidDataException e) { + throw (e); } catch (Exception e) { Logger.error(e); - throw new PersistencyException("Could not store property"); + throw new PersistencyException("Could not store property", ""); } } /************************************************************************** diff --git a/src/main/java/com/c2kernel/lifecycle/instance/predefined/CreateItemFromDescription.java b/src/main/java/com/c2kernel/lifecycle/instance/predefined/CreateItemFromDescription.java index 10e54d1..3197ce5 100644 --- a/src/main/java/com/c2kernel/lifecycle/instance/predefined/CreateItemFromDescription.java +++ b/src/main/java/com/c2kernel/lifecycle/instance/predefined/CreateItemFromDescription.java @@ -128,8 +128,8 @@ public class CreateItemFromDescription extends PredefinedStep prop.setValue(newName); } } - if (!foundName) props.list.add(new Property("Name", newName)); - props.list.add( new Property("Creator", agent.getAgentName())); + if (!foundName) props.list.add(new Property("Name", newName, true)); + props.list.add( new Property("Creator", agent.getAgentName(), false)); /* ITEM CREATION */ diff --git a/src/main/java/com/c2kernel/lifecycle/instance/predefined/WriteProperty.java b/src/main/java/com/c2kernel/lifecycle/instance/predefined/WriteProperty.java index 1eef4f5..abb0e95 100644 --- a/src/main/java/com/c2kernel/lifecycle/instance/predefined/WriteProperty.java +++ b/src/main/java/com/c2kernel/lifecycle/instance/predefined/WriteProperty.java @@ -12,7 +12,10 @@ package com.c2kernel.lifecycle.instance.predefined; import com.c2kernel.common.InvalidDataException; +import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.lookup.AgentPath; +import com.c2kernel.persistency.ClusterStorage; +import com.c2kernel.persistency.ClusterStorageException; import com.c2kernel.process.Gateway; import com.c2kernel.property.Property; import com.c2kernel.utils.Logger; @@ -38,24 +41,30 @@ public class WriteProperty extends PredefinedStep int transitionID, String requestData) throws InvalidDataException { Logger.msg(1, "WriteProperty::request() - Starting."); - String[] params = getDataList(requestData); + if (params.length != 2) - throw new InvalidDataException("WriteProperty::request() - need 2 params - name and value", ""); - try - { - - Logger.msg(5, "WriteProperty::request() - name:" + params[0] +" val:"+params[1]); - - Property newProp = new Property(params[0], params[1]); - Gateway.getStorage().put(itemSysKey, newProp, null ); - } - catch( Exception ex ) - { - Logger.error("WriteProperty::request() - during unmarshall."); - Logger.error(ex); - throw new InvalidDataException(ex.toString(), ""); - } + throw new InvalidDataException("WriteProperty usage: [ name, value ]", ""); + + Logger.msg(5, "WriteProperty::request() - name:" + params[0] +" val:"+params[1]); + + String name = params[0]; + String newValue = params[1]; + + Property prop; + + try { + prop = (Property)Gateway.getStorage().get(itemSysKey, ClusterStorage.PROPERTY+"/"+name, null); + if (!prop.isMutable() && !newValue.equals(prop.getValue())) + throw new InvalidDataException("Property '"+name+"' is not mutable.", ""); + prop.setValue(newValue); + Gateway.getStorage().put(itemSysKey, prop, null); + } catch (ObjectNotFoundException e) { + throw new InvalidDataException("WriteProperty: Property '"+name+"' not found.", ""); + } catch (ClusterStorageException e) { + Logger.error(e); + throw new InvalidDataException("Storage error. See logs.", ""); + } Logger.msg(1, "WriteProperty::request() - DONE."); return requestData; diff --git a/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/NewAgent.java b/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/NewAgent.java index e7301a1..12bbb56 100644 --- a/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/NewAgent.java +++ b/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/NewAgent.java @@ -38,8 +38,8 @@ public class NewAgent extends ModuleImport implements java.io.Serializable { ActiveEntity newAgentEnt = (ActiveEntity)Gateway.getCorbaServer().createEntity(newAgent); Gateway.getLDAPLookup().add(newAgent); // assemble properties - properties.add(new com.c2kernel.property.Property("Name", name)); - properties.add(new com.c2kernel.property.Property("Type", "Agent")); + properties.add(new com.c2kernel.property.Property("Name", name, true)); + properties.add(new com.c2kernel.property.Property("Type", "Agent", false)); try { newAgentEnt.initialise(Gateway.getMarshaller().marshall(new PropertyArrayList(properties))); } catch (Exception ex) { diff --git a/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/NewItem.java b/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/NewItem.java index dacb1a3..d5da008 100644 --- a/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/NewItem.java +++ b/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/NewItem.java @@ -80,7 +80,7 @@ public class NewItem extends ModuleImport { } // set the name property - properties.add(new Property("Name", name)); + properties.add(new Property("Name", name, true)); // init the new item try { diff --git a/src/main/java/com/c2kernel/lookup/LDAPLookup.java b/src/main/java/com/c2kernel/lookup/LDAPLookup.java index 996ca0e..23d30d4 100644 --- a/src/main/java/com/c2kernel/lookup/LDAPLookup.java +++ b/src/main/java/com/c2kernel/lookup/LDAPLookup.java @@ -45,7 +45,7 @@ public class LDAPLookup private LDAPConnection mLDAPConn; private final LDAPProperties mLDAPProps; private final NextKeyManager mNextKeyManager; - private LDAPPropertyManager mPropManager; + private final LDAPPropertyManager mPropManager; private final LDAPRoleManager mRoleManager; @@ -73,14 +73,7 @@ public class LDAPLookup mNextKeyManager = new NextKeyManager(this, "cn=last,"+EntityPath.mTypeRoot); 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"); - mPropManager = new LegacyLDAPPropertyManager(this); - } - else { - Logger.debug(1, "Using Kernel 2.2 LDAP Property Format"); - mPropManager = new LDAPPropertyManager(this); - } + mPropManager = new LDAPPropertyManager(this); mRoleManager = new LDAPRoleManager(this, "cn=agent,"+DomainPath.mTypeRoot, EntityPath.mTypeRoot); } diff --git a/src/main/java/com/c2kernel/lookup/LDAPPropertyManager.java b/src/main/java/com/c2kernel/lookup/LDAPPropertyManager.java index 57ed17d..fcf1ef8 100644 --- a/src/main/java/com/c2kernel/lookup/LDAPPropertyManager.java +++ b/src/main/java/com/c2kernel/lookup/LDAPPropertyManager.java @@ -51,7 +51,9 @@ public class LDAPPropertyManager { LDAPAttribute props = entityEntry.getAttribute("cristalprop"); for (Enumeration e = props.getStringValues(); e.hasMoreElements();) { String thisProp = (String)e.nextElement(); - propbag.add(thisProp.substring(0, thisProp.indexOf(':'))); + String thisName = thisProp.substring(0, thisProp.indexOf(':')); + if (thisName.startsWith("!") && thisName.length()>1) thisName = thisName.substring(1); + propbag.add(thisName); } String[] retArr = new String[props.size()]; @@ -61,12 +63,12 @@ public class LDAPPropertyManager { /** * @param thisEntity - EntityPath of the subject entity * @param propName - the name of the property to retrieve - * @return String the property value + * @return The Property object * @throws ObjectNotFoundException */ - public String getPropertyValue(EntityPath thisEntity, String name) throws ObjectNotFoundException { + public Property getProperty(EntityPath thisEntity, String name) throws ObjectNotFoundException { LDAPEntry entityEntry = LDAPLookupUtils.getEntry(ldap.getConnection(), thisEntity.getFullDN()); - return getPropertyAttr(entityEntry, name); + return getProperty(entityEntry, name); } /** @@ -77,9 +79,13 @@ public class LDAPPropertyManager { */ public void deleteProperty(EntityPath thisEntity, String name) throws ObjectNotFoundException, ObjectCannotBeUpdated { LDAPEntry entityEntry = LDAPLookupUtils.getEntry(ldap.getConnection(), thisEntity.getFullDN()); - String propVal = getPropertyAttr(entityEntry, name); + Property prop = getProperty(entityEntry, name); Logger.msg(6, "LDAPLookupUtils.deleteProperty("+name+") - Deleting property"); - LDAPLookupUtils.removeAttributeValue(ldap.getConnection(), entityEntry, "cristalprop", name+":"+propVal); + LDAPLookupUtils.removeAttributeValue(ldap.getConnection(), entityEntry, "cristalprop", getPropertyAttrValue(prop)); + } + + private static String getPropertyAttrValue(Property prop) { + return (prop.isMutable()?"":"!")+prop.getName()+":"+prop.getValue(); } /** @@ -91,28 +97,42 @@ public class LDAPPropertyManager { public void setProperty(EntityPath thisEntity, Property prop) throws ObjectNotFoundException, ObjectCannotBeUpdated { LDAPEntry entityEntry = LDAPLookupUtils.getEntry(ldap.getConnection(), thisEntity.getFullDN()); try { - String propVal = getPropertyAttr(entityEntry, prop.getName()); - Logger.msg(6, "LDAPLookupUtils.setProperty("+prop.getName()+") - Removing old value '"+propVal+"'"); - LDAPLookupUtils.removeAttributeValue(ldap.getConnection(), entityEntry, "cristalprop", prop.getName()+":"+propVal); + Property oldProp = getProperty(entityEntry, prop.getName()); + Logger.msg(6, "LDAPLookupUtils.setProperty("+prop.getName()+") - Removing old value '"+oldProp.getValue()+"'"); + LDAPLookupUtils.removeAttributeValue(ldap.getConnection(), entityEntry, "cristalprop", getPropertyAttrValue(oldProp)); } catch (ObjectNotFoundException ex) { Logger.msg(6, "LDAPLookupUtils.setProperty("+prop.getName()+") - creating new property."); } Logger.msg(6, "LDAPLookupUtils.setProperty("+prop.getName()+") - setting to '"+prop.getValue()+"'"); - LDAPLookupUtils.addAttributeValue(ldap.getConnection(), entityEntry, "cristalprop", prop.getName()+":"+prop.getValue()); + LDAPLookupUtils.addAttributeValue(ldap.getConnection(), entityEntry, "cristalprop", getPropertyAttrValue(prop)); } - private static String getPropertyAttr(LDAPEntry myEntry, String propName) throws ObjectNotFoundException { + public static Property getProperty(LDAPEntry myEntry, String propName) throws ObjectNotFoundException { // delete existing props LDAPAttribute props = myEntry.getAttribute("cristalprop"); if (props == null) throw new ObjectNotFoundException("Property "+propName+" does not exist", ""); String propPrefix = propName+":"; - for (Enumeration e = props.getStringValues(); e.hasMoreElements();) { - String val = (String)e.nextElement(); - if (val.toLowerCase().startsWith(propPrefix.toLowerCase())) - return val.substring(propPrefix.length()); + String roPropPrefix = "!"+propPrefix; + String val = null, name = null; boolean mutable = false; + for (Enumeration e = props.getStringValues(); name==null && e.hasMoreElements();) { + String attrVal = (String)e.nextElement(); + if (attrVal.toLowerCase().startsWith(propPrefix.toLowerCase())) { + name = attrVal.substring(0, propPrefix.length()-1); + val = attrVal.substring(propPrefix.length()); + mutable = true; break; + } + + if (attrVal.toLowerCase().startsWith(roPropPrefix.toLowerCase())) { + name = attrVal.substring(1, roPropPrefix.length()-1); + val = attrVal.substring(roPropPrefix.length()); + mutable = false; break; + } } - throw new ObjectNotFoundException("Property "+propName+" does not exist", ""); + if (name == null) + throw new ObjectNotFoundException("Property "+propName+" does not exist", ""); + Logger.msg(6, "Loaded "+(mutable?"":"Non-")+"Mutable Property: "+name+"="+val); + return new Property(name, val, mutable); } } diff --git a/src/main/java/com/c2kernel/lookup/LegacyLDAPPropertyManager.java b/src/main/java/com/c2kernel/lookup/LegacyLDAPPropertyManager.java deleted file mode 100644 index 638c694..0000000 --- a/src/main/java/com/c2kernel/lookup/LegacyLDAPPropertyManager.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.c2kernel.lookup; - -import com.c2kernel.common.ObjectCannotBeUpdated; -import com.c2kernel.common.ObjectNotFoundException; -import com.c2kernel.property.Property; -import com.c2kernel.utils.Logger; -import com.novell.ldap.LDAPAttribute; -import com.novell.ldap.LDAPAttributeSet; -import com.novell.ldap.LDAPDN; -import com.novell.ldap.LDAPEntry; -import com.novell.ldap.LDAPException; - -public class LegacyLDAPPropertyManager extends LDAPPropertyManager { - - public LegacyLDAPPropertyManager(LDAPLookup ldap) { - super(ldap); - } - - @Override - public void deleteProperty(EntityPath thisEntity, String name) throws ObjectNotFoundException, ObjectCannotBeUpdated { - try { - LDAPLookupUtils.delete(ldap.getConnection(), "cn="+name+","+thisEntity.getFullDN()); - } catch (LDAPException ex) { - Logger.error("Error deleting prop "+name+" from "+thisEntity.getSysKey()); - Logger.error(ex); - } - } - - @Override - public String[] getPropertyNames(EntityPath thisEntity) throws ObjectNotFoundException { - String props[]= LDAPLookupUtils.getChildrenDNs(ldap.getConnection(), thisEntity.getFullDN(), "objectclass=cristalproperty"); - String names[] = new String[props.length]; - for (int i=0; i