view src/kryshen/catalina/startup/HomesUserDatabase.java @ 7:ca0b81d28307

Compatibility fixes.
author Mikhail Kryshen <mikhail@kryshen.net>
date Sun, 01 Nov 2009 04:25:39 +0300
parents 66a0db4d54aa
children
line source
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
19 package kryshen.catalina.startup;
22 import java.io.File;
23 import java.util.Collections;
24 import java.util.Iterator;
26 /**
27 * Implementation of the <code>UserDatabase</code> interface
28 * lists all users by their home directories found in the specified
29 * base directory.
30 *
31 * @author Mikhail Kryshen
32 */
34 public final class HomesUserDatabase implements UserDatabase {
36 /**
37 * Iterates over users with accessible home directories.
38 */
39 private class UserIterator implements Iterator<User> {
40 final File homeBase;
41 final String[] homes;
43 User next;
44 int index = 0;
46 UserIterator(File homeBase) {
47 this.homeBase = homeBase;
48 this.homes = homeBase.list();
50 findNext();
51 }
53 /**
54 * Find the next accessible home directory in the list and
55 * create appropriate User to be returned by <code>next()</code>.
56 */
57 void findNext() {
58 next = null;
60 while (next == null && index < homes.length) {
61 final String name = homes[index];
62 final File home = new File(homeBase, name);
64 index++;
66 if (!home.isDirectory() /* || !home.canExecute() */) {
67 continue;
68 }
70 next = new User() {
71 public String getName() {
72 return name;
73 }
75 public File getHome() {
76 return home;
77 }
78 };
79 }
80 }
82 public boolean hasNext() {
83 return next != null;
84 }
86 public User next() {
87 User current = next;
88 findNext();
89 return current;
90 }
92 public void remove() {
93 throw new UnsupportedOperationException("Not supported.");
94 }
95 }
97 /**
98 * The UserConfig listener with which we are associated.
99 */
100 private UserConfig userConfig = null;
102 /**
103 * Return the UserConfig listener with which we are associated.
104 */
105 public UserConfig getUserConfig() {
106 return (this.userConfig);
107 }
109 /**
110 * Set the UserConfig listener with which we are associated.
111 *
112 * @param userConfig The new UserConfig listener
113 */
114 public void setUserConfig(UserConfig userConfig) {
115 this.userConfig = userConfig;
116 }
118 /**
119 * Returns Iterator for the list of users.
120 */
121 public Iterator<User> iterator() {
122 File homeBase = new File(userConfig.getHomeBase());
124 if (!homeBase.exists() || !homeBase.isDirectory()) {
125 // PENDING: throw Exception instead?
126 return Collections.<User>emptyList().iterator();
127 }
129 return new UserIterator(homeBase);
130 }
131 }