view src/kryshen/catalina/startup/HomesUserDatabase.java @ 3:3ab011b6e6e8

Cleaner refactored version.
author Mikhail Kryshen <mikhail@kryshen.net>
date Fri, 30 Oct 2009 05:12:58 +0300
parents d11df03af52a
children 6ef7cbf5c8d6
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.HashMap;
25 import java.util.Map;
27 /**
28 * Concrete implementation of the <strong>UserDatabase</code> interface
29 * considers all directories in a directory whose pathname is specified
30 * to our constructor to be "home" directories for those users.
31 *
32 * @author Craig R. McClanahan
33 * @author Mikhail Kryshen
34 */
36 public final class HomesUserDatabase implements UserDatabase {
38 // --------------------------------------------------- Instance Variables
40 /**
41 * The set of home directories for all defined users, keyed by username.
42 */
43 private Map<String, File> homes = new HashMap<String, File>();
46 /**
47 * The UserConfig listener with which we are associated.
48 */
49 private UserConfig userConfig = null;
52 // ----------------------------------------------------------- Properties
55 /**
56 * Return the UserConfig listener with which we are associated.
57 */
58 public UserConfig getUserConfig() {
59 return (this.userConfig);
60 }
63 /**
64 * Set the UserConfig listener with which we are associated.
65 *
66 * @param userConfig The new UserConfig listener
67 */
68 public void setUserConfig(UserConfig userConfig) {
69 this.userConfig = userConfig;
70 init();
72 }
75 // ------------------------------------------------------- Public Methods
78 /**
79 * Return an absolute pathname to the home directory for the specified user.
80 *
81 * @param user User for which a home directory should be retrieved
82 */
83 public File getHome(String user) {
84 return (homes.get(user));
85 }
87 // ------------------------------------------------------ Private Methods
90 /**
91 * Initialize our set of users and home directories.
92 */
93 private void init() {
94 String homeBase = userConfig.getHomeBase();
95 File homeBaseDir = new File(homeBase);
97 if (!homeBaseDir.exists() || !homeBaseDir.isDirectory()) {
98 return;
99 }
101 String homeBaseFiles[] = homeBaseDir.list();
103 for (int i = 0; i < homeBaseFiles.length; i++) {
104 File homeDir = new File(homeBaseDir, homeBaseFiles[i]);
106 if (!homeDir.isDirectory() || !homeDir.canExecute()) {
107 continue;
108 }
110 homes.put(homeBaseFiles[i], homeDir);
111 }
112 }
114 public Map<String, File> getUserHomes() {
115 return Collections.unmodifiableMap(homes);
116 }
117 }