Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Sep 25, 2018
1 parent a41233b commit 72883b1
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -175,6 +176,7 @@ private final class ProcessorContextImpl implements ProcessorContext {
private final Set<String> resourceBundles = new HashSet<>();
private final Set<String> runtimeInitializedClasses = new HashSet<>();
private final Set<List<String>> proxyClasses = new HashSet<>();
private final Map<String, Object> properties = new HashMap<>();

@Override
public BytecodeRecorder addStaticInitTask(int priority) {
Expand Down Expand Up @@ -301,6 +303,16 @@ public boolean isCapabilityPresent(String capability) {
return capabilities.contains(capability);
}

@Override
public <T> void setProperty(String key, T value) {
properties.put(key, value);
}

@Override
public <T> T getProperty(String key) {
return (T) properties.get(key);
}


void writeMainClass() throws IOException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,8 @@ public interface ProcessorContext {
* @return
*/
boolean isCapabilityPresent(String capability);

<T> void setProperty(String key, T value);

<T> T getProperty(String key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public class DataSourceTransactionTestCase {

@Test
public void testTransactionalAnnotation() {
//TODO: this does not really belong here, but it saves having to set all the DB stuff up again

Assert.assertEquals("PASSED", URLTester.relative("rest/datasource/txninterceptor0").invokeURL().asString());

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ class FastBootMetadataBuilder {
addPUManagedClassNamesToMetadataSources(persistenceUnit, metadataSources);

this.metamodelBuilder = (MetadataBuilderImplementor) metadataSources.getMetadataBuilder( standardServiceRegistry );
this.metamodelBuilder.applyScanner(scanner);
if( scanner != null ) {
this.metamodelBuilder.applyScanner( scanner );
}
populate( metamodelBuilder, mergedSettings, standardServiceRegistry );

this.managedResources = MetadataBuildingProcess.prepare(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ public final class PersistenceUnitsHolder {

private static final Object NO_NAME_TOKEN = new Object();

/**
* Initialized JPA for use in a native image. This must be called from within a static init method.
*
* In general the <code>parsedPersistenceXmlDescriptors</code> will be provided by caling {@link #loadOriginalXMLParsedDescriptors()}.
*
* The scanner may be null to use to default scanner, or a custom scanner can be used to stop hibernate scanning
* @param parsedPersistenceXmlDescriptors
* @param scanner
*/
public static void initializeJpa(List<ParsedPersistenceXmlDescriptor> parsedPersistenceXmlDescriptors, Scanner scanner) {
final List<PersistenceUnitDescriptor> units = convertPersistenceUnits( parsedPersistenceXmlDescriptors );
final Map<String,RecordedState> metadata = constructMetadataAdvance( parsedPersistenceXmlDescriptors , scanner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
*/
public final class HibernateResourceProcessor implements ResourceProcessor {

public static final String PARSED_DESCRIPTORS = HibernateResourceProcessor.class.getPackage().getName() + ".parsedDescriptors";

@Override
public void process(final ArchiveContext archiveContext, final ProcessorContext processorContext) throws Exception {

List<ParsedPersistenceXmlDescriptor> descriptors = PersistenceUnitsHolder.loadOriginalXMLParsedDescriptors();
processorContext.setProperty(PARSED_DESCRIPTORS, descriptors);

// Hibernate specific reflective classes; these are independent from the model and configuration details.
HibernateReflectiveNeeds.registerStaticReflectiveNeeds(processorContext);
Expand All @@ -43,16 +46,18 @@ public void process(final ArchiveContext archiveContext, final ProcessorContext
//Modify the bytecode of all entities to enable lazy-loading, dirty checking, etc..
enhanceEntities(domainObjects, archiveContext, processorContext);

//set up the scanner, as this scanning has already been done we need to just tell it about the classes we
//have discovered. This scanner is bytecode serializable and is passed directly into the template
ShamrockScanner scanner = new ShamrockScanner();
Set<ClassDescriptor> classDescriptors = new HashSet<>();
for (String i : domainObjects.getClassNames()) {
ShamrockScanner.ClassDescriptorImpl desc = new ShamrockScanner.ClassDescriptorImpl(i, ClassDescriptor.Categorization.MODEL);
classDescriptors.add(desc);
}
scanner.setClassDescriptors(classDescriptors);
PersistenceUnitsHolder.initializeJpa(descriptors, scanner);

try (BytecodeRecorder recorder = processorContext.addStaticInitTask(RuntimePriority.JPA_DEPLOYMENT)) {
//now we serialize the XML and class list to bytecode, to remove the need to re-parse the XML on JVM startup
recorder.registerNonDefaultConstructor(ParsedPersistenceXmlDescriptor.class.getDeclaredConstructor(URL.class), (i) -> Collections.singletonList(i.getPersistenceUnitRootUrl()));
recorder.getRecordingProxy(JPADeploymentTemplate.class).initMetadata(descriptors, scanner, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.jboss.shamrock.deployment.ResourceProcessor;
import org.jboss.shamrock.deployment.RuntimePriority;
import org.jboss.shamrock.deployment.codegen.BytecodeRecorder;
import org.jboss.shamrock.jpa.HibernateResourceProcessor;
import org.jboss.shamrock.jpa.runtime.JPADeploymentTemplate;
import org.jboss.shamrock.jpa.runtime.cdi.SystemEntityManager;
import org.jboss.shamrock.jpa.runtime.cdi.TransactionScopedEntityManager;
Expand Down Expand Up @@ -67,7 +68,8 @@ public void process(ArchiveContext archiveContext, ProcessorContext processorCon
//this is not great, we really need a better way to do this than generating bytecode

String defaultName = null;
List<PersistenceUnitDescriptor> pus = PersistenceUnitsHolder.getPersistenceUnitDescriptors();
List<PersistenceUnitDescriptor> pus = processorContext.getProperty(HibernateResourceProcessor.PARSED_DESCRIPTORS);
//look through the parsed descriptors to see if we can figure out the default PU name
if(pus.size() ==1) {
defaultName = pus.get(0).getName();

Expand All @@ -90,6 +92,7 @@ public void process(ArchiveContext archiveContext, ProcessorContext processorCon
try(BytecodeRecorder recorder = processorContext.addDeploymentTask(RuntimePriority.BOOTSTRAP_EMF)) {
JPADeploymentTemplate template = recorder.getRecordingProxy(JPADeploymentTemplate.class);

//every persistence unit needs a producer, even if the factory is not injectable
for (String name : allKnownNames) {
String className = getClass().getName() + "$$EMFProducer-" + name;
AtomicReference<byte[]> bytes = new AtomicReference<>();
Expand All @@ -113,7 +116,7 @@ public void process(ArchiveContext archiveContext, ProcessorContext processorCon
producer.returnValue(ret);
}
beanDeployment.addGeneratedBean(className, bytes.get());
template.boostrapPu(null, system);
template.boostrapPu(null, system); //force PU bootstrap at startup
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public Class<? extends Annotation> annotationType() {

public void initMetadata(List<ParsedPersistenceXmlDescriptor> parsedPersistenceXmlDescriptors, Scanner scanner, @ContextObject("bean.container") BeanContainer beanContainer) {

//this initializes the JPA metadata, and also sets the datasource if no connection URL has been set and a DataSource
//is available
if (beanContainer != null) {
BeanContainer.Factory<DataSource> ds = beanContainer.instanceFactory(DataSource.class);
if (ds != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import org.hibernate.boot.archive.scan.spi.Scanner;
import org.hibernate.boot.archive.spi.InputStreamAccess;

/**
* A hard coded scanner. This scanner is serialized to bytecode, and used to avoid scanning on hibernate startup
*/
public class ShamrockScanner implements Scanner {

private Set<ClassDescriptor> classDescriptors;
Expand Down

0 comments on commit 72883b1

Please sign in to comment.