Showing posts with label BPM. Show all posts
Showing posts with label BPM. Show all posts

Sunday, March 8, 2015

Oracle BPM: Solving "The XID is not valid" exception

Recently i faced a strange exception while using the BPM server:
       

java.sql.SQLException: Unexpected exception while enlisting XAConnection java.sql.SQLException: XA error: XAResource.XAER_NOTA start() failed on resource 'SOADataSource_domain': XAER_NOTA : The XID is not valid oracle.jdbc.xa.OracleXAException at oracle.jdbc.xa.OracleXAResource.checkError(OracleXAResource.java:1616)
       
 

In order to resolve this exception, you need to edit your Custom XA Datasource which your BPM processes use with the following attribute.
Enable "Set XA Transaction Timeout"


Restart you domain (Admin and SOA servers)

That's it.

Oracle BPM: Assign User to Application Role Programmatically

In order to assign users to application role programmatically in Oracle BPM, user the following code:

       


import java.util.HashMap;
import java.util.Map;

import oracle.bpel.services.bpm.common.IBPMContext;
import oracle.bpel.services.workflow.client.IWorkflowServiceClientConstants;
import oracle.bpel.services.workflow.client.WorkflowServiceClientFactory;
import oracle.bpel.services.workflow.verification.IWorkflowContext;

import oracle.bpm.client.BPMServiceClientFactory;
import oracle.bpm.services.organization.BPMOrganizationException;
import oracle.bpm.services.organization.model.ApplicationContext;
import oracle.bpm.services.organization.model.ApplicationContextTypeEnum;
import oracle.bpm.services.organization.model.ParticipantTypeEnum;
import oracle.bpm.services.organization.model.PrincipleRefType;
import oracle.bpm.workspace.util.WorkspaceUtil;

public class CustomWorklistUtils {
    public CustomWorklistUtils() {
        super();
    }
    private IBPMContext bpmContext = null;

    private static String adminUsername;
    private static String adminPassword;
    private static String bpmURL;


    public boolean createNewMember(String appRoleName, String member,
                                   String type) {
        System.out.println("Assigning User to ApplicationRole(Lane)...");
        System.out.println("...appRoleName: " + appRoleName);
        System.out.println("...member: " + member);
        System.out.println("...type: " + type);
        if (appRoleName == null) {
            return false;
        }

        PrincipleRefType principleRefType =
            WorkspaceUtil.getBPMObjectFactory().createPrincipleRefType();
        principleRefType.setName(member);
        principleRefType.setRealm(getBPMContext().getIdentityContext());

        if (type.equalsIgnoreCase(("LABEL_USER"))) {
            principleRefType.setType(ParticipantTypeEnum.USER);
        } else if (type.equalsIgnoreCase(("LABEL_GROUP"))) {
            principleRefType.setType(ParticipantTypeEnum.GROUP);
        } else if (type.equalsIgnoreCase(("LABEL_APPROLE"))) {
            principleRefType.setType(ParticipantTypeEnum.APPROLE);
        }

        ApplicationContext appContext =
            WorkspaceUtil.getBPMObjectFactory().createApplicationContext();
        appContext.setApplicationType(ApplicationContextTypeEnum.ORACLE_BPM_PROCESS_ROLES_APP);
        oracle.bpm.services.organization.model.Participant participant =
            WorkspaceUtil.getBPMObjectFactory().createParticipant(principleRefType);
        try {
            WorkspaceUtil.getBPMOrgUnitService().grantAppRoleToPrincipal(getBPMContext(),
                                                                         appContext,
                                                                         appRoleName,
                                                                         participant);

            return true;
        } catch (BPMOrganizationException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public boolean removeMemberFromAppRole(String appRoleName, String member,
                                           String type) {
        if (appRoleName == null) {
            return false;
        }

        PrincipleRefType principleRefType =
            WorkspaceUtil.getBPMObjectFactory().createPrincipleRefType();
        principleRefType.setName(member);
        principleRefType.setRealm(getBPMContext().getIdentityContext());

        if (type.equalsIgnoreCase(("LABEL_USER"))) {
            principleRefType.setType(ParticipantTypeEnum.USER);
        } else if (type.equalsIgnoreCase(("LABEL_GROUP"))) {
            principleRefType.setType(ParticipantTypeEnum.GROUP);
        } else if (type.equalsIgnoreCase(("LABEL_APPROLE"))) {
            principleRefType.setType(ParticipantTypeEnum.APPROLE);
        }

        ApplicationContext appContext =
            WorkspaceUtil.getBPMObjectFactory().createApplicationContext();
        appContext.setApplicationType(ApplicationContextTypeEnum.ORACLE_BPM_PROCESS_ROLES_APP);
        oracle.bpm.services.organization.model.Participant participant;
        participant =
                WorkspaceUtil.getBPMObjectFactory().createParticipant(principleRefType);
        try {
            WorkspaceUtil.getBPMOrgUnitService().revokeAppRoleFromPrincipal(getBPMContext(),
                                                                            appContext,
                                                                            appRoleName,
                                                                            participant);

            return true;
        } catch (BPMOrganizationException ex) {
            ex.printStackTrace();
        }

        return false;
    }

    public IBPMContext getBPMContext() {

        try {
            if (bpmContext == null) {

                bpmContext = getIBPMContext(adminUsername, adminPassword);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bpmContext;
    }

    public static IBPMContext getIBPMContext(String username,
                                             String password) throws Exception {
        return getBPMServiceClientFactory().getBPMUserAuthenticationService().authenticate(username,
                                                                                           password.toCharArray(),
                                                                                           null);
    }

    public static BPMServiceClientFactory getBPMServiceClientFactory() {

        bpmURL = "t3://localhost:8001";
        adminUsername = "weblogic";
        adminPassword = "welcome1";

        Map properties = new HashMap();

        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.CLIENT_TYPE,
                       WorkflowServiceClientFactory.REMOTE_CLIENT);
        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_PROVIDER_URL,
                       bpmURL);
        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_INITIAL_CONTEXT_FACTORY,
                       "weblogic.jndi.WLInitialContextFactory");
        return BPMServiceClientFactory.getInstance(properties, null, null);
    }

}

       
 

Note that you have to add the Organization Service into the foreign JNDI on your domain:
Name: ejb/bpm/services/BPMOrganizationServiceBean
Local JNDI Name: ejb/bpm/services/BPMOrganizationServiceBean
Remote JNDI Name: ejb/bpm/services/BPMOrganizationServiceBean

 CustomWorklistUtils.java