001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.startup;
018:
019: import java.io.File;
020: import java.util.Hashtable;
021: import java.util.Enumeration;
022:
023: /**
024: * Concrete implementation of the <strong>UserDatabase</code> interface
025: * considers all directories in a directory whose pathname is specified
026: * to our constructor to be "home" directories for those users.
027: *
028: * @author Craig R. McClanahan
029: * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:49 $
030: */
031:
032: public final class HomesUserDatabase implements UserDatabase {
033:
034: // --------------------------------------------------------- Constructors
035:
036: /**
037: * Initialize a new instance of this user database component.
038: */
039: public HomesUserDatabase() {
040:
041: super ();
042:
043: }
044:
045: // --------------------------------------------------- Instance Variables
046:
047: /**
048: * The set of home directories for all defined users, keyed by username.
049: */
050: private Hashtable homes = new Hashtable();
051:
052: /**
053: * The UserConfig listener with which we are associated.
054: */
055: private UserConfig userConfig = null;
056:
057: // ----------------------------------------------------------- Properties
058:
059: /**
060: * Return the UserConfig listener with which we are associated.
061: */
062: public UserConfig getUserConfig() {
063:
064: return (this .userConfig);
065:
066: }
067:
068: /**
069: * Set the UserConfig listener with which we are associated.
070: *
071: * @param userConfig The new UserConfig listener
072: */
073: public void setUserConfig(UserConfig userConfig) {
074:
075: this .userConfig = userConfig;
076: init();
077:
078: }
079:
080: // ------------------------------------------------------- Public Methods
081:
082: /**
083: * Return an absolute pathname to the home directory for the specified user.
084: *
085: * @param user User for which a home directory should be retrieved
086: */
087: public String getHome(String user) {
088:
089: return ((String) homes.get(user));
090:
091: }
092:
093: /**
094: * Return an enumeration of the usernames defined on this server.
095: */
096: public Enumeration getUsers() {
097:
098: return (homes.keys());
099:
100: }
101:
102: // ------------------------------------------------------ Private Methods
103:
104: /**
105: * Initialize our set of users and home directories.
106: */
107: private void init() {
108:
109: String homeBase = userConfig.getHomeBase();
110: File homeBaseDir = new File(homeBase);
111: if (!homeBaseDir.exists() || !homeBaseDir.isDirectory())
112: return;
113: String homeBaseFiles[] = homeBaseDir.list();
114:
115: for (int i = 0; i < homeBaseFiles.length; i++) {
116: File homeDir = new File(homeBaseDir, homeBaseFiles[i]);
117: if (!homeDir.isDirectory() || !homeDir.canRead())
118: continue;
119: homes.put(homeBaseFiles[i], homeDir.toString());
120: }
121:
122: }
123:
124: }
|