Friday, December 30, 2011

JavaMail

I am using JavaMail to try to send mail.

I am using smtp as the protocol.

I had to add authentication for the connection.

if (mailhost != null)
props.put("mail." + prot + ".host", mailhost);
if (auth) {
props.put("mail.smtp.starttls.enable","true");
props.put("mail." + prot + ".auth", "true");
}

I had to use the keytool program to add a certificate for the server. The easiest way was to open the server authentication in Thunderbird and export the certificate, then use the keytool to import the certificate to the cacerts file.

\Program Files (x86)\Java\jdk1.6.0_20\jre\bin\keytool" -import -file c:\Users\me\server.com.crt -alias myHost -keystore ..\lib\security\cacerts

This worked for one of my mail servers, but did not work for my school server. The error I received was:

ADDRESS FAILED:
com.sun.mail.smtp.SMTPAddressFailedException: 554 : Relay access denied

After reading online, it dawned on me that I cannot use the mail server when I am not on the domain. I connected to the domain with a VPN and the mail was sent.

My third domain still does not authenticate. I have followed the steps for the other two, but I am still receiving the error that a certificate cannot be found. The strange thing is that this is the server for the ISP that I am using to connect to the internet. Maybe I don't need authentication? BINGO!!! It worked without authentication.

Monday, December 26, 2011

Learning Spring

I am using the book Spring in Action, 3rd edition, Walls, to learn about the Spring framework.

I have been following the examples in the book. Everything has worked until the Java configuration at the end of chapter 3. Two jar files are needed to run Spring with Java configuration: asm.jar and cgilib.jar.

Actually, that didn't work. I am using Spring 3.0 and the Java config is now part of the core. The problem is with the cgilib that is used in the library. I added cgilib-nodep.jar at the head of the classpath and it works.

Saturday, August 13, 2011

Hibernate Tools in Eclipse

Hibernate Tools in Eclipse

I am a NetBeans user and am attempting to learn to use Eclipse.

The Hibernate Tools have a package for Eclipse, but I have not found one for NetBeans. This is why I am trying to learn Eclipse.

Eclipse and Ubuntu follow a similar naming device for versions. Eclipse is now up to Indigo. The previous version was Helios.

I have installed Indigo.

I am reading a page about using the JBoss Update link.

I have gone to Help -> Install New Software, selected Web and EE Developement and then Hibernate Tools. After a license screen, the tools installed. The install site is JBoss Tools Requirements - Composite Indigo RC2 Mirror - http://download.jboss.org/jbosstools/updates/indigo/RC2/

When I open the New wizard, I see the Hibernate wizards. So far so good.

I have create a new Dynamic Web Project and will now try to reverse engineer some POJO classes from my database using the Hibernate Tools.

I am following the instructions for reverse engineering POJO files.
  1. Create hibernate config file (File Name: hibernate.cfg.xml)
  2. Parent folder: HibernateReverse/src
  3. Using mysql and com.mysql.jdbc.Driver. (I do not know if this will be found, but it appeared in drop down list. In Helios, this driver did not appear.)
  4. Creating console configuration, for accessing the database without using code. The console allows me to see all the tables, etc, in the database. Good news, the wizard page has the correct appearance from the tutorial. When I tried this before (without JBoss update) the wizard was different.
  5. Added Hibernate Perspective.
  6. Tried to open console configuration, but db driver could not be found (I told you so).
  7. I will try adding an external jar. Opened project properties, selected Build Path, selected Add External Jar, pointed to the driver jar. It worked!!!
  8. I opened the console configuration and am able to view all the tables in the database.
  9. Starting the Hibernate Configuration wizard. There is a new icon for running hibernate and there is a new option under the Run menu.
  10. Double-clicked Hibernate Code Generation to get a new config. Set output folder.
  11. Select Reverse Engineer from JDBC.
  12. Create new Reverse.eng.xml. Include table(s) to process.
  13. Selected Java 5, EJB3 Annotations and Domain code. I did not select a config file option. I think that the annotations will be enough.
  14. Refresh tab: refresh workspace.
  15. Common tab: no change.
  16. Click Run. It worked!!!
