|
|
After setting up your development environment, the next step in converting a QPAC to a LiveCycle ES component is to create a service implementation. This Java interface defines the service’s operations (public methods), input values, and return values. The public methods that are defined become operations that are exposed by the service. Unlike a custom QPAC, you do not need to implement any Java libraries specific to Adobe.The following example shows the interface that belongs to the email component. Notice that the name of the interface is EmailService.Example: Defining the email service interfacepackage com.adobe.livecycle.sample.email;import com.adobe.idp.Document;//The interface for the email componentpublic interface EmailService {//Sends email to the specified email accountsvoid send(String aToAddress,String aCCAddress,String aBCCAddress,String aSubject,String aMimeType,String aMessage,String aAttachmentName,Document docAttacment) throws Exception;}Next, you must create the service implementation class that implements the interface that you defined. The service implementation class is where similar application logic found in a custom QPAC’s execute method is defined. That is, this class contains Java application logic that performs the component’s operations.The application logic that is executed by LiveCycle ES when the operation is invoked must be specified in the corresponding methods. For example, the send method that is defined in the EmailService interface must contain Java application logic that sends email messages.The following Java code shows the Java implementation class named EmailServiceImpl that implements EmailService.package com.adobe.livecycle.sample.email;import java.util.Properties;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.BodyPart;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;import com.adobe.idp.Document;public class EmailServiceImpl implements EmailService {//Specify data members for the EmailServiceImpl//These members correspond to configuration valuesprotected String m_smtpHost;protected String m_smtpUser;protected String m_smtpPassword;//Set the host Mail serverpublic void setSmtpHost(String aHost) {if (aHost != null) {aHost = aHost.trim();}m_smtpHost = aHost;}//Get the Host namepublic String getSmtpHost() {return m_smtpHost;}//Set the smtp user namepublic void setSmtpUser(String aUser) {if (aUser != null) {aUser = aUser.trim();}m_smtpUser = aUser;}//Get the smtp user namepublic String getSmtpUser() {return m_smtpUser;}//Set the smtp passwordpublic void setSmtpPassword(String aPassword) {if (aPassword != null) {aPassword = aPassword.trim();}m_smtpPassword = aPassword;}//Get the smtp passwordpublic String getSmtpPassword() {return m_smtpPassword;}//Send the email.public void send(String aToAddress,String aCCAddress,String aBCCAddress,String aSubject,String aMimeType,String aMessage,String aAttachmentName,Document docAttachment) throws MessagingException, Exception {{try {Properties _props = new Properties();if (getSmtpHost() != null) {_props.put("mail.smtp.host", getSmtpHost());}if (getSmtpUser() != null) {_props.put("mail.smtp.user", getSmtpUser());}// Setup mail server if authentication usedif (!(isEmpty(m_smtpUser) || !(isEmpty(m_smtpPassword)))){_props.put("mail.smtp.auth", "true");}//Get the default Session and add the new properties to it.Session _session = Session.getInstance(_props, null);//Create a messageMimeMessage _msg = new MimeMessage(_session);//Set the sent from addressInternetAddress fromIntenetAddress = new InternetAddress(getSmtpUser());_msg.setFrom(fromIntenetAddress);InternetAddress[] toInternetAddresses = InternetAddress.parse(aToAddress);_msg.setRecipients(Message.RecipientType.TO, toInternetAddresses);if (!isEmpty(aCCAddress)) {InternetAddress[] toCCInternetAddresses = InternetAddress.parse(aCCAddress);_msg.setRecipients(Message.RecipientType.CC,toCCInternetAddresses);}if (!isEmpty(aBCCAddress)) {InternetAddress[] toBCCInternetAddresses = InternetAddress.parse(aBCCAddress);_msg.setRecipients(Message.RecipientType.BCC,toBCCInternetAddresses);}if (!isEmpty(aSubject)) {_msg.setSubject(aSubject);}if (!isEmpty(aMessage)) {if (!isEmpty(aMimeType)) {_msg.setContent(aMessage, aMimeType);} else {_msg.setText(aMessage);}}Multipart multipart = new MimeMultipart();BodyPart messageBodyPart = new MimeBodyPart();if (docAttachment != null){BodyPart _bodyPartAttachment = new MimeBodyPart();FileDataSource fileSource = new FileDataSource(docAttachment.getFile());_bodyPartAttachment.setDataHandler( new DataHandler(fileSource));if (!isEmpty(aAttachmentName)){_bodyPartAttachment.setFileName(aAttachmentName.trim());} else {_bodyPartAttachment.setFileName("attachment.pdf");}multipart.addBodyPart(_bodyPartAttachment);_msg.setContent(multipart);}//Send the email message_msg.saveChanges(); // implicit with send()messageBodyPart.setContent(aMessage, "text/html; charset=utf-8");multipart.addBodyPart(messageBodyPart);//Set the sent date for the email_msg.setSentDate(new java.util.Date());//Send the messageTransport _transport =_session.getTransport("smtp");_transport.connect(getSmtpHost(), getSmtpUser(), getSmtpPassword());_transport.sendMessage(_msg, _msg.getAllRecipients());} catch (MessagingException ex) {throw ex;} catch (NullPointerException npe) {throw new Exception("Incorrect SMTP server provided");}}}//check If passed in string contains only non whitespacepublic static boolean isEmpty(String aValue) {return (aValue == null || aValue.trim().length() == 0);}}The application logic that is located in the send method is similar to the application logic that is located in the execute method. That is, they both use the Java Mail API to send email messages.However, there are also some differences. For example, the application logic located in the QPAC’s execute method uses APIs specific to Adobe while the application logic located in the EmailServiceImpl class does not. Notice how input values, such as the aToAddress, are passed to a LiveCycle ES component. Input values are specified as operation parameters:public void send(String aToAddress,String aCCAddress,String aBCCAddress,String aSubject,String aMimeType,String aMessage,String aAttachmentName,
Document docAttachment)A component contains other classes that enable you to perform tasks, such as control the component’s behavior when it is started and stopped. You can create a LiveCycle ES implementation class that implements the com.adobe.idp.dsc.component.LifeCycle interface and exposes the following methods:setComponentContext: Sets the context of the component.onStart: Is invoked when the service is startedonStop: Is invoked when the component is stopped.
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/livecycle/es/sdkHelp/programmer/sdkHelp/componentUpgrading.160.9.html