From 1faab1fd00f70881d4666d766fd0f033e172ff80 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Fri, 21 Feb 2014 09:59:26 +0100 Subject: ClusterStorage property can now be an already instanciated ArrayList of ClusterStorage instances, or the original comma separated string of class names. --- .../persistency/ClusterStorageManager.java | 63 +++++++++++++++------- 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/c2kernel/persistency/ClusterStorageManager.java b/src/main/java/com/c2kernel/persistency/ClusterStorageManager.java index f7304b1..6ca5502 100644 --- a/src/main/java/com/c2kernel/persistency/ClusterStorageManager.java +++ b/src/main/java/com/c2kernel/persistency/ClusterStorageManager.java @@ -40,34 +40,60 @@ public class ClusterStorageManager { * This property is usually process specific, and so should be in the server/client.conf and not the connect file. */ public ClusterStorageManager() throws ClusterStorageException { - String allClusters = Gateway.getProperty("ClusterStorage"); - if (allClusters == null || allClusters.equals("")) { + Object clusterStorageProp = Gateway.getProperty("ClusterStorage"); + if (clusterStorageProp == null || clusterStorageProp.equals("")) { throw new ClusterStorageException("ClusterStorageManager.init() - no ClusterStorages defined. No persistency!"); } + + ArrayList rootStores; + if (clusterStorageProp instanceof String) + rootStores = instantiateStores((String)clusterStorageProp); + else if (clusterStorageProp instanceof ArrayList) { + ArrayList propStores = (ArrayList)clusterStorageProp; + rootStores = new ArrayList(); + for (Object thisStore : propStores) { + if (thisStore instanceof ClusterStorage) + rootStores.add((ClusterStorage)thisStore); + else + throw new ClusterStorageException("Supplied ClusterStorage "+thisStore.toString()+" was not an instance of ClusterStorage"); + } + } + else { + throw new ClusterStorageException("Unknown class of ClusterStorage property: "+clusterStorageProp.getClass().getName()); + } + + int clusterNo = 0; + for (ClusterStorage newStorage : rootStores) { + try { + newStorage.open(); + } catch (ClusterStorageException ex) { + Logger.error(ex); + throw new ClusterStorageException("ClusterStorageManager.init() - Error initialising storage handler " + newStorage.getClass().getName() + + ": " + ex.getMessage()); + } + Logger.msg(5, "ClusterStorageManager.init() - Cluster storage " + newStorage.getClass().getName() + + " initialised successfully."); + allStores.put(newStorage.getId(), newStorage); + clusterPriority[clusterNo++] = newStorage.getId(); + } + clusterReaders.put(ClusterStorage.ROOT, rootStores); // all storages are queried for clusters at the root level + + } + + public ArrayList instantiateStores(String allClusters) throws ClusterStorageException { + ArrayList rootStores = new ArrayList(); StringTokenizer tok = new StringTokenizer(allClusters, ","); clusterPriority = new String[tok.countTokens()]; - int clusterNo = 0; - ArrayList rootStores = new ArrayList(); + while (tok.hasMoreTokens()) { ClusterStorage newStorage = null; String newStorageClass = tok.nextToken(); try { try { - newStorage = (ClusterStorage)(Gateway.getResource().getClassForName(newStorageClass).newInstance()); + newStorage = (ClusterStorage)(Class.forName(newStorageClass).newInstance()); } catch (ClassNotFoundException ex2) { - newStorage = (ClusterStorage)(Gateway.getResource().getClassForName("com.c2kernel.persistency."+newStorageClass).newInstance()); + newStorage = (ClusterStorage)(Class.forName("com.c2kernel.persistency."+newStorageClass).newInstance()); } - newStorage.open(); - Logger.msg(5, "ClusterStorageManager.init() - Cluster storage " + newStorageClass + - " initialised successfully."); - allStores.put(newStorage.getId(), newStorage); - rootStores.add(newStorage); - clusterPriority[clusterNo++] = newStorage.getId(); - - } catch (ClusterStorageException ex) { - Logger.error(ex); - throw new ClusterStorageException("ClusterStorageManager.init() - Error initialising storage handler " + newStorageClass + - ": " + ex.getMessage()); } catch (ClassNotFoundException ex) { throw new ClusterStorageException("ClusterStorageManager.init() - The cluster storage handler class " + newStorageClass + " could not be found."); @@ -78,8 +104,9 @@ public class ClusterStorageManager { throw new ClusterStorageException("ClusterStorageManager.init() - The cluster storage handler class " + newStorageClass + " was not allowed to be instantiated."); } + rootStores.add(newStorage); } - clusterReaders.put(ClusterStorage.ROOT, rootStores); // all storages are queried for clusters at the root level + return rootStores; } public void close() { -- cgit v1.2.3