From f37883c23f9032ace7e0e780fa0e641859863c0c Mon Sep 17 00:00:00 2001 From: ogattaz Date: Thu, 24 Jul 2014 18:30:23 +0200 Subject: CastorXMLUtility enhancements to use specific classloader rather than the main one and to configure castor if needed --- src/main/java/com/c2kernel/process/Gateway.java | 10 +- .../java/com/c2kernel/utils/CastorXMLUtility.java | 112 ++++++++++++++++----- 2 files changed, 89 insertions(+), 33 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/c2kernel/process/Gateway.java b/src/main/java/com/c2kernel/process/Gateway.java index 8a34e72..cd76d0e 100644 --- a/src/main/java/com/c2kernel/process/Gateway.java +++ b/src/main/java/com/c2kernel/process/Gateway.java @@ -106,12 +106,10 @@ public class Gateway // report version info Logger.msg("Kernel version: "+getKernelVersion()); - // load kernel mapfiles - try { - mMarshaller = new CastorXMLUtility(mResource.getKernelResourceURL("mapFiles/")); - } catch (MalformedURLException e1) { - throw new InvalidDataException("Invalid Resource Location", ""); - } + // load kernel mapfiles giving the resourse loader and the properties of + // the application to be able to configure castor + mMarshaller = new CastorXMLUtility(mResource, props, "mapFiles/"); + // init module manager try { diff --git a/src/main/java/com/c2kernel/utils/CastorXMLUtility.java b/src/main/java/com/c2kernel/utils/CastorXMLUtility.java index 4b0a8b0..c7d37dd 100644 --- a/src/main/java/com/c2kernel/utils/CastorXMLUtility.java +++ b/src/main/java/com/c2kernel/utils/CastorXMLUtility.java @@ -7,6 +7,7 @@ import java.io.StringWriter; import java.net.MalformedURLException; import java.net.URL; import java.util.HashSet; +import java.util.Properties; import java.util.StringTokenizer; import org.exolab.castor.mapping.Mapping; @@ -19,6 +20,7 @@ import org.exolab.castor.xml.XMLContext; import com.c2kernel.common.InvalidDataException; import com.c2kernel.persistency.outcome.Outcome; +import com.c2kernel.process.resource.ResourceLoader; /************************************************************************** @@ -27,46 +29,102 @@ import com.c2kernel.persistency.outcome.Outcome; * @author $Author: abranson $ $Date: 2004/10/20 14:10:21 $ * @version $Revision: 1.12 $ **************************************************************************/ -public class CastorXMLUtility -{ - private XMLContext mappingContext; +public class CastorXMLUtility{ + + public static final String CASTOR_XML_SERIALIZER_FACTORY = "org.exolab.castor.xml.serializer.factory"; + + private XMLContext pCastorContext; - /** - * Looks for a file called 'index.xml' at the given URL, and loads every file - * listed in there by relative path - * - * @param mapURL - map root - */ - public CastorXMLUtility(URL mapURL) throws InvalidDataException { + /** + * Looks for a file called 'index.xml' at the given URL, and loads every + * file listed in there by relative path + * + * @param aResourceLoader + * the resource loader able to return the right class loader + * @param aAppProperties + * the application properties containint optionnal castor + * configuration + * @param aMapsSubPath + * the map root sub path (in the kernel path) + * @throws InvalidDataException + */ + public CastorXMLUtility(final ResourceLoader aResourceLoader, + final Properties aAppProperties, final String aMapsSubPath) + throws InvalidDataException { + + // build base url + URL wMapsURL ; + try { + wMapsURL = aResourceLoader.getKernelResourceURL(aMapsSubPath); + } catch (MalformedURLException e) { + throw new InvalidDataException(String.format( + "Invalid Resource Location [%s]. Cause:%s", aMapsSubPath, + e.getLocalizedMessage())); + } + // load index - Logger.msg(3, "Loading maps from "+mapURL); + Logger.msg(3,String.format( "CastorXMLUtility. Loading maps from [%s]",wMapsURL)); String index; try { - index = FileStringUtility.url2String( new URL(mapURL, "index") ); + index = FileStringUtility.url2String( new URL(wMapsURL, "index") ); } catch (Exception e) { - Logger.warning("Could not load map index from "+mapURL.toString()); - return; + throw new InvalidDataException(String.format("Could not load map index from [%s]",wMapsURL)); } + + // retrieve the class loader of the class "CastorXMLUtility" + ClassLoader wKernelClassLoader = aResourceLoader + .getClassLoader(CastorXMLUtility.class.getName()); + Logger.msg(3, String.format( + "CastorXMLUtility.: iuse KernelClassLoader=[%s]", + wKernelClassLoader)); + + StringTokenizer sTokenizer = new StringTokenizer(index); - Mapping thisMapping = new Mapping(); + int wNbMap = sTokenizer.countTokens(); + + // init the castor mapping using the classloader of the class "CastorXMLUtility" + Mapping wCastorMapping = new Mapping(wKernelClassLoader); HashSet loadedMapURLs = new HashSet(); try { + int wMapIdx=0; while( sTokenizer.hasMoreTokens() ) { - String thisMap = sTokenizer.nextToken(); - URL thisMapURL = new URL(mapURL, thisMap); - if( !loadedMapURLs.contains(thisMapURL) ) { - Logger.msg(7, "Adding mapping file:"+thisMapURL); - thisMapping.loadMapping( thisMapURL ); - loadedMapURLs.add( thisMapURL ); + String wCurrentMap = sTokenizer.nextToken(); + URL wCurrentMapURL = new URL(wMapsURL, wCurrentMap); + wMapIdx++; + if( !loadedMapURLs.contains(wCurrentMapURL) ) { + Logger.msg( + 3, + String.format( + "CastorXMLUtility.: Adding mapping file (%d/%d):[%s]", + wMapIdx, wNbMap, wCurrentMapURL)); + wCastorMapping.loadMapping( wCurrentMapURL ); + loadedMapURLs.add( wCurrentMapURL ); } else { - Logger.msg("Map file already loaded:"+thisMapURL); + Logger.msg(3,"Map file already loaded:"+wCurrentMapURL); } } - mappingContext = new XMLContext(); - mappingContext.addMapping(thisMapping); + pCastorContext = new XMLContext(); + + pCastorContext.setClassLoader(wKernelClassLoader); + + // if the aAppProperties contains castor properties then + if (aAppProperties.contains(CASTOR_XML_SERIALIZER_FACTORY)) { + + pCastorContext.setProperty(CASTOR_XML_SERIALIZER_FACTORY, + aAppProperties + .getProperty(CASTOR_XML_SERIALIZER_FACTORY)); + + Logger.msg(3, String.format( + "CastorXMLUtility.: castor prop: %s=[%s]", + CASTOR_XML_SERIALIZER_FACTORY, pCastorContext + .getProperty(CASTOR_XML_SERIALIZER_FACTORY))); + + } + + pCastorContext.addMapping(wCastorMapping); } catch (MappingException ex) { Logger.error(ex); throw new InvalidDataException("XML Mapping files are not valid: "+ex.getMessage(), ""); @@ -78,7 +136,7 @@ public class CastorXMLUtility throw new InvalidDataException("Could not read XML mapping files: "+ex.getMessage(), ""); } - Logger.msg(1, "Loaded all maps from "+mapURL.toString()); + Logger.msg(1, String.format("Loaded [%d] maps from [%s]",loadedMapURLs.size(),wMapsURL)); } /************************************************************************** @@ -95,7 +153,7 @@ public class CastorXMLUtility if (obj instanceof Outcome) return ((Outcome)obj).getData(); StringWriter sWriter = new StringWriter(); - Marshaller marshaller = mappingContext.createMarshaller(); + Marshaller marshaller = pCastorContext.createMarshaller(); marshaller.setWriter(sWriter); marshaller.setMarshalAsDocument( false ); @@ -116,7 +174,7 @@ public class CastorXMLUtility { if (data.equals("")) return null; StringReader sReader = new StringReader( data ); - Unmarshaller unmarshaller = mappingContext.createUnmarshaller(); + Unmarshaller unmarshaller = pCastorContext.createUnmarshaller(); return unmarshaller.unmarshal( sReader ); } } -- cgit v1.2.3