tag:blogger.com,1999:blog-139113042007-10-21T07:11:25.960-07:00Common knowledge in JavaJava Foohttp://www.blogger.com/profile/14104080800348407575noreply@blogger.comBlogger14125tag:blogger.com,1999:blog-13911304.post-1121355487076768092005-07-14T08:25:00.000-07:002005-07-19T14:50:46.786-07:00apache.commons.net ParserInitializationException<div style="text-align: left;"><span style="font-size:85%;">We use the jakarta/apache (don't know what they call it these days) commons net jar as our ftp client and this seems to be failing with a TANDEM ftp box.</span><span style="font-size:85%;"> Looks like <a href="http://www.opensubscriber.com/message/commons-dev@jakarta.apache.org/1695230.html">someone else</a> was running into the same error at about the same time.<br />This is the error I was running into:</span><br /><br /><span style="font-size:85%;"><span style="color: rgb(102, 51, 51); font-weight: bold;font-family:courier new;" >org.apache.commons.net.ftp.parser.ParserInitializationException:<br />Error initializing parser at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.<br />createFileEntryParser(DefaultFTPFileEntryParserFactory.java:115) at org.apache.commons.net.ftp.FTPClient.<br />initiateListParsing(FTPClient.java:2306) at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2055) at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2106) at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2146)</span></span><br /><br /><span style="font-size:85%;">After taking a look at the commons code, here's what I figured out, the TANDEM box was returning</span><br /><span style="font-size:85%;">an empty string on a FTP SYST command. Our good people at commons don't handle a null, assuming</span><br /><span style="font-size:85%;">the FTPClient.getSystemName() always returns something. So here's my hack to get around it.</span><br /><br /><span style="font-size:85%;"> <span style="color: rgb(102, 102, 0); font-weight: bold;font-family:courier new;" > if(null == ftpClient.getSystemName()) {</span></span><br /><span style="font-size:85%;"> <span style="color: rgb(102, 102, 0); font-weight: bold;font-family:courier new;" > //use parserKey,pathName method signature</span></span><br /><span style="font-size:85%;"> <span style="color: rgb(102, 102, 0); font-weight: bold;font-family:courier new;" > ftpFileArray = ftpClient.listFiles("UNIX",(String)null);</span></span><br /><span style="font-size:85%;"> <span style="color: rgb(102, 102, 0); font-weight: bold;font-family:courier new;" > } else {</span></span><br /><span style="font-size:85%;"> <span style="color: rgb(102, 102, 0); font-weight: bold;font-family:courier new;" > ftpFileArray = ftpClient.listFiles();</span></span><br /><span style="font-size:85%;"> <span style="color: rgb(102, 102, 0); font-weight: bold;font-family:courier new;" > }</span></span><br /><span style="font-size:85%;"> </span></div>Java Foohttp://www.blogger.com/profile/14104080800348407575noreply@blogger.comtag:blogger.com,1999:blog-13911304.post-1120142967408103912005-06-30T07:35:00.000-07:002005-06-30T07:49:27.413-07:00MySQL: access denied for user: (Using password: YES)More experimenting with struts. So I got ambitious, downloaded MySQL and tried to wire a database into my simple struts app. Setting up MySQL was a breeze, although it was my first time, except that some time was wasted googling for user account creation etc.<br />So, I set up a datasource in Tomcat, which could be looked up by JNDI. I added this in server.xml.<br />But was getting errors with Context.lookup, indicating there was no <span style="color: rgb(0, 0, 153);">"java:comp/env/jdbc"</span> in the Context. Realized I forgot to add the resource-ref in my web descriptor.<br />This done, I was getting a different error.<br /><span style="color: rgb(102, 51, 51); font-weight: bold;">Access denied for user 'blah': (Using password: YES).</span><br />What the ..., I didn't set the password as YES, what the heck was going on?<br />So googled again <span style="color: rgb(153, 153, 0);">(one of these days, when someone asks me what tools, IDE I use for development, I will be sure to add 'Google' in there, in fact this should come integrated with the IDE, any Exception or Error message should be linked to Google search)</span>, anyway I digress.<br />So this seems to be a common problem. I tweak the user account a bit in MySQL, apparently the host shouldn't be set as '%', you are better off setting it to 'localhost'. I also deleted the default user account '', which is an empty string. So either of these fixed it for me and I am on my way.Java Foohttp://www.blogger.com/profile/14104080800348407575noreply@blogger.comtag:blogger.com,1999:blog-13911304.post-1120071930721170192005-06-29T12:02:00.000-07:002005-06-29T12:05:30.726-07:00Gripe about the parameter attribute in Struts action configMy gripe about this generic attribute called 'parameter' in the struts action config.<br />So I have something like:<br /> <span style="color: rgb(204, 102, 0);font-size:85%;" ><span style="font-family: courier new;"> <action path="/logonProcess"</span><br /> <span style="font-family: courier new;"> type="naive.actions.LogonAction"</span><br /> <span style="font-family: courier new;"> name="logonForm"</span><br /> <span style="font-family: courier new;"> parameter="/pages/Logon.jsp"</span><br /> <span style="font-family: courier new;"> validate="true"</span></span><br />Now someone pray tell me what this 'parameter' is supposed to do.<br />I looked up on the Apache site and it says:<br /> "action general purpose configuration parameter".<br /><div style="text-align: justify;">Alright, but what is that supposed to mean. Well it means different things to different action type classes, if it is a ForwardAction it can be the url you want the action to be forwarded to. For DispatchAction it is the key to the different method values. Someone ought to document this, instead of making you run in circles, dammit. But that is what they say about struts, very little documentation. What do I know, anyway.<br /></div>Java Foohttp://www.blogger.com/profile/14104080800348407575noreply@blogger.comtag:blogger.com,1999:blog-13911304.post-1120071654542241052005-06-29T11:50:00.000-07:002005-06-29T12:00:54.556-07:00Struts action, parameter attributeMy gripe about this generic attribute called 'parameter' in the struts action config.<br />So I have something like:<br /> <span style="font-size:85%;"><span style="font-family: courier new; color: rgb(204, 102, 0);"> <action</span><br /> <span style="font-family: courier new; color: rgb(204, 102, 0);"> path="/logonProcess"</span><br /> <span style="font-family: courier new; color: rgb(204, 102, 0);"> type="naive.actions.LogonAction"</span><br /> <span style="font-family: courier new; color: rgb(204, 102, 0);"> name="logonForm"</span><br /> <span style="font-family: courier new; color: rgb(204, 102, 0);"> <span style="font-weight: bold;">parameter="/pages/Logon.jsp"</span></span><br /> <span style="font-family: courier new; color: rgb(204, 102, 0);"> validate="true"<br /><span style="color: rgb(0, 0, 0);"><span style="font-size:100%;"><span style="font-family: georgia;">Now someone pray tell me what this 'paramter' is supposed to do.<br />I looked up on the Apache site and it says:</span><br /> <span style="font-family: georgia;">"action general purpose configuration parameter".</span><br /> </span></span></span></span> <div style="text-align: justify;"><span style="font-size:85%;"><span style="font-family: courier new; color: rgb(204, 102, 0);"><span style="color: rgb(0, 0, 0);"><span style="font-size:100%;"><span style="font-family: georgia;">Alright, but what is that supposed to mean. Well it means different things to different action type classes, if it is a ForwardAction it can be the url you want the action to be forwarded to. For DispatchAction it is the key to the different method values. Someone ought to document this, instead of making you run in circles, dammit. But that is what they say about struts, very little documentation. What do I know, anyway.</span></span></span></span></span><br /><span style="font-size:85%;"><span style="font-family: courier new; color: rgb(204, 102, 0);"><span style="color: rgb(0, 0, 0);"><span style="font-size:100%;"></span></span></span></span></div>Java Foohttp://www.blogger.com/profile/14104080800348407575noreply@blogger.comtag:blogger.com,1999:blog-13911304.post-1119915916791544602005-06-27T16:20:00.000-07:002005-06-27T16:49:27.690-07:00Playing with Struts Day 1<span style="font-size:85%;">Ok, so finally after looking at much examples, I thought I had a basic idea and this was too trivial to actually go about trying. But today out of sheer boredem, I exported the struts-blank.war into Tomcat and got hacking at it. So here goes my tryst with struts:<br /></span> <ul> <li><span style="font-size:85%;">2:15 PM Ok then, I edit struts-config.xml: create a simple logonForm extending DynActionForm, add action logon, map the action to logonForm and a LogonAction class, that I will code in a bit.</span></li> <li><span style="font-size:85%;">2:30 PM I create the jsps using the tags.</span></li> <li><span style="font-size:85%;">2:40 I get interrupted, they want me to do some work actually.</span></li> <li><span style="font-size:85%;">3:30 PM Ok where was I? Right the jsps; I write a simple action class: LogonAction, in which I check the username and password against hard-coded values and if they match, forward the page to success, if not forward to failure.</span></li> <li><span style="font-size:85%;">3:35 PM I am all compiled and ready to go, I fire up Tomcat and go to the logon page.</span></li> <li><span style="font-size:85%;">3:40PM Hurrah! I can actually see the logon page, I hit submit, the page goes to logon.do, but empty page.</span></li> <li><span style="font-size:85%;">3:45 PM I think there might be something wrong in the Action class, so I comment the very cryptic username, password checking logic and just forward the page to success. I fire up Tomcat again.</span></li> <li><span style="font-size:85%;">Fast Forward: a few iterations, debugs, logs and System.out.println later:</span></li> <li><span style="font-size:85%;">4:30 PM Still no luck, also anything I log or write to sysout does not show up in the logs, so probably the action calss is not even getting invoked.</span></li> <li><span style="font-size:85%;">4:45 PM I tear out my hair, google for "struts action empty page", there is something about previous Action API requiring you to implement perform and current ones needing you to implement execute.</span></li> <li><span style="font-size:85%;">5:00 PM So, sure enough I am impelmenting perform, I just change this to execute and try again, Hallelujah!, I am strutting away to glory.</span></li> </ul> <span style="font-size:85%;">Moral of the story when in "Action", "execute", do not "perform".<br />So much for struts.<br /></span>Java Foohttp://www.blogger.com/profile/14104080800348407575noreply@blogger.comtag:blogger.com,1999:blog-13911304.post-1119626170107512312005-06-24T08:08:00.000-07:002005-07-19T21:07:22.536-07:00Inversion of control/Dependency InjectionA very interesting way of looking at Inversion of Control by Craig Walls in his Spring in Action Book:<br />"You can think of IoC as JNDI in reverse— instead of an object looking up dependencies from a container, the container gives the dependencies to the object at instantiation without waiting<br />to be asked."<br /><br /><a href="http://www.martinfowler.com/articles/injection.html">Article</a> by Martin Fowler.<br /><br />This is from an <a href="http://javaboutique.internet.com/tutorials/spring_frame/">article</a> on Spring Framework by Benoy Jose, for quick reference.<br /><-- Start Snippet --><br />There are three types of Dependency Injections.<br /><ul> <li>Type 1 IOC also called Interface Injection In Type 1 IOC the injection is done though an interface. The interface will define the injection method and the implementation class has to implement this interface and provide concrete implementation for the injection method. Pico Container uses this.</li> <li>Type 2 IOC also called Setter Injection In Type 2 IOC the injection is done via a setter method. Type 2 IOC uses setter methods to get the dependent classes it needs. Spring uses this.</li> <li>Type 3 IOC also called Constructor Injection. In Type 3 IOC implementing class defines a constructor to get all its dependents. The dependent classes are defined in the constructor arguments. Avalon uses this.</li> </ul> <-- End Snippet -->Java Foohttp://www.blogger.com/profile/14104080800348407575noreply@blogger.comtag:blogger.com,1999:blog-13911304.post-1119585453848006792005-06-23T20:49:00.000-07:002005-06-23T20:57:33.853-07:00Byte Streams Character StreamsI can't believe I how people have programmed in Java, myself included, without really understanding how Byte Streams are different from Character Streams. There is an excellent article <a href="http://java.sun.com/developer/technicalArticles/Streams/ProgIOStreams/">here</a><br />Some pointers:<br /><ul> <li><span style="font-size:100%;">If you see <code>Reader</code> or <code>Writer</code> think character stream and if you see <code>InputStream</code> and <code>OutputStream <span style="font-family: georgia;">think bytes.</span></code></span></li> <li><span style="font-size:100%;"><span style="font-family: georgia;">Thumb rule: use byte stream for binary data and character stream for everything else.<br /> </span></span></li> </ul>Java Foohttp://www.blogger.com/profile/14104080800348407575noreply@blogger.comtag:blogger.com,1999:blog-13911304.post-1119583881248600842005-06-23T20:20:00.000-07:002005-06-23T20:36:19.750-07:00EJB, again, best practices, patterns<span style="font-weight: bold;"><span style="font-family:verdana;"><span style="font-size:85%;">Session Facade<br /><span style="font-weight: bold;"></span></span></span></span><span style="font-family:verdana;"></span><span style="font-family:verdana;"><span style="font-size:85%;">Consider two Entity Beans and a client for ex: Employee and Paycheck. So you find the Employee, create a new Paycheck, populate some of the employee details or based on some values in Employee, you set some values on Paycheck. So you have all this business logic noise all over the client and the overhead of managing the entity beans. Enter Session Facade: You have a Session bean, that has a single method called managePaycheck, all the client has to do is call this method and all the noise moves to the Session bean.<br /><br /><span style="font-weight: bold;">Value Object/Data Transfer Object<br /></span>Value objects are used to transfer bulk data between tiers. So if you have a remote object (entity bean) and you are calling a multitude of get methods on that object. It makes sense to create a single ValueObject containing all the data needed. So now you are just calling getValueObject on that Entity bean (provided ofcourse the entity bean populates all the fields in the ValueObject first).<br /><br /><span style="font-weight: bold;">Use of dirty flags in the EntityBean<br /></span>Use dirty flags in ejbStore, basically init this as false in ejbLoad and set it to true in the setter. In ejbStore check this flag and only go ahead if it is true.<span style="font-weight: bold;"><br /></span><br />More to come...<br /><span style="font-weight: bold;"></span></span></span><span style="font-weight: bold;"><span style="font-family:verdana;"></span></span>Java Foohttp://www.blogger.com/profile/14104080800348407575noreply@blogger.comtag:blogger.com,1999:blog-13911304.post-1119581456375447572005-06-23T19:36:00.000-07:002005-06-23T19:50:56.380-07:00EJB in two minutes: a refresher<ul> <li>More detail <a href="http://members.tripod.com/gsraj/ejb/chapter/">here</a><br /> </li> <li>You have your EJB server, container, EJBs and clients.</li> <li>Entity Beans (think persistence), Session Beans (stateful/stateless), Mesage Driven Beans (needs no interface)</li> </ul> EJBs have: <ul> <li>Home Interface: provides methods for locating, creating, and removing instances of EJB classes, home object is implementation</li> <li>Remote Interface: is the client facing interface, it has all the business methods.</li> <li>Enterprise Java Bean: Is in the container result of deploying home and remote interface.</li> <li>Create home interface by extending EJBHome</li> <li>Create Remote interface by extending EJBObject</li> <li>Define primary key object.</li> <li>Create your Entity Bean implementing EntityBean</li> </ul> Typical lifecycle: <ul> <li>Locate home object, create or find remote object from home object, apply business methods on remote object<br /> </li> </ul>Java Foohttp://www.blogger.com/profile/14104080800348407575noreply@blogger.comtag:blogger.com,1999:blog-13911304.post-1119576638820588722005-06-23T18:19:00.000-07:002005-06-23T18:30:38.823-07:00What do you look for in Performance<ul> <li>When selecting rows from database, is filtering being done by SQL or by java.</li> <li>Referencing and de-referencing objects, especially in recursion.</li> <li>File processing, are you buffering your reads or reading the entire file into a glob of String.</li> <li>Cache read-only data.</li> <li>See where the code is spending more amount of time</li> <li>More to come...<br /> </li> </ul>Java Foohttp://www.blogger.com/profile/14104080800348407575noreply@blogger.comtag:blogger.com,1999:blog-13911304.post-1119575724903625262005-06-23T18:08:00.000-07:002005-06-23T18:15:24.906-07:00What do you look for in Code reviews?We do a lot of this unconsciously during code reviews, but when asked we turn a blank face.<br /><ul> <li>First the usual suspects, is the code doing what is functionally required, business logic, especially if the team member is new</li> <li>See that there are no hard-codes, standards are being followed.</li> <li>Is there chance to make something configurable, like lookups. See that these are being read from properties files or the database, that way any new additions don't need a code release.</li> <li>Are connections, jndi lookups being cached.</li> <li>Code re-use, both classes and methods.</li> <li>Kludge.</li> <li>Performance considerations, StringBuffer vs String and so on.<br /> </li> </ul>Java Foohttp://www.blogger.com/profile/14104080800348407575noreply@blogger.comtag:blogger.com,1999:blog-13911304.post-1119575229228538032005-06-23T18:02:00.000-07:002005-06-23T18:07:09.230-07:00Pass by value vs. Pass by referenceAlways pass by value, if you ever say pass by reference, you will be rejected for sure. (in the interview that is)<br />You don't believe me, this<a href="http://javadude.com/articles/passbyvalue.htm"> guy</a> explains it far more better than I ever can.Java Foohttp://www.blogger.com/profile/14104080800348407575noreply@blogger.comtag:blogger.com,1999:blog-13911304.post-1119574805748162612005-06-23T17:56:00.000-07:002005-06-23T18:00:05.750-07:00Checked vs. Unchecked Exceptions<ul> <li><span style="font-size:85%;">Checked exceptions are checked at compile time (not exactly checked, but the compiler knows will occur since they are declared as being thrown during compile time), so the compiler will crib if you don't handle them.<br /> </span></li> <li><span style="font-size:85%;">Unchecked exceptions only occur at runtime, mostly extend RuntimeException, these can't be checked, usually occur due to program errors, such as NullPointerException</span></li> <li><span style="font-size:85%;">There have been holy wars waged over this topic.<br /> </span></li> </ul>Java Foohttp://www.blogger.com/profile/14104080800348407575noreply@blogger.comtag:blogger.com,1999:blog-13911304.post-1119574401058266702005-06-23T17:49:00.000-07:002005-06-23T17:53:21.063-07:00Difference between Abstract and Interface<ul> <li>Interface only provides the contract, does not add default behaviour, whereas abstract classes provide default behaviour (not necessarily, only if you want to, but that is the point)<br /> </li> <li>You can implement as many interfaces as you may want, but only extend from one abstract class</li> <li>You cannot have private, protected variables in an Interface.<br /> </li> </ul>Java Foohttp://www.blogger.com/profile/14104080800348407575noreply@blogger.com