Get relative path to a folder inside OSGI Bundle - osgi

I've developed a sample OSGI bundle and deployed it into Karaf, and the bundle contains a folder on the same level as the source.
My class needs to load data from this folder, but I got an error each time:
java.lang.IllegalArgumentException: No such group file: ./data/...
My question is: how do I use a relative path to a folder inside an OSGI Bundle?

Did you try Bundle.getEntry? It is one the methods that can be used to access the contents of a bundle.

solved using context from bundle:
URL wfl = context.getBundle().getResource("data1/file.txt");
or
URL url = MyClass.class.getClassLoader().getResource("data1/file.txt");
if (url != null) {
mFile = FileLocator.toFileURL(url).getFile();
Hope this help someone

Related

giving Freemarker resource folder in deployed application for spring boot

I'm trying to give resource folder for Freemarker template below is my bean config
Configuration freeMarkerConfig() throws IOException {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_29);
cfg.setDirectoryForTemplateLoading(new ClassPathResource("ftl").getFile());
return cfg;
}
and I have ftl folder in the resources folder
-- java
-- resources
-- ftl
-- template.ftl
while deploying it works fine in my local machine but fails in my docker container with exception
java.io.FileNotFoundException: class path resource [ftl] cannot be resolved to absolute file
path because it does not reside in the file system: jar:file:!/BOOT-INF/classes!/ftl
I need to keep ftl in the resources folder and give the directory path to configuration. I don't know how to debug further.
Property spring.freemarker.template-loader-path is available.
spring.freemarker.template-loader-path=classpath:/ftl/
Or, use setTemplateLoaderPath("classpath:/ftl/") instead of setDirectoryForTemplateLoading().
I found the issue after repeated searching. Issue is because of this
Classpath resource not found when running as jar

How do I locate resource files in a Spring Boot project deployed to Elastic Beanstalk?

My Spring Boot project contains an XML file within the src/main/resources folder, which is the common location for such a file.
Running locally and also on Pivotal CloudFoundary, I am able to locate the file and read it in, but on Beanstalk the process results in an empty file.
Code to locate and read file:
URL url = getClass().getResource("/myFile.xml");
LOG.info("File location: " + url.toString());
Resulting log entry:
File location: jar:file:/var/app/current/application.jar!/WEB-INF/classes!/myFile.xml
When I SSH into EC2 instance, I can find the jar in the specified directory.
Do I need to configure Maven to move this file somewhere?
UPDATE
I've since realized that I need to treat this file as in InputStream as it's packaged within the jar.
I'm now using the following code which results in the follow errors:
FileUtils.copyInputStreamToFile(new ClassPathResource("myFile.xml").getInputStream(), myFile);
java.lang.NullPointerException: null
at org.apache.commons.io.FileUtils.openOutputStream(FileUtils.java:345) ~[commons-io-2.5.jar:2.5]
and
FileUtils.copyInputStreamToFile(new ClassPathResource("classpath:myFile.xml").getInputStream(), myFile);
java.io.FileNotFoundException: class path resource [classpath:myFile.xml] cannot be opened because it does not exist
Thanks!
How does your pom.xml look like? Maybe there is a resource-filter active?
In a spring application you could use File file = ResourceUtils.getFile("classpath:myFile.xml"); to read a resource file.
Could you check the result by using ResourceUtils?
Here the link to the api-documentation:
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/ResourceUtils.html#getFile-java.lang.String-

Classpath resource not resolving in spring boot

I am using spring boot and I have a file in resources folder. I am using digital ocean machine and when i run the application using java -jar mywebapp.war, I am unable to access the file from classpath. I am accessing it using following standard syntax:
File file = new ClassPathResource("mfile").getFile();
I am getting error that class path resource cannot be resolved to absolute path. The problem I see is that it is showing the path with ! marks as follows:
/home/u/webapp/target/mywebapp.war!/WEB-INF/classess!/mfile
What am I doing wrong here?
Since you're running it with java -jar you should build it as a JAR file instead of WAR.
Read more: https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html
Get file does not work while running as jar.you should get it as a resource Stream.
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("/file.xsd") ;
File file = File.createTempFile("file", ".xsd");
try {
FileUtils.copyInputStreamToFile(inputStream, file);
} finally {
IOUtils.closeQuietly(inputStream);
it gets you a file. if the requirement is to get as a file.

Path for images folder in GWT project

I have the following line of code to set the URL of an image I want to display
img.setUrl("images/img1.jpg");
Where should I place my images folder in order for it to get picked up correctly. Currently I have it in my WEB-INF folder but this does not appear to work. I get the error on my console "WARNING: No file found for: /images/img1.jpg"
I created my project using Eclipse and I have not changed any folder structure.
You should make a folder named images under your war/project_folder and place the images there.
And while setting the Url you shoud do
image.setUrl(GWT.getModuleBaseURL()+"images/im1.jpg");
You can use either GWT.getModuleBaseURL() or GWT.getHostPageBaseURL().
Create a public folder (named "public") alongside with folders "client" and "server". The content of the public folder will be copied into the war.
>src
>com
>test
>client
--->public
>images
>server
>shared
VoilĂ !
It is always better to use absolut URL rather than relative URL by using GWT.getModuleBaseURL(). If you want to use relative path, then your image should be in the war as described by your relative path.
In your case it is
war->images->img1.jpg
You need to put the images in the web app directory.
You can check your web app directory here
Proeect --> Properties --> Google --> web Application
Here you will find the WAR directory
So in this directory , you need to put your images.
You can put the image directly or else you can make folders as you want
sample -
image path --> WebContent/img/pics/recent/data/1/1.jpg
String imagePath = "/img/pics/recent/data/1/1.jpg" ;
Image img = new Image(imagePath);
NOTE :- How ever GWT Image does not understand absolute file path , you need to use relative file path

Using image resources in a jar using maven

I have a java/maven project that uses tango icons, so I'm using the following dependency:
<dependency>
<groupId>org.freedesktop.tango</groupId>
<artifactId>tango-icon-theme</artifactId>
<version>0.8.90</version>
</dependency>
And the following code, which would work ok before I moved the proyect to maven:
imageIcon = new ImageIcon(this.getClass().getResource(org/freedesktop/tango/22x22/actions/address-book-new.png));
However, it now results in NullPointerException for some reason.
The generated jar does not contain the images, nor reference them in any way, but classes in other jars are.
I've investigated a lot, but I've found most people bundle their images with their source. This isn't my case.
What do I need to do for maven to reference and/or package these images propertly?
How can I get maven to build a standalone runnable jar (this is a swing application).
If you add a slash before the path it works:
ImageIcon imageIcon = new ImageIcon(this.getClass().getResource("/org/freedesktop/tango/22x22/actions/address-book-new.png"));
From the API:
Before delegation, an absolute resource name is constructed from the
given resource name using this algorithm:
If the name begins with a '/' ('\u002f'), then the absolute name of
the resource is the portion of the name following the '/'. Otherwise,
the absolute name is of the following form:
modified_package_name/name Where the modified_package_name is the
package name of this object with '/' substituted for '.' ('\u002e').

Resources