org.eclipse.xtext.resource.persistence.IResourceStorageFacade Java Examples

The following examples show how to use org.eclipse.xtext.resource.persistence.IResourceStorageFacade. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StandaloneBuilder.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void generate(List<Resource> sourceResources) {
	GeneratorContext context = new GeneratorContext();
	context.setCancelIndicator(CancelIndicator.NullImpl);
	for (Resource it : sourceResources) {
		LOG.info("Starting generator for input: '" + it.getURI().lastSegment() + "'");
		registerCurrentSource(it.getURI());
		LanguageAccess access = languageAccess(it.getURI());
		JavaIoFileSystemAccess fileSystemAccess = getFileSystemAccess(access);
		if (isWriteStorageResources()) {
			if (it instanceof StorageAwareResource) {
				IResourceStorageFacade resourceStorageFacade = ((StorageAwareResource) it)
						.getResourceStorageFacade();
				if (resourceStorageFacade != null) {
					resourceStorageFacade.saveResource((StorageAwareResource) it, fileSystemAccess);
				}
			}
		}
		access.getGenerator().generate(it, fileSystemAccess, context);
	}
}
 
Example #2
Source File: MonitoredClusteringBuilderState.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Stores the process resource as a binary if it doesn't contain syntax or linking errors.
 *
 * @param resource
 *          resource to store, must not be {@code null}
 * @param buildData
 *          build data, must not be {@code null}
 */
protected void storeBinaryResource(final Resource resource, final BuildData buildData) {
  if (isBinaryModelStorageAvailable && resource instanceof StorageAwareResource && ((StorageAwareResource) resource).getResourceStorageFacade() != null
      && fileSystemAccess instanceof IFileSystemAccessExtension3) {
    CompletableFuture.runAsync(() -> {
      final long maxTaskExecutionNanos = TimeUnit.NANOSECONDS.convert(1, TimeUnit.SECONDS);

      try {
        long elapsed = System.nanoTime();

        IResourceStorageFacade storageFacade = ((StorageAwareResource) resource).getResourceStorageFacade();
        storageFacade.saveResource((StorageAwareResource) resource, (IFileSystemAccessExtension3) fileSystemAccess);
        buildData.getSourceLevelURICache().getSources().remove(resource.getURI());

        elapsed = System.nanoTime() - elapsed;
        if (elapsed > maxTaskExecutionNanos) {
          LOGGER.info("saving binary taking longer than expected (" + elapsed + " ns) : " + resource.getURI()); //$NON-NLS-1$ //$NON-NLS-2$
        }
        // CHECKSTYLE:OFF
      } catch (Throwable ex) {
        // CHECKSTYLE:ON
        LOGGER.error("Failed to save binary for " + resource.getURI(), ex); //$NON-NLS-1$
      }
    }, binaryStorageExecutor);
  }
}
 
Example #3
Source File: BuilderParticipant.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.8
 */
protected void saveResourceStorage(Resource resource, IFileSystemAccess access) {
	if (resource instanceof StorageAwareResource && access instanceof IFileSystemAccessExtension3) {
		IResourceStorageFacade storageFacade = ((StorageAwareResource) resource).getResourceStorageFacade();
		if (storageFacade != null) {
			storageFacade.saveResource((StorageAwareResource)resource, (IFileSystemAccessExtension3)access);
		}
	}
}
 
