Apache Utilities At Work   V5
Upcoming SlideShare
Loading in...5
×
 

Apache Utilities At Work V5

on

  • 3,438 views

You're on another typical JavaEE-based project, and you find yourself writing the same old infrastructure code. Are you wondering if there's a quicker way to incorporate the basics such as ...

You're on another typical JavaEE-based project, and you find yourself writing the same old infrastructure code. Are you wondering if there's a quicker way to incorporate the basics such as configuration, logging, and email into your application? If so, then this presentation is for you. By using a number of Apache utilities from
Commons, Logging, and other areas, you can learn how to stop re-inventing the wheel.

We'll start with a simple Struts 2 application and iteratively add the ability to:
• Use Commons Lang for String and Date utilities.
• Use Commons Property Configuration to setup and use application Properties.
• Use Commons Logging and Log4J to log messages.
• Generate Excel spreadsheets with POI.
• Use Velocity Templates and Commons Email to format and send email messages.
• Use HttpClient to invoke web apps with HTTP/S.
• Use Commons IOUtils to simplify accessing web content.

Statistics

Views

Total Views
3,438
Views on SlideShare
3,430
Embed Views
8

Actions

Likes
2
Downloads
63
Comments
0

3 Embeds 8

http://www.slideshare.net 5
https://www.linkedin.com 2
http://www.lmodules.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Apache Utilities At Work   V5 Apache Utilities At Work V5 Presentation Transcript

  • Apache Utilities at Work Tom Marrs
  • Who am I?
    • Background
      • Co-author of JBoss At Work: A Practical Guide (O’Reilly, November 2005)
        • Currently working on 2 nd ed.
      • User Groups
        • Founding President of DOSUG
        • Past President of DJUG
      • Related Experience
        • I’ve been on several bad projects
        • I hate re-inventing the wheel
  • Learning Objectives
    • As a result of this presentation you will learn:
      • To research Open Source utilities before coding
      • To avoid re-inventing the wheel
      • What common utilities are available on Apache
      • How to use Apache utilities on your project
      • How to hide the details
  • Prerequisites
    • Core Java SE
    • The right mindset
      • Leverage existing utilities
  • Session Non-Agenda
    • In this Session, we will not discuss:
    • JavaEE
    • A full-blown application
    • Other cool Apache projects:
      • Web Services (Axis 2)
      • CXF
      • XMLBeans
      • Geronimo
      • ServiceMix
      • ActiveMQ
      • Camel
      • Struts
      • Beehive
  • Session Agenda
    • In this Session we will discuss:
    • Commons Lang
    • Commons Configuration
    • Commons Logging and Log4J
    • POI
    • Commons Email
    • Velocity
    • HttpClient
    • Commons IO
    • Summary
  • Session Roadmap
    • Where are we?
    • Commons Lang
    • Commons Configuration
    • Commons Logging and Log4J
    • POI
    • Commons Email
    • Velocity
    • HttpClient
    • Commons IO
    • Summary
  • Like Nails on a Blackboard …
    • if (myString == null || myString.length() == 0) { // Ugh!!
    • }
    • // OR only slightly better.
    • public class MyUtils {
    • public static boolean isNullOrEmpty(String stringToCheck) {
    • boolean retValue = false;
    • if (stringToCheck == null) {
    • retValue = true;
    • } else if (stringToCheck.length() == 0) {
    • retValue = true;
    • }
    • return retVal;
    • }
  • Commons Lang Overview
    • Enhanced methods and classes for core Java SE ( java.lang )
      • http:// commons.apache.org/lang
    • Includes String, numerical, date/time methods
    • Useful: StopWatch (simple timing test)
    • My favorite – StringUtils:
      • isBlank()
      • isEmpty()
      • isNotBlank()
      • isNotEmpty()
  • With StringUtils
    • // Usage
    • if (StringUtils.isEmpty(myString)) {
    • }
    • if (StringUtils.isBlank(myString)) {
    • }
  • Session Roadmap
    • Where are we?
    • Commons Lang
    • Commons Configuration
    • Commons Logging and Log4J
    • POI
    • Commons Email
    • Velocity
    • HttpClient
    • Commons IO
    • Summary
  • Getting Properties – What we want
    • String emailTo = MyPropertyUtil.getProperty(EMAIL_TO);
  • Rolling Your Own Properties
    • public class MyPropertyUtil {
    • private static final String MY_PROPS_FILE_NAME = “…”; // Use –D to set this at startup.
    • public static String getProperty(String key) throws MyPropertyUtilException {
    • Properties props = null;
    • String value = “”,
    • propsFileName = System.getProperty(MY_PROPS_FILE_NAME);
    • if (StringUtils.isNotBlank(key) &&
    • StringUtils.isNotBlank(propsFileName) {
    • try {
    • // FIXMEE: Move Props File Name & Properties Load to static initializer.
    • // The Thread Context ClassLoader is the ClassLoader used by the creator
    • // of the Thread that runs your code. By using the Thread Context ClassLoader,
    • // we’re guaranteed to load the resource (class or property file) as long as
    • // it's on the application's CLASSPATH.
    • ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader();
    • URL myUrl = myClassLoader.getResource(name);
    • props.load(url.openStream());
    • value = props.getProperty(key);
    • } catch (Throwable th) {
    • }
    • }
    • return value;
    • }
  • Commons Configuration Overview
    • A clean way to configure your application
      • http:// commons.apache.org /configuration
    • It loads the configuration data for you
    • Configuration/Factory (main class) can work with:
      • Properties files
      • XML files
      • JNDI
      • JDBC DataSource
      • System Properties (JVM)
      • .INI files (Windows)
  • Commons Configuration Overview … Cont’d
    • It can handle hierarchical data
    • It can combine multiple data sources (composite):
    • CompositeConfiguration config = new CompositeConfiguration(); config.addConfiguration(new SystemConfiguration()); config.addConfiguration(new
    • PropertiesConfiguration("application.properties"));
  • Properties with Commons Configuration
    • public class MyPropertyUtil {
    • private static final String MY_PROPS_FILE_NAME = “…”; // Use –D to set this at startup.
    • public static String getProperty(String key) throws MyPropertyUtilException {
    • Properties props = null;
    • String value = “”,
    • propsFileName = System.getProperty(MY_PROPS_FILE_NAME);
    • if (StringUtils.isNotBlank(key) &&
    • StringUtils.isNotBlank(propsFileName) {
    • try {
    • // FIXMEE: Move Props File Name & Constructor call to static initializer.
    • Configuration config = new PropertiesConfiguration(propsFileName);
    • value = props.getString(key);
    • } catch (Throwable th) {
    • }
    • }
    • return value;
    • }
    • }
  • Session Roadmap
    • Where are we?
    • Commons Lang
    • Commons Configuration
    • Commons Logging and Log4J
    • POI
    • Commons Email
    • Velocity
    • HttpClient
    • Commons IO
    • Summary
  • Session Roadmap
    • Where are we?
    • Commons Lang
    • Commons Configuration
    • Commons Logging and Log4J
    • POI
    • Commons Email
    • Velocity
    • HttpClient
    • Commons IO
    • Summary
  • POI Overview
    • API to access MS file formats
      • Word
      • PowerPoint
      • Outlook
      • Visio
      • Publisher
      • Excel (HSSF)
      • http:// poi.apache.org
  • Generate an Excel File with POI
    • private static void populateExcelWorksheet(Collection rows, FileOutputStream fileOut) {
    • try {
    • if (rows.size() > 0) {
    • HSSFWorkbook workBook = new HSSFWorkbook();
    • HSSFSheet workSheet = workBook.createSheet(); //Create new workSheet
    • Iterator rowDataIterator = rows.iterator();
    • int rowNum = 0,
    • maxColumnSize = 0;
    • while (rowDataIterator.hasNext()) {
    • LinkedList rowStringsList = (LinkedList) rowDataIterator.next();
    • if (rowStringsList.size() > 0) {
    • HSSFRow row = workSheet.createRow(rowNum);
    • // Create header ONLY once
    • if (rowNum == 0) {
    • maxColumnSize = ExcelUtil.createColumnHeaders(rowStringsList, workBook, row);
    • } else {
    • maxColumnSize = ExcelUtil.fillRowCellValue(rowStringsList, workBook, row);
    • }
    • rowNum++;
    • }
    • }
  • Generate an Excel File with POI … Cont’d
    • workBook.write(fileOut);
    • }
    • }
    • catch (IOException e) {
    • ...
    • }
    • }
  • Generate an Excel File with POI … Cont’d
    • private static short createColumnHeaders(LinkedList stringList, HSSFWorkbook workBook, HSSFRow row) {
    • short columnNum = 0;
    • Iterator iter = stringList.iterator();
    • while (iter.hasNext()) {
    • String value = (String) iter.next();
    • // Create a new font and alter it.
    • HSSFFont font = workBook.createFont();
    • //Customize font
    • customizeExcelFont(font);
    • // Fonts are set into a style so create a new one to use.
    • HSSFCellStyle style = workBook.createCellStyle();
    • style.setFont(font);
    • HSSFCell cell = null;
    • cell = row.createCell(columnNum);
    • cell.setCellStyle(style);
    • cell.setCellValue(value);
    • columnNum++;
    • }
    • return columnNum;
    • }
  • Generate an Excel File with POI … Cont’d
    • private static short fillRowCellValue(LinkedList stringList, HSSFWorkbook workBook, HSSFRow row) {
    • short columnNum = 0;
    • Iterator iter = stringList.iterator();
    • while (iter.hasNext()) {
    • String value = (String) iter.next();
    • HSSFCellStyle style = workBook.createCellStyle();
    • HSSFCell cell = null;
    • // Set background color with light "foreground"
    • // foreground being the filled, not the font color.
    • style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
    • style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    • cell = row.createCell(columnNum);
    • cell.setCellStyle(style);
    • cell.setCellValue(value);
    • columnNum++;
    • }
    • return columnNum;
    • }
  • Session Roadmap
    • Where are we?
    • Commons Lang
    • Commons Configuration
    • Commons Logging and Log4J
    • POI
    • Commons Email
    • Velocity
    • HttpClient
    • Commons IO
    • Summary
  • Commons Email Overview
    • Simple API for sending email
      • http:// commons.apache.org /email
    • Based on JavaMail API (but easier to use)
    • Key Areas:
      • Text
      • HTML
      • Attachments
  • Using JavaMail
    • public void send(MyEmailParams emailParams) throws EmailException { // Send Text
    • try {
    • Session session = getDefaultSession();
    • InternetAddress[] recipientsArr = (InternetAddress[])
    • emailParams.getRecipients().toArray(new InternetAddress[0]);
    • MimeMessage msg = new MimeMessage(session);
    • msg.setFrom(emailParams.getSender());
    • msg.setRecipients(Message.RecipientType.TO, recipientsArr);
    • if (!emailParams.getCcRecipients.isEmpty(){
    • InternetAddress[] ccRecipientsArr = (InternetAddress[])
    • emailParams.getCcRecipients().toArray(newInternetAddress[0]);
    • msg.setRecipients(Message.RecipientType.CC, ccRecipientsArr);
    • }
  • Using JavaMail … Cont’d
    • msg.setSubject(emailParams.getSubject());
    • msg.setText(emailParams.getBody());
    • Transport.send(msg); // Send message
    • } catch (MessagingException me) {
    • }
    • }
    • private Session getDefaultSession() throws EmailException {
    • Properties props = new Properties();
    • String mailSmtpHost = MyPropertyUtil.getProperty(MAIL_SMTP_HOST),
    • mailSmtpPort = MyPropertyUtil.getProperty(MAIL_SMTP_PORT),
    • mailDebug = MyPropertyUtil.getProperty(MAIL_DEBUG, FALSE);
    • props.setProperty(MAIL_TRANSPORT_PROTOCOL, SMTP);
    • props.setProperty(MAIL_SMTP_HOST, mailSmtpPort);
    • props.setProperty(MAIL_SMTP_HOST, mailSmtpHost);
    • props.setProperty(MAIL_DEBUG, mailDebug);
    • Session session = Session.getDefaultInstance(props);
    • return session;
    • }
  • Using Commons Email
    • public static void sendSimpleEmail(EmailParams emailParams)
    • throws MyEmailUtilException { // Send text.
    • try {
    • SimpleEmail email = new SimpleEmail();
    • String hostName = MyPropertyUtil.getProperty(MY_SMTP_SERVER);
    • Iterator toAddrIter = emailParams.getToRecipients().iterator(),
    • ccAddrIter = emailParams.getCcRecipients().iterator();
    • email.setHostName(hostName);
    • email.setFrom(emailParams.getFromEmailAddr(), emailParams.getFromName());
    • while (toAddrIter.hasNext()) {
    • email.addTo((String) toAddrIter.next());
    • }
    • while (ccAddrIter.hasNext()) {
    • email.addCc((String) ccAddrIter.next());
    • }
    • email.setSubject(emailParams.getSubject());
    • email.setMsg(emailParams.getMessage());
    • email.send();
    • } catch (EmailException e) {
    • }
    • }
  • Session Roadmap
    • Where are we?
    • Commons Lang
    • Commons Configuration
    • Commons Logging and Log4J
    • POI
    • Commons Email
    • Velocity
    • HttpClient
    • Commons IO
    • Summary
  • Velocity Overview
    • Templating Engine
      • http://velocity.apache.org
    • Other good tools exist:
      • FreeMarker – http://freemarker.sourceforge.net/
  • Using the Velocity API
    • VelocityEngine velocityEngine = new VelocityEngine();
    • Properties props = new Properties();
    • Template template = null;
    • VelocityContext velocityContext = new VelocityContext();
    • props.setProperty(VelocityEngine.RESOURCE_LOADER, "classpath");
    • props.setProperty("classpath." + VelocityEngine.RESOURCE_LOADER +
    • ".class", ClasspathResourceLoader.class.getName());
    • velocityEngine.init(props);
    • try {
    • StringWriter writer = new StringWriter();
    • template = velocityEngine.getTemplate(MY_TEMPLATE);
    • velocityContext.put(RECIPIENT_NAME, recipientName);
    • velocityContext.put(POLICY_NUMBER, policyNumber);
    • velocityContext.put(REFUND_AMOUNT, refundAmount);
    • template.merge(velocityContext, writer);
    • } catch (…) {
    • }
  • Velocity Template
    • <HTML>
    • <BODY>
    • <p>
    • Dear $recipientName,
    • Due to your safe driving record, we are refunding $refundAmount on your auto policy ($policyNumber).
    • </p>
    • </BODY>
    • </HTML>
  • Session Roadmap
    • Where are we?
    • Commons Lang
    • Commons Configuration
    • Commons Logging and Log4J
    • POI
    • Commons Email
    • Velocity
    • HttpClient
    • Commons IO
    • Summary
  • Commons HttpClient Overview
    • API to help develop an HTTP Client
      • GET / POST
      • Authentication
      • Redirect
      • Proxy
      • Encodings
      • Cookies
    • In its final release (3.x)
      • http://hc.apache.org/httpclient-3.x/index.html
    • To be replaced by HttpComponents:
      • http://hc.apache.org
      • Still in Beta
      • “ Not yet, grasshopper”
  • HttpClient Example
    • public class MyHttpClient {
    • public static String sendPostRequest(MyRequestDto myRequestDto)
    • throws MyHttpClientException {
    • String responseString = null;
    • PostMethod httpPost = null;
    • HttpClient httpClient = new HttpClient();
    • MyHttpClient.setupProxy(httpClient);
    • try {
    • // Create and send the HTTP Request.
    • httpPost = MyHttpClient.setupHttpPostRequest(myRequestDto);
    • httpClient.executeMethod(httpPost);
    • // Get the HTTP Status.
    • if (httpPost.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
    • MyHttpClient.handleRedirect(httpClient, httpPost);
    • }
    • //Get HTTP response.
    • responseString = MyHttpClient.getHttpResponse(httpPost);
  • HttpClient Example … Cont’d
    • } catch (HttpException he) {
    • } catch (IOException ioe) {
    • } finally {
    • httpPost.releaseConnection();
    • }
    • return responseString;
    • }
    • private static void setupProxy(HttpClient httpClient) { // Tunnel through a proxy.
    • String useProxy = MyPropertyUtil.getProperty(USE_PROXY);
    • if (StringUtils.isNotBlank(useProxy) && useProxy.equalsIgnoreCase(YES)) {
    • String proxyHost = MyPropertyUtil.getProperty(MY_PROXY_HOST),
    • proxyPort = MyPropertyUtil.getProperty(MY_PROXY_PORT),
    • proxyRealm = MyPropertyUtil.getProperty(MY_PROXY_REALM),
    • proxyUser = MyPropertyUtil.getProperty(MY_PROXY_USER),
    • proxyPassword = MyPropertyUtil.getProperty(MY_PROXY_PASSWORD);
    • httpClient.getHostConfiguration().setProxy(proxyHost,
    • Integer.parseInt(proxyPort));
    • httpClient.getState().setProxyCredentials(proxyRealm, proxyHost,
    • new UsernamePasswordCredentials(proxyUser, proxyPassword));
    • }
    • }
  • HttpClient Example … Cont’d
    • private static PostMethod setupHttpPostRequest(MyRequestDto myRequestDto) {
    • String myUrl = MyPropertyUtil.getProperty(ConfigConstants.CONFIG_MY_URL);
    • PostMethod httpPost = new PostMethod(myUrl);
    • // Populate HTTP Post Parameters.
    • httpPost.addParameter(REDIR_KEY, REDIR_VALUE);
    • httpPost.addParameter(FIRST_NAME_KEY, myRequestDto.getFirstName());
    • return httpPost;
    • }
    • private static void handleRedirect(HttpClient httpclient, PostMethod httpPost)
    • throws MyHttpClientException {
    • try {
    • Header locationHeader =
    • httpPost.getResponseHeader(HTTP_RESPONSE_LOCATION_HEADER);
    • if locationHeader != null) {
    • String redirectLocation = locationHeader.getValue();
    • httpPost.setURI(redirectLocation);
    • httpClient.executeMethod(httpPost);
    • }
    • } catch (URIException ue) {
    • } // 2 others – HttpException, IOException
    • }
  • HttpClient Example … Cont’d
    • private static String getHttpResponse(PostMethod httpPost)
    • throws MyHttpClientException {
    • String responseString = null;
    • InputStream is = null;
    • try {
    • is = httpPost.getResonseBodyAsStream();
    • BufferedReader br = new BufferedReader(new InputStreamReader(is));
    • StringBuffer responseBuffer = new StringBuffer();
    • String line = null;
    • boolean done = false;
    • while (!done) {
    • line = br.readLine();
    • done = (line == null);
    • if (!done) {
    • responseBuffer.append(line);
    • }
    • }
    • responseString – responseBuffer.toString().trim();
    • } catch (IOException ioe) {
  • HttpClient Example … Cont’d
    • } finally {
    • if (is != null) {
    • try {
    • is.close();
    • } catch (…) {
    • }
    • }
    • }
    • return responseString;
    • }
    • }
  • Session Roadmap
    • Where are we?
    • Commons Lang
    • Commons Configuration
    • Commons Logging and Log4J
    • POI
    • Commons Email
    • Velocity
    • HttpClient
    • Commons IO
    • Summary
  • Commons IO Overview
    • API to simplify IO processing
      • http://commons.apache.org/io
    • Key Areas
      • Utility classes – Perform common tasks
      • Filters – File filter implementations
      • Comparators – For comparing lists of files
      • Streams – Stream, Reader and Writer implementations
    • My Favorite – IOUtils
  • Stream I/O without Commons IO
    • private static String getHttpResponse(PostMethod httpPost)
    • throws MyHttpClientException {
    • String responseString = null;
    • InputStream is = null;
    • try {
    • is = httpPost.getResonseBodyAsStream();
    • BufferedReader br = new BufferedReader(new InputStreamReader(is));
    • StringBuffer responseBuffer = new StringBuffer();
    • String line = null;
    • boolean done = false;
    • while (!done) {
    • line = br.readLine();
    • done = (line == null);
    • if (!done) {
    • responseBuffer.append(line);
    • }
    • }
    • responseString – responseBuffer.toString().trim();
    • } catch (IOException ioe) {
  • Stream I/O without Commons IO … Cont’d
    • } finally {
    • if (is != null) {
    • try {
    • is.close();
    • } catch (…) {
    • }
    • }
    • }
    • return responseString;
    • }
    • }
  • Stream I/O with Commons IO
    • private static String getHttpResponse(PostMethod httpPost)
    • throws MyHttpClientException {
    • String responseString = null;
    • InputStream is = null;
    • try {
    • is = httpPost.getResonseBodyAsStream();
    • responseString = IOUtils.toString(is);
    • } catch (IOExeception ioe) {
    • } finally {
    • IOUtils.closeQuietly(is);
    • }
    • return responseString;
    • }
  • Session Roadmap
    • Where are we?
    • Commons Lang
    • Commons Configuration
    • Commons Logging and Log4J
    • POI
    • Commons Email
    • Velocity
    • HttpClient
    • Commons IO
    • Summary
      • Conclusions
      • Questions
      • Resources
  • Conclusions
    • Apache has a wealth of useful APIs
    • SourceForge (and others) also have great APIs
    • Use thin wrappers to hide the details
    • Research Open Source before coding
      • Don’t re-invent the wheel
      • Google, anyone? 
  • Questions?
    • Tom Marrs
    • Principal Architect
    • [email_address]
  • Resources
    • Apache Main Page: http:// www.apache.org
    • Apache Commons: http://commons.apache.org
      • Lang: http://commons.apache.org/lang
      • Configuration: http://commons.apache.org/configuration
      • Logging: http:// commons.apache.org /logging
      • Email: http://commons.apache.org/email
      • IO: http://commons.apache.org/io
      • HttpClient: http://hc.apache.org/httpclient-3.x/index.html
    • Apache Log4J: http://logging.apache.org/log4j/1.2/index.html
    • Apache Velocity: http://velocity.apache.org
    • FreeMarket: http://freemarker.sourceforge.net
    • Apache POI: http://poi.apache.org
    • Apache HttpComponents: http:// hc.apache.org