Commits

Anonymous committed dbbaebf

This commit was manufactured by cvs2svn to create branch 'os'.

git-svn-id: http://svn.opensymphony.com/svn/propertyset/branches/os@24bf3cbcdd-1c1a-0410-9a68-d6f521e3fa7b

Comments (0)

Files changed (2)

src/java/com/opensymphony/module/propertyset/PropertySet.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset;
+
+
+/* ====================================================================
+ * The OpenSymphony Software License, Version 1.1
+ *
+ * (this license is derived and fully compatible with the Apache Software
+ * License - see http://www.apache.org/LICENSE.txt)
+ *
+ * Copyright (c) 2001 The OpenSymphony Group. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ *    if any, must include the following acknowledgment:
+ *       "This product includes software developed by the
+ *        OpenSymphony Group (http://www.opensymphony.com/)."
+ *    Alternately, this acknowledgment may appear in the software itself,
+ *    if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "OpenSymphony" and "The OpenSymphony Group"
+ *    must not be used to endorse or promote products derived from this
+ *    software without prior written permission. For written
+ *    permission, please contact license@opensymphony.com .
+ *
+ * 5. Products derived from this software may not be called "OpenSymphony"
+ *    or "OSCore", nor may "OpenSymphony" or "OSCore" appear in their
+ *    name, without prior written permission of the OpenSymphony Group.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ */
+import org.w3c.dom.Document;
+
+import java.util.*;
+
+
+/**
+ * A <code>PropertySet</code> is designed to be associated with other entities
+ * in the system for storing key/value property pairs.
+ *
+ * <p>A key can only contain one value and a key is unique across all types. If
+ * a property is set using the same key and an already existing property of the
+ * SAME type, the new value will overwrite the old. However, if a property of
+ * DIFFERENT type attempts to overwrite the existing value, a
+ * {@link com.opensymphony.module.propertyset.DuplicatePropertyKeyException}
+ * should be thrown.</p>
+ *
+ * <p>If a property is set of a type that is not allowed, a
+ * {@link com.opensymphony.module.propertyset.IllegalPropertyException}
+ * should be thrown.</p>
+ *
+ * <p>If a property is retrieved that exists but contains a value of different
+ * type, a
+ * {@link com.opensymphony.module.propertyset.InvalidPropertyTypeException}
+ * should be thrown.</p>
+ *
+ * <p>If a property is retrieved that does not exist, null (or the primitive
+ * equivalent) is returned.</p>
+ *
+ * <p>If an Exception is encountered in the actual implementation of the
+ * PropertySet that needs to be rethrown, it should be wrapped in a
+ * {@link com.opensymphony.module.propertyset.PropertyImplementationException}
+ * .</p>
+ *
+ * <p>Some PropertySet implementations may not store along side the data the original
+ * type it was set as. This means that it could be retrieved using a get method of
+ * a different type without throwing an InvalidPropertyTypeException (so long as the
+ * original type can be converted to the requested type.</p>
+ *
+ * <p><b>Typed PropertySet Example</b></p>
+ *
+ * <p><code>
+ * propertySet.setString("something","99");<br>
+ * x = propertySet.getString("something"); // throws InvalidPropertyTypeException
+ * </code></p>
+ *
+ * <p><b>Untyped PropertySet Example</b></p>
+ *
+ * <p><code>
+ * propertySet.setString("something","99");<br>
+ * x = propertySet.getString("something"); // returns 99.
+ * </code></p>
+ *
+ * <p>Typically (unless otherwise stated), an implementation is typed. This can be
+ * checked by calling the {@link #supportsTypes()} method of the implementation.</p>
+ *
+ * <p>Not all PropertySet implementations need to support setter methods (i.e.
+ * they are read only) and not all have to support storage/retrieval of specific
+ * types. The capabilities of the specific implementation can be determined by
+ * calling {@link #supportsType(int)} and {@link #isSettable(String)} .</p>
+ *
+ * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
+ * @version $Revision$
+ */
+public interface PropertySet {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    /** Value-type boolean */
+    int BOOLEAN = 1;
+
+    /** Value-type byte[] */
+    int DATA = 10;
+
+    /** Value-type {@link java.util.Date} */
+    int DATE = 7;
+
+    /** Value-type double */
+    int DOUBLE = 4;
+
+    /** Value-type int */
+    int INT = 2;
+
+    /** Value-type long */
+    int LONG = 3;
+
+    /** Value-type serializable {@link java.lang.Object} */
+    int OBJECT = 8;
+
+    /** Value-type {@link java.util.Properties} */
+    int PROPERTIES = 11;
+
+    /** Value-type {@link java.lang.String} (max length 255) */
+    int STRING = 5;
+
+    /** Value-type text (unlimited length {@link java.lang.String})  */
+    int TEXT = 6;
+
+    /** Value-type XML {@link org.w3c.dom.Document} */
+    int XML = 9;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public void setSchema(PropertySetSchema schema) throws PropertyException;
+
+    public PropertySetSchema getSchema() throws PropertyException;
+
+    void setAsActualType(String key, Object value) throws PropertyException;
+
+    Object getAsActualType(String key) throws PropertyException;
+
+    void setBoolean(String key, boolean value) throws PropertyException;
+
+    boolean getBoolean(String key) throws PropertyException;
+
+    void setData(String key, byte[] value) throws PropertyException;
+
+    byte[] getData(String key) throws PropertyException;
+
+    void setDate(String key, Date value) throws PropertyException;
+
+    Date getDate(String key) throws PropertyException;
+
+    void setDouble(String key, double value) throws PropertyException;
+
+    double getDouble(String key) throws PropertyException;
+
+    void setInt(String key, int value) throws PropertyException;
+
+    int getInt(String key) throws PropertyException;
+
+    /**
+    * List all keys.
+    *
+    * @return Unmodifiable {@link java.util.Collection} of
+    *         {@link java.lang.String}s.
+    */
+    Collection getKeys() throws PropertyException;
+
+    /**
+    * List all keys of certain type.
+    *
+    * @param type Type to list. See static class variables. If null, then
+    *        all types shall be returned.
+    * @return Unmodifiable {@link java.util.Collection} of
+    *         {@link java.lang.String}s.
+    */
+    Collection getKeys(int type) throws PropertyException;
+
+    /**
+    * List all keys starting with supplied prefix.
+    *
+    * @param prefix String that keys must start with. If null, than all
+    *        keys shall be returned.
+    * @return Unmodifiable {@link java.util.Collection} of
+    *         {@link java.lang.String}s.
+    */
+    Collection getKeys(String prefix) throws PropertyException;
+
+    /**
+    * List all keys starting with supplied prefix of certain type. See
+    * statics.
+    *
+    * @param prefix String that keys must start with. If null, than all
+    *        keys shall be returned.
+    * @param type Type to list. See static class variables. If null, then
+    *        all types shall be returned.
+    * @return Unmodifiable {@link java.util.Collection} of
+    *         {@link java.lang.String}s.
+    */
+    Collection getKeys(String prefix, int type) throws PropertyException;
+
+    void setLong(String key, long value) throws PropertyException;
+
+    long getLong(String key) throws PropertyException;
+
+    void setObject(String key, Object value) throws PropertyException;
+
+    Object getObject(String key) throws PropertyException;
+
+    void setProperties(String key, Properties value) throws PropertyException;
+
+    Properties getProperties(String key) throws PropertyException;
+
+    /**
+    * Whether this PropertySet implementation allows values to be set
+    * (as opposed to read-only).
+    */
+    boolean isSettable(String property);
+
+    void setString(String key, String value) throws PropertyException;
+
+    /**
+    * {@link java.lang.String} of maximum 255 chars.
+    */
+    String getString(String key) throws PropertyException;
+
+    void setText(String key, String value) throws PropertyException;
+
+    /**
+    * {@link java.lang.String} of unlimited length.
+    */
+    String getText(String key) throws PropertyException;
+
+    /**
+    * Returns type of value.
+    *
+    * @return Type of value. See static class variables.
+    */
+    int getType(String key) throws PropertyException;
+
+    void setXML(String key, Document value) throws PropertyException;
+
+    Document getXML(String key) throws PropertyException;
+
+    /**
+    * Determine if property exists.
+    */
+    boolean exists(String key) throws PropertyException;
+
+    void init(Map config, Map args);
+
+    /**
+    * Removes property.
+    */
+    void remove(String key) throws PropertyException;
+
+    /**
+    * Whether this PropertySet implementation allows the type specified
+    * to be stored or retrieved.
+    */
+    boolean supportsType(int type);
+
+    /**
+    * Whether this PropertySet implementation supports types when storing
+    * values (i.e. the type of data is stored as well as the actual value).
+    */
+    boolean supportsTypes();
+}

