eclipse resource management. outline introduction basic resource management further resource...

63
Eclipse resource management

Post on 19-Dec-2015

224 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Eclipse resource management

Page 2: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Outline

Introduction Basic resource management Further resource management

Page 3: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Introduction

Package org.eclipse.core.resources Define the notions of workspaces and

resources Provides basic support for managing a

workspace and its resources for resource plug-in

Resource retrieve, create, delete, move ..Etc.

Tracking resource lifecycle changes Similar to a file system

Page 4: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Basic resource management

Resource and the workspace Resource and the local file system Resource properties

Page 5: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Resource and the workspace

Workspace : central hub of user’s file Plug-in use resource API to create,

navigate, manipulate resource in the workspace

Three kinds of resource Project Folder file

Page 6: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Resources (I) Project

Collection of folders and files Organize resources related to specific

projects Interface IProject

Folder Contains other folder and files Like directory in file system Interface IFolder

Page 7: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Resource (II)

File Arbitrary sequence of bytes Interface IFile

Interface Iworkspace Interface IResource

Page 8: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Resource organization

Tree structure Project at top Folder and file

underneath Workspace root

Page 9: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Resource and file system

When resource plug-in is activate, the workspace is represented by an instance of IWorkspace

The IWorkspace instance represent all files and directories in file system

Get the IWorkspace instance : ResourcePlugin.getWorkspace();

Page 10: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Manipulate resource

First get the IWorkspaceRoot instance, which represents the root of the resource tree in the workspace

Access projects in the workspace Access folders and files in projects Access files in folders Similar to java.io.File

Page 11: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Get and open project

IWorkspaceRoot myWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();

IProject myWebProject = myWorkspaceRoot.getProject("MyWeb");

// open if necessary if (myWebProject.exists() && !

myWebProject.isOpen()) myWebProject.open(null);

Page 12: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Get folder, create fileIFolder imagesFolder =

myWebProject.getFolder("images");if (imagesFolder.exists()) {

// create a new file IFile newLogo = imagesFolder.getFile("newLogo.gif"); FileInputStream fileStream = new FileInputStream( "c:/MyOtherData/newLogo.gif"); newLogo.create(fileStream, false, null); // create closes the file stream, so no worries. }

Page 13: Eclipse resource management. Outline Introduction Basic resource management Further resource management

File copy

IFile logo = imagesFolder.getFile("logo.gif");

if (logo.exists()) { IPath newLogoPath = new Path("newLogo.gif"); logo.copy(newLogoPath, false, null); IFile newLogo = imagesFolder.getFile("newLogo.gif"); ... }

Page 14: Eclipse resource management. Outline Introduction Basic resource management Further resource management

File moveIFolder newImagesFolder =

myWebProject.getFolder("newimages");newImagesFolder.create(false, true, null); IPath renamedPath =

newImagesFolder.getFullPath().append("renamedLogo.gif");

newLogo.move(renamedPath, false, null); IFile renamedLogo =

newImagesFolder.getFile("renamedLogo.gif");

Page 15: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Resource and real file location

Use IResource.getLocation() to get the full system path of a resource

Use IProjectDescription.setLocation() to change a project location

Get a resource via a file system pathIWorkspaceRoot.getFileForLocation()IWorkspaceRoot.getContainerForLocati

on()

Page 16: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Resource properties Session properties

Cache information in key-value pair in memory

Lost when a resource is deleted or the project or workspace is closed

Persistent properties Store resource-specific information on disk Store with system metadata and

maintained across platform shutdown and restart

Page 17: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Used Interface and class Class ResourcePlugin

The plug-in run-time class The start point for resource manipulation

Interface IWorkspace Represent the workspace in the platform

Interface IResource The resource in the workspace Superinterface of IFile, IFolder, IProject,

IWorkspaceRoot

Page 18: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Used interface and class

Interface IFile, IFolder, IProject, IWorkspaceRoot System resource instance Provide manipulation methods of resources

Interface IProjectDescription Contain the metadata require to define a

project Contain the information in file .project with

all projects

Page 19: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Further resource management

Resource markers Tracking resource changes Incremental project builders Workspace save participation Project natures Derived resource Resource modification hooks

Page 20: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Marker

a general mechanism for associating notes and meta-data with resources.

Like a small tag to a resource Record information about a problem or a

task, or simply record a location as bookmark

User can jump to the marked location within a resource

Platform defined five standard markers (marker, taskmarker, problemmark,bookmark,textmark)

Page 21: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Properties of a marker

type : String id : long // unique in its resource + additional attributes depending on

specific type of the parker.

Page 22: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Predefined Marker types org.eclipse.core.resource.marker

(IMarker.MARKER) o.e.c.r.taskmarker (IMarker.TASK) o.e.c.r.problemmarker (IMarker.PROBLEM) o.e.c.r.bookmark (IMarker.BOOKMARK) o.e.c.r.textmarker(IMarker.TEXT)

additional markers can be defined by using the extention point: o.e.c.r.markers

Page 23: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Marker type declaration id=“type”

is the unique name (type) of the marker type <super type=“superType” />*

super types of this marker types. <attribute name=“attName” />*

attributes which may be present on this type of markers

<persistent value=“false”/>? whether markers of this type should be

persisted by the platform Instances can be transient if TRANSIENT attr is

set to true.

Page 24: Eclipse resource management. Outline Introduction Basic resource management Further resource management

<extension id="problemmarker“ point="org.eclipse.core.resources.markers“ name="%problemName"> <super type="org.eclipse.core.resources.marker"/> <persistent value="true"/> <attribute name="severity"/> <attribute name="message"/> <attribute name="location"/> </extension>

<extension id="taskmarker" point="org.eclipse.core.resources.markers“ name="%taskName"> <super type="org.eclipse.core.resources.marker"/> <persistent value="true"/> <attribute name="priority"/> <attribute name="message"/> <attribute name="done"/> <attribute name="userEditable"/> </extension>

Page 25: Eclipse resource management. Outline Introduction Basic resource management Further resource management

IMarker.BOOKMARK<extension id="bookmark"

point="org.eclipse.core.resources.markers" name="%bookmarkName“ >

<super type="org.eclipse.core.resources.marker"/> <persistent value="true"/> <attribute name="message"/> <attribute name="location"/> </extension> <extension id=“marker” point = .. name= ..> <persistent type = “true”/> <attribute name=“transient”/></extension>

Page 26: Eclipse resource management. Outline Introduction Basic resource management Further resource management

the text marker

TextMarker (IMarker.TEXT) IMarker.CHAR_START : int // (0-

based) IMarker.CHAR_END : int // exclusive IMarker.LINE_NUMBER:int // 1-based IMARK.TRANSIENT: boolen // inherited used with other markers in editor

