private void updatePackageBinaries(PackageItem item, PackageAssembler packageAssembler)
      throws DetailedSerializationException {
    try {
      ByteArrayOutputStream bout = new ByteArrayOutputStream();
      ObjectOutput out = new DroolsObjectOutputStream(bout);
      out.writeObject(packageAssembler.getBinaryPackage());

      item.updateCompiledPackage(new ByteArrayInputStream(bout.toByteArray()));
      out.flush();
      out.close();

      item.updateBinaryUpToDate(true);

      RuleBase ruleBase =
          RuleBaseFactory.newRuleBase(new RuleBaseConfiguration(getClassLoaders(packageAssembler)));
      ruleBase.addPackage(packageAssembler.getBinaryPackage());

      rulesRepository.save();
    } catch (Exception e) {
      e.printStackTrace();
      log.error(
          "An error occurred building the package [" + item.getName() + "]: " + e.getMessage());
      throw new DetailedSerializationException(
          "An error occurred building the package.", e.getMessage());
    }
  }
  private BuilderResult buildPackage(
      PackageItem item, boolean force, PackageAssemblerConfiguration packageAssemblerConfiguration)
      throws DetailedSerializationException {
    if (!force && item.isBinaryUpToDate()) {
      // we can just return all OK if its up to date.
      return BuilderResult.emptyResult();
    }
    PackageAssembler packageAssembler = new PackageAssembler(item, packageAssemblerConfiguration);

    packageAssembler.compile();

    if (packageAssembler.hasErrors()) {
      BuilderResult result = new BuilderResult();
      BuilderResultHelper builderResultHelper = new BuilderResultHelper();
      result.addLines(builderResultHelper.generateBuilderResults(packageAssembler.getErrors()));
      return result;
    }

    updatePackageBinaries(item, packageAssembler);

    return BuilderResult.emptyResult();
  }
 private ClassLoader[] getClassLoaders(PackageAssembler packageAssembler) {
   Collection<ClassLoader> loaders =
       packageAssembler.getBuilder().getRootClassLoader().getClassLoaders();
   return loaders.toArray(new ClassLoader[loaders.size()]);
 }