1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
package com.c2kernel.utils;
//Java
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.Properties;
import java.util.StringTokenizer;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
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;
import com.c2kernel.process.resource.ResourceLoader;
/**************************************************************************
* Loads all mapfiles, and wraps marshalling/unmarshalling
*
* @author $Author: abranson $ $Date: 2004/10/20 14:10:21 $
* @version $Revision: 1.12 $
**************************************************************************/
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 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,String.format( "CastorXMLUtility.<init> Loading maps from [%s]",wMapsURL));
String index;
try {
index = FileStringUtility.url2String( new URL(wMapsURL, "index") );
} catch (Exception e) {
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.<init>: iuse KernelClassLoader=[%s]",
wKernelClassLoader));
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);
HashSet<URL> loadedMapURLs = new HashSet<URL>();
try {
int wMapIdx=0;
while( sTokenizer.hasMoreTokens() ) {
String wCurrentMap = sTokenizer.nextToken();
URL wCurrentMapURL = new URL(wMapsURL, wCurrentMap);
wMapIdx++;
if( !loadedMapURLs.contains(wCurrentMapURL) ) {
Logger.msg(
3,
String.format(
"CastorXMLUtility.<init>: Adding mapping file (%d/%d):[%s]",
wMapIdx, wNbMap, wCurrentMapURL));
wCastorMapping.loadMapping( wCurrentMapURL );
loadedMapURLs.add( wCurrentMapURL );
}
else {
Logger.msg(3,"Map file already loaded:"+wCurrentMapURL);
}
}
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.<init>: 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(), "");
} 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(), "");
}
Logger.msg(1, String.format("Loaded [%d] maps from [%s]",loadedMapURLs.size(),wMapsURL));
}
/**************************************************************************
* Marshalls a mapped object to string. The mapping must be loaded before.
* See updateMapping().
**************************************************************************/
public String marshall( Object obj )
throws IOException,
MappingException,
MarshalException,
ValidationException
{
if (obj == null) return "<NULL/>";
if (obj instanceof Outcome)
return ((Outcome)obj).getData();
StringWriter sWriter = new StringWriter();
Marshaller marshaller = pCastorContext.createMarshaller();
marshaller.setWriter(sWriter);
marshaller.setMarshalAsDocument( false );
marshaller.marshal( obj );
return sWriter.toString();
}
/**************************************************************************
* Unmarshalls a mapped object from string. The mapping must be loaded before.
* See updateMapping().
**************************************************************************/
public Object unmarshall( String data )
throws IOException,
MappingException,
MarshalException,
ValidationException
{
if (data.equals("<NULL/>")) return null;
StringReader sReader = new StringReader( data );
Unmarshaller unmarshaller = pCastorContext.createUnmarshaller();
return unmarshaller.unmarshal( sReader );
}
}
|