net.kamhon.ieagle.aop.SchemaChangeB4Advice.java Source code

Java tutorial

Introduction

Here is the source code for net.kamhon.ieagle.aop.SchemaChangeB4Advice.java

Source

/*
 * Copyright 2012 Eng Kam Hon (kamhon@gmail.com)
 * 
 * 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.
 */
package net.kamhon.ieagle.aop;

import java.lang.reflect.Method;

import org.apache.commons.lang.StringUtils;
import org.springframework.aop.MethodBeforeAdvice;

public class SchemaChangeB4Advice implements MethodBeforeAdvice {

    private String fromSchema;
    private String toSchema;

    public void before(Method m, Object[] args, Object target) throws Throwable {
        if (StringUtils.isNotBlank(fromSchema) && StringUtils.isNotBlank(toSchema))
            if (args.length > 0) {
                if ((args[0] instanceof String) && isSql((String) args[0])) {
                    String sql = (String) args[0];
                    sql = StringUtils.replace(sql, fromSchema, toSchema);
                }
            }

    }

    /**
     * check whether it is sql.
     * 
     * @param text
     * @return
     */
    private boolean isSql(String text) {
        if (org.apache.commons.lang.StringUtils.isBlank(text) || text.length() < 7)
            return false;

        if (text.trim().substring(0, 6).equalsIgnoreCase("update")
                || text.trim().substring(0, 6).equalsIgnoreCase("select")
                || text.trim().substring(0, 6).equalsIgnoreCase("delete")) {
            return true;
        } else {
            return false;
        }
    }

    public void setFromSchema(String fromSchema) {
        this.fromSchema = fromSchema;
    }

    public void setToSchema(String toSchema) {
        this.toSchema = toSchema;
    }

}