All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aspire.nm.component.common.mobile.MobileUtil Maven / Gradle / Ivy


package com.aspire.nm.component.common.mobile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.aspire.nm.component.common.mobile.Strategy.LoadStrategy;
import com.aspire.nm.component.common.mobile.Strategy.Impl.db.black.Black;
import com.aspire.nm.component.common.mobile.Strategy.Impl.db.black.TerminalStore;
import com.aspire.nm.component.common.mobile.Strategy.Impl.db.legalPerfix.LegalPerfix;
import com.aspire.nm.component.common.mobile.Strategy.Impl.db.segment.SegMent;
import com.aspire.nm.component.common.mobile.Strategy.Impl.db.segment.SegMentsStore;
import com.aspire.nm.component.common.mobile.Strategy.Impl.db.switchs.Swithchs;
import com.aspire.nm.component.common.mobile.Strategy.Impl.db.update.UpdateTimes;
import com.aspire.nm.component.commonUtil.log.ReleaseLogger;




public class MobileUtil extends Thread{
    
	private static Logger logger = Logger.getLogger(MobileUtil.class);
	private static Logger releaselogger = Logger.getLogger(ReleaseLogger.class);
	
    private static final int LEN_LEGAL_MOBILEPHONE = 11;
    
    private static final int REFRESH_SEC = 60;
    
    
    //号段信息存储
    private SegMentsStore segMentsStore;
    //好短信息更新时间
    private String updateSegMentTime;
    
    //黑名单存储
    private TerminalStore blackTerminalStore;
    //黑名单信息更新时间
    private String updateBlackTime;
    
    //携号转网存储
    private TerminalStore switchTerminalStore;
    //携号转网更新时间
    private String updateSwitchTime;
    
    
    //合法手机号前缀存储
    private LegalPerfix legalPerfix;
    //合法手机号前缀更新时间
    private String updateLegalPerfixTime;
    
    
    
    //信息来源,由客户程序自行开发
    private LoadStrategy loadStrategy;
    
    
    private boolean stop = false;
    
    
    
    private SegMentsStore getSegMentsStore() {
    	synchronized(MobileUtil.class){
    		return segMentsStore;
    	}
	}
    private void setSegMentsStore(SegMentsStore segMentsStore) {
		synchronized(MobileUtil.class){
			this.segMentsStore = segMentsStore;
		}
	}
    private TerminalStore getBlackTerminalStore() {
		synchronized(MobileUtil.class){
			return blackTerminalStore;
		}
	}
    private void setBlackTerminalStore(TerminalStore blackTerminalStore) {
		synchronized(MobileUtil.class){
			this.blackTerminalStore = blackTerminalStore;
		}
	}
    private TerminalStore getSwitchTerminalStore() {
		synchronized(MobileUtil.class){
			return switchTerminalStore;
		}
	}
    private void setSwitchTerminalStore(TerminalStore switchTerminalStore) {
		synchronized(MobileUtil.class){
			this.switchTerminalStore = switchTerminalStore;
		}
	}
    private LegalPerfix getLegalPerfix() {
    	synchronized(MobileUtil.class){
    		return legalPerfix;
    	}
	}
    private void setLegalPerfix(LegalPerfix legalPerfix) {
    	synchronized(MobileUtil.class){
    		this.legalPerfix = legalPerfix;
    	}
	}
    
    
	private boolean isCMCCMobile(String mobilePhone) {
        boolean result = false;
        if(mobilePhone==null || "".equals(mobilePhone.trim())){
            return false;
        }
        String [] legalPerfix_cmcc_mobile = getLegalPerfix().getPerfix_legal_cmcc_mobile();
        if(legalPerfix_cmcc_mobile == null) return false;
        
        for (int i = 0; i < legalPerfix_cmcc_mobile.length; i++) {
            if (legalPerfix_cmcc_mobile[i] != null && mobilePhone.trim().startsWith(legalPerfix_cmcc_mobile[i])) {
                result = true;
                break;
            }
        }
        return result;
    }
    private boolean isCTCCMobile(String mobilePhone) {
        boolean result = false;
        if(mobilePhone==null || "".equals(mobilePhone.trim())){
            return false;
        }
        String [] legalPerfix_ctcc_mobile = getLegalPerfix().getPerfix_legal_ctcc_mobile();
        if(legalPerfix_ctcc_mobile == null) return false;
        
        for (int i = 0; i < legalPerfix_ctcc_mobile.length; i++) {
            if (legalPerfix_ctcc_mobile[i] != null && mobilePhone.trim().startsWith(legalPerfix_ctcc_mobile[i])) {
                result = true;
                break;
            }
        }
        return result;
    }
    private boolean isCUCCMobile(String mobilePhone) {
        boolean result = false;
        if(mobilePhone==null || "".equals(mobilePhone.trim())){
            return false;
        }
        String [] legalPerfix_cucc_mobile = getLegalPerfix().getPerfix_legal_cucc_mobile();
        if(legalPerfix_cucc_mobile == null) return false;
        
        for (int i = 0; i < legalPerfix_cucc_mobile.length; i++) {
            if (legalPerfix_cucc_mobile[i] != null && mobilePhone.trim().startsWith(legalPerfix_cucc_mobile[i])) {
                result = true;
                break;
            }
        }
        return result;
    }
    
    
    
    
    
