WSDL ポリシーのサンプル

この WS-I WSDL 検証ポリシーによって、WSDL は WS-I ファセットに準拠するようになります。PolicyGovernor によって呼び出される各ポリシーはこのポリシーと似ています。 WS-I のファセットの 1 つは、操作の引数名に対して適切な名前とバインディングを持つためのものです。 この例では、「XX」が必ず <wsdl:input name="XXgoodname"... の前にリストされるようにポリシーが定義されています。
カスタム・ポリシー作成の手順については、カスタム・ポリシーの開発を参照してください。
package com.ibm.ram.extension.demo.policy;

import java.io.*;
import java.util.Locale;

import org.eclipse.core.runtime.NullProgressMonitor;

import com.ibm.ram.client.*;
import com.ibm.ram.common.data.Artifact;
import com.ibm.ram.extension.ConfigurationDetails;
import com.ibm.ram.extension.PolicyGovernor;
import com.ibm.ram.policy.*;
public class WSIPolicy extends AssetPolicy {
	
	private MyGovernor fGovernor;
	
	private static String WSDL_MATCH0 = "<wsdl:input";
	private static String WSDL_MATCH1 = "<wsdl:message";
	private static String WSDL_MATCH2 = "name=¥"";
	private static String WSDL_MATCH3 = "XX";

	public WSIPolicy(MyGovernor governor) {
		fGovernor = governor;
	}

以下のメソッドは、このポリシーの固有 ID を返します。

public String getID() {
	return "WSDL_000001";
}

以下のメソッドは、ユーザーに対して表示される名前を返します。

public String getName(Locale locale) {
	return "Interoperable Web Services (WS-I)";
}
public String getDescription(Locale locale) {
	return "Ensuring Interoperable Web Services - DEMO";
}

以下のメソッドは、ポリシーの管理プログラムを返します。

public PolicyGovernor getPolicyGovernor() {
	return fGovernor;
}

以下のコードを使用すると、このカスタム・ポリシーを使用する際にアセットを検証する際の基準を示すロジックを入力することができます。

public Result test() {
結果のオブジェクトは、以下の構文を使用してポリシー準拠性テストの総合的な状況を返します。
Result status = new Result(this)

ポリシー・コンテキスト GetRAMAsset メソッドを使用し、次にクライアント API を使用すると、アセットのすべての部分にアクセスできます。 成果物オブジェクトは、アセット内のすべての成果物のリストです。

		RAMAsset asset = this.getPolicyContext().getRAMAsset();
		RAMFolderArtifact folderArtifact = (RAMFolderArtifact)asset.getArtifactsRoot();
		Artifact[] artifacts = folderArtifact.computeArtifactsAsFlatList(new NullProgressMonitor());

以下のコードは、成果物を検証します。

	if (artifacts != null) { 
	validateArtifacts(status, artifacts); 
}		
どの成果物にもエラーがない場合に表示する、全体的に準拠していることを示すメッセージを設定します。
if(status.getReturnCode() != ResultDetail.ERROR){
	status.setMessage("You comply with the WS-I standards");
}
成果物にエラーがある場合は、障害メッセージを表示します。
else{
			status.setMessage("<font color=¥"red¥">You do not comply with the WS-I standards</font> To find out more check out our <a href=¥"www.google.com¥">corporate standard guidlines handbook.</a>");
		}

		return status;
	}
	private void validateWSDL(Result status, InputStream is, Artifact artifact) {
		BufferedReader reader = null;
		try {
			if (is!=null) {
				int lineNo = 0;
				reader = new BufferedReader(new InputStreamReader(is));
				String line;
				StringBuffer message = new StringBuffer();
				while ((line = reader.readLine())!=null) {
					lineNo++;
					int idx = 0;
					while (idx>=0 && idx<line.length()) {
						int ori = idx;
						idx = line.indexOf(WSDL_MATCH0, idx);
						if (idx<0) {
							idx = line.indexOf(WSDL_MATCH1, ori);
						}
						if (idx>=0) {
							idx = line.indexOf(WSDL_MATCH2, idx);
							
							if (idx>=0) {
								idx+=WSDL_MATCH2.length();
								if (idx+WSDL_MATCH3.length()<line.length()) {
									if (line.substring(idx, idx+WSDL_MATCH3.length()).equalsIgnoreCase(WSDL_MATCH3)) {
										
										message.append("<a href=¥"http://www.ws-i.org/Profiles/BasicProfile-1.0-2004-04-16.html¥">All WSDLs must WS-I Compliant </a>: ");
										message.append (artifact.getName());
										message.append ("["+lineNo+"]");
										
										status.addDetail(new ArtifactResultDetail(
													artifact, 
													ResultDetail.ERROR,
													message.toString()));										
									}									
								}
								idx+=WSDL_MATCH3.length();
							}
						}
					}
					
				}
			}
		} 
		catch (Throwable e) {}
		finally {
			if (reader!=null) {
				try {
					reader.close();
				} catch (IOException e) {}
			}
		}			
	}
以下のコードは、アセットにあるすべての成果物について検証テストを実行します。 このコードは、ファイル名の末尾が wsdl であることを確認することで成果物を検証します。
/**
	 * Runs validation on all the artifacts in the asset
	 */
	private void validateArtifacts(Result status, Artifact[] artifacts) {
		for (int i = 0; i < artifacts.length; i++) {
			Artifact artifact = artifacts[i];

			// Validate an XML artifact - make sure the file name end in XML
			if (artifact.getName() != null
					&& artifact.getName().endsWith(".wsdl")) {
				InputStream is = null;
				BufferedReader reader = null;
				try {
					if (artifact instanceof RAMArtifact) {
					    // Use the artifactAccessor to read the contents of an artifact
						is = ((RAMArtifact)artifact).downloadContents();
						if (is != null) {
							validateWSDL(status, is, artifact);
						}
					}
				} catch (Throwable e) {
					//print out any exception
					e.printStackTrace();
				} finally {

以下のコードは、入力ストリームを閉じます。

					try {
						if (reader != null)
							reader.close();
						if (is != null)
							is.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	public ConfigurationDetails[] getConfigurationDetails(Locale locale) {
		return null;
	}
}

フィードバック