use of org.eclipse.wst.sse.core.internal.provisional.exceptions.ResourceInUse in project webtools.sourceediting by eclipse.
the class ModelManagerImpl method copyModelForEdit.
/**
*/
public IStructuredModel copyModelForEdit(String oldId, String newId) throws ResourceInUse {
IStructuredModel newModel = null;
// get the existing model associated with this id
IStructuredModel model = getExistingModel(oldId);
// be a programming error.
if (model == null)
return null;
SharedObject sharedObject = null;
try {
SYNC.acquire();
// now be sure newModel does not exist
sharedObject = (SharedObject) fManagedObjects.get(newId);
if (sharedObject != null) {
throw new ResourceInUse();
}
sharedObject = new SharedObject(newId);
fManagedObjects.put(newId, sharedObject);
} finally {
SYNC.release();
}
// ask the loader to copy
synchronized (sharedObject) {
sharedObject.doWait = false;
newModel = copy(model, newId);
sharedObject.doWait = true;
}
if (newModel != null) {
// add to our cache
synchronized (sharedObject) {
sharedObject.theSharedModel = newModel;
sharedObject.referenceCountForEdit = 1;
// $NON-NLS-1$
trace("copied model", newId, sharedObject.referenceCountForEdit);
}
} else {
SYNC.acquire();
fManagedObjects.remove(newId);
SYNC.release();
}
sharedObject.setLoaded();
return newModel;
}
use of org.eclipse.wst.sse.core.internal.provisional.exceptions.ResourceInUse in project webtools.sourceediting by eclipse.
the class ModelManagerImpl method _doCommonCreateModel.
private void _doCommonCreateModel(IFile file, String id, IModelHandler handler, URIResolver resolver, ReadEditType rwType, EncodingRule encodingRule, SharedObject sharedObject) throws CoreException, IOException {
// XXX: Does not integrate with FileBuffers
boolean doRemove = true;
try {
synchronized (sharedObject) {
InputStream inputStream = null;
IStructuredModel model = null;
try {
model = _commonCreateModel(id, handler, resolver);
IModelLoader loader = handler.getModelLoader();
inputStream = Utilities.getMarkSupportedStream(file.getContents(true));
loader.load(Utilities.getMarkSupportedStream(inputStream), model, encodingRule);
} catch (ResourceInUse e) {
// impossible, since we've already found
handleProgramError(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
}
}
}
if (model != null) {
// add to our cache
sharedObject.theSharedModel = model;
_initCount(sharedObject, rwType);
doRemove = false;
}
}
} finally {
if (doRemove) {
SYNC.acquire();
fManagedObjects.remove(id);
SYNC.release();
}
sharedObject.setLoaded();
}
}
use of org.eclipse.wst.sse.core.internal.provisional.exceptions.ResourceInUse in project webtools.sourceediting by eclipse.
the class ModelManagerImpl method _doCommonCreateModel.
private void _doCommonCreateModel(InputStream inputStream, String id, IModelHandler handler, URIResolver resolver, ReadEditType rwType, String encoding, String lineDelimiter, SharedObject sharedObject) throws IOException {
boolean doRemove = true;
try {
synchronized (sharedObject) {
IStructuredModel model = null;
try {
model = _commonCreateModel(id, handler, resolver);
IModelLoader loader = handler.getModelLoader();
if (inputStream == null) {
// $NON-NLS-1$ //$NON-NLS-2$
Logger.log(Logger.WARNING, "model was requested for id " + id + " without a content InputStream");
}
loader.load(id, Utilities.getMarkSupportedStream(inputStream), model, encoding, lineDelimiter);
} catch (ResourceInUse e) {
// impossible, since we've already found
handleProgramError(e);
}
if (model != null) {
/**
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=264228
*
* Ensure that the content type identifier field of the model
* is properly set. This is normally handled by the
* FileBufferModelManager when working with files as it knows
* the content type in advance; here is where we handle it for
* streams.
*/
if (model instanceof AbstractStructuredModel) {
DocumentReader reader = new DocumentReader(model.getStructuredDocument());
IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(reader, id, new QualifiedName[0]);
reader.close();
if (description != null && description.getContentType() != null) {
((AbstractStructuredModel) model).setContentTypeIdentifier(description.getContentType().getId());
}
}
sharedObject.theSharedModel = model;
_initCount(sharedObject, rwType);
doRemove = false;
}
}
} finally {
if (doRemove) {
SYNC.acquire();
// remove it if we didn't get one back
fManagedObjects.remove(id);
SYNC.release();
}
sharedObject.setLoaded();
}
}
use of org.eclipse.wst.sse.core.internal.provisional.exceptions.ResourceInUse in project webtools.sourceediting by eclipse.
the class ModelManagerImpl method createUnManagedStructuredModelFor.
/**
* Conveience method. It depends on the loaders newModel method to return
* an appropriate StrucuturedModel appropriately initialized.
*/
public IStructuredModel createUnManagedStructuredModelFor(String contentTypeId, URIResolver resolver) {
IStructuredModel result = null;
ModelHandlerRegistry cr = getModelHandlerRegistry();
IModelHandler handler = cr.getHandlerForContentTypeId(contentTypeId);
try {
// $NON-NLS-1$
result = _commonCreateModel(UNMANAGED_MODEL, handler, resolver);
} catch (ResourceInUse e) {
// this may need to be re-examined.
if (Logger.DEBUG_MODELMANAGER)
// $NON-NLS-1$ //$NON-NLS-2$
Logger.log(Logger.INFO, "ModelMangerImpl::createUnManagedStructuredModelFor. Model unexpectedly in use.");
}
return result;
}
use of org.eclipse.wst.sse.core.internal.provisional.exceptions.ResourceInUse in project webtools.sourceediting by eclipse.
the class AbstractStructuredModel method copy.
/**
* Provides a copy of the model, but a new ID must be provided. The
* principle of this copy is not to copy fields, etc., as is typically
* done in a clone method, but to return a model with the same content in
* the structuredDocument. Note: It is the callers responsibility to
* setBaseLocation, listners, etc., as appropriate. Type and Encoding are
* the only fields set by this method. If the newId provided already exist
* in the model manager, a ResourceInUse exception is thrown.
*/
public IStructuredModel copy(String newId) throws ResourceInUse {
IStructuredModel newModel = null;
// this first one should fail, if not, its treated as an error
// If the caller wants to use an existing one, they can call
// getExisting
// after this failure
newModel = getModelManager().getExistingModelForEdit(newId);
if (newModel != null) {
// be sure to release the reference we got "by accident" (and
// no
// longer need)
newModel.releaseFromEdit();
throw new ResourceInUse();
}
newModel = getModelManager().copyModelForEdit(getId(), newId);
return newModel;
}
Aggregations