Plugin Data Storage

You are viewing an old version (v. 2) 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.

  1. Configuration storage
    Configuration storage is used by plugin to store data related to specific configuration, it is simply a sub directory under the configuration directory of server node (refer to glossary chapter in User's Guide for configuration directory definition). If the configuration is deleted, its companion configuration storage will also be deleted. Plugin developer should choose the sub directory name carefully to avoid conflict with other plugins.
    A typical usage of configuration storage is the build statistics plugin: when a build is finished, the statistics plugin collect build metrics, it processes them and it saves the processed results into a sub directory named build-stats under the corresponding configuration directory on server node. It will also add a tab to the configuration statistics page, and display the build statistics charts using the pre-processed data saved in the build-stats directory.
    Please note that it is also possible that a single plugin can use both build and configuration storage. For example, the junit report plugin generates two types of reports: the junit report for specific build, and the junit statistics report for a particular configuration. Data corresponding to build related report are stored in the build storage, and data corresponding to configuration related reports are stored in the configuration storage.
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.