Author: rdonkin
Date: Sun Jan 27 03:18:06 2008
New Revision: 615576

URL: http://svn.apache.org/viewvc?rev=615576&view=rev
Log:
Very basic implementation of user meta-data repository

Added:
    james/server/trunk/basic-user-function/src/main/java/org/apache/james/user/
    
james/server/trunk/basic-user-function/src/main/java/org/apache/james/user/impl/
    
james/server/trunk/basic-user-function/src/main/java/org/apache/james/user/impl/file/
    
james/server/trunk/basic-user-function/src/main/java/org/apache/james/user/impl/file/FileUserMetaDataRepository.java
    james/server/trunk/basic-user-function/src/test/java/org/
    james/server/trunk/basic-user-function/src/test/java/org/apache/
    james/server/trunk/basic-user-function/src/test/java/org/apache/james/
    james/server/trunk/basic-user-function/src/test/java/org/apache/james/user/
    
james/server/trunk/basic-user-function/src/test/java/org/apache/james/user/impl/
    
james/server/trunk/basic-user-function/src/test/java/org/apache/james/user/impl/file/
    
james/server/trunk/basic-user-function/src/test/java/org/apache/james/user/impl/file/FileUserMetaDataRepositoryTest.java
    
james/server/trunk/phoenix-deployment/src/java/org/apache/james/phoenix/user/
    
james/server/trunk/phoenix-deployment/src/java/org/apache/james/phoenix/user/FileUserMetaDataService.java
    
james/server/trunk/phoenix-deployment/src/java/org/apache/james/phoenix/user/FileUserMetaDataService.xinfo

Added: 
james/server/trunk/basic-user-function/src/main/java/org/apache/james/user/impl/file/FileUserMetaDataRepository.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/basic-user-function/src/main/java/org/apache/james/user/impl/file/FileUserMetaDataRepository.java?rev=615576&view=auto
==============================================================================
--- 
james/server/trunk/basic-user-function/src/main/java/org/apache/james/user/impl/file/FileUserMetaDataRepository.java
 (added)
+++ 
james/server/trunk/basic-user-function/src/main/java/org/apache/james/user/impl/file/FileUserMetaDataRepository.java
 Sun Jan 27 03:18:06 2008
@@ -0,0 +1,170 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.user.impl.file;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+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.Serializable;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.james.api.user.UserMetaDataRespository;
+import org.apache.james.api.user.UserRepositoryException;
+
+/**
+ * Stores user meta-data in the file system.
+ */
+public class FileUserMetaDataRepository implements UserMetaDataRespository {
+    
+    private static final String SERIALIZED_FILE_TYPE_NAME = ".ser";
+
+    /** Characters that may safely be used to create names */
+    private final static char[] SAFE_CHARS = {
+        'a','b','c','d','e','f','g','h','i','j',
+        'k','l','m','n','o','p','q','r','s','t',
+        'u','v','w','x','y','z','0','1','2','3',
+        '4','5','6','7','8','9'
+    };
+    
+    private final String baseDirectory;
+    
+    public FileUserMetaDataRepository(final String baseDirectory) {
+        super();
+        this.baseDirectory = baseDirectory;
+    }
+
+    public void clear(String username) throws UserRepositoryException {
+        final File userDir = userDirectory(username);
+        try {
+            FileUtils.deleteDirectory(userDir);
+        } catch (IOException e) {
+            throw new UserRepositoryException("Cannot delete " + 
userDir.getAbsolutePath(), e);
+        }
+    }
+
+    public Serializable getAttribute(String username, String key)
+            throws UserRepositoryException {
+        final File valueFile = valueFile(username, key);
+        final Serializable result;
+        if (valueFile.exists()) {
+            ObjectInputStream in = null;
+            try {
+                in = new ObjectInputStream(new BufferedInputStream(new 
FileInputStream(valueFile)));
+                result = (Serializable) in.readObject();
+            } catch (IOException e) {
+                throw new UserRepositoryException(e);
+            } catch (ClassNotFoundException e) {
+                throw new UserRepositoryException(e);
+            } finally {
+                IOUtils.closeQuietly(in);
+            }
+            
+        } else {
+            result = null;
+        }
+        return result;
+    }
+
+    public void setAttribute(String username, Serializable value, String key)
+            throws UserRepositoryException {
+
+        final File valueFile = valueFile(username, key);
+        ObjectOutputStream out = null;
+        try {
+            
+            out = new ObjectOutputStream(new BufferedOutputStream(new 
FileOutputStream(valueFile)));
+            out.writeObject(value);
+            
+        } catch (IOException e) {
+            throw new UserRepositoryException(e);
+        } finally {
+            IOUtils.closeQuietly(out);
+        }
+    }
+
+    private File valueFile(String username, String key) throws 
UserRepositoryException {
+        final File userDir = userDirectory(username);
+        
+        final String valueFileName = fileSystemSafeName(key, 
SERIALIZED_FILE_TYPE_NAME);
+        final File valueFile = new File(userDir, valueFileName);
+        return valueFile;
+    }
+
+    private File userDirectory(String username) throws UserRepositoryException 
{
+        final File baseDir = getBaseDirectory();
+        
+        final String userDirectoryName = fileSystemSafeName(username);
+        final File userDir = new File(baseDir, userDirectoryName);
+        if (!userDir.exists()) {
+            if (!userDir.mkdir()) {
+                throw new UserRepositoryException("Cannot create directory: " 
+ userDir.getAbsolutePath());
+            }
+        }
+        return userDir;
+    }
+
+    private File getBaseDirectory() throws UserRepositoryException {
+        final File baseDir = new File(baseDirectory);
+        if (!baseDir.exists()) {
+            if (!baseDir.mkdirs()) {
+                throw new UserRepositoryException("Cannot create directory: " 
+ baseDirectory);
+            }
+        }
+        return baseDir;
+    }
+
+    /**
+     * Maps a value to a file-system safe name.
+     * @param value name, not null
+     * @param suffix optional suffix to be append, possibly null
+     * @return file system safe mapping of the name
+     */
+    private String fileSystemSafeName(String value) {
+        return fileSystemSafeName(value, null);
+    }
+    
+    /**
+     * Maps a value to a file-system safe name.
+     * @param value name, not null
+     * @param suffix optional suffix to be append, possibly null
+     * @return file system safe mapping of the name
+     */
+    private String fileSystemSafeName(String value, String suffix) {
+        final int length = value.length();
+        final StringBuffer buffer = new StringBuffer(length * 10);
+        for (int i=0;i<length;i++) {
+            final int next = value.charAt(i);
+            for (int j=0;j<4;j++) {
+                buffer.append(SAFE_CHARS[(next >> j * 4) % 32]);
+            }
+        }
+        if (suffix != null) {
+            buffer.append(suffix);
+        }
+        final String result = buffer.toString();
+        return result;
+    }
+}

