Did this blog help you solve a problem? Tip me with Bitcoins at: 17dQktcAmU4urXz7tGk2sbuiCqykm3WLs6

Sunday, December 25, 2011

Serialize an Object to a JSON String in Java Using Gson

To serialize a specified object or bean into its equivalent Json representation in Java we can use Google's GSON library. First create a serializable object and set its fields to some values. Next new up a com.google.gson.Gson object. Finally call the Gson objects's toJson() method passing it your serializable object.


import com.google.gson.Gson;

// The following example code demonstrates how to serialize a Bean
// object to a JSON String using the GSON library from Google.

public class Bean2JsonUsingGson {

    public static void main(String[] args) {

        SampleBean sampleBean = new SampleBean(3.14, "xyz", 42);
        Gson gson = new Gson();
        String json = gson.toJson(sampleBean);

        System.out.println(json);
    }

    static class SampleBean {

        private double value1;
        private String value2;
        private int value3;

        // Constructor
        SampleBean(double value1, String value2, int value3) {
            this.value1 = value1;
            this.value2 = value2;
            this.value3 = value3;
        }
    }
}

Here is the output of the example program:
{"value1":3.14,"value2":"xyz","value3":42}

Piece of cake!!!

Tuesday, December 6, 2011

Set Font Style in an Excel File Using Java and POI

To create an Excel file and style a cell's font in Java we can use Apache's POI library. First a HSSFWorkbook object is created and a HSSFSheet object is added. A HSSFRow is added to the sheet, and a HSSFCell is added to the row. To sent the font style of a cell's content, a HSSFFont object is created, customized, and applied to a HSSFCellStyle object which in turn is applied to a cell in the spreadsheet. The following sample code was successfully tested with POI versions 3.6 and 3.7.



import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

// The following example code demonstrates how to create an Excel  
// file using the org.apache.poi library and style the font in a cell.

public class StyleExcelFileFont {

