Config.java
Go to the documentation of this file.
1 package p2putility;
2 
3 import java.io.DataInputStream;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.util.Hashtable;
7 
14 public class Config {
15 
16  //Inizializzazione delle variabili configurabili da file
17  private static String classpath = "";
18  private static String rmicpath = "";
19  private static String configpath = "";
20  private static String userlistfile = "";
21  private static String defip = "";
22  private static String userp2p = "";
23  private static String netmask = "";
24  private static String dbp2p = "";
25  private static String clientlog = "";
26  private static String serverlog = "";
27  private static String genericlog = "";
28 
29  private static long filemaxsizetosend = 0;
30  private static long filemaxsizetorecv = 0;
31  private static int port = 0;
32  private static int availableconn = 0;
33  private static int threadtimeout = 0;
34  private static int rmiregport= 0;
35  private static int bcasttimeout= 0;
36 
37  // Non configurabili da file
38  private static int nofactive = availableconn;
39  private static String currentpath = System.getProperty("user.dir"); // current application absolute path (es. c:\prg\)
40  private static String configfile = "config.fil";
41  private static boolean serverOpen = false;
42  private static boolean clientOpen = false;
43  private static String ico_server_enable = "/icons/enable_server_ico.png";
44  private static String ico_server_disable = "/icons/no_connection_ico.png";
50  public static Hashtable cauth = new Hashtable();
51 
57  public Config(){
58  super();
59 
60  if(loadConfig()) return; //Se la configurazione UTENTE e' andata a buon fine esce
61 
62  // Altrimenti, se non carica la configurazione definibile dall'UTENTE applica i
63  //DEFAULT SETTINGS
64  classpath = "/build/classes/";
65  rmicpath = "";
66  configpath = "/p2pfile/";
67  userlistfile = "userlist.csv";
68  defip = "127.0.0.1";
69  port = 9090;
70  userp2p = "p2puser";
71  dbp2p = "p2pdb";
72  netmask = "255.255.255.0";
73  availableconn = 1;
74  nofactive = availableconn;
75  threadtimeout = 10000;
76  rmiregport= 1099;
77  clientlog = "LOG_client.txt";
78  serverlog = "LOG_server.txt";
79  genericlog = "LOG_generic.txt";
80  bcasttimeout = 3000;
81  filemaxsizetosend = 1048576;//1Mb
82  filemaxsizetorecv = 1048576;//1Mb
83  //END DEFAULT SETTINGS
84 
85  }
86 
98  private boolean loadConfig(){
99  try{
100  File f = new File(currentpath+File.separator+configfile);
101 
102  if(!f.exists()) return false;
103 
104  DataInputStream d = new DataInputStream(new FileInputStream(f));
105 
106  java.util.Hashtable total = new java.util.Hashtable();
107 
108  while(d.available()>0){
109 
110  // Ignoro i commenti e le righe inutili
111  String v = "";
112  do{
113  v = d.readLine();
114  if(v==null)
115  v = "#";
116  }
117  while((d.available()>0)&&((v.startsWith("#")||(v.length()<2))));
118 
119  String ids = "classpath=";
120  if(v.startsWith(ids)){
121  classpath = v.substring(ids.length()).trim();
122  if(!total.containsKey(ids))
123  total.put(ids, 0);
124  }
125  else{ // 1
126  ids = "rmicpath=";
127  if(v.startsWith(ids)){
128  rmicpath = v.substring(ids.length()).trim();
129  if(!total.containsKey(ids))
130  total.put(ids, 1);
131  }
132  else{ // 2
133  ids = "configpath=";
134  if(v.startsWith(ids)){
135  configpath = v.substring(ids.length()).trim();
136  if(!total.containsKey(ids))
137  total.put(ids, 2);
138  }
139  else{ // 3
140  ids = "userlistfile=";
141  if(v.startsWith(ids)){
142  userlistfile = v.substring(ids.length()).trim();
143  if(!total.containsKey(ids))
144  total.put(ids, 3);
145  }
146  else{ // 4
147  ids = "defip=";
148  if(v.startsWith(ids)){
149  defip = v.substring(ids.length()).trim();
150  if(!total.containsKey(ids))
151  total.put(ids, 4);
152  }
153  else{ // 5
154  ids = "port=";
155  if(v.startsWith(ids)){
156  port = Integer.parseInt(v.substring(ids.length()).trim());
157  if(!total.containsKey(ids))
158  total.put(ids, 5);
159  }
160  else{ // 6
161  ids = "userp2p=";
162  if(v.startsWith(ids)){
163  userp2p = v.substring(ids.length()).trim();
164  if(!total.containsKey(ids))
165  total.put(ids, 6);
166  }
167  else{ // 7
168  ids = "dbp2p=";
169  if(v.startsWith(ids)){
170  dbp2p = v.substring(ids.length()).trim();
171  if(!total.containsKey(ids))
172  total.put(ids, 7);
173  }
174  else{ // 8
175  ids = "netmask=";
176  if(v.startsWith(ids)){
177  netmask = v.substring(ids.length()).trim();
178  if(!total.containsKey(ids))
179  total.put(ids, 8);
180  }
181  else{ // 9
182  ids = "availableconn=";
183  if(v.startsWith(ids)){
184  availableconn = Integer.parseInt(v.substring(ids.length()).trim());
185  nofactive = availableconn;
186  if(!total.containsKey(ids))
187  total.put(ids, 9);
188  }
189  else{ // 10
190  ids = "threadtimeout=";
191  if(v.startsWith(ids)){
192  threadtimeout = Integer.parseInt(v.substring(ids.length()).trim());
193  if(!total.containsKey(ids))
194  total.put(ids, 10);
195  }
196  else{ // 11
197  ids = "rmiregport=";
198  if(v.startsWith(ids)){
199  rmiregport = Integer.parseInt(v.substring(ids.length()).trim());
200  if(!total.containsKey(ids))
201  total.put(ids, 11);
202  }
203  else{ // 12
204  ids = "clientlog=";
205  if(v.startsWith(ids)){
206  clientlog = v.substring(ids.length()).trim();
207  if(!total.containsKey(ids))
208  total.put(ids, 12);
209  }
210  else{ // 13
211  ids = "serverlog=";
212  if(v.startsWith(ids)){
213  serverlog = v.substring(ids.length()).trim();
214  if(!total.containsKey(ids))
215  total.put(ids, 13);
216  }
217  else{ // 14
218  ids = "genericlog=";
219  if(v.startsWith(ids)){
220  genericlog = v.substring(ids.length()).trim();
221  if(!total.containsKey(ids))
222  total.put(ids, 14);
223  }
224  else{ // 15
225  ids = "bcasttimeout=";
226  if(v.startsWith(ids)){
227  bcasttimeout = Integer.parseInt(v.substring(ids.length()).trim());
228  if(!total.containsKey(ids))
229  total.put(ids, 15);
230  }
231  else{ // 16
232  ids = "filemaxsizetorecv=";
233  if(v.startsWith(ids)){
234  filemaxsizetorecv = Integer.parseInt(v.substring(ids.length()).trim());
235  if(!total.containsKey(ids))
236  total.put(ids, 16);
237  }
238  else{ // 17
239  ids = "filemaxsizetosend=";
240  if(v.startsWith(ids)){
241  filemaxsizetosend = Integer.parseInt(v.substring(ids.length()).trim());
242  if(!total.containsKey(ids))
243  total.put(ids, 17);
244  }
245  }//17
246  }//16
247  }//15
248  }//14
249  }//13
250  }//12
251  }//11
252  }//10
253  }//9
254  }//8
255  }//7
256  }//6
257  }//5
258  }//4
259  }//3
260  }//2
261  }//1
262 
263  }
264  d.close();
265 
266  if(total.size() != 18)
267  p2putility.Util.logGeneric("Config file WARNING: missing arguments!\n"+total);
268 
269  return true;
270  }
271  catch(Exception x){
272  p2putility.Util.logGeneric("Config file ERROR: "+x);
273  return false;
274  }
275  }
276 
280  public static String r_currentpath(){return currentpath;}
281 
285  public static String r_classpath(){return classpath;}
286 
290  public static String r_rmicpath(){return rmicpath;}
291 
295  public static String r_configpath(){return configpath;}
296 
300  public static String r_userlistfile(){return userlistfile;}
301 
305  public static int r_port(){return port;}
306 
310  public static String r_defip(){return defip;}
311 
315  public static String r_userp2p(){return userp2p;}
316 
320  public static String r_dbp2p(){return dbp2p;}
321 
325  public static String r_netmask(){return netmask;}
326 
330  public static int r_threadtimeout(){return threadtimeout;}
331 
335  public static int r_rmiregport(){return rmiregport;}
336 
340  public static String r_clientlog(){return currentpath+configpath+clientlog;}
341 
345  public static String r_serverlog(){return currentpath+configpath+serverlog;}
346 
350  public static String r_genericlog(){return currentpath+configpath+genericlog;}
351 
355  public static int r_bcasttimeout(){return bcasttimeout;}
356 
360  public static long r_filemaxsizetorecv(){
361  if(filemaxsizetorecv>Integer.MAX_VALUE)
362  return Integer.MAX_VALUE;
363  return filemaxsizetorecv;
364  }
365 
369  public static long r_filemaxsizetosend(){
370  if(filemaxsizetosend>Integer.MAX_VALUE)
371  return Integer.MAX_VALUE;
372  return filemaxsizetosend;
373  }
374 
378  public static boolean open_upload_conn(){
379  if(nofactive<0)
380  return false;
381  nofactive--;
382  return true;
383  }
384 
388  public static boolean close_upload_conn(){
389  if(nofactive>=availableconn)
390  return false;
391  nofactive++;
392  return true;
393  }
394 
395  // Funzioni per le varibili non configurabili da file
396 
400  public static boolean r_serverOpen() {return serverOpen;}
401 
405  public static void w_serverOpen(boolean value) {serverOpen = value;}
406 
410  public static boolean r_clientOpen() {return clientOpen;}
411 
415  public static void w_clientOpen(boolean value) {clientOpen = value;}
416 
420  public static String r_ico_server_enable() {return ico_server_enable;}
421 
425  public static String r_ico_server_disable() {return ico_server_disable;}
426 
427 }
static void w_serverOpen(boolean value)
Permette la scrittura della varibile relativa in modo sicuro.
Definition: Config.java:405
static int threadtimeout
Definition: Config.java:33
static String r_configpath()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:295
Classe che permette la configurazione di tutto il programma in base alle specifiche esigenze dell&#39;ute...
Definition: Config.java:14
static String serverlog
Definition: Config.java:26
static String userp2p
Definition: Config.java:22
Classe che implementa metodi vari di utilita&#39; generale.
Definition: Util.java:14
static String ico_server_enable
Definition: Config.java:43
boolean loadConfig()
Caricare la configurazione dal file apposito.
Definition: Config.java:98
static boolean clientOpen
Definition: Config.java:42
static int r_bcasttimeout()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:355
static String ico_server_disable
Definition: Config.java:44
static String genericlog
Definition: Config.java:27
static boolean open_upload_conn()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:378
static String r_userp2p()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:315
static String r_userlistfile()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:300
static String r_rmicpath()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:290
static long filemaxsizetorecv
Definition: Config.java:30
static long filemaxsizetosend
Definition: Config.java:29
static int r_threadtimeout()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:330
static String r_genericlog()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:350
static int rmiregport
Definition: Config.java:34
static long r_filemaxsizetorecv()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:360
static String userlistfile
Definition: Config.java:20
static int port
Definition: Config.java:31
static int r_port()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:305
static String configfile
Definition: Config.java:40
static void logGeneric(String data)
Scrive i dati in input sul file di log Generico (se questo esiste valido)
Definition: Util.java:132
static int nofactive
Definition: Config.java:38
static String r_dbp2p()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:320
static String rmicpath
Definition: Config.java:18
static int r_rmiregport()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:335
Config()
Costruttore.
Definition: Config.java:57
static int bcasttimeout
Definition: Config.java:35
static String r_netmask()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:325
static String defip
Definition: Config.java:21
static boolean r_serverOpen()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:400
static boolean serverOpen
Definition: Config.java:41
static String r_defip()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:310
static Hashtable cauth
Hashtable contenente le coppie (k,v) fornite dal server, per autenticare un client che vuole accedere...
Definition: Config.java:50
static String r_currentpath()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:280
static String netmask
Definition: Config.java:23
static String r_clientlog()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:340
static String clientlog
Definition: Config.java:25
static int availableconn
Definition: Config.java:32
static String dbp2p
Definition: Config.java:24
static String currentpath
Definition: Config.java:39
static String configpath
Definition: Config.java:19
static boolean close_upload_conn()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:388
static boolean r_clientOpen()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:410
static void w_clientOpen(boolean value)
Permette la scrittura della varibile relativa in modo sicuro.
Definition: Config.java:415
static String r_ico_server_enable()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:420
static String r_serverlog()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:345
static long r_filemaxsizetosend()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:369
static String r_classpath()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:285
static String r_ico_server_disable()
Permette la lettura della varibile relativa in modo sicuro.
Definition: Config.java:425
static String classpath
Definition: Config.java:17