ManagerParse.java 11 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
/*
 * Copyright (c) 2013, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software;Designed and Developed mainly by many Chinese 
 * opensource volunteers. you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License version 2 only, as published by the
 * Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 * 
 * Any questions about this component can be directed to it's project Web address 
 * https://code.google.com/p/opencloudb/.
 *
 */
package io.mycat.route.parser;

import io.mycat.route.parser.util.ParseUtil;

/**
 * @author mycat
 * @author mycat
 */
public final class ManagerParse {

	public static final int OTHER = -1;
	public static final int SELECT = 1;
	public static final int SET = 2;
	public static final int SHOW = 3;
	public static final int SWITCH = 4;
	public static final int KILL_CONN = 5;
	public static final int STOP = 6;
	public static final int RELOAD = 7;
	public static final int ROLLBACK = 8;
	public static final int OFFLINE = 9;
	public static final int ONLINE = 10;
	public static final int CLEAR = 11;
	public static final int CONFIGFILE = 12;
	public static final int LOGFILE = 13;

	public static final int ZK = 14;

	public static int parse(String stmt) {
		for (int i = 0; i < stmt.length(); i++) {
			switch (stmt.charAt(i)) {
			case ' ':
				continue;
			case '/':
			case '#':
				i = ParseUtil.comment(stmt, i);
				continue;
			case 'C':
			case 'c':
				return cCheck(stmt, i);
			case 'F':
			case 'f':
				return fCheck(stmt, i);
			case 'L':
			case 'l':
				return lCheck(stmt, i);
			case 'S':
			case 's':
				return sCheck(stmt, i);
			case 'K':
			case 'k':
				return kill(stmt, i);
			case 'O':
			case 'o':
				return oCheck(stmt, i);
			case 'R':
			case 'r':
				return rCheck(stmt, i);
			case 'Z':
			case 'z':
				return zCheck(stmt,i);
			default:
				return OTHER;
			}
		}
		return OTHER;
	}

	private static int zCheck(String stmt, int offset) {
		String thePart = stmt.substring(offset).toUpperCase();
		if(thePart.startsWith("ZK")){
			return ZK;
		}else {
			return OTHER;
		}
	}

	// show LOG check
	private static int lCheck(String stmt, int offset) {
		String thePart = stmt.substring(offset).toUpperCase();
		if (thePart.startsWith("LOG @@")) {
			return LOGFILE;
		} else {
			return OTHER;
		}
	}

	// config file check
	private static int fCheck(String stmt, int offset) {
		String thePart = stmt.substring(offset).toUpperCase();
		if (thePart.startsWith("FILE @@")) {
			return CONFIGFILE;
		}
		return OTHER;
	}

	// CLEAR or config file
	private static int cCheck(String stmt, int offset) {
		if (stmt.length() > offset + "LEAR ".length()) {
			char c1 = stmt.charAt(++offset);
			char c2 = stmt.charAt(++offset);
			char c3 = stmt.charAt(++offset);
			char c4 = stmt.charAt(++offset);
			char c5 = stmt.charAt(++offset);
			if ((c1 == 'L' || c1 == 'l') && (c2 == 'E' || c2 == 'e')
					&& (c3 == 'A' || c3 == 'a') && (c4 == 'R' || c4 == 'r')
					&& (c5 == ' ' || c5 == '\t' || c5 == '\r' || c5 == '\n')) {
				return (offset << 8) | CLEAR;
			}
		}
		return OTHER;
	}

	private static int oCheck(String stmt, int offset) {
		if (stmt.length() > ++offset) {
			switch (stmt.charAt(offset)) {
			case 'F':
			case 'f':
				return ofCheck(stmt, offset);
			case 'N':
			case 'n':
				return onCheck(stmt, offset);
			default:
				return OTHER;
			}
		}
		return OTHER;
	}

	private static int onCheck(String stmt, int offset) {
		if (stmt.length() > offset + "line".length()) {
			char c1 = stmt.charAt(++offset);
			char c2 = stmt.charAt(++offset);
			char c3 = stmt.charAt(++offset);
			char c4 = stmt.charAt(++offset);
			if ((c1 == 'l' || c1 == 'L')
					&& (c2 == 'i' || c2 == 'I')
					&& (c3 == 'n' || c3 == 'N')
					&& (c4 == 'e' || c4 == 'E')
					&& (stmt.length() == ++offset || ParseUtil.isEOF(stmt
							.charAt(offset)))) {
				return ONLINE;
			}
		}
		return OTHER;
	}

