view src/kryshen/catalina/startup/PasswdUserConfig.java @ 12:2f82b0faeb89

Javadoc.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 03 Nov 2009 00:36:43 +0300
parents 3a71cbe721f9
children 618d0df5d46c
line source
1 package kryshen.catalina.startup;
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.util.regex.Pattern;
9 import org.apache.juli.logging.LogFactory;
11 /**
12 * Event listener for Host that deploys and updates applications
13 * provided by users listed in passwd database.
14 *
15 * @author Mikhail Kryshen
16 */
17 public class PasswdUserConfig extends UserConfig {
19 private static final String PASSWD_DATABASE = "passwd";
21 /* Passwd format details. */
22 private static final Pattern PASSWD_SPLIT_PATTERN = Pattern.compile(":");
23 private static final int PASSWD_FIELD_USERNAME = 0;
24 private static final int PASSWD_FIELD_HOME = 5;
26 /**
27 * Command for retrieving passwd database.
28 */
29 private String getent = null;
31 /**
32 * Path to local passwd file.
33 */
34 private String passwd = "/etc/passwd";
37 public PasswdUserConfig() {
38 log = LogFactory.getLog(PasswdUserConfig.class);
39 }
41 /**
42 * Get command for retrieving passwd database.
43 */
44 public String getGetent() {
45 return getent;
46 }
48 /**
49 * Set command for retrieving passwd database.
50 */
51 public void setGetent(String getent) {
52 this.getent = getent;
53 }
55 /**
56 * Get path to local passwd file.
57 */
58 public String getPasswd() {
59 return passwd;
60 }
62 /**
63 * Set path to local passwd file.
64 */
65 public void setPasswd(String passwd) {
66 this.passwd = passwd;
67 }
69 @Override
70 protected void deployUserApps() {
71 BufferedReader in;
73 try {
74 if (getent == null) {
75 in = new BufferedReader(new FileReader(passwd));
76 } else {
77 Process process = Runtime.getRuntime().exec(
78 new String[] {getent, PASSWD_DATABASE});
80 in = new BufferedReader(
81 new InputStreamReader(process.getInputStream()));
82 }
84 String line;
86 try {
87 while ((line = in.readLine()) != null) {
88 String[] fields = PASSWD_SPLIT_PATTERN.split(line);
90 String name = fields[PASSWD_FIELD_USERNAME];
91 File home = new File(fields[PASSWD_FIELD_HOME]);
93 if (!home.isDirectory()) {
94 //log.warn("Invalid home directory for user "
95 // + name + ": " + home.getPath() + ".");
96 continue;
97 }
99 deployUserApps(name, home);
100 }
101 } finally {
102 in.close();
103 }
104 } catch (IOException e) {
105 log.error("Error reading passwd database.", e);
106 } catch (ArrayIndexOutOfBoundsException e) {
107 log.error("Invalid passwd format.", e);
108 }
109 }
110 }