OSDN Git Service

Initial commit
[ultramonkey-l7/l7gui.git] / src / src / org / ultramonkey / l7 / controller / VirtualSetting.java
1 package org.ultramonkey.l7.controller;
2
3 import java.io.Serializable;
4 import java.util.Vector;
5 import java.util.regex.*;
6
7 /**
8  * 
9  * <p>
10  * class VirtualSetting
11  * </p>
12  * <p>
13  * Copyright(c) NTT COMWARE 2008
14  * </p>
15  * 
16  * @author tanuma
17  */
18 public class VirtualSetting implements Serializable {
19         static final long serialVersionUID = 1;
20
21         /**
22          * virtual service
23          */
24         public EndPoint virtual;
25
26         /**
27          * real server
28          */
29         public Vector<EndPoint> real;
30
31         /**
32          * protocol module name
33          */
34         public String protomod = "";
35
36         /**
37          * protocol module option strings
38          */
39         public String option = "";
40
41         /**
42          * protocol module reschedule flag
43          */
44         public boolean reschedule;
45
46         /**
47          * scheduler name
48          */
49         public String sched = "";
50
51         /**
52          * max connection setting
53          */
54         public int maxconn;
55
56         /**
57          * QoS limit per virtual service
58          */
59         public long qosservice;
60
61         /**
62          * QoS limit per client
63          */
64         public long qosclient;
65
66         /**
67          * current throughput
68          */
69         public long throughput;
70
71         /**
72          * sorry server
73          */
74         public EndPoint sorryserver;
75
76         /**
77          * sorry flag
78          */
79         public boolean sorryflag;
80
81         /**
82          * timeout of real server monitor
83          */
84         public int timeout;
85
86         /**
87          * check count of real server monitor
88          */
89         public int checkcount;
90
91         /**
92          * connection type of real server monitor
93          */
94         public String checktype = "";
95
96         /**
97          * service type of real server monitor
98          */
99         public String service = "";
100
101         /**
102          * quiescent flag
103          */
104         public String quiescent = "";
105
106         /**
107          * check port of real server monitor
108          */
109         public int checkport;
110
111         /**
112          * request string of real server monitor
113          */
114         public String request = "";
115
116         /**
117          * response string of real server monitor
118          */
119         public String receive = "";
120
121         /**
122          * HTTP method of real server monitor
123          */
124         public String httpmethod = "";
125
126         /**
127          * HTTP virtual host of real server monitor
128          */
129         public String virtualhost = "";
130
131         /**
132          * login user name for real server monitor
133          */
134         public String login = "";
135
136         /**
137          * password for real server monitor
138          */
139         public String passwd = "";
140
141         /**
142          * database name for real server monitor
143          */
144         public String database = "";
145
146         /**
147          * protocol name
148          */
149         public String protocol = "";
150         
151         /**
152          * real server down callback script file path
153          */
154         public String downcallback = "";
155
156         /**
157          * real server up callback script file path
158          */
159         public String upcallback = "";
160
161         /**
162          * virtual position in l7directord.cf
163          */
164         int cfPosition = 0;
165
166         /**
167          * 
168          * Constructor
169          * 
170          */
171         public VirtualSetting() {
172                 this.virtual = new EndPoint();
173                 this.real = new Vector<EndPoint>();
174                 this.sorryserver = new EndPoint();
175         }
176
177         /**
178          * 
179          * Copy Constructor
180          * 
181          * @param v
182          */
183         public VirtualSetting(VirtualSetting v) {
184                 this.virtual = new EndPoint(v.virtual);
185                 this.real = (Vector<EndPoint>) v.real.clone();
186                 this.protomod = new String(v.protomod);
187                 this.option = new String(v.option);
188                 this.sched = new String(v.sched);
189                 this.maxconn = v.maxconn;
190                 this.qosservice = v.qosservice;
191                 this.qosclient = v.qosclient;
192                 this.sorryserver = new EndPoint(v.sorryserver);
193                 this.sorryflag = v.sorryflag;
194                 this.timeout = v.timeout;
195                 this.checkcount = v.checkcount;
196                 this.checktype = new String(v.checktype);
197                 this.service = new String(v.service);
198                 this.quiescent = new String(v.quiescent);
199                 this.checkport = v.checkport;
200                 this.request = new String(v.request);
201                 this.receive = new String(v.receive);
202                 this.httpmethod = new String(v.httpmethod);
203                 this.virtualhost = new String(v.virtualhost);
204                 this.login = new String(v.login);
205                 this.passwd = new String(v.passwd);
206                 this.database = new String(v.database);
207                 this.protocol = new String(v.protocol);
208                 this.downcallback = new String(v.downcallback);
209                 this.upcallback = new String(v.upcallback);
210         }
211
212         /**
213          * 
214          * <p>
215          * complementOption method
216          * </p>
217          * 
218          * @param vs
219          * @return
220          */
221         public void complementOption() {
222                 String fullOption = "";
223                 if (this.protomod == null || this.option == null)
224                         return;
225                 if (this.protomod.equals("cinsert")) {
226                         Pattern p = Pattern.compile("(?:--cookie-name|-C)\\s+([^\\s]+)");
227                         Matcher m = p.matcher(this.option);
228                         if (m.find())
229                                 fullOption = "--cookie-name " + m.group(1);
230                         else {
231                                 fullOption = "--cookie-name CookieName";
232                         }
233                         p = Pattern.compile("(?:--cookie-expire|-E)\\s+(\\d+)");
234                         m = p.matcher(this.option);
235                         if (m.find())
236                                 fullOption += " --cookie-expire " + m.group(1);
237                         else {
238                                 fullOption += " --cookie-expire 86400";
239                         }
240                         p = Pattern.compile("(?:--forwarded-for|-F)");
241                         m = p.matcher(this.option);
242                         if (m.find())
243                                 fullOption += " --forwarded-for";
244                         p = Pattern.compile("(?:--reschedule|-R)");
245                         m = p.matcher(this.option);
246                         if (m.find())
247                                 fullOption += " --reschedule";
248                         p = Pattern.compile("(?:--no-reschedule|-N)");
249                         m = p.matcher(this.option);
250                         if (m.find())
251                                 fullOption += " --no-reschedule";
252                 }
253                 else if (this.protomod.equals("url")) {
254                         Pattern p = Pattern.compile("(?:--pattern-match|-P)\\s+([^\\s]+)");
255                         Matcher m = p.matcher(this.option);
256                         if (m.find())
257                                 fullOption = "--pattern-match " + m.group(1);
258                         p = Pattern.compile("(?:--forwarded-for|-F)");
259                         m = p.matcher(this.option);
260                         if (m.find())
261                                 fullOption += " --forwarded-for";
262                 }
263                 else if (this.protomod.equals("sessionless")) {
264                         Pattern p = Pattern.compile("(?:--forwarded-for|-F)");
265                         Matcher m = p.matcher(this.option);
266                         if (m.find())
267                                 fullOption += "--forwarded-for";
268                 }
269                 else if (this.protomod.equals("sslid")) {
270                         Pattern p = Pattern.compile("(?:--timeout|-T)\\s+(\\d+)");
271                         Matcher m = p.matcher(this.option);
272                         if (m.find())
273                                 fullOption += "--timeout " + m.group(1);
274                         else {
275                                 fullOption += "--timeout 3600";
276                         }
277                         p = Pattern.compile("(?:--maxlist|-M)\\s+(\\d+)");
278                         m = p.matcher(this.option);
279                         if (m.find())
280                                 fullOption += " --maxlist " + m.group(1);
281                         else {
282                                 fullOption += " --maxlist 1024";
283                         }
284                         p = Pattern.compile("(?:--reschedule|-R)");
285                         m = p.matcher(this.option);
286                         if (m.find())
287                                 fullOption += " --reschedule";
288                         p = Pattern.compile("(?:--no-reschedule|-N)");
289                         m = p.matcher(this.option);
290                         if (m.find())
291                                 fullOption += " --no-reschedule";
292                 }
293                 this.option = fullOption;
294         }
295
296         /**
297          * 
298          * <p>
299          * isSameService method
300          * </p>
301          * 
302          * @param vs
303          * @return
304          */
305         public boolean isSameService(VirtualSetting vs) {
306                 if (this.virtual.equals(vs.virtual) && this.isSameKey(vs)) {
307                         return true;
308                 }
309                 return false;
310         }
311
312         /**
313          * 
314          * <p>
315          * isSameKey method
316          * </p>
317          * 
318          * @param vs
319          * @return
320          */
321         public boolean isSameKey(VirtualSetting vs) {
322                 if (this.protomod.equals(vs.protomod)) {
323                         String key1 = this.getKey();
324                         String key2 = vs.getKey();
325                         if (key1 != null && key2 != null && key1.equals(key2))
326                                 return true;
327                 }
328                 return false;
329         }
330
331         /**
332          * 
333          * <p>
334          * getKey method
335          * </p>
336          * 
337          * @return
338          */
339         public String getKey() {
340                 if (this.protomod == null || this.option == null)
341                         return null;
342                 this.complementOption();
343                 if (this.protomod.equals("cinsert")) {
344                         Pattern p = Pattern.compile("--cookie-name\\s+([^\\s]+)");
345                         Matcher m = p.matcher(this.option);
346                         if (m.find())
347                                 return m.group(1).replaceAll("['\"]", "");
348                         else 
349                                 return null;
350                 }
351                 if (this.protomod.equals("url")) {
352                         Pattern p = Pattern.compile("--pattern-match\\s+([^\\s]+)");
353                         Matcher m = p.matcher(this.option);
354                         if (m.find())
355                                 return m.group(1).replaceAll("['\"]", "");
356                         else
357                                 return null;
358                 }
359                 if (this.protomod.equals("sslid"))
360                         return "";
361                 if (this.protomod.equals("sessionless"))
362                         return "";
363                 
364                 return null;
365         }
366
367         /**
368          * 
369          * <p>
370          * equals method
371          * </p>
372          * 
373          * @param vs
374          * @return
375          */
376         public boolean equals(VirtualSetting vs) {
377                 this.complementOption();
378                 vs.complementOption();
379                 if (this.virtual.equals(vs.virtual)
380                                 && this.protomod.equals(vs.protomod)
381                                 && this.option.equals(vs.option)
382                                 && this.sched.equals(vs.sched)
383                                 && this.maxconn == vs.maxconn
384                                 && this.qosservice == vs.qosservice
385                                 && this.qosclient == vs.qosclient
386                                 && this.sorryserver.equals(vs.sorryserver)
387                                 && this.sorryflag == vs.sorryflag
388                                 && this.timeout == vs.timeout
389                                 && this.checkcount == vs.checkcount
390                                 && this.checktype.equals(vs.checktype)
391                                 && this.service.equals(vs.service)
392                                 && this.quiescent == vs.quiescent
393                                 && this.checkport == vs.checkport
394                                 && this.request.equals(vs.request)
395                                 && this.receive.equals(vs.receive)
396                                 && this.httpmethod.equals(vs.httpmethod)
397                                 && this.virtualhost.equals(vs.virtualhost)
398                                 && this.login.equals(vs.login) && this.passwd.equals(vs.passwd)
399                                 && this.database.equals(vs.database)
400                                 && this.protocol.equals(vs.protocol)
401                                 && this.downcallback.equals(vs.downcallback)
402                                 && this.upcallback.equals(vs.upcallback)) {
403                         if (this.real == null && vs.real == null)
404                                 return true;
405                         if (this.real.size() == vs.real.size()) {
406                                 int sameCount = 0;
407                                 for (EndPoint e1: this.real) {
408                                         for (EndPoint e2: vs.real) {
409                                                 if (e1.equals(e2)) {
410                                                         sameCount++;
411                                                 }
412                                         }
413                                 }
414                                 if (sameCount == this.real.size())
415                                         return true;
416                         }
417                 }
418                 return false;
419         }
420
421     /**
422      * 
423      * <p>
424      * equalsAdm method
425      * </p>
426      * 
427      * @param vs
428      * @return
429      */
430         public boolean equalsAdm(VirtualSetting vs) {
431                 this.complementOption();
432                 vs.complementOption();
433                 if (this.virtual.equals(vs.virtual)
434                                 && this.protomod.equals(vs.protomod)
435                                 && this.option.equals(vs.option) && this.sched.equals(vs.sched)
436                                 && this.maxconn == vs.maxconn
437                                 && this.qosservice == vs.qosservice
438                                 && this.qosclient == vs.qosclient
439                                 && this.sorryserver.equals(vs.sorryserver)) {
440                         if (this.real == null && vs.real == null)
441                                 return true;
442                         if (this.real.size() == vs.real.size()) {
443                                 int sameCount = 0;
444                                 for (EndPoint e1: this.real) {
445                                         for (EndPoint e2: vs.real) {
446                                                 if (e1.equals(e2)) {
447                                                         sameCount++;
448                                                 }
449                                         }
450                                 }
451                                 if (sameCount == this.real.size())
452                                         return true;
453                         }
454                 }
455                 return false;
456         }
457
458         /**
459          * 
460          * {@inheritDoc}
461          */
462         public String toString() {
463                 return 
464                         "virtual=(" + this.virtual + "), " + 
465                         "real=(" + this.real + "), " +
466                         "protomod=\"" +  this.protomod + "\", " +
467                         "option=\"" + this.option + "\", " +
468                         "sched=\"" + this.sched + "\", " +
469                         "maxconn=" + this.maxconn + ", " +
470                         "qosservice=" + this.qosservice + ", " +
471                         "qosclient=" + this.qosclient + ", " +
472                         "sorryserver=(" + this.sorryserver + "), " +
473                         "sorryflag=" + this.sorryflag + ", " + 
474                         "timeout=" + this.timeout + ", " +
475                         "checkcount=" + this.checkcount + ", " +
476                         "checktype=\"" + this.checktype + "\", " +
477                         "service=\"" + this.service + "\", " +
478                         "quiescent=\"" + this.quiescent + "\", " +
479                         "checkport=\"" + this.checkport + "\", " +
480                         "request=\"" + this.request + "\", " +
481                         "receive=\"" + this.receive + "\", " +
482                         "httpmethod=\"" + this.httpmethod + "\", " +
483                         "virtualhost=\"" + this.virtualhost + "\", " +
484                         "login=\"" + this.login + "\", " +
485                         "passwd=\"" + this.passwd + "\", " +
486                         "database=\"" + this.database + "\", " +
487                         "protocol=\"" + this.protocol + "\", " +
488                         "downcallback=\"" + this.downcallback + "\", " +
489                         "upcallback=\"" + this.upcallback + "\"";
490         }
491 }