blob: 8c82c93554017d6b34b6193a10c338a81d16ae63 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2008 Cisco Systems, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Cisco Systems, Inc. - Initial Version
*******************************************************************************/
package org.eclipse.tigerstripe.workbench.internal.api.impl.pluggable;
import java.io.File;
import java.net.URI;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.tigerstripe.workbench.TigerstripeException;
import org.eclipse.tigerstripe.workbench.WorkingCopyException;
import org.eclipse.tigerstripe.workbench.internal.BasePlugin;
import org.eclipse.tigerstripe.workbench.internal.api.impl.AbstractTigerstripeProjectHandle;
import org.eclipse.tigerstripe.workbench.internal.core.project.ProjectDetails;
import org.eclipse.tigerstripe.workbench.internal.core.project.pluggable.GeneratorProjectDescriptor;
import org.eclipse.tigerstripe.workbench.internal.core.project.pluggable.runtime.PluginClasspathEntry;
import org.eclipse.tigerstripe.workbench.plugins.EPluggablePluginNature;
import org.eclipse.tigerstripe.workbench.plugins.IGlobalRule;
import org.eclipse.tigerstripe.workbench.plugins.IPluginClasspathEntry;
import org.eclipse.tigerstripe.workbench.plugins.IPluginProperty;
import org.eclipse.tigerstripe.workbench.plugins.IRule;
import org.eclipse.tigerstripe.workbench.plugins.LogLevel;
import org.eclipse.tigerstripe.workbench.project.IProjectDetails;
import org.eclipse.tigerstripe.workbench.project.ITigerstripeGeneratorProject;
public abstract class GeneratorProjectHandle extends
AbstractTigerstripeProjectHandle implements
ITigerstripeGeneratorProject {
public GeneratorProjectHandle(URI projectContainerURI) {
super(projectContainerURI);
}
/**
* This might be a deployed generator, in which case
* we can't use the simple case...
* it will be name - ver _ temp
*
*/
@Override
public String getName() {
String ver = "";
try{
ver = getProjectDetails().getVersion();
} catch (TigerstripeException t){
// Don't care
}
String tail = "-"+ver+"_temp";
String lastSeg = getLocation().lastSegment();
if (lastSeg.endsWith(tail)){
lastSeg = lastSeg.substring(0, lastSeg.length()-tail.length() );
}
if (lastSeg.startsWith(".")){
lastSeg = lastSeg.substring(1);
}
return lastSeg;
}
// // =========================================
// public String[] getRequiredAnnotationPlugins() throws TigerstripeException {
// List<String> list = getDescriptor().getRequiredAnnotationPlugins();
// return list.toArray(new String[list.size()]);
// }
@SuppressWarnings("unchecked")
public <T extends IPluginProperty> Class<T>[] getSupportedProperties() {
try {
return getDescriptor().getSupportedProperties();
} catch (TigerstripeException e) {
return new Class[0];
}
}
public String[] getSupportedPluginPropertyLabels() {
try {
return getDescriptor().getSupportedPluginPropertyLabels();
} catch (TigerstripeException e) {
return new String[0];
}
}
public <T extends IPluginProperty> IPluginProperty makeProperty(
Class<T> propertyType) throws TigerstripeException {
return getDescriptor().makeProperty(propertyType);
}
public void addGlobalProperties(IPluginProperty[] properties)
throws TigerstripeException {
for (IPluginProperty property : properties) {
addGlobalProperty(property);
}
}
public void addGlobalProperty(IPluginProperty property)
throws TigerstripeException {
assertSet();
getDescriptor().addGlobalProperty(property);
}
public void removeGlobalProperties(IPluginProperty[] properties)
throws TigerstripeException {
for (IPluginProperty property : properties) {
removeGlobalProperty(property);
}
}
public void removeGlobalProperty(IPluginProperty property)
throws TigerstripeException {
assertSet();
getDescriptor().removeGlobalProperty(property);
}
public void addClasspathEntry(IPluginClasspathEntry entry)
throws TigerstripeException {
assertSet();
getDescriptor().addClasspathEntry(entry);
}
public IPluginClasspathEntry[] getClasspathEntries()
throws TigerstripeException {
return getDescriptor().getClasspathEntries();
}
public IPluginClasspathEntry makeClasspathEntry() {
return new PluginClasspathEntry();
}
public void removeClasspathEntries(IPluginClasspathEntry[] entries)
throws TigerstripeException {
assertSet();
getDescriptor().removeClasspathEntries(entries);
}
public void removeClasspathEntry(IPluginClasspathEntry entry)
throws TigerstripeException {
assertSet();
getDescriptor().removeClasspathEntry(entry);
}
public IPluginProperty[] getGlobalProperties() throws TigerstripeException {
return getDescriptor().getGlobalProperties();
}
public void setGlobalProperties(IPluginProperty[] properties)
throws TigerstripeException {
assertSet();
getDescriptor().setGlobalProperties(properties);
}
// =========================================
public void addGlobalRule(IGlobalRule rule) throws TigerstripeException {
assertSet();
getDescriptor().addGlobalRule(rule);
}
public void addGlobalRules(IGlobalRule[] rules) throws TigerstripeException {
for (IGlobalRule rule : rules) {
addGlobalRule(rule);
}
}
public IGlobalRule[] getGlobalRules() throws TigerstripeException {
return getDescriptor().getGlobalRules();
}
public void removeGlobalRule(IRule rule) throws TigerstripeException {
assertSet();
getDescriptor().removeGlobalRule(rule);
}
public void removeGlobalRules(IRule[] rules) throws TigerstripeException {
assertSet();
getDescriptor().removeGlobalRules(rules);
}
@SuppressWarnings("unchecked")
public <T extends IGlobalRule> Class<T>[] getSupportedGlobalRules() {
try {
return getDescriptor().getSupportedGlobalRules();
} catch (TigerstripeException e) {
return new Class[0];
}
}
public String[] getSupportedGlobalRuleLabels() {
try {
return getDescriptor().getSupportedPluginRuleLabels();
} catch (TigerstripeException e) {
return new String[0];
}
}
public <T extends IRule> IRule makeRule(Class<T> ruleType)
throws TigerstripeException {
return getDescriptor().makeRule(ruleType);
}
protected abstract String getDescriptorFilename();
public IProjectDetails getProjectDetails() throws TigerstripeException {
GeneratorProjectDescriptor descriptor = getDescriptor();
return descriptor.getProjectDetails();
}
public void setProjectDetails(IProjectDetails projectDetails)
throws WorkingCopyException, TigerstripeException {
assertSet();
getDescriptor().setProjectDetails((ProjectDetails) projectDetails);
}
@Override
protected boolean findProjectDescriptor() {
return new File(new File(getProjectContainerURI()), getDescriptorFilename()).exists();
}
@Override
public boolean exists() {
boolean result = false;
// check that a descriptor can be found and that it is valid
if (findProjectDescriptor()) {
try {
getDescriptor();
result = true;
} catch (TigerstripeException e) {
result = false;
}
}
return result;
}
public abstract GeneratorProjectDescriptor getDescriptor()
throws TigerstripeException;
@Override
protected void doCommit(IProgressMonitor monitor)
throws TigerstripeException {
GeneratorProjectDescriptor descriptor = getDescriptor();
if (descriptor != null) {
descriptor.doSave(null);
try {
getIProject().refreshLocal(1, monitor);
} catch (CoreException e) {
throw new TigerstripeException("Error saving descriptor", e);
}
GeneratorProjectHandle original = (GeneratorProjectHandle) getOriginal();
original.getDescriptor().reload(true); // this will force a reload.
return;
}
throw new TigerstripeException("Invalid project, cannot save.");
}
public String[] getAdditionalFiles(int includeExclude)
throws TigerstripeException {
return getDescriptor().getAdditionalFiles(includeExclude).toArray(
new String[getDescriptor().getAdditionalFiles(includeExclude)
.size()]);
}
public void addAdditionalFile(String relativePath, int includeExclude)
throws TigerstripeException {
assertSet();
getDescriptor().addAdditionalFile(relativePath, includeExclude);
}
public void removeAdditionalFile(String relativePath, int includeExclude)
throws TigerstripeException {
assertSet();
getDescriptor().removeAdditionalFile(relativePath, includeExclude);
}
public LogLevel getDefaultLogLevel() throws TigerstripeException {
return getDescriptor().getDefaultLogLevel();
}
public String getLogPath() throws TigerstripeException {
return getDescriptor().getLogPath();
}
public boolean isLogEnabled() throws TigerstripeException {
return getDescriptor().isLogEnabled();
}
public void setDefaultLogLevel(LogLevel defaultLevel)
throws TigerstripeException {
assertSet();
getDescriptor().setDefaultLogLevel(defaultLevel);
}
public void setLogEnabled(boolean isLogEnabled) throws TigerstripeException {
assertSet();
getDescriptor().setLogEnabled(isLogEnabled);
}
public void setLogPath(String logPath) throws TigerstripeException {
assertSet();
getDescriptor().setLogPath(logPath);
}
public void setPluginNature(EPluggablePluginNature nature)
throws WorkingCopyException, TigerstripeException {
assertSet();
getDescriptor().setPluginNature(nature);
}
public EPluggablePluginNature getPluginNature() throws TigerstripeException {
return getDescriptor().getPluginNature();
}
@Override
public boolean isDirty() {
try {
return getDescriptor().isDirty();
} catch (TigerstripeException e) {
BasePlugin.log(e);
}
return false;
}
public String getId() throws TigerstripeException {
return getDescriptor().getId();
}
public boolean containsErrors() {
IProject project = (IProject) this.getAdapter(IProject.class);
try {
IMarker[] markers = project.findMarkers(IMarker.PROBLEM, true,
IResource.DEPTH_INFINITE);
for (int i = 0; i < markers.length; i++) {
if (IMarker.SEVERITY_ERROR == markers[i].getAttribute(
IMarker.SEVERITY, IMarker.SEVERITY_INFO)) {
return true;
}
}
} catch (CoreException e) {
BasePlugin.log(e);
}
return false;
}
public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter) {
if (adapter == IProject.class) {
try {
return getIProject();
} catch (TigerstripeException e) {
return null;
}
} else if (adapter == IJavaProject.class) {
try {
IProject project = getIProject();
// Note that this will be null for the PhantomProject
if (project != null) {
return JavaCore.create(project);
}
} catch (TigerstripeException e) {
return null;
}
}
return null;
}
private IProject getIProject()
throws TigerstripeException {
IContainer container = getIContainer();
if (container instanceof IProject) {
return (IProject) container;
}
/**
* Project also can be child folder in other project. In this case this
* container will be IFolder instead of IProject
*/
if (container != null) {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
for (IProject project : root.getProjects()) {
if (project.getLocation().equals(container.getLocation())) {
return project;
}
}
}
throw new TigerstripeException("Can't resolve " + this.getLocation()
+ " as Eclipse IProject");
}
private IContainer container;
// Introduced as a result of Bugzilla 319896
private IContainer getIContainer() {
if (container == null) {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
File file = new File(this.getLocation().toOSString());
IPath path = new Path(file.getAbsolutePath());
container = root.getContainerForLocation(path);
}
return container;
}
public void delete(final boolean force, IProgressMonitor monitor)
throws TigerstripeException {
if (monitor == null)
monitor = new NullProgressMonitor();
final IProject project = (IProject) getAdapter(IProject.class);
if (project != null) {
try {
ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor monitor)
throws CoreException {
project.delete(force, monitor);
}
}, project, IResource.NONE, monitor);
} catch (CoreException e) {
new TigerstripeException(
"An error occured while trying to delete project:"
+ e.getMessage(), e);
}
}
}
@Override
public IPath getFullPath() {
String name = getName();
if (name == null) {
return null;
}
IResource member = ResourcesPlugin.getWorkspace().getRoot()
.findMember(name);
if (member == null) {
return null;
}
return member.getFullPath();
}
@Override
public IPath getLocation() {
return getURI() == null ? null : new Path(new File(getURI()).getAbsolutePath());
}
}