1 /***
2 * ObjectType.java
3 *
4 * This file is part of the creme library.
5 * The creme library intends to ease the development effort of large
6 * applications by providing easy to use support classes.
7 *
8 * Copyright (C) 2003 Denis Bregeon
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * contact information: dbregeon@sourceforge.net
25 */
26 package org.jcreme.permissioning;
27
28 import java.util.HashSet;
29 import java.util.Hashtable;
30
31 import org.jcreme.enumerations.Enumeration;
32
33 /***
34 * This enumeration represent the types of the manipulated objects (data). It
35 * provides the association between domains and data types.
36 *
37 * @author $Author: dbregeon $
38 * @version $Revision: 1.1 $
39 */
40 public class ObjectType extends Enumeration {
41 protected static final Hashtable fromName = new Hashtable();
42
43 protected Hashtable authorizedDomains = new Hashtable();
44
45 protected ObjectType(String name) {
46 super(name);
47 }
48
49 /***
50 * @see org.jcreme.enumerations.Enumeration#getFromName()
51 */
52 protected Hashtable getFromName() {
53 return fromName;
54 }
55
56 /***
57 *
58 * @param name
59 * @return
60 */
61 public static ObjectType getObjectType(String name) {
62 ObjectType result = null;
63 if (name != null) {
64 result = (ObjectType) fromName.get(name);
65 if (result == null) {
66 result = new ObjectType(name);
67 }
68 }
69 return result;
70 }
71
72 /***
73 *
74 * @return
75 */
76 public static ObjectType[] getObjectTypes() {
77 return (ObjectType[]) fromName.values().toArray(new ObjectType[0]);
78 }
79
80 /***
81 * @return
82 */
83 public static Enumeration[] getValues() {
84 return (Enumeration[]) fromName.values().toArray(new Enumeration[0]);
85 }
86
87 /***
88 *
89 * @param domain
90 * @param access
91 */
92 public void addDomain(Domain domain, AccessType access) {
93 if ((domain != null) && (access != null)) {
94 HashSet authorizedAccesses = (HashSet) this.authorizedDomains
95 .get(domain);
96 if (authorizedAccesses == null) {
97 authorizedAccesses = new HashSet();
98 this.authorizedDomains.put(domain, authorizedAccesses);
99 }
100 authorizedAccesses.add(access);
101 }
102 }
103
104 /***
105 *
106 * @param domain
107 * @param access
108 */
109 public void removeDomain(Domain domain, AccessType access) {
110 if ((domain != null) && (access != null)) {
111 HashSet authorizedAccesses = (HashSet) this.authorizedDomains
112 .get(domain);
113 if (authorizedAccesses != null) {
114 authorizedAccesses.remove(access);
115 }
116 }
117 }
118
119 /***
120 *
121 * @param domain
122 * @param access
123 * @return
124 */
125 public boolean canAccess(Domain domain, AccessType access) {
126 boolean result = false;
127 if ((domain != null) && (access != null)) {
128 HashSet authorizedAccesses = (HashSet) this.authorizedDomains
129 .get(domain);
130 result = (authorizedAccesses != null)
131 && (authorizedAccesses.contains(access));
132 }
133 return result;
134 }
135
136 }