    001    package org.apache.turbine.om.security;
    002
    003    /*
    004     * Licensed to the Apache Software Foundation (ASF) under one
    005     * or more contributor license agreements.  See the NOTICE file
    006     * distributed with this work for additional information
    007     * regarding copyright ownership.  The ASF licenses this file
    008     * to you under the Apache License, Version 2.0 (the
    009     * "License"); you may not use this file except in compliance
    010     * with the License.  You may obtain a copy of the License at
    011     *
    012     *   http://www.apache.org/licenses/LICENSE-2.0
    013     *
    014     * Unless required by applicable law or agreed to in writing,
    015     * software distributed under the License is distributed on an
    016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    017     * KIND, either express or implied.  See the License for the
    018     * specific language governing permissions and limitations
    019     * under the License.
    020     */
    021
    022    import java.util.Collections;
    023    import java.util.HashMap;
    024    import java.util.Map;
    025
    026    /**
    027     * This class represents a generic object used in the Access Control Lists.
    028     *
    029     * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
    030     * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
    031     * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
    032     * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
    033     * @version $Id: SecurityObject.java 1071091 2011-02-15 22:06:55Z tv $
    034     */
    035    public abstract class SecurityObject<T extends SecurityEntity> implements Comparable<T>
    036    {
    037        /** The name of this object. */
    038        private String name;
    039
    040        /** The id of this object */
    041        private int id;
    042
    043        /** The attributes of this object. */
    044        private Map<String, Object> attributes;
    045
    046        /**
    047         * Constructs a new SecurityObject
    048         */
    049        public SecurityObject()
    050        {
    051            this("");
    052        }
    053
    054        /**
    055         * Constructs a new SecurityObject with the specified name.
    056         *
    057         * @param name The name of the new object.
    058         */
    059        public SecurityObject(String name)
    060        {
    061            setName(name);
    062            setId(0);
    063            setAttributes(Collections.synchronizedMap(new HashMap<String, Object>()));
    064        }
    065
    066        /**
    067         * Returns a Map containing this object's attributes.
    068         *
    069         * @return the object's attributes.
    070         */
    071        public Map<String, Object> getAttributes()
    072        {
    073            return attributes;
    074        }
    075
    076        /**
    077         * Replaces this object's attributes with the specified Map.
    078         *
    079         * @param attributes The new attributes of the object.
    080         */
    081        public void setAttributes(Map<String, Object> attributes)
    082        {
    083            this.attributes = attributes;
    084        }
    085
    086        /**
    087         * Retrieves the value of specific attribute of this object.
    088         *
    089         * @param name the name of the attribute
    090         * @return the value of the attribute
    091         */
    092        public Object getAttribute(String name)
    093        {
    094            return attributes.get(name);
    095        }
    096
    097        /**
    098         * Sets the value of specific attribute of this object.
    099         *
    100         * @param name the name of the attribute
    101         * @param value the value of the attribute
    102         */
    103        public void setAttribute(String name, Object value)
    104        {
    105            attributes.put(name, value);
    106        }
    107
    108        /**
    109         * Returns the name of this object.
    110         *
    111         * @return The name of the object.
    112         */
    113        public String getName()
    114        {
    115            return name;
    116        }
    117
    118        /**
    119         * Sets the name of this object.
    120         *
    121         * @param name The name of the object.
    122         */
    123        public void setName(String name)
    124        {
    125            this.name = name;
    126        }
    127
    128        /**
    129         * Unused. There is an ID column in the
    130         * database scheme but it doesn't seem
    131         * to be used.
    132         *
    133         * @return 0
    134         */
    135        public int getId()
    136        {
    137            return id;
    138        }
    139
    140        /**
    141         * Unused. There is an ID column in the
    142         * database scheme but it doesn't seem
    143         * to be used.
    144         *
    145         * @return null
    146         */
    147        public Integer getIdAsObj()
    148        {
    149            return new Integer(id);
    150        }
    151
    152        /**
    153         * Unused. There is an ID column in the
    154         * database scheme but it doesn't seem
    155         * to be used.
    156         *
    157         * @param id The id of the User.
    158         */
    159        public void setId(int id)
    160        {
    161            this.id = id;
    162        }
    163
    164        /**
    165         * Used for ordering SecurityObjects.
    166         *
    167         * @param obj The Object to compare to.
    168         * @return -1 if the name of the other object is lexically greater than this
    169         *         group, 1 if it is lexically lesser, 0 if they are equal.
    170         */
    171        public int compareTo(T obj)
    172        {
    173            if (this.getClass() != obj.getClass())
    174            {
    175                throw new ClassCastException();
    176            }
    177
    178            String name1 = obj.getName();
    179            String name2 = this.getName();
    180
    181            return name2.compareTo(name1);
    182        }
    183
    184        /**
    185         * Returns a textual representation of this object, consisted by
    186         * it's name and attributes.
    187         *
    188         * @return  a textual representation of this group.
    189         */
    190        public String toString()
    191        {
    192            return (getName() + ':' + getAttributes().toString());
    193        }
    194    }