KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > user > provider > file > FileAccessProvider


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.user.provider.file;
6
7 import com.opensymphony.user.Entity;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Properties JavaDoc;
17
18
19 /**
20  * @author Dave Brondsema
21  */

22 abstract public class FileAccessProvider implements com.opensymphony.user.provider.AccessProvider {
23     //~ Static fields/initializers /////////////////////////////////////////////
24

25     protected static final Log log = LogFactory.getLog(FileAccessProvider.class);
26
27     //~ Instance fields ////////////////////////////////////////////////////////
28

29     protected FileGroupsCache groupCache; /* this must be set by any child class's init() function */
30
31     //~ Methods ////////////////////////////////////////////////////////////////
32

33     public boolean addToGroup(String JavaDoc username, String JavaDoc groupname) {
34         if (!inGroup(username, groupname)) {
35             boolean rv = getGroup(groupname).users.add(username);
36
37             return rv && groupCache.store();
38         }
39
40         return false;
41     }
42
43     public boolean create(String JavaDoc name) {
44         if (groupCache.groups.containsKey(name)) {
45             return false;
46         }
47
48         FileGroup group = new FileGroup();
49         group.name = name;
50         groupCache.groups.put(name, group);
51
52         return groupCache.store();
53     }
54
55     public void flushCaches() {
56         groupCache.store();
57     }
58
59     public boolean handles(String JavaDoc name) {
60         if (groupCache == null) {
61             return false;
62         }
63         return groupCache.groups.containsKey(name);
64         // @TODO: Major hack
65
/*
66         if (!handle) {
67             handle = new SerializableCredentialsProvider().handles(name);
68         }
69
70         return handle;
71         */

72
73         /*
74
75         this needs to check if this access provider handles groups AND users
76
77         but it has no reference to the in memory users
78
79
80         old version:
81
82         return groups.containsKey( name );
83
84         */

85     }
86
87     public boolean inGroup(String JavaDoc username, String JavaDoc groupname) {
88         FileGroup group = getGroup(groupname);
89
90         return (group != null) && group.users.contains(username);
91     }
92
93     public boolean init(Properties JavaDoc properties) {
94         return true;
95     }
96
97     public List JavaDoc list() {
98         return Collections.unmodifiableList(new ArrayList JavaDoc(groupCache.groups.keySet()));
99     }
100
101     public List JavaDoc listGroupsContainingUser(String JavaDoc username) {
102         List JavaDoc result = new ArrayList JavaDoc();
103         Iterator JavaDoc i = groupCache.groups.keySet().iterator();
104
105         while (i.hasNext()) {
106             String JavaDoc currentGroup = (String JavaDoc) i.next();
107
108             if (inGroup(username, currentGroup)) {
109                 result.add(currentGroup);
110             }
111         }
112
113         return Collections.unmodifiableList(result);
114     }
115
116     public List JavaDoc listUsersInGroup(String JavaDoc groupname) {
117         FileGroup g = getGroup(groupname);
118
119         if (g == null) {
120             return Collections.EMPTY_LIST;
121         }
122
123         return Collections.unmodifiableList(getGroup(groupname).users);
124     }
125
126     public boolean load(String JavaDoc name, Entity.Accessor accessor) {
127         accessor.setMutable(true);
128
129         return true;
130     }
131
132     public boolean remove(String JavaDoc name) {
133         boolean rv = groupCache.groups.remove(name) != null;
134
135         return rv && groupCache.store();
136     }
137
138     public boolean removeFromGroup(String JavaDoc username, String JavaDoc groupname) {
139         boolean rv = getGroup(groupname).users.remove(username);
140
141         return rv && groupCache.store();
142     }
143
144     public boolean store(String JavaDoc name, Entity.Accessor accessor) {
145         return groupCache.store();
146     }
147
148     private FileGroup getGroup(String JavaDoc groupname) {
149         FileGroup group = (FileGroup) groupCache.groups.get(groupname);
150
151         return group;
152     }
153 }
154
Popular Tags