using property-placeholder for in jar properties file - java

I use this line :
context:property-placeholder location="classpath*:resources/BLLresources/MQ.properties"
in a spring.xml file to look for MQ.properties which is inside the jar where the xml file is (in config folder at the root level).
I got error :
Caused by: org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: {{uriMesarimReadQueue}} due to: Properties file classpath*:resources/BLLresources/MQ.properties not found in classpath
If I put the properties file outside the jar it works fine.
the jar is in the classpath.
any advice ?

I think your have your references setup incorrectly. In a normal java project the resources folder would be located in src/main/resources, this folder gets mapped to the root of the generated jar file. Therefore, if you had a file src/main/resources/BLLresources/MQ.properties, the mapping would be classpath*:BLLresources/MQ.properties

Related

Log4j2 configuration file in Java EE project

I have simple Maven Java-EE based on Jersey web service in my Eclipse that runs in Windows 10. Project is created from jersey-quickstart-webapp artifact. I'm trying to use Log4j logger, but can't find where to place log4j2.xml file. I was trying to place it together with source code java files, but logger complains regarding configuration file not found. What is the right place to keep configuration file?
Put it into resources directory. Usually, all configuration files are kept there.
According to docs , Log4j2 will look for the configuration file in the following orders :
Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration using the
ConfigurationFactory that matches the file extension.
If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.
If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.
If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the
classpath.
If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.
So you can see that apart from (1) , all are looking the configuration file from classpath . In the standard Maven project , /src/main/resources is the root of the classpath .So you can put log4j2.xml in /src/main/resources.
Otherwise, you have to explicitly specify the configuration file by log4j.configurationFile system property :
-Dlog4j.configurationFile=path/to/log4j2.xml

Moving mybatis property file to a custom location in tomcat

I have a web application which needs mybatis to connect to a sql server.
I have a separate property file as jdbc.properties which contains the dburl, username, password etc. and mybatis-config.xml file has following to read that
<properties resource="jdbc.properties"/>
If I add jdbc.properties file to the src folder it works fine.
But I have several other property files located at "catalina_home/bin/config" folder so I would like to move my jdbc.properties file also to that location.
But mybatis is not able to read the property file from their no matter how I changed the path to the resource.
However if I moved the jdbc.properties file to the "catalina_home/lib" folder it works. Therefore then I tries resource path like this "../bin/config" but it didn't worked either.
My question is, can I move the jdbc.properties file to the bin/config folder and let mybatis to read it from there.
EDIT: Since my production environment may change its CATALINA_HOME location I cannot give the full path to the config file location.
The reason that mybatis cannot find these properties files is that tomcat's bin/config folder is not in classpath of tomcat (or your web application) unlike $CATALINA_HOME/lib. And mybatis looks for file specified in resource attribute in classpath.
If you use url attribute you can specify full path name to file there.
<properties url="/full/path/to/you/jdbc.properties"/>

Liquibase file path in Spring Configuration file

