Author: bdekruijff at gmail.com
Date: Thu Jan 13 16:09:30 2011
New Revision: 623

Log:
AMDATU-207 refactorred tenantstore-fs to use fsstorage lib

Added:
   
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantEntity.java
   
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantStorage.java
Removed:
   
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantIdList.java
Modified:
   trunk/amdatu-core/tenantstore-fs/pom.xml
   
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantStore.java
   
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProvider.java
   
trunk/amdatu-core/tenantstore-fs/src/test/java/org/amdatu/core/tenantstore/fs/osgi/FSTenantStorageProviderActivatorTest.java

Modified: trunk/amdatu-core/tenantstore-fs/pom.xml
==============================================================================
--- trunk/amdatu-core/tenantstore-fs/pom.xml    (original)
+++ trunk/amdatu-core/tenantstore-fs/pom.xml    Thu Jan 13 16:09:30 2011
@@ -23,7 +23,7 @@
     </dependency>
     <dependency>
       <groupId>org.amdatu.libraries</groupId>
-      <artifactId>utilities</artifactId>
+      <artifactId>fsstorage</artifactId>
       <version>${platform.version}</version>
       <scope>compile</scope>
     </dependency>    

Added: 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantEntity.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantEntity.java
  Thu Jan 13 16:09:30 2011
@@ -0,0 +1,37 @@
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.core.tenantstore.fs.internal;
+
+import org.amdatu.core.tenant.TenantEntity;
+import org.amdatu.libraries.fsstorage.FSStoreEntity;
+
+public class FSTenantEntity implements FSStoreEntity {
+
+    private final TenantEntity m_tenantEntity;
+
+    public FSTenantEntity(TenantEntity tenantEntity) {
+        m_tenantEntity = tenantEntity;
+    }
+
+    public String getId() {
+        return m_tenantEntity.getId();
+    }
+
+    public TenantEntity getTenantEntity() {
+        return m_tenantEntity;
+    }
+}

Added: 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantStorage.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantStorage.java
 Thu Jan 13 16:09:30 2011
@@ -0,0 +1,39 @@
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.core.tenantstore.fs.internal;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.amdatu.libraries.fsstorage.FSStorageBase;
+
+
+public class FSTenantStorage extends FSStorageBase<FSTenantStore, 
FSTenantEntity> {
+
+    private static final String ENTITYLIST_FILENAME = "tenantIdList.ser";
+    private static final String STORAGEFILE_PREFIX = "t_";
+    private static final String STORAGEFILE_POSTFIX = ".ser";
+
+    public FSTenantStorage(File dataDirectory) throws IOException {
+        super(dataDirectory, ENTITYLIST_FILENAME, STORAGEFILE_PREFIX, 
STORAGEFILE_POSTFIX);
+    }
+
+    @Override
+    protected FSTenantStore newFSStore(File file) throws IOException {
+        return new FSTenantStore(file);
+    }
+}
\ No newline at end of file

Modified: 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantStore.java
==============================================================================
--- 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantStore.java
   (original)
+++ 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantStore.java
   Thu Jan 13 16:09:30 2011
@@ -17,113 +17,37 @@
 package org.amdatu.core.tenantstore.fs.internal;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.UnsupportedEncodingException;
-import java.util.HashMap;
 import java.util.Map;
 
 import org.amdatu.core.tenant.TenantEntity;
-import org.amdatu.core.tenant.TenantStorageException;
+import org.amdatu.libraries.fsstorage.FSStoreBase;
 import org.amdatu.libraries.utilities.FSUtil;
 
 /**
  * Implementation of a persistent tenant store (containing 0 or more tenants) 
on disk.
  */
