2

Spring Oxm allows you to use different marshallers/unmarshallers and Castor is one of them.

By default castor marshalles xml documents unindented and official documents tell that putting a castor.properties file in the search locations including the line org.exolab.castor.indent=true will override the default behavior.

Now, when using Spring Oxm in a web application (Spring Batch Admin) how can I override the castor.properties in the castor jar?

I have the following bean configurations (extra lines removed) and they do not have the necessary properties to set for this as far as I can see.

<bean id="myCastorMarshaller" 
    class="org.springframework.oxm.castor.CastorMarshaller">
<property name="mappingLocation" value="classpath:/mapping/my-mapping.xml" />
</bean>

<bean id="myXmlWriter" 
   class="org.springframework.batch.item.xml.StaxEventItemWriter">
<property name="marshaller" ref="myCastorMarshaller" />
</bean>
Serkan Arıkuşu
  • 5,549
  • 5
  • 33
  • 50

2 Answers2

1

Answering my own question: Using Spring 3.1.2 there is no solution for this question.

The class org.springframework.oxm.castor.CastorMarshaller should be in charge for setting properties and it has even jira issue for this SPR-8470 but the patches are not committed to main branch after more than a year.

Anyone may compare CastorMarshaller at Github with the proposed patch.

In brief, we need a setProperties(...) method in the CastorMarshaller but the patch is not committed.

Serkan Arıkuşu
  • 5,549
  • 5
  • 33
  • 50
0

when using Spring Oxm in a web application (Spring Batch Admin) how can I override the castor.properties in the castor jar?

It is generally described in the link search locations you mentioned:

Castor loads the castor.properties in the following order:

  1. From classpath (usually from the jar file)
  2. From {java.home}/lib (if present)
  3. From the local working directory

Each properties file overrides the previous. So you don't have to come up with a properties file with all the properties and values, just the ones you want to change. This also means you don't have to touch the properties file found in the jar file.


Generally there are two kinds of properties file (with fixed file name) scanned and loaded by Castor, you can check out Castor source for more details:

We usually put castor.properties under the classpath root, which is your project's src/ directory (if you use maven, under src/main/java/).

Hope this helps.

yorkw
  • 40,926
  • 10
  • 117
  • 130
  • Hi yorkw, I think this differs in web applications; I have tried many locations including /src/main/resources and /src/main/java but somehow it is not read; and I believe this relates to how Oxm calls castor during marshalling. – Serkan Arıkuşu Jul 17 '12 at 08:00
  • How do you run the application? If you use maven build the war file, make sure the castor.properties file is actually packed inside war file. – yorkw Jul 17 '12 at 08:33
  • It resides in WEB-INF/classes folder in war file. I think this is the default location that maven copies files under /src/main/resources. Do you suggest that I move it to another location in the generated war file? – Serkan Arıkuşu Jul 17 '12 at 09:28
  • Oh I see what you mean "it relates to how OXM calls castor during marshalling" now. Castor provide its own API to integrate with Spring described [here](http://www.castor.org/spring-xml-intro.html) Looks like Sping web app doesn't know how to read the file from its classpath root. Have you tried other option like create `castor.core.properties` under `JAVA_HOME/lib/` or create environment variable `org.castor.user.properties.location` point to castor.properties file? – yorkw Jul 17 '12 at 09:40
  • In addition, `castor.properties` under `WEB-INF/classes` doesn't look quite right (it should directly under `WEB-INF/` in war file). In your project, try moving `castor.properties` into `src/main/webapp/WEB-INF/`. – yorkw Jul 17 '12 at 09:48
  • Hi yorkw, putting it under src/main/webapp/WEB-INF did not work also. I really doubt it searches for this file when using Spring Oxm, because I've tried almost everywhere – Serkan Arıkuşu Jul 19 '12 at 10:41
  • Hmmm, I have done this before with jar file (castor.properties), but never tried in war file. Have you tried other approach like `JAVA_HOME/lib/` or via environment variable? You can enable log for `org.castor.core.*` and check out the log file to see if the code is executed. If you use environment variable `org.castor.user.properties.location`, it should write some info to log like `Loading custom Castor properties from ...`. – yorkw Jul 19 '12 at 10:52
  • I've added a self answer, can you please check it if you have time – Serkan Arıkuşu Jul 19 '12 at 11:15