From 0ed2c1124cf1b9e49a2ec1fa0126a8df09f9e758 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Tue, 7 Oct 2014 09:18:11 +0200 Subject: Repackage to org.cristalise --- .../storage/MemoryOnlyClusterStorage.java | 166 +++++++++++++++++++ .../java/org/cristalise/storage/ProxyLoader.java | 160 ++++++++++++++++++ .../org/cristalise/storage/XMLClusterStorage.java | 178 +++++++++++++++++++++ 3 files changed, 504 insertions(+) create mode 100644 src/main/java/org/cristalise/storage/MemoryOnlyClusterStorage.java create mode 100644 src/main/java/org/cristalise/storage/ProxyLoader.java create mode 100644 src/main/java/org/cristalise/storage/XMLClusterStorage.java (limited to 'src/main/java/org/cristalise/storage') diff --git a/src/main/java/org/cristalise/storage/MemoryOnlyClusterStorage.java b/src/main/java/org/cristalise/storage/MemoryOnlyClusterStorage.java new file mode 100644 index 0000000..737a173 --- /dev/null +++ b/src/main/java/org/cristalise/storage/MemoryOnlyClusterStorage.java @@ -0,0 +1,166 @@ +/** + * This file is part of the CRISTAL-iSE kernel. + * Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + * + * http://www.fsf.org/licensing/licenses/lgpl.html + */ +package org.cristalise.storage; + +import java.util.ArrayList; +import java.util.ConcurrentModificationException; +import java.util.HashMap; +import java.util.Map; + +import org.cristalise.kernel.common.PersistencyException; +import org.cristalise.kernel.entity.C2KLocalObject; +import org.cristalise.kernel.lookup.ItemPath; +import org.cristalise.kernel.persistency.ClusterStorage; +import org.cristalise.kernel.process.auth.Authenticator; +import org.cristalise.kernel.utils.Logger; + + +public class MemoryOnlyClusterStorage extends ClusterStorage { + + HashMap> memoryCache = new HashMap>(); + /** + * + */ + public MemoryOnlyClusterStorage() { + memoryCache = new HashMap>(); + } + + @Override + public void open(Authenticator auth) throws PersistencyException { + + } + + @Override + public void close() throws PersistencyException { + } + + @Override + public short queryClusterSupport(String clusterType) { + return ClusterStorage.READWRITE; + } + + @Override + public String getName() { + return "Memory Cache"; + } + + @Override + public String getId() { + return "Memory Cache"; + } + + @Override + public C2KLocalObject get(ItemPath thisItem, String path) + throws PersistencyException { + Map sysKeyMemCache = memoryCache.get(thisItem); + if (sysKeyMemCache != null) + return sysKeyMemCache.get(path); + return null; + } + + @Override + public void put(ItemPath thisItem, C2KLocalObject obj) + throws PersistencyException { + + // create item cache if not present + Map sysKeyMemCache; + synchronized (memoryCache) { + if (memoryCache.containsKey(thisItem)) + sysKeyMemCache = memoryCache.get(thisItem); + else { + sysKeyMemCache = new HashMap(); + memoryCache.put(thisItem, sysKeyMemCache); + } + } + + // store object in the cache + String path = ClusterStorage.getPath(obj); + synchronized(sysKeyMemCache) { + sysKeyMemCache.put(path, obj); + } + + } + + @Override + public void delete(ItemPath thisItem, String path) + throws PersistencyException { + Map sysKeyMemCache = memoryCache.get(thisItem); + if (sysKeyMemCache != null) { + synchronized (sysKeyMemCache) { + if (sysKeyMemCache.containsKey(path)) { + sysKeyMemCache.remove(path); + if (sysKeyMemCache.isEmpty()) { + synchronized (memoryCache) { + memoryCache.remove(thisItem); + } + } + } + } + } + } + + @Override + public String[] getClusterContents(ItemPath thisItem, String path) + throws PersistencyException { + Map sysKeyMemCache = memoryCache.get(thisItem); + ArrayList result = new ArrayList(); + if (sysKeyMemCache != null) { + while (path.endsWith("/")) + path = path.substring(0,path.length()-1); + path = path+"/"; + for (String thisPath : sysKeyMemCache.keySet()) { + if (thisPath.startsWith(path)) { + String end = thisPath.substring(path.length()); + int slash = end.indexOf('/'); + String suffix = slash>-1?end.substring(0, slash):end; + if (!result.contains(suffix)) result.add(suffix); + } + } + } + return result.toArray(new String[result.size()]); + } + + public void dumpContents(ItemPath thisItem) { + synchronized(memoryCache) { + Logger.msg(0, "Cached Objects of Entity "+thisItem); + Map sysKeyMemCache = memoryCache.get(thisItem); + if (sysKeyMemCache == null) { + Logger.msg(0, "No cache found"); + return; + } + try { + synchronized(sysKeyMemCache) { + for (Object name : sysKeyMemCache.keySet()) { + String path = (String) name; + try { + Logger.msg(0, " Path "+path+": "+sysKeyMemCache.get(path).getClass().getName()); + } catch (NullPointerException e) { + Logger.msg(0, " Path "+path+": reaped"); + } + } + } + } catch (ConcurrentModificationException ex) { + Logger.msg(0, "Cache modified - aborting"); + } + } + Logger.msg(0, "Total number of cached entities: "+memoryCache.size()); + } +} diff --git a/src/main/java/org/cristalise/storage/ProxyLoader.java b/src/main/java/org/cristalise/storage/ProxyLoader.java new file mode 100644 index 0000000..8922e35 --- /dev/null +++ b/src/main/java/org/cristalise/storage/ProxyLoader.java @@ -0,0 +1,160 @@ +/** + * This file is part of the CRISTAL-iSE kernel. + * Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + * + * http://www.fsf.org/licensing/licenses/lgpl.html + */ +package org.cristalise.storage; +import java.util.HashMap; +import java.util.StringTokenizer; + +import org.cristalise.kernel.common.ObjectNotFoundException; +import org.cristalise.kernel.common.PersistencyException; +import org.cristalise.kernel.entity.AgentHelper; +import org.cristalise.kernel.entity.C2KLocalObject; +import org.cristalise.kernel.entity.Item; +import org.cristalise.kernel.entity.ItemHelper; +import org.cristalise.kernel.lookup.ItemPath; +import org.cristalise.kernel.lookup.Lookup; +import org.cristalise.kernel.persistency.ClusterStorage; +import org.cristalise.kernel.persistency.outcome.Outcome; +import org.cristalise.kernel.process.Gateway; +import org.cristalise.kernel.process.auth.Authenticator; +import org.cristalise.kernel.utils.Logger; + + +/** Used by proxies to load clusters by queryData from the Entity. +* Last client storage - only used if not cached elsewhere +*/ + +public class ProxyLoader extends ClusterStorage { + HashMap entities = new HashMap(); + Lookup lookup; + + @Override + public void open(Authenticator auth) throws PersistencyException { + lookup = Gateway.getLookup(); + } + + @Override + public void close() throws PersistencyException { + } + // introspection + @Override + public short queryClusterSupport(String clusterType) { + return READ; + } + + @Override + public String getName() { + return "Proxy Cluster Loader"; + } + + @Override + public String getId() { + return "CORBA"; + } + + // retrieve object by path + @Override + public C2KLocalObject get(ItemPath thisItem, String path) throws PersistencyException { + try { + Item thisEntity = getIOR(thisItem); + String type = getClusterType(path); + + // fetch the xml from the item + String queryData = thisEntity.queryData(path); + if (Logger.doLog(6)) Logger.msg(6, "ProxyLoader - "+thisItem+":"+path+" = "+queryData); + + if (queryData != null) { + if (type.equals(OUTCOME)) + return new Outcome(path, queryData); + else + return (C2KLocalObject)Gateway.getMarshaller().unmarshall(queryData); + } + } catch (ObjectNotFoundException e) { + return null; + } catch (Exception e) { + Logger.error(e); + throw new PersistencyException(e.getMessage()); + } + return null; + } + + // store object by path + @Override + public void put(ItemPath thisItem, C2KLocalObject obj) throws PersistencyException { + // not supported + throw new PersistencyException("Cannot write to items through the ProxyLoader"); + } + // delete cluster + @Override + public void delete(ItemPath thisItem, String path) throws PersistencyException { + // not supported + throw new PersistencyException("Cannot write to items through the ProxyLoader"); + } + + /* navigation */ + + // directory listing + @Override + public String[] getClusterContents(ItemPath thisItem, String path) throws PersistencyException { + try { + Item thisEntity = getIOR(thisItem); + String contents = thisEntity.queryData(path+"/all"); + StringTokenizer tok = new StringTokenizer(contents, ","); + String[] result = new String[tok.countTokens()]; + for (int i=0; i paths = FileStringUtility.listDir( filePath, true, false ); + if (paths == null) return result; // dir doesn't exist yet + ArrayList contents = new ArrayList(); + String previous = null; + for (int i=0; i -1) next = next.substring(next.lastIndexOf('/')+1); + contents.add(next); + } + + result = contents.toArray(result); + return result; + } catch (Exception e) { + Logger.error(e); + throw new PersistencyException("XMLClusterStorage.getClusterContents() - Could not get contents of "+path+" from "+itemPath+": "+e.getMessage()); + } + } + + protected String getFilePath(ItemPath itemPath, String path) throws InvalidItemPathException { + if (path.length() == 0 || path.charAt(0) != '/') path = "/"+path; + String filePath = rootDir+itemPath.toString()+path; + Logger.msg(8, "XMLClusterStorage.getFilePath() - "+filePath); + return filePath; + } +} -- cgit v1.2.3