protected KnowledgeAgent buildKnowledgeAgent(GuvnorConnectionUtils guvnorUtils) {
   try {
     ResourceChangeScannerConfiguration sconf =
         ResourceFactory.getResourceChangeScannerService().newResourceChangeScannerConfiguration();
     sconf.setProperty("drools.resource.scanner.interval", "10");
     ResourceFactory.getResourceChangeScannerService().configure(sconf);
     ResourceFactory.getResourceChangeScannerService().start();
     ResourceFactory.getResourceChangeNotifierService().start();
     KnowledgeAgentConfiguration aconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
     aconf.setProperty("drools.agent.newInstance", "false");
     KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent("Guvnor default", aconf);
     kagent.applyChangeSet(ResourceFactory.newReaderResource(guvnorUtils.createChangeSet()));
     return kagent;
   } catch (Throwable t) {
     logger.error("Could not load processes from Guvnor: " + t.getMessage(), t);
     return null;
   }
 }
  public synchronized void syncPackages() {
    try {
      GuvnorConnectionUtils guvnorUtils = new GuvnorConnectionUtils();
      if (guvnorUtils.guvnorExists()) {

        if (kagent == null) {
          kagent = buildKnowledgeAgent(guvnorUtils);
        }
        List<String> guvnorPackages = guvnorUtils.getBuiltPackageNames();

        guvnorPackages.removeAll(knownPackages);

        if (guvnorPackages.size() > 0 && kagent != null) {
          kagent.applyChangeSet(
              ResourceFactory.newReaderResource(guvnorUtils.createChangeSet(guvnorPackages)));
          knownPackages.addAll(guvnorPackages);
        }
      }
    } catch (Exception e) {
      logger.error("Error while checking packages from Guvnor", e);
    }
  }