package net.helipilot50.aerospike;
import java.util.ArrayList;
import java.util.List;
import net.helipilot50.aerospike.model.ClusterCoordinates;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
/**
* The activator class controls the plug-in life cycle
*/
public class Activator extends AbstractUIPlugin {
// The plug-in ID
public static final String PLUGIN_ID = "AerospikeInfo"; //$NON-NLS-1$
public static final String DB_PROJECT_NAME = "Aerospike Clusters";
// The shared instance
private static Activator plugin;
private IFolder dbFolder;
/**
* The constructor
*/
public Activator() {
}
/*
* (non-Javadoc)
* @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;
}
public static IProject getDBProject() {
IProject dbProject = ResourcesPlugin.getWorkspace().getRoot().getProject(DB_PROJECT_NAME);
if (!dbProject.exists()) {
try {
IProjectDescription desc = dbProject.getWorkspace().newProjectDescription(dbProject.getName());
dbProject.create(desc, new NullProgressMonitor());
if (!dbProject.isOpen()) {
dbProject.open(new NullProgressMonitor());
}
} catch (CoreException e) {
log(IStatus.ERROR, "Failed to create Cluster Project", e);
}
}
return dbProject;
}
public static List<ClusterCoordinates> getServerList(){
List<ClusterCoordinates> elements = new ArrayList<ClusterCoordinates>();
try {
IProject dbProject = getDBProject();
IResource[] members = dbProject.members();
for (IResource resource : members){
if (!(resource instanceof IFolder))
continue; //skip it if its not a folder
IFolder folder = (IFolder)resource;
IFile file = folder.getFile(ClusterCoordinates.FILE_NAME);
if (!file.exists())
continue; //skip it if the folder does not contain the coordinates file
ClusterCoordinates coordinates = new ClusterCoordinates(file.getContents());
elements.add(coordinates);
}
} catch (CoreException e) {
Activator.log(IStatus.ERROR, "Cluster List Content Provider error", e);
}
return elements;
}
/*
* (non-Javadoc)
* @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
}
/**
* Returns the shared instance
*
* @return the shared instance
*/
public static Activator getDefault() {
return plugin;
}
/**
* Returns an image descriptor for the image file at the given
* plug-in relative path
*
* @param path the path
* @return the image descriptor
*/
public static ImageDescriptor getImageDescriptor(String path) {
ImageDescriptor imageDesc = imageDescriptorFromPlugin(PLUGIN_ID, path);
return imageDesc;
}
/**
* Returns an Image for the image file at the given
* plug-in relative path
* @param iconString
* @return
*/
public static Image getImage(String iconString) {
ImageDescriptor img = getImageDescriptor(iconString);
if (img == null)
return null;
return img.createImage();
}
public static void log(int level, String message){
plugin.getLog().log(new Status(level, PLUGIN_ID, message, null));
}
public static void log(int level, String message, Throwable e){
plugin.getLog().log(new Status(level, PLUGIN_ID, message, e));
}
}