blob: d234b47f347ddd82ddb52267b8500f54f2479246 [file] [log] [blame]
/*
* Copyright (c) 1998, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// James Sutherland - initial API and implementation
package org.eclipse.persistence.testing.framework.server;
import java.util.HashMap;
import java.util.Map;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
/**
* Spring container platform.
*/
public class SpringPlatform implements ServerPlatform {
TransactionStatus status;
String persistenceUnit;
Map<String, ClassPathXmlApplicationContext> contexts = new HashMap<String, ClassPathXmlApplicationContext>();
/**
* This is a hack to enable weaving in Spring tests.
* The Spring agent does not load persistence units in premain
* So it must be forced to do so before any domain classes are loaded,
* otherwise weaving will not work.
* TODO: Spring needs to fix this or something.
*/
@Override
public void initialize() {
getContext("default");
getContext("fieldaccess");
}
/**
* Return if the Spring transaction is active.
*/
@Override
public boolean isTransactionActive() {
return (this.status != null);
}
/**
* Return if the Spring transaction is roll back only.
*/
@Override
public boolean getRollbackOnly() {
return (this.status != null) && (this.status.isRollbackOnly());
}
/**
* Start a new Spring transaction.
*/
@Override
public void beginTransaction() {
if (this.status != null) {
rollbackTransaction();
throw new Error("Attempt to begin transaction while transaction still active.");
}
this.status = getTransactionManager().getTransaction(new DefaultTransactionDefinition());
}
/**
* Commit the existing Spring transaction.
*/
@Override
public void commitTransaction() {
try {
getTransactionManager().commit(this.status);
} finally {
this.status = null;
}
}
/**
* Roll back the existing Spring transaction.
*/
@Override
public void rollbackTransaction() {
try {
getTransactionManager().rollback(this.status);
} finally {
this.status = null;
}
}
public PlatformTransactionManager getTransactionManager() {
return (JpaTransactionManager)getContext().getBean("transactionManager");
}
/**
* Mark the existing Spring transaction for rollback.
*/
@Override
public void setTransactionForRollback() {
if (this.status != null) {
this.status.setRollbackOnly();
}
}
/**
* Is the platform Oracle?
*/
@Override
public boolean isOc4j() {
return false;
}
/**
* Is the platform Weblogic?
*/
@Override
public boolean isWeblogic() {
return false;
}
/**
* Is the platform JBoss?
*/
@Override
public boolean isJBoss() {
return false;
}
/**
* Is the platform clustered?
*/
@Override
public boolean isClustered() {
return false;
}
/**
* Is the platform Spring?
*/
@Override
public boolean isSpring() {
return true;
}
/**
* Managed entity managers do not need to be closed.
*/
@Override
public void closeEntityManager(EntityManager entityManager) {
}
/**
* Return the managed EntityManager for the persistence unit.
*/
@Override
public EntityManager getEntityManager(String persistenceUnit) {
this.persistenceUnit = persistenceUnit;
return (EntityManager)getContext().getBean("entityManager");
//SpringBean bean = (SpringBean)getContext().getBean("testBean");
//return bean.getEntityManager();
}
/**
* Return the managed EntityManagerFactory for the persistence unit.
*/
@Override
public EntityManagerFactory getEntityManagerFactory(String persistenceUnit) {
this.persistenceUnit = persistenceUnit;
return (EntityManagerFactory)getContext().getBean("entityManagerFactory");
}
/**
* Return the managed EntityManagerFactory for the persistence unit.
*/
public ClassPathXmlApplicationContext getContext() {
return getContext(this.persistenceUnit);
}
/**
* Return the managed EntityManagerFactory for the persistence unit.
*/
public ClassPathXmlApplicationContext getContext(String persistenceUnit) {
ClassPathXmlApplicationContext context = this.contexts.get(persistenceUnit);
if (context == null) {
context = new ClassPathXmlApplicationContext(persistenceUnit + "-spring.xml");
this.contexts.put(persistenceUnit, context);
}
return context;
}
/**
* Join the transaction if required
*/
@Override
public void joinTransaction(EntityManager em) {
//bug 405308:a shared EM will throw an exception if joinTransaction is called on it.
}
}