Search in sources :

Example 1 with AccessTimeout

use of javax.ejb.AccessTimeout in project Payara by payara.

the class AccessTimeoutHandler method processAnnotation.

protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, EjbContext[] ejbContexts) throws AnnotationProcessorException {
    AccessTimeout timeout = (AccessTimeout) ainfo.getAnnotation();
    for (EjbContext ejbContext : ejbContexts) {
        if (ejbContext.getDescriptor() instanceof EjbSessionDescriptor) {
            EjbSessionDescriptor sessionDesc = (EjbSessionDescriptor) ejbContext.getDescriptor();
            if (sessionDesc.isStateless()) {
                continue;
            }
            if (ElementType.TYPE.equals(ainfo.getElementType())) {
                // Delay processing Class-level default until after methods are processed
                ejbContext.addPostProcessInfo(ainfo, this);
            } else {
                Method annMethod = (Method) ainfo.getAnnotatedElement();
                // are overridden and applies the correct .xml overriding semantics.
                if (!matchesExistingAccessTimeoutMethod(annMethod, sessionDesc)) {
                    MethodDescriptor newMethodDesc = new MethodDescriptor(annMethod);
                    sessionDesc.addAccessTimeoutMethod(newMethodDesc, timeout.value(), timeout.unit());
                }
            }
        }
    }
    return getDefaultProcessedResult();
}
Also used : AccessTimeout(javax.ejb.AccessTimeout) EjbContext(com.sun.enterprise.deployment.annotation.context.EjbContext) Method(java.lang.reflect.Method) EjbSessionDescriptor(org.glassfish.ejb.deployment.descriptor.EjbSessionDescriptor) MethodDescriptor(com.sun.enterprise.deployment.MethodDescriptor)

Example 2 with AccessTimeout

use of javax.ejb.AccessTimeout in project muikku by otavanopisto.

the class PyramusUpdater method updatePerson.