    public static void main(String[] args) {

        HSSFWorkbook workbook = new HSSFWorkbook();

        HSSFSheet firstSheet = workbook.createSheet("Sheet 1");

        // Write a String in Cell 2B
        HSSFRow row1 = firstSheet.createRow(1);
        HSSFCell cell2B = row1.createCell(1);
        cell2B.setCellValue(new HSSFRichTextString("Sample String"));

        // Style Font in Cell 2B
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle = workbook.createCellStyle();
        HSSFFont hSSFFont = workbook.createFont();
        hSSFFont.setFontName(HSSFFont.FONT_ARIAL);
        hSSFFont.setFontHeightInPoints((short) 16);
        hSSFFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        hSSFFont.setColor(HSSFColor.GREEN.index);
        cellStyle.setFont(hSSFFont);
        cell2B.setCellStyle(cellStyle);

        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(new File("/Temp/Test5.xls"));
            workbook.write(fileOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.flush();
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}


Here is a screenshot of the generated Excel File:


Piece of cake!!!

Monday, December 5, 2011

Set Background Color and Add Border to a Cell in an Excel File Using Java and POI

To create an Excel file and style a cell with a background color and a border in Java we can use Apache's POI library. First a HSSFWorkbook object is created and a HSSFSheet object is added. A HSSFRow is added to the sheet, and a HSSFCell is added to the row. To add a String to the cell, a HSSFRichTextString is used. To add a background color to the cell and add a border, a HSSFCellStyle object is applied to the cell. The following sample code was successfully tested with POI versions 3.6 and 3.7.



import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

// The following example code demonstrates how to create an Excel  
// file using the org.apache.poi library and style the cell by setting
// its background color and adding a border.

public class StyleExcelFileCells {

    public static void main(String[] args) {

        HSSFWorkbook workbook = new HSSFWorkbook();

        HSSFSheet firstSheet = workbook.createSheet("Sheet 1");

        // Write a String in Cell 2B
        HSSFRow row1 = firstSheet.createRow(1);
        HSSFCell cell2B = row1.createCell(1);
        cell2B.setCellValue(new HSSFRichTextString("Sample String"));

        // Style Cell 2B
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle = workbook.createCellStyle();
        cellStyle.setFillForegroundColor(HSSFColor.YELLOW.index);
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        cellStyle.setBorderTop((short) 1); // single line border
        cellStyle.setBorderBottom((short) 1); // single line border
        cell2B.setCellStyle(cellStyle);

        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(new File("/Temp/Test4.xls"));
            workbook.write(fileOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.flush();
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Here is a screenshot of the generated Excel File:


Piece of cake!!!

Sunday, December 4, 2011

Read Contents of an Excel File using Java and POI

To get the content from an Excel file in Java we can use Apache's POI library. First a file is imported from disk passing a FileInputStream object to a org.apache.poi.hssf.usermodel.HSSFWorkbook object. In this example only the first sheet was parsed. To access the cell content of the entire sheet, each row is iterated followed by iterating over each cell in the row. For each cell, the content is simply read using the getStringCellValue() method from the HSSFCell object. The following sample code was successfully tested with POI versions 3.6 and 3.7.

Here is a screenshot of the test file I used for this example:





import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

// The following example code demonstrates how to parse the contents
// of all cells in the first sheet of an Excel spreadsheet file

public class ReadExcelFile {

    public static void main(String[] args) {

        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("/Temp/Test1.xls");

            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);

            HSSFSheet sheet = workbook.getSheetAt(0);

            Iterator rows = sheet.rowIterator();
            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();

                Iterator cells = row.cellIterator();
                while (cells.hasNext()) {
                    HSSFCell cell = (HSSFCell) cells.next();

                    System.out.println(cell.getStringCellValue());
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Here is the output of the program:
Hello
There
Piece of cake!!!

Crow Vending Machine - Crows Find Coins for Peanuts



Monday, November 28, 2011

Create an Excel File using Java and POI

To create an Excel file in Java we can use Apache's POI library. The example file created by the following example code demostrates how to add a String, a Date, a Boolean, and a Number to specified cells in the spreadsheet. First a HSSFWorkbook object is created and a HSSFSheet object is added. A HSSFRow is added to the sheet, and a HSSFCell is added to the row. To add a String to the cell, a HSSFRichTextString is used. To add a Date to the cell, a HSSFCellStyle is used in conjunction with a HSSFDataFormat object appied to a Date object. A Boolean value and a Number value are added directly to the cell. The following sample code was successfully tested with POI versions 3.6 and 3.7.


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

// The following example code demonstrates how to create an Excel  
// file using the org.apache.poi library. In the sample file,
// a String, Date, Boolean, and Number are added to individual cells
// in the spreadsheet.

public class CreateExcelFile {

    public static void main(String[] args) {

        HSSFWorkbook workbook = new HSSFWorkbook();

        HSSFSheet firstSheet = workbook.createSheet("Sheet 1");

        // Write a String in Cell 1A
        HSSFRow row1 = firstSheet.createRow(0);
        HSSFCell cell1A = row1.createCell(0);
        cell1A.setCellValue(new HSSFRichTextString("Sample String"));

        // Write a Date in Cell 2B
        HSSFRow row2 = firstSheet.createRow(1);
        HSSFCell cell2B = row2.createCell(1);
        cell2B.setCellValue(new Date());
        // Format the Date so it looks like a date
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle = workbook.createCellStyle();
        cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
        cell2B.setCellStyle(cellStyle);

        // Write a Boolean in Cell 3C
        HSSFRow row3 = firstSheet.createRow(2);
        HSSFCell cell3C = row3.createCell(2);
        cell3C.setCellValue(true);

        // Write a Number in Cell 4D
        HSSFRow row4 = firstSheet.createRow(3);
        HSSFCell cell4D = row4.createCell(3);
        cell4D.setCellValue(3.14);

        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(new File("/Temp/Test3.xls"));
            workbook.write(fileOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.flush();
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


Here is a screenshot of the generated Excel File:


Piece of cake!!!

Saturday, November 19, 2011

Get Sheet Names from an Excel File Using Java and POI

To get the sheet names from an Excel file in Java we can use Apache's POI library. First a file is imported from disk passing a FileInputStream object to a org.apache.poi.hssf.usermodel.HSSFWorkbook object. Looping over each sheet in the Excel spreadsheet, the sheet name is determined using the workbook.getSheetName() method. The following sample code was successfully tested with POI versions 3.6 and 3.7.

Here is a screenshot of the test file I used for this example:



import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

// The following example code demonstrates how to get the Sheet
// names in an Excel spreadsheet file

public class GetExcelSheetNames {

    public static void main(String[] args) {

        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("/Temp/Test1.xls");

            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);

            // for each sheet in the workbook
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {

                System.out.println("Sheet name: " + workbook.getSheetName(i));
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}


Here is the output of the program:
Sheet name: Sheet1
Sheet name: Sheet2
Sheet name: Sheet3
Piece of cake!!!

Thursday, October 20, 2011

Validate a URL in Java

To validate a URL String in Java we can use the java.net.URL object. First a URL object is created, passing the URL String to its constructor. If the URL String is malformed or invalid, a MalformedURLException will be thrown indicating a malformed URL. Next the URL is converted to a URI using the toURI() method. If URL is not formatted strictly according to to RFC2396 and cannot be converted to a URI, a URISyntaxException will be thrown indicating a malformed URL. The following code demonstrates how to validate a URL String.


package url;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

//
// The following example code demonstrates how to
// check if a given URL is valid or not.
//
public class CheckURLValidity {

    public static void main(String[] args) {

        String testURLString = "https://thermodynamiccomputing.com/main?&t=20&f=52";
        System.out.println(isValidURL(testURLString));

        testURLString = "http6://thermodynamiccomputing.com/";
        System.out.println(isValidURL(testURLString));

        testURLString = "http://thermodynamiccomputing.com/   dd";
        System.out.println(isValidURL(testURLString));

        testURLString = "http://<thermodynamiccomputing.com";
        System.out.println(isValidURL(testURLString));
    }

    private static boolean isValidURL(String pUrl) {

        URL u = null;
        try {
            u = new URL(pUrl);
        } catch (MalformedURLException e) {
            return false;
        }
        try {
            u.toURI();
        } catch (URISyntaxException e) {
            return false;
        }
        return true;
    }

}

Here is the output of the example code:
true
false
false
false
Piece of cake!!!

Sunday, October 16, 2011

Get Domain Name from a URL String in Java

To get the domain name or sub-domain name from a URL String in Java we can use the java.net.URL object. First a URL object is created, passing the URL String to its constructor. If the URL String is malformed or invalid, a MalformedURLException will be thrown. The getHost() method is then used to pull only the part of the URL containing the domain name. The following code demonstrates getting the domain name from a URL String.


import java.net.MalformedURLException;
import java.net.URL;

//
// The following example code demonstrates how to
// get the domain name from a URL String
//
public class GetDomainNameFromURL {

    public static void main(String[] args) throws MalformedURLException {

        String testURLString = "https://thermodynamiccomputing.com/main?&t=20&f=52";
        URL lURL = new URL(testURLString);
        String hostName = lURL.getHost();
        System.out.println(hostName);

        testURLString = "http://www.thermodynamiccomputing.com/main?&t=20&f=52";
        lURL = new URL(testURLString);
        hostName = lURL.getHost();
        System.out.println(hostName);

        testURLString = "http://www.sub.thermodynamiccomputing.com/main?&t=20&f=52";
        lURL = new URL(testURLString);
        hostName = lURL.getHost();
        System.out.println(hostName);

    }
}



Here is the output of the example code:
thermodynamiccomputing.com
www.thermodynamiccomputing.com
www.sub.thermodynamiccomputing.com
Piece of cake!!!

Wednesday, August 3, 2011

Java Web Apps - Integrating Charts into a Servlet

Adding a chart to a Java web application is really easy to do - thanks to the open source Java charting API XChart. XChart's charting library lets you very easily make charts an integrate them into all your Java-based applications. In this blog post, I am going to demonstrate how to add charts to a Java web application. If you want to integrate XChart charts in your Java applications, download the JAR here. You can also see more XChart exmaples here.

The Basic Idea

The basic idea for adding XChart charts to a web app is as follows. First, you have a JSP or HTML page with a link on it that will request a new page with a chart. The new JSP or HTML page that is generated has an img tag in it with a reference to an in-memory chart stored in a HashMap in a Servlet that maps the reference and the chart. When the page reloads, the browser parses the img tag and subsequently requests the image from the Servlet. The Servlet then streams the chart to the browser and it is rendered on the page.

An Example

If the basic idea explanation above did not make complete sense, the following example should make it clear.

Step 1: ChartServlet. This Servlet does one of two things in the doGet() method, depending on the request. If it receives and 'action', it generates a chart. Here you could pass in different 'action' parameters to create different charts if you want. If it receives a 'chart_id', it pulls the chart from CHART_MAP and streams it out to the browser.
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xeiam.xcharts.BitmapEncoder;
import com.xeiam.xcharts.Chart;
import com.xeiam.xcharts.QuickChart;

@javax.servlet.annotation.WebServlet(name = "ChartServlet", urlPatterns = { "/chart" })
public class ChartServlet extends HttpServlet {

    private static Map<String, Chart> CHART_MAP = new HashMap<String, Chart>();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // generate the Chart
        String action = request.getParameter("action");
        if (action != null && !action.equals("null")) {
            String chartId = generateRandomChart();
            request.setAttribute("chart_id", chartId);
            request.getRequestDispatcher("/chart.jsp").forward(request, response);
        }

        // Fetch the Chart
        String chartId = request.getParameter("chart_id");
        if (chartId != null && !chartId.equals("null")) {

            Chart chart = CHART_MAP.get(chartId);
            if (chart != null) {
                response.setContentType("image/png");
                ServletOutputStream out = response.getOutputStream();
                try {
                    BitmapEncoder.streamPNG(out, chart);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                out.close();
                chart = null;
                CHART_MAP.remove(chartId);
            } else {
                System.err.println("CHART NOT FOUND!!!");
            }
        }
    }

    // generate the chart
    public static String generateRandomChart() {
        Chart chart = QuickChart.getChart("XChart Sample - Random Walk", "X", "Y", null, null, getRandomWalk(105));
        String uuid = UUID.randomUUID().toString();
        CHART_MAP.put(uuid, chart);
        return uuid;
    }

    // generate random walk data set
    private static double[] getRandomWalk(int numPoints) {
        double[] y = new double[numPoints];
        y[0] = 0;
        for (int i = 1; i < y.length; i++) {
            y[i] = y[i - 1] + Math.random() - .5;
        }
        return y;
    }
}

Step 2: Chart JSP. To request and display the Chart, the JSP contains two DIVs - one for a link to request the chart and one for a IMG tag to display the Chart image. I named this JSP chart.jsp.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
 <head>
     <meta http-equiv="content-type" content="text/html;charset=utf-8" />
     <% String chartId = (String) request.getAttribute("chart_id");%>
 </head>
    <body>
     <div>
      <a href="<%=request.getContextPath()%>/chart?action=whatever">Generate Chart</a>
     </div>
     <%if(chartId != null){ %>
     <div>
      <img src="<%=request.getContextPath()%>/chart?chart_id=<%=chartId %>"/> 
     </div>
  <%} %>
    </body>
</html>

Step 3: Test it. Now deploy your webapp with the new Servlet and JSP and in your browser go to: http://localhost/YourWebAppContext/chart.jsp

After clicking on 'Generate Chart' your chart will appear on the web page. Each time you click it, a new chart will show up.

Piece of Cake!!!

See also: Java XChart Library Now Supports Error Bars

Tuesday, August 2, 2011

Using Yank as a MySQL Persistence Layer in Your Java Application

Yank is a lightweight JDBC persistence layer API for any type of Java application. Looking at the JavaDocs, we can see that it only has a grand total of 4 classes. I love APIs like this because I can dig into the JavaDocs and very quickly understand the design and structure, making it a snap to get up and running with working code. Yank makes it easy to interact with JDBC databases in a structured, organized, and clean-cut approach. If you don't use a persistence layer like this, you quickly find that your database querying code becomes an unmanageable mess.


The Basic Idea

Each table in your database has a corresponding Java object. The object has private fields with names and data types that match the column names of your table. In other words, the Bean is a Java object that is a one-to-one mapping of a single row in your table.

The next thing you need is a Data Access Object, or DAO, for the Bean. In this class, you create methods for interacting with the table. In each method, with only a few lines of code, you create the specific SQL query you want and use the com.xeiam.yank.DBProxy to handle the query for you.

The advantage of this is that your code for interacting with the database is all wrapped up in just two very simple classes for each table in your database - a POJO and a DAO. And because the actual nitty-gritty details of handling database connections and results sets in handled for you in com.xeiam.yank.DBProxy, you don't have to worry about coding that yourself. It saves time and prevents errors.

An Example

In order to make the relationship between a table, the Bean, and the DAO absolutely clear, I will walk through an example.

Step 0: Create a Database. We'll call the database "Yank", which matches the jdbc URL in step 4 exactly. How you create the database may be different than how I do it, but here's what I did at the command line:

$ /usr/local/mysql/bin/mysql -u root -p
mysql$ create database Yank;
mysql$ show databases;
mysql$ exit;

Step 1: Create a table. I'll be using similar code to the Yank example code for this. In MySQL create a database called 'Yank' and add the following table called 'Books' to it:

CREATE TABLE `Books` (
`TITLE` varchar(42) DEFAULT NULL,
`AUTHOR` varchar(42) DEFAULT NULL,
`PRICE` double DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Step 2: Create a Bean for the table. First, add the fields corresponding to the column names. The names MUST match, although case doesn't matter. Finally add getters and setters for all the fields. In Eclipse, these can be easily auto generated.
package com.xeiam.yank.demo;

/**
 * A class used to represent rows in the BOOKS table 
* Note: class member naming tip: data type and name must match SQL table!
* Note: DBUtils uses reflection to match column names to class member names.
* Class members are matched to columns based on several factors: *
    *
  • set* methods that match the table's cloumn names (i.e. title <--> setTitle()). The name comparison is case insensitive.
  • *
  • The columns are matched to the object's class members
  • *
  • If the conversion fails (i.e. the property was an int and the column was a Timestamp) an SQLException is thrown.
  • *
* * @author timmolter */ public class Book { private String title; private String author; private double price; /** Pro-tip: In Eclipse, generate all getters and setters after defining class fields: Right-click --> Source --> Generate Getters and Setters... */ public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } /** Pro-tip: In Eclipse, generate a toString() method for a class: Right-click --> Source --> Generate toString()... */ @Override public String toString() { return "Book [title=" + title + ", author=" + author + ", price=" + price + "]"; } }

Step 3: Create a DAO class. I'll add two methods to it - one for inserting a Book and one for selecting all the books in the table.
package com.xeiam.yank.demo;

import java.util.List;

import com.xeiam.yank.DBProxy;

/**
 * DAO (Data Access Object) Class for BOOKS table. 
* This is where you create your own methods for SQL interaction with a database table.
* Each table in your database should have it's own DAO Class.
* * @author timmolter */ public class BooksDAO { public static int insertBook(Book book) { Object[] params = new Object[] { book.getTitle(), book.getAuthor(), book.getPrice() }; String SQL = "INSERT INTO BOOKS (TITLE, AUTHOR, PRICE) VALUES (?, ?, ?)"; return DBProxy.executeSQL("myconnectionpoolname", SQL, params); } public static List selectAllBooks() { String SQL = "SELECT * FROM BOOKS"; return DBProxy.queryObjectListSQL("myconnectionpoolname", SQL, Book.class, null); } }

Step 4: Use your new persistence layer in your code. For demonstration purposes, I'll create a simple class that calls the two methods in the DAO and prints out the results. In this class, first define the connection properties. The Yank API needs to know how to connect to your database: which JDBC driver, which table, which user, and the password, and how many Connections in the connection pool. For the last four properties, the word 'local' corresponds to the name of the connection pool. Notice in the DAO class that the DBProxy method calls are passed 'local' as the first parameter. This feature allows you to have multiple Connection pools in your application if you want.
import com.xeiam.yank.DBConnectionManager;

public class BooksExample {

    public static void main(String[] args) {

        Properties props = new Properties();
        props.setProperty("driverclassname", "com.mysql.jdbc.Driver");
        props.setProperty("local.url", "jdbc:mysql://localhost:3306/Yank");
        props.setProperty("local.user", "root");
        props.setProperty("local.password", "");
        props.setProperty("local.maxconn", "5");

        DBConnectionManager.INSTANCE.init(props);

        // Insert Book
        Book book = new Book();
        book.setTitle("Cryptonomicon");
        book.setAuthor("Neal Stephenson");
        book.setPrice(23.99);
        BooksDAO.insertBook(book);

        // Select All Books
        List allBooks = BooksDAO.selectAllBooks();
        for (Book book1 : allBooks) {
            System.out.println(book1.toString());
        }

        DBConnectionManager.INSTANCE.release();
    }
}
Here's the output:
Book [title=Cryptonomicon, author=Neal Stephenson, price=23.99]
Piece of Cake!!!

Additional Information

As you can see, it is really easy to use Yank to push and pull data into and out of a database from your Java application. On the Yank example page, you can see a few more advanced capabilities as well. You can set up your database connection information in a properties file rather than directly in your Java code if you want. You can also keep all you SQL statements in a single properties file and access them using the DBProxy.*SQLKey() methods. In addition to the insert and select statements shown above, you can query in anyway you want: update, insert ignore, replace, batch query, etc.

Yank has 3 dependencies, so in order to integrate Yank into your application you'll need not only the Yank jar on your classpath, but also these:
Apache Commons DBUtils
Simple Logging Facade for Java (SLF4J)
MySQL Connector/J

And remember, this will work with any type of database that has a JDBC driver, such as PostgreSQL. You would just have to add the appropriate JDBC driver to your classpath, and modify the connection properties appropriately.

Monday, August 1, 2011

Pull Entire Directory Using SCP on a Mac

The following command can be used to fetch an entire directory including all the files from a remote host to a local directory.

$ scp -r username@host:/fully/qualified/path/on/remote/host/ /fully/qualified/path/on/local/host/

An alternative approach is to reference a directory relative to the user's account.

$ scp -r username@host:user's/path/on/remote/host/ user's/path/on/local/host/

Or using '.', the files will be copied to the current working direcory

$ scp -r username@host:user's/path/on/remote/host/ .

To use a wildcard to fetch only certain file types use the * syntax.

$ scp -r username@host:user's/path/on/remote/host/*.png .

Piece of Cake!!!

Thursday, July 28, 2011

Executing a Shell Script from Java

Using MySQL and Java? Check out an easier way: Yank

Today I figured out how to run a bash script from Java for the first time, and I wanted to jot down the essential steps needed to get it all working along with some things to watch out for. This is specific to mysqldump, but it should work for any script you have and want to run in the bash shell. This also demonstrates how to pass in an argument to the script.

Step 1: The first order of business in running a shell script from within a Java program is to setup a function that you can pass shell commands to. The following method takes a command as an argument and runs it in a bash shell. I put this method in a class called ExecUtils.

public static void execShellCmd(String cmd) {
        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(new String[] { "/bin/bash", "-c", cmd });
            int exitValue = process.waitFor();
            System.out.println("exit value: " + exitValue);
            BufferedReader buf = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            while ((line = buf.readLine()) != null) {
                System.out.println("exec response: " + line);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

Step 2: Creating the script is pretty straight forward, but there are a few things to watch out for. For a bash script, you need #!/bin/bash on the first line. To pass an argument into the script, I used FILENAME=$1 and later used $FILENAME in my command. $1 represents the first argument. I also exit the script with the value returned from the mysqldump command.
#!/bin/bash
FILENAME=$1
exit mysqldump --user=username --password=pass --databases DB_XYZ | gzip -9 > temp/dump/$FILENAME
For this example, I placed my file, dump.sh in the directory '/temp'. One more thing to check is that your script is executable. Run this command in the terminal to make it executable:
sudo chmod 777 /temp/dump.sh

Step 3: Now just run the script through the execShellCmd method.
package mysql;

import java.util.Date;

import com.xeiam.utils.ExecUtils;

/**
 * This class demonstrates running a shell script from within Java
 */
public class MySQLDumpScript {

    public static void main(String[] args) {

        String fileName = "DUMPFILE.sql.gz";
        String shellCommand = "/temp/dump.sh " + fileName;
        ExecUtils.execShellCmd(shellCommand);
    }
}

You should now see a file called DUMPFILE.sql.gz in /temp/dump. Piece of Cake!!!

Executing mysqldump from Java and Pitfalls to Avoid

Using MySQL and Java? Check out an easier way: Yank

I just spent a lot of time figuring out how to run mysqldump from within my Java program. I came across several different problems. Each time it was not working, there was a different problem and solution, and never really easy to debug. Sometimes it would just not work at all. Sometimes it would create an empty file. In this blog, I document how I finally got it all to work and which pitfalls I fell into along the way.

Step 1: The first order of business in running mysqldump from within a Java program is to setup a function that you can pass shell commands to. The following method takes a command as an argument and runs it in a bash shell. I put this method in a class called ExecUtils.

public static void execShellCmd(String cmd) {
        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(new String[] { "/bin/bash", "-c", cmd });
            int exitValue = process.waitFor();
            System.out.println("exit value: " + exitValue);
            BufferedReader buf = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            while ((line = buf.readLine()) != null) {
                System.out.println("exec response: " + line);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

Step 2: Now we just need to run the proper mysqldump command through our execShellCmd method. The following shellCommand String will backup the database DB_XYZ and write it to a file called /test.sql.gz.
private void mysqldump() {

        String shellCommand = "mysqldump --user=username --password=pass --databases DB_XYZ | gzip -9 > " + "/test.sql.gz";
        ExecUtils.execShellCmd(shellCommand);

    }

Pitfall 1: LOCK TABLES user permission. In order for the mysqldump command to work, the user which you define using the --user argument must have the LOCK TABLES permission. If that user does not have that privilege, you can add it with the following commands in MYSQL:

GRANT lock tables ON DB_XYZ.* TO 'username'@'localhost' IDENTIFIED BY 'pass';
GRANT lock tables ON DB_XYZ.* TO 'username'@'%' IDENTIFIED BY 'pass';
flush privileges;

Pitfall 2: False file permissions. In order for the /test.sql.gz to be written to disk, the user in which the Java program is running must have permission to write a file there. In this case: '/'.

Pitfall 3: mysqldump cannot be found on system PATH. In the command above I used the unqualified 'mysqldump' as the command to execute. You could just as easily replace 'mysqldump' with the fully qualified version: '/usr/local/mysq/bin/mysqldump', as in my case on my local machine. One possible problem with this approach though is that your Java code doesn't become portable to other platforms. For example, on one of my Linux machines, mysqldump is found here: '/usr/bin/mysqldump'. To be able to use just 'mysqldump' you have to make sure it is on the system PATH. This is how you do that...

On my Linux machine:
$ locate mysqldump
/usr/bin/mysqldump

OK, using the command 'locate' I determined that 'mysqldump' is located in the directory '/usr/bin'. Now let's check if '/usr/bin' is on PATH:
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

Yes, it is. No problem using 'mysqldump' directly.

On my Mac machine:
$ locate mysqldump
/usr/local/mysql/bin/mysqldump

OK, using the command 'locate' I determined that 'mysqldump' is located in the directory '/usr/local/mysql/bin'. Now let's check if '/usr/local/mysql/bin' is on PATH:
$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin

Nope. Now we need to add /usr/local/mysql/bin to PATH on Mac OS X (10.5 Leopard). Here's how I did that. I created a file in /etc/paths.d called mysqldump
$ sudo vim /etc/paths.d/mysqldump
and added the following text:
/usr/local/mysql/bin
and restarted my computer.

Now let's check again if '/usr/local/mysql/bin' is on PATH:
$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin:/usr/local/mysql/bin

Yup, all good!


Monday, May 9, 2011

Intel's Tri-Gate Process for Their Future Transistors

A Random Tomcat Error

Want to integrate charts into your webapp? Check out XChart.

Here's another nano-blog that will hopefully help someone solve a Tomcat error that I was dealing with today:

java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addServlet

The problem was that I was deploying catalina.jar within my .war file. Once I fixed up my build file to not include the catalina.jar file in the .war file, the error went away.

Saturday, May 7, 2011

Java One-Jar Example Using Ant and Eclipse

One-Jar is an amazingly clever and simple tool that let's you package an entire Java application containing external jar files all into one Jar. But just like anything, it's simple and easy to use only AFTER you figure out how to get it working. Having just worked through using One-jar, I thought I'd lay out the steps needed to successfully use One-jar in Eclipse using Ant.

Step 1: Get One-jar. Go to the One-jar download page on Source-Forge and download the "one-jar-ant-task-0.97.jar" file.


Step 2: Integrate this jar into Eclipse so that when running Ant, Ant has access to the one-jar Ant Task. If you don't do this, you may get and error like:

/eclipse/helloworkspace/HelloOneJar/build.xml:54: Problem: failed to create task or type one-jar
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any / declarations have taken place.

Move the jar to your Eclipse plugins folder. For me, that was in /eclipse/plugins. You can really put this anywhere you want, but it made sense for me to put it here as Eclipse stores a lot of jars it needs here.

In Eclipse, open preferences, drill down to Ant-->Runtime, highlight Ant Home Entries, and click "Add external JARs...". Find the one-jar JAR, and add it here.


Step 3: Create Ant build file. For demonstration and testing purposes, I created a tiny Java Project called HelloOneJar which contains a Hello class. All the Hello class does is log two messages using log4j. Here is a screenshot of the project structure and the Hello class:


build.properties

project.name=hello-onejar
lib.dir=lib
src.dir=src
build.dir=build
dist.dir=dist

build.xml

<?xml version="1.0"?>
<project name="hello-onejar" default="onejar" basedir=".">
 
    <taskdef name="one-jar" classname="com.simontuffs.onejar.ant.OneJarTask" onerror="report" />
 
    <property file="build.properties"/>
 
    <tstamp>
       <format property="timestamp" pattern="yyyy-MM-dd HH:mm:ss" />
    </tstamp>
 
    <path id="classpath">
     <fileset dir="${lib.dir}" />
    </path>

    <target name="clean">
        <echo>Cleaning the ${build.dir}</echo>
        <delete dir="${build.dir}"/>
        <delete dir="${dist.dir}"/>
    </target>

    <target name="init" depends="clean">
        <echo>Creating the build directory</echo>
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${dist.dir}"/>
    </target>
 
    <target name="compile" depends="init">
        <echo>Compile the source files</echo>
        <javac srcdir="${src.dir}" destdir="${build.dir}" debug="on">
            <classpath refid="classpath"/>
        </javac>
    </target>

 <target name="mainjar" depends="compile">
       <jar jarfile="${dist.dir}/main.jar">
         <manifest>
            <attribute name="Built-By" value="${user.name}"/>
             <attribute name="Build-Date" value="${timestamp}"/>                 
             <attribute name="Main-Class" value="com.hello.Hello"/> 
           </manifest>
          <fileset dir="${build.dir}">
              <include name="**/*.class"/>
          </fileset>
        </jar>
 </target>

    <target name="onejar" depends="mainjar">
     
        <!-- Construct the One-JAR file -->   
  <one-jar destfile="${dist.dir}/${project.name}.jar">
         <main jar="${dist.dir}/main.jar">
         </main>
         <lib>
             <fileset dir="${lib.dir}" />
         </lib>
  </one-jar>
    </target>
 
</project>
The follwing parts are very important!

<taskdef name="one-jar" classname="com.simontuffs.onejar.ant.OneJarTask" onerror="report" />
without that line, the one-jar task is undefined. Remember to tell Ant where this class is in step 2!

<attribute name="Main-Class" value="com.hello.Hello"/>
without this in the manifest you may get an error like this when you run your jar:
Exception in thread "main" java.lang.Exception: hello-onejar.jar main class was not found (fix: add main/main.jar with a Main-Class manifest attribute, or specify -Done-jar.main.class=), or use One-Jar-Main-Class in the manifest
at com.simontuffs.onejar.Boot.run(Boot.java:327)
at com.simontuffs.onejar.Boot.main(Boot.java:168)

Step 4: Run the Ant build. In this example, I run the "onejar" task that I defined. This build file first creates a jar called main.jar that contains my application code. This jar has it's own manifest and could be ran separately. The onejar target wraps main.jar in hello-one-jar.jar and packages all the jar(s) in the lib folder inside. Both jars are created in the ./dist folder.

Step 5: Inspect the built jar. This is how I did this on my Mac. First I created a folder: /HelloOneJar and moved the hello-onejar.jar into it. Then, in terminal:
cd /HelloOneJar
jar -xvf hello-onejar.jar
Now the jar is unpacked and we can see what's in it. You can do the same for main.jar and verify that the manifest file was created correctly.

Step 6: Run the jar and make sure it all works. This is how I did this on my Mac. First I created a folder: /HelloOneJar and moved the hello-onejar.jar into it. Then, in terminal:
cd /HelloOneJar/
java -jar hello-onejar.jar

The following output appeared in Terminal:
0 [main] INFO com.hello.Hello - Hello.
1 [main] INFO com.hello.Hello - Bye Bye.

Exactly what I expected!

Piece of Cake!!

Thursday, May 5, 2011

Get Day of Week from Date Object in Java

To get the day of the week from a java.util.Date object, we can use the java.text.SimpleDateFormat class. First, create a Date object. Next, create a SimpleDateFormat instance using the getDateInstance() method, passing the String "E" or "EEEE" as the argument. Get the day of the week as a String using the format() method passing in the java.util.Date object. To get the weekday in numerical format, we can use the java.util.Calendar object's get method passing in Calendar.DAY_OF_WEEK.

Get Day of Week from Date in Java - Example Code

//
// The following example code demonstrates how to
// print out the Day of the Week from a Date object.
//
public class GetDayFromDate {

    public static void main(String[] args) {

        Date now = new Date();

        SimpleDateFormat simpleDateformat = new SimpleDateFormat("E"); // the day of the week abbreviated
        System.out.println(simpleDateformat.format(now));

        simpleDateformat = new SimpleDateFormat("EEEE"); // the day of the week spelled out completely
        System.out.println(simpleDateformat.format(now));

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); // the day of the week in numerical format

    }
}

Here is the output of the example code:
Wed
Wednesday
4

Wednesday, May 4, 2011

Get Month from Date Object in Java

To get the month from a java.util.Date object, we can use the java.text.SimpleDateFormat class. First, create a Date object. Next, create a SimpleDateFormat instance using the getDateInstance() method, passing the String "MM", "MMM", or "MMMM" as the argument. Finally, get the month as a String using the format() method passing in the java.util.Date object.

Get Month from Date in Java - Example Code

import java.text.SimpleDateFormat;
import java.util.Date;

//
//The following example code demonstrates how to
//print out the Month from a Date object.
//
public class GetMonthFromDate {

    public static void main(String[] args) {

        Date now = new Date();
        SimpleDateFormat simpleDateformat = new SimpleDateFormat("MM"); // two digit numerical represenation
        System.out.println(simpleDateformat.format(now));

        simpleDateformat = new SimpleDateFormat("MMM"); // three digit abbreviation
        System.out.println(simpleDateformat.format(now));

        simpleDateformat = new SimpleDateFormat("MMMM"); // full month name
        System.out.println(simpleDateformat.format(now));

    }

}

Here is the output of the example code:
05
May
May

Monday, May 2, 2011

Port Forwarding (80 to 8080 for Tomcat) Using IPFW on Mac OSX

Here I show how easy it is to set up port forwarding (80 to 8080 for Tomcat) on Mac OSX using ipfw from the Terminal. IPFW is the built-in firewall of Mac OSX and we can quickly set up a firewall rule to allow port forwarding...


Step 1: View current firewall rules.
sudo ipfw show

Step 2: Add port forwarding rule (80 to 8080 for Tomcat)
sudo ipfw add 100 fwd 127.0.0.1,8080 tcp from any to any 80 in

If you want to remove your firewall rules run:
sudo ipfw flush


Piece of Cake!!!

Tuesday, April 26, 2011

Setup Remote Shared Git Repository on Linux

Recently I set up Git on a remote Linux machine so that a friend and I could collaborate on a few coding projects. Following are the steps I took to get it all set up. The Linux distro I have installed on the remote host is Ubuntu.


Step 1: Install Git. SSH into the remoteserver and install git using apt-get. This installs git under /usr/bin
sudo apt-get update
sudo apt-get install git-core

Step 2: Set up Git Users
sudo groupadd developers
sudo useradd -G developers -d /home/timmolter -m -s /bin/bash timmolter
sudo passwd timmolter

Step 3: Passwordless SSH

Logged into remote account:
mkdir .ssh
From local account**:
scp ~/.ssh/id_rsa.pub timmolter@hostname:.ssh/authorized_keys
**assuming you already have a SSH key. If not try here.

Step 4: Setup a shared repository
Using an account with sudo-rights ("manager" - not a developer user that you just created):
cd ~
mkdir -p repositories/project.git
cd repositories/project.git
git init --bare --shared=group
ls -l
sudo chgrp -R developers .
ls -l

Now you have your Git repository and you can push you project to it!

Step 5 (optional):

Setup a symbolic link to the repository so the user can access it directly from his/her account.
sudo ln -s /home/manager/repositories/project.git/ /home/timmolter/


Piece of cake!!!

References

kovshenin.com
useradd

Eclipse Trick - Hide Jars on Classpath in Project Explorer

The trick for me was to use the Package Explorer View instead of the Project Explorer View.

Wednesday, April 20, 2011

Human Towers and Collapsing Human Towers

Casteller from Mike Randolph on Vimeo.

Get Year from Date Object in Java

To get the year from a java.util.Date object, we can use the java.text.SimpleDateFormat class. First, create a Date object. Next, create a SimpleDateFormat instance using the getDateInstance() method, passing the String "yyyy" as the argument. Finally, get the year as a String using the format() method passing in the java.util.Date object.

Get Year from Date in Java - Example Code

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

//
// The following example code demonstrates how to
// print out the Year from a Date object.
//
public class GetYearFromDate {

    public static void main(String[] args) {

        Date now = new Date();
        SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy");
        System.out.println(simpleDateformat.format(now));

    }
}

Here is the output of the example code:
2011

Disable Spotlight on Mac OSX

Victory! After years of being somewhat irritated about the mdsworker and mds process running in the background and causing my fan to go on sometimes, I finally decided to do something about it. I never use Spotlight anyway so why not get rid it that and save some CPU cycles? There are many suggestions and tricks out there to disable Spotlight on Mac OSX, but most of them didn't make sense to me or didn't do exactly what I wanted when I tried them. The solution that I settled on here is the most effective, reversible, and safest in my opinion.

First a little background about OSX. When your mac boots up, it runs several property-list files (*.plist) via launchd, a unified, open source service management framework for starting, stopping and managing daemons, applications, processes, and scripts. So if we don't want Spotlight to start up and we don't want the mds process to index your drive(s), all we need to do it prevent launchd from starting the plist files that start these processes.

Here's how you do that (tested on Mac OSX Leopard 10.5)...

Spotlight

Open up Terminal, and run the command:
sudo launchctl unload -w /System/Library/LaunchAgents/com.apple.Spotlight.plist

MDS, MDSWORKER

In Terminal, run the command:
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

Spotlight's Index Files

To get rid of Spotlight's files run the following command in Terminal:
sudo rm -r /.Spotlight-V100

Getting Spotlight back

If you change your mind after diabling Spotlight, all you need to do to start it again is to run the following commands in Terminal:
sudo launchctl load -w /System/Library/LaunchAgents/com.apple.Spotlight.plist
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

Piece of Cake!!!

**Disclaimer - Use at your own risk. And remember, don't believe everything you read on the Internet! :)


References:

launchctl

Sunday, April 17, 2011

Install EGit for Eclipse

I recently decided to try out EGit, a GIT client plugin-for Eclipse. Following are the steps I took to get it running in Eclipse-Helios.

Step 1: In Eclipse go to Help --> Eclipse Marketplace



Step 2: In the wizard that pops up, highlight "Eclipse Marketplace" and click Next.


Step 3: Search for "git". On the top of the list, EGit showed up for me.


Step 4: Click Install and wait while it calculates requirements and dependencies. Click "Next".


Step 5: Accept the terms and conditions and click "Finish". Choose to restart Eclipse after the install finishes.

Step 6: Window --> Show View --> Other...


Step 7: Find and choose the Git View under Git > Git Repositories.


Now you should have your Git view and you can start using Git from within Eclipse!

Piece of Cake!!!

Remove Timestamp from Date in Java

To remove the timestamp from a java.util.Date object,
we can use the java.text.SimpleDateFormat class. First, create a Date
object. Next, create a SimpleDateFormat instance using the getDateInstance(SimpleDateFormat.SHORT) method. Next, create a String representing the Date without a timestamp with the format method. Finally, create a new Date object using the SimpleDateFormat instance's parse method and passing it the date String.

Remove Timestamp from Date in Java - Example Code

import java.text.DateFormat;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

// The following example code demonstrates how to
// remove the timestamp from a Date object.

public class RemoveTimestampFromDate {

    public static void main(String[] args) {

        Date date = new Date();
        System.out.println(date.toString());

        DateFormat df = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
        String dateString = df.format(date);
        try {
            Date dateWithoutTimestamp = df.parse(dateString);
            System.out.println(dateWithoutTimestamp.toString());
        } catch (ParseException e) {

        }
    }

}

Here is the output of the example code:
Sun Apr 17 10:44:43 CEST 2011
Sun Apr 17 00:00:00 CEST 2011

Saturday, February 19, 2011

Set Tomcat's Default Locale

If for whatever reason you want to set the locale in Tomcat you simply need to add two jvm arguments in the catalina.sh file found in the "bin" directory of the Tomcat installation. I was able to set the default locale for Tomcat to US/en on a Mac running Mac OS X Leopard with Tomcat 7.0.8 installed. These instructions should work for any *nix machine, but I can't guarantee it. If you're using windows, these instructions are probably also relevant, but instead you'd change catalina.bat.

Step 1: Open up the catalina.sh file with any text editor.


Step 2: Search for "JAVA_OPTS=" in the file and in particular locate the following lines of code:
if [ -z "$LOGGING_MANAGER" ]; then
JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
else
JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER"
fi

Step 3: At the end of the JAVA_OPTS definitions add "-Duser.language=en -Duser.region=US".
if [ -z "$LOGGING_MANAGER" ]; then
JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Duser.language=en -Duser.region=US"
else
JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER -Duser.language=en -Duser.region=US"
fi

Step 4: Save the file and restart Tomcat.

Piece of Cake!!

Sunday, February 13, 2011

My Mac OS X MySQL Cheat Sheet

Using MySQL and Java? Check out an easier way: Yank

Following is my Mac OS X MySQL cheat sheet. Normally I use a great open source SQL client called Sequel Pro for my SQL work, but every now and then I need to log into MySQL directly using Terminal for some advanced options.

Log In

/usr/local/mysql/bin/mysql -u root -p

Create Database

create database DATABASE_NAME;

Show Databases

show databases;

Use Table

use TABLE_NAME;

Indexes

show index from TABLE_NAME;

Drop Table

drop table TABLE_NAME;

Truncate

truncate table TABLE_NAME;

See also: Uninstall MySQL on Mac OS X
And also: Install MySQL on Mac OS X
And also: Installing and Running Tomcat on Mac OS X
And also: Set MySQL Server System Variables on Mac OS X

Saturday, February 12, 2011

Set MySQL Server System Variables on Mac OS X

Using MySQL and Java? Check out an easier way: Yank

If you need to set a system variable for the MySQL server, it's done by adding a name-value pair in a file called my.cnf. Where is my.cnf you're wondering? MySQL will look inside the /etc for "my.cnf" file. This file does not exist by default. The following steps describe how to create the my.cnf file used to confugure MySQL.

Step 1: Log into mysql. Fire up the terminal and enter:
/usr/local/mysql/bin/mysql -u root -p
Edit path and user appropriately.

Step 2: View MYSQL's current values:
show variables;
I'm interested in overriding the ft_min_word_len variable, and I see it's currently set to 4.

Step 3: Create /etc/my.cnf:
sudo vim /etc/my.cnf
and add (for example):

[mysqld]
default-time-zone='+00:00'
ft_min_word_len=1

to the file. Exit vim (esc :x).

Step 4: Restart MYSQL:
sudo /Library/StartupItems/MySQLCOM/MySQLCOM restart

Step 5: Log into MYSQL again and verify the change occured.

Piece of cake!!!


References

MySQL 5.5 system variables
The MySQL Configuration File


See also: Uninstall MySQL on Mac OS X
And also: Install MySQL on Mac OS X
And also: Installing and Running Tomcat on Mac OS X
And also: My Mac OS X MySQL Cheat Sheet

My Mac OS X Filesystem Permissions Cheat Sheet

With Mac OS X being a Unix-based Operating System, we can use the Terminal application to get under the hood of the OS and perform many advanced operations. One the the most basic Unix or Linux command line operations or set of operations deals with file permissions. Here, I have accumulated revavent information and commands to filesystem permissions general to *nix and also specific to a mac.

Users and Groups

Listing

dscl . -list /Groups PrimaryGroupID
dscl . -list /Users UniqueID

Creating a non-standard user

First, choose a User ID and a Group ID which is not already in use, which must be a positive integer, lower than 500.
sudo dscl . -create /Groups/_tomcat PrimaryGroupID 107
sudo dscl . -create /Groups/_tomcat RealName "Tomcat Users"
sudo dscl . -create /Groups/_tomcat Password \*
sudo dscl . -create /Users/_tomcat UniqueID 107
sudo dscl . -create /Users/_tomcat PrimaryGroupID 107
sudo dscl . -create /Users/_tomcat HomeDirectory /usr/local/tomcat
sudo dscl . -create /Users/_tomcat UserShell /usr/bin/false
sudo dscl . -create /Users/_tomcat RealName "Tomcat Administrator"
sudo dscl . -create /Users/_tomcat Password \*
Choosing /usr/bin/false as the UserShell, and setting the Password to “*” turns this account unusable as a standard user account. The _name convention is used for non-standard accounts.

List Files - ls


ls -lhFa

Arguments

-l long format, displaying Unix file types, permissions, number of hard links, owner, group, size, date, and filename
-F appends a character revealing the nature of a file, for example, * for an executable, or / for a directory. Regular files have no suffix.
-a lists all files in the given directory, including those whose names start with "." (which are hidden files in Unix). By default, these files are excluded from the list.
-R recursively lists subdirectories. The command ls -R / would therefore list all files.
-d shows information about a symbolic link or directory, rather than about the link's target or listing the contents of a directory.
-t sort the list of files by modification time.
-h print sizes in human readable format. (e.g., 1K, 234M, 2G, etc.)

Explained

drwxr-xr-x@ 14 root wheel 476B Feb 12 00:12 ./
drwxr-xr-x 6 root wheel 204B Feb 10 22:11 ../
-rw-r--r--@ 1 root wheel 55K Feb 4 13:51 LICENSE
-rw-r--r--@ 1 root wheel 1.2K Feb 4 13:51 NOTICE
-rw-r--r--@ 1 root wheel 8.6K Feb 4 13:51 RELEASE-NOTES
-rw-r--r--@ 1 root wheel 6.5K Feb 4 13:51 RUNNING.txt
drwxr-xr-x@ 26 root wheel 884B Feb 12 00:55 bin/
drwxr-xr-x@ 9 root wheel 306B Feb 12 01:07 conf/
drwxr-xr-x@ 21 root wheel 714B Feb 10 23:40 lib/
drwxrws---@ 24 _tomcat admin 816B Feb 12 00:55 logs/
drwxrws--- 6 _tomcat admin 204B Feb 12 00:12 static/
drwxrws---@ 6 _tomcat admin 204B Feb 12 12:40 temp/
drwxrws---@ 12 _tomcat admin 408B Feb 12 00:49 webapps/
drwxrws---@ 4 _tomcat admin 136B Feb 12 01:08 work/
d=directory, -=normal file, l=symbolic link
rwx=red/write/execute, rwx truple=user/group/others
@=attributes, use xattr -l filename to view them
next comes the user owner, then the group owner, then file size, then last modified, then name

CHOWN


Arguments

-R recursive

Explained

sudo chown root file.txt
sudo chown -R root:wheel .
sudo chown root:_tomcat conf/tomcat-users.xml
sudo chown _tomcat:admin logs temp webapps work
root=user, wheel=group

CHMOD

Arguments

-R recursive

Explained

sudo chmod 644 conf/*
sudo chmod 640 conf/tomcat-users.xml
sudo chmod 2770 logs temp webapps work
sudo chmod -R 2770 /usr/local/tomcat/static
rwx=red/write/execute, rwx truple=user/group/others
7 full
6 read and write
5 read and execute
4 read only
3 write and execute
2 write only
1 execute only
0 none
2770, the first bit (2) in a 4-bytle chmod is for setuid and setgid permissions. See: techrepublic.com


References:

Joel's Writings
ls-Wikipedia
anselmbradford
chown-Wikipedia
chmod
setuid

Friday, February 11, 2011

Debugging a Launchd Configuration on Mac OS X

Recently I set up a launchd configuration on my macbook that starts Tomcat up automatically during bootup. Of course it didn't work until after man hours of trial and error and combing Google for answers with a fine toothed comb. During part of the ordeal I came across a couple terminal commands for debugging Launchd. Here they are:

1. set the launcdctr log level to debug
sudo launchctl log level debug (debug, info, notice, warning, error, critical, alert, emergency)

2. Tail the system.log
tail -f /var/log/system.log

3. set the launcdctr log level back to error
sudo launchctl log level error

The possible log levels are as follows: debug, info, notice, warning, error, critical, alert, emergency.

Useful links I came across:
Creating launchd Daemons and Agents
Joel's Writings