Using JNDI properties file in OpenEJB embedded mode - jndi

Eventhough the jndi properties file is kept in the classpath its not loaded and JNDI properties when configured in JVM arguments in ANT its working fine.
<jvmarg value="-Djava.naming.factory.initial=org.openejb.client.LocalInitialContextFactory"/>
How to configure the JNDI thorugh properties so that even when
Context=new InititalContext() is used in the EJB's the JNDI properties that are configured in properties needs to be loaded.
Thanks,
Velmurugan R

Definitely make sure the jndi.properties file is at the root of the classpath and not in a META-INF/ directory or any other location. By root, I mean some directory that is parallel to a where a META-INF/ would be. For example:
foo.jar/jndi.properties
foo.jar/META-INF/MANIFEST.MF
That file is processed by the JVM itself, so if it doesn't work, odds are it is related to it not being on the classpath correctly.

Related

How to set log4j.property to .jar location

I'm Setting up Log4j2 in a Spring-boot application. I now want to create a /log directory exactly where the .jar file is located.
This is needed as we start the java application from a startup script and the configuration should work on both windows and unix developer machines as well as a server.
I already tried with:
<RollingFile name="FileAppender" fileName="./logs/mylog.log"
filePattern="logs/mylog-%d{yyyy-MM-dd}-%i.log">
which just creates a log folder at the directory where the jar gets started.
then I read i should use .\log/mylog.log as .\ points to the directory of the jar file.
But then it just creates a folder called .\log.
I also tried with configuration with jvm arguments and calling them at the log4j2.xml with: ${logFile}. Now a directory gets created called '${logFile}.
The only ${} command working is the directory of the log4j configuration file. But as this is inside the jar it just gets me a pretty useless folder structure
Thanks in Advance
EDIT: In the End what I did was setting up two configuration files, log4j2.xml and log4j2-prod.xml
The log4j2.xml took the system property as Vikas Sachdeva mentioned, while the prod.xml got the location hard coded.
Not really the solution I was looking for but made it work.
One solution is to pass log directory location through system properties.
Configuration file will look like -
<RollingFile name="FileAppender" fileName="${sys:basePath}/mylog.log"
filePattern="${sys:basePath}/mylog-%d{yyyy-MM-dd}-%i.log">
Now, pass VM argument basePath with absolute path of directory containing JAR file -
java -jar myapp.jar -DbasePath=/home/ubuntu/app

classpath property file in grails app is not being used

My problem is that I've put a well-formed properties file in my classpath and told grails to look for the properties file in my classpath, but the properties in the file aren't overriding the existing ones.
In my config.groovy I have the lines:
grails.config.locations = [ "classpath:${appName}-config.properties",
"file:./${appName}-config.properties"]
// verified that appName is being used. It prints out classpath:XXX-config.properties, as expected, where XXX is my appname.
println "grails.config.locations: ${grails.config.locations}"
In my XXX-config.properties, I specify a different DB connection URL (this is the one I want to use):
dataSource.url=jdbc:postgresql://products.yyyyy.ap-southeast-1.rds.amazonaws.com:5432/mydatabasename
Then I put the XXX-config.properties in my classpath and startup tomcat7, but the errors are thrown, saying it can't find my deliberately fake db, meaning the database property did not get overridden.
Now I'm thinking my classpath is wrong, but I do ps aux | grep tomcat which tells me that the command line arg for tomcat7 contains " -classpath :/home/jbu/:_other_paths_here_". Note I put my properties file at /home/jbu/XXX-config.properties. I am slightly concerned about that leading colon, but it doesn't seem to cause any other problems.
So the classpath dir appears correct, so now I thought maybe my properties file isn't properly formatted, so I added a harded "file:/home/jbu/XXX-config.properties" to my grails.config.locations, and the properties were found and successfully overridden, meaning nothing is wrong with the properties file itself.
I don't have any more ideas here. Can someone help me out?
Sidenote: I modified the CLASSPATH variable for tomcat7 by adding this line to /usr/share/tomcat7/bin/setenv.sh:
export CLASSPATH="${CLASSPATH}:/home/jbu/"
This (among with many other things in Grails, it seems) is broken. Sigh...I'm using grails 2.4.5 so I'm a bit surprised that it has been broken since 1.3.5.
http://jay-chandran.blogspot.com/2010/10/grails-using-external-properties-file.html
Grails 1.3.5: How to configure Datasource.groovy to either connect to MySQL or SQL Server

