/*
* 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. 知识小罐头09(tomcat8启动源码分析 下)

    初始化已经完成,现在就是启动这些组件,Tomcat中的start方法就是用于启动的,其实start的原理还是和上一篇说的初始化几乎一样!这里我就大概说一下,看几个比较关键的地方就行了. 前面的步骤就大 ...

  2. 知识小罐头06(tomcat8请求源码分析 中)

    更正上一篇一个小错误,Connector中首先是将socket请求过来的信息封装成一个普通的Request对象(上一篇我写成HttpRequest对象,失误失误,根本就木有HttpRequest这样的 ...

  3. 『练手』003 Laura.SqlForever如何扩展 兼容更多数据库引擎

     003 Laura.SqlForever如何扩展 兼容更多数据库引擎 数据库引擎插件 在 界面上的体现 导航窗体 的 工具栏 中的 引擎下拉列表        导航窗体 的 树形控件 中的 引擎主节 ...

  4. centos7 安装 smplayer

    How to setup multimedia on CentOS 7 You will need to also install the EPEL repository as nux-dextop ...

  5. C#中的CultureInfo类

    CultureInfo类位于System.Globalization命名空间内,这个类和命名空间许多人都不是很熟悉,实际我们在写程序写都经常间接性的接触这个类,当进行数字,日期时间,字符串匹配时,都会 ...

  6. css 半圆效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. CSS中的一下小技巧2之CSS3动画勾选运用

    使用CSS3实现动画勾选 相信大家在项目中会经常遇到这种需求:勾选框.现在用CSS3来实现一个动画勾选,只需要一个标签即可完成: 这次需要用到CSS中伪类 after,这个小技巧也是很容易忘记的,所以 ...

  8. Android为TV端助力:EventBus跨进程发送消息

    单一app内的用法 如果你在单一app内进行多进程开发,那么只需要做以下三步: Step 1 在gradle文件中加入下面的依赖:   dependencies {   compile 'xiaofe ...

  9. (最完美)MIUI12系统的Usb调试模式在哪里开启的步骤

    当我们使用安卓手机通过数据线链接到Pc的时候,或者使用的有些app比如我们公司营销小组当使用的app引号精灵,之前的老版本就需要开启usb调试模式下使用,现当新版本不需要了,如果手机没有开启usb调试 ...

  10. 通过 Sqoop1.4.7 将 Mysql5.7、Hive2.3.4、Hbase1.4.9 之间的数据导入导出

    目录 目录 1.什么是 Sqoop? 2.下载应用程序及配置环境变量 2.1.下载 Sqoop 1.4.7 2.2.设置环境变量 2.3.设置安装所需环境 3.安装 Sqoop 1.4.7 3.1.修 ...