summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/persistency/LDAPClusterStorage.java
diff options
context:
space:
mode:
authorAndrew Branson <andrew.branson@cern.ch>2012-05-30 08:37:45 +0200
committerAndrew Branson <andrew.branson@cern.ch>2012-05-30 08:37:45 +0200
commitb086f57f56bf0eb9dab9cf321a0f69aaaae84347 (patch)
tree8e6e26e8b7eed6abad7a17b093bdbb55c5e6b1ba /src/main/java/com/c2kernel/persistency/LDAPClusterStorage.java
parent22088ae8d2d5ff390518dbe1c4372325ffb3a647 (diff)
Initial Maven Conversion
Diffstat (limited to 'src/main/java/com/c2kernel/persistency/LDAPClusterStorage.java')
-rw-r--r--src/main/java/com/c2kernel/persistency/LDAPClusterStorage.java172
1 files changed, 172 insertions, 0 deletions
diff --git a/src/main/java/com/c2kernel/persistency/LDAPClusterStorage.java b/src/main/java/com/c2kernel/persistency/LDAPClusterStorage.java
new file mode 100644
index 0000000..16ac7a0
--- /dev/null
+++ b/src/main/java/com/c2kernel/persistency/LDAPClusterStorage.java
@@ -0,0 +1,172 @@
+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.EntityPath;
+import com.c2kernel.lookup.InvalidEntityPathException;
+import com.c2kernel.lookup.LDAPPropertyManager;
+import com.c2kernel.process.Gateway;
+import com.c2kernel.property.Property;
+import com.c2kernel.utils.Logger;
+
+public class LDAPClusterStorage extends ClusterStorage {
+ LDAPPropertyManager ldapStore;
+
+ @Override
+ public void open() throws ClusterStorageException {
+ ldapStore = Gateway.getLDAPLookup().getPropManager();
+
+ }
+
+ @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();
+
+ EntityPath thisEntity;
+ try {
+ thisEntity = new EntityPath(sysKey.intValue());
+ } catch (InvalidEntityPathException e) {
+ throw new ClusterStorageException("Invalid Syskey:"+sysKey);
+ }
+
+ String objName = tok.nextToken();
+ C2KLocalObject newObj;
+
+ if (type.equals(PROPERTY)) {
+ try {
+ String value = ldapStore.getPropertyValue(thisEntity, objName);
+ Property newProperty = new Property();
+ newProperty.setName(objName);
+ newProperty.setValue(value);
+ 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();
+
+ EntityPath thisEntity;
+ try {
+ thisEntity = new EntityPath(sysKey.intValue());
+ } catch (InvalidEntityPathException 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();
+
+ EntityPath thisEntity;
+ try {
+ thisEntity = new EntityPath(sysKey.intValue());
+ } catch (InvalidEntityPathException 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
+ {
+ EntityPath thisEntity = new EntityPath(sysKey.intValue());
+ if (type.equals(PROPERTY))
+ return ldapStore.getPropertyNames(thisEntity);
+ else
+ if (type.equals("")) { // root query
+ String[] allClusters = new String[0];
+ ArrayList<String> clusterList = new ArrayList<String>();
+ if (ldapStore.hasProperties(thisEntity))
+ clusterList.add(PROPERTY);
+ allClusters = clusterList.toArray(allClusters);
+ return allClusters;
+ }
+ else
+ throw new ClusterStorageException("Cluster type "+type+" not supported.");
+ } catch (InvalidEntityPathException e) {
+ throw new ClusterStorageException("Invalid Syskey:"+sysKey);
+ } catch (ObjectNotFoundException e) {
+ throw new ClusterStorageException("Entity "+sysKey+" does not exist");
+ }
+ }
+}