Example #4
Source File: PortableURIsTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testPortableReferenceDescriptions() {
  try {
    final XtextResourceSet resourceSet = this.<XtextResourceSet>get(XtextResourceSet.class);
    Resource _createResource = resourceSet.createResource(URI.createURI("hubba:/bubba.langatestlanguage"));
    final StorageAwareResource resourceA = ((StorageAwareResource) _createResource);
    Resource _createResource_1 = resourceSet.createResource(URI.createURI("hubba:/bubba2.langatestlanguage"));
    final StorageAwareResource resourceB = ((StorageAwareResource) _createResource_1);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("type B");
    _builder.newLine();
    resourceB.load(this.getAsStream(_builder.toString()), null);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("import \'hubba:/bubba2.langatestlanguage\'");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("type A extends B");
    _builder_1.newLine();
    resourceA.load(this.getAsStream(_builder_1.toString()), null);
    final ByteArrayOutputStream bout = new ByteArrayOutputStream();
    final ResourceStorageWritable writable = resourceA.getResourceStorageFacade().createResourceStorageWritable(bout);
    writable.writeResource(resourceA);
    IResourceStorageFacade _resourceStorageFacade = resourceA.getResourceStorageFacade();
    byte[] _byteArray = bout.toByteArray();
    ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_byteArray);
    final ResourceStorageLoadable loadable = _resourceStorageFacade.createResourceStorageLoadable(_byteArrayInputStream);
    Resource _createResource_2 = resourceSet.createResource(URI.createURI("hubba:/bubba3.langatestlanguage"));
    final StorageAwareResource resourceC = ((StorageAwareResource) _createResource_2);
    resourceC.loadFromStorage(loadable);
    final IReferenceDescription refDesc = IterableExtensions.<IReferenceDescription>head(resourceC.getResourceDescription().getReferenceDescriptions());
    EObject _head = IterableExtensions.<EObject>head(resourceB.getContents());
    Assert.assertSame(IterableExtensions.<Type>head(((Main) _head).getTypes()), resourceSet.getEObject(refDesc.getTargetEObjectUri(), false));
    EObject _head_1 = IterableExtensions.<EObject>head(resourceC.getContents());
    Assert.assertSame(IterableExtensions.<Type>head(((Main) _head_1).getTypes()), resourceSet.getEObject(refDesc.getSourceEObjectUri(), false));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #5
Source File: MonitoredClusteringBuilderState.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Deletes the binary models for the given set of URIs.
 *
 * @param toBeDeleted
 *          set of URIs, must not be {@code null}
 */
protected void deleteBinaryResources(final Set<URI> toBeDeleted) {
  if (!isBinaryModelStorageAvailable || fileSystemAccess == null) {
    return;
  }
  for (URI uri : toBeDeleted) {
    IResourceStorageFacade resourceStorageFacade = resourceServiceProviderRegistry.getResourceServiceProvider(uri).get(IResourceStorageFacade.class);
    if (resourceStorageFacade instanceof DirectLinkingResourceStorageFacade) {
      ((DirectLinkingResourceStorageFacade) resourceStorageFacade).deleteStorage(uri, fileSystemAccess);
    }
  }
}
 
Example #6
Source File: XStatefulIncrementalBuilder.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/** Generate code for the given resource */
protected void generate(Resource resource, XSource2GeneratedMapping newMappings,
		IResourceServiceProvider serviceProvider) {
	GeneratorDelegate generator = serviceProvider.get(GeneratorDelegate.class);
	if (generator == null) {
		return;
	}

	if (isResourceInOutputDirectory(resource, serviceProvider)) {
		return;
	}

	URI source = resource.getURI();
	Set<URI> previous = newMappings.deleteSource(source);
	URIBasedFileSystemAccess fileSystemAccess = this.createFileSystemAccess(serviceProvider, resource);
	fileSystemAccess.setBeforeWrite((uri, outputCfgName, contents) -> {
		newMappings.addSource2Generated(source, uri, outputCfgName);
		previous.remove(uri);
		request.setResultGeneratedFile(source, uri);
		return contents;
	});
	fileSystemAccess.setBeforeDelete((uri) -> {
		newMappings.deleteGenerated(uri);
		request.setResultDeleteFile(uri);
		return true;
	});
	fileSystemAccess.setContext(resource);
	if (request.isWriteStorageResources() && resource instanceof StorageAwareResource) {
		IResourceStorageFacade resourceStorageFacade = ((StorageAwareResource) resource)
				.getResourceStorageFacade();
		if (resourceStorageFacade != null) {
			resourceStorageFacade.saveResource((StorageAwareResource) resource, fileSystemAccess);
		}
	}
	if (request.canGenerate()) {
		GeneratorContext generatorContext = new GeneratorContext();
		generatorContext.setCancelIndicator(request.getCancelIndicator());
		generator.generate(resource, fileSystemAccess, generatorContext);
		XtextResourceSet resourceSet = request.getResourceSet();
		for (URI noLongerCreated : previous) {
			try {
				resourceSet.getURIConverter().delete(noLongerCreated, CollectionLiterals.emptyMap());
				request.setResultDeleteFile(noLongerCreated);
			} catch (IOException e) {
				Exceptions.sneakyThrow(e);
			}
		}
	}
}
 
Example #7
Source File: BuilderTestLanguageRuntimeModule.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends IResourceStorageFacade> bindIResourceStorageFacade() {
	return ResourceStorageFacade.class;
}
 
Example #8
Source File: ResourceStorageTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends IResourceStorageFacade> bindIResourceStorageFacade() {
  return BatchLinkableResourceStorageFacade.class;
}
 
Example #9
Source File: DomainmodelRuntimeModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends IResourceStorageFacade> bindResourceStorageFacade() {
	return BatchLinkableResourceStorageFacade.class;
}
 
Example #10
Source File: BuilderTestLanguageRuntimeModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends IResourceStorageFacade> bindIResourceStorageFacade() {
	return ResourceStorageFacade.class;
}
 
Example #11
Source File: XtendRuntimeModule.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends IResourceStorageFacade> bindResourceStorageFacade() {
	return BatchLinkableResourceStorageFacade.class;
}
 
Example #12
Source File: IncrementalBuilder.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Generate code for the given resource
 */
protected void generate(Resource resource, BuildRequest request, Source2GeneratedMapping newMappings) {
	IResourceServiceProvider serviceProvider = getResourceServiceProvider(resource);
	GeneratorDelegate generator = serviceProvider.get(GeneratorDelegate.class);
	if (generator == null) {
		return;
	}
	Set<URI> previous = newMappings.deleteSource(resource.getURI());
	URIBasedFileSystemAccess fileSystemAccess = createFileSystemAccess(serviceProvider, resource);
	fileSystemAccess.setBeforeWrite((uri, outputCfgName, contents) -> {
		newMappings.addSource2Generated(resource.getURI(), uri, outputCfgName);
		previous.remove(uri);
		request.getAfterGenerateFile().apply(resource.getURI(), uri);
		return contents;
	});
	fileSystemAccess.setBeforeDelete((uri) -> {
		newMappings.deleteGenerated(uri);
		request.getAfterDeleteFile().apply(uri);
		return true;
	});
	fileSystemAccess.setContext(resource);
	if (request.isWriteStorageResources() && resource instanceof StorageAwareResource) {
		IResourceStorageFacade resourceStorageFacade = ((StorageAwareResource) resource)
				.getResourceStorageFacade();
		if (resourceStorageFacade != null) {
			resourceStorageFacade.saveResource((StorageAwareResource) resource, fileSystemAccess);
		}
	}
	GeneratorContext generatorContext = new GeneratorContext();
	generatorContext.setCancelIndicator(request.getCancelIndicator());
	generator.generate(resource, fileSystemAccess, generatorContext);
	XtextResourceSet resourceSet = request.getResourceSet();
	for (URI noLongerCreated : previous) {
		try {
			resourceSet.getURIConverter().delete(noLongerCreated, Collections.emptyMap());
			request.getAfterDeleteFile().apply(noLongerCreated);
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		}
	}
}
 
Example #13
Source File: LangATestLanguageRuntimeModule.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends IResourceStorageFacade> bindIResourceStorageFacade() {
	return ResourceStorageFacade.class;
}
 
Example #14
Source File: AbstractSARLRuntimeModule.java    From sarl with Apache License 2.0 4 votes vote down vote up
public Class<? extends IResourceStorageFacade> bindIResourceStorageFacade() {
	return BatchLinkableResourceStorageFacade.class;
}