framework.

Page 27: Eclipse resource management. Outline Introduction Basic resource management Further resource management

the Bookmark and taskmarker IMareker.BOOKMARK

IMarker.MESSAGE :String IMarker.LOCATION:String

IMarker.TASK IMarker.PRIORITY: int

IMarker.PRIORITY_HIGH, _NORMAL, _LOW IMarker.MESSAGE IMarker.DONE :boolean // completed

Page 28: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Problem Marker (o.e.c.rproblemmarker)

IMarker.PROBLEM IMarker.SEVERITY :int

IMarker.SEVERITY_ERROR, _WARNING, _INFO

(2) (1) (0) IMarker.MESSAGE:String IMarker.LOCATION :String

Page 29: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Resource API defines methods for creating, setting value, extending the platform with new marker type

Platform manage markers, it will throw away markers attached to resource that are deleted

Plug-in control marker’s creation, removal and attribute value. It also removes markers that no longer apply to a resource

Interface IMarker

Page 30: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Marker operationIResource.createMarker(ty

pe) Marker creation

Using factory method IResource.createMarker() Ex: IMarker marker =

file.createMarker(IMarker.TASK); // file is now the resource of marker.

Marker deletion Ex:try { marker.delete(); } catch (CoreException e) { // Something went

wrong }

Page 31: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Marker attributes access

<T> getAttribute(String name, T defValue ) possible <T>: boolean, int, String.

setAttribute(String name, <T> value) possible <T> : String, int, boolean,

Page 32: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Batch delete int depth = IResource.DEPTH_INFINITE; // others: _ZERO, _ONE

