From d0e42f669232987359ae3431b310530dc588fc6f Mon Sep 17 00:00:00 2001 From: ogattaz Date: Thu, 28 Aug 2014 11:05:51 +0200 Subject: pull master devs --- .../java/com/c2kernel/utils/CastorXMLUtility.java | 74 +++++++++------------- .../java/com/c2kernel/utils/LocalObjectLoader.java | 27 ++------ 2 files changed, 38 insertions(+), 63 deletions(-) (limited to 'src/main/java/com/c2kernel/utils') diff --git a/src/main/java/com/c2kernel/utils/CastorXMLUtility.java b/src/main/java/com/c2kernel/utils/CastorXMLUtility.java index c7d37dd..c5c2499 100644 --- a/src/main/java/com/c2kernel/utils/CastorXMLUtility.java +++ b/src/main/java/com/c2kernel/utils/CastorXMLUtility.java @@ -29,11 +29,11 @@ import com.c2kernel.process.resource.ResourceLoader; * @author $Author: abranson $ $Date: 2004/10/20 14:10:21 $ * @version $Revision: 1.12 $ **************************************************************************/ -public class CastorXMLUtility{ +public class CastorXMLUtility +{ public static final String CASTOR_XML_SERIALIZER_FACTORY = "org.exolab.castor.xml.serializer.factory"; - - private XMLContext pCastorContext; + private XMLContext mappingContext; /** * Looks for a file called 'index.xml' at the given URL, and loads every @@ -42,89 +42,77 @@ public class CastorXMLUtility{ * @param aResourceLoader * the resource loader able to return the right class loader * @param aAppProperties - * the application properties containint optionnal castor + * the application properties containing optional castor * configuration - * @param aMapsSubPath - * the map root sub path (in the kernel path) + * @param mapURL + * the root URL for the mapfiles * @throws InvalidDataException */ public CastorXMLUtility(final ResourceLoader aResourceLoader, - final Properties aAppProperties, final String aMapsSubPath) + final Properties aAppProperties, final URL mapURL) 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,String.format( "CastorXMLUtility. Loading maps from [%s]",wMapsURL)); + Logger.msg(3,String.format( "CastorXMLUtility. Loading maps from [%s]",mapURL)); String index; try { - index = FileStringUtility.url2String( new URL(wMapsURL, "index") ); + index = FileStringUtility.url2String( new URL(mapURL, "index") ); } catch (Exception e) { - throw new InvalidDataException(String.format("Could not load map index from [%s]",wMapsURL)); + throw new InvalidDataException(String.format("Could not load map index from [%s]",mapURL)); } // retrieve the class loader of the class "CastorXMLUtility" - ClassLoader wKernelClassLoader = aResourceLoader + ClassLoader defaultClassLoader = aResourceLoader .getClassLoader(CastorXMLUtility.class.getName()); Logger.msg(3, String.format( - "CastorXMLUtility.: iuse KernelClassLoader=[%s]", - wKernelClassLoader)); + "CastorXMLUtility.: defaultClassLoader=[%s]", + defaultClassLoader)); StringTokenizer sTokenizer = new StringTokenizer(index); int wNbMap = sTokenizer.countTokens(); // init the castor mapping using the classloader of the class "CastorXMLUtility" - Mapping wCastorMapping = new Mapping(wKernelClassLoader); + Mapping thisMapping = new Mapping(defaultClassLoader); HashSet loadedMapURLs = new HashSet(); try { int wMapIdx=0; while( sTokenizer.hasMoreTokens() ) { - String wCurrentMap = sTokenizer.nextToken(); - URL wCurrentMapURL = new URL(wMapsURL, wCurrentMap); + String thisMap = sTokenizer.nextToken(); + URL thisMapURL = new URL(mapURL, thisMap); 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 ); + if( !loadedMapURLs.contains(thisMapURL) ) { + Logger.msg( 3, + String.format("CastorXMLUtility.: Adding mapping file (%d/%d):[%s]", wMapIdx, wNbMap, thisMapURL)); + thisMapping.loadMapping( thisMapURL ); + loadedMapURLs.add( thisMapURL ); } else { - Logger.msg(3,"Map file already loaded:"+wCurrentMapURL); + Logger.msg(3,"Map file already loaded:"+thisMapURL); } } - pCastorContext = new XMLContext(); + mappingContext = new XMLContext(); - pCastorContext.setClassLoader(wKernelClassLoader); + mappingContext.setClassLoader(defaultClassLoader); // if the aAppProperties contains castor properties then if (aAppProperties.contains(CASTOR_XML_SERIALIZER_FACTORY)) { - pCastorContext.setProperty(CASTOR_XML_SERIALIZER_FACTORY, + mappingContext.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 + CASTOR_XML_SERIALIZER_FACTORY, mappingContext .getProperty(CASTOR_XML_SERIALIZER_FACTORY))); } - pCastorContext.addMapping(wCastorMapping); + mappingContext.addMapping(thisMapping); } catch (MappingException ex) { Logger.error(ex); throw new InvalidDataException("XML Mapping files are not valid: "+ex.getMessage(), ""); @@ -136,7 +124,7 @@ public class CastorXMLUtility{ throw new InvalidDataException("Could not read XML mapping files: "+ex.getMessage(), ""); } - Logger.msg(1, String.format("Loaded [%d] maps from [%s]",loadedMapURLs.size(),wMapsURL)); + Logger.msg(1, String.format("Loaded [%d] maps from [%s]", loadedMapURLs.size(), mapURL)); } /************************************************************************** @@ -153,7 +141,7 @@ public class CastorXMLUtility{ if (obj instanceof Outcome) return ((Outcome)obj).getData(); StringWriter sWriter = new StringWriter(); - Marshaller marshaller = pCastorContext.createMarshaller(); + Marshaller marshaller = mappingContext.createMarshaller(); marshaller.setWriter(sWriter); marshaller.setMarshalAsDocument( false ); @@ -174,7 +162,7 @@ public class CastorXMLUtility{ { if (data.equals("")) return null; StringReader sReader = new StringReader( data ); - Unmarshaller unmarshaller = pCastorContext.createUnmarshaller(); + Unmarshaller unmarshaller = mappingContext.createUnmarshaller(); return unmarshaller.unmarshal( sReader ); } } diff --git a/src/main/java/com/c2kernel/utils/LocalObjectLoader.java b/src/main/java/com/c2kernel/utils/LocalObjectLoader.java index f0d8928..503e951 100644 --- a/src/main/java/com/c2kernel/utils/LocalObjectLoader.java +++ b/src/main/java/com/c2kernel/utils/LocalObjectLoader.java @@ -1,5 +1,5 @@ package com.c2kernel.utils; - + import java.util.Iterator; import com.c2kernel.common.InvalidDataException; @@ -24,28 +24,15 @@ public class LocalObjectLoader { { DomainPath defRoot = new DomainPath(root); Iterator e = Gateway.getLookup().search(defRoot, name); - ItemProxy defProxy = null; int currentLayer = -1; - while (e.hasNext()) { + if (e.hasNext()) { DomainPath defPath = (DomainPath)e.next(); - ItemProxy thisProxy = Gateway.getProxyManager().getProxy(defPath); - int thisLayer; - try { - String thisLayerProp = thisProxy.getProperty("Layer"); - thisLayer = Integer.parseInt(thisLayerProp); - } catch (Exception ex) { - thisLayer = 0; - } - if (thisLayer > currentLayer) { - currentLayer = thisLayer; - defProxy = thisProxy; - } - else if (thisLayer == currentLayer) { - throw new ObjectNotFoundException("Duplicate definition for "+name+" in "+root+" found in Layer "+thisLayer, ""); - } + if (e.hasNext()) throw new ObjectNotFoundException("Too many matches for "+name+" in "+root, ""); + return Gateway.getProxyManager().getProxy(defPath); } - if (defProxy == null) + else { throw new ObjectNotFoundException("No match for "+name+" in "+root, ""); - return defProxy; + } + } static public String getScript(String scriptName, int scriptVersion) throws ObjectNotFoundException { -- cgit v1.2.3