Jmeter二次开发代码(2)


/*
* 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 fileContainers = new HashMap<>();

/* The cache of file packs - used to improve thread access */
private static final ThreadLocal> filePacks =
new ThreadLocal>() {
@Override
protected Map 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 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 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 my = filePacks.get();
for (Iterator> i = my.entrySet().iterator(); i.hasNext();) {
Map.Entry fw = i.next();
log.info("Removing " + fw.toString());
i.remove();
}
fileContainers.clear();
defaultFile = ""; //$NON-NLS-1$
}
}