    private void doRun(){
        UpdateTimes updateTimes = loadStrategy.getUpdateTimes();
        
        if(updateLegalPerfixTime == null || !updateTimes.getUpdateLegalPerfixTime().equals(updateLegalPerfixTime)){
        	
        	updateLegalPerfixTime = updateTimes.getUpdateLegalPerfixTime();
        	
        	logger.debug("reLoadLegalPerfix...");
        	
        	LegalPerfix tmpLegalPerfix = loadStrategy.loadLegalPerfix();
        	
        	if(tmpLegalPerfix != null){
        	    setLegalPerfix(tmpLegalPerfix);
        	}
        	
        	logger.debug("reLoadLegalPerfix Done ,  " + tmpLegalPerfix);
        }
        
        if(updateSegMentTime == null || !updateTimes.getUpdateSegmentTime().equals(updateSegMentTime)){
            
            updateSegMentTime = updateTimes.getUpdateSegmentTime();
            
            logger.debug("reLoadSegMents...");
            
            List segMents = loadStrategy.loadSegMents();
            
            if(segMents != null){
                SegMentsStore tmpStore = new SegMentsStore();
                for(SegMent segMent: segMents){
                    tmpStore.add(segMent);
                }
                setSegMentsStore(tmpStore);
            }
            
            logger.debug("reLoadSegMents Done , size = " + (getSegMentsStore()==null?0:getSegMentsStore().size()));
        }
        
        if(updateBlackTime == null || !updateTimes.getUpdateBlackTime().equals(updateBlackTime)){
            updateBlackTime = updateTimes.getUpdateBlackTime();
            logger.debug("reLoadBlacks...");
            
            List blacks = loadStrategy.loadBlacks();
            
            if(blacks != null){
                TerminalStore tmpStore = new TerminalStore();
                for(Black black : blacks){
                    tmpStore.add(black.getTerminal(), black);
                }
                setBlackTerminalStore(tmpStore);
            }
            
            logger.debug("reLoadBlacks Done , size = " + (getBlackTerminalStore()==null?0:getBlackTerminalStore().size()));
        }
        
        
        if(updateSwitchTime == null || !updateTimes.getUpdateSwitchTime().equals(updateSwitchTime)){
            
            updateSwitchTime = updateTimes.getUpdateSwitchTime();
            
            logger.debug("reLoadSwitchs...");
            
            List switchs = loadStrategy.loadSwithchs();
            
            if(switchs != null){
                TerminalStore tmpStore = new TerminalStore();
                for(Swithchs swithchs : switchs){
                    tmpStore.add(swithchs.getTerminal(), swithchs);
                }
                setSwitchTerminalStore(tmpStore);
            }
            logger.debug("reLoadSwitchs Done , size = " + (getSwitchTerminalStore()==null?0:getSwitchTerminalStore().size()));
        }
        
    }
    public MobileUtil(LoadStrategy loadStrategy){
        this(loadStrategy,true);
    }
    public MobileUtil(LoadStrategy loadStrategy,boolean availability){
        this.loadStrategy = loadStrategy;
        if(availability){
            init();
        }
    }
    private void init(){
    	doRun();
        start();
    }

	public void run(){
	    
	    while(true){
	        
	    	try {
	            Thread.sleep(REFRESH_SEC * 1000);
	        } catch (InterruptedException e) {}
	    	
	        if(stop){
	            break;
	        }
	        doRun();
	        
	        
	    }
	}
	







    
    
    
    
    
    
    
    
    
    
	
	
    
	
	
    
    
    