Trying again a month later. Needless to say, I have forgotten everything, so I started playing around.

Once the Hibernate tools are added, there is a new icon in the tool strip. It is a green, play arrow like the other Run configurations, but it is for Hibernate. To do a reverse engineering process, select Hibernate Code Generation Configurations from that list. This option is also available at the bottom of the normal Run tool icon.

First create the console configuration for connecting to the database, then create a Hibernate Reverse Engineering configuration. The reverse engineering file can be created from there.

Sunday, March 27, 2011

Web Services in Java

I am attempting to learn about web services in Java.

Working with NetBeans

In general, web services have two flavors: RESTful and (SOAP, WSDL). RESTful are supposed to be easier to consume. WSDL are more robust.
http://netbeans.org/kb/docs/websvc/intro-ws.html

There is an implementation named Axis, based on SOAP, also has services for RESTful.
http://netbeans.org/kb/docs/websvc/gs-axis.html

Creating Axis2 Web Service in NetBeans

I have created a web service using Axis2 and have accessed the service through a browser. It returns an XML file. http://netbeans.org/kb/docs/websvc/gs-axis.html

Creating Files to Access an Existing Web Service

I will now follow the tutorial to create a JAX-WS client that can access the service.
http://netbeans.org/kb/docs/websvc/client.html

First, I created the files to access an existing web service.
  1. Create web application
  2. New.Web Services.Web Service Client
  3. Specify URL of WSDL file: http://ws.cdyne.com/SpellChecker/check.asmx?wsdl
  4. Leave package blank, so it will be read friom WSDL
  5. Select JAX-WS as the client style.
  6. Many source files are generated.
  7. A local copy of the WSDL is generated.
Creating a Client to Access the Web Service

Second, I created a client to access it.
  1. Create web application.
  2. Create user interface in JSP.
  3. Create servlet.
  4. Drag service component into servlet. The tutorial is wrong about the code that is inserted: a method is inserted. Call the method to run the service.
  5. Modify the servlet to call the service and update the UI.
Brilliant!

The above approach uses NetBeans and Metro.

Working with Eclipse

I will now try to use Eclipse and Apache CXF.

Update Eclipse so it can use Tomcat to Run Servlets


First, add ability to run servlet in Eclipse.
  1. Download Eclipse with Java EE.
  2. Add extensions for JAX-WS from
    http://wiki.eclipse.org/images/b/bf/Jaxws-1.0.0-201003201732.psf
  3. Add them to Eclipse with File->Import->Team->Team Project File. The username is anonymous and the password is an email address.
  4. Create a Dynamic Web Application. Create a New Runtime (Server) for Tomcat. Select Create local server, to save a step later.
  5. Create servlet from New -> Servlet.
  6. Works perfectly.
  7. After I had to restore my computer, the eclipse installation was no longer working. Tomcat was not recognized. I switched workspaces (File->Switch Workspace) and reinstalled the Tomcat plugin. It is working again, but there are still some red Xs for some packages.
