All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ecfeed.core.utils.JarExtractor Maven / Gradle / Ivy

Go to download

An open library used to connect to the ecFeed service. It can be also used as a standalone testing tool. It is integrated with Junit5 and generates a stream of test cases using a selected algorithm (e.g. Cartesian, N-Wise). There are no limitations associated with the off-line version but the user cannot access the on-line computation servers and the model database.

The newest version!
/*******************************************************************************
 *
 * Copyright (c) 2016 ecFeed AS.                                                
 * All rights reserved. This program and the accompanying materials              
 * are made available under the terms of the Eclipse Public License v1.0         
 * which accompanies this distribution, and is available at                      
 * http://www.eclipse.org/legal/epl-v10.html 
 *  
 *******************************************************************************/

package com.ecfeed.core.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class JarExtractor {

	public static String getFileContent(String filePathName, String jarPathName) {
		File jarFile = new File(jarPathName);
		JarFile jar = null;
		String fileContent = null;
		String exceptionMessage = null;

		try {
			jar = new JarFile(jarFile);
			fileContent = readFileFromJar(filePathName, jar);
		} catch (IOException | EcException e) {
			SystemLogger.logCatch(e);
			exceptionMessage = e.getMessage();
		} finally {
			tryCloseJar(jar);
		}

		if (exceptionMessage != null) {
			ExceptionHelper.reportRuntimeException(exceptionMessage);
		}

		return fileContent;
	}

	private static String readFileFromJar(String filePathName, JarFile jarFile) throws EcException {
		Enumeration jarEntries = jarFile.entries();

		while (jarEntries.hasMoreElements()) {
			JarEntry jarEntry = (JarEntry)jarEntries.nextElement();

			String jarEntryName = jarEntry.getName(); 
			if (jarEntryName.equals(filePathName)) {
				return readJarEntry(jarEntry, jarFile);
			}
		}

		return null;
	}

	private static String readJarEntry(JarEntry jarEntry, JarFile jarFile) throws EcException {
		String exceptionMessage = null;
		String content = null;
		InputStream inputStream = null;

		try {
			inputStream = jarFile.getInputStream(jarEntry);
			content = StreamHelper.streamToString(inputStream);
		} catch (IOException e) {
			SystemLogger.logCatch(e);
			exceptionMessage = e.getMessage();
		} finally {
			tryCloseInputStream(inputStream);
		}

		if (exceptionMessage != null) {
			EcException.report(exceptionMessage);
		}		

		return content;
	}

	private static void tryCloseJar(JarFile jar) {
		try {
			jar.close();
		} catch (IOException e) {
			SystemLogger.logCatch(e);
		}
	}

	private static void tryCloseInputStream(InputStream inputStream) {
		try {
			inputStream.close();
		} catch (IOException e) {
			SystemLogger.logCatch(e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy