package com.cloudhopper.mq.util;
/*
* #%L
* ch-mq
* %%
* Copyright (C) 2012 Cloudhopper by Twitter
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import com.cloudhopper.mq.queue.QueueFatalException;
/**
* Utility class for queue names such as checking if they are valid.
*
* @author joelauer
*/
public class QueueNameValidator {
static public boolean isValid(String queueName) {
try {
QueueNameValidator.validate(queueName);
return true;
} catch (QueueFatalException e) {
return false;
}
}
static public void validate(String queueName) throws QueueFatalException {
if (queueName == null) {
throw new QueueFatalException("Queue name cannot be null");
}
if (queueName.equals("")) {
throw new QueueFatalException("Queue name cannot be an empty string");
}
// name can only contain a-z, A-Z, 0-9, or .-_ (not as the first character)
for (int i = 0; i < queueName.length(); i++) {
char c = queueName.charAt(i);
if ('a' <= c && c <= 'z') {
// do nothing
} else if ('A' <= c && c <= 'Z') {
// do nothing
} else if ('0' <= c && c <= '9') {
// do nothing
} else if (c == '+') {
// do nothing
} else if (i != 0 && (c == '.' || c == '-' || c == '_')) {
// do nothing
} else {
throw new QueueFatalException("Invalid character [" + c + "] in queue name [" + queueName + "]");
}
}
}
}