カスタム・ポリシーで使用する API

カスタム・ポリシーを作成する際は、いくつかの API を使用できます。 使用する API は、どのようなアセットをどのように操作するのかによって異なります。 例えば、ポリシーの実行対象となっているアセットを更新する場合は、アセットを作成する場合とは異なる API を使用します。

現在のアセットで使用する API

ポリシーは、Java クライアント API を使用して作成することができます。

ポリシーの実行対象となっているアセット を更新するだけの場合は、ポリシー・コンテキスト・メソッドを使用してセッションを作成し、アセットを取得することができます。 RAMSession を作成し、現行の RAMAsset を取り出すには、以下の API を使用します。
RAMSession ramSession = getPolicyContext().getRAMSession();
RAMAsset ramAsset = getPolicyContext().getRAMAsset();

Rational® Asset Manager Java API クラスのいくつかを使用して、現在のアセットを更新することができます。 例えば、関係を追加したりアクションを設定したりできます。 これらの API クラスを使用して、アセット、アセット属性、および成果物を操作することもできます。

Java API を使用して、ポリシーの実行対象となっているアセットに加えて、他のアセットを変更または作成することもできます。 Java クライアント JAR ファイルを組み込んで、RAMSession() を使用する必要があります。ram.client API の使用時に最適なパフォーマンスを得るために、アセットの更新を単一のセッションに制限してください。

カスタム・ポリシー・コードで以下のステップを実行してください。
  1. getRAMSession メソッドを使用してセッションを作成します。このセッションではライセンスは取り込まれません。
  2. getRAMAsset メソッドを使用してアセットを取得します。
  3. RAMSession API および RAMAsset API を使用して、アセット・オペレーションを実行します。
  4. session.putAssets メソッドを使用して、変更を 1 つのトランザクションとしてコミットします。
例えば、次のようにします。
RAMSession session = getPolicyContext().getRAMSession();  
RAMAsset currentAsset = this.getPolicyContext().getRAMAsset();  
  
// Use the client APIs to make changes to the asset ...

// You can also modify or create other assets:
			RAMAsset asset = session.createAsset("1.0");
			asset.setName("Test Asset");
			asset.setCommunity(session.getCommunity("Sample Application Development"));
			asset.setAssetType(session.getAssetType("Business Solution"));
			asset.setShortDescription("Test asset");
			asset.setOwners(new RAMUser[] { session.getUser("admin") });
		 	
// Commit changes to the asset repository for the current asset:
			session.putAsset(currentAsset);  

// You can use queueAssetForPut and then putAssets to queue and then 
// commit all assets you have changed in the session as one transaction:
			session.queueAssetForPut(currentAsset);  

// Commit changes to all assets you have changed in the session:
			session.putAssets(new NullProgressMonitor());  	
ram.client API を使用するには、ram.client およびポリシー JAR ファイルの両方をアップロードして、これらのファイルを次のようにポリシー・コードにインポートする必要があります。
import com.ibm.ram.common.*
import com.ibm.ram.client.* 

Java クライアント API については、『クライアント・パッケージ (client package)』および『共通データ・パッケージ (common data package)』を参照してください。

その他の例は、このトピックの例のセクション、 およびRational Asset Manager Java API の使用に記載されています。

  • アセット ID を取得します。
    myAssetUniqueID = getManifestAccessor().getId();
  • GUID とバージョンに基づいてアセットを取り出します。
    RAMAsset myAsset = session.getAsset(new AssetIdentification(myAssetUniqueID, "*"));
    
    // Get asset attributes examples:
    AssetAttribute myAttribute = myAsset.getAssetAttribute("Family Name");
    String[] myAttrValue = myAttribute.getValues();
    
    myAttribute = myAsset.getAssetAttribute("Work Item");
    myAttrValue = myAttribute.getValues();
    
    myAttribute = myAsset.getAssetAttribute("Requirement");
    myAttrValue = myAttribute.getValues();
  • アセットを検索するための照会を作成します。
    SearchQuery query = session.createAssetQuery(queryParam);
    int offset = 0;
    int maxResults = 100;
    query.setResultsStartIndex(offset);
    query.setMaxResults(maxResults);
    SearchResult result = session.getAssets(query);
  • アセットを作成します。
    RAMAsset newAsset = session.createAsset("1.0");
    newAsset.setName("The new related asset");
    newAsset.setCommunity(session.getCommunity("Rational Asset Manager Development"));
    newAsset.setAssetType(session.getAssetType("Specification"));
    newAsset.setShortDescription("The Specification asset is required.");
  • 新しいアセットの AssetID を作成します。
    AssetID id = new AssetID();
    id.setGUID(newAsset.getId());
    id.setVersion(newAsset.getVersion());
  • Java クライアント API を検索のために使用します。
    public Result test() {
    		try {
    			// Create configuration parameters for the url, user, password
    			String url = "http://host-name-goes-here:9080/ram";
    			String user = "admin";
    			String password = "admin";
    			String queryParam = null;
    
    			// Set a query 
    			RAMSession session = new RAMSession(url, user, password);
    			SearchQuery query = session.createAssetQuery(queryParam);
    			int offset = 0;
    			int maxResults = 100;
    			query.setResultsStartIndex(offset);
    			query.setMaxResults(maxResults);
    			SearchResult result = session.getAssets(query); 
    			 
    			if (result != null && result.getAssetSearchResults() != null) {
    				AssetSearchResult[] assets = result.getAssetSearchResults();
    	
    				for (AssetSearchResult asset : assets) {
    					AssetInformation assetInformation = asset.getAsset();
    				// Modify an asset here, for example,
    				// create a "DependsOn" relationship from Release and Implementation type assets to a Specification asset of the same name using the client APIs, like this:
    				// Relationship theRelationships = session.getRelationships
    				// getRelationships().add(newItem);
    				}
    			}
    		}
     
    		// Catch exceptions...
    				Result result = new Result();
    		result.setMessage("Success");
    		return result;
    		 
    	}
  • Rational Asset Manager Java API を使用してフォルダーのロケーションを取得し、アセット成果物の情報を取得してアセット成果物をダウンロードします。
     		// Get the asset
    		RAMAsset ramAsset = getRAMAsset(); 
                
    		 		// Get the location and content of the artifacts
    		RAMFolderArtifact srcFolderArtifact = (RAMFolderArtifact)ramAsset.getArtifactsRoot();
    		Artifact[] srcArtifacts = srcFolderArtifact.computeArtifactsAsFlatList(new NullProgressMonitor());
    		 
    		for (Artifact artifact : srcArtifacts) {
            RAMArtifact ramArtifact = (RAMArtifact) artifact;
    					InputStream is = ramArtifact.downloadContents();
    					f = File.createTempFile("theArtifact", null, new File(ramSession.getLocalStorageLocation()));
    					UtilitiesCommon.copyStreams(is, new FileOutputStream(f), null, true, true);
             }

フィードバック