How to access resource files in Java code running on Nifi? - apache-nifi

I've tried setting a class loader in the init() function like so:
classLoader = Thread.currentThread().getContextClassLoader();
I subsequently try the following in my OnScheduled method:
InputStream in = classLoader.getResourceAsStream(filename);
My Java project has a file at src/main/resources/foo/bar. Regardless of whether I pass "foo/bar" or "/foo/bar" as "fileName" in the above code, classLoader.getResourceAsStream() returns null. I have verified that the file is inside the NAR archive, at META_INF/bundled_dependencies/foo/bar.
What's the best way of accessing the file's contents from my code?

MyProcessor.class.getResourceAsStream(...);
Is the way to go here. You want to use the classloader associated with your customer processor or controller service, not the current running thread.

Related

Need to get path to a folder in resources

I am using Spring boot application. I have this structure in my resource folder
resources
|__customers
|__retail
I have to pass the path to this folder to one of the beans for writing files.
The names of the files are dynamic so I cannot pass a predefined value to the file.
To do this I tried
#value(${classpath:resources/customer/retail} )
Resources resource;
// Also tried
ResourceLoader loader = new FileSystemResourceLoader();
ResourceUtils.getURL(filePathDump).getPath().getClass().getResource(
"indexingData/publication/analysis/dump"
);
// and several other options
but it shows throw file not found or resource not found Exception, Now I checked the way path are defined for each one of them and I am positive they were all specified correctly
I need a way to pass this path to a folder in resources. Please help
you can autowire org.springframework.core.io.ResourceLoader and then get file as follows:
resourceLoader.getResource("classpath:customer/retail/someFile.txt")

Spring Boot classpath: vs relative path

In Spring Boot, to access a resource, say myresource.json, I can use both classpath: or a relative path, like ./myresource.json
What is the difference? Which one should I use?
When you call getResource() on a specific application context, and the location path specified doesn't have a specific prefix like ./myresource.json, you will get back a Resource type that is appropriate to that particular application context.
If getResource() was executed against a ClassPathXmlApplicationContext instance it will return a ClassPathResource.If the same method was executed against a FileSystemXmlApplicationContext instance, you'd get back a FileSystemResource. For a WebApplicationContext, you'd get back a ServletContextResource, and so on.
As such, you can load resources in a fashion appropriate to the particular application context.
On the other hand, you may also force ClassPathResource to be used, regardless of the application context type, by specifying the special classpath: prefix.
See this doc

Heritrix 3.2.0: Writing and Adding Extensions

I am currently working with Heritrix and I have a standard installation (this one: http://builds.archive.org/maven2/org/archive/heritrix/heritrix/3.2.0/) and it works fine.
But now I want to write and add my own extensions e.g. change the priority of urls which should be crawled or just a simple extractor. I can inspect the java code of an existing extractor but how can I add it to the crawler?
I tried to export my java test project to a jar file and put this file in the lib folder of Heritrix (where the other libraries are). Furthermore I added a bean to my job's cxml file.
But after starting I got this error: 2014-11-07T19:51:40.296Z SEVERE Could not instantiate bean class [myModule.TestClass]: No default constructor found; nested exception is java.lang.NoSuchMethodException: myModule.TestClass.(); Can't create bean 'myModule.TestClass#0'
It is just the extractorHTML renamed and in a new project and exported to a jar file.
Any idea what is wrong? I read all the documentations but there are only explanations how to write extensions and not how to add it?
Greetings and thank you :-)
I think the issue is the class loader requires a default constructor (A constructor - which takes no arguments) Add a default constructor
public YourClass() { }
and the required getters and setters for setting the member variables.

Flyway callback classpath

I'm using Flyway Maven Plugin to do my migrations.
I wrote a callback class to go in a resource directory and run the function/procedure files. However it doesn't find some files in the resource folder.
I added the function files in the folder src/main/resources/db/functions.
In my flyway callback code, I call the following code, but I get a null url.
URL url = ClassLoader.getSystemResource("db/functions/mySQL.sql");
I also tryed some variations of getting the system resourse, like add classpath before and some variations to get the resources.
The same code worked if I created a class with the main method using the Flyway Java API, it seems a classloader problem in Maven. How can I do this?
Two options.
Option 1:
Don't forget to call compile to make sure the resources are available on the classpath
Use Thread.currentThread().getContextClassLoader().getResourceAsStream()
Option 2:
Use a FileReader

What is difference between class path , file system?

I know that:
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
loads context definition from an XML file located in the classpath, treating context definitions as classpath resources.
ApplicationContext context = new FileSystemXmlApplicationContext("bean.xml");
loads context definition from an XML file in the filesystem.
XmlWebApplicationContext
loads context definition from an XML file contained within a web application.
But, what does it exactly mean??
Thanks :)
ClassPathXmlApplicationContext will read files from your classpath. They must be in classes folder of your web application or in a jar in your libfolder.
FileSystemXmlApplicationContext can access all your file system, for example c:/config/applicationContext.xml.
XmlWebApplicationContext certainly can access to files contained in your web application, but this is not the most important thing. It implements WebApplicationContext and this means that it will detect ServletContextAware beans, register custom scopes (request, session, ...) among other things.
FileSystemXmlApplicationContext- You need to provide complete full path of xml bean
ClassPathXmlApplicationContext - In this case you DONOT need to set full path, as long as classpath is set
I think above opinion may have something wrong, FileSystemXmlApplicationContext can not access your whole file system, what it can only scan is your whole project folder.In order to prove my conclusion i make a example, first using ClasspathXmlApplicationContext and everything is normal, the second time i move beans.xml file to my desktop folder, so there is no beans.xml file in the project hirachy, and change ClassPathXmlApplicationContext to FileSytemXmlApplicationContext and something goes wrong, error trace below:
INFO: Loading XML bean definitions from file [/Users/crabime/Development/IdeaProjects/springInterview/Users/crabime/Desktop/beans.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [/Users/crabime/Development/IdeaProjects/springInterview/Users/crabime/Desktop/beans.xml]; nested exception is java.io.FileNotFoundException: Users/crabime/Desktop/beans.xml (No such file or directory)
So FileSystemXmlApplicationContext can only detect the current project all folder. For example you make a directory which named config under the project root directory, and you can change your Main Class code like below:
ApplicationContext atx = new FileSystemXmlApplicationContext("/config/beans.xml");
And everything will ok again. So if all like sinuhepop said i think there should something need to be changed.

Resources