OSDN Git Service

Initial commit
[ultramonkey-l7/l7gui.git] / src / src / org / ultramonkey / l7 / model / L7SyncCommand.java
1 package org.ultramonkey.l7.model;
2
3 import java.util.HashSet;
4
5 import org.apache.log4j.Logger;
6
7 /**
8  * <p>class L7SyncCommand</p>
9  * <p>Copyright(c) NTT COMWARE 2008</p>
10  * @author momose
11  */
12 public class L7SyncCommand {
13     protected static Logger debugLogger = Logger.getLogger(LogCategorySet.SYNC_SYNC);
14
15     protected HashSet<Integer> categorySet;
16
17     protected int serverMode = 0;
18
19     private static final int ACT_MODE = 1;
20
21     private static final int SBY_MODE = 2;
22
23     private static final int MODE_ERROR = 3;
24
25     private static final int UNDESIGNED_MODE = 0;
26
27     private int MODE_LOCATION = 0;
28
29     private int CATEGORY_START_LOCATION = MODE_LOCATION + 1;
30
31     protected Logger logger = Logger.getLogger(LogCategorySet.SYNC_SYNC);
32
33     /**
34      * Constructor
35      *
36      * @param args
37      */
38     public L7SyncCommand(String[] args) {
39         //      debug log(constructor)
40         if (debugLogger.isDebugEnabled()) {
41             debugLogger.debug("11232 class L7SyncCommand created.");
42         }
43
44         categorySet = new HashSet<Integer>();
45         try {
46             this.checkArgument(args);
47         } catch (MessageView m) {
48             System.out.println(Message.SYNC_INVALID_ARGUMENT + " L7SyncCommand.");
49             logger.info("21032 " + Message.SYNC_INVALID_ARGUMENT + " L7SyncCommand.");
50         }
51     }
52
53     /**
54      * <p> checks arguments</p>
55      *
56      * @param args argument value from command line
57      * @throws MessageView
58      */
59     protected void checkArgument(String[] args) throws MessageView {
60         //      debug log(in method)
61         if (debugLogger.isDebugEnabled()) {
62             StringBuffer buf = new StringBuffer();
63             buf
64                             .append("L7SyncCommand::checkArgument(String[] args) throws MessageView in args= ");
65             if (0 != args.length) {
66                 for (int i = 0; i < args.length; i++) {
67                     if (args.length - 1 == i) {
68                         buf.append(args[i].toString());
69                         break;
70                     }
71                     buf.append(args[i].toString() + ",");
72                 }
73             } else {
74                 buf.append("null");
75             }
76             debugLogger.debug("11233 " + buf.toString());
77         }
78
79         if (0 == args.length) {
80             setServerMode(UNDESIGNED_MODE);
81         } else if (args[MODE_LOCATION].equals("-a")) {
82             setServerMode(ACT_MODE);
83             if (args.length <= CATEGORY_START_LOCATION) {
84                 throw new MessageView(40024, LogCategorySet.SYNC_SYNC,
85                                 Message.SYNC_INVALID_ARGUMENT + " checkArgument.");
86             }
87             for (int i = CATEGORY_START_LOCATION; i < args.length; i++) {
88                 try {
89                     setCaterogyIdSet(SyncCategoryIdSet.toCategoryId(args[i]));
90                 } catch (MessageView m) {
91                     throw new MessageView(40025, LogCategorySet.SYNC_SYNC,
92                                     Message.SYNC_INVALID_ARGUMENT + " checkArgument.");
93                 }
94             }
95         } else if (args[MODE_LOCATION].equals("-s")) {
96             setServerMode(SBY_MODE);
97         } else {
98             setServerMode(MODE_ERROR);
99             throw new MessageView(40026, LogCategorySet.SYNC_SYNC,
100                             Message.SYNC_INVALID_ARGUMENT + " checkArgument.");
101         }
102
103         //      debug log(out method)
104         if (debugLogger.isDebugEnabled()) {
105             StringBuffer buf = new StringBuffer();
106             buf
107                             .append("L7SyncCommand::checkArgument(String[] args) throws MessageView out ");
108             debugLogger.debug("11234 " + buf.toString());
109         }
110
111     }
112
113     /**
114      * <p> starts L7GUI synchronous processing</p>
115      *
116      * @param args
117      */
118     public static void main(String[] args) {
119         //      debug log(in method)
120         if (debugLogger.isDebugEnabled()) {
121             StringBuffer buf = new StringBuffer();
122             buf.append("L7SyncCommand::main(String[] args) in args= ");
123             if (0 == args.length) {
124                 buf.append("null");
125             } else {
126                 for (int i = 0; i < args.length; i++) {
127                     if (args.length - 1 == i) {
128                         buf.append(args[i].toString());
129                         break;
130                     }
131                     buf.append(args[i].toString() + ",");
132                 }
133             }
134             debugLogger.debug("11235 " + buf.toString());
135         }
136
137         if (0 == args.length) {
138             L7SyncCommand.usage();
139
140         } else {
141             L7SyncCommand l7SyncCommand = new L7SyncCommand(args);
142             l7SyncCommand.start();
143         }
144         //      debug log(out method)
145         if (debugLogger.isDebugEnabled()) {
146             StringBuffer buf = new StringBuffer();
147             buf.append("L7SyncCommand::main(String[] args) out ");
148             debugLogger.debug("11236 " + buf.toString());
149         }
150
151     }
152
153     /**
154      * <p> starts active or stand-by processing</p>
155      *
156      */
157     protected void start() {
158         //      debug log(in method)
159         if (debugLogger.isDebugEnabled()) {
160             StringBuffer buf = new StringBuffer();
161             buf.append("L7SyncCommand::start() in ");
162             debugLogger.debug("11237 " + buf.toString());
163         }
164
165         L7SyncManager l7SyncManager = new L7SyncManager();
166         switch (serverMode) {
167         case ACT_MODE:
168             try {
169                 l7SyncManager.syncAct(getCategoryIdSet());
170             } catch (MessageView m) {
171                 System.out.println(m.message);
172             }
173             break;
174
175         case SBY_MODE:
176             try {
177                 l7SyncManager.syncSby();
178             } catch (MessageView m) {
179                 System.out.println(m.message);
180             }
181             break;
182         default:
183             System.out.println(Message.SYNC_INVALID_SERVER_MODE);
184             break;
185         }
186         //      debug log(out method)
187         if (debugLogger.isDebugEnabled()) {
188             StringBuffer buf = new StringBuffer();
189             buf.append("L7SyncCommand::start() out ");
190             debugLogger.debug("11238 " + buf.toString());
191         }
192
193     }
194
195     /**
196      * <p> shows how to use L7SyncCommand</p>
197      *
198      */
199     protected static void usage() {
200         //      debug log(in method)
201         if (debugLogger.isDebugEnabled()) {
202             StringBuffer buf = new StringBuffer();
203             buf.append("L7SyncCommand::usage() in ");
204             debugLogger.debug("11239 " + buf.toString());
205         }
206
207         System.out.println(
208                         "Usage: L7SyncCommand -a [CATEGORY]...\n" +
209                         "       L7SyncCommand -s\n" +
210                         "\n" +
211                         "Commands:\n" +
212                         "  -a    active mode. send specified category data to standby server.\n" +
213                         "  -s    standby mode. receive data from active server.\n" +
214                         "\n" +
215                         "Category:\n" +
216                         "  l7vsd_log         log settings of l7vsd\n" +
217                         "  adm_log           log settings of l7vsadm\n" +
218                         "  snmp_log          log settings of l7agent\n" +
219                         "  log_all           all log settings\n" +
220                         "  adm_info          command settings of l7vsadm\n" +
221                         "  replication_info  replication settings of l7vsd\n" +
222                         "  snmp_info         interval settings of l7agent\n" +
223                         "  monitor           global settings of l7directord.cf\n" +
224                         "  service           virtual settings of l7directord.cf\n" +
225                         "  userdata          user data of l7gui\n"
226                         );
227
228         //      debug log(out method)
229         if (debugLogger.isDebugEnabled()) {
230             StringBuffer buf = new StringBuffer();
231             buf.append("L7SyncCommand::usage() out ");
232             debugLogger.debug("11240 " + buf.toString());
233         }
234
235     }
236
237     /**
238      * <p> gets CategoryIdSet</p>
239      *
240      * @return categorySet
241      */
242     protected HashSet<Integer> getCategoryIdSet() {
243         //      debug log(in method)
244         if (debugLogger.isDebugEnabled()) {
245             StringBuffer buf = new StringBuffer();
246             buf.append("L7SyncCommand::getCategoryIdSet() in ");
247             debugLogger.debug("11241 " + buf.toString());
248         }
249
250         //      debug log(out method)
251         if (debugLogger.isDebugEnabled()) {
252             StringBuffer buf = new StringBuffer();
253             buf.append("L7SyncCommand::getCategoryIdSet() out ");
254             buf.append("return= " + categorySet.toString());
255             debugLogger.debug("11242 " + buf.toString());
256         }
257
258         return categorySet;
259     }
260
261     /**
262      * <p> sets CaterogyIdSet</p>
263      *
264      * @param categoryId
265      */
266     protected void setCaterogyIdSet(Integer categoryId) {
267         //      debug log(in method)
268         if (debugLogger.isDebugEnabled()) {
269             StringBuffer buf = new StringBuffer();
270             buf.append("L7SyncCommand::setCaterogyIdSet() in ");
271             debugLogger.debug("11243 " + buf.toString());
272         }
273         categorySet.add(categoryId);
274
275         //      debug log(out method)
276         if (debugLogger.isDebugEnabled()) {
277             StringBuffer buf = new StringBuffer();
278             buf.append("L7SyncCommand::setCaterogyIdSet() out ");
279             debugLogger.debug("11244 " + buf.toString());
280         }
281     }
282
283     /**
284      * <p> gets serverMode</p>
285      *
286      * @return
287      */
288     protected int getServerMode() {
289         //      debug log(in method)
290         if (debugLogger.isDebugEnabled()) {
291             StringBuffer buf = new StringBuffer();
292             buf.append("L7SyncCommand::getServerMode() in ");
293             debugLogger.debug("11245 " + buf.toString());
294         }
295         //      debug log(out method)
296         if (debugLogger.isDebugEnabled()) {
297             StringBuffer buf = new StringBuffer();
298             buf.append("L7SyncCommand::getServerMode() out ");
299             buf.append("return= " + serverMode);
300             debugLogger.debug("11246 " + buf.toString());
301         }
302         return serverMode;
303     }
304
305     /**
306      * <p> sets serverMode</p>
307      *
308      * @param mode
309      */
310     protected void setServerMode(int mode) {
311         //      debug log(in method)
312         if (debugLogger.isDebugEnabled()) {
313             StringBuffer buf = new StringBuffer();
314             buf.append("L7SyncCommand::setServerMode() in ");
315             buf.append("mode= " + mode);
316             debugLogger.debug("11247 " + buf.toString());
317         }
318         //      debug log(out method)
319         if (debugLogger.isDebugEnabled()) {
320             StringBuffer buf = new StringBuffer();
321             buf.append("L7SyncCommand::getServerMode() out ");
322             debugLogger.debug("11248 " + buf.toString());
323         }
324         serverMode = mode;
325     }
326 }