	private static int ofCheck(String stmt, int offset) {
		if (stmt.length() > offset + "fline".length()) {
			char c1 = stmt.charAt(++offset);
			char c2 = stmt.charAt(++offset);
			char c3 = stmt.charAt(++offset);
			char c4 = stmt.charAt(++offset);
			char c5 = stmt.charAt(++offset);
			if ((c1 == 'f' || c1 == 'F')
					&& (c2 == 'l' || c2 == 'L')
					&& (c3 == 'i' || c3 == 'I')
					&& (c4 == 'n' || c4 == 'N')
					&& (c5 == 'e' || c5 == 'E')
					&& (stmt.length() == ++offset || ParseUtil.isEOF(stmt
							.charAt(offset)))) {
				return OFFLINE;
			}
		}
		return OTHER;
	}

	private static int sCheck(String stmt, int offset) {
		if (stmt.length() > ++offset) {
			switch (stmt.charAt(offset)) {
			case 'E':
			case 'e':
				return seCheck(stmt, offset);
			case 'H':
			case 'h':
				return show(stmt, offset);
			case 'W':
			case 'w':
				return swh(stmt, offset);
			case 'T':
			case 't':
				return stop(stmt, offset);
			default:
				return OTHER;
			}
		}
		return OTHER;
	}

	private static int seCheck(String stmt, int offset) {
		if (stmt.length() > ++offset) {
			switch (stmt.charAt(offset)) {
			case 'L':
			case 'l':
				return select(stmt, offset);
			case 'T':
			case 't':
				if (stmt.length() > ++offset) {
					char c = stmt.charAt(offset);
					if (c == ' ' || c == '\r' || c == '\n' || c == '\t'
							|| c == '/' || c == '#') {
						return SET;
					}
				}
				return OTHER;
			default:
				return OTHER;
			}
		}
		return OTHER;
	}

	private static int rCheck(String stmt, int offset) {
		if (stmt.length() > ++offset) {
			switch (stmt.charAt(offset)) {
			case 'E':
			case 'e':
				return reload(stmt, offset);
			case 'O':
			case 'o':
				return rollback(stmt, offset);
			default:
				return OTHER;
			}
		}
		return OTHER;
	}

	// RELOAD' '
	private static int reload(String stmt, int offset) {
		if (stmt.length() > offset + 5) {
			char c1 = stmt.charAt(++offset);
			char c2 = stmt.charAt(++offset);
			char c3 = stmt.charAt(++offset);
			char c4 = stmt.charAt(++offset);
			char c5 = stmt.charAt(++offset);
			if ((c1 == 'L' || c1 == 'l') && (c2 == 'O' || c2 == 'o')
					&& (c3 == 'A' || c3 == 'a') && (c4 == 'D' || c4 == 'd')
					&& (c5 == ' ' || c5 == '\t' || c5 == '\r' || c5 == '\n')) {
				return (offset << 8) | RELOAD;
			}
		}
		return OTHER;
	}

	// ROLLBACK' '
	private static int rollback(String stmt, int offset) {
		if (stmt.length() > offset + 7) {
			char c1 = stmt.charAt(++offset);
			char c2 = stmt.charAt(++offset);
			char c3 = stmt.charAt(++offset);
			char c4 = stmt.charAt(++offset);
			char c5 = stmt.charAt(++offset);
			char c6 = stmt.charAt(++offset);
			char c7 = stmt.charAt(++offset);
			if ((c1 == 'L' || c1 == 'l') && (c2 == 'L' || c2 == 'l')
					&& (c3 == 'B' || c3 == 'b') && (c4 == 'A' || c4 == 'a')
					&& (c5 == 'C' || c5 == 'c') && (c6 == 'K' || c6 == 'k')
					&& (c7 == ' ' || c7 == '\t' || c7 == '\r' || c7 == '\n')) {
				return (offset << 8) | ROLLBACK;
			}
		}
		return OTHER;
	}

	// SELECT' '
	private static int select(String stmt, int offset) {
		if (stmt.length() > offset + 4) {
			char c1 = stmt.charAt(++offset);
			char c2 = stmt.charAt(++offset);
			char c3 = stmt.charAt(++offset);
			char c4 = stmt.charAt(++offset);
			if ((c1 == 'E' || c1 == 'e') && (c2 == 'C' || c2 == 'c')
					&& (c3 == 'T' || c3 == 't')
					&& (c4 == ' ' || c4 == '\t' || c4 == '\r' || c4 == '\n')) {
				return (offset << 8) | SELECT;
			}
		}
		return OTHER;
	}

