示例#1
0
  private static String getPomPropertiesFromZipFile(String rootPath) {
    File actualZipFile = new File(rootPath);
    if (!actualZipFile.exists()) {
      log.error(
          "Unable to load pom.properties from"
              + rootPath
              + " as jarPath cannot be found\n"
              + rootPath);
      return null;
    }

    ZipFile zipFile = null;

    try {
      zipFile = new ZipFile(actualZipFile);

      String file = KieBuilderImpl.findPomProperties(zipFile);
      if (file == null) {
        log.warn("Unable to find pom.properties in " + rootPath);
        return null;
      }
      ZipEntry zipEntry = zipFile.getEntry(file);

      String pomProps =
          StringUtils.readFileAsString(
              new InputStreamReader(zipFile.getInputStream(zipEntry), IoUtils.UTF8_CHARSET));
      log.debug("Found and used pom.properties " + file);
      return pomProps;
    } catch (Exception e) {
      log.error("Unable to load pom.properties from " + rootPath + "\n" + e.getMessage());
    } finally {
      try {
        zipFile.close();
      } catch (IOException e) {
        log.error("Error when closing InputStream to " + rootPath + "\n" + e.getMessage());
      }
    }
    return null;
  }
  @Test
  public void testGlobal() throws Exception {
    Options xjcOpts = new Options();
    xjcOpts.setSchemaLanguage(Language.XMLSCHEMA);
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

    JaxbConfiguration jaxbConf = KnowledgeBuilderFactory.newJaxbConfiguration(xjcOpts, "xsd");

    kbuilder.add(
        ResourceFactory.newClassPathResource("order.xsd", getClass()), ResourceType.XSD, jaxbConf);

    assertFalse(kbuilder.hasErrors());

    kbuilder.add(
        ResourceFactory.newClassPathResource("test_Jaxb.drl", getClass()), ResourceType.DRL);

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    assertFalse(kbuilder.hasErrors());

    assertFalse(kbuilder.hasErrors());

    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

    KnowledgeRuntimeCommand setGlobalStage =
        PipelineFactory.newStatefulKnowledgeSessionSetGlobal("order");

    JAXBContext jaxbCtx =
        KnowledgeBuilderHelper.newJAXBContext(
            jaxbConf.getClasses().toArray(new String[jaxbConf.getClasses().size()]), kbase);

    Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
    Transformer transformer = PipelineFactory.newJaxbFromXmlTransformer(unmarshaller);
    transformer.setReceiver(setGlobalStage);

    Pipeline pipeline = PipelineFactory.newStatefulKnowledgeSessionPipeline(ksession);
    pipeline.setReceiver(transformer);

    String xml =
        StringUtils.readFileAsString(
            new InputStreamReader(getClass().getResourceAsStream("order.xml")));

    ResultHandlerImpl resultHandler = new ResultHandlerImpl();
    pipeline.insert(xml, resultHandler);

    // now round trip that global
    Action executeResult = PipelineFactory.newExecuteResultHandler();

    Action assignAsResult = PipelineFactory.newAssignObjectAsResult();
    assignAsResult.setReceiver(executeResult);

    // transformer = PipelineFactory.newXStreamToXmlTransformer( xstream );
    Marshaller marshaller = jaxbCtx.createMarshaller();
    transformer = PipelineFactory.newJaxbToXmlTransformer(marshaller);
    transformer.setReceiver(assignAsResult);

    KnowledgeRuntimeCommand getGlobalStage = PipelineFactory.newStatefulKnowledgeSessionGetGlobal();
    getGlobalStage.setReceiver(transformer);

    pipeline = PipelineFactory.newStatefulKnowledgeSessionPipeline(ksession);
    pipeline.setReceiver(getGlobalStage);

    resultHandler = new ResultHandlerImpl();
    pipeline.insert("order", resultHandler);

    assertEqualsIgnoreWhitespace(xml, (String) resultHandler.getObject());
  }