Maven Spring fileUpload using uploadTempDir property - spring

Actually my requirement is to store uploaded files in the classpath directory i.e. src/main/resources/uploads. (I am using maven project)
But the dispatcher is not able to find this path. I am getting the following error.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multipartResolver' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'uploadTempDir' threw exception; nested exception is java.io.FileNotFoundException: class path resource [uploads] cannot be resolved to URL because it does not exist
The below configuration is added in the dispatcher file:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1048576"/>
<property name="uploadTempDir" ref="uploadDirResource"/>
</bean>
<bean id="uploadDirResource" class="org.springframework.core.io.ClassPathResource">
<constructor-arg>
<value>/uploads</value>
</constructor-arg>
</bean>

The path src/main/resources(/uploads) is a path used by Maven BEFORE compiling. Your Server does not know anything about this path!
The artefacts in src/main/resources(/uploads) are copied within the war file into the directory WEB-INF/classes/uploads.
But this directory also contain the java class fiels, and you should never allow a user to modify or add files to that directory!
One point that is may not correct with you configration is the whitespace in
<value>/uploads </value>

Related

Grails database configuration

I'm trying to make some changes to the database configuration for a Grails application. Grails version is 2.5.3.
The goal is to remove hard coded dependencies to MySql to be able to use the application with other database providers.
I am also trying to run locally with environment set to prod and with a local MySql database. (To be able to test my changes without deploying, since the "dev" environment uses the H2 database which is set up quite different.) So I'm starting with mvn grails:run-app -Dgrails.env=prod.
I'm not very experienced with Grails and a lot of things seems to happen "magically" which makes troubleshooting a little difficult.
Some configuration files that seems to be involved are:
context.xml. As I understand it this is a tomcat configuration file on the server (outside the application). There is also a myapp/resources/tomcat-conf/context.xml which I believe is used when I'm running locally. context.xml contains a database configuration like this:
<Context>
...
<Resource name="jdbc/TP" auth="Container" type="javax.sql.DataSource"
maxActive="10" maxIdle="5" maxWait="10000" username="xxx" password="xxx"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://xxx:3306/xxx?autoReconnect=true" validationQuery="select 1"
/>
...
</Context>
Then there is a myapp/src/main/resources/myapp-PROD.xml
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
<property name="generateDdl" value="false" />
<property name="database" value="MYSQL" />
</bean>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/TP"/>
<property name="resourceRef" value="true" />
</bean>
I would like to get rid of the hardcoded "MYSQL" here and preferable be able to get that from context.xml (or a properties file on the server).
myapp-PROD.xml also imports myapp-config.xml which contains:
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
Then there is some configuration in myapp/grails-app/conf/DataSource.groovy:
(Including some outcommented configuration)
environments {
...
production {
dataSource {
// driverClassName = "com.mysql.jdbc.Driver"
// username = "xxx"
// password = "xxx"
// url = "jdbc:mysql://xxx:3306/xxx"
jndiName = "java:comp/env/jdbc/TP"
}
}
}
And there is also myapp/grails-app/conf/BootStrap.groovy which does some H2 configuration for "dev" and "test" but no database configuration for "prod".
So I have tried to specify my local MySql database in context.xml. It does not work well. In myapp/target/tomcat/logs/myapp.log I can see this:
*2022-05-23 13:40:01,669 [localhost-startStop-1] DEBUG spring.OptimizedAutowireCapableBeanFactory - Invoking afterPropertiesSet() on bean with name 'dialectDetector'
2022-05-23 13:40:05,703 [localhost-startStop-1] WARN spring.GrailsWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'hibernateProperties' while setting bean property 'hibernateProperties'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateProperties': Cannot resolve reference to bean 'dialectDetector' while setting bean property 'properties' with key [hibernate.dialect]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dialectDetector': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (**Could not create connection to database server. Attempted reconnect 3 times. Giving up.**)*
I don't see any connection attempts in my database logs, so obviously it is not trying to connect to the database i specified in context.xml.
I also tried to specify the database in DataSource.groovy, but that didn't work either.
So:
What do I need to do to run locally with my local MySql database?
Is there any additional helpful logs I can enable?
How do I get rid of "MYSQL" in myapp-PROD.xml and get dialect from context.xml instead?
What do I need to do to run locally with my local MySql database?
You need to supply a value for the dataSource driver that points to the Mysql driver and you need to supply a JDBC url that the MySql driver recognizes and points to your local MySql.
How do I get rid of "MYSQL" in myapp-PROD.xml and get dialect from
context.xml instead?
The dialect is only 1 piece of the puzzle and probably really isn't the key to the problem. If it really is a requirement to get the dialect from context.xml I don't know how to do it, but a much more common way to deal with the situation is to either use JNDI so you don't have to make any mention of dialects and driver names and user names and passwords in your app. Another option is to put the database config in an external config file that is loaded by the app at startup time.

