Plugin Data Storage

You are viewing an old version (v. 3) of this page.
The latest version is v. 10, last edited on Mar 11, 2010 (view differences | )
<< View previous version | view page history | view next version >>

Store build related data

Plugin might need to have a place on server to store build related data. For example, JUnit plugin stores processed JUnit test data for each build into the build storage area after JUnit publish step runs, and use that data to render JUnit report upon UI access. Normally this place is a directory choosed by plugins under the Build Publish Directory, and files can be stored into the place by calling publish method of the build object.
Again we demonstrate this by enhancing myplugin to store the user specified message at server when the step runs so that it can be rendered into build overview screen.
First modify MyAnotherStep.java as below to save message into a file and got it published to server.

package com.example.myplugin;

import java.io.File;

import org.hibernate.validator.NotEmpty;

import com.pmease.quickbuild.annotation.Editable;
import com.pmease.quickbuild.annotation.Scriptable;
import com.pmease.quickbuild.stepsupport.Step;
import com.pmease.quickbuild.util.FileUtils;

@Editable(category="examples", name="publish message", description=
    "This step publishes an user defined message and render them on build overview screen.")
public class MyAnotherStep extends Step {

	private static final long serialVersionUID = 1L;
	
	private String message;

	@Editable
	@NotEmpty
	@Scriptable
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Override
	public void run() {
		File tempDir = FileUtils.createTempDir();
		FileUtils.writeFile(new File(tempDir, "message.txt"), getMessage());
		try {
                        // call publish to transfer files to server node. Do not simply
                        // copy files here since the step might be running on an agent 
                        // node.
			getBuild().publish(tempDir.getAbsolutePath(), null, "myplugin");
		} finally {
			FileUtils.deleteDir(tempDir);
		}
	}

}

And then modify MyPanel.java like below to render the message in published file.

package com.example.myplugin;

import java.io.File;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;

import com.pmease.quickbuild.Context;
import com.pmease.quickbuild.util.FileUtils;

public class MyPanel extends Panel {

	private static final long serialVersionUID = 1L;
	
	public MyPanel(String id) {
		super(id);

		String message = FileUtils.readFileAsString(
				new File(Context.getBuild().getPublishDir(), "myplugin/message.txt"));
		add(new Label("message", message));
	}

}

Finally modify MyPlugin.java to contribute the message panel only when sub directory myplugin exists.

package com.example.myplugin;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.panel.Panel;

import com.pmease.quickbuild.extensionpoint.BuildDashboardContribution;
import com.pmease.quickbuild.extensionpoint.StepProvider;
import com.pmease.quickbuild.extensionpoint.support.PanelProvider;
import com.pmease.quickbuild.model.Build;
import com.pmease.quickbuild.pluginsupport.AbstractPlugin;
import com.pmease.quickbuild.stepsupport.Step;

public class MyPlugin extends AbstractPlugin {

	@Override
	public Object[] getExtensions() {
		return new Object[] {
			new StepProvider() {

				@Override
				public Class<? extends Step> getStepClass() {
					return MyStep.class;
				}
				
			}, 
			new StepProvider() {

				@Override
				public Class<? extends Step> getStepClass() {
					return MyAnotherStep.class;
				}
				
			}, 
			new BuildDashboardContribution() {
				
				public List<PanelProvider> getProviders(Build build) {
					List<PanelProvider> providers = new ArrayList<PanelProvider>();
                                        
					if (new File(build.getPublishDir(), "myplugin").exists()) {
						providers.add(new PanelProvider() {
	
							public Panel getPanel(String id) {
								return new MyPanel(id);
							}
							
						});
					}
					return providers;
				}
				
				public int getOrder() {
					return 500;
				}
			}
		};
	}

}

Now start QuickBuild, and run the root configuration. You will see the published message file under directory <global storage directory>/builds/<build id>/myplugin, where <global storage directory> is the directory you specified when set up QuickBuild server, and <build id> refers to id of the newly generated build and is displayed at the build overview screen.
Overview screen of the newly generated build will also contains a panel displaying your specified message when define the step.

Store configuration related data

Besides storing build related data, plugin may also need to store configuation related data to server. Still taking the JUnit plugin for example, it accumulates test statistics information for a configuration overtime and use it to render JUnit statistics chart upon UI access. The Configuration Publish Directory is a good place to store such data, and plugins can create sub directories under this place to hold their own data if necessary.
We demonstrate this by enhancing myplugin to introduce a new statistics tab displaying total number of published messages.
First add a new class MyMetricsCollector.java as below:


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.