private EntityManagerFactory createAndPublishEMF()

in jpa-container/src/main/java/org/apache/aries/jpa/container/impl/AriesEntityManagerFactoryBuilder.java [337:417]


	private EntityManagerFactory createAndPublishEMF(Map<String, Object> overrides) {
		
		boolean makeTracker;
		String dbDriver;
		synchronized (this) {
			makeTracker = driver != null && tracker == null;
			dbDriver = driver; 
		}
		
		if(makeTracker) {
			ServiceTracker<?, ?> dsfTracker = new DSFTracker(containerContext, 
					this, dbDriver);
			synchronized (this) {
				tracker = dsfTracker;
				activeProps = overrides;
			}
			dsfTracker.open();
			
			synchronized (this) {
				if(emf == null) {
					throw new IllegalStateException("No database driver is currently available for class " + dbDriver);
				} else {
					return emf;
				}
			}
		} else {
			synchronized (this) {
				if(!complete) {
					throw new IllegalArgumentException("The persistence unit " + getPUName() + 
							" has incomplete configuration and cannot be created. The configuration is" + overrides);
				}
			}
		}
		
		final EntityManagerFactory tmp = provider.createContainerEntityManagerFactory(persistenceUnit, overrides);
		boolean register = false;
		synchronized (this) {
			if(emf == null) {
				emf = tmp;
				activeProps = overrides;
				register = true;
			}
		}
		if(register) {
			Dictionary<String, Object> props = createBuilderProperties(overrides);
			BundleContext uctx = bundle.getBundleContext();
			ServiceRegistration<EntityManagerFactory> tmpReg = 
					uctx.registerService(EntityManagerFactory.class, 
							(EntityManagerFactory) Proxy.newProxyInstance(getClass().getClassLoader(), 
									new Class<?>[] {EntityManagerFactory.class}, 
									new InvocationHandler() {
										
										@Override
										public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
											if("close".equals(method.getName())) {
												// Ignore close as per the spec
												return null;
											}
											return method.invoke(tmp, args);
										}
									}), props);
			
			synchronized (this) {
				if(emf == tmp) {
					reg = tmpReg;
				} else {
					register = false;
				}
			}
			
			if(!register) {
				tmpReg.unregister();
			}
		} else {
			tmp.close();
			synchronized (this) {
				return emf;
			}
		}
		return tmp;
	}