How to get the jar filename of web-fragment - resources

In servlet 3, web-fragments are jars deployed under WAR's WEB-INF/libs. The resources of web-fragments will be merged and be traded as they are resources of WAR. But that's not suitable for every situation.
For example when I want to get some resources under web-fragment's WEB-INF directory, i cannot read them directly, because no matter it's in a jar or a war, the resources under WEB-INF is simply not accessable by URL. Even worse, the typical way to read such resource, with the help of ServletContext doesn't work for web-fragments, because they are jars, not a real directory structure.
Finally I found a solution, to read them with ClassLoader, anyway I still need the exact resource name, I cannot do something like give me all xml files under the directory WEB-INF/myconfig/ The only way I think is to scan all web-fragement jars, build a directory structure in memory. However I still don't know how to get the jar names of all web-fragments. If I cannot get them, I must scan all jars under WEB-INF/libs. Is there any better solution?
Thanks

Related

Spring boot alternate src/main/resource directory on classpath

This post is not about serving static resources in Spring Boot. There are other posts that cover that nicely. I think I have a grasp now of the different /public /static directory options, whether located underneath src/main/resources or not. What I want to know is whether I can specify an additional directory in my project whose contents will also be placed at the root of the classpath (as opposed to the web document root).
Specifically, I am referring to my translated resource bundles (e.g., index.properties, index_fr.properties, index_de.properties, etc.). In my pre-Spring Boot projects I have a "bundles" directory designated for all my translated property files. I would prefer to continue using a bundles directory for my translated strings and not have to place them in my src/main/resources directory with the main application property files.

How to load a resource from disk in OSGI environment

Part 1: I've been trying to load a (XML) file as a resource from disk using bundle class loader. I can't package the file in a bundle beforehand as it'll be generated at runtime. I tried searching too, and almost always, people talk about loading resources from within a bundle (either same or different). So is it even possible to load a resource from disk in an OSGi environment using bundle classloader? If yes, how can it be done?
Part 2: Now I need to add a constraint to the above. In my complete scenario, while I'd be generating the file, it would be loaded by a third-party bundle. In this case, what could be done (generate in a certain location, any changes to classpath etc.) so that the third-party bundle's class loader could find the generated file?
Using: apache karaf 3.0.2, ubuntu 12.
Part 1:
So is it even possible to load a resource from disk in an OSGi environment using bundle classloader?
Resources (read-only files on the classpath) can be loaded with classloaders, not ordinary files from any folder of the disk. When you want to process the content of files from the ClassPath, you should use the classloader.
You want to generate a temporary file (generated and processed at runtime) so you should use the standard Java API for that:
File myTmpFile = File.createTempFile(...);
For more info, see the javadoc of this function: https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile(java.lang.String,%20java.lang.String)
Part 2:
The third bundle should have an API that either accepts a File, URL, Path or other type instance that can point to a file in the file system.

WS02 ESB - How to get Custom java class property file on the classpath

I have loaded a custom jar file into WSO2 by placing it into the /repository/components/lib directory, performing a restart. I then call that class from a script mediator using inline groovy. The groovy script recognizes the class, however the custom class is attempting to load a properties file that must be on the classpath. I have put that property file nearly everywhere but I keep getting an error that it cannot find the file on the classpath.
I am running the standalone WSO2 ESB 4.7.0. I have put the file as part of the jar, I have also attempted to place it in several directories within the WSO2 file structure as well. All to to avail.
you could try to register a resource in the carbon registry and add a Property to this Resource. Basically there are two ways (in java...):
Here is an example how to connect to the registry via a service with the PropertiesAdminServiceStub: http://www.massapi.com/class/org/wso2/carbon/registry/properties/stub/PropertiesAdminServiceStub.java.html
The most important here is that you authenticated your user, the result is a cookie which yoou have to add to the stub.
The other would be something like this (probably a duplicate of your question)
I am unable to get the list of services with in the applicaton i.e.; wso2 governance registry? I am working with binary code
The last one asumes that the carbon-context is available, means you are running the search inside the wso2 like a feature for example.
Unfortunately there is no place to put that properties file. Luckily this jar file, is an in house entity. It was written to search the classpath for the properties file and upon not finding one on the classpath to throw an exception. We ended up rewriting the code that loads the properties file to upon not finding the file on the classpath to search in a directory which we specified as a system environment variable in the wso2server.sh file. Not very elegant, but it is working perfectly.

Excluding jar files from GWTP war file

Currently I am working on a GWTP application. While deploying to application server I remove all the jar files from the application.war file and then I push the war to webapps. I have put all the jars in the application server lib directory. The application works fine. I just have to know, can we remove all the jars from WEB-INF/lib of war file. Is this approach right? please let me know.
And also I have another question. I manually remove the jar files from the created war file which got created using ant. Please give me examples to automatically remove jars from build.xml.
I see only disadvantages from this approach:
If you want to run another application on your server which use an other version of a library, you can't.
Each time you update a jar in you dev environment, you will need to manually copy it to your server lib/ directory instead of having it automatically packaged.
Why having them in WEB-INF/lib bothers you?

About Maven resources

From the stand point of a Maven project, should files stored there be accessed in any special way or be treated as any other file where "resources" is just another directory?
What makes this directory special? Is it fair to say that "resource" is anything that is not a source file and that is used by a program?
I am a bit confused here. Please clarity
All files in the resources directory get added to your jar (or war) without being compiled. Generally things like config files or other non-source resources are put in this directory, although as long as your files don't end in ".java" they could live in the sources directory and the resulting artifact would be the same.
To access a file in the resources directory you would use the ClassLoader.getResource or getResourceAsStream methods.
The other feature of resources when using Maven is that you can include property tokens that will be replaced by Maven as part of building your project. For instance:
This line in a resource file
artifactName=${project.build.finalName}
Would be replaced with something like:
artifactName=my-project-1.0.0
Any of the properties available within Maven can be replaced in your resources.
It's just a standard. By default, the contents of the resources directory are copied to the same target directory as the .class files and, if packaged as a jar, in the root of the jar.
You can also specify how resources are encoded. If you don't, you effectively preserve the encoding of the system on which you built, which is non-portable and is something Maven will warn you about.

Resources