Added: 
james/server/trunk/basic-user-function/src/test/java/org/apache/james/user/impl/file/FileUserMetaDataRepositoryTest.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/basic-user-function/src/test/java/org/apache/james/user/impl/file/FileUserMetaDataRepositoryTest.java?rev=615576&view=auto
==============================================================================
--- 
james/server/trunk/basic-user-function/src/test/java/org/apache/james/user/impl/file/FileUserMetaDataRepositoryTest.java
 (added)
+++ 
james/server/trunk/basic-user-function/src/test/java/org/apache/james/user/impl/file/FileUserMetaDataRepositoryTest.java
 Sun Jan 27 03:18:06 2008
@@ -0,0 +1,113 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.user.impl.file;
+
+import java.io.File;
+
+import org.apache.commons.io.FileUtils;
+
+import junit.framework.TestCase;
+
+public class FileUserMetaDataRepositoryTest extends TestCase {
+
+    private static final String SYMBOLIC_KEY = "\\/><'@~ #][}{()=+.,| !`%$3\" 
exit(0)";
+
+    private static final String KEY = "key";
+
+    private static final String USER = "user";
+    private static final String ANOTHER_USER = "another user";
+
+    private static final String TEST_DIRECTORY = "target/testusermetadata";
+    
+    FileUserMetaDataRepository repository;
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        final File directory = new File(TEST_DIRECTORY);
+        if (directory.exists()) {
+            FileUtils.deleteDirectory(directory);
+        }
+        repository = new FileUserMetaDataRepository(TEST_DIRECTORY);
+    }
+
+    public void testClear() throws Exception {
+        final Long value = new Long(99);
+        repository.setAttribute(USER, value, KEY);
+        assertEquals(value, repository.getAttribute(USER, KEY));
+        final Long anotherValue = new Long(199);
+        repository.setAttribute(ANOTHER_USER, anotherValue, KEY);
+        assertEquals(anotherValue, repository.getAttribute(ANOTHER_USER, KEY));
+        repository.clear(USER);
+        assertNull(repository.getAttribute(USER, KEY));
+        assertEquals(anotherValue, repository.getAttribute(ANOTHER_USER, KEY));
+    }
+
+    public void testSetGetAttribute() throws Exception {
+        final Long value = new Long(99);
+        repository.setAttribute(USER, value, KEY);
+        assertEquals(value, repository.getAttribute(USER, KEY));
+    }
+
+    public void testUnsafeUserNames() throws Exception {
+        final Long valueOne = new Long(99);
+        final String userOne = USER + '\u25B2';
+        repository.setAttribute(userOne, valueOne, KEY);
+        final Long valueTwo = new Long(90);
+        final String userTwo = USER + '\u25B3';
+        repository.setAttribute(userTwo, valueTwo, KEY);
+        final Long valueThree = new Long(80);
+        final String userThree = USER + '\u25B4';
+        repository.setAttribute(userThree, valueThree, KEY);
+        final Long valueFour = new Long(199);
+        final String userFour = USER + '\uA48A';
+        repository.setAttribute(userFour, valueFour, KEY);
+        final Long valueFive = new Long(190);
+        final String userFive = USER + '\uA485';
+        repository.setAttribute(userFive, valueFive, KEY);
+        final Long valueSix = new Long(180);
+        final String userSix = USER + '\uA486';
+        repository.setAttribute(userSix, valueSix, KEY);
+        final Long valueSeven = new Long(290);
+        final String userSeven = USER + '\uFFFE';
+        repository.setAttribute(userSeven, valueSeven, KEY);
+        final Long valueEight = new Long(280);
+        final String userEight = USER + '\uFFFF';
+        repository.setAttribute(userEight, valueEight, KEY);
+        
+        assertEquals(valueOne, repository.getAttribute(userOne, KEY));
+        assertEquals(valueTwo, repository.getAttribute(userTwo, KEY));
+        assertEquals(valueThree, repository.getAttribute(userThree, KEY));
+        assertEquals(valueFour, repository.getAttribute(userFour, KEY));
+        assertEquals(valueFive, repository.getAttribute(userFive, KEY));
+        assertEquals(valueSix, repository.getAttribute(userSix, KEY));
+        assertEquals(valueSeven, repository.getAttribute(userSeven, KEY));
+        assertEquals(valueEight, repository.getAttribute(userEight, KEY));
+    }
+    
+    public void testUnsafeKeyNames() throws Exception {
+        final Long value = new Long(99);
+        repository.setAttribute(USER, value, SYMBOLIC_KEY);
+        assertEquals(value, repository.getAttribute(USER, SYMBOLIC_KEY));
+    }
+    
+    public void testGetWithoutSet() throws Exception {
+        assertNull(repository.getAttribute(USER, KEY));
+    }
+}