-public final class FSTenantStore {
+public final class FSTenantStore extends FSStoreBase<FSTenantEntity> {
 
-    private final File m_file;
-    private final Map<String, TenantEntity> m_entities;
-
-    public FSTenantStore(File file) throws TenantStorageException {
-        m_file = file;
-        m_entities = new HashMap<String, TenantEntity>();
-        try {
-            readEntities();
-        }
-        catch (IOException e) {
-            throw new TenantStorageException(e);
-        }
-    }
-
-    public TenantEntity addEntity(final TenantEntity entity) {
-        return m_entities.put(entity.getId(), entity);
-    }
-
-    public TenantEntity removeEntity(final String entityId) {
-        return m_entities.remove(entityId);
-    }
-
-    public TenantEntity getEntity(final String entityId) {
-        return m_entities.get(entityId);
+    public FSTenantStore(File file) throws IOException {
+        super(file);
     }
 
-    public void save() throws TenantStorageException {
-        try {
-            writeEntities();
-        }
-        catch (IOException e) {
-            throw new TenantStorageException(e);
-        }
-    }
-
-    private void readEntities() throws IOException {
-        if (!m_file.exists()) {
-            return;
-        }
-        m_entities.clear();
-        FileInputStream fis = null;
-        ObjectInputStream ois = null;
-        try {
-            fis = new FileInputStream(m_file);
-            ois = new ObjectInputStream(fis);
-            final int numberOfTenants = ois.readInt();
-            for (int i = 0; i < numberOfTenants; i++) {
-                final TenantEntity tenantEntity = readEntity(ois);
-                m_entities.put(tenantEntity.getId(), tenantEntity);
-            }
-        }
-        finally {
-            FSUtil.closeInputStreamsSafely(ois, fis);
-        }
-    }
-
-    private TenantEntity readEntity(ObjectInputStream ois) throws IOException,
+    protected FSTenantEntity readEntity(ObjectInputStream ois) throws 
IOException,
         UnsupportedEncodingException {
         final String id = FSUtil.readString(ois);
         final String name = FSUtil.readString(ois);
         final Map<String, String> properties = FSUtil.readProperties(ois);
-        return new TenantEntity(id, name, properties);
-    }
-
-    private void writeEntities() throws IOException {
-        if ((m_entities == null || m_entities.size() == 0) && m_file.exists()) 
{
-            m_file.delete();
-            return;
-        }
-        FileOutputStream fos = null;
-        ObjectOutputStream oos = null;
-        try {
-            fos = new FileOutputStream(m_file);
-            oos = new ObjectOutputStream(fos);
-            oos.writeInt(m_entities.size());
-            for (TenantEntity entity : m_entities.values()) {
-                writeEntity(oos, entity);
-            }
-            oos.flush();
-        }
-        finally {
-            FSUtil.closeOutputStreamsSafely(oos, fos);
-        }
+        return new FSTenantEntity(new TenantEntity(id, name, properties));
     }
 
-    private void writeEntity(ObjectOutputStream oos, TenantEntity entity) 
throws UnsupportedEncodingException,
+    protected void writeEntity(ObjectOutputStream oos, FSTenantEntity entity) 
throws UnsupportedEncodingException,
         IOException {
-        FSUtil.writeString(oos, entity.getId());
-        FSUtil.writeString(oos, entity.getName());
-        FSUtil.writeProperties(oos, entity.getProperties());
+        FSUtil.writeString(oos, entity.getTenantEntity().getId());
+        FSUtil.writeString(oos, entity.getTenantEntity().getName());
+        FSUtil.writeProperties(oos, entity.getTenantEntity().getProperties());
     }
 }

Modified: 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProvider.java
==============================================================================
--- 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProvider.java
  (original)
+++ 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProvider.java
  Thu Jan 13 16:09:30 2011
@@ -17,6 +17,7 @@
 package org.amdatu.core.tenantstore.fs.service;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.Dictionary;
 import java.util.LinkedList;
 import java.util.List;
@@ -24,84 +25,38 @@
 import org.amdatu.core.tenant.TenantEntity;
 import org.amdatu.core.tenant.TenantStorageException;
 import org.amdatu.core.tenant.TenantStorageProvider;
-import org.amdatu.core.tenantstore.fs.internal.FSTenantIdList;
-import org.amdatu.core.tenantstore.fs.internal.FSTenantStore;
+import org.amdatu.core.tenantstore.fs.internal.FSTenantEntity;
+import org.amdatu.core.tenantstore.fs.internal.FSTenantStorage;
 import org.osgi.service.cm.ConfigurationException;
 import org.osgi.service.log.LogService;
 
 /**
  * Filesystem backed implementation of the <code>TenantStorageProvider</code> 
service interface.
  */
-public class FSTenantStorageProvider implements TenantStorageProvider {
-    // File naming constants
-    private static final String ENTITYLIST_FILENAME = "tenantIdList.ser";
-    private static final String STORAGEFILE_PREFIX = "t_";
-    private static final String STORAGEFILE_POSTFIX = ".ser";
+public class FSTenantStorageProvider implements
+    TenantStorageProvider {
 
-    // DM injected (option)
     private volatile LogService m_logService;
-
-    // Storage directory
-    private File m_dataDirectory;
-
-    // Collaborator
-    private FSTenantIdList m_tenantIdList;
-
-    /*
-     * Constructors
-     */
+    private FSTenantStorage m_storage;
 
     public FSTenantStorageProvider() {
     }
 
-    /*
-     * Accessor methods
-     */
-
-    public synchronized File getDataDirectory() {
-        return m_dataDirectory;
+    public FSTenantStorage getFSStorage() {
+        return m_storage;
     }
 
-    public synchronized void setDataDirectory(File dataDirectory) throws 
TenantStorageException {
-        if (!dataDirectory.isAbsolute()) {
-            File userDirectory = new File(System.getProperty("user.dir"));
-            dataDirectory = new File(userDirectory, dataDirectory.getPath());
-        }
-        if (!((dataDirectory.exists() && dataDirectory.canRead() && 
dataDirectory.canWrite()) || dataDirectory
-            .mkdirs())) {
-            throw new TenantStorageException("Unable to access data directory: 
"
-                + dataDirectory.getAbsolutePath());
-        }
-        m_dataDirectory = dataDirectory;
-        m_tenantIdList = new FSTenantIdList(new File(m_dataDirectory, 
ENTITYLIST_FILENAME));
-
-        if (m_logService != null)
-            m_logService.log(LogService.LOG_DEBUG, "Datadirectory set to: " + 
m_dataDirectory.getAbsolutePath());
-    }
-
-    /*
-     * DM Service lifecycle
-     */
-
     public synchronized void start() throws TenantStorageException {
-        // by contract DM ConfigurationDependency contract dataDirectory has
-        // been set through the updated method.
         if (m_logService != null)
             m_logService.log(LogService.LOG_INFO, "Filesystem Tenant storage 
provider started");
     }
 
     public synchronized void stop() {
-        m_tenantIdList = null;
-
         if (m_logService != null)
             m_logService.log(LogService.LOG_INFO, "Filesystem Tenant storage 
provider stopped");
     }
 
-    /*
-     * DM ManagedService
-     */
-
-    public synchronized void updated(Dictionary dictionary) throws 
ConfigurationException {
+    public synchronized void updated(Dictionary<String, Object> dictionary) 
throws ConfigurationException {
         if (dictionary != null) {
             String dataDirectoryName = (String) dictionary.get(DATA_DIRECTORY);
             if (dataDirectoryName == null || "".equals(dataDirectoryName)) {
@@ -117,49 +72,82 @@
         }
     }
 
+    public void setDataDirectory(File dataDirectory) throws 
TenantStorageException {
+        if (!dataDirectory.isAbsolute()) {
+            File userDirectory = new File(System.getProperty("user.dir"));
+            dataDirectory = new File(userDirectory, dataDirectory.getPath());
+        }
+        if (!((dataDirectory.exists() && dataDirectory.canRead() && 
dataDirectory.canWrite()) || dataDirectory
+            .mkdirs())) {
+            throw new TenantStorageException("Unable to access data directory: 
"
+                + dataDirectory.getAbsolutePath());
+        }
+        try {
+            m_storage = new FSTenantStorage(dataDirectory);
+        }
+        catch (IOException e) {
+            throw new TenantStorageException(e);
+        }
+        if (m_logService != null)
+            m_logService.log(LogService.LOG_DEBUG, "Datadirectory set to: "
+                + m_storage.getDataDirectory().getAbsolutePath());
+    }
+
     /*
      * TenantStorageProvider API
      */
 
-    public synchronized void store(final TenantEntity entity) throws 
TenantStorageException {
-        FSTenantStore tenantStorageFile = getStorageFile(entity.getId());
-        TenantEntity storedEntity = tenantStorageFile.addEntity(entity);
-        if (storedEntity == null) {
-            m_tenantIdList.addTenantId(entity.getId());
+    public void store(final TenantEntity tenantEntity) throws 
TenantStorageException {
+        checkState();
+        try {
+            m_storage.addEntity(new FSTenantEntity(tenantEntity));
+        }
+        catch (IOException e) {
+            throw new TenantStorageException(e);
         }
-        tenantStorageFile.save();
     }
 
-    public synchronized void delete(final TenantEntity entity) throws 
TenantStorageException {
-        FSTenantStore tenantStorageFile = getStorageFile(entity.getId());
-        TenantEntity storedEntity = 
tenantStorageFile.removeEntity(entity.getId());
-        if (storedEntity != null) {
-            m_tenantIdList.removeTenantId(entity.getId());
-            tenantStorageFile.save();
+    public void delete(final TenantEntity entity) throws 
TenantStorageException {
+        checkState();
+        try {
+            m_storage.removeEntity(entity.getId());
+        }
+        catch (IOException e) {
+            throw new TenantStorageException(e);
         }
     }
 
-    public synchronized TenantEntity getById(final String entityId) throws 
TenantStorageException {
-        FSTenantStore tenantStorageFile = getStorageFile(entityId);
-        return tenantStorageFile.getEntity(entityId);
+    public TenantEntity getById(final String entityId) throws 
TenantStorageException {
+        checkState();
+        try {
+            FSTenantEntity storeEntity = m_storage.getEntity(entityId);
+            if (storeEntity != null) {
+                return storeEntity.getTenantEntity();
+            }
+            return null;
+        }
+        catch (IOException e) {
+            throw new TenantStorageException(e);
+        }
     }
 
-    public synchronized List<TenantEntity> getAll() throws 
TenantStorageException {
-        final List<String> entityIdList = m_tenantIdList.getAll();
-        final List<TenantEntity> tenantEntityList = new 
LinkedList<TenantEntity>();
-        for (String tenantEntityId : entityIdList) {
-            tenantEntityList.add(getById(tenantEntityId));
+    public List<TenantEntity> getAll() throws TenantStorageException {
+        checkState();
+        try {
+            List<FSTenantEntity> fsEntityList = m_storage.getAll();
+            List<TenantEntity> tenantEntityList = new 
LinkedList<TenantEntity>();
+            for (FSTenantEntity storeEntity : fsEntityList) {
+                tenantEntityList.add(storeEntity.getTenantEntity());
+            }
+            return tenantEntityList;
+        }
+        catch (IOException e) {
+            throw new TenantStorageException(e);
         }
-        return tenantEntityList;
     }
 
-    private FSTenantStore getStorageFile(final String entityId) throws 
TenantStorageException {
-        final int hash = Math.abs(entityId.hashCode());
-        final String subdirName = "" + (hash % 50);
-        final File subdirFile = new File(m_dataDirectory, subdirName);
-        if (!subdirFile.exists()) {
-            subdirFile.mkdir();
-        }
-        return new FSTenantStore(new File(subdirFile, STORAGEFILE_PREFIX + 
hash + STORAGEFILE_POSTFIX));
+    private void checkState() throws TenantStorageException {
+        if (m_storage == null)
+            throw new TenantStorageException("Storage backend is not 
configured");
     }
 }

Modified: 
trunk/amdatu-core/tenantstore-fs/src/test/java/org/amdatu/core/tenantstore/fs/osgi/FSTenantStorageProviderActivatorTest.java
==============================================================================
--- 
trunk/amdatu-core/tenantstore-fs/src/test/java/org/amdatu/core/tenantstore/fs/osgi/FSTenantStorageProviderActivatorTest.java
        (original)
+++ 
trunk/amdatu-core/tenantstore-fs/src/test/java/org/amdatu/core/tenantstore/fs/osgi/FSTenantStorageProviderActivatorTest.java
        Thu Jan 13 16:09:30 2011
@@ -196,9 +196,9 @@
         provider.store(new TenantEntity("1", "Bram"));
 
         // assert that the correct storage directory was actually used
-        Assert.assertEquals(((FSTenantStorageProvider) 
provider).getDataDirectory().getAbsolutePath(),
+        Assert.assertEquals(((FSTenantStorageProvider) 
provider).getFSStorage().getDataDirectory().getAbsolutePath(),
             absolutebundleStorageDirectory.getAbsolutePath());
-        String[] files = ((FSTenantStorageProvider) 
provider).getDataDirectory().list();
+        String[] files = ((FSTenantStorageProvider) 
provider).getFSStorage().getDataDirectory().list();
         Assert.assertTrue(files.length > 0);
 
         // checkerdiecheck

Reply via email to