1 package org.apache.turbine.om.security;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 /**
27 * This class represents a generic object used in the Access Control Lists.
28 *
29 * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
30 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
31 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
32 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
33 * @version $Id: SecurityObject.java 1071091 2011-02-15 22:06:55Z tv $
34 */
35 public abstract class SecurityObject<T extends SecurityEntity> implements Comparable<T>
36 {
37 /** The name of this object. */
38 private String name;
39
40 /** The id of this object */
41 private int id;
42
43 /** The attributes of this object. */
44 private Map<String, Object> attributes;
45
46 /**
47 * Constructs a new SecurityObject
48 */
49 public SecurityObject()
50 {
51 this("");
52 }
53
54 /**
55 * Constructs a new SecurityObject with the specified name.
56 *
57 * @param name The name of the new object.
58 */
59 public SecurityObject(String name)
60 {
61 setName(name);
62 setId(0);
63 setAttributes(Collections.synchronizedMap(new HashMap<String, Object>()));
64 }
65
66 /**
67 * Returns a Map containing this object's attributes.
68 *
69 * @return the object's attributes.
70 */
71 public Map<String, Object> getAttributes()
72 {
73 return attributes;
74 }
75
76 /**
77 * Replaces this object's attributes with the specified Map.
78 *
79 * @param attributes The new attributes of the object.
80 */
81 public void setAttributes(Map<String, Object> attributes)
82 {
83 this.attributes = attributes;
84 }
85
86 /**
87 * Retrieves the value of specific attribute of this object.
88 *
89 * @param name the name of the attribute
90 * @return the value of the attribute
91 */
92 public Object getAttribute(String name)
93 {
94 return attributes.get(name);
95 }
96
97 /**
98 * Sets the value of specific attribute of this object.
99 *
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 }