Search in sources :

Example 96 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project jbpm by kiegroup.

the class CorrelationPersistenceTest method before.

@Before
public void before() throws Exception {
    context = setupWithPoolingDataSource(JBPM_PERSISTENCE_UNIT_NAME);
    CorrelationKeyFactory factory = KieInternalServices.Factory.get().newCorrelationKeyFactory();
    // populate table with test data
    EntityManagerFactory emf = (EntityManagerFactory) context.get(EnvironmentName.ENTITY_MANAGER_FACTORY);
    UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
    ut.begin();
    try {
        EntityManager em = emf.createEntityManager();
        em.persist(factory.newCorrelationKey("test123"));
        List<String> props = new ArrayList<String>();
        props.add("test123");
        props.add("123test");
        em.persist(factory.newCorrelationKey(props));
        ut.commit();
    } catch (Exception ex) {
        ut.rollback();
        Assert.fail("Exception thrown while trying to prepare correlation data.");
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) EntityManager(javax.persistence.EntityManager) CorrelationKeyFactory(org.kie.internal.process.CorrelationKeyFactory) EntityManagerFactory(javax.persistence.EntityManagerFactory) ArrayList(java.util.ArrayList) Before(org.junit.Before)

Example 97 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project jbpm by kiegroup.

the class CorrelationPersistenceTest method testCreateCorrelationMultiValueDoesMatch.

@Test
public void testCreateCorrelationMultiValueDoesMatch() throws Exception {
    EntityManagerFactory emf = (EntityManagerFactory) context.get(EnvironmentName.ENTITY_MANAGER_FACTORY);
    EntityManager em = emf.createEntityManager();
    Query query = em.createNamedQuery("GetProcessInstanceIdByCorrelation");
    query.setParameter("ckey", "test123:123test");
    List<Long> processInstances = query.getResultList();
    em.close();
    assertNotNull(processInstances);
    assertEquals(1, processInstances.size());
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) EntityManagerFactory(javax.persistence.EntityManagerFactory) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 98 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project jbpm by kiegroup.

the class ProcessInstanceResolverStrategyTest method testWithDatabaseAndStartProcess.

@Test
public void testWithDatabaseAndStartProcess() throws Exception {
    // Create variable
    Map<String, Object> params = new HashMap<String, Object>();
    NonSerializableClass processVar = new NonSerializableClass();
    processVar.setString("1234567890");
    params.put(VAR_NAME, processVar);
    // Persist variable
    UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    ut.begin();
    EntityManagerFactory emf = (EntityManagerFactory) context.get(ENTITY_MANAGER_FACTORY);
    EntityManager em = emf.createEntityManager();
    em.setFlushMode(FlushModeType.COMMIT);
    em.joinTransaction();
    em.persist(processVar);
    em.close();
    ut.commit();
    // Generate, insert, and start process
    ProcessInstance processInstance = ksession.startProcess(PROCESS_ID, params);
    // Test resuls
    Assert.assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    processVar = (NonSerializableClass) ((WorkflowProcessInstance) processInstance).getVariable(VAR_NAME);
    Assert.assertNotNull(processVar);
}
Also used : UserTransaction(javax.transaction.UserTransaction) NonSerializableClass(org.jbpm.persistence.processinstance.objects.NonSerializableClass) EntityManager(javax.persistence.EntityManager) HashMap(java.util.HashMap) EntityManagerFactory(javax.persistence.EntityManagerFactory) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) InitialContext(javax.naming.InitialContext) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 99 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project jbpm by kiegroup.

the class RuntimeEnvironmentBuilder method getDefault.

/**
 * Provides default configuration of <code>RuntimeEnvironmentBuilder</code> that is based on:
 * <ul>
 * 	<li>DefaultRuntimeEnvironment</li>
 * </ul>
 * This one is tailored to works smoothly with kjars as the notion of kbase and ksessions
 * @param releaseId <code>ReleaseId</code> that described the kjar
 * @param kbaseName name of the kbase defined in kmodule.xml stored in kjar
 * @param ksessionName name of the ksession define in kmodule.xml stored in kjar
 * @return new instance of <code>RuntimeEnvironmentBuilder</code> that is already preconfigured with defaults
 *
 * @see DefaultRuntimeEnvironment
 */
