blob: 937355b0f78b690f9870021f7b038fca91679ebe [file] [log] [blame]
package org.eclipse.tigerstripe.workbench.internal.api.impl;
import java.io.File;
import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.tigerstripe.workbench.TigerstripeException;
import org.eclipse.tigerstripe.workbench.generation.IM1Generator;
import org.eclipse.tigerstripe.workbench.generation.IM1RunConfig;
import org.eclipse.tigerstripe.workbench.generation.IPluginRunStatus;
import org.eclipse.tigerstripe.workbench.generation.IRunConfig;
import org.eclipse.tigerstripe.workbench.internal.WorkingCopyManager;
import org.eclipse.tigerstripe.workbench.internal.api.ITigerstripeRuntime;
import org.eclipse.tigerstripe.workbench.internal.api.contract.segment.IFacetReference;
import org.eclipse.tigerstripe.workbench.internal.api.contract.useCase.IUseCaseReference;
import org.eclipse.tigerstripe.workbench.internal.contract.useCase.UseCaseReference;
import org.eclipse.tigerstripe.workbench.internal.core.generation.BaseM1Generator;
import org.eclipse.tigerstripe.workbench.internal.core.generation.M0RunConfig;
import org.eclipse.tigerstripe.workbench.internal.core.generation.M1RunConfig;
import org.eclipse.tigerstripe.workbench.internal.core.generation.RunConfig;
import org.eclipse.tigerstripe.workbench.internal.core.model.ArtifactManager;
import org.eclipse.tigerstripe.workbench.internal.core.project.ModelReference;
import org.eclipse.tigerstripe.workbench.internal.core.project.TigerstripeProject;
import org.eclipse.tigerstripe.workbench.model.deprecated_.IArtifactManagerSession;
import org.eclipse.tigerstripe.workbench.project.IDependency;
import org.eclipse.tigerstripe.workbench.project.IModelReference;
import org.eclipse.tigerstripe.workbench.project.IPluginConfig;
import org.eclipse.tigerstripe.workbench.project.IProjectDescriptor;
import org.eclipse.tigerstripe.workbench.project.IProjectDetails;
import org.eclipse.tigerstripe.workbench.project.ITigerstripeModelProject;
public class BaseTigerstripeModelProject extends AbstractTigerstripeProjectHandle implements ITigerstripeModelProject {
// Eventually this needs to implement the necessary parts of
// ITigerstripeModelProject
protected IProjectDescriptor tsProject;
private ArtifactManager manager;
protected IArtifactManagerSession artifactMgrSession;
protected ITigerstripeRuntime runtime;
private final Map<String, IModelReference> loadedReferencesMap = new HashMap<>();
public BaseTigerstripeModelProject(ITigerstripeRuntime runtime, URI projectContainerURI) {
super(projectContainerURI);
this.runtime = runtime;
}
@Override
public IRunConfig newGenerationConfig(int runType) throws TigerstripeException {
return RunConfig.newGenerationConfig(this, runType);
}
@Override
public void dispose() {
super.dispose();
tsProject = null;
if (artifactMgrSession != null) {
artifactMgrSession.dispose();
artifactMgrSession = null;
}
if (manager != null) {
manager.dispose();
manager = null;
}
loadedReferencesMap.clear();
runtime = null;
}
@Override
public synchronized IArtifactManagerSession getArtifactManagerSession() throws TigerstripeException {
if (artifactMgrSession == null) {
if (manager == null) {
manager = new BaseArtifactManagerImpl(runtime, getTSProject());
}
setArtifactManagerSession(new BaseArtifactManagerSessionImpl(manager));
}
return artifactMgrSession;
}
protected synchronized void setArtifactManagerSession(IArtifactManagerSession session) {
artifactMgrSession = session;
}
public synchronized IProjectDescriptor getTSProject() throws TigerstripeException {
if (tsProject == null) {
// try and create a project for the URI
File baseDir = new File(this.getProjectContainerURI());
if (baseDir.isDirectory()) {
tsProject = createProjectDescriptor(baseDir);
} else {
throw new TigerstripeException("Invalid project " + baseDir.toString());
}
}
tsProject.reload(false);
return this.tsProject;
}
protected IProjectDescriptor createProjectDescriptor(File baseDir) {
return new TigerstripeProject(runtime, baseDir, this);
}
public IProjectDetails getProjectDetails() throws TigerstripeException {
return getTSProject().getProjectDetails();
}
public String getModelId() throws TigerstripeException {
String modelId = getProjectDetails().getModelId();
if (modelId == null || modelId.isEmpty()) {
String name = super.getName();
if (name == null) {
return "";
}
return name;
}
return modelId;
}
@Override
public String getName() {
String name = super.getName();
if (name == null || name.isEmpty()) {
try {
return getProjectDetails().getModelId();
} catch (TigerstripeException e) {
getRuntime().logErrorMessage("Exception getting project Name", e);
}
}
return name;
}
@Override
public boolean exists() {
// check that a descriptor can be found and that it is valid
return findProjectDescriptor();
}
/**
* Tries and locate the project descriptor for this project
*
* @return
*/
@Override
protected boolean findProjectDescriptor() {
return new File(new File(getProjectContainerURI()), "tigerstripe.xml").exists();
}
protected IM1Generator getGenerator(ITigerstripeModelProject project, IM1RunConfig config) {
return new BaseM1Generator(this, config);
}
@Override
public IPluginRunStatus[] generate(IM1RunConfig config, IProgressMonitor monitor) throws TigerstripeException {
IM1Generator generator = getGenerator(this, config);
if (monitor == null)
return generator.run();
return generator.run(monitor);
}
@Override
public IPluginConfig[] getPluginConfigs() throws TigerstripeException {
return getTSProject().getPluginConfigs();
}
@Override
public IRunConfig makeRunConfig(int runType) throws TigerstripeException {
switch (runType) {
case IRunConfig.M0:
return new M0RunConfig(this);
case IRunConfig.M1:
return new M1RunConfig(this);
}
throw new IllegalArgumentException("Unknown generation config type (" + runType + ").");
}
// Advanced properties
@Override
public String getAdvancedProperty(String property) throws TigerstripeException {
return getTSProject().getAdvancedProperty(property);
}
@Override
public String getAdvancedProperty(String property, String defaultValue) throws TigerstripeException {
return getTSProject().getAdvancedProperty(property, defaultValue);
}
// Facets
@Override
public IFacetReference getActiveFacet() throws TigerstripeException {
return getArtifactManagerSession().getActiveFacet();
}
@Override
public void resetActiveFacet() throws TigerstripeException {
getArtifactManagerSession().resetActiveFacet();
}
@Override
public IFacetReference[] getFacetReferences() throws TigerstripeException {
return getTSProject().getFacetReferences();
}
@Override
public IUseCaseReference makeUseCaseReference(String projectRelativePath) throws TigerstripeException {
return new UseCaseReference(projectRelativePath, getTSProject());
}
@Override
public void addFacetReference(IFacetReference facetRef) throws TigerstripeException {
// TODO
}
@Override
public void setActiveFacet(IFacetReference facet, IProgressMonitor monitor) throws TigerstripeException {
getArtifactManagerSession().setActiveFacet(facet, monitor);
}
// Working copy - should not really ever be called
@Override
protected WorkingCopyManager doCreateCopy(IProgressMonitor monitor) throws TigerstripeException {
return new BaseTigerstripeModelProject(runtime, getURI());
}
@Override
protected void doCommit(IProgressMonitor monitor) throws TigerstripeException {
// Not supported here
}
@Override
public boolean wasDisposed() {
// Not supported here
return super.wasDisposed();
}
@Override
public boolean isDirty() {
// Not supported here
return false;
}
// References
@Override
public boolean hasReference(ITigerstripeModelProject project) throws TigerstripeException {
return getTSProject().hasReference(project);
}
@Override
public ITigerstripeModelProject[] getReferencedProjects() throws TigerstripeException {
return getTSProject().getReferencedProjects();
}
@Override
public ITigerstripeModelProject[] getEnabledReferencedProjects() throws TigerstripeException {
return getTSProject().getEnabledReferencedProjects();
}
@Override
public IModelReference[] getModelReferences() throws TigerstripeException {
return getTSProject().getModelReferences();
}
@Override
public IModelReference[] getEnabledModelReferences() throws TigerstripeException {
return getTSProject().getEnabledModelReferences();
}
@Override
public IModelReference[] getReferencingModels(int level) throws TigerstripeException {
return new IModelReference[0];
}
// Dependencies
@Override
public IDependency[] getDependencies() throws TigerstripeException {
return getTSProject().getDependencies();
}
@Override
public IDependency[] getEnabledDependencies() throws TigerstripeException {
return getTSProject().getEnabledDependencies();
}
@Override
public boolean hasDependency(IDependency dep) throws TigerstripeException {
if (dep == null || !dep.isValid())
throw new TigerstripeException("Invalid dependency");
IProjectDescriptor project = getTSProject();
return Arrays.asList(project.getDependencies()).contains(dep);
}
@Override
public ITigerstripeRuntime getRuntime() {
return runtime;
}
@Override
public final IModelReference makeReference(String modelId) {
synchronized (loadedReferencesMap) {
return loadedReferencesMap.computeIfAbsent(modelId, this::newRef);
}
}
public final ITigerstripeModelProject getReferenceAsProject(String modelId) {
IModelReference ref = null;
synchronized (loadedReferencesMap) {
ref = loadedReferencesMap.get(modelId);
}
return ref != null ? ref.getResolvedModel() : null;
}
protected IModelReference clearReference(String modelId) {
synchronized (loadedReferencesMap) {
return loadedReferencesMap.remove(modelId);
}
}
protected IModelReference newRef(String modelId) {
return new ModelReference(getRuntime(), this, modelId);
}
}