protected void addConnectorImplementation(
     ConnectorImplementation impl, List<IResource> resourcesToExport, boolean includeSources)
     throws FileNotFoundException, CoreException {
   final IRepositoryStore store = getImplementationStore();
   String fileName = NamingUtils.getEResourceFileName(impl, true);
   final EMFFileStore fileStore = (EMFFileStore) store.getChild(fileName);
   if (!fileStore.canBeShared()) {
     File f = new File(fileStore.getEMFResource().getURI().toFileString());
     if (f.exists()) {
       IFile implFile = store.getResource().getFile(f.getName());
       implFile.create(new FileInputStream(f), true, Repository.NULL_PROGRESS_MONITOR);
       resourcesToExport.add(implFile);
       cleanAfterExport.add(implFile);
     }
   } else {
     implBackup = EcoreUtil.copy(impl);
     String jarName =
         NamingUtils.toConnectorImplementationFilename(
                 impl.getImplementationId(), impl.getImplementationVersion(), false)
             + ".jar";
     if (!impl.getJarDependencies().getJarDependency().contains(jarName)) {
       impl.getJarDependencies().getJarDependency().add(jarName);
     }
     impl.setHasSources(includeSources);
     IRepositoryFileStore file = store.getChild(fileName);
     file.save(EcoreUtil.copy(impl));
     resourcesToExport.add(file.getResource());
   }
 }
  protected void addConnectorDefinition(
      ConnectorImplementation impl, List<IResource> resourcesToExport)
      throws FileNotFoundException, CoreException {
    final IRepositoryStore store = getDefinitionStore();
    ConnectorDefinition def =
        ((IDefinitionRepositoryStore) store)
            .getDefinition(impl.getDefinitionId(), impl.getDefinitionVersion());
    EMFFileStore file =
        (EMFFileStore) store.getChild(URI.decode(def.eResource().getURI().lastSegment()));

    if (file != null && !file.canBeShared()) {
      File f = new File(file.getEMFResource().getURI().toFileString());
      if (f.exists()) {
        IFile defFile = store.getResource().getFile(f.getName());
        defFile.create(new FileInputStream(f), true, Repository.NULL_PROGRESS_MONITOR);
        resourcesToExport.add(defFile);
        cleanAfterExport.add(defFile);
      }
    } else if (file != null) {
      resourcesToExport.add(file.getResource());
    }

    addDefinitionIcons(resourcesToExport, store, def);
    addDefinitionPropertiesFile(resourcesToExport, store, def);
  }
  protected void addArchiveDescriptor(
      IRepositoryStore<IRepositoryFileStore> sourceStore, List<IResource> resourcesToExport) {
    IFile descFile = sourceStore.getResource().getFile(DESCRIPTOR_FILE);
    if (descFile.exists()) {
      try {
        descFile.delete(true, Repository.NULL_PROGRESS_MONITOR);
      } catch (CoreException e) {
        BonitaStudioLog.error(e);
      }
    }

    final Properties properties = new Properties();
    properties.put(VERSION, ProductVersion.CURRENT_VERSION);
    properties.put(TYPE, getArchiveType());
    FileOutputStream out = null;
    try {
      out = new FileOutputStream(descFile.getLocation().toFile());
      properties.store(out, null);
      resourcesToExport.add(descFile);
      cleanAfterExport.add(descFile);
    } catch (Exception e) {
      BonitaStudioLog.error(e);
    } finally {
      if (out != null) {
        try {
          out.close();
        } catch (IOException e) {
          BonitaStudioLog.error(e);
        }
      }
      sourceStore.refresh();
    }
  }
  protected IStatus addImplementationJar(
      ConnectorImplementation implementation,
      IFolder classpathFolder,
      SourceRepositoryStore sourceStore,
      IRepositoryStore implStore,
      List<IResource> resourcesToExport)
      throws CoreException, InvocationTargetException, InterruptedException {
    final String connectorJarName =
        NamingUtils.toConnectorImplementationFilename(
                implementation.getImplementationId(),
                implementation.getImplementationVersion(),
                false)
            + ".jar";
    final IFile jarFile = classpathFolder.getFile(Path.fromOSString(connectorJarName));
    String qualifiedClassName = impl.getImplementationClassname();
    String packageName = "";
    if (qualifiedClassName.indexOf(".") != -1) {
      packageName = qualifiedClassName.substring(0, qualifiedClassName.lastIndexOf("."));
    }
    final PackageFileStore file = (PackageFileStore) sourceStore.getChild(packageName);
    if (file != null) {
      file.exportAsJar(jarFile.getLocation().toFile().getAbsolutePath(), false);
      if (includeSources) {
        IFolder srcFolder = implStore.getResource().getFolder(SRC_DIR);
        if (srcFolder.exists()) {
          srcFolder.delete(true, Repository.NULL_PROGRESS_MONITOR);
        }
        srcFolder.create(true, true, Repository.NULL_PROGRESS_MONITOR);
        cleanAfterExport.add(srcFolder);

        IPath path =
            file.getResource()
                .getFullPath()
                .makeRelativeTo(sourceStore.getResource().getFullPath());
        IFolder newFolder = srcFolder.getFolder(path);
        newFolder.getLocation().toFile().getParentFile().mkdirs();
        srcFolder.refreshLocal(IResource.DEPTH_INFINITE, Repository.NULL_PROGRESS_MONITOR);
        file.getResource().copy(newFolder.getFullPath(), true, Repository.NULL_PROGRESS_MONITOR);

        resourcesToExport.add(srcFolder);
      }
    }

    return Status.OK_STATUS;
  }
 protected void addDefinitionPropertiesFile(
     List<IResource> resourcesToExport, final IRepositoryStore store, ConnectorDefinition def) {
   final DefinitionResourceProvider messageProvider =
       DefinitionResourceProvider.getInstance(store, getBundle());
   List<File> propertiesFile = messageProvider.getExistingLocalesResource(def);
   for (File propertyFile : propertiesFile) {
     String newFilename = propertyFile.getName();
     try {
       IFile f = store.getResource().getFile(newFilename);
       if (!f.exists()) {
         FileInputStream fis = new FileInputStream(propertyFile);
         f.create(fis, true, Repository.NULL_PROGRESS_MONITOR);
         fis.close();
         cleanAfterExport.add(f);
       }
       if (!resourcesToExport.contains(f)) {
         resourcesToExport.add(f);
       }
     } catch (Exception e) {
       BonitaStudioLog.error(e);
     }
   }
 }
  public IStatus run(IProgressMonitor progressMonitor) {
    progressMonitor.beginTask(Messages.exporting, IProgressMonitor.UNKNOWN);
    IStatus status = Status.OK_STATUS;
    try {
      ignoredLibs.add(
          NamingUtils.toConnectorImplementationFilename(
                  impl.getImplementationId(), impl.getImplementationVersion(), false)
              + ".jar");
      cleanAfterExport = new ArrayList<IResource>();
      final SourceRepositoryStore sourceStore = getSourceStore();
      final IRepositoryStore implStore = getImplementationStore();

      List<IResource> resourcesToExport = new ArrayList<IResource>();
      IFolder classpathFolder = implStore.getResource().getFolder(CLASSPATH_DIR);
      if (classpathFolder.exists()) {
        classpathFolder.delete(true, Repository.NULL_PROGRESS_MONITOR);
      }

      classpathFolder.create(true, true, Repository.NULL_PROGRESS_MONITOR);
      resourcesToExport.add(classpathFolder);
      cleanAfterExport.add(classpathFolder);

      status =
          addImplementationJar(impl, classpathFolder, sourceStore, implStore, resourcesToExport);
      if (status.getSeverity() != IStatus.OK) {
        return status;
      }
      addArchiveDescriptor(sourceStore, resourcesToExport);

      if (addDependencies) {
        status = addDependencies(impl, classpathFolder);
        if (status.getSeverity() != IStatus.OK) {
          return status;
        }
      }

      addConnectorImplementation(impl, resourcesToExport, includeSources);
      addConnectorDefinition(impl, resourcesToExport);

      final ArchiveFileExportOperation operation =
          new ArchiveFileExportOperation(null, resourcesToExport, destPath);
      operation.setUseCompression(true);
      operation.setUseTarFormat(false);
      operation.setCreateLeadupStructure(false);
      operation.run(progressMonitor);
      if (!operation.getStatus().isOK()) {
        return operation.getStatus();
      }
    } catch (CoreException e) {
      BonitaStudioLog.error(e);
      return ValidationStatus.error(e.getMessage(), e);
    } catch (InvocationTargetException e) {
      BonitaStudioLog.error(e);
      return ValidationStatus.error(e.getMessage(), e);
    } catch (InterruptedException e) {
      BonitaStudioLog.error(e);
      return ValidationStatus.error(e.getMessage(), e);
    } catch (FileNotFoundException e) {
      BonitaStudioLog.error(e);
      return ValidationStatus.error(e.getMessage(), e);
    } finally {
      if (implBackup != null) {
        final IRepositoryStore store = getImplementationStore();
        String fileName = NamingUtils.getEResourceFileName(implBackup, true);
        if (fileName == null) {
          fileName =
              NamingUtils.toConnectorImplementationFilename(
                  implBackup.getImplementationId(), implBackup.getImplementationVersion(), true);
        }
        IRepositoryFileStore implFile = store.getChild(fileName);
        if (implFile != null) {
          implFile.save(implBackup);
        } else {
          return ValidationStatus.error(fileName + " not found in repository");
        }
      }

      for (IResource r : cleanAfterExport) {
        if (r.exists()) {
          try {
            r.delete(true, Repository.NULL_PROGRESS_MONITOR);
          } catch (CoreException e) {
            BonitaStudioLog.error(e);
          }
        }
      }
    }

    return status;
  }
  protected void addDefinitionIcons(
      List<IResource> resourcesToExport, final IRepositoryStore store, ConnectorDefinition def) {
    if (def.getIcon() != null) {
      IFile iconFile = store.getResource().getFile(Path.fromOSString(def.getIcon()));
      if (iconFile != null && iconFile.exists()) {
        resourcesToExport.add(iconFile);
      } else {
        URL url =
            ConnectorPlugin.getDefault()
                .getBundle()
                .getResource(ConnectorDefRepositoryStore.STORE_NAME + "/" + def.getIcon());
        if (url != null) {
          try {
            IFile f = store.getResource().getFile(def.getIcon());
            if (!f.exists()) {
              InputStream is = url.openStream();
              f.create(is, true, Repository.NULL_PROGRESS_MONITOR);
              if (!resourcesToExport.contains(f)) {
                resourcesToExport.add(f);
              }
              cleanAfterExport.add(f);
              is.close();
            }

          } catch (Exception e) {
            BonitaStudioLog.error(e);
          }
        }
      }
    }

    for (Category c : def.getCategory()) {
      if (c.getIcon() != null) {
        IFile iconFile = store.getResource().getFile(Path.fromOSString(c.getIcon()));
        if (iconFile != null && iconFile.exists()) {
          if (!resourcesToExport.contains(iconFile)) {
            resourcesToExport.add(iconFile);
          }
        } else {
          URL url =
              ConnectorPlugin.getDefault()
                  .getBundle()
                  .getResource(ConnectorDefRepositoryStore.STORE_NAME + "/" + c.getIcon());
          if (url != null) {
            try {
              IFile f = store.getResource().getFile(c.getIcon());
              if (!f.exists()) {
                InputStream is = url.openStream();
                f.create(is, true, Repository.NULL_PROGRESS_MONITOR);
                if (!resourcesToExport.contains(f)) {
                  resourcesToExport.add(f);
                }
                cleanAfterExport.add(f);
                is.close();
              }
            } catch (Exception e) {
              BonitaStudioLog.error(e);
            }
          }
        }
      }
    }
  }