src/java/com/opensymphony/module/propertyset/cached/CachingPropertySet.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset.cached;
+
+import com.opensymphony.module.propertyset.*;
+import com.opensymphony.module.propertyset.memory.SerializablePropertySet;
+
+import com.opensymphony.util.DataUtil;
+
+import org.w3c.dom.Document;
+
+import java.io.Serializable;
+
+import java.util.*;
+
+
+/**
+ * A PropertySet which decorates another PropertySet and caches the results.
+ * <p>
+ *
+ * This is only sensible to use in a situation where your application has exclusive access
+ * to the underlying PropertySet (otherwise it can be dangerous to use).
+ * <p>
+ *
+ * You can also use this property set to bulk load data from the decorated property set
+ * when the PS is created.
+ * <p>
+ *
+ * <b>THINK BEFORE USING THIS - IT COULD HURT YOU ;)</b>
+ * <p>
+ *
+ * <b>Required Args</b>
+ * <ul>
+ *  <li><b>PropertySet</b> - the PropertySet that will be cached</li>
+ * </ul>
+ * <p>
+ *
+ * <b>Optional Args</b>
+ * <ul>
+ *  <li><b>bulkload</b> - Boolean that, when true, causes the cache to be bulk loaded</li>
+ *  <li><b>serializableName</b> - the name that can be used to retrieve a SerializablePropertySet, defaults to "serializable"</li>
+ * </ul>
+ * @author <a href="mailto:mike@atlassian.com">Mike Cannon-Brookes</a>
+ * @version $Revision$
+ */
+public class CachingPropertySet implements PropertySet, Serializable {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    PropertySet decoratedPS;
+    SerializablePropertySet cachePS;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public void setAsActualType(String key, Object value) throws PropertyException {
+        if (value instanceof Boolean) {
+            setBoolean(key, DataUtil.getBoolean((Boolean) value));
+        } else if (value instanceof Integer) {
+            setInt(key, DataUtil.getInt((Integer) value));
+        } else if (value instanceof Long) {
+            setLong(key, DataUtil.getLong((Long) value));
+        } else if (value instanceof Double) {
+            setDouble(key, DataUtil.getDouble((Double) value));
+        } else if (value instanceof String) {
+            setString(key, (String) value);
+        } else if (value instanceof Date) {
+            setDate(key, (Date) value);
+        } else if (value instanceof Document) {
+            setXML(key, (Document) value);
+        } else if (value instanceof byte[]) {
+            setData(key, (byte[]) value);
+        } else if (value instanceof Properties) {
+            setProperties(key, (Properties) value);
+        } else {
+            setObject(key, value);
+        }
+    }
+
+    public Object getAsActualType(String key) throws PropertyException {
+        int type = getType(key);
+        Object value = null;
+
+        switch (type) {
+        case BOOLEAN:
+            value = new Boolean(getBoolean(key));
+
+            break;
+
+        case INT:
+            value = new Integer(getInt(key));
+
+            break;
+
+        case LONG:
+            value = new Long(getLong(key));
+
+            break;
+
+        case DOUBLE:
+            value = new Double(getDouble(key));
+
+            break;
+
+        case STRING:
+            value = getString(key);
+
+            break;
+
+        case DATE:
+            value = getDate(key);
+
+            break;
+
+        case XML:
+            value = getXML(key);
+
+            break;
+
+        case DATA:
+            value = getData(key);
+
+            break;
+
+        case PROPERTIES:
+            value = getProperties(key);
+
+            break;
+
+        case OBJECT:
+            value = getObject(key);
+
+            break;
+        }
+
+        return value;
+    }
+
+    public void setBoolean(String key, boolean value) throws PropertyException {
+        decoratedPS.setBoolean(key, value);
+        cachePS.setBoolean(key, value);
+    }
+
+    public boolean getBoolean(String key) throws PropertyException {
+        if (!cachePS.exists(key)) {
+            cachePS.setBoolean(key, decoratedPS.getBoolean(key));
+        }
+
+        return cachePS.getBoolean(key);
+    }
+
+    public void setData(String key, byte[] value) throws PropertyException {
+        decoratedPS.setData(key, value);
+        cachePS.setData(key, value);
+    }
+
+    public byte[] getData(String key) throws PropertyException {
+        if (!cachePS.exists(key)) {
+            cachePS.setData(key, decoratedPS.getData(key));
+        }
+
+        return cachePS.getData(key);
+    }
+
+    public void setDate(String key, Date value) throws PropertyException {
+        decoratedPS.setDate(key, value);
+        cachePS.setDate(key, value);
+    }
+
+    public Date getDate(String key) throws PropertyException {
+        if (!cachePS.exists(key)) {
+            cachePS.setDate(key, decoratedPS.getDate(key));
+        }
+
+        return cachePS.getDate(key);
+    }
+
+    public void setDouble(String key, double value) throws PropertyException {
+        decoratedPS.setDouble(key, value);
+        cachePS.setDouble(key, value);
+    }
+
+    public double getDouble(String key) throws PropertyException {
+        if (!cachePS.exists(key)) {
+            cachePS.setDouble(key, decoratedPS.getDouble(key));
+        }
+
+        return cachePS.getDouble(key);
+    }
+
+    public void setInt(String key, int value) throws PropertyException {
+        decoratedPS.setInt(key, value);
+        cachePS.setInt(key, value);
+    }
+
+    public int getInt(String key) throws PropertyException {
+        if (!cachePS.exists(key)) {
+            cachePS.setInt(key, decoratedPS.getInt(key));
+        }
+
+        return cachePS.getInt(key);
+    }
+
+    public Collection getKeys() throws PropertyException {
+        return decoratedPS.getKeys();
+    }
+
+    public Collection getKeys(int type) throws PropertyException {
+        return decoratedPS.getKeys(type);
+    }
+
+    public Collection getKeys(String prefix) throws PropertyException {
+        return decoratedPS.getKeys(prefix);
+    }
+
+    public Collection getKeys(String prefix, int type) throws PropertyException {
+        return decoratedPS.getKeys(prefix, type);
+    }
+
+    public void setLong(String key, long value) throws PropertyException {
+        decoratedPS.setLong(key, value);
+        cachePS.setLong(key, value);
+    }
+
+    public long getLong(String key) throws PropertyException {
+        if (!cachePS.exists(key)) {
+            cachePS.setLong(key, decoratedPS.getLong(key));
+        }
+
+        return cachePS.getLong(key);
+    }
+
+    public void setObject(String key, Object value) throws PropertyException {
+        decoratedPS.setObject(key, value);
+        cachePS.setObject(key, value);
+    }
+
+    public Object getObject(String key) throws PropertyException {
+        if (!cachePS.exists(key)) {
+            cachePS.setObject(key, decoratedPS.getObject(key));
+        }
+
+        return cachePS.getObject(key);
+    }
+
+    public void setProperties(String key, Properties value) throws PropertyException {
+        decoratedPS.setProperties(key, value);
+        cachePS.setProperties(key, value);
+    }
+
+    public Properties getProperties(String key) throws PropertyException {
+        if (!cachePS.exists(key)) {
+            cachePS.setProperties(key, decoratedPS.getProperties(key));
+        }
+
+        return cachePS.getProperties(key);
+    }
+
+    public void setSchema(PropertySetSchema schema) throws PropertyException {
+        decoratedPS.setSchema(schema);
+    }
+
+    public PropertySetSchema getSchema() throws PropertyException {
+        return decoratedPS.getSchema();
+    }
+
+    public boolean isSettable(String property) {
+        return decoratedPS.isSettable(property);
+    }
+
+    public void setString(String key, String value) throws PropertyException {
+        decoratedPS.setString(key, value);
+        cachePS.setString(key, value);
+    }
+
+    public String getString(String key) throws PropertyException {
+        if (!cachePS.exists(key)) {
+            cachePS.setString(key, decoratedPS.getString(key));
+        }
+
+        return cachePS.getString(key);
+    }
+
+    public void setText(String key, String value) throws PropertyException {
+        decoratedPS.setText(key, value);
+        cachePS.setText(key, value);
+    }
+
+    public String getText(String key) throws PropertyException {
+        if (!cachePS.exists(key)) {
+            cachePS.setText(key, decoratedPS.getText(key));
+        }
+
+        return cachePS.getText(key);
+    }
+
+    public int getType(String key) throws PropertyException {
+        return decoratedPS.getType(key);
+    }
+
+    public void setXML(String key, Document value) throws PropertyException {
+        decoratedPS.setXML(key, value);
+        cachePS.setXML(key, value);
+    }
+
+    public Document getXML(String key) throws PropertyException {
+        if (!cachePS.exists(key)) {
+            cachePS.setXML(key, decoratedPS.getXML(key));
+        }
+
+        return cachePS.getXML(key);
+    }
+
+    public boolean exists(String key) throws PropertyException {
+        return decoratedPS.exists(key);
+    }
+
+    public void init(Map config, Map args) {
+        decoratedPS = (PropertySet) args.get("PropertySet");
+
+        String serializableName = (String) config.get("serializableName");
+
+        if (serializableName == null) {
+            serializableName = "serializable";
+        }
+
+        cachePS = (SerializablePropertySet) PropertySetManager.getInstance(serializableName, null);
+
+        Boolean bulkload = (Boolean) args.get("bulkload");
+
+        if ((bulkload != null) && bulkload.booleanValue()) {
+            PropertySetManager.clone(decoratedPS, cachePS);
+        }
+    }
+
+    public void remove(String key) throws PropertyException {
+        cachePS.remove(key);
+        decoratedPS.remove(key);
+    }
+
+    public boolean supportsType(int type) {
+        return decoratedPS.supportsType(type);
+    }
+
+    public boolean supportsTypes() {
+        return decoratedPS.supportsTypes();
+    }
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.