use of org.hibernate.boot.jaxb.spi.Binding in project hibernate-orm by hibernate.
the class AdditionalJaxbMappingProducerImpl method produceAdditionalMappings.
@Override
public Collection<MappingDocument> produceAdditionalMappings(final MetadataImplementor metadata, IndexView jandexIndex, final MappingBinder mappingBinder, final MetadataBuildingContext buildingContext) {
final ServiceRegistry serviceRegistry = metadata.getMetadataBuildingOptions().getServiceRegistry();
final EnversService enversService = serviceRegistry.getService(EnversService.class);
if (!enversService.isEnabled()) {
// short-circuit if envers integration has been disabled.
return Collections.emptyList();
}
final ArrayList<MappingDocument> additionalMappingDocuments = new ArrayList<>();
// atm we do not have distinct origin info for envers
final Origin origin = new Origin(SourceType.OTHER, "envers");
// final DOMWriter writer = new DOMWriter();
final MappingCollector mappingCollector = new MappingCollector() {
@Override
public void addDocument(Document document) throws DocumentException {
dump(document);
// while the commented-out code here is more efficient (well, understanding that
// this whole process is un-efficient) it leads to un-decipherable messages when
// we get mapping mapping errors from envers output.
// final DOMSource domSource = new DOMSource( writer.write( document ) );
// domSource.setSystemId( "envers" );
// final Binding jaxbBinding = mappingBinder.bind( domSource, origin );
// this form at least allows us to get better error messages
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
final Writer w = new BufferedWriter(new OutputStreamWriter(baos, "UTF-8"));
final XMLWriter xw = new XMLWriter(w, new OutputFormat(" ", true));
xw.write(document);
w.flush();
} catch (IOException e) {
throw new HibernateException("Unable to bind Envers-generated XML", e);
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
BufferedInputStream bis = new BufferedInputStream(bais);
final Binding jaxbBinding = mappingBinder.bind(bis, origin);
final JaxbHbmHibernateMapping jaxbRoot = (JaxbHbmHibernateMapping) jaxbBinding.getRoot();
additionalMappingDocuments.add(new MappingDocument(jaxbRoot, origin, buildingContext));
}
};
enversService.initialize(metadata, mappingCollector);
return additionalMappingDocuments;
}
use of org.hibernate.boot.jaxb.spi.Binding in project hibernate-orm by hibernate.
the class CacheableFileXmlSource method doBind.
@Override
@SuppressWarnings("unchecked")
public Binding doBind(Binder binder) {
if (strict) {
try {
return new Binding(readSerFile(), getOrigin());
} catch (SerializationException e) {
throw new MappingException(String.format("Unable to deserialize from cached file [%s]", getOrigin().getName()), e, getOrigin());
} catch (FileNotFoundException e) {
throw new MappingException(String.format("Unable to locate cached file [%s]", getOrigin().getName()), e, getOrigin());
}
} else {
if (!isSerfileObsolete()) {
try {
return readSerFile();
} catch (SerializationException e) {
log.unableToDeserializeCache(serFile.getName(), e);
} catch (FileNotFoundException e) {
log.cachedFileNotFound(serFile.getName(), e);
}
} else {
log.cachedFileObsolete(serFile);
}
log.readingMappingsFromFile(xmlFile.getPath());
final Binding binding = FileXmlSource.doBind(binder, xmlFile, getOrigin());
writeSerFile(binding);
return binding;
}
}
use of org.hibernate.boot.jaxb.spi.Binding in project hibernate-orm by hibernate.
the class MappingBinder method doBind.
@Override
protected Binding doBind(XMLEventReader staxEventReader, StartElement rootElementStartEvent, Origin origin) {
final String rootElementLocalName = rootElementStartEvent.getName().getLocalPart();
if ("hibernate-mapping".equals(rootElementLocalName)) {
log.debugf("Performing JAXB binding of hbm.xml document : %s", origin.toString());
XMLEventReader hbmReader = new HbmEventReader(staxEventReader, xmlEventFactory);
JaxbHbmHibernateMapping hbmBindings = jaxb(hbmReader, LocalSchema.HBM.getSchema(), hbmJaxbContext(), origin);
return new Binding<JaxbHbmHibernateMapping>(hbmBindings, origin);
} else {
try {
final XMLEventReader reader = new JpaOrmXmlEventReader(staxEventReader, xmlEventFactory);
return new Binding<Document>(toDom4jDocument(reader, origin), origin);
} catch (JpaOrmXmlEventReader.BadVersionException e) {
throw new UnsupportedOrmXsdVersionException(e.getRequestedVersion(), origin);
}
}
}
Aggregations