Apex メタデータ deploy() 組織のファイル表現のコンポーネントを作成する
環境
Apex Salesforce
概要
ファイル表現のコンポーネントを使用して、Salesforce 組織のファイル表現のコンポーネントを作成、更新、または削除します。
構文
AsyncResult = metadatabinding.deploy(base64 zipFile, DeployOptions deployOptions)
引数
zipFile
Base 64 で符号化されたバイナリデータ。クライアントアプリケーションは、バイナリデータを base64 に符号化する必要があります。
deployOptions
リリースするパッケージまたはファイルを特定するためのオプションをカプセル化します。
このコールでは次のリリースオプションを選択できます。
allowMissingFiles
package.xml に指定されているファイルが .zip ファイル内に存在しない場合にも、リリースの継続を許可するかどうかを指定します。
autoUpdatePackage
ファイルが .zip ファイルにはあるが、package.xml で指定されていない場合、ファイルを自動的にパッケージに追加するかどうかを指定します。.
使用例
package com.doc.samples;
import java.io.*;
import java.rmi.RemoteException;
import com.sforce.soap.metadata.AsyncResult;
import com.sforce.soap.metadata.DeployDetails;
import com.sforce.soap.metadata.MetadataConnection;
import com.sforce.soap.metadata.DeployOptions;
import com.sforce.soap.metadata.DeployResult;
import com.sforce.soap.metadata.DeployMessage;
import com.sforce.soap.metadata.RunTestsResult;
import com.sforce.soap.metadata.RunTestFailure;
import com.sforce.soap.metadata.CodeCoverageWarning;
import com.sforce.soap.enterprise.LoginResult;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
/**
* メタデータ コンポーネントの zip ファイルをデプロイする.
* Prerequisite: Have a deploy.zip file that includes a package.xml manifest file that
* details the contents of the zip file.
*/
public class DeploySample {
// メタデータ API 呼び出しを行うために使用されるメタデータ WSDL のバインディング
private MetadataConnection metadataConnection;
static BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
private static final String ZIP_FILE = "deploy.zip";
// one second in milliseconds
private static final long ONE_SECOND = 1000;
// zip ファイルの展開を試行する最大回数
private static final int MAX_NUM_POLL_REQUESTS = 50;
public static void main(String[] args) throws Exception {
final String USERNAME = "user@company.com";
// This is only a sample. Hard coding passwords in source files is a bad practice.
final String PASSWORD = "password";
final String URL = "https://login.salesforce.com/services/Soap/c/29.0";
DeploySample sample = new DeploySample(USERNAME, PASSWORD, URL);
sample.deployZip();
}
public DeploySample(String username, String password, String loginUrl)
throws ConnectionException {
createMetadataConnection(username, password, loginUrl);
}
public void deployZip()
throws RemoteException, Exception
{
byte zipBytes[] = readZipFile();
DeployOptions deployOptions = new DeployOptions();
deployOptions.setPerformRetrieve(false);
deployOptions.setRollbackOnError(true);
AsyncResult asyncResult = metadataConnection.deploy(zipBytes, deployOptions);
String asyncResultId = asyncResult.getId();
// デプロイが完了するまで待ち
int poll = 0;
long waitTimeMilliSecs = ONE_SECOND;
DeployResult deployResult = null;
boolean fetchDetails;
do {
Thread.sleep(waitTimeMilliSecs);
// 待ち時間
waitTimeMilliSecs *= 2;
if (poll++ > MAX_NUM_POLL_REQUESTS) {
throw new Exception("Request timed out. If this is a large set " +
"of metadata components, check that the time allowed by " +
"MAX_NUM_POLL_REQUESTS is sufficient.");
}
// 進行の詳細を取得
fetchDetails = (poll % 3 == 0);
deployResult = metadataConnection.checkDeployStatus(asyncResultId, fetchDetails);
System.out.println("Status is: " + deployResult.getStatus());
if (!deployResult.isDone() && fetchDetails) {
printErrors(deployResult, "Failures for deployment in progress:\n");
}
}
while (!deployResult.isDone());
if (!deployResult.isSuccess() && deployResult.getErrorStatusCode() != null) {
throw new Exception(deployResult.getErrorStatusCode() + " msg: " +
deployResult.getErrorMessage());
}
if (!fetchDetails) {
// 前回の試行で実行しなかった場合は、詳細を含む最終結果を取得.
deployResult = metadataConnection.checkDeployStatus(asyncResultId, true);
}
if (!deployResult.isSuccess()) {
printErrors(deployResult, "Final list of failures:\n");
throw new Exception("ファイルは正常にデプロイされませんでした");
}
System.out.println("ファイルは " + ZIP_FILE + "正常にデプロイされます");
}
/**
* zip ファイルの内容をバイト配列に読み取る.
* @return byte[]
* @throws Exception - if cannot find the zip file to deploy
*/
private byte[] readZipFile()
throws Exception
{
// deploy.zipファイルの取得
File deployZip = new File(ZIP_FILE);
if (!deployZip.exists() || !deployZip.isFile())
throw new Exception("Cannot find the zip file to deploy. Looking for " +
deployZip.getAbsolutePath());
FileInputStream fos = new FileInputStream(deployZip);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int readbyte = -1;
while ((readbyte = fos.read()) != -1) {
bos.write(readbyte);
}
fos.close();
bos.close();
return bos.toByteArray();
}
/**
* デプロイに関連するエラーがある場合は、エラーを出力
* @param result - DeployResult
*/
private void printErrors(DeployResult result, String messageHeader)
{
DeployDetails deployDetails = result.getDetails();
StringBuilder errorMessageBuilder = new StringBuilder();
if (deployDetails != null) {
DeployMessage[] componentFailures = deployDetails.getComponentFailures();
for (DeployMessage message : componentFailures) {
String loc = (message.getLineNumber() == 0 ? "" :
("(" + message.getLineNumber() + "," +
message.getColumnNumber() + ")"));
if (loc.length() == 0
&& !message.getFileName().equals(message.getFullName())) {
loc = "(" + message.getFullName() + ")";
}
errorMessageBuilder.append(message.getFileName() + loc + ":" +
message.getProblem()).append('\n');
}
RunTestsResult rtr = deployDetails.getRunTestResult();
if (rtr.getFailures() != null) {
for (RunTestFailure failure : rtr.getFailures()) {
String n = (failure.getNamespace() == null ? "" :
(failure.getNamespace() + ".")) + failure.getName();
errorMessageBuilder.append("Test failure, method: " + n + "." +
failure.getMethodName() + " -- " +
failure.getMessage() + " stack " +
failure.getStackTrace() + "\n\n");
}
}
if (rtr.getCodeCoverageWarnings() != null) {
for (CodeCoverageWarning ccw : rtr.getCodeCoverageWarnings()) {
errorMessageBuilder.append("Code coverage issue");
if (ccw.getName() != null) {
String n = (ccw.getNamespace() == null ? "" :
(ccw.getNamespace() + ".")) + ccw.getName();
errorMessageBuilder.append(", class: " + n);
}
errorMessageBuilder.append(" -- " + ccw.getMessage() + "\n");
}
}
}
if (errorMessageBuilder.length() > 0) {
errorMessageBuilder.insert(0, messageHeader);
System.out.println(errorMessageBuilder.toString());
}
}
private void createMetadataConnection(
final String username,
final String password,
final String loginUrl) throws ConnectionException {
final ConnectorConfig loginConfig = new ConnectorConfig();
loginConfig.setAuthEndpoint(loginUrl);
loginConfig.setServiceEndpoint(loginUrl);
loginConfig.setManualLogin(true);
LoginResult loginResult = (new EnterpriseConnection(loginConfig)).login(
username, password);
final ConnectorConfig metadataConfig = new ConnectorConfig();
metadataConfig.setServiceEndpoint(loginResult.getMetadataServerUrl());
metadataConfig.setSessionId(loginResult.getSessionId());
this.metadataConnection = new MetadataConnection(metadataConfig);
}
}
package com.doc.samples;
import java.io.*;
import java.rmi.RemoteException;
import com.sforce.soap.metadata.AsyncResult;
import com.sforce.soap.metadata.DeployDetails;
import com.sforce.soap.metadata.MetadataConnection;
import com.sforce.soap.metadata.DeployOptions;
import com.sforce.soap.metadata.DeployResult;
import com.sforce.soap.metadata.DeployMessage;
import com.sforce.soap.metadata.RunTestsResult;
import com.sforce.soap.metadata.RunTestFailure;
import com.sforce.soap.metadata.CodeCoverageWarning;
import com.sforce.soap.enterprise.LoginResult;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
/**
* メタデータ コンポーネントの zip ファイルをデプロイする.
* Prerequisite: Have a deploy.zip file that includes a package.xml manifest file that
* details the contents of the zip file.
*/
public class DeploySample {
// メタデータ API 呼び出しを行うために使用されるメタデータ WSDL のバインディング
private MetadataConnection metadataConnection;
static BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
private static final String ZIP_FILE = "deploy.zip";
// one second in milliseconds
private static final long ONE_SECOND = 1000;
// zip ファイルの展開を試行する最大回数
private static final int MAX_NUM_POLL_REQUESTS = 50;
public static void main(String[] args) throws Exception {
final String USERNAME = "user@company.com";
// This is only a sample. Hard coding passwords in source files is a bad practice.
final String PASSWORD = "password";
final String URL = "https://login.salesforce.com/services/Soap/c/29.0";
DeploySample sample = new DeploySample(USERNAME, PASSWORD, URL);
sample.deployZip();
}
public DeploySample(String username, String password, String loginUrl)
throws ConnectionException {
createMetadataConnection(username, password, loginUrl);
}
public void deployZip()
throws RemoteException, Exception
{
byte zipBytes[] = readZipFile();
DeployOptions deployOptions = new DeployOptions();
deployOptions.setPerformRetrieve(false);
deployOptions.setRollbackOnError(true);
AsyncResult asyncResult = metadataConnection.deploy(zipBytes, deployOptions);
String asyncResultId = asyncResult.getId();
// デプロイが完了するまで待ち
int poll = 0;
long waitTimeMilliSecs = ONE_SECOND;
DeployResult deployResult = null;
boolean fetchDetails;
do {
Thread.sleep(waitTimeMilliSecs);
// 待ち時間
waitTimeMilliSecs *= 2;
if (poll++ > MAX_NUM_POLL_REQUESTS) {
throw new Exception("Request timed out. If this is a large set " +
"of metadata components, check that the time allowed by " +
"MAX_NUM_POLL_REQUESTS is sufficient.");
}
// 進行の詳細を取得
fetchDetails = (poll % 3 == 0);
deployResult = metadataConnection.checkDeployStatus(asyncResultId, fetchDetails);
System.out.println("Status is: " + deployResult.getStatus());
if (!deployResult.isDone() && fetchDetails) {
printErrors(deployResult, "Failures for deployment in progress:\n");
}
}
while (!deployResult.isDone());
if (!deployResult.isSuccess() && deployResult.getErrorStatusCode() != null) {
throw new Exception(deployResult.getErrorStatusCode() + " msg: " +
deployResult.getErrorMessage());
}
if (!fetchDetails) {
// 前回の試行で実行しなかった場合は、詳細を含む最終結果を取得.
deployResult = metadataConnection.checkDeployStatus(asyncResultId, true);
}
if (!deployResult.isSuccess()) {
printErrors(deployResult, "Final list of failures:\n");
throw new Exception("ファイルは正常にデプロイされませんでした");
}
System.out.println("ファイルは " + ZIP_FILE + "正常にデプロイされます");
}
/**
* zip ファイルの内容をバイト配列に読み取る.
* @return byte[]
* @throws Exception - if cannot find the zip file to deploy
*/
private byte[] readZipFile()
throws Exception
{
// deploy.zipファイルの取得
File deployZip = new File(ZIP_FILE);
if (!deployZip.exists() || !deployZip.isFile())
throw new Exception("Cannot find the zip file to deploy. Looking for " +
deployZip.getAbsolutePath());
FileInputStream fos = new FileInputStream(deployZip);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int readbyte = -1;
while ((readbyte = fos.read()) != -1) {
bos.write(readbyte);
}
fos.close();
bos.close();
return bos.toByteArray();
}
/**
* デプロイに関連するエラーがある場合は、エラーを出力
* @param result - DeployResult
*/
private void printErrors(DeployResult result, String messageHeader)
{
DeployDetails deployDetails = result.getDetails();
StringBuilder errorMessageBuilder = new StringBuilder();
if (deployDetails != null) {
DeployMessage[] componentFailures = deployDetails.getComponentFailures();
for (DeployMessage message : componentFailures) {
String loc = (message.getLineNumber() == 0 ? "" :
("(" + message.getLineNumber() + "," +
message.getColumnNumber() + ")"));
if (loc.length() == 0
&& !message.getFileName().equals(message.getFullName())) {
loc = "(" + message.getFullName() + ")";
}
errorMessageBuilder.append(message.getFileName() + loc + ":" +
message.getProblem()).append('\n');
}
RunTestsResult rtr = deployDetails.getRunTestResult();
if (rtr.getFailures() != null) {
for (RunTestFailure failure : rtr.getFailures()) {
String n = (failure.getNamespace() == null ? "" :
(failure.getNamespace() + ".")) + failure.getName();
errorMessageBuilder.append("Test failure, method: " + n + "." +
failure.getMethodName() + " -- " +
failure.getMessage() + " stack " +
failure.getStackTrace() + "\n\n");
}
}
if (rtr.getCodeCoverageWarnings() != null) {
for (CodeCoverageWarning ccw : rtr.getCodeCoverageWarnings()) {
errorMessageBuilder.append("Code coverage issue");
if (ccw.getName() != null) {
String n = (ccw.getNamespace() == null ? "" :
(ccw.getNamespace() + ".")) + ccw.getName();
errorMessageBuilder.append(", class: " + n);
}
errorMessageBuilder.append(" -- " + ccw.getMessage() + "\n");
}
}
}
if (errorMessageBuilder.length() > 0) {
errorMessageBuilder.insert(0, messageHeader);
System.out.println(errorMessageBuilder.toString());
}
}
private void createMetadataConnection(
final String username,
final String password,
final String loginUrl) throws ConnectionException {
final ConnectorConfig loginConfig = new ConnectorConfig();
loginConfig.setAuthEndpoint(loginUrl);
loginConfig.setServiceEndpoint(loginUrl);
loginConfig.setManualLogin(true);
LoginResult loginResult = (new EnterpriseConnection(loginConfig)).login(
username, password);
final ConnectorConfig metadataConfig = new ConnectorConfig();
metadataConfig.setServiceEndpoint(loginResult.getMetadataServerUrl());
metadataConfig.setSessionId(loginResult.getSessionId());
this.metadataConnection = new MetadataConnection(metadataConfig);
}
}
package com.doc.samples; import java.io.*; import java.rmi.RemoteException; import com.sforce.soap.metadata.AsyncResult; import com.sforce.soap.metadata.DeployDetails; import com.sforce.soap.metadata.MetadataConnection; import com.sforce.soap.metadata.DeployOptions; import com.sforce.soap.metadata.DeployResult; import com.sforce.soap.metadata.DeployMessage; import com.sforce.soap.metadata.RunTestsResult; import com.sforce.soap.metadata.RunTestFailure; import com.sforce.soap.metadata.CodeCoverageWarning; import com.sforce.soap.enterprise.LoginResult; import com.sforce.soap.enterprise.EnterpriseConnection; import com.sforce.ws.ConnectionException; import com.sforce.ws.ConnectorConfig; /** * メタデータ コンポーネントの zip ファイルをデプロイする. * Prerequisite: Have a deploy.zip file that includes a package.xml manifest file that * details the contents of the zip file. */ public class DeploySample { // メタデータ API 呼び出しを行うために使用されるメタデータ WSDL のバインディング private MetadataConnection metadataConnection; static BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in)); private static final String ZIP_FILE = "deploy.zip"; // one second in milliseconds private static final long ONE_SECOND = 1000; // zip ファイルの展開を試行する最大回数 private static final int MAX_NUM_POLL_REQUESTS = 50; public static void main(String[] args) throws Exception { final String USERNAME = "user@company.com"; // This is only a sample. Hard coding passwords in source files is a bad practice. final String PASSWORD = "password"; final String URL = "https://login.salesforce.com/services/Soap/c/29.0"; DeploySample sample = new DeploySample(USERNAME, PASSWORD, URL); sample.deployZip(); } public DeploySample(String username, String password, String loginUrl) throws ConnectionException { createMetadataConnection(username, password, loginUrl); } public void deployZip() throws RemoteException, Exception { byte zipBytes[] = readZipFile(); DeployOptions deployOptions = new DeployOptions(); deployOptions.setPerformRetrieve(false); deployOptions.setRollbackOnError(true); AsyncResult asyncResult = metadataConnection.deploy(zipBytes, deployOptions); String asyncResultId = asyncResult.getId(); // デプロイが完了するまで待ち int poll = 0; long waitTimeMilliSecs = ONE_SECOND; DeployResult deployResult = null; boolean fetchDetails; do { Thread.sleep(waitTimeMilliSecs); // 待ち時間 waitTimeMilliSecs *= 2; if (poll++ > MAX_NUM_POLL_REQUESTS) { throw new Exception("Request timed out. If this is a large set " + "of metadata components, check that the time allowed by " + "MAX_NUM_POLL_REQUESTS is sufficient."); } // 進行の詳細を取得 fetchDetails = (poll % 3 == 0); deployResult = metadataConnection.checkDeployStatus(asyncResultId, fetchDetails); System.out.println("Status is: " + deployResult.getStatus()); if (!deployResult.isDone() && fetchDetails) { printErrors(deployResult, "Failures for deployment in progress:\n"); } } while (!deployResult.isDone()); if (!deployResult.isSuccess() && deployResult.getErrorStatusCode() != null) { throw new Exception(deployResult.getErrorStatusCode() + " msg: " + deployResult.getErrorMessage()); } if (!fetchDetails) { // 前回の試行で実行しなかった場合は、詳細を含む最終結果を取得. deployResult = metadataConnection.checkDeployStatus(asyncResultId, true); } if (!deployResult.isSuccess()) { printErrors(deployResult, "Final list of failures:\n"); throw new Exception("ファイルは正常にデプロイされませんでした"); } System.out.println("ファイルは " + ZIP_FILE + "正常にデプロイされます"); } /** * zip ファイルの内容をバイト配列に読み取る. * @return byte[] * @throws Exception - if cannot find the zip file to deploy */ private byte[] readZipFile() throws Exception { // deploy.zipファイルの取得 File deployZip = new File(ZIP_FILE); if (!deployZip.exists() || !deployZip.isFile()) throw new Exception("Cannot find the zip file to deploy. Looking for " + deployZip.getAbsolutePath()); FileInputStream fos = new FileInputStream(deployZip); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int readbyte = -1; while ((readbyte = fos.read()) != -1) { bos.write(readbyte); } fos.close(); bos.close(); return bos.toByteArray(); } /** * デプロイに関連するエラーがある場合は、エラーを出力 * @param result - DeployResult */ private void printErrors(DeployResult result, String messageHeader) { DeployDetails deployDetails = result.getDetails(); StringBuilder errorMessageBuilder = new StringBuilder(); if (deployDetails != null) { DeployMessage[] componentFailures = deployDetails.getComponentFailures(); for (DeployMessage message : componentFailures) { String loc = (message.getLineNumber() == 0 ? "" : ("(" + message.getLineNumber() + "," + message.getColumnNumber() + ")")); if (loc.length() == 0 && !message.getFileName().equals(message.getFullName())) { loc = "(" + message.getFullName() + ")"; } errorMessageBuilder.append(message.getFileName() + loc + ":" + message.getProblem()).append('\n'); } RunTestsResult rtr = deployDetails.getRunTestResult(); if (rtr.getFailures() != null) { for (RunTestFailure failure : rtr.getFailures()) { String n = (failure.getNamespace() == null ? "" : (failure.getNamespace() + ".")) + failure.getName(); errorMessageBuilder.append("Test failure, method: " + n + "." + failure.getMethodName() + " -- " + failure.getMessage() + " stack " + failure.getStackTrace() + "\n\n"); } } if (rtr.getCodeCoverageWarnings() != null) { for (CodeCoverageWarning ccw : rtr.getCodeCoverageWarnings()) { errorMessageBuilder.append("Code coverage issue"); if (ccw.getName() != null) { String n = (ccw.getNamespace() == null ? "" : (ccw.getNamespace() + ".")) + ccw.getName(); errorMessageBuilder.append(", class: " + n); } errorMessageBuilder.append(" -- " + ccw.getMessage() + "\n"); } } } if (errorMessageBuilder.length() > 0) { errorMessageBuilder.insert(0, messageHeader); System.out.println(errorMessageBuilder.toString()); } } private void createMetadataConnection( final String username, final String password, final String loginUrl) throws ConnectionException { final ConnectorConfig loginConfig = new ConnectorConfig(); loginConfig.setAuthEndpoint(loginUrl); loginConfig.setServiceEndpoint(loginUrl); loginConfig.setManualLogin(true); LoginResult loginResult = (new EnterpriseConnection(loginConfig)).login( username, password); final ConnectorConfig metadataConfig = new ConnectorConfig(); metadataConfig.setServiceEndpoint(loginResult.getMetadataServerUrl()); metadataConfig.setSessionId(loginResult.getSessionId()); this.metadataConnection = new MetadataConnection(metadataConfig); } }