OSDN Git Service

Initial commit
[ultramonkey-l7/l7gui.git] / src / src / org / ultramonkey / l7 / controller / DirectorController.java
1 package org.ultramonkey.l7.controller;
2
3 import java.io.*;
4 import java.util.*;
5
6 import org.apache.log4j.Logger;
7 import org.ultramonkey.l7.model.LogCategorySet;
8
9 /**
10  * /usr/sbin/l7directored control class
11  * 
12  * @author nakai
13  * @versin 1.0
14  * 
15  */
16 public class DirectorController implements Serializable {
17         static final long serialVersionUID = 1L;
18         
19         private final String l7directord = "/usr/sbin/l7directord";
20
21         protected String filename = "";
22
23         protected String CHECKTIMEOUT_KEY = "checktimeout";
24
25         protected String CHECKINTERVAL_KEY = "checkinterval";
26         
27         protected String CHECKCOUNT_KEY = "checkcount";
28
29         protected String FALLBACK_KEY = "fallback";
30
31         protected String AUTORELOAD_KEY = "autoreload";
32
33         protected String EXECUTE_KEY = "execute";
34         
35         protected String LOGFILE_KEY = "logfile";
36
37         protected String QUIESCENT_KEY = "quiescent";
38
39         protected String CALLBACK_KEY = "callback";
40
41         protected String NEGOTIATE_TIMEOUT_KEY = "negotiatetimeout";
42
43         protected String SUPERVISED_KEY = "supervised";
44
45         protected String VIRTUAL_KEY = "virtual";
46
47         protected String CHECKTYPE_KEY = "checktype";
48
49         protected String CHECKPORT_KEY = "checkport";
50
51         protected String MAXCONN_KEY = "maxconn";
52
53         protected String MODULE_KEY = "module";
54
55         protected String PROTOCOL_KEY = "protocol";
56
57         protected String QOSCLIENTS_KEY = "qosclients";
58
59         protected String QOSSERVICE_KEY = "qosservice";
60
61         protected String RECEIVE_KEY = "receive";
62
63         protected String SERVICE_KEY = "service";
64
65         protected String REAL_KEY = "real";
66
67         protected String REQUEST_KEY = "request";
68
69         protected String SCHEDULER_KEY = "scheduler";
70
71         protected String SORRYSERVER_KEY = "sorryserver";
72
73         protected String HTTPMETHOD_KEY = "httpmethod";
74
75         protected String VIRTUALHOST_KEY = "virtualhost";
76
77         protected String PASSWORD_KEY = "passwd";
78
79         protected String LOGIN_KEY = "login";
80         
81         protected String DATABASE_KEY = "database";
82
83         protected String REALDOWNCALLBACK_KEY = "realdowncallback";
84
85         protected String REALRECOVERCALLBACK_KEY = "realrecovercallback";
86
87         /**
88          * pair class
89          * <p>
90          * class pair
91          * </p>
92          * <p>
93          * Copyright(c) NTT COMWARE 2008
94          * </p>
95          * 
96          * @author kubota
97          */
98         protected class Pair<E, N> {
99                 protected E elementFirst;
100
101                 protected N elementSecond;
102
103                 public Pair(E firstIn, N secondIn) {
104                         elementFirst = firstIn;
105                         elementSecond = secondIn;
106                 }
107
108                 public void set(E firstIn, N secondIn) {
109                         elementFirst = firstIn;
110                         elementSecond = secondIn;
111                 }
112
113                 public E first() {
114                         return elementFirst;
115                 }
116
117                 public N second() {
118                         return elementSecond;
119                 }
120         }
121
122         protected Pair<String, String> paseKey(String inLine) {
123                 if (inLine == null)
124                         return null;
125                 int pos = inLine.indexOf("=");
126                 if (pos == -1)
127                         return new Pair(inLine, "");
128                 String first = inLine.substring(0, pos);
129                 String second = inLine.substring(pos + 1, inLine.length());
130                 if (first.indexOf('#') != -1)
131                         return new Pair<String, String>("", "");
132                 if (second.indexOf('#') != -1) {
133                         pos = second.indexOf('#');
134                         second = second.substring(0, pos);
135                 }
136                 first = first.trim();
137                 second = second.trim();
138                 Pair<String, String> pair = new Pair<String, String>(first, second);
139                 return pair;
140         }
141
142         protected boolean serchfile() {
143                 // sengenbasyo kentou yousu
144                 final String confname = "l7directord.cf";
145                 final String confdir1 = "/etc/ha.d/";
146                 final String confdir2 = "/etc/ha.d/conf/";
147                 String conf = confdir1 + confname;
148                 File objFile = new File(conf);
149                 if (!objFile.exists()) {
150                         conf = confdir2 + confname;
151                         objFile = new File(conf);
152                         if (!objFile.exists()) {
153                                 return false;
154                         }
155                 }
156                 if (!objFile.isFile()) {
157                         return false;
158                 }
159                 filename = objFile.getPath();
160                 return true;
161         }
162         
163         private static String trimQuote(String s) {
164                 if (s != null && s.matches("^\".*\"$")) {
165                         s = s.substring(1, s.length() - 1);
166                 }
167                 return s;
168         }
169
170         protected DirectorData loadFromFile()
171                         throws org.ultramonkey.l7.model.FileNotReadException {
172                 DirectorData data;
173                 try {
174                         java.io.FileReader file = new java.io.FileReader(filename);
175                         java.io.BufferedReader reader = new java.io.BufferedReader(file);
176                         String buffer;
177                         data = new DirectorData();
178                         Stack<String> stack = new Stack<String>();
179                         int position = 0;
180                         while ((buffer = reader.readLine()) != null) {
181                                 buffer = buffer.replaceAll("\\s+$", "");
182                                 buffer = buffer.replaceAll("\\t", "    ");
183                                 if (buffer.length() == 0 || buffer.matches("^\\s*#.*"))
184                                         continue;
185                                 if (buffer.indexOf(VIRTUAL_KEY) == 0)
186                                         position++;
187                                 stack.push(buffer);
188                         }
189                         String tmpGlobalLogFile = "";
190                         String tmpGlobalExecute = "";
191                         boolean tmpGlobalAutoreaload = false;
192                         String tmpGlobalCallback = "";
193                         String tmpGlobalFallback = "";
194                         String tmpGlobalCheckInterval = "";
195                         String tmpGlobalCheckTimeOut = "";
196                         String tmpGlobalCheckCount = "";
197                         String tmpGlobalNegotiateTimeOut = "";
198                         String tmpGlobalQuiescent = "";
199                         String tmpGlobalSupervised = "no";
200                         String tmpCheckTimeOut = "";
201                         String tmpCheckCount = "";
202                         String tmpNegotiateTimeOut = "";
203                         String tmpQuiescent = "";
204                         String tmpQosClients = "";
205                         String tmpQosService = "";
206                         String tmpSorryServer = "";
207                         String tmpMaxConn = "";
208                         String tmpProtocol = "";
209                         String tmpScheduler = "";
210                         String tmpReceive = "";
211                         String tmpRequest = "";
212                         String tmpService = "";
213                         String tmpModule = "";
214                         Vector<String> tmpReal = new Vector<String>();
215                         String tmpCheckType = "";
216                         String tmpCheckport = "";
217                         String tmpHttpMethod = "";
218                         String tmpVirtualHost = "";
219                         String tmpPassword = "";
220                         String tmpLogin = "";
221                         String tmpDatabase = "";
222                         String tmpRealDownCallback = "";
223                         String tmpRealRecoverCallback = "";
224                         while (!stack.empty()) {
225                                 buffer = stack.pop();
226                                 if (buffer.indexOf(AUTORELOAD_KEY) == 0) {
227                                         Pair<String, String> pair = paseKey(buffer);
228                                         if (pair.second().indexOf("yes") != -1)
229                                                 tmpGlobalAutoreaload = true;
230                                         else
231                                                 tmpGlobalAutoreaload = false;
232                                 }
233                                 if (buffer.indexOf(CALLBACK_KEY) == 0) {
234                                         Pair<String, String> pair = paseKey(buffer);
235                                         tmpGlobalCallback = trimQuote(pair.second());
236                                 } else if (buffer.indexOf(CHECKTIMEOUT_KEY) == 0) {
237                                         Pair<String, String> pair = paseKey(buffer);
238                                         tmpGlobalCheckTimeOut = pair.second();
239                                 } else if (buffer.indexOf(CHECKINTERVAL_KEY) == 0) {
240                                         Pair<String, String> pair = paseKey(buffer);
241                                         tmpGlobalCheckInterval = pair.second();
242                                 } else if (buffer.indexOf(CHECKCOUNT_KEY) == 0) {
243                                         Pair<String, String> pair = paseKey(buffer);
244                                         tmpGlobalCheckCount = pair.second();
245                                 } else if (buffer.indexOf(FALLBACK_KEY) == 0) {
246                                         Pair<String, String> pair = paseKey(buffer);
247                                         tmpGlobalFallback = pair.second();
248                                 } else if (buffer.indexOf(EXECUTE_KEY) == 0) {
249                                         Pair<String, String> pair = paseKey(buffer);
250                                         tmpGlobalExecute = trimQuote(pair.second());
251                                 } else if (buffer.indexOf(LOGFILE_KEY) == 0) {
252                                         Pair<String, String> pair = paseKey(buffer);
253                                         tmpGlobalLogFile = trimQuote(pair.second());
254                                 } else if (buffer.indexOf(NEGOTIATE_TIMEOUT_KEY) == 0) {
255                                         Pair<String, String> pair = paseKey(buffer);
256                                         tmpGlobalNegotiateTimeOut = pair.second();
257                                 } else if (buffer.indexOf(QUIESCENT_KEY) == 0) {
258                                         Pair<String, String> pair = paseKey(buffer);
259                                         tmpGlobalQuiescent = pair.second();
260                                 } else if (buffer.indexOf(SUPERVISED_KEY) == 0) {
261                                         tmpGlobalSupervised = "yes";
262                                 } else if (buffer.indexOf(CHECKTIMEOUT_KEY) >= 4) {
263                                         Pair<String, String> pair = paseKey(buffer);
264                                         tmpCheckTimeOut = pair.second();
265                                 } else if (buffer.indexOf(CHECKCOUNT_KEY) >= 4) {
266                                         Pair<String, String> pair = paseKey(buffer);
267                                         tmpCheckCount = pair.second();
268                                 } else if (buffer.indexOf(NEGOTIATE_TIMEOUT_KEY) >= 4) {
269                                         Pair<String, String> pair = paseKey(buffer);
270                                         tmpNegotiateTimeOut = pair.second();
271                                 } else if (buffer.indexOf(QUIESCENT_KEY) >= 4) {
272                                         Pair<String, String> pair = paseKey(buffer);
273                                         tmpQuiescent = pair.second();
274                                 } else if (buffer.indexOf(CHECKTYPE_KEY) >= 4) {
275                                         Pair<String, String> pair = paseKey(buffer);
276                                         tmpCheckType = pair.second();
277                                 } else if (buffer.indexOf(CHECKPORT_KEY) >= 4) {
278                                         Pair<String, String> pair = paseKey(buffer);
279                                         tmpCheckport = pair.second();
280                                 } else if (buffer.indexOf(MAXCONN_KEY) >= 4) {
281                                         Pair<String, String> pair = paseKey(buffer);
282                                         tmpMaxConn = pair.second();
283                                 } else if (buffer.indexOf(MODULE_KEY) >= 4) {
284                                         Pair<String, String> pair = paseKey(buffer);
285                                         tmpModule = pair.second();
286                                 } else if (buffer.indexOf(PROTOCOL_KEY) >= 4) {
287                                         Pair<String, String> pair = paseKey(buffer);
288                                         tmpProtocol = pair.second();
289                                 } else if (buffer.indexOf(QOSCLIENTS_KEY) >= 4) {
290                                         Pair<String, String> pair = paseKey(buffer);
291                                         tmpQosClients = pair.second();
292                                 } else if (buffer.indexOf(QOSSERVICE_KEY) >= 4) {
293                                         Pair<String, String> pair = paseKey(buffer);
294                                         tmpQosService = pair.second();
295                                 } else if (buffer.indexOf(REAL_KEY) >= 4
296                                                 && buffer.indexOf(REALRECOVERCALLBACK_KEY) == -1
297                                                 && buffer.indexOf(REALDOWNCALLBACK_KEY) == -1) {
298                                         Pair<String, String> pair = paseKey(buffer);
299                                         String real = pair.second();
300                                         tmpReal.add(real);
301                                 } else if (buffer.indexOf(RECEIVE_KEY) >= 4) {
302                                         Pair<String, String> pair = paseKey(buffer);
303                                         tmpReceive = trimQuote(pair.second());
304                                 } else if (buffer.indexOf(REQUEST_KEY) >= 4) {
305                                         Pair<String, String> pair = paseKey(buffer);
306                                         tmpRequest = trimQuote(pair.second());
307                                 } else if (buffer.indexOf(SCHEDULER_KEY) >= 4) {
308                                         Pair<String, String> pair = paseKey(buffer);
309                                         tmpScheduler = pair.second();
310                                 } else if (buffer.indexOf(SERVICE_KEY) >= 4) {
311                                         Pair<String, String> pair = paseKey(buffer);
312                                         tmpService = trimQuote(pair.second());
313                                 } else if (buffer.indexOf(HTTPMETHOD_KEY) >= 4) {
314                                         Pair<String, String> pair = paseKey(buffer);
315                                         tmpHttpMethod = pair.second();
316                                 } else if (buffer.indexOf(VIRTUALHOST_KEY) >= 4) {
317                                         Pair<String, String> pair = paseKey(buffer);
318                                         tmpVirtualHost = trimQuote(pair.second());
319                                 } else if (buffer.indexOf(PASSWORD_KEY) >= 4) {
320                                         Pair<String, String> pair = paseKey(buffer);
321                                         tmpPassword = trimQuote(pair.second());
322                                 } else if (buffer.indexOf(LOGIN_KEY) >= 4) {
323                                         Pair<String, String> pair = paseKey(buffer);
324                                         tmpLogin = trimQuote(pair.second());
325                                 } else if (buffer.indexOf(DATABASE_KEY) >= 4) {
326                                         Pair<String, String> pair = paseKey(buffer);
327                                         tmpDatabase = trimQuote(pair.second());
328                                 } else if (buffer.indexOf(SORRYSERVER_KEY) >= 4) {
329                                         Pair<String, String> pair = paseKey(buffer);
330                                         tmpSorryServer = pair.second();
331                                 } else if (buffer.indexOf(REALDOWNCALLBACK_KEY) >= 4) {
332                                         Pair<String, String> pair = paseKey(buffer);
333                                         tmpRealDownCallback = trimQuote(pair.second());
334                                 } else if (buffer.indexOf(REALRECOVERCALLBACK_KEY) >= 4) {
335                                         Pair<String, String> pair = paseKey(buffer);
336                                         tmpRealRecoverCallback = trimQuote(pair.second());
337                                 } else if (buffer.indexOf(VIRTUAL_KEY) == 0) {
338                                         Pair<String, String> pair = paseKey(buffer);
339                                         VirtualSetting vs = new VirtualSetting();
340                                         int pos = pair.second().indexOf(":");
341                                         vs.virtual.host = pair.second().substring(0, pos);
342                                         vs.virtual.port = pair.second().substring(pos + 1,
343                                                         pair.second().length());
344                                         if (! vs.virtual.port.matches("^\\d+$"))
345                                                 vs.virtual.port = GetServ.byName(vs.virtual.port, "tcp");
346                                         for (Iterator<String> itr = tmpReal.iterator(); itr
347                                                         .hasNext();) {
348                                                 String realstr = itr.next();
349                                                 pos = realstr.indexOf(":");
350                                                 EndPoint ed = new EndPoint();
351                                                 ed.host = realstr.substring(0, pos);
352                                                 int spacepos = realstr.lastIndexOf(" ");
353                                                 String tmpWeight = "";
354                                                 if (spacepos == -1) {
355                                                         ed.port = realstr.substring(pos + 1, realstr
356                                                                         .length());
357                                                 } else {
358                                                         ed.port = realstr.substring(pos + 1, spacepos).replaceAll(" masq", "");
359                                                         tmpWeight = realstr.substring(spacepos + 1, realstr
360                                                                         .length());
361                                                 }
362                                                 if (! ed.port.matches("^\\d+$"))
363                                                         ed.port = GetServ.byName(ed.port, "tcp");
364
365                                                 if (tmpWeight.length() != 0) {
366                                                         try {
367                                                                 ed.weight = Integer.parseInt(tmpWeight);
368                                                         } catch (java.lang.NumberFormatException e) {
369                                                                 ed.weight = 1;
370                                                         }
371                                                 }
372                                                 vs.real.add(ed);
373                                         }
374                                         vs.checktype = tmpCheckType;
375                                         if (tmpMaxConn.length() != 0)
376                                                 vs.maxconn = Integer.parseInt(tmpMaxConn);
377                                         pos = tmpModule.indexOf(" ");
378                                         if (pos != -1) {
379                                                 vs.protomod = tmpModule.substring(0, pos);
380                                                 vs.option = tmpModule
381                                                                 .substring(pos + 1, tmpModule.length());
382                                         } else {
383                                                 vs.protomod = tmpModule;
384                                                 vs.option = "";
385                                         }
386                                         vs.protocol = tmpProtocol;
387                                         if (tmpQosClients.length() != 0) {
388                                                 if ((pos = tmpQosClients.indexOf('k')) != -1
389                                                                 || (pos = tmpQosClients.indexOf('K')) != -1) {
390                                                         vs.qosclient = Long.parseLong(tmpQosClients
391                                                                         .substring(0, pos));
392                                                         vs.qosclient = vs.qosclient * 1000;
393                                                 } else if ((pos = tmpQosClients.indexOf('M')) != -1
394                                                                 || (pos = tmpQosClients.indexOf('m')) != -1) {
395                                                         vs.qosclient = Long.parseLong(tmpQosClients
396                                                                         .substring(0, pos));
397                                                         vs.qosclient = vs.qosclient * 1000000;
398                                                 } else {
399                                                         vs.qosclient = Long.parseLong(tmpQosClients);
400                                                 }
401                                         }
402                                         // QoSService
403                                         if (tmpQosService.length() != 0) {
404                                                 if ((pos = tmpQosService.indexOf('K')) != -1
405                                                                 || (pos = tmpQosService.indexOf('k')) != -1) {
406                                                         vs.qosservice = Long.parseLong(tmpQosService
407                                                                         .substring(0, pos));
408                                                         vs.qosservice = vs.qosservice * 1000;
409                                                 } else if ((pos = tmpQosService.indexOf('M')) != -1
410                                                                 || (pos = tmpQosService.indexOf('m')) != -1) {
411                                                         vs.qosservice = Long.parseLong(tmpQosService
412                                                                         .substring(0, pos));
413                                                         vs.qosservice = vs.qosservice * 1000000;
414                                                 } else {
415                                                         vs.qosservice = Long.parseLong(tmpQosService);
416                                                 }
417                                         }
418                                         vs.receive = tmpReceive;
419                                         vs.service = tmpService;
420                                         vs.request = tmpRequest;
421                                         vs.sched = tmpScheduler;
422                                         pos = tmpSorryServer.indexOf(":");
423                                         if (pos != -1) {
424                                                 vs.sorryserver.host = tmpSorryServer.substring(0, pos);
425                                                 vs.sorryserver.port = tmpSorryServer.substring(pos + 1,
426                                                                 tmpSorryServer.length());
427                                                 if (! vs.sorryserver.port.matches("^\\d+$"))
428                                                         vs.sorryserver.port = GetServ.byName(vs.sorryserver.port, "tcp");
429                                         }
430                                         vs.quiescent = tmpQuiescent;
431                                         vs.upcallback = tmpRealRecoverCallback;
432                                         vs.downcallback = tmpRealDownCallback;
433                                         if (tmpCheckport.length() != 0)
434                                                 vs.checkport = Integer.parseInt(tmpCheckport);
435                                         vs.httpmethod = tmpHttpMethod;
436                                         vs.virtualhost = tmpVirtualHost;
437                                         vs.login = tmpLogin;
438                                         vs.database = tmpDatabase;
439                                         vs.passwd = tmpPassword;
440                                         if (tmpCheckType != null && 
441                                                         ((tmpCheckType.equals("connect") || tmpCheckType.equals("ping")) ||
442                                                          ((tmpCheckType.equals("negotiate") || tmpCheckType.matches("^\\d$")) &&
443                                                                          tmpService != null && (tmpService.equals("dns") || tmpService.equals("sip"))
444                                                                          )
445                                                         )
446                                                 ) {
447                                                 if (tmpCheckTimeOut.matches("^\\d+$"))
448                                                         vs.timeout = Integer.parseInt(tmpCheckTimeOut);
449                                         }
450                                         else {
451                                                 if (tmpNegotiateTimeOut.matches("^\\d+$"))
452                                                         vs.timeout = Integer.parseInt(tmpNegotiateTimeOut);
453                                         }
454                                         if (tmpCheckCount.matches("^\\d+$"))
455                                                 vs.checkcount = Integer.parseInt(tmpCheckCount);
456                                         vs.cfPosition = position;
457                                         position--;
458                                         data.virtualSettings.add(vs);
459                                         tmpCheckTimeOut = "";
460                                         tmpCheckCount = "";
461                                         tmpNegotiateTimeOut = "";
462                                         tmpQuiescent = "";
463                                         tmpQosClients = "";
464                                         tmpQosService = "";
465                                         tmpSorryServer = "";
466                                         tmpMaxConn = "";
467                                         tmpProtocol = "";
468                                         tmpScheduler = "";
469                                         tmpReceive = "";
470                                         tmpRequest = "";
471                                         tmpService = "";
472                                         tmpModule = "";
473                                         tmpReal = new Vector<String>();
474                                         tmpCheckType = "";
475                                         tmpCheckport = "";
476                                         tmpHttpMethod = "";
477                                         tmpVirtualHost = "";
478                                         tmpPassword = "";
479                                         tmpLogin = "";
480                                         tmpDatabase = "";
481                                         tmpRealDownCallback = "";
482                                         tmpRealRecoverCallback = "";
483                                 }// keyif if
484                         }// while(stack)
485                         data.autoreload = tmpGlobalAutoreaload;
486                         data.callback = tmpGlobalCallback;
487                         data.checkCount = tmpGlobalCheckCount;
488                         data.checkInterval = tmpGlobalCheckInterval;
489                         data.checkTimeOut = tmpGlobalCheckTimeOut;
490                         data.fallback = tmpGlobalFallback;
491                         data.logfile = tmpGlobalLogFile;
492                         data.execute = tmpGlobalExecute;
493                         data.negotiateTimeOut = tmpGlobalNegotiateTimeOut;
494                         data.quiescent = tmpGlobalQuiescent;
495                         data.supervised = tmpGlobalSupervised;
496                 } catch (Exception e) {
497                         org.ultramonkey.l7.model.FileNotReadException ex = new org.ultramonkey.l7.model.FileNotReadException();
498                         throw ex;
499                 }
500
501                 return data;
502         }
503
504         /**
505          * 
506          * <p>
507          * buildNewConf method
508          * </p>
509          * 
510          * @param in
511          * @return new configuration lines
512          * @throws org.ultramonkey.l7.model.FileNotWriteException
513          */
514         protected LinkedList<String> buildNewConf(DirectorData in)
515                         throws org.ultramonkey.l7.model.FileNotWriteException {
516                 LinkedList<String> datas = new LinkedList();
517                 try {
518                         java.io.FileReader file = new java.io.FileReader(filename);
519                         java.io.BufferedReader reader = new java.io.BufferedReader(file);
520                         String buffer;
521                         while ((buffer = reader.readLine()) != null) {
522                                 datas.addLast(buffer);
523                         }
524                 } catch (Exception e) {
525                         org.ultramonkey.l7.model.FileNotWriteException ex = new org.ultramonkey.l7.model.FileNotWriteException();
526                         ex.setErrMessage("Cannot open file : " + filename);
527                         throw ex;
528                 }
529
530                 try {
531                         HashMap<String, String> map = new HashMap<String, String>();
532                         map.put(CHECKTIMEOUT_KEY, in.checkTimeOut);
533                         map.put(CHECKINTERVAL_KEY, in.checkInterval);
534                         map.put(CHECKCOUNT_KEY, in.checkCount);
535                         map.put(FALLBACK_KEY, in.fallback);
536                         if (in.autoreload)
537                                 map.put(AUTORELOAD_KEY, "yes");
538                         else
539                                 map.put(AUTORELOAD_KEY, "no");
540                         if (in.logfile != null && in.logfile.length() > 0)
541                                 map.put(LOGFILE_KEY, "\"" + in.logfile + "\"");
542                         else
543                                 map.put(LOGFILE_KEY, null);
544                         if (in.execute != null && in.execute.length() > 0)
545                                 map.put(EXECUTE_KEY, "\"" + in.execute + "\"");
546                         else
547                                 map.put(EXECUTE_KEY, null);
548                         map.put(QUIESCENT_KEY, in.quiescent);
549                         if (in.callback != null && in.callback.length() > 0)
550                                 map.put(CALLBACK_KEY, "\"" + in.callback + "\"");
551                         else
552                                 map.put(CALLBACK_KEY, null);
553                         map.put(NEGOTIATE_TIMEOUT_KEY, in.negotiateTimeOut);
554                         if (in.supervised != null && in.supervised.equals("yes"))
555                                 map.put(SUPERVISED_KEY, in.supervised);
556                         else
557                                 map.put(SUPERVISED_KEY, null);
558
559                         // global datas
560                         for (Iterator<String> itr = map.keySet().iterator(); itr.hasNext();) {
561                                 String key = itr.next();
562                                 String value = map.get(key);
563                                 int commentLines = 0;
564                                 boolean inserted = false;
565                                 for (int i = 0; i < datas.size(); ++i) {
566                                         String line = datas.get(i);
567                                         String buffer = line.replaceAll("\\s+$", "");
568                                         if (buffer.length() == 0 || buffer.matches("^\\s*#.*")) {
569                                                 commentLines++;
570                                                 continue;
571                                         }
572                                         else if (buffer.indexOf(VIRTUAL_KEY) == 0) {
573                                                 if (value != null && value.length() > 0) {
574                                                         StringBuffer buf = new StringBuffer(key);
575                                                         if (! key.equals(SUPERVISED_KEY)) {
576                                                                 buf.append("=");
577                                                                 buf.append(value);
578                                                         }
579                                                         datas.add(i - commentLines, buf.toString());
580                                                 }
581                                                 inserted = true;
582                                                 break;
583                                         }
584                                         commentLines = 0;
585                                         Pair<String, String> pair = paseKey(buffer);
586                                         if (pair.first().equals(key)) {
587                                                 if (value == null || ! pair.second().equals(value)) {
588                                                         StringBuffer buf = new StringBuffer("#");
589                                                         buf.append(buffer);
590                                                         datas.set(i, buf.toString());
591                                                         if (value != null && value.length() > 0) {
592                                                                 buf = new StringBuffer();
593                                                                 buf.append(key);
594                                                                 if (! key.equals(SUPERVISED_KEY)) {
595                                                                         buf.append("=");
596                                                                         buf.append(value);
597                                                                 }
598                                                                 datas.add(i, buf.toString());
599                                                         }
600                                                 }
601                                                 inserted = true;
602                                                 break;
603                                         }
604                                 }
605                                 if (inserted == false) {
606                                         if (value != null && value.length() > 0) {
607                                                 StringBuffer buf = new StringBuffer(key);
608                                                 if (! key.equals(SUPERVISED_KEY)) {
609                                                         buf.append("=");
610                                                         buf.append(value);
611                                                 }
612                                                 datas.add(buf.toString());
613                                         }
614                                 }
615                         }
616                         // virtual settings
617                         DirectorData currentData = null;
618                         currentData = this.loadFromFile();
619                         LinkedList<Integer> delVsPosition = new LinkedList<Integer>();
620                         LinkedList<VirtualSetting> addVs = new LinkedList<VirtualSetting>();
621                         for (Iterator<VirtualSetting> itr = in.virtualSettings
622                                         .iterator(); itr.hasNext();) {
623                                 VirtualSetting vs = itr.next();
624                                 boolean sameFlag = false;
625                                 for (Iterator<VirtualSetting> curItr = currentData.virtualSettings
626                                                 .iterator(); curItr.hasNext();) {
627                                         VirtualSetting curVs = curItr.next();
628                                         if (curVs.equals(vs)) {
629                                                 sameFlag = true;
630                                                 curVs.cfPosition = 0;
631                                                 break;
632                                         }
633                                 }
634                                 if (sameFlag == false) {
635                                         addVs.add(vs);
636                                 }
637                         }
638                         for (Iterator<VirtualSetting> curItr = currentData.virtualSettings
639                                         .iterator(); curItr.hasNext();) {
640                                 VirtualSetting curVs = curItr.next();
641                                 if (curVs.cfPosition != 0)
642                                         delVsPosition.add(curVs.cfPosition);
643                         }
644
645                         int position = 0;
646                         int commentLines = 0;
647                         for (int i = 0; i < datas.size(); ++i) { 
648                                 String buffer = datas.get(i);
649                                 String line = buffer.replaceAll("\\s+$", "");
650                                 if (line.length() == 0 || line.matches("^\\s*#.*")) {
651                                         continue;
652                                 }
653                                 else if (buffer.indexOf(VIRTUAL_KEY) == 0) {
654                                         position++;
655                                         if (delVsPosition.contains(position)) {
656                                                 int vsStartLine = i;
657                                                 for (; i < datas.size(); ++i) {
658                                                         buffer = datas.get(i);
659                                                         line = buffer.replaceAll("\\s+$", "");
660                                                         if (line.length() == 0 || line.matches("^\\s*#.*")) {
661                                                                 commentLines++;
662                                                                 continue;
663                                                         }
664                                                         if (buffer.indexOf(VIRTUAL_KEY) == 0 && i != vsStartLine) {
665                                                                 i -= 1 + commentLines;
666                                                                 commentLines = 0;
667                                                                 break;
668                                                         }
669                                                         commentLines = 0;
670                                                         StringBuffer buf = new StringBuffer("#");
671                                                         buf.append(datas.get(i));
672                                                         datas.set(i, buf.toString());
673                                                 }
674                                         }
675                                 }
676                         }
677                         for (VirtualSetting vs : addVs) {
678                                 datas.add(VIRTUAL_KEY + "=" + vs.virtual.host
679                                                 + ":" + vs.virtual.port);
680                                 if (vs.real.size() != 0) {
681                                         for (java.util.Iterator<EndPoint> realitr = vs.real
682                                                         .iterator(); realitr.hasNext();) {
683                                                 EndPoint endpoint = realitr.next();
684                                                 datas.add("\t" + REAL_KEY + "=" + endpoint.host
685                                                                 + ":" + endpoint.port + " masq "
686                                                                 + String.valueOf(endpoint.weight));
687                                         }
688                                 }
689                                 if (vs.checktype.length() != 0)
690                                         datas.add("\t" + CHECKTYPE_KEY + "=" + vs.checktype);
691                                 if (vs.checkport != 0)
692                                         datas.add("\t" + CHECKPORT_KEY + "=" + vs.checkport);
693                                 if (vs.maxconn != 0)
694                                         datas.add("\t" + MAXCONN_KEY + "="
695                                                         + String.valueOf(vs.maxconn));
696                                 if (vs.protomod.length() != 0)
697                                         datas.add("\t" + MODULE_KEY + "=" + vs.protomod + " "
698                                                         + vs.option);
699                                 if (vs.qosclient != 0) {
700                                         StringBuffer buf = new StringBuffer();
701                                         if (vs.qosclient > 1000000) {
702                                                 buf.append(vs.qosclient / 1000000);
703                                                 buf.append('M');
704                                         } else if (vs.qosclient > 1000) {
705                                                 buf.append(vs.qosclient / 1000);
706                                                 buf.append('K');
707                                         } else
708                                                 buf.append(vs.qosclient);
709                                         datas.add("\t" + QOSCLIENTS_KEY + "="
710                                                         + buf.toString());
711                                 }
712                                 if (vs.qosservice != 0) {
713                                         StringBuffer buf = new StringBuffer();
714                                         if (vs.qosservice > 1000000) {
715                                                 buf.append(vs.qosservice / 1000000);
716                                                 buf.append('M');
717                                         } else if (vs.qosservice > 1000) {
718                                                 buf.append(vs.qosservice / 1000);
719                                                 buf.append('K');
720                                         } else
721                                                 buf.append(vs.qosservice);
722                                         datas.add("\t" + QOSSERVICE_KEY + "="
723                                                         + buf.toString());
724                                 }
725                                 if (vs.receive.length() != 0)
726                                         datas.add("\t" + RECEIVE_KEY + "=\"" + vs.receive + "\"");
727                                 if (vs.service.length() != 0)
728                                         datas.add("\t" + SERVICE_KEY + "=\"" + vs.service + "\"");
729                                 if (vs.request.length() != 0)
730                                         datas.add("\t" + REQUEST_KEY + "=\"" + vs.request + "\"");
731                                 if (vs.sched.length() != 0)
732                                         datas.add("\t" + SCHEDULER_KEY + "=" + vs.sched);
733                                 if (vs.sorryserver.host.length() != 0)
734                                         datas.add("\t" + SORRYSERVER_KEY + "="
735                                                         + vs.sorryserver.host + ":" + vs.sorryserver.port);
736                                 if (vs.httpmethod.length() != 0)
737                                         datas.add("\t" + HTTPMETHOD_KEY + "=" + vs.httpmethod);
738                                 if (vs.virtualhost.length() != 0)
739                                         datas.add("\t" + VIRTUALHOST_KEY + "=\"" + vs.virtualhost + "\"");
740                                 if (vs.passwd.length() != 0)
741                                         datas.add("\t" + PASSWORD_KEY + "=\"" + vs.passwd + "\"");
742                                 if (vs.login.length() != 0)
743                                         datas.add("\t" + LOGIN_KEY + "=\"" + vs.login + "\"");
744                                 if (vs.database.length() != 0)
745                                         datas.add("\t" + DATABASE_KEY + "=\"" + vs.database + "\"");
746                                 if (vs.downcallback.length() != 0)
747                                         datas.add("\t" + REALDOWNCALLBACK_KEY + "=\""
748                                                         + vs.downcallback + "\"");
749                                 if (vs.upcallback.length() != 0)
750                                         datas.add("\t" + REALRECOVERCALLBACK_KEY + "=\""
751                                                         + vs.upcallback + "\"");
752 //                              if (vs.checkcount != 0)
753 //                                      datas.add("\t" + CHECKCOUNT_KEY + "=" + vs.checkcount);
754                                 if (vs.timeout != 0) {
755                                         if (vs.checktype != null && 
756                                                         ((vs.checktype.equals("connect") || vs.checktype.equals("ping")) ||
757                                                          ((vs.checktype.equals("negotiate") || vs.checktype.matches("^\\d$")) &&
758                                                                          vs.service != null && (vs.service.equals("dns") || vs.service.equals("sip"))
759                                                                          )
760                                                         )
761                                                 ) {
762                                                 datas.add("\t" + CHECKTIMEOUT_KEY + "=" + vs.timeout);
763                                         }
764                                         else {
765                                                 datas.add("\t" + NEGOTIATE_TIMEOUT_KEY + "=" + vs.timeout);
766                                         }
767                                 }
768                                 if (vs.quiescent.length() != 0)
769                                         datas.add("\t" + QUIESCENT_KEY + "=" + vs.quiescent);
770                                 if (vs.protocol.length() != 0)
771                                         datas.add("\t" + PROTOCOL_KEY + "=" + vs.protocol);
772                                 datas.add("");
773                         }
774                 } catch (Exception e) {
775                         org.ultramonkey.l7.model.FileNotWriteException ex = new org.ultramonkey.l7.model.FileNotWriteException();
776                         ex.setErrMessage("Cannot open file : " + filename
777                                         + "BaseException : " + e.getMessage());
778                         throw ex;
779                 }
780                 
781                 return datas;
782         }
783
784         /**
785          * 
786          * <p>
787          * saveToFile method
788          * </p>
789          * 
790          * @param in
791          * @throws org.ultramonkey.l7.model.FileNotWriteException
792          */
793         protected void saveToFile(DirectorData in)
794                         throws org.ultramonkey.l7.model.FileNotWriteException {
795                 LinkedList<String> datas = this.buildNewConf(in);
796                 try {
797                         java.io.File file = new java.io.File(filename);
798                         if (file.exists()) {
799                                 Date date = new Date();
800                                 java.io.File backfile = new java.io.File(filename + ".bak."
801                                                 + String.valueOf(date.getTime()));
802                                 file.renameTo(backfile);
803                                 file = new java.io.File(filename);
804                         }
805                         java.io.FileWriter writer = new java.io.FileWriter(file);
806                         for (Iterator<String> itr = datas.iterator(); itr
807                                         .hasNext();) {
808                                 StringBuffer buf = new StringBuffer(itr.next());
809                                 buf.append('\n');
810                                 writer.write(buf.toString());
811                         }
812                         writer.flush();
813                         writer.close();
814                 } catch (Exception e) {
815                         org.ultramonkey.l7.model.FileNotWriteException ex = new org.ultramonkey.l7.model.FileNotWriteException();
816                         ex.setErrMessage("Cannot write file : " + filename);
817                         throw ex;
818                 }
819         }
820
821         /**
822          * 
823          * <p>
824          * syntaxCheck method
825          * </p>
826          * 
827          * @param in
828          * @throws org.ultramonkey.l7.model.FileNotWriteException
829          */
830         protected String syntaxCheck(DirectorData in)
831                         throws org.ultramonkey.l7.model.FileNotWriteException {
832                 StringBuffer result = new StringBuffer();
833                 LinkedList<String> datas = this.buildNewConf(in);
834                 try {
835                         Date date = new Date();
836                         String tmpFile = filename + "." + String.valueOf(date.getTime());
837                         File file = new java.io.File(tmpFile);
838                         FileWriter writer = new FileWriter(file);
839                         for (Iterator<String> itr = datas.iterator(); itr
840                                         .hasNext();) {
841                                 StringBuffer buf = new StringBuffer(itr.next());
842                                 buf.append('\n');
843                                 writer.write(buf.toString());
844                         }
845                         writer.flush();
846                         writer.close();
847                         
848                         // TODO sudo
849                         String command = "sudo " + l7directord + " -t " + tmpFile;
850                         Process p = Runtime.getRuntime().exec(command);
851                         InputStream stderr = p.getErrorStream();
852                         BufferedReader br = new BufferedReader(new InputStreamReader(stderr));
853                         String line = null;
854                         while ((line = br.readLine()) != null) {
855                                 result.append(line + "\n");
856                         }
857                         InputStream is = p.getInputStream();
858                         br = new BufferedReader(new InputStreamReader(is));
859                         while ((line = br.readLine()) != null) {
860                                 result.append(line + "\n");
861                         }
862                         
863                         file.delete();
864                 } catch (Exception e) {
865                         org.ultramonkey.l7.model.FileNotWriteException ex = new org.ultramonkey.l7.model.FileNotWriteException();
866                         ex.setErrMessage("Cannot write file : " + filename);
867                         throw ex;
868                 }
869                 
870                 return result.toString();
871         }
872
873         /**
874          * get DirectorData from l7directord.cf
875          * <p>
876          * getDirectorData method
877          * </p>
878          * 
879          * @return DirectorData l7directord.cf datas
880          * @throws org.ultramonkey.l7.model.FileNotReadException
881          */
882         public DirectorData getDirectorData()
883                         throws org.ultramonkey.l7.model.FileNotReadException {
884                 // --- debug log (in method) ---
885                 Logger ioFileLogger = Logger.getLogger(LogCategorySet.GUI_IO_FILE);
886                 if (ioFileLogger.isDebugEnabled()) {
887             ioFileLogger.debug("11575 DirectorController::getDirectorData() in");
888                 }
889                 // --- debug log (in method) ---
890
891                 DirectorData data = null;
892                 synchronized (this) {
893                         if (serchfile()) {
894                                 data = loadFromFile();
895                         }
896                 }
897
898                 // --- debug log (out method) ---
899                 if (ioFileLogger.isDebugEnabled()) {
900             ioFileLogger.debug("11576 DirectorController::getDirectorData() out return=(" + data + ")");
901                 }
902                 // --- debug log (out method) ---
903                 return data;
904         }
905
906         /**
907          * set DirectorData to l7directord.cf
908          * <p>
909          * setDirectorData method
910          * </p>
911          * 
912          * @param in
913          */
914         public void setDirectorData(DirectorData in)
915                         throws org.ultramonkey.l7.model.FileNotWriteException {
916                 // --- debug log (in method) ---
917                 Logger ioFileLogger = Logger.getLogger(LogCategorySet.GUI_IO_FILE);
918                 if (ioFileLogger.isDebugEnabled()) {
919             ioFileLogger.debug("11577 DirectorController::setDirectorData(DirectorData in) in in=(" + in + ")");
920                 }
921                 // --- debug log (in method) ---
922
923                 synchronized (this) {
924                         if (in != null && serchfile()) {
925                                 saveToFile(in);
926                         }
927                 }
928
929                 // --- debug log (out method) ---
930                 if (ioFileLogger.isDebugEnabled()) {
931             ioFileLogger.debug("11578 DirectorController::setDirectorData(DirectorData in) out");
932                 }
933                 // --- debug log (out method) ---
934         }
935
936         /**
937          * check new l7directord.cf
938          * <p>
939          * checkDirectorData method
940          * </p>
941          * 
942          * @param in
943          */
944         public String checkDirectorData(DirectorData in)
945                         throws org.ultramonkey.l7.model.FileNotWriteException {
946                 // --- debug log (in method) ---
947                 Logger ioFileLogger = Logger.getLogger(LogCategorySet.GUI_IO_FILE);
948                 if (ioFileLogger.isDebugEnabled()) {
949             ioFileLogger.debug("11579 DirectorController::checkDirectorData(DirectorData in) in in=(" + in + ")");
950                 }
951                 // --- debug log (in method) ---
952
953                 synchronized (this) {
954                         if (in != null && serchfile()) {
955                                 String ret = this.syntaxCheck(in);
956                                 
957                                 // --- debug log (out method) ---
958                                 if (ioFileLogger.isDebugEnabled()) {
959                             ioFileLogger.debug("11580 DirectorController::checkDirectorData(DirectorData in) out return=\"" + ret + "\"");
960                                 }
961                                 // --- debug log (out method) ---
962                                 return ret;
963                         }
964
965                         // --- debug log (out method) ---
966                         if (ioFileLogger.isDebugEnabled()) {
967                     ioFileLogger.debug("11581 DirectorController::checkDirectorData(DirectorData in) out return=null");
968                         }
969                         // --- debug log (out method) ---
970                         return null;
971                 }
972         }
973 }