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());
   }
 }
  @Test
  public void testCustomonnectorMigrationWithGetterCustom() throws Exception {
    final URL url = TestCustomConnectorMigrationUseCase.class.getResource("fab--1.0.bar");
    final File migratedProc = BarImporterTestUtil.migrateBar(url);
    assertNotNull("Fail to migrate bar file", migratedProc);
    assertNotNull("Fail to migrate bar file", migratedProc.exists());
    final Resource resource = BarImporterTestUtil.assertIsLoadable(migratedProc);
    final MainProcess mainProc = BarImporterTestUtil.getMainProcess(resource);

    final ConnectorDefRepositoryStore defStore =
        (ConnectorDefRepositoryStore)
            RepositoryManager.getInstance().getRepositoryStore(ConnectorDefRepositoryStore.class);
    assertNotNull(defStore.getDefinition("fabulous_connector", "1.0.0"));

    final ConnectorImplRepositoryStore implStore =
        (ConnectorImplRepositoryStore)
            RepositoryManager.getInstance().getRepositoryStore(ConnectorImplRepositoryStore.class);
    ConnectorImplementation implementation =
        implStore.getImplementation("fabulous_connector-impl", "1.0.0");
    assertNotNull(implementation);

    final ConnectorSourceRepositoryStore sourceStore =
        (ConnectorSourceRepositoryStore)
            RepositoryManager.getInstance()
                .getRepositoryStore(ConnectorSourceRepositoryStore.class);
    assertNotNull(sourceStore.getChild(implementation.getImplementationClassname()));

    final List<Connector> connectors =
        ModelHelper.getAllItemsOfType(mainProc, ProcessPackage.Literals.CONNECTOR);
    assertEquals("Invalid number of connector", 1, connectors.size());
  }
  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 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;
  }
 /* (non-Javadoc)
  * @see org.bonitasoft.studio.configuration.AbstractConnectorConfigurationSynchronizer#updateJarDependencies(org.bonitasoft.studio.model.configuration.FragmentContainer, org.bonitasoft.studio.connector.model.implementation.ConnectorImplementation, org.eclipse.emf.edit.domain.EditingDomain, org.eclipse.emf.common.command.CompoundCommand)
  */
 @Override
 protected void updateJarDependencies(
     FragmentContainer connectorContainer,
     ConnectorImplementation implementation,
     EditingDomain editingDomain,
     CompoundCommand cc,
     boolean forceDriver) {
   super.updateJarDependencies(connectorContainer, implementation, editingDomain, cc, forceDriver);
   store =
       (DatabaseConnectorPropertiesRepositoryStore)
           RepositoryManager.getInstance()
               .getRepositoryStore(DatabaseConnectorPropertiesRepositoryStore.class);
   DatabaseConnectorPropertiesFileStore fileStore =
       (DatabaseConnectorPropertiesFileStore)
           store.getChild(
               implementation.getDefinitionId()
                   + "."
                   + DatabaseConnectorPropertiesRepositoryStore.CONF_EXT);
   if (fileStore != null) {
     String defaultDriver = fileStore.getDefault();
     List<String> jars = fileStore.getJarList();
     boolean autoAddDriver = fileStore.getAutoAddDriver() || forceDriver;
     Configuration conf = (Configuration) connectorContainer.eContainer().eContainer();
     FragmentContainer otherDependencies = null;
     for (FragmentContainer c : conf.getProcessDependencies()) {
       if (FragmentTypes.OTHER.equals(c.getId())) {
         otherDependencies = c;
       }
     }
     for (String jar : jars) {
       boolean exists = false;
       for (Fragment dep : otherDependencies.getFragments()) {
         if (dep.getValue().equals(jar)) {
           exists = true;
           break;
         }
       }
       if (!exists) {
         Fragment depFragment = ConfigurationFactory.eINSTANCE.createFragment();
         if (jar.equals(defaultDriver) && autoAddDriver) {
           depFragment.setExported(true);
         } else {
           depFragment.setExported(false);
         }
         depFragment.setKey(jar);
         depFragment.setValue(jar);
         depFragment.setType(FragmentTypes.JAR);
         cc.append(
             AddCommand.create(
                 editingDomain,
                 otherDependencies,
                 ConfigurationPackage.Literals.FRAGMENT_CONTAINER__FRAGMENTS,
                 depFragment));
       }
     }
   }
 }
  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 IStatus addDependencies(ConnectorImplementation impl, IFolder classpathFolder)
      throws CoreException {
    final IDefinitionRepositoryStore store = (IDefinitionRepositoryStore) getDefinitionStore();
    final DependencyRepositoryStore depStore =
        (DependencyRepositoryStore)
            RepositoryManager.getInstance().getRepositoryStore(DependencyRepositoryStore.class);
    final DefinitionResourceProvider resourceProvider =
        DefinitionResourceProvider.getInstance(getDefinitionStore(), getBundle());

    ConnectorDefinition def =
        store.getDefinition(impl.getDefinitionId(), impl.getDefinitionVersion());
    for (String jarName : def.getJarDependency()) {
      if (ignoredLibs.contains(jarName)) {
        continue;
      }
      IRepositoryFileStore file = depStore.getChild(jarName);
      if (file != null) {
        if (file.getResource().exists()) {
          if (!classpathFolder.getFile(file.getName()).exists()) {
            try {
              file.getResource()
                  .copy(
                      classpathFolder.getFullPath().append(file.getName()),
                      true,
                      Repository.NULL_PROGRESS_MONITOR);
            } catch (CoreException e) {
              BonitaStudioLog.error(e);
            }
          }
        }
      } else { // Search in provided jars
        InputStream is = resourceProvider.getDependencyInputStream(jarName);
        if (is != null) {
          IFile jarFile = classpathFolder.getFile(jarName);
          if (!jarFile.exists()) {
            jarFile.create(is, true, Repository.NULL_PROGRESS_MONITOR);
          }
        } else {
          return ValidationStatus.error(Messages.bind(Messages.implementationDepNotFound, jarName));
        }
      }
    }
    for (String jarName : impl.getJarDependencies().getJarDependency()) {
      if (ignoredLibs.contains(jarName)) {
        continue;
      }
      IRepositoryFileStore file = depStore.getChild(jarName);
      if (file != null) {
        if (file.getResource().exists()) {
          if (!classpathFolder.getFile(file.getName()).exists()) {
            try {
              file.getResource()
                  .copy(
                      classpathFolder.getFullPath().append(file.getName()),
                      true,
                      Repository.NULL_PROGRESS_MONITOR);
            } catch (CoreException e) {
              BonitaStudioLog.error(e);
            }
          }
        }
      } else { // Search in provided jars
        InputStream is = resourceProvider.getDependencyInputStream(jarName);
        if (is != null) {
          IFile jarFile = classpathFolder.getFile(jarName);
          if (!jarFile.exists()) {
            jarFile.create(is, true, Repository.NULL_PROGRESS_MONITOR);
          }
        } else {
          return ValidationStatus.error(Messages.bind(Messages.implementationDepNotFound, jarName));
        }
      }
    }
    return Status.OK_STATUS;
  }