/*
* 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 org.apache.jmeter.functions;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This class wraps the FileRowColContainer for use across multiple threads.
*
* It does this by maintaining a list of open files, keyed by file name (or
* alias, if used). A list of open files is also maintained for each thread,
* together with the current line number.
*
*/
public final class MyExcelRead {

private static final Logger log = LoggerFactory.getLogger(MyExcelRead.class);

private static final int NO_LINE = -1;

private static volatile String defaultFile = ""; // for omitted file names //$NON-NLS-1$

/*
* This Map serves two purposes:
* - maps file names to containers
* - ensures only one container per file across all threads
*/
private static final Map<String, MyExcelReadBeanInfo> fileContainers = new HashMap<>();

/* The cache of file packs - used to improve thread access */
private static final ThreadLocal<Map<String, MyExcelRead>> filePacks =
new ThreadLocal<Map<String, MyExcelRead>>() {
@Override
protected Map<String, MyExcelRead> initialValue() {
return new HashMap<>();
}
};

private final MyExcelReadBeanInfo container;

private int currentRow;

/*
* Only needed locally
*/
private MyExcelRead(MyExcelReadBeanInfo fdc) {
super();
container = fdc;
currentRow = -1;
}

private static String checkDefault(String file) {
if (file.length() == 0) {
if (fileContainers.size() == 1 && defaultFile.length() > 0) {
log.warn("Using default: " + defaultFile);
file = defaultFile;
} else {
log.error("Cannot determine default file name");
}
}
return file;
}

/*
* called by ExcelRead(file,sheet,rowNum,cellNum)
*/
public static synchronized void open(String file, String sheet, int rowNum, int cellNum) {
log.info("Opening " + file + " as " + sheet);
file = checkDefault(file);
if (sheet.length() == 0) {
log.error("sheet cannot be empty");
return;
}
Map<String, MyExcelRead> m = filePacks.get();
MyExcelReadBeanInfo frcc;
try {
frcc = getFile(file, sheet, rowNum, cellNum);
log.info("Stored " + file + " as " + sheet);
m.put(sheet, new MyExcelRead(frcc));
} catch (IOException e) {
// Already logged
}
}

private static MyExcelReadBeanInfo getFile(String file, String sheet, int rowNum, int cellNum) throws FileNotFoundException, IOException {
MyExcelReadBeanInfo frcc;
if ((frcc = fileContainers.get(sheet)) == null) {
frcc = new MyExcelReadBeanInfo(file, sheet, rowNum, cellNum);
fileContainers.put(sheet, frcc);
log.info("Saved " + file + " as " + sheet);
if (defaultFile.length() == 0) {
defaultFile = file;// Save in case needed later
}
}
return frcc;
}

public static String getColumn(String file, String sheet, int rowNum, int cellNum) {
Map<String, MyExcelRead> my = filePacks.get();
MyExcelRead fw = my.get(file);
if (fw == null) // First call
{
file = checkDefault(file);
log.info("Attaching " + file);
open(file, sheet, rowNum, cellNum);
fw = my.get(sheet);
// TODO improve the error handling
if (fw == null) {
return ""; //$NON-NLS-1$
}
}
return fw.getColumn();
}

private String getColumn() {
if (currentRow == NO_LINE) {
currentRow = container.nextRow();
}
return container.getColumn(currentRow);
}

/**
*
*/
public static void clearAll() {
log.debug("clearAll()");
Map<String, MyExcelRead> my = filePacks.get();
for (Iterator<Map.Entry<String, MyExcelRead>> i = my.entrySet().iterator(); i.hasNext();) {
Map.Entry<String, MyExcelRead> fw = i.next();
log.info("Removing " + fw.toString());
i.remove();
}
fileContainers.clear();
defaultFile = ""; //$NON-NLS-1$
}
}

