Search in sources :

Example 26 with ObjectMarshallingStrategy

use of org.kie.api.marshalling.ObjectMarshallingStrategy in project opennms by OpenNMS.

the class DroolsCorrelationEngine method marshallStateToDisk.

private void marshallStateToDisk(boolean serialize) {
    final File stateFile = getPathToState().toFile();
    LOG.debug("Saving state for engine {} in {} ...", m_name, stateFile);
    final KieMarshallers kMarshallers = KieServices.Factory.get().getMarshallers();
    final ObjectMarshallingStrategy oms = serialize ? kMarshallers.newSerializeMarshallingStrategy() : kMarshallers.newIdentityMarshallingStrategy();
    final Marshaller marshaller = kMarshallers.newMarshaller(m_kieBase, new ObjectMarshallingStrategy[] { oms });
    try (FileOutputStream fos = new FileOutputStream(stateFile)) {
        m_kieSession.halt();
        marshaller.marshall(fos, m_kieSession);
        m_kieSession.dispose();
        m_kieSession.destroy();
        LOG.info("Sucessfully save state for engine {} in {}.", m_name, stateFile);
    } catch (IOException e) {
        LOG.error("Failed to save state for engine {} in {}.", m_name, stateFile, e);
    }
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) KieMarshallers(org.kie.api.marshalling.KieMarshallers)

Example 27 with ObjectMarshallingStrategy

use of org.kie.api.marshalling.ObjectMarshallingStrategy in project opennms by OpenNMS.

the class DroolsCorrelationEngine method unmarshallStateFromDisk.

private void unmarshallStateFromDisk(boolean serialize) {
    final File stateFile = getPathToState().toFile();
    if (!stateFile.exists()) {
        LOG.error("Can't restore state from {} because the file doesn't exist", stateFile);
        return;
    }
    LOG.debug("Restoring state for engine {} from {} ...", m_name, stateFile);
    final KieMarshallers kMarshallers = KieServices.Factory.get().getMarshallers();
    final ObjectMarshallingStrategy oms = serialize ? kMarshallers.newSerializeMarshallingStrategy() : kMarshallers.newIdentityMarshallingStrategy();
    final Marshaller marshaller = kMarshallers.newMarshaller(m_kieBase, new ObjectMarshallingStrategy[] { oms });
    try (FileInputStream fin = new FileInputStream(stateFile)) {
        marshaller.unmarshall(fin, m_kieSession);
        stateFile.delete();
        LOG.info("Sucessfully restored state for engine {} from {}.", m_name, stateFile);
    } catch (IOException | ClassNotFoundException e) {
        LOG.error("Failed to restore state for engine {} from {}.", m_name, stateFile, e);
    }
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) KieMarshallers(org.kie.api.marshalling.KieMarshallers)

Example 28 with ObjectMarshallingStrategy

use of org.kie.api.marshalling.ObjectMarshallingStrategy in project opennms by OpenNMS.

the class DroolsNorthbounder method marshallStateToDisk.

/**
 * Marshall state to disk.
 *
 * @param serialize the serialize
 */
private void marshallStateToDisk(boolean serialize) {
    final File stateFile = getPathToState().toFile();
    LOG.debug("Saving state for engine {} in {} ...", getName(), stateFile);
    final KieMarshallers kMarshallers = KieServices.Factory.get().getMarshallers();
    final ObjectMarshallingStrategy oms = serialize ? kMarshallers.newSerializeMarshallingStrategy() : kMarshallers.newIdentityMarshallingStrategy();
    final Marshaller marshaller = kMarshallers.newMarshaller(m_kieBase, new ObjectMarshallingStrategy[] { oms });
    try (FileOutputStream fos = new FileOutputStream(stateFile)) {
        m_kieSession.halt();
        marshaller.marshall(fos, m_kieSession);
        m_kieSession.dispose();
        m_kieSession.destroy();
        LOG.info("Sucessfully save state for engine {} in {}. There are {} elements on the working memory.", getName(), stateFile, m_kieSession.getObjects().size());
    } catch (IOException e) {
        LOG.error("Failed to save state for engine {} in {}.", getName(), stateFile, e);
    }
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) KieMarshallers(org.kie.api.marshalling.KieMarshallers)

Example 29 with ObjectMarshallingStrategy

use of org.kie.api.marshalling.ObjectMarshallingStrategy in project jbpm by kiegroup.

the class CaseFileInstanceMarshallingStrategy method unmarshal.

