Category Archives: Uncategorized

Java and overcomplication


When I first started working with Java to do real work, the servlet spec was pretty immature, and I was coming from a world of cgi-lib.pl and PHP.  Things were quite simple.  You wrote a class that had a method that received GET and POSTs, called some simple methods to get the decoded query string parameters, and printed out the results.  It was easy to learn, but the code was not pretty or well organized.  Calls to the database were simple, ugly, and prone to connection leaks.  They were easy to write, however.

Fast forward to 10 years later, and in order to write a “clean” Java web app, you need to be using an MVC framework, persistence APIs, etc. This involves pulling in a complicated tree of JAR dependencies, composing complicated XML configuration, and implementing highly abstracted interfaces which require quite a bit of thinking to get your head around.  I can’t help but think if I were just getting into Java right now, I’d be intimidated almost to the point of giving up.

Java is still pretty solidly established in the “enterprise” space, so I don’t see it disappearing anytime soon. If you want to look to the future, however, it is probably wise to take a look at what the smaller companies and startups are doing.  They have the benefit of starting fresh and being able to use the technologies that most effectively support their goals, without having to port existing stuff/retrain staff.  Java is present in those environments, but other technologies seem to be far more well represented.  I wonder how much this has to do with the Java community’s progression over the last several years to the point where it is highly complicated and difficult to learn.

Cool program

I purchased one of the new MacBook Pro’s that were released in April, and I’ve been really liking it.  Mostly it just offers improvements to some things that were getting annoying about my old one.  It has more memory, a higher resolution display, and the unibody case is cool.  In any case, the battery life is nowhere even close to the 8-9 hours they advertise.  As a result, I was curious about how often it was using the high powered discrete NVidia graphics chip, so I started looking with system profiler, and it was pretty much always running.  With some experimenting, I found out it was mostly because of Google Chrome.  It would be cool if Apple released some tools to fine tune the graphics switching, and I imagine they eventually will, but for now there is this cool program, gfxCardStatus, which has really advanced quickly since it was released.  One thing that is really cool now is that it shows a list of programs that are causing the system to use the high powered chip.

JDBC connections under an oppressive regime

If you are using tomcat in an environment where you are not allowed to view the data source configurations, it can be frustrating.  Fortunately, when you need to debug, there’s a programmatic way to get the configuration.  Here’s a sample:

<%@page import="javax.naming.InitialContext" %>
<%@page import="javax.naming.Context" %>
<%@page import="javax.naming.NamingEnumeration" %>
<%@page import="javax.naming.NameClassPair" %>
<%@page import="org.apache.tomcat.dbcp.dbcp.BasicDataSource" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <body>
      <ul>
      <%
      if ("alwold".equals(request.getRemoteUser())) {
        InitialContext ic = new InitialContext();
        NamingEnumeration<NameClassPair> ne = ic.list("java:comp/env/jdbc");
        while (ne.hasMore()) {
          NameClassPair ncp = ne.next();
          Object o = ic.lookup("java:comp/env/jdbc/"+ncp.getName());
          if (o instanceof BasicDataSource) {
            BasicDataSource ds = (BasicDataSource) o;
            out.println("<li>"+ncp.getName()+"</li><ul>");
            out.println("<li>"+ds.getUrl()+"</li>");
            out.println("<li>"+ds.getUsername()+"</li>");
            out.println("<li>"+ds.getPassword()+"</li>");
            out.println("</ul>");
          } else {
            out.println("<li>found odd object "+ncp.getName()+" of type "+o.getClass().getName());
          }
        }
        }

      %>
      </ul>
    </body>
</html>

It’s particularly important to have the part that checks for remote user, because this thing is printing out passwords to the screen. You’ll have to put some sort of filter to set the remote user and then replace my username with yours (or come up with some entirely different protection mechanism).

An elegant DAO design for un-elegant queries

I am a big Hibernate fan, but sometimes you can’t use Hibernate. When you can’t, Spring’s JDBC support is awesome. One of the reasons I encounter frequently for not being able to use Hibernate is some ridiculously complicated queries (I’m looking at you, Peoplesoft). When you have a giant long SQL query, you often encounter some code that looks like this:

String sql = “SELECT ” +
“RM.RTITLE1, ” +
“RM.RNAME, ” +
“SR.USER_SEQ_NO, ” +
“SR.INT_SEQ_NO AS SUBREQ, ” +
“SRT.USER_SEQ_NO, ” +
“SRT.TEXT, ” +
“(SELECT SRA.COURSE FROM SUB_REQ_AR SRA WHERE SR.INT_SEQ_NO = SRA.SUB_INT_SEQ_NO AND SRA.USER_SEQ_NO = 1) AS COURSE ” +
“FROM DPROG D JOIN DPROG_REQ DR ON D.INSTIDQ = DR.INSTIDQ ” +
“AND D.INSTID = DR.INSTID ” +
“AND D.INSTCD = DR.INSTCD ” +
“AND D.DPROG = DR.DPROG ” +
“AND D.DPFYT = DR.DPFYT JOIN REQ_MAIN RM ON D.INSTIDQ = RM.INSTIDQ ” +

Ugly, isn’t it? Well, some of my coworkers that were running into this came up with the idea of putting the SQL into its own files. It is a great idea and I doubt they are the first to think of it. They don’t use Spring, though, and I thought Spring would be able to provide some good glue to put it all together.

