Skip to content

Commit

Permalink
feat:reform the name black list
Browse files Browse the repository at this point in the history
  • Loading branch information
leizhiyuan committed Nov 18, 2019
1 parent a88394b commit 95c06df
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/main/java/com/alipay/hessian/NameBlackListFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public class NameBlackListFilter implements ClassNameFilter {
/**
* 黑名单 包名前缀
*/
protected List<String> blackPrefixList;
protected static List<String> blackPrefixList;

/**
* 类名是否在黑名单中结果缓存。{className:true/false}
*/
protected ConcurrentMap<String, Boolean> resultOfInBlackList;
protected static ConcurrentMap<String, Boolean> resultOfInBlackList;

/**
* 指定黑名单前缀
Expand All @@ -55,24 +55,25 @@ public NameBlackListFilter(List<String> blackPrefixList) {
* @param maxCacheSize 最大缓存大小
*/
public NameBlackListFilter(List<String> blackPrefixList, int maxCacheSize) {
this.blackPrefixList = blackPrefixList;
buildCache(maxCacheSize);
NameBlackListFilter.blackPrefixList = blackPrefixList;
buildCache(blackPrefixList, maxCacheSize);
}

/**
* 初始化缓存
*
* @param maxCacheSize 最大缓存
*/
protected void buildCache(int maxCacheSize) {
public static void buildCache(List<String> blackPrefixList, int maxCacheSize) {
if (blackPrefixList != null && !blackPrefixList.isEmpty()) {
NameBlackListFilter.blackPrefixList = blackPrefixList;
int min = Math.min(256, maxCacheSize);
int max = Math.min(10240, maxCacheSize);
ConcurrentLinkedHashMap.Builder<String, Boolean> builder = new ConcurrentLinkedHashMap.Builder<String, Boolean>()
.initialCapacity(min).maximumWeightedCapacity(max);
this.resultOfInBlackList = builder.build();
NameBlackListFilter.resultOfInBlackList = builder.build();
} else {
this.resultOfInBlackList = null;
NameBlackListFilter.resultOfInBlackList = null;
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/test/java/com/alipay/hessian/NameBlackListFilterManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alipay.hessian;

import java.util.List;

public class NameBlackListFilterManager {
/**
* rebuild cache.
* @param blackList
*/
public static void rebuildBlackList(List<String> blackList) {

NameBlackListFilter.buildCache(blackList, 4096);
}
}

0 comments on commit 95c06df

Please sign in to comment.