本示例说明如何使用 Java API 来上载多个资产。
以下是一个 Main 类示例,其中着重演示了您如何从本地数据执行批处理上载。本示例包含有关如何设置属性、工件和关系的详细信息。
package com.ibm.ram.client.test;
import org.apache.log4j.Logger;
import com.ibm.ram.client.LocalFileArtifact;
import com.ibm.ram.client.RAMAsset;
import com.ibm.ram.client.RAMCategory;
import com.ibm.ram.client.RAMCategorySchema;
import com.ibm.ram.client.RAMFolderArtifact;
import com.ibm.ram.client.RAMSession;
import com.ibm.ram.client.RAMSubCategory;
import com.ibm.ram.client.status.RAMStatus;
import com.ibm.ram.client.status.RAMStatusMonitor;
import com.ibm.ram.common.data.Asset;
/**
* A sample program to show how you could batch upload assets
*
*/
public class BatchUploadAssets {
private static String URL = "http://localhost:8080/ram.ws";
private static String USERID = "userid";
private static String PASS = "password";
//Logger this can be replaced with how ever you want to report status
private static final Logger logger = Logger.getLogger(BatchUploadAssets.class.getName());
/**
* Main method where the program begins execution
* @param args
*/
public static void main(String[] args) {
RAMSession session = null;
try{
//Create a session
session = new RAMSession(URL, USERID, PASS);
//Create RAMAsset objects for each of your local assets
loadAssets(session);
//Upload the assets to the server.
RAMStatus finalStatus = session.putAssets(getStatusMonitor());
//Report the final result
reportStatusMessage(finalStatus.getSeverity(), finalStatus.getException(), finalStatus.getMessage());
}
catch (Exception e) {
logger.error("Unexpected Error : " + e.getLocalizedMessage(), e);
}
finally{
if(session != null){
session.release();
}
}
}
/**
* Customize this method to read and local assets
* and transform them to RAMAssets to be loaded into
* Rational Asset Manager
*/
private static void loadAssets(RAMSession session){
//Iterate through your local data to discover your local assets
Asset[] localAssets = null;
//For each loacal asset you will create a RAMAsset and
//populate it with the local data.
for(int i = 0; i < localAssets.length;i++){
//Create the asset - It is best if you can pull a global unique ID from your local asset
//this will make it more efficient to retrieve. If not you can just provide a version
//and RAM will generate a new GUID for you.
RAMAsset newAsset = session.createAsset(localAssets[i].getGUID(), localAssets[i].getVersion());
//You must provide a Community, Type, Name and short description for every asset
newAsset.setCommunityName(localAssets[i].getCommunityName());
newAsset.setTypeName(localAssets[i].getTypeName());
newAsset.setName(localAssets[i].getName());
newAsset.setShortDescription(localAssets[i].getShortDescription());
//Optionally you can set - full description
newAsset.setDescription(localAssets[i].getDescription());
//Optionally you can set - artifacts
((RAMFolderArtifact)newAsset.getArtifactsRoot()).addArtifact("sample.txt", new LocalFileArtifact(localfile));
//Optionally you can set - attributes
newAsset.getAssetAttribute("namespace").setValues(new String[]{"com.ibm.sampple"});
//Optionally you can set - categorizations
RAMCategorySchema schema = session.getCategorySchema("Development");
RAMCategory category = (RAMCategory)schema.getCategory("Languages");
RAMSubCategory subCategory = (RAMSubCategory)category.getSubCategory("Java");
newAsset.categorize(subCategory);
//Optionally you can set - relationships
newAsset.addRelatedAsset(someOtherAsset, session.getRelationshipType("Dependency"));
//Etc ...
//Once complete, queue the asset on the session to be uploaded in bulk.
//If you have relationships use the force True to ignore collisions.
session.queueAssetForPut(newAsset, true);
}
}
/**
* The status monitor reports progress while the assets are being uploaded to the server.
*/
private static RAMStatusMonitor getStatusMonitor(){
return new RAMStatusMonitor(){
@Override
public void appendStatus(Object targetObject, int severity, int code, String message, Throwable exception) {
super.appendStatus(targetObject, severity, code, message, exception);
reportStatusMessage(severity, exception, message);
}
};
}
/**
* Report a status message
* @param severity
* @param exception
* @param message
*/
private static void reportStatusMessage(int severity, Throwable exception, String message) {
if(severity == RAMStatus.OK || severity == RAMStatus.INFO){
logger.info(message);
}
else if(severity == RAMStatus.WARNING){
if(exception == null)logger.warn(message);
else logger.warn(message, exception);
}
else if(severity == RAMStatus.ERROR){
if(exception == null)logger.error(message);
else logger.error(message, exception);
}
}
}