public static Object unmarshall(byte[] content, Environment env, ClassLoader classloader) {
    MarshallerReaderContext context = null;
    try {
      ByteArrayInputStream stream = new ByteArrayInputStream(content);
      MarshallingConfigurationImpl marshallingConfigurationImpl = null;
      if (env != null) {
        marshallingConfigurationImpl =
            new MarshallingConfigurationImpl(
                (ObjectMarshallingStrategy[])
                    env.get(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES),
                false,
                false);
      } else {
        marshallingConfigurationImpl =
            new MarshallingConfigurationImpl(
                new ObjectMarshallingStrategy[] {
                  new SerializablePlaceholderResolverStrategy(
                      ClassObjectMarshallingStrategyAcceptor.DEFAULT)
                },
                false,
                false);
      }
      ObjectMarshallingStrategyStore objectMarshallingStrategyStore =
          marshallingConfigurationImpl.getObjectMarshallingStrategyStore();
      context =
          new MarshallerReaderContext(
              stream, null, null, objectMarshallingStrategyStore, null, env);
      if (classloader != null) {
        context.classLoader = classloader;
      } else {
        context.classLoader = ContentMarshallerHelper.class.getClassLoader();
      }
      ExtensionRegistry registry = PersisterHelper.buildRegistry(context, null);
      Header _header = PersisterHelper.readFromStreamWithHeader(context, registry);
      Variable parseFrom = JBPMMessages.Variable.parseFrom(_header.getPayload(), registry);
      Object value = ProtobufProcessMarshaller.unmarshallVariableValue(context, parseFrom);

      if (value instanceof Map) {
        Map result = new HashMap();
        Map<String, Variable> variablesMap = (Map<String, Variable>) value;
        for (String key : variablesMap.keySet()) {
          result.put(
              key,
              ProtobufProcessMarshaller.unmarshallVariableValue(context, variablesMap.get(key)));
        }
        return result;
      }
      return value;
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }
Example #2
0
 @SuppressWarnings("unchecked")
 private QueryElementFactHandle createQueryResultHandle(
     PropagationContext context, InternalWorkingMemory workingMemory, Object[] objects) {
   QueryElementFactHandle handle = null;
   ProtobufMessages.FactHandle _handle = null;
   if (context.getReaderContext() != null) {
     Map<TupleKey, QueryElementContext> map =
         (Map<TupleKey, QueryElementContext>)
             context.getReaderContext().nodeMemories.get(node.getId());
     if (map != null) {
       QueryElementContext _context = map.get(PersisterHelper.createTupleKey(leftTuple));
       if (_context != null) {
         _handle = _context.results.removeFirst();
       }
     }
   }
   if (_handle != null) {
     // create a handle with the given id
     handle = new QueryElementFactHandle(objects, _handle.getId(), _handle.getRecency());
   } else {
     handle =
         new QueryElementFactHandle(
             objects,
             workingMemory.getFactHandleFactory().getAtomicId().incrementAndGet(),
             workingMemory.getFactHandleFactory().getAtomicRecency().incrementAndGet());
   }
   return handle;
 }
Example #3
0
 @SuppressWarnings("unchecked")
 private InternalFactHandle createFactHandle(
     final PropagationContext context,
     final InternalWorkingMemory workingMemory,
     final LeftTuple leftTuple) {
   InternalFactHandle handle = null;
   ProtobufMessages.FactHandle _handle = null;
   if (context.getReaderContext() != null) {
     Map<TupleKey, QueryElementContext> map =
         (Map<TupleKey, QueryElementContext>) context.getReaderContext().nodeMemories.get(getId());
     if (map != null) {
       _handle = map.get(PersisterHelper.createTupleKey(leftTuple)).handle;
     }
   }
   if (_handle != null) {
     // create a handle with the given id
     handle =
         workingMemory
             .getFactHandleFactory()
             .newFactHandle(
                 _handle.getId(), null, _handle.getRecency(), null, workingMemory, workingMemory);
   } else {
     handle =
         workingMemory
             .getFactHandleFactory()
             .newFactHandle(null, null, workingMemory, workingMemory);
   }
   return handle;
 }
  public static ContentData marshal(Object o, Environment env) {
    MarshallerWriteContext context = null;
    ContentData content = null;
    try {
      MarshallingConfigurationImpl marshallingConfigurationImpl = null;
      if (env != null) {
        marshallingConfigurationImpl =
            new MarshallingConfigurationImpl(
                (ObjectMarshallingStrategy[])
                    env.get(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES),
                false,
                false);
      } else {
        marshallingConfigurationImpl =
            new MarshallingConfigurationImpl(
                new ObjectMarshallingStrategy[] {
                  new SerializablePlaceholderResolverStrategy(
                      ClassObjectMarshallingStrategyAcceptor.DEFAULT)
                },
                false,
                false);
      }
      ObjectMarshallingStrategyStore objectMarshallingStrategyStore =
          marshallingConfigurationImpl.getObjectMarshallingStrategyStore();
      ByteArrayOutputStream stream = new ByteArrayOutputStream();

      context =
          new MarshallerWriteContext(stream, null, null, null, objectMarshallingStrategyStore, env);
      Variable marshallVariable = null;
      if (o instanceof Map) {
        marshallVariable =
            ProtobufProcessMarshaller.marshallVariablesMap(context, (Map<String, Object>) o);
      } else {
        marshallVariable = ProtobufProcessMarshaller.marshallVariable(context, "results", o);
      }
      PersisterHelper.writeToStreamWithHeader(context, marshallVariable);

      context.close();

      byte[] toByteArray = stream.toByteArray();

      content = new ContentData();
      content.setContent(toByteArray);
      content.setType(o.getClass().getCanonicalName());
      content.setAccessType(AccessType.Inline);

    } catch (IOException ex) {
      ex.printStackTrace();
    }
    return content;
  }
Example #5
0
 @SuppressWarnings("unchecked")
 private InternalFactHandle createResultFactHandle(
     final PropagationContext context,
     final InternalWorkingMemory workingMemory,
     final LeftTuple leftTuple,
     final Object result) {
   InternalFactHandle handle = null;
   ProtobufMessages.FactHandle _handle = null;
   if (context.getReaderContext() != null) {
     Map<ProtobufInputMarshaller.TupleKey, ProtobufMessages.FactHandle> map =
         (Map<ProtobufInputMarshaller.TupleKey, ProtobufMessages.FactHandle>)
             context.getReaderContext().nodeMemories.get(getId());
     if (map != null) {
       _handle = map.get(PersisterHelper.createTupleKey(leftTuple));
     }
   }
   if (_handle != null) {
     // create a handle with the given id
     handle =
         workingMemory
             .getFactHandleFactory()
             .newFactHandle(
                 _handle.getId(),
                 result,
                 _handle.getRecency(),
                 workingMemory
                     .getObjectTypeConfigurationRegistry()
                     .getObjectTypeConf(context.getEntryPoint(), result),
                 workingMemory,
                 null); // so far, result is not an event
   } else {
     handle =
         workingMemory
             .getFactHandleFactory()
             .newFactHandle(
                 result,
                 workingMemory
                     .getObjectTypeConfigurationRegistry()
                     .getObjectTypeConf(context.getEntryPoint(), result),
                 workingMemory,
                 null); // so far, result is not an event
   }
   return handle;
 }
  // Input methods
  public ProcessInstance readProcessInstance(MarshallerReaderContext context) throws IOException {
    InternalRuleBase ruleBase = context.ruleBase;
    InternalWorkingMemory wm = context.wm;

    JBPMMessages.ProcessInstance _instance =
        (org.jbpm.marshalling.impl.JBPMMessages.ProcessInstance) context.parameterObject;
    if (_instance == null) {
      // try to parse from the stream
      ExtensionRegistry registry = PersisterHelper.buildRegistry(context, null);
      Header _header;
      try {
        _header = PersisterHelper.readFromStreamWithHeader(context, registry);
      } catch (ClassNotFoundException e) {
        // Java 5 does not accept [new IOException(String, Throwable)]
        IOException ioe = new IOException("Error deserializing process instance.");
        ioe.initCause(e);
        throw ioe;
      }
      _instance = JBPMMessages.ProcessInstance.parseFrom(_header.getPayload(), registry);
    }

    WorkflowProcessInstanceImpl processInstance = createProcessInstance();
    processInstance.setId(_instance.getId());
    String processId = _instance.getProcessId();
    processInstance.setProcessId(processId);
    String processXml = _instance.getProcessXml();
    Process process = null;
    if (processXml != null && processXml.trim().length() > 0) {
      processInstance.setProcessXml(processXml);
      process = processInstance.getProcess();
    } else {
      process = ruleBase.getProcess(processId);
      processInstance.setProcess(process);
    }
    processInstance.setState(_instance.getState());
    long nodeInstanceCounter = _instance.getNodeInstanceCounter();
    processInstance.setKnowledgeRuntime(wm.getKnowledgeRuntime());

    if (_instance.getSwimlaneContextCount() > 0) {
      Context swimlaneContext =
          ((org.jbpm.process.core.Process) process)
              .getDefaultContext(SwimlaneContext.SWIMLANE_SCOPE);
      SwimlaneContextInstance swimlaneContextInstance =
          (SwimlaneContextInstance) processInstance.getContextInstance(swimlaneContext);
      for (JBPMMessages.ProcessInstance.SwimlaneContextInstance _swimlane :
          _instance.getSwimlaneContextList()) {
        swimlaneContextInstance.setActorId(_swimlane.getSwimlane(), _swimlane.getActorId());
      }
    }

    for (JBPMMessages.ProcessInstance.NodeInstance _node : _instance.getNodeInstanceList()) {
      context.parameterObject = _node;
      readNodeInstance(context, processInstance, processInstance);
    }

    for (JBPMMessages.ProcessInstance.ExclusiveGroupInstance _excl :
        _instance.getExclusiveGroupList()) {
      ExclusiveGroupInstance exclusiveGroupInstance = new ExclusiveGroupInstance();
      processInstance.addContextInstance(ExclusiveGroup.EXCLUSIVE_GROUP, exclusiveGroupInstance);
      for (Long nodeInstanceId : _excl.getGroupNodeInstanceIdList()) {
        NodeInstance nodeInstance = processInstance.getNodeInstance(nodeInstanceId);
        if (nodeInstance == null) {
          throw new IllegalArgumentException(
              "Could not find node instance when deserializing exclusive group instance: "
                  + nodeInstanceId);
        }
        exclusiveGroupInstance.addNodeInstance(nodeInstance);
      }
    }

    if (_instance.getVariableCount() > 0) {
      Context variableScope =
          ((org.jbpm.process.core.Process) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
      VariableScopeInstance variableScopeInstance =
          (VariableScopeInstance) processInstance.getContextInstance(variableScope);
      for (JBPMMessages.Variable _variable : _instance.getVariableList()) {
        try {
          Object _value = ProtobufProcessMarshaller.unmarshallVariableValue(context, _variable);
          variableScopeInstance.internalSetVariable(_variable.getName(), _value);
        } catch (ClassNotFoundException e) {
          throw new IllegalArgumentException("Could not reload variable " + _variable.getName());
        }
      }
    }
    processInstance.internalSetNodeInstanceCounter(nodeInstanceCounter);
    if (wm != null) {
      processInstance.reconnect();
    }
    return processInstance;
  }