private static final Log LOG = LogFactory.getLog(MergeJPAPersistenceResource.class);
private ErrorHandler handler = new SimpleSaxErrorHandler(LOG);
public Resource getMergedConfigResource(ResourceInputStream[] sources) throws BeansException {
Resource configResource = null;
ResourceInputStream merged = null;
try {
List<String> mappingFiles = new ArrayList<String>(20);
ResourceInputStream[] inMemoryStreams = new ResourceInputStream[sources.length];
for (int j=0;j<sources.length;j++){
byte[] sourceArray = buildArrayFromStream(sources[j]);
compileMappingFiles(mappingFiles, sourceArray);
inMemoryStreams[j] = new ResourceInputStream(new ByteArrayInputStream(sourceArray), sources[j].getName());
}
merged = merge(inMemoryStreams);
//read the final stream into a byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
boolean eof = false;
while (!eof) {
int temp = merged.read();
if (temp == -1) {
eof = true;
} else {
baos.write(temp);
}
}
configResource = new ByteArrayResource(baos.toByteArray());
if (LOG.isDebugEnabled()) {
LOG.debug("Merged config: \n" + serialize(configResource));
}
} catch (MergeException e) {
throw new FatalBeanException("Unable to merge source and patch locations", e);
} catch (MergeManagerSetupException e) {
throw new FatalBeanException("Unable to merge source and patch locations", e);
} catch (IOException e) {
throw new FatalBeanException("Unable to merge source and patch locations", e);
} catch (SAXException e) {
throw new FatalBeanException("Unable to merge source and patch locations", e);
} catch (ParserConfigurationException e) {
throw new FatalBeanException("Unable to merge source and patch locations", e);
} finally {
if (merged != null) {
try{ merged.close(); } catch (Throwable e) {
LOG.error("Unable to merge source and patch locations", e);
}
}
}