12.07.2015 Views

Database.com Metadata API Developer's Guide - Salesforce.com

Database.com Metadata API Developer's Guide - Salesforce.com

Database.com Metadata API Developer's Guide - Salesforce.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Quick StartStep 3: Walk Through the Java Sample CodeStep 3: Walk Through the Java Sample CodeOnce you have imported the WSDL files, you can begin building client applications that use <strong>Metadata</strong> <strong>API</strong>. The sample isa good starting point for writing your own code.Before you run the sample, modify your project and the code to:1. Include the WSC JAR, its dependencies, and the JAR files you generated from the WSDLs.Note: Although WSC has other dependencies, the following sample only requires Rhino (js-1.7R2.jar),which you can download from mvnrepository.<strong>com</strong>/artifact/rhino/js.2. Update USERNAME and PASSWORD variables in the <strong>Metadata</strong>LoginUtil.login() method with your user nameand password. If your current IP address isn’t in your organization's trusted IP range, you'll need to append a security tokento the password.3. If you are using a test database, be sure to change the login URL.Login UtilityJava users can use ConnectorConfig to connect to Enterprise, Partner, and <strong>Metadata</strong> SOAP <strong>API</strong>. <strong>Metadata</strong>LoginUtilcreates a ConnectorConfig object and logs in using the Enterprise WSDL login method. Then it retrieves sessionIdand metadataServerUrl to create a ConnectorConfig and connects to <strong>Metadata</strong> <strong>API</strong> endpoint. ConnectorConfigis defined in WSC.The <strong>Metadata</strong>LoginUtil class abstracts the login code from the other parts of the sample, allowing portions of this codeto be reused without change across different <strong>Database</strong>.<strong>com</strong> <strong>API</strong>s.import <strong>com</strong>.sforce.soap.enterprise.EnterpriseConnection;import <strong>com</strong>.sforce.soap.enterprise.LoginResult;import <strong>com</strong>.sforce.soap.metadata.<strong>Metadata</strong>Connection;import <strong>com</strong>.sforce.ws.ConnectionException;import <strong>com</strong>.sforce.ws.ConnectorConfig;/*** Login utility.*/public class <strong>Metadata</strong>LoginUtil {public static <strong>Metadata</strong>Connection login() throws ConnectionException {final String USERNAME = "user@<strong>com</strong>pany.<strong>com</strong>";// 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.<strong>com</strong>/services/Soap/c/30.0";final LoginResult loginResult = loginTo<strong>Salesforce</strong>(USERNAME, PASSWORD, URL);return create<strong>Metadata</strong>Connection(loginResult);}private static <strong>Metadata</strong>Connection create<strong>Metadata</strong>Connection(final LoginResult loginResult) throws ConnectionException {final ConnectorConfig config = new ConnectorConfig();config.setServiceEndpoint(loginResult.get<strong>Metadata</strong>ServerUrl());config.setSessionId(loginResult.getSessionId());return new <strong>Metadata</strong>Connection(config);}private static LoginResult loginTo<strong>Salesforce</strong>(final String username,final String password,final String loginUrl) throws ConnectionException {final ConnectorConfig config = new ConnectorConfig();config.setAuthEndpoint(loginUrl);config.setServiceEndpoint(loginUrl);7

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!