public static RuntimeEnvironmentBuilder getDefault(ReleaseId releaseId, String kbaseName, String ksessionName) {
    KieMavenRepository repository = KieMavenRepository.getKieMavenRepository();
    repository.resolveArtifact(releaseId.toExternalForm());
    KieServices ks = KieServices.Factory.get();
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    DeploymentDescriptorManager descriptorManager = new DeploymentDescriptorManager();
    List<DeploymentDescriptor> descriptorHierarchy = descriptorManager.getDeploymentDescriptorHierarchy(kieContainer);
    DeploymentDescriptorMerger merger = new DeploymentDescriptorMerger();
    DeploymentDescriptor descriptor = merger.merge(descriptorHierarchy, MergeMode.MERGE_COLLECTIONS);
    if (StringUtils.isEmpty(kbaseName)) {
        KieBaseModel defaultKBaseModel = ((KieContainerImpl) kieContainer).getKieProject().getDefaultKieBaseModel();
        if (defaultKBaseModel != null) {
            kbaseName = defaultKBaseModel.getName();
        } else {
            kbaseName = DEFAULT_KBASE_NAME;
        }
    }
    InternalKieModule module = (InternalKieModule) ((KieContainerImpl) kieContainer).getKieModuleForKBase(kbaseName);
    if (module == null) {
        throw new IllegalStateException("Cannot find kbase, either it does not exist or there are multiple default kbases in kmodule.xml");
    }
    KieBase kbase = kieContainer.getKieBase(kbaseName);
    RuntimeEnvironmentBuilder builder = null;
    if (descriptor.getPersistenceMode() == PersistenceMode.NONE) {
        builder = getDefaultInMemory();
    } else {
        builder = getDefault();
    }
    Map<String, Object> contaxtParams = new HashMap<String, Object>();
    contaxtParams.put("classLoader", kieContainer.getClassLoader());
    // populate various properties of the builder
    if (descriptor.getPersistenceUnit() != null) {
        EntityManagerFactory emf = EntityManagerFactoryManager.get().getOrCreate(descriptor.getPersistenceUnit());
        builder.entityManagerFactory(emf);
        contaxtParams.put("entityManagerFactory", emf);
    }
    // process object models that are globally configured (environment entries, session configuration)
    for (NamedObjectModel model : descriptor.getEnvironmentEntries()) {
        Object entry = getInstanceFromModel(model, kieContainer, contaxtParams);
        builder.addEnvironmentEntry(model.getName(), entry);
    }
    for (NamedObjectModel model : descriptor.getConfiguration()) {
        Object entry = getInstanceFromModel(model, kieContainer, contaxtParams);
        builder.addConfiguration(model.getName(), (String) entry);
    }
    ObjectMarshallingStrategy[] mStrategies = new ObjectMarshallingStrategy[descriptor.getMarshallingStrategies().size() + 1];
    int index = 0;
    for (ObjectModel model : descriptor.getMarshallingStrategies()) {
        Object strategy = getInstanceFromModel(model, kieContainer, contaxtParams);
        mStrategies[index] = (ObjectMarshallingStrategy) strategy;
        index++;
    }
    // lastly add the main default strategy
    mStrategies[index] = new SerializablePlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT);
    builder.addEnvironmentEntry(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, mStrategies);
    builder.addEnvironmentEntry("KieDeploymentDescriptor", descriptor).knowledgeBase(kbase).classLoader(kieContainer.getClassLoader()).registerableItemsFactory(new KModuleRegisterableItemsFactory(kieContainer, ksessionName));
    return builder;
}
Also used : SerializablePlaceholderResolverStrategy(org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy) ObjectModel(org.kie.internal.runtime.conf.ObjectModel) NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) HashMap(java.util.HashMap) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) DeploymentDescriptorManager(org.jbpm.runtime.manager.impl.deploy.DeploymentDescriptorManager) KieServices(org.kie.api.KieServices) KieBaseModel(org.kie.api.builder.model.KieBaseModel) NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) KieBase(org.kie.api.KieBase) EntityManagerFactory(javax.persistence.EntityManagerFactory) KieMavenRepository(org.kie.scanner.KieMavenRepository) DeploymentDescriptorMerger(org.jbpm.runtime.manager.impl.deploy.DeploymentDescriptorMerger) KieContainer(org.kie.api.runtime.KieContainer) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 100 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project jbpm by kiegroup.

the class PersistenceUtil method cleanUp.

/**
 * This method should be called in the @After method of a test to clean up
 * the persistence unit and datasource.
 *
 * @param context
 *            A HashMap generated by
 *            {@link org.drools.persistence.util.PersistenceUtil setupWithPoolingDataSource(String)}
 */
public static void cleanUp(Map<String, Object> context) {
    if (context != null) {
        Object emfObject = context.remove(ENTITY_MANAGER_FACTORY);
        if (emfObject != null) {
            try {
                EntityManagerFactory emf = (EntityManagerFactory) emfObject;
                emf.close();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
        Object ds1Object = context.remove(DATASOURCE);
        if (ds1Object != null) {
            try {
                PoolingDataSource ds1 = (PoolingDataSource) ds1Object;
                ds1.close();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }
}
Also used : PoolingDataSource(org.jbpm.test.util.PoolingDataSource) EntityManagerFactory(javax.persistence.EntityManagerFactory)

Aggregations

EntityManagerFactory (javax.persistence.EntityManagerFactory)302 EntityManager (javax.persistence.EntityManager)103 Test (org.junit.Test)90 HashMap (java.util.HashMap)48 EntityTransaction (javax.persistence.EntityTransaction)30 EJBException (javax.ejb.EJBException)22 Map (java.util.Map)17 AssertionFailedError (junit.framework.AssertionFailedError)17 TestFailureException (org.apache.openejb.test.TestFailureException)17 ArrayList (java.util.ArrayList)15 JMSException (javax.jms.JMSException)15 KieSession (org.kie.api.runtime.KieSession)14 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)14 Properties (java.util.Properties)13 InitialContext (javax.naming.InitialContext)13 List (java.util.List)12 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)12 RemoteException (java.rmi.RemoteException)11 Query (javax.persistence.Query)11 PrintWriter (java.io.PrintWriter)10