It turns out, Spring has something called Resource that can automatically load things from various places. I chose to use the classpath, and put the SQL files into the same package as the DAO. The only problem here, is with Maven, you end up with the SQL file in src/main/resources and the code in src/main/java, which is a little annoying. The Resource object doesn’t get the whole job done either; it just provides an InputStream, and having to write code to read the contents of the file has no place in my elegant solution. So, this is where commons-io comes into play. In the setter for the Resource object, I use IOUtils to read the file and stick it in a String.

So, the overall strategy goes like this:

  • Put SQL in file in src/main/resources
  • Create DAO object with a Resource setter for the SQL file
  • Wire the SQL file resource in your applicationContext.xml
  • Read in the file using IOUtils in the setter

Here’s the DAO definition in applicationContext.xml


<bean id="darsCourseDao" class="edu.asu.eadvisor.onoff.dao.DarsCourseDaoImpl">
<property name="dataSource" ref="darsDataSource">
<property name="subreqsForCourseQuery" value="classpath:/edu/asu/eadvisor/onoff/dao/GetSubreqForCourse.sql">
</property>
</property></bean>

And the setter in the DAO looks like this:

    public void setSubreqsForCourseQuery(Resource subreqsForCourseQuery) throws IOException {
InputStream is = subreqsForCourseQuery.getInputStream();
try {
this.subreqsForCourseQuery = IOUtils.toString(is);
} finally {
IOUtils.closeQuietly(is);
}
}

Why you SHOULD use a case sensitive file system on your Mac

There is a lot of noise on the Internet saying case sensitive file systems on the Mac are bad. I guess if you are running Adobe apps, you can listen to that, since they are apparently developed sloppily and depend on a lack of case sensitivity. Any real developer, however, will find that the lack of case sensitivity causes problems. Here are two I recently ran into that ended up wasting a LOT of time debugging:

  • In Tapestry 5, a lot of effort has been made to make things not case sensitive. Developing a t5 app on my Mac, I had that in my head, and created some page templates using all lower case, and their Java classes in typical upper camel case. I ran the app and tested it out until I was satisfied it was working. I deployed it to the Unix dev server and it mysteriously stopped working. It turns out, it searches for page templates based on the case sensitive class name. So my Index class was looking for the file Index.tml. I had index.tml. On the Mac, the OS returns index.tml when you look for Index.tml. Worst of all, I had to use a Unix machine to rename the files in the SVN repository because the Mac refused to acknowledge any change.
  • Working on another project, I had a directory structure that had a directory in all caps. I needed to change it to lower case. Once that was done, I tried to commit the changes to SVN. I got this:

    svn: Commit failed (details follow):
    svn: Server sent unexpected return value (405 Method Not Allowed) in response to MKCOL request for ‘/svn/!svn/wrk/626a333b-f635-42b4-896e-5df2dd0475c2/ecampus/src/com’

    Not cool.

Pictures

I think my blog is really boring because it doesn’t have any pictures.  I’m jealous of those people that get themselves to carry their camera and take random pictures of interesting stuff during their day.  My brother Bob was doing a picture a day project, but that seems to have died.  What I’d like to do, though, is capture a photo that complements whatever I happen to be writing about, as opposed to the picture being the star of the show.  I have a really nice camera, but it is too big to carry around all the time.  If you feel like getting me a smaller one, have a visit to my Amazon wish list and pick one up :).

Security

So, it is tax season and I have been taking care of some housekeeping items with my personal finances. If you have messed around with banks and online and even telephone access, you probably noticed that there are some seriously draconian security measures in place these days. In most cases it seems like there is some combination of impossible requirements for the password combined with a requirement that you change it frequently and don’t use a password you have used before.

The end result of all this is that it is impossible to come up with passwords that you can actually remember. With that in mind, you have two choices. You can write down your password (which pretty much destroys the purported security being enforced) or you can call up their support line and have them reset your password each time you need to access whatever obscure service it is they are providing. In other words, the situation is a mess.

Various technical solutions exist for this problem, and sometimes they are used. For example, the place I work uses SecurID for VPN access sometimes. The problem is, they generally cost a lot of money and aren’t standardized. Anyway, I think there’s a lot of opportunity in this field, but I think it would make sense to approach it in an innovative way. Instead of trying to capitalize on it immediately, I think it makes sense to build something everyone can use, maybe making it free, standardized or open source. Once a standard takes hold, then there will be opportunity to provide enhancement for profit.

Friday words

  • altura – height
  • olor – scent
  • esquina – corner
  • crecer – to grow
  • sanidad – health
  • baloncesto – basketball
  • mirador – viewpoint
  • rango – rank
  • rascacielos – skyscraper
  • dirección – address

Languages

I’ve always been a fan of traveling, I think because I am constantly looking for things that are out of the ordinary. Visiting foreign cultures, one is surrounded by strange environments where things are totally different from the typical day-to-day. This probably leads to my recent increase in interest in foreign languages. It is cool to explore other countries, but you can only learn so much by observation and that which an English speaking guide will tell you. Anyway, it is probably not worth the effort it would take, but I have recently renewed my interest in getting better at new languages. I’ve had four years of high school Spanish, so I am pretty decent with the basics of that, but I still can barely understand a native speaker. At various points, I have tried to read some Spanish news sites to get some experience, but it is pretty hard to jump in and understand. Most recently, after my trip to Germany this summer, I bought some language software to start learning German. It is pretty interesting. I think I’m making some good progress, but am nowhere near my level of understanding of Spanish. I do wonder, however, if it is even possible to learn a language well by self study, and if so, how long it would take if you were pretty dedicated to it.