JNDI lookup failure

i would be grateful if someone can help me rectify the issue in my code. Not sure where I'm going wrong.
Currently my persistence.xml contains
<property name="hibernate.transaction.manager_lookup_class" value="#####.hibernate.JbossTSTransactionManagerLookup"/>
<property name="hibernate.current_session_context_class" value="jta"/>
along with
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="####"/>
<property name="jpaDialect" ref="jpaDialect"/>
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="java:comp/env/TransactionManager"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
Context initialization failed : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [###XMLConfig.xml]: Invocation of init method failed; nested exception is org.springframework.transaction.TransactionSystemException: JTA TransactionManager is not available at JNDI location [java:comp/env/TransactionManager]; nested exception is org.springframework.jndi.TypeMismatchNamingException: Object of type [class com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple] available at JNDI location [java:comp/env/TransactionManager] is not assignable to [javax.transaction.TransactionManager]
Caused by: org.springframework.transaction.TransactionSystemException: JTA TransactionManager is not available at JNDI location [java:comp/env/TransactionManager]; nested exception is org.springframework.jndi.TypeMismatchNamingException: Object of type [class com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple] available at JNDI location [java:comp/env/TransactionManager] is not assignable to [javax.transaction.TransactionManager]
at org.springframework.transaction.jta.JtaTransactionManager.lookupTransactionManager(JtaTransactionManager.java:598)
Caused by: org.springframework.jndi.TypeMismatchNamingException: Object of type [class com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple] available at JNDI location [java:comp/env/TransactionManager] is not assignable to [javax.transaction.TransactionManager] at org.springframework.jndi.JndiTemplate.lookup()
at org.springframework.transaction.jta.JtaTransactionManager.lookupTransactionManager()
Looks like there is some issue with the classpath of your project. Check if there are two versions of the same dependency in your project. In my case I had multiple versions of the jboss-transaction jar in the classpath, keeping just one version of it fixed the issue for me. Check if there is any transitive dependency which is causing this issue which you can exclude in your maven pom.

Trying to deploy a Spring Service on WSO2 Application Service

I'm trying to deploy a Spring Service in WSO2 Application Service. I did THIS tutorial and the app works fine on Eclipse, but when I try to deploy it on WSO2 I get this error:
Cannot load Spring beans. Please check the Spring context
configuration file and verify that the defined Spring beans exist in
the .jar file.
I unziped de .jar file and JdbcCustomerDAO class is there with all the others.
Spring context:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="customerDAO" class="com.mkyong.customer.dao.impl.JdbcCustomerDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="url" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
</beans>
I also tried to include spring and mysql-connector-java jars on /repository/components/extensions as says HERE.
EDIT:
ERROR {org.wso2.carbon.springservices.ui.SpringServiceMaker} - Cannot
load Spring beans. Please check the Spring context configuration file
and verify that the defined Spring beans exist in the .jar file.
{org.wso2.carbon.springservices.ui.SpringServiceMaker}
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot
find class
[org.springframework.jdbc.datasource.DriverManagerDataSource] for bean
with name 'dataSource' defined in resource loaded through InputStream;
nested exception is java.lang.ClassNotFoundException:
org.springframework.jdbc.datasource.DriverManagerDataSource
Looks like it couldn't find spring-jdbc, so I added the jar to extensions but now I get this error:
ERROR {org.wso2.carbon.springservices.ui.SpringServiceMaker} - Cannot load
Spring beans. Please check the Spring context configuration file and
verify that the defined Spring beans exist in the .jar file.
{org.wso2.carbon.springservices.ui.SpringServiceMaker}
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'dataSource' defined in resource loaded
through InputStream: Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Could not
instantiate bean class
[org.springframework.jdbc.datasource.DriverManagerDataSource]:
Constructor threw exception; nested exception is
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
I understand that commons.logging is also missing, but when I try to add it to extensions, WSO2 doesn't start. I get a lot of
Log4j:ERROR Could not instantiate...

Custom Document Filter - ClassNotFoundException when starting Connector

I am developing a custom document filter. So far I have created a project in eclipse, added jar files necessary and successfully built a jar file with my own document filter.
What step am I missing to make the connector find the class ??
When it comes to configuring the document filter in the file 'connectorInstance.xml' something goes wrong and its seems the jar file cannot be found in the class path...
The Java package has the following classpath :
com.google.enterprise.connector.util.filter.DocFilterWildCardSearch
or also tried the following path
com.kapsch.gsa.filter.DocFilterWildCardSearch
I copied the 'DocFilterWildCardSearch.jar' file into the following path:
C:\Program Files\GoogleConnectors\GSAConnectors1\Tomcat\webapps\connector-manager\WEB-INF\lib
Restarted the connector and got the following error message:
Nov 20, 2013 4:50:29 PM [Init] com.google.enterprise.connector.servlet.StartUp doStartup
SEVERE: Connector Manager Startup failed:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'DocumentFilters' defined in ServletContext resource [/WEB-INF/documentFilters.xml]: Cannot create inner bean 'asfsdf' of type [com.kapsch.gsa.filter.DocFilterWildCardSearch] while setting constructor argument with key [0]; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.kapsch.gsa.filter.DocFilterWildCardSearch] for bean with name 'asfsdf' defined in ServletContext resource [/WEB-INF/documentFilters.xml]; nested exception is java.lang.ClassNotFoundException: com.kapsch.gsa.filter.DocFilterWildCardSearch
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:230)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:117)
Config file for Document Filter : connectorInstance.xml
<?xml version="1.0"?>
<beans>
<bean class="com.example.connector.HelloWorldConnector" id="helloworld-connector"> </bean>
<bean class="com.google.enterprise.connector.util.filter.DocumentFilterChain" id="DocumentFilters">
<constructor-arg>
<list>
<bean class="com.kapsch.gsa.filter.DocFilterWildCardSearch" id="asfsdf">
<property value="Author" name="propertyName"/>
<property value="Roli" name="propertyValue"/>
<property value="false" name="overwrite"/>
</bean>
</list>
</constructor-arg>
</bean>
</beans>
Your settings are supposed to go inthe the documentFilters.xml file and not the connector instance file.
Google's got a support article on the subject here here.
It looks like you're attempting to nGram. I've got a filter already that I've been meaning to open source. If you would like it, drop me a note.
Finally, 7.2 is rumored to have wild carding in. We won't know for sure until it comes out.
Any chance you can include the output of jar tf on your doc filter jar file?
Michael is right in that you configure doc filters in documentfilters.xml.

replaced-method question in spring 3

Excuse me. I'm a beginner of spring. Now, there's some question in it.
I'm reading Spring in action and try the code in MyEclipse. But when I try the code of replaced-method, there's a error.
<bean id="harry" class="com.springinaction.sprintidol.Magician">
<property name="magicBox" ref="magicBox" />
<property name="magicWords" value="Bippity boppity boo" />
</bean>
<bean id="magicBox" class="com.springinaction.sprintidol.MagicBoxImpl">
<replaced-method name="getContents" replacer="tigerReplacer" />
</bean>
<bean id="tigerReplacer" class="com.springinaction.sprintidol.TigerReplacer" />
This is the applicationContext.xml. And the exception as followed:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stevie' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: net/sf/cglib/proxy/CallbackFilter
All of the code come from the book. Why do I get the exception? And how to solve?
Thanks.
I found the reason. Because I didn't include the Spring 3.0 Persistence Core Libraries.Thanks all the same.

Resources