OpenEJB: How to exclude jar from module search?

I use OpenEJB to run unit tests for applications ultimately deployed to WebSphere Application Server. My problem is a(n unavoidable) dependency on the WAS runtime jar. I've added an expression to the exclude property (I've also tried the physical path):
p.put("openejb.deployments.classpath.exclude", ".*?runtime-6.1.*?");
// p.put("openejb.deployments.classpath.exclude", "C:/Users/user/.m3/repository/was/runtime/6.1/runtime-6.1.jar");
p.put("openejb.exclude-include.order", "include-exclude");
This value is confirmed in the logs:
OpenEJB.options-2014-08-07-main--INFO -OpenEJB.options:Using 'openejb.exclude-include.order=include-exclude'
OpenEJB.options-2014-08-07-main--INFO -OpenEJB.options:Using 'openejb.deployments.classpath.include=.*eed-jar.*'
OpenEJB.options-2014-08-07-main--INFO -OpenEJB.options:Using 'openejb.deployments.classpath.exclude=.*?runtime-6.1.*?'
but the jar is still being inspected for loadable modules:
OpenEJB.startup.config-2014-08-07-main--INFO -OpenEJB.startup.config:Found EjbModule in classpath: C:\Users\user\.m3\repository\was\runtime\6.1\runtime-6.1.jar
The result is startup failure for OpenEJB:
org.apache.openejb.OpenEjbContainer$InvalidApplicationException: org.apache.openejb.config.ValidationFailedException: Module failed validation. AppModule(name=)
and
WARNING: can't load com.ibm.ws.management.j2ee.ManagementBean
org.apache.openejb.OpenEJBRuntimeException: Management
I've been creating a 0-length dummy file and renaming runtime-6.1.jar to execute the tests in Eclipse, but I need to restore the jar to run the Maven build (which Eclipse will then bind to, necessitating a restart to rename the jar).
What's the right way to exclude this jar?
You might need to set openejb.deployments.classpath.filter.descriptors to true as well. See http://tomee.apache.org/application-discovery-via-the-classpath.html
Otherwise, filters will not be applied to resources that contain a descriptor file (ejb-jar.xml).
If you still have problems, you can debug through org.apache.openejb.config.DeploymentsResolves and see why your resources aren't excluded.

External properties file with JSF2

I'd like to NOT put my properties file in the war file's classes directory. Can I do this and what would I specify in faces-config.xml for it to use the correct resource-bundle ?
Thanks
Binh Nguyen
Put it in an external folder and add its path to the runtime classpath. Then you can access it from the classpath the usual way as if it's in /WEB-INF/classes (which is just by default part of the classpath).
Adding the path to an external folder to the classpath is best to be configured at the webserver level. In Tomcat for example, you can specify it in the shared.loader or common.loader property of Tomcat's /conf/catalina.properties file.
shared.loader = /path/to/propertiesfiles

Configuring log4j at runtime

I'm using org.apache.tools.ant.listener.Log4jListener to manage logging with my ant script. The ant script is highly configurable and designed to be run different ways with different parameters and therefore I need to be able to log to files specified at runtime. I have a log4j.properties which specifies a log file to be build.log, and despite my attempts to launch ant redefining properties defined in log4j.properties have been unsuccessful.
The build ignores them and continues to write to build.log. I haven't found much support regarding writing to custom files unless it's in Java with their Logger class.
Perhaps I'm thinking this through wrong. log4j.properties isn't treated in the same way as a property file in an ant script (hence overrideable from the command line)? Is there a way I can do this intelligently without writing a custom task or something?
You setup your log4j.properties file using a system property that you can define dynamically on the command line. The property below is "${logfile.name}". An example log4j configuration would be like this:
# logfile is set to be a RollingFileAppender
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${logfile.name}
log4j.appender.logfile.MaxFileSize=10MB
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=[%-5p]%d{yyyyMMdd#HH\:mm\:ss,SSS}\:%c - %m%n
The command line option to pass a property, when calling "ant", is "-Dlogfile.name={runtime path/filename of log file}". Replace {runtime path/filename of log file} with your file name. When ant is run this value is set as a system property. That system property is then substituted into the log4j.properties at runtime.
http://ant.apache.org/manual/running.html

Resources