@Lock(LockType.WRITE)
@AccessTimeout(value = 30000)
public void updatePerson(Person person) {
    Long userEntityId = null;
    String defaultIdentifier = null;
    Long defaultUserId = person.getDefaultUserId();
    UserRole defaultUserPyramusRole = null;
    List<String> identifiers = new ArrayList<>();
    List<String> removedIdentifiers = new ArrayList<>();
    List<String> updatedIdentifiers = new ArrayList<>();
    List<String> discoveredIdentifiers = new ArrayList<>();
    Map<SchoolDataIdentifier, List<String>> emails = new HashMap<>();
    // List all person's students and staffMembers
    Student[] students = pyramusClient.get().get(String.format("/persons/persons/%d/students", person.getId()), Student[].class);
    StaffMember[] staffMembers = pyramusClient.get().get(String.format("/persons/persons/%d/staffMembers", person.getId()), StaffMember[].class);
    // If person does not have a defaultUserId specified, we try to guess something
    if (defaultUserId == null) {
        if ((staffMembers != null) && (staffMembers.length > 0)) {
            // If person has a staffMember instance, lets use that one
            defaultUserId = staffMembers[0].getId();
        } else {
            if (students != null) {
                // Otherwise just use first non archived student (if any)
                for (Student student : students) {
                    if (!student.getArchived()) {
                        defaultUserId = student.getId();
                        break;
                    }
                }
            }
        }
    }
    if (students != null) {
        // Iterate over all student instances
        for (Student student : students) {
            String identifier = identifierMapper.getStudentIdentifier(student.getId());
            SchoolDataIdentifier schoolDataIdentifier = toIdentifier(identifier);
            List<String> identifierEmails = new ArrayList<String>();
            if (!student.getArchived()) {
                // If student is not archived, add it to identifiers list
                identifiers.add(identifier);
                // If it's the specified defaultUserId, update defaultIdentifier and role accordingly
                if ((defaultIdentifier == null) && student.getId().equals(defaultUserId)) {
                    defaultIdentifier = identifier;
                    defaultUserPyramusRole = UserRole.STUDENT;
                }
                // List emails and add all emails that are not specified non unique (e.g. contact persons) to the emails list
                Email[] studentEmails = pyramusClient.get().get("/students/students/" + student.getId() + "/emails", Email[].class);
                if (studentEmails != null) {
                    for (Email studentEmail : studentEmails) {
                        if (studentEmail.getContactTypeId() != null) {
                            ContactType contactType = pyramusClient.get().get("/common/contactTypes/" + studentEmail.getContactTypeId(), ContactType.class);
                            if (!contactType.getNonUnique() && !identifierEmails.contains(studentEmail.getAddress())) {
                                identifierEmails.add(studentEmail.getAddress());
                            }
                        } else {
                            logger.log(Level.WARNING, "ContactType of email is null - email is ignored");
                        }
                    }
                }
            } else {
                // If the student instance if archived, we add it the the removed identifiers list
                removedIdentifiers.add(identifier);
            }
            emails.put(schoolDataIdentifier, identifierEmails);
        }
    }
    if (staffMembers != null) {
        for (StaffMember staffMember : staffMembers) {
            // Add staffMember identifier into the identifier list
            String identifier = identifierMapper.getStaffIdentifier(staffMember.getId());
            SchoolDataIdentifier schoolDataIdentifier = toIdentifier(identifier);
            List<String> identifierEmails = new ArrayList<String>();
            identifiers.add(identifier);
            // If it's the specified defaultUserId, update defaultIdentifier and role accordingly
            if ((defaultIdentifier == null) && staffMember.getId().equals(defaultUserId)) {
                defaultIdentifier = identifier;
                defaultUserPyramusRole = staffMember.getRole();
            }
            // List emails and add all emails that are not specified non unique (e.g. contact persons) to the emails list
            Email[] staffMemberEmails = pyramusClient.get().get("/staff/members/" + staffMember.getId() + "/emails", Email[].class);
            if (staffMemberEmails != null) {
                for (Email staffMemberEmail : staffMemberEmails) {
                    if (staffMemberEmail.getContactTypeId() != null) {
                        ContactType contactType = pyramusClient.get().get("/common/contactTypes/" + staffMemberEmail.getContactTypeId(), ContactType.class);
                        if (!contactType.getNonUnique() && !identifierEmails.contains(staffMemberEmail.getAddress())) {
                            identifierEmails.add(staffMemberEmail.getAddress());
                        }
                    } else {
                        logger.log(Level.WARNING, "ContactType of email is null - email is ignored");
                    }
                }
            }
            emails.put(schoolDataIdentifier, identifierEmails);
        }
    }
    // Iterate over all discovered identifiers (students and staff members)
    for (String identifier : identifiers) {
        UserSchoolDataIdentifier userSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierByDataSourceAndIdentifier(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE, identifier);
        if (userSchoolDataIdentifier == null) {
            // If no user entity can be found by the identifier, add it the the discovered identities list
            discoveredIdentifiers.add(identifier);
        } else {
            // user entity found with given identity, so we need to make sure they all belong to same user
            UserEntity userEntity = userSchoolDataIdentifier.getUserEntity();
            if (userEntityId == null) {
                userEntityId = userEntity.getId();
            } else if (!userEntityId.equals(userEntity.getId())) {
                logger.warning(String.format("Person %d synchronization failed. Found two userEntitys bound to it (%d and %d)", person.getId(), userEntityId, userEntity.getId()));
                return;
            }
        }
    }
    UserEntity userEntity = userEntityId != null ? userEntityController.findUserEntityById(userEntityId) : null;
    if (userEntity != null) {
        // User already exists in the system so we check which of the identifiers have been removed and which just updated
        List<UserSchoolDataIdentifier> existingSchoolDataIdentifiers = userSchoolDataIdentifierController.listUserSchoolDataIdentifiersByUserEntity(userEntity);
        for (UserSchoolDataIdentifier existingSchoolDataIdentifier : existingSchoolDataIdentifiers) {
            if (existingSchoolDataIdentifier.getDataSource().getIdentifier().equals(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE)) {
                if (!identifiers.contains(existingSchoolDataIdentifier.getIdentifier())) {
                    if (!removedIdentifiers.contains(existingSchoolDataIdentifier.getIdentifier())) {
                        removedIdentifiers.add(existingSchoolDataIdentifier.getIdentifier());
                    }
                } else if (!discoveredIdentifiers.contains(existingSchoolDataIdentifier.getIdentifier())) {
                    updatedIdentifiers.add(existingSchoolDataIdentifier.getIdentifier());
                }
            }
        }
    }
    // Resolve the user's desired environment role
    SchoolDataIdentifier environmentRoleIdentifier = null;
    if (defaultUserPyramusRole != null) {
        String roleIdentifier = identifierMapper.getEnvironmentRoleIdentifier(defaultUserPyramusRole);
        environmentRoleIdentifier = new SchoolDataIdentifier(roleIdentifier, SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE);
    }
    // And finally fire the update event
    fireSchoolDataUserUpdated(userEntityId, defaultIdentifier, removedIdentifiers, updatedIdentifiers, discoveredIdentifiers, emails, environmentRoleIdentifier);
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) RoleSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.RoleSchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) Email(fi.otavanopisto.pyramus.rest.model.Email) ContactType(fi.otavanopisto.pyramus.rest.model.ContactType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Student(fi.otavanopisto.pyramus.rest.model.Student) StudentGroupStudent(fi.otavanopisto.pyramus.rest.model.StudentGroupStudent) CourseStudent(fi.otavanopisto.pyramus.rest.model.CourseStudent) CourseStaffMember(fi.otavanopisto.pyramus.rest.model.CourseStaffMember) StaffMember(fi.otavanopisto.pyramus.rest.model.StaffMember) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) UserGroupUserEntity(fi.otavanopisto.muikku.model.users.UserGroupUserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) UserRole(fi.otavanopisto.pyramus.rest.model.UserRole) List(java.util.List) ArrayList(java.util.ArrayList) AccessTimeout(javax.ejb.AccessTimeout) Lock(javax.ejb.Lock)

Example 3 with AccessTimeout

use of javax.ejb.AccessTimeout in project muikku by otavanopisto.

