java.nio.file.DirectoryStream in WAR - java

I'm trying to work with nio's way to read files from directory.
I've come across the java.nio.file.DirectoryStream
which is cool because now I don't need to maintain a list of files in the directory by myself and I like this idea.
Now, my code is going to be run inside the WAR so that it will read the resources (say some xml files) packed inside jars inside this WAR.
I couldn't find a good example of working with DirectoryStream like this. So my question is how can I use this approach (not necessarily this particular class of course) in my situation?
Maybe there are some thirdparties that provide the similar abstraction...
In addition I would like to test this code from my unit test (which means that ideally this code shouldn't really assume that it runs inside/outside the WAR if its possible of course).
Thanks and have a nice day

I would suggest creating a temporary folder and copying the needed resources into this directory. Now you can use the DirectoryStream-Class, because the files are placed in the filesystem.

Related

Resource Handling approach in Maven

I am handling below way to work with files.
1st Approach:
I am keeping my files in D:\Projects\JavaProjects\LearnCucumber\src\test\resources\
-With the help of ClassLoader, I am getting the path and working on files
ResourceUtils.class.getClassLoader().getResource(".").getPath();
2nd Approach:
Now, I keep file in D:\Projects\JavaProjects\LearnCucumber\BrowserDrivers\
using System.getProperty("user.dir") I am dealing with files in this approach.
Now Which approach is efficient, when we run our code in different platform(windows,linux) in terms of handling files. Does it really makes difference?
Try to avoid working with files on the filesystem as those are usually less portable from one operating system to the next one. Also if you put files at certain locations other users of your software need to have those files available at the same location as well, whereas with your approach #1 you can ship these files directly with your application (packaged as jar) and access it from the classpath easily.

Is it proper for unit tests to use premade files?

I am writing unit tests for my Java program. My program does a lot of things that involve reading from a file that the user inputs and creating a new file based on the contents of the inputted file.
Currently, my unit tests are using premade files made for testing that I've placed in the resources source directory. When it creates a file, it places it in the same resources source directory.
I've looked at a lot of questions and answers on here, and there are so many varying opinions on how to handle files in unit tests. Is it proper to use these premade files within my unit tests, or is there a better solution?
Yes, the standard practice is to place your static input files for testing in the src/test/resources directory. If you main code generates output, the cleanest place to put it during testing is in your platform-specific tmp directory. In Java, this is under the System property java.io.tmpdir:
System.out.println(System.getProperty("java.io.tmpdir"));
If you use this directory, it will be portable between developers running it under MacOS, Linux, or Windows - as well as any build server.
It is also a good idea for your test to delete these temporary files when the test is complete, and for your test to not make any assumption about temporary files that may exist from a previous test run.
Whether or not static "premade" files are appropriate largely depends on the logic you're trying to test and how you want to manage your test data. Perhaps you need test data that contains a date that is relative to today's date. In this case using a static file could be more trouble than writing code to calculate a relative date.
As JB Nizet pointed out in his comment it's generally not a good idea to store test data alongside your source code. I'm not sure if you're familiar with maven and its standard directory layout but you might take a look at it and see how it helps keep the test and source files separated.

What are the criteria(in the programming point of view) that one should keep in mind while creating a java jar(as library) file for android

I know how to create a jar file using Eclipse.
I was trying to create a share library so that I can avoid redundant source code. I have figured out that a jar should be :-
independent
should not make call to external class attributes(properties)/methods except the standard library imports.
The resources should be given as a parameter to jar file to perform a action.
Should work as a independent entity.
I tried to well organised my code in different packages also added MANIFEST.MF file.
This is first time I'm trying for data abstraction.
I would like to request suggestions/instructions as per the programmer point of view, what are the criteria that jar code should have ?
Is it good idea that my jar is or depend on another jar (viz java mail api jar) ?
Thanks in advance.
As you've tagged this with Android, I assume that Android is the intended use case.
The easiest way to share your code between several projects is probably to create a library project, this way you can keep the source code at hand too (less convenient to attach source to the jar every time you use it).

Extract a particular folder from a jar and copy it to a desired destination on my system

I need to extract the resource folder from inside a jar to a desired location in my system. I want to do it by calling a function in a class, which is in the same jar.
I don't want to copy one file at a time. Can you please suggest me a way in which I can copy the entire folder?
I initially thought of compressing them into a zip, and copying it elsewhere, and extracting it.
How will this work? Is there a more efficient way to do this?
Thanks in advance.
If you are going to do this using java API I know only one way: you have to use JarInputStream or ZipInputStream, iterate over Zip entries, detect which entries belong to the folder and extract them, i.e. read from zip and write to disk. There is no other "magical" way.
But if you want you can probably use some kind of higher level API. Check VFS from Jakarta: http://commons.apache.org/vfs/
It provides API that probably does what you need.
You could use Runtime.exec api to execute something similar to the following :
jar xf <your_jar_file_name> <path_to_directory_to_be_extracted>
In this way, you do not have to create specialized class to handle Jar files and you can focus on solving actual problem at hand.
Note : this is restricted to JDK may not work on JRE.

Using the ClassLoader method to retrieve all resources under classes as Input Streams

My problem is one that you would think is quite common, but I haven't so far managed to find a solution.
Building a Java web app under Tomcat 5.5 (although a requirement is that it can be deployed anywhere, like under a WebLogic environment, hence the loading resources as streams requirement). Good practice dictates that resource files are placed under WEB-INF/classes and loaded using the ClassLoader's getResourceAsStream() method. All well and good when you know the name of the resource you want to load.
My problem is that I need to load everything (including recursively in non-empty sub-directories) that lives in a subdirectory of classes.
So, for example, if I have the following under WEB-INF/classes:
folderX/folderY
folderX/folderY/fileA.properties
folderX/fileB.properties
I need the fileA.properties and fileB.properties classes to be loaded, without actually knowing their names before the application is started (ie I need the ability to arbitrarily load resources from any directory under WEB-INF/classes).
What is the most elegant way to do this? What object could I interrogate to find the information I need (the resource paths to each of the required resources)? A non-servlet specific solution would be best (keeping it all within the class loading framework if possible).
Thanks in advance!
As far as I am aware, there is no such ability, since the classloader only attempts to load things it is asked for. It doesn't pre-fetch all items on the classpath, or treat them as a directory structure.
The way I would solve the problem is create a directory listing in a text file of all relevant resources at build time and include that in the war, and then walk it through that way.
You can do that with some tricks :)
Get the resource as URL, extract the protocol :
file protocol - get the URL path and you have a folder, scan for files.
jar/zip protocol - extract the jar/zip path and use JarFile to browse the files and extract everything under your path/package.

Categories