KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > plugins > TmpFilePassword


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.security.plugins;
8
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.CharArrayWriter JavaDoc;
13 import java.io.RandomAccessFile JavaDoc;
14
15 import org.jboss.logging.Logger;
16
17 /** Read a password from a file specified via the ctor and then overwrite
18  the file contents with garbage, and then remove it. This may be used as a
19  password accessor in conjunction with the JaasSecurityDomain
20  {CLASS}org.jboss.security.plugins.TmpFilePassword:password-file
21  format of the KeyStorePass attribute.
22  
23  This class waits until the file exists if it does not when toCharArray()
24  is called. It prints out to the console every 10 seconds the path to
25  the file it is waiting on until the file is created.
26
27  @author Scott.Stark@jboss.org
28  @version $Revison:$
29  */

30 public class TmpFilePassword
31 {
32    private static Logger log = Logger.getLogger(TmpFilePassword.class);
33    private File JavaDoc passwordFile;
34
35    public TmpFilePassword(String JavaDoc file)
36    {
37       passwordFile = new File JavaDoc(file);
38    }
39
40    public char[] toCharArray()
41       throws IOException JavaDoc
42    {
43       while( passwordFile.exists() == false )
44       {
45          log.info("Waiting for password file: "+passwordFile.getAbsolutePath());
46          try
47          {
48             Thread.sleep(10*1000);
49          }
50          catch(InterruptedException JavaDoc e)
51          {
52             log.info("Exiting wait on InterruptedException");
53             break;
54          }
55       }
56       FileInputStream JavaDoc fis = new FileInputStream JavaDoc(passwordFile);
57       CharArrayWriter JavaDoc writer = new CharArrayWriter JavaDoc();
58       int b;
59       while( (b = fis.read()) >= 0 )
60       {
61          if( b == '\r' || b == '\n' )
62             continue;
63          writer.write(b);
64       }
65       fis.close();
66       char[] password = writer.toCharArray();
67       writer.reset();
68       for(int n = 0; n < password.length; n ++)
69          writer.write('\0');
70
71       // Overwrite the password file
72
try
73       {
74          RandomAccessFile JavaDoc raf = new RandomAccessFile JavaDoc(passwordFile, "rws");
75          for(int i = 0; i < 10; i ++)
76          {
77             raf.seek(0);
78             for(int j = 0; j < password.length; j ++)
79                raf.write(j);
80          }
81          raf.close();
82          if( passwordFile.delete() == false )
83             log.warn("Was not able to delete the password file");
84       }
85       catch(Exception JavaDoc e)
86       {
87          log.warn("Failed to zero the password file", e);
88       }
89       return password;
90    }
91 }
92
Popular Tags