Added: 
james/server/trunk/phoenix-deployment/src/java/org/apache/james/phoenix/user/FileUserMetaDataService.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/phoenix-deployment/src/java/org/apache/james/phoenix/user/FileUserMetaDataService.java?rev=615576&view=auto
==============================================================================
--- 
james/server/trunk/phoenix-deployment/src/java/org/apache/james/phoenix/user/FileUserMetaDataService.java
 (added)
+++ 
james/server/trunk/phoenix-deployment/src/java/org/apache/james/phoenix/user/FileUserMetaDataService.java
 Sun Jan 27 03:18:06 2008
@@ -0,0 +1,61 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.phoenix.user;
+
+import java.io.Serializable;
+
+import org.apache.avalon.framework.activity.Initializable;
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.james.api.user.UserMetaDataRespository;
+import org.apache.james.api.user.UserRepositoryException;
+import org.apache.james.user.impl.file.FileUserMetaDataRepository;
+
+public class FileUserMetaDataService extends AbstractLogEnabled 
+        implements UserMetaDataRespository, Configurable, Initializable {
+
+    private UserMetaDataRespository repository;
+    private String baseDirectory;
+    
+    public void clear(String username) throws UserRepositoryException {
+        repository.clear(username);
+    }
+
+    public Serializable getAttribute(String username, String key)
+            throws UserRepositoryException {
+        return repository.getAttribute(username, key);
+    }
+
+    public void setAttribute(String username, Serializable value, String key)
+            throws UserRepositoryException {
+        repository.setAttribute(username, value, key);
+    }
+
+    public void configure(final Configuration configuration) throws 
ConfigurationException {
+        baseDirectory = configuration.getAttribute("baseDir");
+    }
+
+    public void initialize() throws Exception {
+        repository = new FileUserMetaDataRepository(baseDirectory);
+    }
+
+}

Added: 
james/server/trunk/phoenix-deployment/src/java/org/apache/james/phoenix/user/FileUserMetaDataService.xinfo
URL: 
http://svn.apache.org/viewvc/james/server/trunk/phoenix-deployment/src/java/org/apache/james/phoenix/user/FileUserMetaDataService.xinfo?rev=615576&view=auto
==============================================================================
--- 
james/server/trunk/phoenix-deployment/src/java/org/apache/james/phoenix/user/FileUserMetaDataService.xinfo
 (added)
+++ 
james/server/trunk/phoenix-deployment/src/java/org/apache/james/phoenix/user/FileUserMetaDataService.xinfo
 Sun Jan 27 03:18:06 2008
@@ -0,0 +1,33 @@
+<?xml version="1.0"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one   
+  or more contributor license agreements.  See the NOTICE file 
+  distributed with this work for additional information        
+  regarding copyright ownership.  The ASF licenses this file   
+  to you under the Apache License, Version 2.0 (the            
+  "License"); you may not use this file except in compliance   
+  with the License.  You may obtain a copy of the License at   
+                                                               
+    http://www.apache.org/licenses/LICENSE-2.0                 
+                                                               
+  Unless required by applicable law or agreed to in writing,   
+  software distributed under the License is distributed on an  
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       
+  KIND, either express or implied.  See the License for the    
+  specific language governing permissions and limitations      
+  under the License.                                           
+ -->
+<blockinfo>
+
+  <!-- section to describe block -->
+  <block>
+    <version>1.0</version>
+  </block>
+
+  <!-- services that are offered by this block -->
+  <services>
+    <service name='org.apache.james.api.user.UserMetaDataRespository' 
version="1.0"/>
+  </service>
+
+  <dependencies/>  
+</blockinfo>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to