Jmeter二次开发代码(2)的更多相关文章

  1. Jmeter二次开发代码(3)

    package org.apache.jmeter.functions; import java.io.FileInputStream;import java.io.FileNotFoundExcep ...

  2. Jmeter二次开发代码(1)

    package org.apache.jmeter.functions; import java.util.Collection;import java.util.LinkedList;import ...

  3. JMeter二次开发(2)-编写 JSON Assertion 插件

    本篇文章主要介绍如何对JMeter进行二次开发,添加自己所需的功能.这里以Json验证为例进行说明.在web接口测试过程中,JSON的应用已经非常普遍,但原声的JMeter并没有提供Json及Json ...

  4. Jmeter二次开发——自定义函数

    在之前的博文中,Jmeter二次开发--基于Java请求,已介绍了Jmeter二次开发的基础情况,上次分享的是java请求开发,今天来分享下Jmeter中的函数开发.聊到Jmeter的函数,知道Jme ...

  5. JMeter二次开发(1)-eclipse环境配置及源码编译

    1.下载src并解压 http://jmeter.apache.org/download_jmeter.cgi   2.获取所需jar包,编译 ant download_jars ant instal ...

  6. jmeter 二次开发

    基于jmeter的java请求的二次开发 常用的方法: ①.addArgument("name", "value")    定义参数 ②.sampleStart ...

  7. Jmeter二次开发——基于Java请求

    简述 这近几年,越来越多非http的协议需要进行性能测试,包括不仅限于各类rpc.mq.缓存等.对于这些协议,市面上可能没有现成的工具可以直接使用,这个时候,我们可以自己动手,通过编写相应的JavaS ...

  8. kettle工具二次开发-代码启动JOB

    kettle工具是一款优秀的数据同步.数据处理的BI工具,收到了很多人的青睐.kettle软件通过可视化的图标可以让我们很轻易的能完成数据同步.处理的开发工作.但是使用kettle可视化界面在跑JOB ...

  9. 性能测试基础---jmeter二次开发

    ·Jmeter的二次开发,常见的有以下几种类型: ·扩展.修改Jmeter已有的组件(源代码) ·扩展.修改Jmeter已有的函数. ·完全自主开发一个新的组件(依赖于Jmeter提供的框架). ·扩 ...

随机推荐

  1. sklearn中的Pipeline

    在将sklearn中的模型持久化时,使用sklearn.pipeline.Pipeline(steps, memory=None)将各个步骤串联起来可以很方便地保存模型. 例如,首先对数据进行了PCA ...

  2. 【网摘】C#中TransactionScope的使用方法和原理

    时间 2013-08-12 19:59:34  51CTO推荐博文 原文  http://cnn237111.blog.51cto.com/2359144/1271600 在.net 1.1的时代,还 ...

  3. Manacher's Algorithm(马拉车算法)

    ## 背景 该算法用于求字符串的最长回文子串长度. ## 参考文章 >[最长回文子串——Manacher 算法](https://segmentfault.com/a/1190000003914 ...

  4. 二分查找BinarySearch(Java)

    二分查找(折半查找)BinarySearch 二分查找  一组排好顺序的数,查找其中的一个数(value)的位置,按照数组(int[] a)存放这组数据,数组的索引所指的位置就是需要查找的数,用三个变 ...

  5. Ext JS中的typeOf

    Ext JS中的typeOf:以字符串格式,返回给定变量的类型 其中对字符串对象.元素节点.文本节点.空白文本节点判断并不准确 测试代码如下: <!DOCTYPE HTML PUBLIC &qu ...

  6. Html和Css学习笔记-html基础知识

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 此篇博客是我的复习笔记,html和css学的时间太久了,忘得差不多了,最近要使用一下,所以重新打开html的书略读,后记录了标签 ...

  7. weblogic patch log显示

    如何在WebLogic 12.1.3 版本的 server log 中显示 opatch 的补丁信息? 打补丁  patch 23558563 之后. 需要在 JAVA_OPTIONS 中添加如下参数 ...

  8. 智能指针std::weak_ptr

    std::weak_ptr 避免shared_ptr内存泄漏的利器.

  9. 深入浅出ES6教程模块化

    大家好,本人名叫苏日俪格,大家叫我 (格格) 就好,在上一章节中我们学到了Promise的用法,下面我们一起来继续学习模块化: JavaScript本身是不支持模块化的,只不过后来一些社区的大佬制定了 ...

  10. idea Maven项目找不到相关依赖包(红色波浪线)

    前两天做项目的时候,把团队其他人的代码从git同步到自己电脑上,出现了冲突.发现是maven依赖出现了问题,之前的截图找不到了,我就简单描述一下.就是下图箭头所示位置出现了红色波浪线. 在网上找了很多 ...