summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Branson <andrew.branson@cern.ch>2013-07-10 10:50:15 +0200
committerAndrew Branson <andrew.branson@cern.ch>2013-07-10 11:10:39 +0200
commit74d433dc0083324e7e2774148f3d92f7644e1e64 (patch)
treeee66b65fc740d4f12d324426a8ee36ea2a28b1c9
parent434747fbd551d3f540c7e5b3d7b86f4b984a1bf5 (diff)
CastorXMLUtility switched to recommended XMLContext. Should be faster
and may help with IssueID #132
-rw-r--r--src/main/java/com/c2kernel/utils/CastorXMLUtility.java86
-rw-r--r--src/main/java/com/c2kernel/utils/Logger.java36
2 files changed, 59 insertions, 63 deletions
diff --git a/src/main/java/com/c2kernel/utils/CastorXMLUtility.java b/src/main/java/com/c2kernel/utils/CastorXMLUtility.java
index 6a9350f..98ebbda 100644
--- a/src/main/java/com/c2kernel/utils/CastorXMLUtility.java
+++ b/src/main/java/com/c2kernel/utils/CastorXMLUtility.java
@@ -4,6 +4,7 @@ package com.c2kernel.utils;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
+import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.StringTokenizer;
@@ -14,6 +15,7 @@ import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
+import org.exolab.castor.xml.XMLContext;
import com.c2kernel.common.InvalidDataException;
import com.c2kernel.persistency.outcome.Outcome;
@@ -27,8 +29,7 @@ import com.c2kernel.persistency.outcome.Outcome;
**************************************************************************/
public class CastorXMLUtility
{
- private final Mapping mMapping = new Mapping();
- private final HashSet<URL> mMappingKeys = new HashSet<URL>();
+ private XMLContext mappingContext;
/**
* Looks for a file called 'index.xml' at the given URL, and loads every file
@@ -48,53 +49,38 @@ public class CastorXMLUtility
}
StringTokenizer sTokenizer = new StringTokenizer(index);
- while( sTokenizer.hasMoreTokens() ) {
- String thisMap = sTokenizer.nextToken();
- try {
- addMapping( new URL(mapURL, thisMap));
- } catch (Exception e) {
- Logger.error(e);
- throw new InvalidDataException("Error loading map '"+thisMap+"'", "");
- }
- }
-
- // Test the maps now, instead of on first use.
+ Mapping thisMapping = new Mapping();
+ HashSet<URL> loadedMapURLs = new HashSet<URL>();
+ try {
+ 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 );
+ }
+ else {
+ Logger.msg("Map file already loaded:"+thisMapURL);
+ }
+ }
+
+ mappingContext = new XMLContext();
+ mappingContext.addMapping(thisMapping);
+ } catch (MappingException ex) {
+ Logger.error(ex);
+ throw new InvalidDataException("XML Mapping files are not valid: "+ex.getMessage());
+ } catch (MalformedURLException ex) {
+ Logger.error(ex);
+ throw new InvalidDataException("Mapping file location invalid: "+ex.getMessage());
+ } catch (IOException ex) {
+ Logger.error(ex);
+ throw new InvalidDataException("Could not read XML mapping files: "+ex.getMessage());
+ }
- try {
- new Unmarshaller( mMapping );
- } catch (MappingException e) {
- Logger.error(e);
- throw new InvalidDataException("Castor mapfiles are not valid", "");
- }
Logger.msg(1, "Loaded all maps from "+mapURL.toString());
}
-
- /**************************************************************************
- * Updates a mapping referenced by the mapID.
- * The same mapping cannot be loaded many times as it generates an exception.
- * That is the reason for this method as it maintains the HashSet of MappingKeys.
- **************************************************************************/
- private void addMapping( URL mapID )
- throws IOException,
- MappingException,
- MarshalException,
- ValidationException
- {
-
- if( !mMappingKeys.contains(mapID) )
- {
- Logger.msg(7, "Added mapping file:"+mapID);
-
- mMapping.loadMapping( mapID );
- mMappingKeys.add( mapID );
- }
- else
- {
- Logger.msg(1, "Map file already loaded:"+mapID);
- }
- }
-
/**************************************************************************
* Marshalls a mapped object to string. The mapping must be loaded before.
* See updateMapping().
@@ -109,9 +95,9 @@ public class CastorXMLUtility
if (obj instanceof Outcome)
return ((Outcome)obj).getData();
StringWriter sWriter = new StringWriter();
- Marshaller marshaller = new Marshaller( sWriter );
-
- marshaller.setMapping( mMapping );
+ Marshaller marshaller = mappingContext.createMarshaller();
+
+ marshaller.setWriter(sWriter);
marshaller.setMarshalAsDocument( false );
marshaller.marshal( obj );
@@ -128,9 +114,9 @@ public class CastorXMLUtility
MarshalException,
ValidationException
{
- StringReader sReader = new StringReader( data );
- Unmarshaller unmarshaller = new Unmarshaller( mMapping );
if (data.equals("<NULL/>")) return null;
+ StringReader sReader = new StringReader( data );
+ Unmarshaller unmarshaller = mappingContext.createUnmarshaller();
return unmarshaller.unmarshal( sReader );
}
}
diff --git a/src/main/java/com/c2kernel/utils/Logger.java b/src/main/java/com/c2kernel/utils/Logger.java
index da476df..72c6918 100644
--- a/src/main/java/com/c2kernel/utils/Logger.java
+++ b/src/main/java/com/c2kernel/utils/Logger.java
@@ -23,6 +23,7 @@ public class Logger
* add ten to output time before each message
*/
private static int mHighestLogLevel = 0;
+ private static long startTime = System.currentTimeMillis();
private static HashMap<PrintStream, Integer> logStreams = new HashMap<PrintStream, Integer>();
static protected SimpleTCPIPServer mConsole = null;
@@ -31,15 +32,17 @@ public class Logger
synchronized(logStreams) {
for (Iterator<PrintStream> iter = logStreams.keySet().iterator(); iter.hasNext();) {
PrintStream element = iter.next();
- int logLevel = logStreams.get(element).intValue();
- if (logLevel < msgLogLevel || (logLevel > 9 && logLevel - 10 < msgLogLevel))
+ int logLevel = logStreams.get(element);
+
+ if ( (logLevel > 9 && logLevel - 10 < msgLogLevel) ||
+ (msgLogLevel > 9 && logLevel < msgLogLevel - 10) ||
+ (logLevel < 10 && msgLogLevel < 10 && logLevel < msgLogLevel) )
continue;
- if (logLevel > 9)
- {
- Timestamp ts = new Timestamp(System.currentTimeMillis());
- String sTime = ts.toString();
- message = sTime + " - " + message;
+
+ if (logLevel > 9 || msgLogLevel > 9) {
+ message = reportTime() + " - " + message;
}
+
try {
element.println(message);
} catch (Exception ex) {
@@ -48,6 +51,13 @@ public class Logger
}
}
}
+
+ static private String reportTime() {
+ long now = System.currentTimeMillis();
+ Timestamp ts = new Timestamp(now);
+ double since = (now - startTime) / 1000.0;
+ return ts.toString() + " ("+since+"s)";
+ }
static private void printMessage(Throwable ex) {
StringWriter msgString = new StringWriter();
@@ -59,6 +69,7 @@ public class Logger
static public boolean doLog(int logLevel)
{
+ if (logLevel > 9) logLevel -= 10;
return mHighestLogLevel >= logLevel;
}
/**
@@ -130,9 +141,8 @@ public class Logger
}
synchronized(logStreams) {
- logStreams.put(console, new Integer(logLevel));
- if (logLevel > 9) logLevel-=10;
- if (logLevel > mHighestLogLevel) mHighestLogLevel = logLevel;
+ logStreams.put(console, logLevel);
+ if ((logLevel>10?logLevel-10:logLevel) > mHighestLogLevel) mHighestLogLevel = logLevel;
}
}
@@ -148,10 +158,10 @@ public class Logger
// recalculate lowest log level
if (logLevel == mHighestLogLevel || (logLevel > 9 && logLevel-10 == mHighestLogLevel)) {
- mHighestLogLevel = -1;
+ mHighestLogLevel = 0;
for (Integer element : logStreams.values()) {
- int thisLogLevel = element.intValue()>9?element.intValue()-10:element.intValue();
- if (thisLogLevel > mHighestLogLevel || mHighestLogLevel == -1)
+ int thisLogLevel = element>9?element-10:element;
+ if (thisLogLevel > mHighestLogLevel)
mHighestLogLevel = thisLogLevel;
}
}