What would cause java.io.FIleNotFoundException (access is denied)? - java

I am trying to save an extent report to my documents folder. I am getting an access is denied problem.
I have tried giving java full control, through all the user groups. I have given jetbrains full control as well but still access is denied.
This is where I am trying to save the file.
public void beforeSuite() {
extent = ExtentManager.createInstance("MobileCustomerCare_" + fileName + ".html");
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(System.getProperty("user.home") + "//Documents/ExtentReport//");
extent.attachReporter(htmlReporter);
I have added the ExtentManager class where the instance gets created
public class ExtentManager {
private static ExtentReports extent;
private static String fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(new Date());
static ExtentReports createInstance(String fileName) {
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(fileName);
htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setTheme(Theme.DARK);
htmlReporter.config().setDocumentTitle(fileName);
htmlReporter.config().setEncoding("utf-8");
htmlReporter.config().setReportName(fileName);
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
return extent;
}
}
EDIT: I have now changed to use ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(System.getProperty("user.home") + "//Documents/ExtentReport//"); But still getting (access is denied)
I am expecting it to save the file in the location specified. What it's actually doing is saving it in my project.

I think there is no path separator between the folder name and the file name, please try adding a path separator while creating ExtentHtmlReporter Object
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("C:\\Users\\tom.cockram\\Documents\\ExtentReport\\");
Try not to keep spaces in the file name, it will help you in future
public static ExtentReports getInstance() {
if (extent == null)
createInstance("MobileCustomerCare_" + fileName + ".html");
return extent;
}

i had this problem when i was generating an XML file
try this :
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("C://Users//tom.cockram//Documents//ExtentReport");

Related

Crystal reports creating report from XML and exporting it to PDF without needing path to original XML

I have a Java web application that exports pdf files.
I have to use Crystal Reports 11. I can already export pdfs, the issue is that it only works locally because the .rpt file has a reference to an XML file in my machine.
So, when I want to export a report and the .rpt file can't find the file, even though I'm giving it a new dataset to work with it still throws a not found exception. I tried changing the file's connection programmatically but it always throws an exception related to the connection.
public InputStream export() throws ReportSDKException, IOException, IllegalArgumentException, IllegalAccessException {
ReportClientDocument reportClientDoc = new ReportClientDocument();
reportClientDoc.setLocale(Locale.forLanguageTag(localeTag));
reportClientDoc.open(reportPath, OpenReportOptions._discardSavedData);
DatabaseController databaseController = reportClientDoc.getDatabaseController();
IConnectionInfo oldConn = databaseController.getConnectionInfos(null).get(0);
IConnectionInfo newConn = resolveConnection(reportClientDoc).get(0);
int replaceParams = DBOptions._ignoreCurrentTableQualifiers | DBOptions._doNotVerifyDB;
databaseController.replaceConnection(oldConn, newConn, null,replaceParams);
reportClientDoc.getDatabaseController().setDataSource(this.dataset);
ParameterFieldController parameterController = reportClientDoc.getDataDefController()
.getParameterFieldController();
for (Param<Double> p : doubleParams) {
parameterController.setCurrentValue(p.subReportName, p.fieldName, p.value);
}
for (Param<Object> p : objectParams) {
parameterController.setCurrentValue(p.subReportName, p.fieldName, p.value);
}
return reportClientDoc.getPrintOutputController().export(this.format);
}
private ConnectionInfos resolveConnection(ReportClientDocument reportClientDoc) throws ReportSDKException {
IConnectionInfo oldConnection = new ConnectionInfo();
DatabaseController dbController = reportClientDoc.getDatabaseController();
oldConnection = dbController.getConnectionInfos(null).getConnectionInfo(0);
String xsdPath = Paths.get(this.xsdPath).toAbsolutePath().toString();
final String SERVER_NAME = dummyXmlPath + " " + xsdPath;
final String DATABASE_DLL = oldConnection.getAttributes().getStringValue("Database DLL");
final String LOCAL_SCHEMA_FILE = xsdPath;
final String SERVER_TYPE = "XML";
final String PREQESERVERNAME = SERVER_NAME;
final String PREQESERVERTYPE = "XML";
final String LOCAL_XML_FILE = dummyXmlPath;
PropertyBag newAttributes = new PropertyBag();
newAttributes.put("Server Name", SERVER_NAME);
newAttributes.put("Database DLL", DATABASE_DLL);
newAttributes.put("Local Schema File", LOCAL_SCHEMA_FILE);
newAttributes.put("PreQEServerName", PREQESERVERNAME);
newAttributes.put("PreQEServerType", PREQESERVERTYPE);
newAttributes.put("Server Type", SERVER_TYPE);
newAttributes.put("Local XML File", LOCAL_XML_FILE);
IConnectionInfo newConnection = (IConnectionInfo) oldConnection.clone(true);
newConnection.setAttributes(newAttributes);
newConnection.setKind(oldConnection.getKind());
ConnectionInfos connectionInfos = new ConnectionInfos();
connectionInfos.add(newConnection);
return connectionInfos;
}
I was able to fix this by adding the Subreport data to the subreports.
SubreportController subreportController = reportClientDoc.getSubreportController();
for (String string : subreportController.querySubreportNames()) {
subreportController.setDataSource(string, dataset);
}

Test File Selection with JUnit/JMockit

I am attempting to test a method that returns a File object using JUnit and JMockit. I am a beginner with both of these.
The problem I am having is that I can't figure out how to properly/successfully mock the implementation method returning a file, since in reality, the user has to actually select a file for the method to return. The error I keep running into is:
java.lang.IllegalStateException: Missing invocation to mocked type at this point; please make sure such invocations appear only after the declaration of a suitable mock field or parameter
Any suggestions?
Here is a recreation of my implementation:
public final class MyClass {
public static File OpenFile(Stage stage, String title, String fileTypeText, ArrayList<String> fileType) throws Exception {
File file = null;
try {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle(title);
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionsFilter(fileTypeText + fileType, fileType);
fileChooser.getExtensionsFilters().add(extFilter);
file = fileChooser.showOpenDialog(stage);
return file;
}
catc (Exception e) {
if(fileType==null) {
...
}
return file;
}
}
}
Here is a recreation of my attempted JUnit test:
#Test
public void TestOpenFile(#Mocked Stage stage) throws Exception {
final ArrayList<String> extensions = new ArrayList<String>();
extensions.add(".txt");
final File file = null;
new Expectations() {{
MyClass.OpenFile(stage, anyString, anyString, extensions); returns(file);
}};
assertEquals(file, MyClass.OpenFile(stage, "some title", "some type", extensions));
}
Your solution is correct, but I would use expectations instead:
public void TestOpenFile(#Mocked FileChooser chooser) throws Exception{
new Expectations() {
{
chooser.showOpenDialog(stage); result = expectedFile;
}};
final File actualFile = MyClass.OpenFile(...);
assertEquals(expectedFile, actualFile);}
I find this easier to understand and write (my personal preference)
I realized that I was approaching the problem incorrectly at first. What I did to resolve this was:
Mock the FileChooser.showOpenDialog method to return a file instead of trying to mock my own method to return a file, which would have defeated the purpose of testing.
final File expectedFile = new File("abc");
new MockUp<FileChooser>() {
#Mock
File showOpenDialog(final Window overWindow) {
return expectedFile;
}
};
final File actualFile = MyClass.OpenFile(...);
assertEquals(expectedFile, actualFile);

Variables from properties.file not being passed - TestNG - WebDriver Test

I am using Webdriver + TestNG. I created a class where you can run any test with the browser of your choice just by using the method below where you enter which browser to run and what test to run as your variables. Now I am trying to feed my "browser" variable and "test" variable from a properties file. When I run it I just get a Pass but nothing happens. If I manually enter the variables it works fine. How come it wont take the values from the properties file? If I do a print - it prints the correct value...
This is the contents of the properties file:
browser="BROWSER GOES HERE"
test="TEST GOES HERE"
Here is my method to run the test:
#Test
public void runTest() throws IOException {
Properties prop = new Properties();
prop.load(new FileInputStream(
"path\\test.properties"));
localBrowser(prop.getProperty("browser"));
masterTest(driver, prop.getProperty("test"));
}
Here is a sandbox example of my test:
If I put the variables in myself the browser will open and execute the test. If I use the property loader it will just print the variables but will not run the test. Here is a sandbox example so you can try it.
Package Structure:
-src/test/java
--SandboxTest
-src/test/resources
--test.properties
Properties File example:
browser=firefox
test=test1
Class example:
public class SandboxTest {
private WebDriver driver;
InputStream input = null;
Properties prop = new Properties();
#Test
public void runTest() throws FileNotFoundException, IOException {
input = new FileInputStream(
(new File(
"C:INSERT_PATH_HERE\\test.properties")));
prop.load(input);
//This doesnt run the test
System.out.println(prop.getProperty("browser"));
System.out.println(prop.getProperty("test"));
localBrowser(prop.getProperty("browser"));
masterTest(driver, prop.getProperty("test"));
/*
* This works
* localBrowser("firefox");
* masterTest(driver, "test1");
*/
}
public void localBrowser(String browser) {
if (browser == "firefox") {
driver = new FirefoxDriver();
} else if (browser == "chrome") {
System.setProperty("webdriver.chrome.driver",
"C:\\INSERT_PATH_HERE\\chromedriver.exe");
driver = new ChromeDriver();
} else if (browser == "ie") {
System.setProperty("webdriver.ie.driver",
"C:\\INSERT_PATH_HERE\\IEDriverServer.exe");
driver = new InternetExplorerDriver(caps);
}
}
public void masterTest(WebDriver driver, String test) {
if (test == "test1") {
Test1(driver);
} else if (test == "test2") {
Test2(driver);
}
}
// *********************************TESTS*****************************************************
public void Test1(WebDriver driver) {
driver.get("http://www.marca.com/en/");
driver.findElement(By.linkText("Barcelona")).click();
}
public void Test2(WebDriver driver) {
driver.get("http://www.marca.com");
driver.findElement(By.linkText("FĂștbol")).click();
}
}
Well.. I am really not sure what you are doing, But if it doesn't work so probably it doesn't load the properties file properly.
try this one
Resource resource = new ClassPathResource("/environment.properties");
properties = PropertiesLoaderUtils.loadProperties(resource);
Tell me if that is ok, hope that helps
EDIT:
The above is part of Spring,
you can use
prop.load(new FileInputStream(new File("test.properties")));
Please make sure that test.properties will be under src folder in the project
EDIT:
The problem not in the property file.. The problem is in the way you are comparing 2 strings
When you do browser == "firefox" The check is for the object address(referenec)
When you are comparing Object type you need to use Eqauls
For example browser.equals("firefox") It will work this way.
Because when you put the value your self it check the refernece and it is the same.. Once you get from property this is 2 diffrent objects but they are "equal" So it will work only with Equals
What you should do instead is put the property in the testing.xml file and then read the property from there instead. Then, if you need to have different property files, you can just create multiple testing.xml files and when you execute the tests just pass the testng.xml file as an argument to TestNG.
In the project I referenced above in the hyperlink, you can see I reference the properties something like so:
#BeforeClass
public void setUp( ITestContext context ) {
suiteParams = context.getSuite().getXmlSuite().getAllParameters();
String browser = suiteParams.get( "browser" );
String sauceUser = suiteParams.get( "sauceUser" );
String sauceKey = suiteParams.get( "sauceKey" )
....
If you have to do it using a properties file, then I would change you code and do it differently. Something like so (assuming use of TestNG):
protected WebDriver driver;
protected String browser;
#BeforeTest
public void setUp() {
Properties prop = new Properties();
prop.load( new FileInputStream("path/test.properties") );
browser = prop.getProperty("browser");
if ( browser.equals("firefox") {
driver = new FirefoxDriver();
} else {
driver = null; // bad choice
}
}
#Test
public void runTest() throws IOException {
driver.navigateTo( url);
// do stuff
Assert.assertTrue( 1 == 1 );
}
#AfterTest
public void cleanUp() {
driver.quit();
}

How to list the files inside a JAR file?

I have this code which reads all the files from a directory.
File textFolder = new File("text_directory");
File [] texFiles = textFolder.listFiles( new FileFilter() {
public boolean accept( File file ) {
return file.getName().endsWith(".txt");
}
});
It works great. It fills the array with all the files that end with ".txt" from directory "text_directory".
How can I read the contents of a directory in a similar fashion within a JAR file?
So what I really want to do is, to list all the images inside my JAR file, so I can load them with:
ImageIO.read(this.getClass().getResource("CompanyLogo.png"));
(That one works because the "CompanyLogo" is "hardcoded" but the number of images inside the JAR file could be from 10 to 200 variable length.)
EDIT
So I guess my main problem would be: How to know the name of the JAR file where my main class lives?
Granted I could read it using java.util.Zip.
My Structure is like this:
They are like:
my.jar!/Main.class
my.jar!/Aux.class
my.jar!/Other.class
my.jar!/images/image01.png
my.jar!/images/image02a.png
my.jar!/images/imwge034.png
my.jar!/images/imagAe01q.png
my.jar!/META-INF/manifest
Right now I'm able to load for instance "images/image01.png" using:
ImageIO.read(this.getClass().getResource("images/image01.png));
But only because I know the file name, for the rest I have to load them dynamically.
CodeSource src = MyClass.class.getProtectionDomain().getCodeSource();
if (src != null) {
URL jar = src.getLocation();
ZipInputStream zip = new ZipInputStream(jar.openStream());
while(true) {
ZipEntry e = zip.getNextEntry();
if (e == null)
break;
String name = e.getName();
if (name.startsWith("path/to/your/dir/")) {
/* Do something with this entry. */
...
}
}
}
else {
/* Fail... */
}
Note that in Java 7, you can create a FileSystem from the JAR (zip) file, and then use NIO's directory walking and filtering mechanisms to search through it. This would make it easier to write code that handles JARs and "exploded" directories.
Code that works for both IDE's and .jar files:
import java.io.*;
import java.net.*;
import java.nio.file.*;
import java.util.*;
import java.util.stream.*;
public class ResourceWalker {
public static void main(String[] args) throws URISyntaxException, IOException {
URI uri = ResourceWalker.class.getResource("/resources").toURI();
Path myPath;
if (uri.getScheme().equals("jar")) {
FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap());
myPath = fileSystem.getPath("/resources");
} else {
myPath = Paths.get(uri);
}
Stream<Path> walk = Files.walk(myPath, 1);
for (Iterator<Path> it = walk.iterator(); it.hasNext();){
System.out.println(it.next());
}
}
}
erickson's answer worked perfectly:
Here's the working code.
CodeSource src = MyClass.class.getProtectionDomain().getCodeSource();
List<String> list = new ArrayList<String>();
if( src != null ) {
URL jar = src.getLocation();
ZipInputStream zip = new ZipInputStream( jar.openStream());
ZipEntry ze = null;
while( ( ze = zip.getNextEntry() ) != null ) {
String entryName = ze.getName();
if( entryName.startsWith("images") && entryName.endsWith(".png") ) {
list.add( entryName );
}
}
}
webimages = list.toArray( new String[ list.size() ] );
And I have just modify my load method from this:
File[] webimages = ...
BufferedImage image = ImageIO.read(this.getClass().getResource(webimages[nextIndex].getName() ));
To this:
String [] webimages = ...
BufferedImage image = ImageIO.read(this.getClass().getResource(webimages[nextIndex]));
I would like to expand on acheron55's answer, since it is a very non-safe solution, for several reasons:
It doesn't close the FileSystem object.
It doesn't check if the FileSystem object already exists.
It isn't thread-safe.
This is somewhat a safer solution:
private static ConcurrentMap<String, Object> locks = new ConcurrentHashMap<>();
public void walk(String path) throws Exception {
URI uri = getClass().getResource(path).toURI();
if ("jar".equals(uri.getScheme()) {
safeWalkJar(path, uri);
} else {
Files.walk(Paths.get(path));
}
}
private void safeWalkJar(String path, URI uri) throws Exception {
synchronized (getLock(uri)) {
// this'll close the FileSystem object at the end
try (FileSystem fs = getFileSystem(uri)) {
Files.walk(fs.getPath(path));
}
}
}
private Object getLock(URI uri) {
String fileName = parseFileName(uri);
locks.computeIfAbsent(fileName, s -> new Object());
return locks.get(fileName);
}
private String parseFileName(URI uri) {
String schemeSpecificPart = uri.getSchemeSpecificPart();
return schemeSpecificPart.substring(0, schemeSpecificPart.indexOf("!"));
}
private FileSystem getFileSystem(URI uri) throws IOException {
try {
return FileSystems.getFileSystem(uri);
} catch (FileSystemNotFoundException e) {
return FileSystems.newFileSystem(uri, Collections.<String, String>emptyMap());
}
}
There's no real need to synchronize over the file name; one could simply synchronize on the same object every time (or make the method synchronized), it's purely an optimization.
I would say that this is still a problematic solution, since there might be other parts in the code that use the FileSystem interface over the same files, and it could interfere with them (even in a single threaded application).
Also, it doesn't check for nulls (for instance, on getClass().getResource().
This particular Java NIO interface is kind of horrible, since it introduces a global/singleton non thread-safe resource, and its documentation is extremely vague (a lot of unknowns due to provider specific implementations). Results may vary for other FileSystem providers (not JAR). Maybe there's a good reason for it being that way; I don't know, I haven't researched the implementations.
So I guess my main problem would be, how to know the name of the jar where my main class lives.
Assuming that your project is packed in a Jar (not necessarily true!), you can use ClassLoader.getResource() or findResource() with the class name (followed by .class) to get the jar that contains a given class. You'll have to parse the jar name from the URL that gets returned (not that tough), which I will leave as an exercise for the reader :-)
Be sure to test for the case where the class is not part of a jar.
I've ported acheron55's answer to Java 7 and closed the FileSystem object. This code works in IDE's, in jar files and in a jar inside a war on Tomcat 7; but note that it does not work in a jar inside a war on JBoss 7 (it gives FileSystemNotFoundException: Provider "vfs" not installed, see also this post). Furthermore, like the original code, it is not thread safe, as suggested by errr. For these reasons I have abandoned this solution; however, if you can accept these issues, here is my ready-made code:
import java.io.IOException;
import java.net.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
public class ResourceWalker {
public static void main(String[] args) throws URISyntaxException, IOException {
URI uri = ResourceWalker.class.getResource("/resources").toURI();
System.out.println("Starting from: " + uri);
try (FileSystem fileSystem = (uri.getScheme().equals("jar") ? FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap()) : null)) {
Path myPath = Paths.get(uri);
Files.walkFileTree(myPath, new SimpleFileVisitor<Path>() {
#Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println(file);
return FileVisitResult.CONTINUE;
}
});
}
}
}
Here is an example of using Reflections library to recursively scan classpath by regex name pattern augmented with a couple of Guava perks to to fetch resources contents:
Reflections reflections = new Reflections("com.example.package", new ResourcesScanner());
Set<String> paths = reflections.getResources(Pattern.compile(".*\\.template$"));
Map<String, String> templates = new LinkedHashMap<>();
for (String path : paths) {
log.info("Found " + path);
String templateName = Files.getNameWithoutExtension(path);
URL resource = getClass().getClassLoader().getResource(path);
String text = Resources.toString(resource, StandardCharsets.UTF_8);
templates.put(templateName, text);
}
This works with both jars and exploded classes.
Here's a method I wrote for a "run all JUnits under a package". You should be able to adapt it to your needs.
private static void findClassesInJar(List<String> classFiles, String path) throws IOException {
final String[] parts = path.split("\\Q.jar\\\\E");
if (parts.length == 2) {
String jarFilename = parts[0] + ".jar";
String relativePath = parts[1].replace(File.separatorChar, '/');
JarFile jarFile = new JarFile(jarFilename);
final Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
final JarEntry entry = entries.nextElement();
final String entryName = entry.getName();
if (entryName.startsWith(relativePath)) {
classFiles.add(entryName.replace('/', File.separatorChar));
}
}
}
}
Edit:
Ah, in that case, you might want this snippet as well (same use case :) )
private static File findClassesDir(Class<?> clazz) {
try {
String path = clazz.getProtectionDomain().getCodeSource().getLocation().getFile();
final String codeSourcePath = URLDecoder.decode(path, "UTF-8");
final String thisClassPath = new File(codeSourcePath, clazz.getPackage().getName().repalce('.', File.separatorChar));
} catch (UnsupportedEncodingException e) {
throw new AssertionError("impossible", e);
}
}
Just to mention that if you are already using Spring, you can take advantage of the PathMatchingResourcePatternResolver.
For instance to get all the PNG files from a images folder in resources
ClassLoader cl = this.getClass().getClassLoader();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
Resource[] resources = resolver.getResources("images/*.png");
for (Resource r: resources){
logger.info(r.getFilename());
// From your example
// ImageIO.read(cl.getResource("images/" + r.getFilename()));
}
A jar file is just a zip file with a structured manifest. You can open the jar file with the usual java zip tools and scan the file contents that way, inflate streams, etc. Then use that in a getResourceAsStream call, and it should be all hunky dory.
EDIT / after clarification
It took me a minute to remember all the bits and pieces and I'm sure there are cleaner ways to do it, but I wanted to see that I wasn't crazy. In my project image.jpg is a file in some part of the main jar file. I get the class loader of the main class (SomeClass is the entry point) and use it to discover the image.jpg resource. Then some stream magic to get it into this ImageInputStream thing and everything is fine.
InputStream inputStream = SomeClass.class.getClassLoader().getResourceAsStream("image.jpg");
JPEGImageReaderSpi imageReaderSpi = new JPEGImageReaderSpi();
ImageReader ir = imageReaderSpi.createReaderInstance();
ImageInputStream iis = new MemoryCacheImageInputStream(inputStream);
ir.setInput(iis);
....
ir.read(0); //will hand us a buffered image
Given an actual JAR file, you can list the contents using JarFile.entries(). You will need to know the location of the JAR file though - you can't just ask the classloader to list everything it could get at.
You should be able to work out the location of the JAR file based on the URL returned from ThisClassName.class.getResource("ThisClassName.class"), but it may be a tiny bit fiddly.
Some time ago I made a function that gets classess from inside JAR:
public static Class[] getClasses(String packageName)
throws ClassNotFoundException{
ArrayList<Class> classes = new ArrayList<Class> ();
packageName = packageName.replaceAll("\\." , "/");
File f = new File(jarName);
if(f.exists()){
try{
JarInputStream jarFile = new JarInputStream(
new FileInputStream (jarName));
JarEntry jarEntry;
while(true) {
jarEntry=jarFile.getNextJarEntry ();
if(jarEntry == null){
break;
}
if((jarEntry.getName ().startsWith (packageName)) &&
(jarEntry.getName ().endsWith (".class")) ) {
classes.add(Class.forName(jarEntry.getName().
replaceAll("/", "\\.").
substring(0, jarEntry.getName().length() - 6)));
}
}
}
catch( Exception e){
e.printStackTrace ();
}
Class[] classesA = new Class[classes.size()];
classes.toArray(classesA);
return classesA;
}else
return null;
}
public static ArrayList<String> listItems(String path) throws Exception{
InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(path);
byte[] b = new byte[in.available()];
in.read(b);
String data = new String(b);
String[] s = data.split("\n");
List<String> a = Arrays.asList(s);
ArrayList<String> m = new ArrayList<>(a);
return m;
}
There are two very useful utilities both called JarScan:
www.inetfeedback.com/jarscan
jarscan.dev.java.net
See also this question: JarScan, scan all JAR files in all subfolders for specific class
The most robust mechanism for listing all resources in the classpath is currently to use this pattern with ClassGraph, because it handles the widest possible array of classpath specification mechanisms, including the new JPMS module system. (I am the author of ClassGraph.)
How to know the name of the JAR file where my main class lives?
URI mainClasspathElementURI;
try (ScanResult scanResult = new ClassGraph().whitelistPackages("x.y.z")
.enableClassInfo().scan()) {
mainClasspathElementURI =
scanResult.getClassInfo("x.y.z.MainClass").getClasspathElementURI();
}
How can I read the contents of a directory in a similar fashion within a JAR file?
List<String> classpathElementResourcePaths;
try (ScanResult scanResult = new ClassGraph().overrideClasspath(mainClasspathElementURI)
.scan()) {
classpathElementResourcePaths = scanResult.getAllResources().getPaths();
}
There are lots of other ways to deal with resources too.
One more for the road that's a bit more flexible for matching specific filenames because it uses wildcard globbing. In a functional style this could resemble:
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Consumer;
import static java.nio.file.FileSystems.getDefault;
import static java.nio.file.FileSystems.newFileSystem;
import static java.util.Collections.emptyMap;
/**
* Responsible for finding file resources.
*/
public class ResourceWalker {
/**
* Globbing pattern to match font names.
*/
public static final String GLOB_FONTS = "**.{ttf,otf}";
/**
* #param directory The root directory to scan for files matching the glob.
* #param c The consumer function to call for each matching path
* found.
* #throws URISyntaxException Could not convert the resource to a URI.
* #throws IOException Could not walk the tree.
*/
public static void walk(
final String directory, final String glob, final Consumer<Path> c )
throws URISyntaxException, IOException {
final var resource = ResourceWalker.class.getResource( directory );
final var matcher = getDefault().getPathMatcher( "glob:" + glob );
if( resource != null ) {
final var uri = resource.toURI();
final Path path;
FileSystem fs = null;
if( "jar".equals( uri.getScheme() ) ) {
fs = newFileSystem( uri, emptyMap() );
path = fs.getPath( directory );
}
else {
path = Paths.get( uri );
}
try( final var walk = Files.walk( path, 10 ) ) {
for( final var it = walk.iterator(); it.hasNext(); ) {
final Path p = it.next();
if( matcher.matches( p ) ) {
c.accept( p );
}
}
} finally {
if( fs != null ) { fs.close(); }
}
}
}
}
Consider parameterizing the file extensions, left an exercise for the reader.
Be careful with Files.walk. According to the documentation:
This method must be used within a try-with-resources statement or similar control structure to ensure that the stream's open directories are closed promptly after the stream's operations have completed.
Likewise, newFileSystem must be closed, but not before the walker has had a chance to visit the file system paths.
Just a different way of listing/reading files from a jar URL and it does it recursively for nested jars
https://gist.github.com/trung/2cd90faab7f75b3bcbaa
URL urlResource = Thead.currentThread().getContextClassLoader().getResource("foo");
JarReader.read(urlResource, new InputStreamCallback() {
#Override
public void onFile(String name, InputStream is) throws IOException {
// got file name and content stream
}
});

Spring - validate that all message resources are well configured

We need to add some code to be executed on application load in order to validate that all the messages.properties elements are well defined for all languages.
Is this possible?
Steps: dynamically read on application load all the spring message codes from JSP or java classes then pass through all message resources properties files and validate that nothing is missing from them.
We ended up doing this manually but without using any library.
Steps:
have all the keys used in Java classes or JSP defined in a constant file
Read them using Java .class properties:
Field[] fields = Constants.class.getFields();
String filed[i].get(Constants.class);
Read all messageResources.properties file names from the project using:
String pathToThisClass = MessageResourcesValidator.class.getProtectionDomain().getCodeSource().getLocatin().getPath();
File filePath = new File(pathToThisClass);
String[] list = filePath.list(new DirFilter("(messages).*\\.(properties)"));
DirFilter is a normal class implementing Java's FileNameFilter
Create a class that read the properties from a file using its file name:
public class PropertiesFile{
private Properties prop;
public PropertiesFile(String fileName) throws Exception
{
init(fileName);
}
private void init(String fileName) throws Exception
{
prop = new Properties();
try (InputStream input = getClass().getClassLoader().getResourceAsStream(fileName);)
{
if(input == null)
{
throw new Exception("Enable to load properties file " + fileName);
}
prop.load(input);
}
catch(IOException e)
{
throw new Exception("Error loading properties file " + fileName);
}
}
public List<String> getPropertiesKeysList()
{
List<String> result = new ArrayList<>();
Enumeration<?> e = prop.propertyNames();
while(e.hasMoreElements())
{
result.add((String) e.nextElement());
// String value = prop.getProperty(key);
}
return result;
}
}
The part that does the comparison should be something as the following code that calls the above methods:
List<String> msgResourcesFiles = getMessageResourcesFileNames();
List<String> codeKeys = getListOfCodeMessageResources();
PropertiesFile file = null;
List<String> propKeys = null;
for(String fileName : msgResourcesFiles)
{
file = new PropertiesFile(fileName);
propKeys = file.getPropertiesKeysList();
for(String key : codeKeys)
{
if(!propKeys.contains(key))
{
throw new Exception("Missing key " + key);
}
}
}
Note: or another workaround would be to compare all message resources files to a default one and this way we minimize the code needed from the above explanation.

Categories