Project

General

Profile

Download (4.78 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2007 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10

    
11

    
12
package eu.etaxonomy.cdm.common;
13

    
14
import java.io.File;
15
import java.io.FileInputStream;
16
import java.io.FileNotFoundException;
17
import java.io.FileOutputStream;
18
import java.io.IOException;
19
import java.util.Properties;
20

    
21
import org.apache.log4j.Logger;
22

    
23

    
24
public class AccountStore {
25

    
26
    private static Logger logger = Logger.getLogger(AccountStore.class);
27

    
28
    public final static File accountsFile = new File(CdmUtils.perUserCdmFolder + File.separator + ".dbaccounts.properties");
29

    
30
    public static String getAccountsFileName(){
31
        try {
32
            return accountsFile.getCanonicalPath();
33
        } catch (IOException e) {
34
            logger.warn(e);
35
            return "AN ERROR OCCURRED WHEN RETRIEVING THE FILE NAME";
36
        }
37
    }
38

    
39
    public String getPassword(String dbms, String strServer, String userName){
40
        String pwd = null;
41

    
42
        Properties accounts = loadAccounts();
43
        String key = strServer+'.'+dbms+'.'+userName;
44
        pwd = accounts.getProperty(key);
45
        return pwd;
46
    }
47

    
48
    private Properties loadAccounts() {
49
        Properties accounts = new Properties();
50
        try {
51
            accountsFile.createNewFile();
52

    
53
            // this gives me errors. Properties object doesn't have a method
54
            // with this signature
55
            //FileReader in = new FileReader(accountsFile);
56
            //accounts.load(in);
57
            //in.close();
58

    
59
            FileInputStream inStream = new FileInputStream(accountsFile);
60
            accounts.load(inStream);
61
            inStream.close();
62

    
63
        } catch (IOException e) {
64
            logger.error(e);
65
        }
66
        return accounts;
67
    }
68

    
69
    public void setPassword(String dbms, String strServer, String userName, String pwd){
70
        Properties accounts = loadAccounts();
71
        String key = strServer+'.'+dbms+'.'+userName;
72
        accounts.setProperty(key, pwd);
73
        saveAccounts(accounts);
74
    }
75

    
76
    private void saveAccounts(Properties accounts) {
77
        //FileWriter out;
78
        FileOutputStream outStream;
79
        try {
80
            //out = new FileWriter(accountsFile);
81
            //accounts.store(out, "");
82
            //out.close();
83
            outStream = new FileOutputStream(accountsFile);
84
            accounts.store(outStream, "");
85
            outStream.close();
86
        } catch (FileNotFoundException e) {
87
            logger.error("File " + accountsFile.toString() + " not found", e);
88
        } catch (IOException e) {
89
            logger.error("Unable to write properties", e);
90
        }
91
    }
92

    
93
    public void removePassword(String dbms, String strServer, String userName){
94
        String key = strServer+'.'+dbms+'.'+userName;
95
        Properties accounts = loadAccounts();
96
        accounts.remove(key);
97
        saveAccounts(accounts);
98
    }
99

    
100
    public static String readOrStorePassword(String dbms, String strServer, String userName, String pwd){
101
        AccountStore accounts = new AccountStore();
102
        boolean doStore = false;
103
        try {
104
            if (pwd == null){
105
                pwd = accounts.getPassword(dbms, strServer, userName);
106
                if(pwd == null){
107
                    doStore = true;
108
                    pwd = CdmUtils.readInputLine("Please insert password for user '" + CdmUtils.Nz(userName) + "': ");
109
                } else {
110
                    logger.info("using stored password for user '" + CdmUtils.Nz(userName) + "'");
111
                }
112
            }
113
            // on success store userName, pwd in property file
114
            if(doStore){
115
                accounts.setPassword(dbms, strServer, userName, pwd);
116
                logger.info("password stored in " + AccountStore.getAccountsFileName());
117
            }
118
        } catch (Exception e) {
119
            if(doStore){
120
                accounts.removePassword(dbms, strServer, userName);
121
                logger.info("password removed from " + AccountStore.getAccountsFileName());
122
            }
123
            logger.error(e);
124
            return null;
125
        }
126
        return pwd;
127
    }
128

    
129
    public static void main(String[] args) {
130
        AccountStore a = new AccountStore();
131
//		BerlinModel - EditWp6
132
        String dbms = "SQLServer";
133
        String strServer = "BGBM111";
134
        String userName = "webUser";
135
        a.setPassword(dbms, strServer, userName, "psst");
136
        logger.info(a.getPassword(dbms, strServer, userName));
137
        a.removePassword(dbms, strServer, userName);
138
        logger.info(a.getPassword(dbms, strServer, userName));
139

    
140
    }
141
}
(2-2/20)