try { resource.deleteMarkers(IMarker.PROBLEM, true/*subType*/,

depth);} catch (CoreException e) { // something went wrong }

Attribute settingif (marker.exists()) { try { marker.setAttribute(IMarker.MESSAGE, "A sample marker

message"); marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH); } catch (CoreException e) { // You need to handle the case where the marker no longer

exists } }

Page 33: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Querying markers

Resource can be queried for their markers and the markers of their children

IMarker[] problems = null; int depth = IResource.DEPTH_INFINITE; boolean subtype = true ; try { problems = resource.findMarkers(IMarker.PROBLEM,

subtype, depth);

} catch (CoreException e) { // something went wrong }

Page 34: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Extend the platform with new marker type

New marker type are derived from existing ones using multiple inheritance, it will inherit all of the attribute from

supertypes

Plug-ins must declare new marker type in plugin.xml before using it Persistent Markers saved when workspace

is saved.

Page 35: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Marker extension

<extension id="mymarker" <-- marker type --> point="org.eclipse.core.resources.markers" />

<extension id="myproblem" point="org.eclipse.core.resources.markers">

<super type="org.eclipse.core.resources.problemmarker"/>

<super type="com.example.markers.mymarker"/> <attribute name="myAttribute" /> <persistent value="true" /> </extension>

Page 36: Eclipse resource management. Outline Introduction Basic resource management Further resource management

public IMarker createMyMarker(IResource resource){try {Imarker marker=

resource.createMarker("com.example.markers.myproblem"); // marker id

marker.setAttribute("myAttribute", "MYVALUE");return marker; } catch (CoreException e){ // You need to handle the cases where attribute

value is rejected } }

Page 37: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Tracking resource changes

The resource API include a event mechanism for resource changes

Use IResourceChangeListener and IResourceChangeEvent to track resource change

Method that create, delete or change a resource typically trigger a resource change event

Page 38: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Must register a resource change listener with the workspace

IResourceChangeListener listener = new MyResourceChangeReporter();

ResourcesPlugin.getWorkspace().addResourceChangeListener( listener, IResourceChangeEvent.POST_CHANGE);

Use IWorkspace.run(runnable,monitor) can batch the change operation, resource change event will be triggered once when the runnable is completed

Page 39: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Resource deltas Resource change event contains a resource

delta that describes the net effect of change Structured as a tree rooted at the

workspace root Describe four type of resource change

Created, deleted, or changed Moved or renamed via IResource.move() Marker that has been added, removed, or

changed Files that has been modified

Page 40: Eclipse resource management. Outline Introduction Basic resource management Further resource management

To traverse a resource tree, implement the interface IResourceDeltaVisitor or using IResourceDelta.getAffectedChildren()

Page 41: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Resource change event

PRE_CLOSE PRE_DELETE PRE_AUTOBUILD POST_AUTOBUILE POST_CHANGE

Page 42: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Implementing a resource change listener

IResourceChangeListener listener = new MyResourceChangeReporter();

ResourcesPlugin.getWorkspace().addResourceChangeListener(listener,

IResourceChangeEvent.PRE_CLOSE | IResourceChangeEvent.PRE_DELETE |

IResourceChangeEvent.PRE_AUTO_BUILD|

IResourceChangeEvent.POST_AUTO_BUILD | IResourceChangeEvent.POST_CHANGE);

Page 43: Eclipse resource management. Outline Introduction Basic resource management Further resource management

public class MyResourceChangeReporter implements IResourceChangeListener {

public void resourceChanged(IResourceChangeEvent event) {IResource res = event.getResource();switch (event.getType()) {

case IResourceChangeEvent.PRE_CLOSE:System.out.print("Project ");

System.out.print(res.getFullPath()); System.out.println(" is about to close."); break;

case IResourceChangeEvent.PRE_DELETE:System.out.print("Project ");

System.out.print(res.getFullPath()); System.out.println(" is about to be deleted."); break;

case IResourceChangeEvent.POST_CHANGE:System.out.println("Resources have changed.");event.getDelta().accept(new DeltaPrinter()); break;

} } }

Page 44: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Incremental project builder

Manipulate the resource in a project in a fashion defined by the builder itself

Often used to apply a transformation on a resource to produce a resource or other kind

Platform define two kind of builds Full build Incremental build

Page 45: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Incremental builds are seeded with a resource change delta to reflect the net effect of all resource change

Page 46: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Invoke a build

A build can be invoked in two ways IProject.build() for the reciving project IWorkspace.build() for all open project

in the workspace An incremental project builders are

also invoked implicitly by the platform during an auto-build

Page 47: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Defining an incremental project builder

<extension id="mybuilder" name="My Sample Builder" point="org.eclipse.core.resources.builders"> <builder <run class="com.example.builders.BuilderExample">

<parameter name="optimize" value="true" /> <parameter name="comment" value="Builder comment" /> </run> </builder>

</extension>

Page 48: Eclipse resource management. Outline Introduction Basic resource management Further resource management

public class BuilderExample extends IncrementalProjectBuilder{

IProject[] build(int kind, Map args, IProgressMonitor

monitor) throws CoreException

{ // add your build logic here return null; }

Protected void startupOnInitialize() { // add builder init logic here } }

Page 49: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Builder implementation

protected IProject[] build(int kind, Map args, IProgressMonitor

monitor throws CoreException {if (kind == IncrementalProjectBuilder.FULL_BUILD)

{ fullBuild(monitor); } //full buildelse { IResourceDelta delta = getDelta(getProject());

if (delta == null) { fullBuild(monitor); } // auto build

else { incrementalBuild(delta, monitor); }} return null; }

Page 50: Eclipse resource management. Outline Introduction Basic resource management Further resource management

protected void fullBuild(final IProgressMonitor monitor) throws CoreException {

try {getProject().accept(new MyBuildVisitor());

} catch (CoreException e) { } }

class MyBuildVisitor implements IResourceVisitor { public boolean visit(IResource res) {

//build the specified resource. //return true to continue visiting children. return true;

} }

Page 51: Eclipse resource management. Outline Introduction Basic resource management Further resource management

protected void incrementalBuild(IResourceDelta delta,

IProgressMonitor monitor) throws CoreException { // the visitor does the work. delta.accept(new MyBuildDeltaVisitor());

}

Page 52: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Association an incremental project builder with a project

IProjectDescription desc = project.getDescription();ICommand[] commands = desc.getBuildSpec();boolean found = false;for (int i = 0; i < commands.length; ++i) {

if (commands[i].getBuilderName().equals(BUILDER_ID)) {found = true; break; } }

if (!found) { //add builder to projectICommand command = desc.newCommand();command.setBuilderName(BUILDER_ID);ICommand[] newCommands = new ICommand[commands.length + 1];

Page 53: Eclipse resource management. Outline Introduction Basic resource management Further resource management

// Add it before other builders.System.arraycopy(commands, 0, newCommands, 1, commands.length);newCommands[0] = command;desc.setBuildSpec(newCommands);project.setDescription(desc, null); }

Page 54: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Workspace save participation

Plug-in can participate in workspace save process to save plug-in data

To participate in saving, add a save participation to the workspace in plug-in startup() method

ResourcesPlugin.getWorkspace().addSaveParticipant(this, saveParticipant);

Page 55: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Save participation implementation

Interface ISaveParticipant defines the protocol for a workspace save participation

Provide behavior for different stages of the save process

Implement method for each stage

Page 56: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Saving steps

prepareToSave Notify the participant the workspace

is about to be saved, it should suspend normal operation

saving Tell the participant to save its

important state

Page 57: Eclipse resource management. Outline Introduction Basic resource management Further resource management

doneSaving Notifies the participant that the

workspace has been saved and the participant can continue normal operation

rollback Tells the participant to rollback the

important state because the save operation has failed

Page 58: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Project natures

Allow a plug-in to tag a project as a specific kink of project

A project can have more then one nature

Special constrains for the nature One-of-nature Requires-nature

Interface IProjectNature

Page 59: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Defining a nature

<extension point="org.eclipse.core.resources.natures" id="myNature" name="My Nature"> <runtime>

<run class="com.example.natures.MyNature">

</run> </runtime>

</extension>

Page 60: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Implement a nature

public class MyNature implements IProjectNature { private IProject project; public void configure() throws CoreException { // Add nature-specific information // for the project, such as adding a builder // to a project's build spec. } public void deconfigure() throws CoreException { // Remove the nature-specific information here. } public IProject getProject() { return project; } public void setProject(IProject value) { project = value; } }

Page 61: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Associate a nature to a project

try { IProjectDescription description = project.getDescription(); String[] natures = description.getNatureIds(); String[] newNatures = new String[natures.length + 1];System.arraycopy(natures, 0, newNatures, 0, natures.length);newNatures[natures.length] = "com.example.natures.myNature";description.setNatureIds(newNatures); project.setDescription(description, null); } catch (CoreException e) { // Something went wrong }

Page 62: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Derived resources

Resources that are not original data, and can be recreated from their source files

Use IResource.setDerived(boolean) to indicate that a resource is derived

Use IResource.isDerived() to determine a resource is derived

Page 63: Eclipse resource management. Outline Introduction Basic resource management Further resource management

Resource modification hooks

Enable the team support plug-in