the class CommunicatorMessageSentNotifier method onCommunicatorMessageSent.

@Asynchronous
@AccessTimeout(value = 30, unit = TimeUnit.MINUTES)
@Transactional(value = TxType.REQUIRES_NEW)
public void onCommunicatorMessageSent(@Observes(during = TransactionPhase.AFTER_COMPLETION) CommunicatorMessageSent event) {
    CommunicatorMessage communicatorMessage = communicatorController.findCommunicatorMessageById(event.getCommunicatorMessageId());
    UserEntity sender = userEntityController.findUserEntityById(communicatorMessage.getSender());
    UserEntity recipient = userEntityController.findUserEntityById(event.getRecipientUserEntityId());
    if ((communicatorMessage != null) && (sender != null) && (recipient != null)) {
        if (!Objects.equals(sender.getId(), recipient.getId())) {
            Map<String, Object> params = new HashMap<String, Object>();
            User senderUser = userController.findUserByUserEntityDefaults(sender);
            params.put("sender", String.format("%s %s", senderUser.getFirstName(), senderUser.getLastName()));
            params.put("subject", communicatorMessage.getCaption());
            params.put("content", communicatorMessage.getContent());
            params.put("url", String.format("%s/communicator", event.getBaseUrl()));
            // TODO Hash paramters cannot be utilized in redirect URLs
            // params.put("url", String.format("%s/communicator#inbox/%d", baseUrl, message.getCommunicatorMessageId().getId()));
            notifierController.sendNotification(communicatorNewInboxMessageNotification, sender, recipient, params);
        }
    } else {
        logger.log(Level.SEVERE, String.format("Communicator couldn't send notifications as some entity was not found"));
    }
}
Also used : CommunicatorMessage(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage) User(fi.otavanopisto.muikku.schooldata.entity.User) HashMap(java.util.HashMap) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) Asynchronous(javax.ejb.Asynchronous) AccessTimeout(javax.ejb.AccessTimeout) Transactional(javax.transaction.Transactional)

Example 4 with AccessTimeout

use of javax.ejb.AccessTimeout in project Payara by payara.

the class AccessTimeoutHandler method postProcessAnnotation.

/**
 * Set the default value (from class type annotation) on all
 * methods that don't have a value.
 */
public void postProcessAnnotation(AnnotationInfo ainfo, EjbContext ejbContext) throws AnnotationProcessorException {
    EjbSessionDescriptor ejbDesc = (EjbSessionDescriptor) ejbContext.getDescriptor();
    // At this point, all method-level specific annotations have been processed.
    // For non-private methods, find the ones from the EjbContext's
    // component definition view that are declared on this class.  This will correctly
    // eliminate any overridden methods and provide the most-derived version of each.
    // Use the Class's declared methods list to get the private methods.
    Class classAn = (Class) ainfo.getAnnotatedElement();
    AccessTimeout timeoutAnn = (AccessTimeout) ainfo.getAnnotation();
    List<Method> toProcess = new ArrayList<Method>();
    for (Method m : ejbContext.getComponentDefinitionMethods()) {
        if (classAn.equals(m.getDeclaringClass())) {
            toProcess.add(m);
        }
    }
    for (Method m : classAn.getDeclaredMethods()) {
        if (Modifier.isPrivate(m.getModifiers())) {
            toProcess.add(m);
        }
    }
    for (Method m : toProcess) {
        // descriptor, set it.
        if (!matchesExistingAccessTimeoutMethod(m, ejbDesc)) {
            MethodDescriptor newMethodDesc = new MethodDescriptor(m);
            ejbDesc.addAccessTimeoutMethod(newMethodDesc, timeoutAnn.value(), timeoutAnn.unit());
        }
    }
}
Also used : AccessTimeout(javax.ejb.AccessTimeout) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) EjbSessionDescriptor(org.glassfish.ejb.deployment.descriptor.EjbSessionDescriptor) MethodDescriptor(com.sun.enterprise.deployment.MethodDescriptor)

Aggregations

AccessTimeout (javax.ejb.AccessTimeout)4 MethodDescriptor (com.sun.enterprise.deployment.MethodDescriptor)2 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 EjbSessionDescriptor (org.glassfish.ejb.deployment.descriptor.EjbSessionDescriptor)2 EjbContext (com.sun.enterprise.deployment.annotation.context.EjbContext)1 RoleSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.RoleSchoolDataIdentifier)1 UserGroupUserEntity (fi.otavanopisto.muikku.model.users.UserGroupUserEntity)1 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)1 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)1 CommunicatorMessage (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage)1 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)1 User (fi.otavanopisto.muikku.schooldata.entity.User)1 ContactType (fi.otavanopisto.pyramus.rest.model.ContactType)1 CourseStaffMember (fi.otavanopisto.pyramus.rest.model.CourseStaffMember)1 CourseStudent (fi.otavanopisto.pyramus.rest.model.CourseStudent)1 Email (fi.otavanopisto.pyramus.rest.model.Email)1 StaffMember (fi.otavanopisto.pyramus.rest.model.StaffMember)1