I'm writing a java Application for Tomcat 7.
I have a bean configuration for a class that creates a log file and appends information to it.
now the question is how can I know the tomcat log directory path in bean configuration.
for now I have the following bean:
<bean id="foo_logger" class="com.bar.LoggerBean">
<!-- <property name="logPath" value="/path/DWHEventGenerator.log"/> -->
<property name="logPath" value="/var/lib/tomcat7/logs/mylog.log"/>
<property name="logLevel" value="ALL"/> <!-- ALL, FINE/R/ST, INFO, SEVERE, WARNING, OFF -->
</bean>
what i'd like to do is instead of specify /var/log/tomcat7/log, is to specify some variable that will indicate the actual path of the logs directory of tomcat. is that possible ?
thank you.
The simplest approach would be to use catalina.base from the system properties in the logPath property value
<property name="logPath" value="${catalina.base}/logs/mylog.log"/>
This property will be set by Tomcat's launch script (catalina.sh/catalina.bat) so will be available for use when Spring loads the application context file.
Related
We are using JBOSS 5.1.0.GA and spring integration framework. We are placing the configuration files under the conf directory of the JBOSS to read them from the classpath. But now we are told that we should move all the configuration files from the conf directory to the WEB-INF directory of the war file. Everything was working fine When we placed the files under conf directory.
<bean id="xyz" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:CustomerService/property-files/*.properties</value>
</list>
</property>
</bean>
But when we move the configuration files from conf directory to WEB-INF directory by making the following changes we are getting the Exceptionjava.io.FileNotFoundException.
<bean id="xyz" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>/WEB-INF/CustomerService/property-files/*.properties</value>
</list>
</property>
</bean>
The exception details:
java.io.FileNotFoundException: URL [jndi:/localhost/pqawdTestWebApp/WEB-INF/CustomerService/spring-integration/Jobs/] cannot be resolved to absolute file path because it does not reside in the file system: jndi:/localhost/pqawdTestWebApp/WEB-INF/CustomerService/spring-integration/Jobs/
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:205)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
at org.springframework.core.io.UrlResource.getFile(UrlResource.java:169)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.doFindPathMatchingFileResources(PathMatchingResourcePatternResolver.java:526)
Anybody has idea on what to do?
Place them in the class path (by some build means).
/WEB-INF/classes/CustomerService/property-files/*.properties
WEB-INF directory path will not be available as classpath in the standalone Spring project. So, I have moved the configuration files to src/resources folder to import them without any hassle.
My JBoss version is Jboss-as-7.1.1.Final.
I have some system properties in standalone.xml:
<system-properties>
<property name="ims.eas.service.registerSystem" value="registerSystem"/>
<property name="ims.developerMode" value="false"/>
...
</system-properties>
In web.xml refer to them as ${ims.developerMode}. But it does not work.
Watched System.getProperties() in debug mode, system properties are there. It reads them from standalone.xml but not replaces in web.xml. With jboss-eap-6.1 it works pretty well.
I'm doing some EJB with JPA project that maps/persists some entities to mysql database.
I have defined persistence unit in persistence.xml like this:
<persistence-unit name="MyAppPU" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>MyAppDS</jta-data-source>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
<property name="openjpa.jdbc.DBDictionary" value="mysql" />
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
</properties>
</persistence-unit>
Then, in tomee/conf/tomee.xml file i have defined data source like this:
<Resource id="MyAppDS" type="DataSource">
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:mysql://127.0.0.1:3306/MyAppDB
UserName root
Password 123
JtaManaged true
DefaultAutoCommit false
</Resource>
All this works fine, i create MyApp.jar, deploy it to TomEE server, test it and i get mysql tables in database.
My question is "Is there any other place where I could define data source resource?"
Or it has to be in tomee/conf/tomee.xml file?
Can it be defined somewhere inside application structure, in some xml file, and deployed inside apps jar file to server?
That's the whole point of a JNDI data source, to externalize it outside of your application, so you can modify it without recompiling or repackaging. So it is better to leave it this way.
For testing purpose, some EE server such as JBoss (Wildfly) let you define this in your project.
It might be a bit late to answer this, You can in tomee place the resource definition in WEB-INF/resources.xml.
You only have to set the tomee.xml. If you are using Eclipse, you must copy the tomee.xml into the servers configuration/Tomee to get recognize it into the web project, otherwise you will get troubles.
I am attempting to complete this Java Brains tutorial on hibernate, using hibernate3.6.10.Final and Eclipse IDE, and I am encountering an exception that is not covered in the video. Here is the output:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.hibernate.cfg.Configuration.reset(Configuration.java:332)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:298)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:302)
at com.helo478.firsthibernateproject.SimpleTest.setUpHibernate(SimpleTest.java:31)
at com.helo478.firsthibernateproject.SimpleTest.main(SimpleTest.java:19)
Caused by: java.lang.NullPointerException
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:167)
at org.hibernate.cfg.Environment.<clinit>(Environment.java:618)
... 5 more
[EDIT] I thought the first 3 lines of code were irrelevent, because I've seen a working version of this program (in the tutorial) that had that output. However, it seems that the issue was, indeed with the slf4j jars. Thank you #Jayaram Pradhan and # drurenia[/EDIT]
I did some searching on StackOverflow already and found this similar case (actually they were probably doing the same tutorial). Unfortunately, that thread does not, at this time, have a usable solution. The questioner solved their problem by starting from scratch and using a different database. I have done that (using both PostgreSQL and MySQL), and I get the same error on each.
The other response indicated that the configuration file should be in the project root directory and that the Configurations.configure() method takes an optional String filepath. I have tried placing the hibernate.cfg.xml file in both the project root directory, as well as the "src" directory. I have also tried pointing to the file in with an argument on the .configure() method. There is no change in output.
Because every line of my Java code is taken directly from the tutorial, I think it is most likely that there is a problem with my hibernate.cfg.xml file. The file is altered from a template that was packaged with hibernate3. I just changed it to reference the MySQL database. Being new to Hibernate, I suppose I must have made a mistake there. Here is the complete text:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
<property name="connection.username">root</property>
<property name="connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Names the annotated entity class -->
<mapping class="com.helo478.firsthibernateproject.User" />
</session-factory>
</hibernate-configuration>
My question is this:
What, if anything, am I doing wrong in my hibernate.cfg.xml file? If nothing is wrong, what else might explain the failure?
I didn't see the tutorial you are reffering but from your stack trace its look like, your slf4j dependencies are not present.
Reffer to the below links which will help you to include the slf4j dependcies:
Hibernate 3.4 with slf4j and log4j
http://www.slf4j.org/manual.html
Make sure you have :
•slf4j-api-1.7.5.jar
•slf4j-simple-1.6.1.jar
in your lib.
This is quite similar question to one older but the solution did not work for me.
I have a WAR package.
In web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
In application-context.xml
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:social.properties</value>
</property>
</bean>
But getting this:
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/social.properties]
I checked the WAR package - .xml and .properties files are both in /WEB-INF/classes
.properties file is in src/main/resources and .xml in src/main/java (in default package both) and maven transports them (I think) correctly in the default package of WEB-INF/classes
Does anyone know why i could get this exception? Thank you.
EDIT: I just want to add that JUnit tests goes correctly (i mean they load what they should from social.properties) but when running the app it ignores my classpath: prefix
Do not use classpath. This may cause problems with different ClassLoaders (container vs. application). WEB-INF is always the better choice.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>
and
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/social.properties</value>
</property>
</bean>
Put the things like /src/main/resources/foo/bar.properties and then reference them as classpath:/foo/bar.properties.
Try to use classpath*: prefix instead.
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources.html#resources-classpath-wildcards
Also please try to deploy exploded war, to ensure that all files are there.
I think currently the application-context.xml file is into src/main/resources AND the social.properties file is into src/main/java... so when you package (mvn package) or when you run tomcat (mvn tomcat:run) your social.properties disappeared (I know you said when you checked into the .war the files are here... but your exception says the opposite).
The solution is simply to put all your configuration files (application-context.xml and social.properties) into src/main/resources to follow the maven standard structure.
Are you having Tomcat unpack the WAR file? It seems that the files cannot be found on the classpath when a WAR file is loaded and it is not being unpacked.
try with this code...
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>/social.properties</value>
</list>
</property>
</bean>
Mark sure propertie file is in "/WEB-INF/classes" try to use
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/classes/social.properties</value>
</property>
</bean>
I had the same error.
My filename was jpaContext.xml and it was placed in src/main/resources. I specified param value="classpath:/jpaContext.xml".
Finally I renamed the file to applicationContext.xml and moved it to the WEB-INF directory and changed param value to /WEB-INF/applicationContext.xml, then it worked!