Project

General

Profile

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

    
10

    
11
package eu.etaxonomy.cdm.common;
12

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

    
20
import org.apache.log4j.Logger;
21

    
22

    
23
public class AccountStore {
24

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
139
    }
140
}
(2-2/20)