-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalConf.java
More file actions
191 lines (173 loc) · 5.77 KB
/
LocalConf.java
File metadata and controls
191 lines (173 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package skydata.core;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import skydata.core.FolderTree.RootFolder;
import skydata.interfaces.Interface;
/**
*
* @author ivan
*/
public class LocalConf {
private static Properties p;
private static String name;
private static String folder;
public LocalConf(String n) {
name = n;
p = new Properties();
String fileName = name;
InputStream is = null;
//Open config file
try {
is = new FileInputStream(fileName);
File f = new File(name);
folder = f.getParent();
} catch (FileNotFoundException ex) {
//create if doesn't exists
//Logger.getLogger(LocalConf.class.getName()).log(Level.SEVERE, null, ex);
File f;
f = new File(name);
folder = f.getParent();
try {
f.createNewFile();
setDefault();
} catch (IOException ex1) {
Logger.getLogger(LocalConf.class.getName()).log(Level.SEVERE, null, ex1);
}
//try to open again the new file
try {
is = new FileInputStream(fileName);
} catch (FileNotFoundException ex1) {
Logger.getLogger(LocalConf.class.getName()).log(Level.SEVERE, null, ex1);
}
}
//load configurations
try {
p.load(is);
} catch (IOException ex) {
Logger.getLogger(LocalConf.class.getName()).log(Level.SEVERE, null, ex);
}
}
public LocalConf() {
this("config");
}
public static String getFolder() {
return folder;
}
public void setDefault() {
p.setProperty("User", "");
p.setProperty("Pass", "");
p.setProperty("Folders", "");
save();
}
private void save() {
try {
p.store(new FileOutputStream(name), null);
} catch (FileNotFoundException ex) {
Logger.getLogger(LocalConf.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(LocalConf.class.getName()).log(Level.SEVERE, null, ex);
}
}
public String getUser() {
return p.getProperty("User");
}
public String getPass() {
return p.getProperty("Pass");
}
public ArrayList<ArrayList<String>> getFolders() {
String s = p.getProperty("Folders");
String folders[] = s.split(";");
ArrayList<ArrayList<String>> res = new ArrayList<ArrayList<String>>();
for(int i = 0; i < folders.length; ++i) {
if (!folders[i].equals("")) {
String fold[] = folders[i].split("\"");
ArrayList<String> res2 = new ArrayList<String>();
res2.add(fold[0]);
res2.add(fold[1]);
res2.add(fold[2]);
res2.add(fold[3]);
res2.add(fold[4]);
res.add(res2);
}
}
//System.out.println("getFolders " + res);
return res;
}
public TreeMap<String,TreeMap<String,String>> getFoldersMap() {
String s = p.getProperty("Folders");
String folders[] = s.split(";");
TreeMap<String,TreeMap<String,String>> res = new TreeMap<String,TreeMap<String,String>>();
for(int i = 0; i < folders.length; ++i) {
s = folders[i];
if (!folders[i].equals("")) {
String fold[] = s.split("\"");
TreeMap<String,String>res2 = new TreeMap<String,String>();
res2.put("path",fold[1]);
res2.put("sync",fold[2]);
res2.put("write",fold[3]);
res2.put("owner",fold[4]);
res.put(fold[0],res2);
}
}
//System.out.println("getFoldersMap " + res);
return res;
}
public void setUser(String s) {
p.setProperty("User", s);
save();
}
public void setPass(String s) {
p.setProperty("Pass", s);
save();
}
public void setFolders(ArrayList<ArrayList<String>> list) {
String s = "";
for(int i = 0; i < list.size(); ++i) {
if(i != 0) s = s+";";
s = s+list.get(i).get(0)+"\""+list.get(i).get(1)+"\""+list.get(i).get(2)+"\""+list.get(i).get(3)+"\""+list.get(i).get(4);
}
p.setProperty("Folders",s);
//System.out.println("setFolders " + s);
save();
}
public void setFoldersMap(TreeMap<String,TreeMap<String,String>> map) {
String s = "";
Iterator it = map.entrySet().iterator();
int i = 0;
while(it.hasNext()) {
Map.Entry e = (Map.Entry)it.next();
if(i != 0) s = s+";";
TreeMap<String,String> val = (TreeMap<String,String>)e.getValue();
s = s+e.getKey()+"\""+val.get("path")+"\""+val.get("sync")+"\""+val.get("write")+"\""+val.get("owner");
i++;
}
p.setProperty("Folders",s);
//System.out.println("setFoldersMap " + s);
save();
}
public boolean exists() {
File f = new File(name);
return f.exists();
}
}