Verwenden Sie diese vereinfachten Dateien als vereinfachtes Beispiel der bereitgestellten Erweiterungspunkte.
Beispiel für FileSystemBatchDataSource
package com.ibm.ram.batch.example;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import com.ibm.ram.client.LocalFileArtifact;
import com.ibm.ram.client.LocalFolderArtifact;
import com.ibm.ram.client.batch.BatchDataSource;
import com.ibm.ram.client.status.RAMStatusMonitor;
import com.ibm.ram.common.data.*;
/**
* Data source for an example batch client extension that creates assets from folders and .zip files
* in a given root folder. Those folders and .zip files must contain a file named .asset_info, whose
* contents must be in the format of a .properties file (commonly, one key=value pair per line...
* see {@link java.util.Properties}) and contain properties for the asset:
* <ul>
* <li>name (Optional. If omitted, the file/folder name will be used.)
* <li>version (Optional. If omitted, 1.0 will be used.)
* <li>community (Required)
* <li>asset_type (Required)
* <li>short_description (Optional. If omitted, the file/folder name will be used.)
* <li>description (Optional)
* </ul>
*
* @author ebordeau
*/
public class FileSystemBatchDataSource extends BatchDataSource {
private static final String NAME = "name";
private static final String VERSION = "version";
private static final String COMMUNITY = "community";
private static final String ASSET_TYPE = "asset_type";
private static final String SHORT_DESCRIPTION = "short_description";
private static final String DESCRIPTION = "description";
private static final String ASSET_INFO = ".asset_info";
private File root;
private String rootPath;
public FileSystemBatchDataSource() {}
public FileSystemBatchDataSource(String path) {
rootPath = path;
}
/**
* Create and return the assets based on the root path.
*
* @see com.ibm.ram.client.batch.BatchDataSource#fetchAssets(com.ibm.ram.client.status.RAMStatusMonitor)
*/
@Override
public Asset[] fetchAssets(RAMStatusMonitor monitor) {
if (root == null) {
if (rootPath == null) // if the root path isn't specified, just return an empty array
return new Asset[0];
root = new File(rootPath);
}
List<Asset> assets = new ArrayList<Asset>();
// get all the files in the root directory
File[] files = root.listFiles();
for (File file : files) {
// we need to figure out what folder to use for the asset
File folder = null;
if (file.isDirectory()) {
// if it's a directory, just use it as-is
folder = file;
} else if (file.getName().toLowerCase().endsWith(".zip")) {
// if it's a zip file, extract to a temp directory
try {
ZipFile zip = new ZipFile(file);
folder = extractZip(zip);
} catch (ZipException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
// if it's not a zip file or a directory, ignore it
continue;
}
// parse the .asset_info file
File assetInfoFile = new File(folder, ASSET_INFO);
Asset asset = new Asset();
parseAssetInfo(assetInfoFile, asset);
// if name, version or short description were not set, set them to their defaults
if (asset.getName() == null)
asset.setName(folder.getName());
if (asset.getIdentification().getVersion() == null)
asset.getIdentification().setVersion("1.0");
if (asset.getShortDescription() == null)
asset.setShortDescription(folder.getName());
// add all files in the folder (except the .asset_info file) as artifacts
File[] artifactFiles = folder.listFiles();
List<Artifact> artifacts = new ArrayList<Artifact>();
for (File artifactFile : artifactFiles) {
if (artifactFile.getName().toLowerCase().equals(ASSET_INFO))
continue;
if (artifactFile.isDirectory())
artifacts.add(new LocalFolderArtifact(artifactFile));
else
artifacts.add(new LocalFileArtifact(artifactFile));
}
if (!artifacts.isEmpty()) {
FolderArtifact artifactsRoot = asset.getArtifactsRoot();
if (artifactsRoot == null) {
artifactsRoot = new FolderArtifact();
asset.setArtifactsRoot(artifactsRoot);
}
artifactsRoot.setChildren(artifacts.toArray(new Artifact[artifacts.size()]));
}
assets.add(asset);
}
// return the assets that were created
return assets.toArray(new Asset[assets.size()]);
}
/**
* Parse the .asset_info file and set the values on the asset.
*/
private void parseAssetInfo(File assetInfoFile, Asset asset) {
Properties props = new Properties();
try {
props.load(new FileInputStream(assetInfoFile));
asset.setName(props.getProperty(NAME));
asset.getIdentification().setVersion(props.getProperty(VERSION));
CommunityInformation community = new CommunityInformation();
community.setName(props.getProperty(COMMUNITY));
asset.setCommunity(community);
AssetType assetType = new AssetType();
assetType.setName(props.getProperty(ASSET_TYPE));
asset.setAssetType(assetType);
asset.setShortDescription(props.getProperty(SHORT_DESCRIPTION));
asset.setDescription(props.getProperty(DESCRIPTION));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Extract the .zip file to a temp directory and return that temp directory.
*/
private File extractZip(ZipFile zip) {
String name = zip.getName();
int i = name.lastIndexOf(File.separator);
name = name.substring(i + 1, name.length() - 4);
File destination = new File(System.getProperty("java.io.tmpdir"), name);
Enumeration entries = zip.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry)entries.nextElement();
File tmp = new File(destination, entry.getName());
tmp.getParentFile().mkdirs();
InputStream in = null;
FileOutputStream out = null;
try {
in = zip.getInputStream(entry);
out = new FileOutputStream(tmp);
byte[] data = new byte[8192];
int read;
while ((read = in.read(data)) > -1) {
out.write(data, 0, read);
}
} catch (IOException e) {
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {}
}
}
}
return destination;
}
/**
* Return the root path for this data source.
*/
public String getRootPath() {
return rootPath;
}
/**
* This is the ID of your data source extension in the plugin.xml.
*
* @see com.ibm.ram.client.batch.BatchDataSource#getTypeId()
*/
@Override
public String getTypeId() {
return "com.ibm.ram.batch.example.filesystem";
}
/**
* The metadata here is just the root path.
*
* @see com.ibm.ram.client.batch.BatchDataSource#initialize(java.lang.String)
*/
@Override
public void initialize(String metadata) {
rootPath = metadata;
}
/**
* We only need to save the root path. In a more complex example, this
* would probably be something more useful, like XML.
*
* @see com.ibm.ram.client.batch.BatchDataSource#save()
*/
@Override
public String save() {
return rootPath;
}
}
FileSystemBatchUIContributor example
package com.ibm.ram.batch.example;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Shell;
import com.ibm.ram.batch.ui.AbstractBatchUIContributor;
import com.ibm.ram.client.batch.BatchDataSource;
import com.ibm.ram.internal.rich.ui.util.ImageUtil;
/**
* Batch UI contributor that uses a directory selection dialog to allow the user
* to select the root directory from which to create assets. See {@link FileSystemBatchDataSource}
* for the expected structure of the root directory. Also provides basic label and
* content providers.
*
* @author ebordeau
*/
public class FileSystemBatchUIContributor extends AbstractBatchUIContributor {
/**
* Creates a new data source by opening a directory selection dialog from
* which the user can select the root directory.
*
* @see com.ibm.ram.batch.ui.AbstractBatchUIContributor#createNewDataSource(org.eclipse.swt.widgets.Shell)
*/
@Override
public BatchDataSource createNewDataSource(Shell shell) {
DirectoryDialog dialog = new DirectoryDialog(shell, SWT.NONE);
dialog.setMessage("Select root directory");
String dir = dialog.open();
if (dir != null)
return new FileSystemBatchDataSource(dir);
return null;
}
/**
* Returns a content provider that does nothing, since the data source
* will have no children.
*
* @see com.ibm.ram.batch.ui.AbstractBatchUIContributor#getContentProvider()
*/
@Override
public ITreeContentProvider getContentProvider() {
return new ITreeContentProvider() {
public Object[] getChildren(Object parentElement) {
return new Object[0];
}
public Object getParent(Object element) {
return null;
}
public boolean hasChildren(Object element) {
return false;
}
public Object[] getElements(Object inputElement) {
return new Object[0];
}
public void dispose() {}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
};
}
/**
* Returns a label provider that provides an icon and label for the root
* directory.
*
* @see com.ibm.ram.batch.ui.AbstractBatchUIContributor#getLabelProvider()
*/
@Override
public ILabelProvider getLabelProvider() {
return new LabelProvider() {
public Image getImage(Object element) {
if (element instanceof FileSystemBatchDataSource)
return ImageUtil.FOLDER_IMAGE;
return super.getImage(element);
}
public String getText(Object element) {
if (element instanceof FileSystemBatchDataSource)
return ((FileSystemBatchDataSource)element).getRootPath();
return super.getText(element);
}
};
}
}