Note to self: Structs in C++ are like Value Objects in Actionscript or Java

Wiser folks than I have accurately pointed out that no analogy is perfect. In this case, it’s plain to see that Structs in C++ are an official data type that’s part of the language itself, whereas Value Objects are more of a convention (former design pattern[1]) created by Java programmers (among others) over the years.

Structs are similar to Value Objects since both are essentially containers for a bunch of data. Both don’t have methods. Perhaps, Value Objects could have Getters & Setters, but more often there aren’t methods.

An example of a Struct in a C++ tutorial:

struct database {
  int id_number;
  int age;
  float salary;
};

int main()
{
  database employee;  
  employee.age = 22;
  employee.id_number = 1;
  employee.salary = 12000.21;
}

An example of a Value Object from an AS3 tutorial using PureMVC:

package com.flashtuts.model.vo
{
	public class DataVO
	{
		public var dataURL:String          = 'assets/xml/data.xml';
		public var urlsArray:Array	  = [ ];
		public var urlsDataArray:Array  = [ ];
	}
}

1. In Java the Value Object Pattern is now called the Transfer Object Pattern.

Adding MySQL to $PATH Environment Variable on OS X

Update, 09.10.2012: This just worked for me for adding Android’s adb tool to my environment path on OS X 10.6.8. The first time I tried I got an error because I wrote “export $PATH=” instead of “export PATH=”.

Earlier this year I took some Java courses. I learned a ton from what was on the syllabus. It was exciting to see stuff like Generics in the Collections framework in JDK 1.5 and compare it to how Actionscript didn’t have that option until very recently, when the Vector class was added to AS3 for Flash Player 10.

One thing that caught me by surprise was that all software installation instructions for class materials were for Windows only (may be I was naive to expect otherwise). The professor was good but he wasn’t a Mac guy and he was super busy at his day job. A few of us OS X guys had to do extra work on our own on top of all the regular class work, just to get Eclipse, Tomcat & MySQL working on our systems. As painful as it was to spend extra days on things like “Why do I have to spend days on learning UNIX when my Java homework is due tomorrow?!!!” OR “Why do I keep getting a 404 Error for Servlets but not for JSP?! !@##$#^$%^$%^!!”… in the end, I think we ended up as slightly better developers, with a little bit more experience, because we all had to teach ourselves a little Terminal and OS X UNIX commands on the spot.

At the very least, I can always say, “Hey, at least I taught myself how to add “mysql” to the $PATH Environment Variable on OS X:

  1. launch Terminal (assuming it’s a bash shell)
  2. write ‘echo $PATH’ to see what your path is now
  3. if ‘mysql’ isn’t in there, write in the path to where you installed it via this command:
    ‘export PATH=/Users/your_user_name/your_path_to_mysql/mysql/bin:$PATH >> ~/.bash_profile’
  4. that should add it to your bash_profile
  5. write ‘echo $PATH’ again, you should see ‘mysql’ in there

Note: this is for installing MySQL as a stand alone app for use with J2EE/Java projects. If you’re just trying to do some PHP on a development machine, it’s easier to use a wysiwyg LAMP package like MAMP or XAMPP.

More on the $PATH environment variable here.

The same technique can be used to add the open source Flex SDK compiler, MXMLC, to your $PATH environment variable on OS X.

Checking out a new book for Java EE & Flex 3, published 2009

I  couldn’t find any sort of companion site url. Was I not reading carefully enough or does one not exist on purpose/due to time constraints? 

Trying out the Ch.6: Writing Java Web Applications example…  

On first try, I had a configuration problem trying to install ALL of  Spring IDE 2.2.4 plugin for Eclipse 3.4 for Java EE on OS X 10.4.11.  Here’s the error message:

2

Staying in Eclipse 3.4 I then tried to install only the Spring IDE parts specified on p.33 by doing Help > Software Updates > Available Software > Add Site  (http://springide.org/updatesite)  and the installation worked.

Note
It was quicker and easier to do this Spring MVC tutorial first, before doing Ch.6’s detailed ANT build solution. There was 1 vague area here, in the Adding Libraries section it was unclear which folders contain which .jar file. Also, the file names didn’t contain the version numbers in them. After a little digging inside /mylocalpath/spring-framework-2.5.6/, it was fine. Most were in “dist,” “lib,” or “jakarta-commons.”

Here’s a resource for going beyond the Ch.6 example: Mastering Spring MVC.

J2EE Code Behind for Flex / Actionscript Developers

Just re-stumbled upon Ted Patrick’s post on using the Code Behind technique in Flex with Actionscript. Ted wrote about how he used it in .NET and in Flex. There’re more explanations of Code Behind here and here.

The same technique exists in J2EE in the form of JSP and Java Beans. It’s also known as Model 1 architecture (Model 2 is an example of MVC). As Ted states MXML is just a bunch of tags that are really ActionScript code under the hood and a .mxml file is a .as file. JSP can be thought of as tags that represent Java code. They’re XML-like tags that the compiler breaks down into a Java class, specifically, a Servlet.

JSP is used for displaying visual content, what’s called the “View” in design pattern terminology. Java Beans are Java class files that handle the application’s logic.

You can use a JSP import statement to use a Bean to create objects and then access its public methods like so:

<jsp:useBean id="calc" class="com.mysite.MyCalc" scope="session" />

<html><head><title>JSP Code Behind</title></head><body>

<%
	double myResult = calc.addTwoNumbrs(92.2231, 2384.0);
	out.print("myResult = " + myResult);
        //in the browser  you should see
        //myResult = 2476.2231
%>

</body></html>

Here “id=calc” refers to the object reference, or an instance name called “calc.” The “com.mysite.MyCalc” part specifies the package “com.mysite” and class “MyCalc.” For the Tomcat 6 servlet container (or application server), this Bean would live inside the Tomcat6/webapps/yourApp/WEB-INF/classes/com/mysite/ directory.

package com.mysite;
public class MyCalc {
  public void MyCalc() { }
  public double addTwoNumbrs(double num1, double num2 )
  {
     return num1 + num2;
  }
}

How to set up Connector/J in Eclipse

Connector/J is a “Type IV JDBC driver for MySQL.”

Assumptions

You have downloaded & installed MySQL 5.1, Connector/J for 5.1 (the JDBC driver for MySQL) from mysql.com, Tomcat 5.5 or 6, Eclipse 3.4 for Java EE. I’m assuming this is a Dynamic Web Project in Eclipse with Tomcat 6 as the Target Runtime. I did this successfully on OS X 10.5 (Leopard) & on Windows XP.

The Steps:

  1. Select your project folder in Eclipse’s Project Explorer (just click on it so it’s highlighted).
  2. Open project properties. There’re several ways to do this. For example, you can go to Project > Properties in Eclipse’s top menu.
  3. Click on Java Build Path. Click “Add External Jars.” Navigate to where you unzipped Connector/J on your hard drive. Choose the .jar file (in my case mysql-connector-java-5.1.7-bin.jar) & click Open. It’ll now show up under JARs and class folders in the build path.
  4. Click on Java EE Module dependencies in the menu on the left.
  5. Check the checkbox next to the Connector/J path under JAR/Module. Hit Apply and OK.
    Run a skeleton test Servlet to see if it works:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestJDBC extends HttpServlet {

    public void init(){
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            System.out.println("JDBC driver loaded");
        } catch (ClassNotFoundException e) {
            System.out.println(e.toString());
        } //Eclipse may auto-force some extra catch blocks here
     }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("Check the Console tab in" +
		" Eclipse... it should say \"JDBC driver loaded\"");
    }

}