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
|
private static Logger logger = Logger.getLogger(AccountStore.class);
|
26
|
|
27
|
final static String userprofile = System.getProperty("user.home");
|
28
|
|
29
|
public final static File accountsFile = new File(userprofile + File.separator + ".cdmLibrary" + File.separator + ".dbaccounts.properties");
|
30
|
|
31
|
public static String getAccountsFileName(){
|
32
|
try {
|
33
|
return accountsFile.getCanonicalPath();
|
34
|
} catch (IOException e) {
|
35
|
logger.warn(e);
|
36
|
return "AN ERROR OCCURRED WHEN RETRIEVING THE FILE NAME";
|
37
|
}
|
38
|
}
|
39
|
|
40
|
public String getPassword(String dbms, String strServer, String userName){
|
41
|
String pwd = null;
|
42
|
|
43
|
Properties accounts = loadAccounts();
|
44
|
String key = strServer+'.'+dbms+'.'+userName;
|
45
|
pwd = accounts.getProperty(key);
|
46
|
return pwd;
|
47
|
}
|
48
|
|
49
|
private Properties loadAccounts() {
|
50
|
Properties accounts = new Properties();
|
51
|
try {
|
52
|
accountsFile.createNewFile();
|
53
|
|
54
|
// this gives me errors. Properties object doesn't have a method
|
55
|
// with this signature
|
56
|
//FileReader in = new FileReader(accountsFile);
|
57
|
//accounts.load(in);
|
58
|
//in.close();
|
59
|
|
60
|
FileInputStream inStream = new FileInputStream(accountsFile);
|
61
|
accounts.load(inStream);
|
62
|
inStream.close();
|
63
|
|
64
|
} catch (IOException e) {
|
65
|
logger.error(e);
|
66
|
}
|
67
|
return accounts;
|
68
|
}
|
69
|
|
70
|
public void setPassword(String dbms, String strServer, String userName, String pwd){
|
71
|
Properties accounts = loadAccounts();
|
72
|
String key = strServer+'.'+dbms+'.'+userName;
|
73
|
accounts.setProperty(key, pwd);
|
74
|
saveAccounts(accounts);
|
75
|
}
|
76
|
|
77
|
private void saveAccounts(Properties accounts) {
|
78
|
//FileWriter out;
|
79
|
FileOutputStream outStream;
|
80
|
try {
|
81
|
//out = new FileWriter(accountsFile);
|
82
|
//accounts.store(out, "");
|
83
|
//out.close();
|
84
|
outStream = new FileOutputStream(accountsFile);
|
85
|
accounts.store(outStream, "");
|
86
|
outStream.close();
|
87
|
} catch (FileNotFoundException e) {
|
88
|
logger.error("File " + accountsFile.toString() + " not found", e);
|
89
|
} catch (IOException e) {
|
90
|
logger.error("Unable to write properties", e);
|
91
|
}
|
92
|
}
|
93
|
|
94
|
public void removePassword(String dbms, String strServer, String userName){
|
95
|
String key = strServer+'.'+dbms+'.'+userName;
|
96
|
Properties accounts = loadAccounts();
|
97
|
accounts.remove(key);
|
98
|
saveAccounts(accounts);
|
99
|
}
|
100
|
|
101
|
public static String readOrStorePassword(String dbms, String strServer, String userName, String pwd){
|
102
|
AccountStore accounts = new AccountStore();
|
103
|
boolean doStore = false;
|
104
|
try {
|
105
|
if (pwd == null){
|
106
|
pwd = accounts.getPassword(dbms, strServer, userName);
|
107
|
if(pwd == null){
|
108
|
doStore = true;
|
109
|
pwd = CdmUtils.readInputLine("Please insert password for user '" + CdmUtils.Nz(userName) + "': ");
|
110
|
} else {
|
111
|
logger.info("using stored password for user '" + CdmUtils.Nz(userName) + "'");
|
112
|
}
|
113
|
}
|
114
|
// on success store userName, pwd in property file
|
115
|
if(doStore){
|
116
|
accounts.setPassword(dbms, strServer, userName, pwd);
|
117
|
logger.info("password stored in " + AccountStore.getAccountsFileName());
|
118
|
}
|
119
|
} catch (Exception e) {
|
120
|
if(doStore){
|
121
|
accounts.removePassword(dbms, strServer, userName);
|
122
|
logger.info("password removed from " + AccountStore.getAccountsFileName());
|
123
|
}
|
124
|
logger.error(e);
|
125
|
return null;
|
126
|
}
|
127
|
return pwd;
|
128
|
}
|
129
|
|
130
|
public static void main(String[] args) {
|
131
|
AccountStore a = new AccountStore();
|
132
|
// BerlinModel - EditWp6
|
133
|
String dbms = "SQLServer";
|
134
|
String strServer = "BGBM111";
|
135
|
String userName = "webUser";
|
136
|
a.setPassword(dbms, strServer, userName, "psst");
|
137
|
logger.info(a.getPassword(dbms, strServer, userName));
|
138
|
a.removePassword(dbms, strServer, userName);
|
139
|
logger.info(a.getPassword(dbms, strServer, userName));
|
140
|
|
141
|
}
|
142
|
}
|