Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:reform the name black list #49

Merged
merged 1 commit into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 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 Expand Up @@ -102,7 +103,7 @@ public String resolve(String className) throws IOException {
* @param className
* @return 是否在黑名单中
*/
private boolean inBlackList(String className) {
protected boolean inBlackList(String className) {
for (String prefix : blackPrefixList) {
if (className.startsWith(prefix)) {
return Boolean.TRUE;
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);
}
}
3 changes: 2 additions & 1 deletion src/test/java/com/alipay/stc/bl/MockNameBlacklistFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public MockNameBlacklistFilter() {
super(INTERNAL_BLACK_LIST);
}

protected Boolean inBlackList(String className) {
@Override
protected boolean inBlackList(String className) {
for (String prefix : INTERNAL_BLACK_LIST) {
if (className.startsWith(prefix)) {
return Boolean.TRUE;
Expand Down