    /**
     * 释放资源(停止线程)
     */
    public void release(){
        stop = true;
    	interrupt();
    	releaselogger.debug("release MobileUtil SUCESS");
    }
    
    
    
    /**
     * 获取号段信息,如果没有获得相关公共数据,则固定返回null
     * @param segment 7位号段
     * @return 号段信息,返回null标示无法获取号段信息
     */
    public SegMent getSegMent(String segment){
    	return getSegMentsStore().get(segment);
    }
    /**
     * 是否是黑/白名单,,如果没有获得相关公共数据,则固定返回false
     * @param terminal
     * @return true 是黑名单  false 不是黑名单
     */
    public boolean isBlackDesc(String mobile,String desc){
        if(getBlackTerminalStore().getObj(mobile) != null &&
                getBlackTerminalStore().getObj(mobile) instanceof Black){
            return ((Black)getBlackTerminalStore().getObj(mobile)).getDesc().equals(desc);
        }
        return false;
    }
    
    
    /**
     * 获取运营商类型,如果没有获得相关公共数据,则固定返回null
     * @param mobiles
     * @return 返回null表示无法获取运营商信息
     */
    public Map> getTypeMap(String mobiles){
        Map> result = new HashMap>();
        for(String mobile : mobiles.split(",")){
            MobileUtil.Type type = getType(mobile);
            if(type == null){
                return null;
            }else{
                if(result.get(type) == null){
                    result.put(type, new ArrayList());
                }
                result.get(type).add(mobile);
            }
        }
        return result;
    }
    
    
    
    /**
     * 获取运营商类型,如果没有获得相关公共数据,则固定返回null
     * @param mobile
     * @return Type.CMCC/Type.CTCC/Type.CUCC/null ,返回null表示无法获取运营商信息
     */
    public MobileUtil.Type getType(String mobile){
        if(!isLegalMobile(mobile)){
            return null;
        }
        if(getSwitchTerminalStore() != null){
            Object obj = getSwitchTerminalStore().getObj(mobile);
            
            if(obj != null && obj instanceof Swithchs){
                if(((Swithchs)obj).getType() == Swithchs.TYPE_CMCC){
                    return Type.CMCC; 
                }
                if(((Swithchs)obj).getType() == Swithchs.TYPE_CTCC){
                    return Type.CTCC; 
                }
                if(((Swithchs)obj).getType() == Swithchs.TYPE_CUCC){
                    return Type.CUCC; 
                }
            }
        }
        
        
        if(isCMCCMobile(mobile)){
            return Type.CMCC;
        }
        if(isCTCCMobile(mobile)){
            return Type.CTCC;
        }
        if(isCUCCMobile(mobile)){
            return Type.CUCC;
        }
        
        return null;
    }
    
    /**
     * public MobileUtil.Type getType(String terminal)的返回值
     * 
     */
    public static enum Type{
        CMCC,CTCC,CUCC
    };
    
    
    
    
    
    
    
    
    /**
     * 判断是否是合法的手机号,如果没有获得相关公共数据,则固定返回false
     * @param mobile
     * @return true 是合法的手机号   ,  false 不是合法的手机号
     */
    public boolean isLegalMobile(String mobile){
        boolean result = true;
        if (mobile == null || "".equals(mobile.trim())) {
            result = false;
        } else {
        	String [] legalPerfix_mobile = getLegalPerfix().getPerfix_legal_mobile();
            if(legalPerfix_mobile == null) return false;
            
            if (mobile.trim().startsWith("86")) {
                mobile = mobile.trim().substring(2);
            } else if (mobile.trim().startsWith("+86")) {
                mobile = mobile.trim().substring(3);
            } else {
                mobile = mobile.trim();
            }

            if (!StringUtils.isNumeric(mobile)) {
                result = false;
            } else {
                boolean prefixLegal = false;
                for (int i = 0; i < legalPerfix_mobile.length; i++) {
                    if (legalPerfix_mobile[i] != null && mobile.trim().startsWith(legalPerfix_mobile[i])) {
                        prefixLegal = true;
                        break;
                    }
                }
                if (prefixLegal && mobile.trim().length() == LEN_LEGAL_MOBILEPHONE) {
                    result = true;
                } else {
                    result = false;
                }
            }
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy