How to use local xsd for EhCache with Spring - spring

We are facing one issue about EhCache and Spring, when we point XSD file in ehcache.xml to http://ehcache.org/ehcache.xsd, everything is ok, our application can startup correct.
But our server can't access external website, so we changed XSD location to local as below, but the application can't startup with following exception (already copy ehcache.xsd to classes folder, same as ehcache.xml).
I have googled many solution, but it still can't be fixed. I need your help about this. Thank you very much.
Spring: 3.1.0
EhCache: 2.4.2
Server: Tomcat 6.0
Java : 1.6
Config:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="false" monitoring="autodetect" dynamicConfig="true">
Exception:
2012-11-07 16:54:42,003 WARN [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - <Ignored XML validation warning>
org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'ehcache.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.warning(ErrorHandlerWrapper.java:96)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:380)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.findSchemaGrammar(XMLSchemaValidator.java:2440)
... more
2012-11-07 16:54:42,007 ERROR [org.springframework.web.context.ContextLoader] - <Context initialization failed>
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 3 in XML document from ServletContext resource [/WEB-INF/classes/META-INF/ehcache.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'ehcache'.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
... more
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'ehcache'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
... more

Download ehcache.xsd, put it to the same folder where is your config xml file.
modify the xml head like below, notice the classpath prefix:
<ehcache
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="classpath:ehcache.xsd">
This will work in the development and production environment.

usually I set xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd", But this can't work in a offline environment.

My Workaround is add this xsd file to absolute path, such as: c:/ehcache.xsd and point to this location in ehcache.xml.
Hope this will help someone who facing the same issue.
Still looking forward to find out other solution.
Thanks.

In my case just downloading the file from http://ehcache.org/ehcache.xsd into src/main/resources solves the problem
cd src/main/resources
wget http://ehcache.org/ehcache.xsd

Related

Cannot find the declaration of element 'configuration' while importing log4j2.xml file in spring

I am getting the exception as -
INFO: Initializing Spring FrameworkServlet 'dispatcher' WARN Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 3 in XML document from class path resource [log4j2.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 31; Element type "Configuration" must be declared.
ERROR Context initialization failed
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 3 in XML document from class path resource [log4j2.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 31; Element type "Configuration" must be declared.
Below is the Spring annotation configuration -
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = "com.test.*")
#PropertySource(value = {"classpath:application.properties"})
#ImportResource("classpath:log4j2.xml")
public class AppConfig extends WebMvcConfigurerAdapter {
...
}
Below is the log4j2 xml configuration file -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configuration>
<Configuration status="debug">
<Properties>
<Property name="log-path">C:/logs/</Property>
</Properties>
<Appenders>
...
</Appenders>
<Loggers>
...
</Loggers>
</Configuration>
How to get rid of this error ? Is there any mistake while importing the file into the application ? Want to enable log4j2 configuration into the project. Will the xml file be automatically understood by spring or need to write some class for it ? The maven pom file does not include sax dependency. Is that a problem ?
I think that "#ImportResource("classpath:log4j2.xml")" is the problem. Please remove this line and try again.
With #ImportResource you could add some Spring-XML-Configurations. This is not for Log4J2-Configuration.
The Log4J2-Framework will search for this resources (log4j2.xml) in the classpath. Please look at: http://logging.apache.org/log4j/2.x/manual/configuration.html
You have to enable the logging-framework with Spring. Please see http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html
Thanks, everyone for you answers. Was problem in the configuration file. After an hour of brainstorming I figured it out. Just changed the configuration file code and it is working fine.

Karaf Feature install throwing Unsupported 'Bundle-ManifestVersion' value: 1

Hi guys i am getting the below error while using feture install in karaf
org.apache.karaf.features.internal.util.MultiException: Error
at org.apache.karaf.features.internal.download.impl.MavenDownloadManager$MavenDownloader.<init>(MavenDownloadManager.java:84)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.download.impl.MavenDownloadManager.createDownloader(MavenDownloadManager.java:72)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.region.Subsystem.downloadBundles(Subsystem.java:358)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.region.Subsystem.downloadBundles(Subsystem.java:355)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.region.SubsystemResolver.resolve(SubsystemResolver.java:191)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.service.Deployer.deploy(Deployer.java:263)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.service.FeaturesServiceImpl.doProvision(FeaturesServiceImpl.java:1079)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.service.FeaturesServiceImpl$1.call(FeaturesServiceImpl.java:975)[9:org.apache.karaf.features.core:4.0.3]
at java.util.concurrent.FutureTask.run(FutureTask.java:262)[:1.7.0_79]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)[:1.7.0_79]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)[:1.7.0_79]
at java.lang.Thread.run(Thread.java:745)[:1.7.0_79]
Caused by: java.lang.Exception: Unable to create resource for bundle mvn:org.dt.dummy/Diff-Blueprint/0.0.1-SNAPSHOT
at org.apache.karaf.features.internal.region.Subsystem.createResource(Subsystem.java:567)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.region.Subsystem$1.downloaded(Subsystem.java:376)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.download.impl.MavenDownloadManager$MavenDownloader$1.operationComplete(MavenDownloadManager.java:133)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.download.impl.MavenDownloadManager$MavenDownloader$1.operationComplete(MavenDownloadManager.java:127)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.download.impl.DefaultFuture.notifyListener(DefaultFuture.java:344)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.download.impl.DefaultFuture.notifyListeners(DefaultFuture.java:329)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.download.impl.DefaultFuture.setValue(DefaultFuture.java:255)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.download.impl.AbstractDownloadTask.setFile(AbstractDownloadTask.java:61)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.download.impl.AbstractRetryableDownloadTask.run(AbstractRetryableDownloadTask.java:59)[9:org.apache.karaf.features.core:4.0.3]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)[:1.7.0_79]
at java.util.concurrent.FutureTask.run(FutureTask.java:262)[:1.7.0_79]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)[:1.7.0_79]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)[:1.7.0_79]
... 3 more
Caused by: org.osgi.framework.BundleException: Unable to build resource for mvn:org.dt.dummy/Diff-Blueprint/0.0.1-SNAPSHOT: Unsupported 'Bundle-ManifestVersion' value: 1
at org.apache.karaf.features.internal.resolver.ResourceBuilder.build(ResourceBuilder.java:80)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.resolver.ResourceBuilder.build(ResourceBuilder.java:69)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.region.Subsystem.createResource(Subsystem.java:565)[9:org.apache.karaf.features.core:4.0.3]
... 15 more
Caused by: org.osgi.framework.BundleException: Unsupported 'Bundle-ManifestVersion' value: 1
at org.apache.karaf.features.internal.resolver.ResourceBuilder.doBuild(ResourceBuilder.java:88)[9:org.apache.karaf.features.core:4.0.3]
at org.apache.karaf.features.internal.resolver.ResourceBuilder.build(ResourceBuilder.java:78)[9:org.apache.karaf.features.core:4.0.3]
... 17 more
feature.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<features xmlns="http://karaf.apache.org/xmlns/features/v1.3.0" name="Diff-Blueprint">
<feature name="Diff-Blueprint" description="Diff-Blueprint" version="0.0.1.SNAPSHOT">
<feature prerequisite="true" dependency="false">wrap</feature>
<bundle>mvn:org.dt.dummy/Diff-Blueprint/0.0.1-SNAPSHOT</bundle>
<bundle>wrap:mvn:osgi/osgi/3.0.1</bundle>
<bundle>wrap:mvn:javax.xml/xml/3.1.0</bundle>
<bundle>wrap:mvn:xmlunit/xmlunit/1.6</bundle>
<bundle>wrap:mvn:junit/junit/4.11</bundle>
<bundle>wrap:mvn:org.hamcrest/hamcrest-core/1.3</bundle>
<bundle>mvn:com.googlecode.json-simple/json-simple/1.1.1</bundle>
<bundle>wrap:mvn:de.odysseus.staxon/staxon-jackson/1.2</bundle>
<bundle>mvn:com.fasterxml.jackson.core/jackson-core/2.0.6</bundle>
<bundle>wrap:mvn:de.odysseus.staxon/staxon/1.2</bundle>
<bundle>mvn:log4j/log4j/1.2.17</bundle>
<bundle>mvn:org.codehaus.jackson/jackson-mapper-asl/1.9.13</bundle>
<bundle>mvn:org.codehaus.jackson/jackson-core-asl/1.9.13</bundle>
<bundle>wrap:mvn:org.unitils/unitils/2.4</bundle>
<bundle>wrap:mvn:commons-logging/commons-logging/1.1</bundle>
<bundle>wrap:mvn:commons-dbcp/commons-dbcp/1.2.2</bundle>
<bundle>wrap:mvn:commons-pool/commons-pool/1.3</bundle>
<bundle>wrap:mvn:commons-lang/commons-lang/2.3</bundle>
<bundle>wrap:mvn:commons-collections/commons-collections/3.2</bundle>
<bundle>wrap:mvn:ognl/ognl/2.6.9</bundle>
<bundle>wrap:mvn:ant/ant/1.6.5</bundle>
</feature>
</features>
Appreciate that I am resurrecting an old thread here, but to elaborate on #Charity's answer, for anybody who discovers this at a later date (as I did)...
The problem is that we are trying to deploy a non-OSGi compliant bundle, to the container. The solution is to "wrap" the bundle that is causing you problems, by using the Wrap protocol described in the OPS4J Wiki.
JAR Example
A simple example using Maven is shown below:
wrap:mvn:commons-logging/commons-logging/1.1
Note that we have to prefix the bundle with "wrap:", and that is all. See the wiki for more complex examples which override settings defined in the manifest.
WAR Example
Deploy a non-OSGi compliant WAR file in much the same way, using the "webbundle" URI handler instead:
webbundle:mvn:com.example/example-web-app/1.0/war
I have found a solution if we wrap the jars which throw Bundle-ManifestVersion: 1 then this will work
Evidently the Diff-Blueprint bundle has Bundle-ManifestVersion: 1 as a MANIFEST.MF header. This indicates that it predates OSGi Release 4.0, which was released in 2005, so it's unsurprising that a modern version of Karaf does not support it.
So you will have to look into the tool that produced that bundle.
Try to edit MANIFEST.MF file of problematic bundle, just change Bundle-ManifestVersion with value of 2 instead of 1.

Getting error in trying to read applicationContext.xml in Standalone java

ApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
InvGroupDelegate igcd = (InvGroupDelegate) context.getBean("invGroupDelegate");
Giving error:
Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
The appContext file is present in myProject/properties
And this is also added to 'Src folders in the build path'.
Resolved it. Was an eclipse issue. Closed the project and reopned it. Then cleaned up everything and built once more. Then things were fine.

Spring JDO configuration parsing error

I have a spring test case that annotated as following
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath:conf/allocadia-base.xml", "classpath:META-INF/jdoconfig.xml"})
my jdoconfig is
<?xml version="1.0" encoding="utf-8"?>
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">
when I run the test I get
java.lang.IllegalStateException: Failed to load ApplicationContext
.
.
.
Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 4 in XML document from class path resource [META-INF/jdoconfig.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'jdoconfig'.
if I changed jdoconfig to
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig http://java.sun.com/xml/ns/jdo/jdoconfig_2_3.xsd">
the error is
Caused by:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: Unable to locate Spring NamespaceHandler for
XML schema namespace [http://java.sun.com/xml/ns/jdo/jdoconfig]
Offending resource: class path resource [META-INF/jdoconfig.xml]
I use the latest spring 3.2 and JDO 2.3-e
the application works fine when I deploy it to tomcat I'm just getting this error when trying to run the test
Your jdoconfig.xml consist incorrect url. Try this:
xsi:schemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig http://java.sun.com/xml/ns/jdo/jdoconfig_3_0.xsd"
Validating jdoconfig with incorrect url
How to solve validation error on xsi:noNamespaceSchemaLocation in jdoconfig.xml

Spring inside war cannot find classpath resource in an inner jar file

I have a project organized like this:
core
-- /src/main/resources/company/config/spring-config.xml
webapp
-- /WEB-INF/applicationContext.xml
The webapp depends on core.jar, which is included correctly in WEB-INF/lib when I deploy.
In web.xml I have:
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
And in applicationContext.xml I have:
<import resource="classpath:/company/config/spring-config.xml" />
But when I run, I get this error:
2012-10-04 20:03:39,156 [localhost-startStop-1] springframework.web.context.ContextLoader ERROR: Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:/company/config/spring-config.xml]
Offending resource: ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [company/config/spring-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [company/config/spring-config.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
....
Caused by: java.io.FileNotFoundException: class path resource [company/config/spring-config.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:142)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
... 36 more
When spring-config.xml is in webapp, everything works fine.
I noticed the leading / is removed from some of the errors on the stack trace, and I wonder if this has anything to do with it.
Also, I am (unfortunately) using Spring 2.5, if that matters.
For future reference, I figured out the problem after much debugging. It turns out Eclipse was building my "core" library as a jar, but with a web application package layout, so instead of my resource being located here:
/company/config/spring-config.xml
it was located here:
/WEB-INF/company/config/spring-config.xml
which caused the problem. I had checked the jar a few times before, but had never noticed the sneaky "/WEB-INF" hiding in plain sight.
Removing the project and re-importing it into Eclipse (via the Maven pom.xml file) was not enough to fix the problem.
I had to manually delete the Eclipse-specific .project, .classpath, and .settings files. When I did that and re-imported the project everything was fixed.
The moral of the lesson is: ALWAYS check your resource paths when the exception says "File Not Found".

Resources