Install CXF and Create a Web Service (Did not work!!! Don't do this!!!)

Second, download Apache CXF.
  1. http://www.apache.org/dyn/closer.cgi?path=/cxf/2.3.3/apache-cxf-2.3.3.zip
  2. Download Apache Ant: http://ant.apache.org/bindownload.cgi
  3. Follow video at:
    http://repo.fusesource.com/videos/service-creation/Java2WSClientServer.wmv
  4. The video is outdated. Click Add to enter the location of CXF. Be sure to check the new entry.
  5. In Eclipse: Window.Preferences.Web Services
    1. ...CXF Preferences; browse to install dir, select installation, export at runtime.
    2. ...JAX-WS; enable annotations; do not enable validation
    3. ..Spring Config; use Spring
  6. Create web app; select configuration as CXF; my install does not have springframework list, but is shown in video. Video shows for runtime. To select CXF, I had to select Tomcat.
  7. The Libraries are different from the video: jar files are in CXF, not Web App.
  8. Add converter for celsius and farenheit
  9. Make into web service: video shows Button Up POJO. That is not in my option list.
Failed miserably!!!

Creating Axis2 Web Service in Eclipse

Second attempt at Eclipse. New Tutorial:
http://www.softwareagility.gr/index.php?q=node/28
  1. Install Apache Axis2
  2. Create Dynamic web app; set configuration to include Axis2; define service class.
  3. Right-click service class and select Web Services.Create Web Service; set runtime to Axis2
  4. Run service; default Axis2 page appears; view service methods
  5. In Eclipse: Run.Launch Web Service Exporer; enter URL for WSDL; run methods
Brilliant! I now have a web service in Eclipse using Axis2 (like NetBeans)

Creating Client to Access Web Service using Eclipse

Now, to create a client in Eclipse:
http://www.eclipse.org/webtools/community/tutorials/BottomUpAxis2WebService/bu_tutorial.html
  1. Tutorial creates another web service, like above.
  2. Tutorial then goes on to explain how to create client in separate app. In the first part, there was a check box for creating the client files. They were created in a separate app: same name as before, appended with Client; however, it does not have all the files as explained in the tutorial.
  3. Only one stub is created, not three (Http, Soap11, Soap12) as in tutorial.
  4. Imported the code to the client. Cannot be accessed in Tomcat.
  5. Try creating test case. They don't work either.
I have reached a road block with Eclipse.
  1. I was planning to redo it all, when I noticed the stubs I had created inadvertently, when I first created the service. I decided to delete these. When I did, I received a lot of errors, so I undeleted them. At that moment, I noticed the return type on the celsiusFarenheit method. It was not a float; this was good, because that was a problem I was having with the others.
  2. I copied the client code from the web again and modified it with ClientStub.
  3. It worked!
  4. Solution: use the ConverterStub class, not any of the ones that have Soap in the name.
  5. Update on solution: it seems that you must create a completely new app for the client, do not import the stubs into a current app. The correct stubs were only created when creating a new client app.

Monday, March 14, 2011

Shoemaker Proof

I am working as a coach for our programming team.

One of the problems is the Shoemaker problem. The complete description can be found at Shoemaker Description.

The simple solution is to sort the jobs in order by the cost/delay ratio, then do the jobs in decreasing order.

It is a simple solution, but I was not convinced that it was correct, so I proved it as follows.

I will use the following annotations to indicate sums, costs and delays.

cost(m) will be the cost for each day that job m is delayed.
delay(m) will be the number of days it takes to complete job m. All jobs that are completed after m will incur a cost for this delay. Job m does not pay a penalty for itself.
sum_cost(i,j) will be the sum of all the costs for jobs i through j.

It is easy to validate if there are only two jobs. The cost for each job is

Do 1 first: delay(1)*cost(2)
Do 2 first: delay(2)*cost(1)

The first formula will be less than the second, when cost(2)/delay(2) < cost(1)/delay(1)

The difficulty arises when there are more terms.

Assume that the jobs have been sorted by cost/delay.

The proposed minimum sum is
delay(1)sum_cost(2,n) + delay(2)sum_cost(3,n) + ... + delay(n-1)sum_cost(n,n)

Consider any other sum than this one. A position m can be found such that

cost(m)/delay(m) < cost(m+1)/delay(m+1)

In other terms,

formula 1 (f1): delay(m+1)cost(m) - delay(m)cost(m+1) < 0

By switching the order of these two in the sum, then a smaller cost will result.

Term m: delay(m)sum_cost(m+1,n) + delay(m+1)sum_cost(m+2,n) =

Effect of switching term m and term m+1:
delay(m)sum_cost(m+1,n) - delay(m)cost(m+1) + delay(m+1)sum_cost(m+2,n) + delay(m+1)cost(m)

The difference between this new term and the original is:
delay(m+1)cost(m) - delay(m)cost(m+1)

By the above formula (f1), this term is less than 0. The conclusion is that any sum that has terms that are out of order can be rearranged to find a smaller sum. It follows that the sum formed from the jobs in descending order by cost/delay is the minimum. QED.

Followers