com.gzj.tulip.load.LoadScope.java Source code

Java tutorial

Introduction

Here is the source code for com.gzj.tulip.load.LoadScope.java

Source

/*
 * Copyright 2007-2009 the original author or authors.
 *
 * 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 com.gzj.tulip.load;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * @author  [qieqie.wang@gmail.com]
 * 
 */
public class LoadScope {

    // controllers->com.yourcompany.yourapp
    // applicationContext->com.yourcampany.yourapp
    // ...
    private Map<String, String[]> load = new HashMap<String, String[]>();

    /**
     * ?LoadScope
     * loadScopecomponetTypedefTypecomponetType.
     * <p>
     * loadScopeString: componetConf [; componetConf]*<br>
     * componetConf: [componetType = ] componetConfValue<br>
     * componetType: 'controllers' | 'applicationContext' | 'messages' |
     * '*' <br>
     * componetConfValue: package [, packages]*<br>
     * 
     * @param loadScopeString
     * @param defType
     */
    public LoadScope(String loadScopeString, String defType) {
        init(loadScopeString, defType);
    }

    public String[] getScope(String componentType) {
        String[] scope = this.load.get(componentType);
        if (scope == null) {
            scope = this.load.get("*");
        }
        return scope;
    }

    private void init(String loadScope, String defType) {
        if (StringUtils.isBlank(loadScope) || "*".equals(loadScope)) {
            return;
        }
        loadScope = loadScope.trim();
        String[] componetConfs = StringUtils.split(loadScope, ";");
        for (String componetConf : componetConfs) {
            if (StringUtils.isBlank(loadScope)) {
                continue;
            }
            // "controllers=com.renren.xoa, com.renren.yourapp"
            componetConf = componetConf.trim();
            int componetTypeIndex;
            String componetType = defType; // "controllers", "applicationContext", "dao", "messages", "*"
            String componetConfValue = componetConf;
            if ((componetTypeIndex = componetConf.indexOf('=')) != -1) {
                componetType = componetConf.substring(0, componetTypeIndex).trim();
                componetConfValue = componetConf.substring(componetTypeIndex + 1).trim();
            }
            if (componetType.startsWith("!")) {
                componetType = componetType.substring(1);
            } else {
                componetConfValue = componetConfValue + ", com.gzj.tulip";
            }
            String[] packages = StringUtils.split(componetConfValue, ", \t\n\r\0");//\t
            this.load.put(componetType, packages);
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (Entry<String, String[]> componetConf : load.entrySet()) {
            String componetType = componetConf.getKey();
            String componetConfValue[] = componetConf.getValue();
            sb.append(componetType).append("=");
            for (String value : componetConfValue) {
                sb.append(value).append(";");
            }
            if (componetConfValue.length > 0) {
                sb.setLength(sb.length() - 1);
            }
        }
        return super.toString();
    }
}