I have a Spring Configuration java file in my project (i.e. with #Configuration annotation). Now I'm creating a bean of SpringLiquibase in this class. The main file of changelogs i.e. db.changelog.xml file is in resources folder. So directly it's accessible from resources folder if I use springLiquibaseObj.setChangeLog("classpath:db.changelog.xml"); method. But when I try to access other xml files from db.changelog.xml file with <include file="src/main/config/db/db.changelog-main.xml" /> I get error that folder and file is not found.
Here, I want to keep db.changelog.xml file in resources folder and all other new changelog files in config folder which should be at same level as resources. The reason for this is, in resources folder, packages are there so folder structure config/1.0.0 is considered as config/1/0/0 which I don't want.
Is there any way to achieve this structure (shown below) with the current set up in my project?
config
1.0.0
db.changelog.201412120101.xml
1.1.0
2.0.0
resources
db.changelog.xml
or
config
1.0.0
db.changelog.201412120101.xml
1.1.0
2.0.0
db.changelog.xml
resources
I found the solution for this. Before this, I had copied the folder src/main/config into src/main/resouces in netbeans so it took that folder as package. So the folder name - config/db/1.0.0 got changed to config/db/1/0/0, but when I created new folder in resources folder, it worked and all the files in folder 1.0.0 are accessible correctly. Now, my folder structure is like this:
resources
config
db
1.0.0
db.changelog.201412120101.xml
1.1.0
2.0.0
db.changelog.xml
I can access db.changelog.xml file from my Spring class as,springLiquibaseObj.setChangeLog("classpath:db.changelog.xml"); and other changelog files from db.changelog.xml file as, <include file="classpath:config/db/1.0.0/db.changelog-201412120101.xml" relativeToChangelogFile="false" />.
Liquibase uses the application classpath, so as long as config is in your classpath you should be able to load them as well.
They would have a path relative to config, so something like "src="1.0.0/db.changelog.201312120101.xml" in your example.
If config isn't in the classpath, you can use the relativeToChangeLog="true" attribute with a src="../../config/1.0.0/db.changelog.201412120101.xml" but having config directly accessible by liquibase is the cleaner option.

Adding spring XML files to classpath (Windows cmd-line)

I am trying to run a jar file via cmd line that uses Spring and a spring xml configuration file.
The cmd line call is similar to:
java -cp lib/MyJar.jar my.package.MyClass
The error I get is:
Caused by: java.io.FileNotFoundException: class path resource
[myPath/mySpringCfg.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
My manifest classpath is similar to:
Class-Path: 3rdPartyJar1.jar 3rdPartyJar2.jar ./myPath/
The call that loads the file equates to:
context = new ClassPathXmlApplicationContext("myPath/mySpringCfg.xml");
Is there a way to correctly pull in XML files in the classpath so that Spring will work as expected? It seems like the classpath docs only talk about archive files and folders.
Thanks!
UPDATE
It seems to run fine when I switch over to FileSystemXmlApplicationContext. I guess the ClassPathXmlApplicationContext cannot be used from command-line
Your reference to the XML is myPath/mySpringCfg.xml - this means that myPath has to be in the classpath.
Change your manifest to be:
Class-Path: 3rdPartyJar1.jar 3rdPartyJar2.jar ./
This way myPath will be a part of the classpath and not just its contents.
Note:
The application configuration XML is a part of your application's code, don't mistake it for a configuration.
If you want configuration - put it outside in a properties file and use place-holders in your XML configuration file.
Update:
I think the root cause of your problem is in the code (I didn't test it though) - try this instead:
context = new ClassPathXmlApplicationContext("/myPath/mySpringCfg.xml");
The difference is in the '/' before 'myPath'
I am not aware of the architecture of your project, but why not place your xml configuration file into your project jar?

Load persistence.xml from aar in tomcat

I have a openJPA based project that I need to deploy it in format of aar into the following folder under Tomcat.
tomcat\webapps\axis2\WEB-INF\services
But it seems that the service cannot load the persistence.xml file in the META-INF folder within the aar file. I found a solution to rename the aar file into jar, like DummySerivce.aar -> DummySerivce.jar and then put the DummySerivce.jar into the lib folder, to be exactly
tomcat\webapps\axis2\WEB-INF\lib
This time, the persistence.xml could be loaded and the service worked well. But this unnecessarily imported a jar file which was not included in the specification.
So my question is, is there any way to load the persistence.xml from a aar file? is it possible to overload the path of persistence.xml file in code?
btw, only entity information are left in the persistence.xml, database connection parameters are already define in code, and I'm using Axis2.
Thanks!
I have found a solution.
By adding the following line into the services.xml, persistence.xml packed in aar file could be successfully loaded.
<parameter name="ServiceTCCL">composite</parameter>
for detailed info. please refer to http://wso2.org/node/1131
Now checking if there's any side effect.
TCCL - Thread context class loader
default - Status quo, normal behavior
composite - TCCL contains all
jars from environment (say webapp) and those specified in the
service
archive (aar) file under /lib service - TCCL contains all jars from
service archive (aar)

Categories

Resources