@SuppressWarnings("unchecked")
@Override
public Object unmarshal(Context context, ObjectInputStream is, byte[] object, ClassLoader classloader) throws IOException, ClassNotFoundException {
    logger.debug("About to read {} bytes to unmarshal CaseFileInstance", (object == null ? 0 : object.length));
    Map<String, Object> caseFileContent = (Map<String, Object>) caseFileMarshaller.unmarshal(context, is, object, classloader);
    CaseFileInstanceImpl caseFileInstance = new CaseFileInstanceImpl();
    caseFileInstance.setCaseId((String) caseFileContent.get(CASE_ID_KEY));
    caseFileInstance.setDefinitionId((String) caseFileContent.get(CASE_DEF_ID_KEY));
    caseFileInstance.setCaseStartDate((Date) caseFileContent.get(CASE_START_KEY));
    caseFileInstance.setCaseEndDate((Date) caseFileContent.get(CASE_END_KEY));
    caseFileInstance.setCaseReopenDate((Date) caseFileContent.get(CASE_REOPEN_KEY));
    caseFileInstance.setRolesAssignments((Map<String, CaseRoleInstance>) caseFileContent.get(CASE_ROLE_ASSIGNMENTS_KEY));
    caseFileInstance.setComments((List<CommentInstance>) caseFileContent.get(CASE_COMMENTS_KEY));
    logger.debug("CaseFileInstance meta data unmarshalled properly into {}", caseFileInstance);
    List<SerializedContent> caseDataContent = (List<SerializedContent>) caseFileContent.get(CASE_DATA_KEY);
    logger.debug("About to read serialized content {}", caseDataContent);
    for (SerializedContent serializedContent : caseDataContent) {
        ObjectMarshallingStrategy marshaller = marshallersByName.get(serializedContent.getMarshaller());
        logger.debug("Marshaller for {} is of type {}", serializedContent, marshaller);
        Object value = marshaller.unmarshal(context, is, serializedContent.getContent(), classloader);
        caseFileInstance.add(serializedContent.getName(), value);
        logger.debug("Data unmarshalled into {} and put into case file under '{}' name", value, serializedContent.getName());
    }
    caseFileInstance.setAccessRestrictions((Map<String, List<String>>) caseFileContent.get(CASE_DATA_RESTRICTIONS_KEY));
    caseFileInstance.setParentInstanceId((Long) caseFileContent.get(CASE_PARENT_INSTANCE_ID_KEY));
    caseFileInstance.setParentWorkItemId((Long) caseFileContent.get(CASE_PARENT_WORK_ITEM_ID_KEY));
    logger.debug("Unmarshal of CaseFileInstance completed - result {}", caseFileInstance);
    return caseFileInstance;
}
Also used : CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) ArrayList(java.util.ArrayList) List(java.util.List) CaseRoleInstance(org.jbpm.casemgmt.api.model.instance.CaseRoleInstance) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 30 with ObjectMarshallingStrategy

use of org.kie.api.marshalling.ObjectMarshallingStrategy in project jbpm by kiegroup.

the class MarshalVariablesProcessEventListener method afterProcessCompleted.

public void afterProcessCompleted(ProcessCompletedEvent event) {
    ObjectMarshallingStrategy[] strategies = (ObjectMarshallingStrategy[]) event.getKieRuntime().getEnvironment().get(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES);
    VariableScopeInstance variableScope = (VariableScopeInstance) ((WorkflowProcessInstance) event.getProcessInstance()).getContextInstance(VariableScope.VARIABLE_SCOPE);
    Map<String, Object> variables = variableScope.getVariables();
    for (Map.Entry<String, Object> variable : variables.entrySet()) {
        logger.debug("Searching for applicable strategy to handle variable name '{}' value '{}'", variable.getKey(), variable.getValue());
        for (ObjectMarshallingStrategy strategy : strategies) {
            // are removed together with process instance
            if (strategy instanceof SerializablePlaceholderResolverStrategy) {
                continue;
            }
            if (strategy.accept(variable.getValue())) {
                logger.debug("Strategy of type {} found to handle variable '{}'", strategy, variable.getKey());
                try {
                    ProcessMarshallerWriteContext context = new ProcessMarshallerWriteContext(new ByteArrayOutputStream(), null, null, null, null, event.getKieRuntime().getEnvironment());
                    context.setProcessInstanceId(event.getProcessInstance().getId());
                    context.setState(ProcessMarshallerWriteContext.STATE_COMPLETED);
                    strategy.marshal(null, context, variable.getValue());
                    logger.debug("Variable '{}' successfully persisted by strategy {}", variable.getKey(), strategy);
                    break;
                } catch (Exception e) {
                    logger.warn("Errer while storing process variable {} due to {}", variable.getKey(), e.getMessage());
                    logger.debug("Variable marshal error:", e);
                }
            }
        }
    }
}
Also used : SerializablePlaceholderResolverStrategy(org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Map(java.util.Map) ProcessMarshallerWriteContext(org.drools.core.marshalling.impl.ProcessMarshallerWriteContext)

Aggregations

ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)35 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)7 IOException (java.io.IOException)6 SerializablePlaceholderResolverStrategy (org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 File (java.io.File)4 Map (java.util.Map)4 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)4 EventFactHandle (org.drools.core.common.EventFactHandle)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)4 Marshaller (org.kie.api.marshalling.Marshaller)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ObjectInputStream (java.io.ObjectInputStream)3 QueryElementFactHandle (org.drools.core.common.QueryElementFactHandle)3 MarshallerReaderContext (org.drools.core.marshalling.impl.MarshallerReaderContext)3 MarshallingConfigurationImpl (org.drools.core.marshalling.impl.MarshallingConfigurationImpl)3 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)3 KieMarshallers (org.kie.api.marshalling.KieMarshallers)3