	// SHOW' '
	private static int show(String stmt, int offset) {
		if (stmt.length() > offset + 3) {
			char c1 = stmt.charAt(++offset);
			char c2 = stmt.charAt(++offset);
			char c3 = stmt.charAt(++offset);
			if ((c1 == 'O' || c1 == 'o') && (c2 == 'W' || c2 == 'w')
					&& (c3 == ' ' || c3 == '\t' || c3 == '\r' || c3 == '\n')) {
				return (offset << 8) | SHOW;
			}
		}
		return OTHER;
	}

	// SWITCH' '
	private static int swh(String stmt, int offset) {
		if (stmt.length() > offset + 5) {
			char c1 = stmt.charAt(++offset);
			char c2 = stmt.charAt(++offset);
			char c3 = stmt.charAt(++offset);
			char c4 = stmt.charAt(++offset);
			char c5 = stmt.charAt(++offset);
			if ((c1 == 'I' || c1 == 'i') && (c2 == 'T' || c2 == 't')
					&& (c3 == 'C' || c3 == 'c') && (c4 == 'H' || c4 == 'h')
					&& (c5 == ' ' || c5 == '\t' || c5 == '\r' || c5 == '\n')) {
				return (offset << 8) | SWITCH;
			}
		}
		return OTHER;
	}

	// STOP' '
	private static int stop(String stmt, int offset) {
		if (stmt.length() > offset + 3) {
			char c1 = stmt.charAt(++offset);
			char c2 = stmt.charAt(++offset);
			char c3 = stmt.charAt(++offset);
			if ((c1 == 'O' || c1 == 'o') && (c2 == 'P' || c2 == 'p')
					&& (c3 == ' ' || c3 == '\t' || c3 == '\r' || c3 == '\n')) {
				return (offset << 8) | STOP;
			}
		}
		return OTHER;
	}

	// KILL @
	private static int kill(String stmt, int offset) {
		if (stmt.length() > offset + 3) {
			char c1 = stmt.charAt(++offset);
			char c2 = stmt.charAt(++offset);
			char c3 = stmt.charAt(++offset);
			char c4 = stmt.charAt(++offset);
			if ((c1 == 'I' || c1 == 'i') && (c2 == 'L' || c2 == 'l')
					&& (c3 == 'L' || c3 == 'l')
					&& (c4 == ' ' || c4 == '\t' || c4 == '\r' || c4 == '\n')) {
				while (stmt.length() > ++offset) {
					switch (stmt.charAt(offset)) {
					case ' ':
					case '\t':
					case '\r':
					case '\n':
						continue;
					case '@':
						return killConnection(stmt, offset);
					default:
						return OTHER;
					}
				}
				return OTHER;
			}
		}
		return OTHER;
	}

	// KILL @@CONNECTION' ' XXXXXX
	private static int killConnection(String stmt, int offset) {
		if (stmt.length() > offset + "@CONNECTION ".length()) {
			char c1 = stmt.charAt(++offset);
			char c2 = stmt.charAt(++offset);
			char c3 = stmt.charAt(++offset);
			char c4 = stmt.charAt(++offset);
			char c5 = stmt.charAt(++offset);
			char c6 = stmt.charAt(++offset);
			char c7 = stmt.charAt(++offset);
			char c8 = stmt.charAt(++offset);
			char c9 = stmt.charAt(++offset);
			char c10 = stmt.charAt(++offset);
			char c11 = stmt.charAt(++offset);
			char c12 = stmt.charAt(++offset);
			if ((c1 == '@')
					&& (c2 == 'C' || c2 == 'c')
					&& (c3 == 'O' || c3 == 'o')
					&& (c4 == 'N' || c4 == 'n')
					&& (c5 == 'N' || c5 == 'n')
					&& (c6 == 'E' || c6 == 'e')
					&& (c7 == 'C' || c7 == 'c')
					&& (c8 == 'T' || c8 == 't')
					&& (c9 == 'I' || c9 == 'i')
					&& (c10 == 'O' || c10 == 'o')
					&& (c11 == 'N' || c11 == 'n')
					&& (c12 == ' ' || c12 == '\t' || c12 == '\r' || c12 == '\n')) {
				while (stmt.length() > ++offset) {
					switch (stmt.charAt(offset)) {
					case ' ':
					case '\t':
					case '\r':
					case '\n':
						continue;
					default:
						return (offset << 8) | KILL_CONN;
					}
				}
			}
		}
		return OTHER;
	}

}