Finding What is Not There in Eclipse

In my current project there is a need to add some parameters to portlet definition files. The first pass through we only updated the ones we knew needed the update. As is often the case, what we knew then and what we know now changed, and it became a better approach to simply add the values to all of the portlet definition files. Rather than sorting through one by one to find which had not been updated, I did a search and found a stackoverflow thread about how to do these searches in Eclipse. The thread included the following example of a regular expression for finding files that do not include a particular value:

(?s)A((?!YourSearchText).)*Z

For those who like pictures, below is how the search box is used with regex to find files missing the string:

EclipseRegExSearchExample

Facebooktwitterredditlinkedinmail
© Scott S. Nelson

Catching the Un-Catchable

I know it is bad practice to catch java.lang.NoClassDefFoundError.  Today, however, I did need a way to deal with it because I have this annoying habit of wanting everything else to work even when some non-critical piece is broken.

This case was a matter of a third-party security jar that would throw up rather than out when it could not find its properties file. No matter how many catches I had wrapped around the call to the method in the jar, the java.lang.NoClassDefFoundError would bubble up as a big render issue in the JSP.  I am only using this jar to get the user display name, so it isn’t like my application is not secure if I don’t have their name to make them feel special.

After several annoying and pointless approaches, I came up with the following (ugly-but-effective) solution:

try //the Security class relies on this property file but pukes if not found, 
    //so we test for it here
{
   FileInputStream in = new FileInputStream("security.properties");
   in.close();
}
catch(Exception e)
{
   System.out.println("security.properties is missing from classpath");
   return personInfo;
}
//...go use the third-party class that depends on the file having proven it is there

This allowed the application to continue running and not falling apart at the header just because one file was missing. It is one thing to have a bug that is minor, another when that bug becomes something that users pick up the phone about.

Facebooktwitterredditlinkedinmail
© Scott S. Nelson

Dynamic Log Location for log4j

I was really surprised that log4j doesn’t have any facility to set the log file location with a relative path. Hard to build a portable application if you need to set the full value of a local path. And don’t talk to me about config files, because that requires reading documentation, which is only read less than it is written.

So, if you use the standard log4j servlet approach to start your logging, you can weave in the following to set your path relative to your app:

org.w3c.dom.NodeList nodes = doc.getElementsByTagName("param");;
org.w3c.dom.Node node = null;//nodes.item(0);
org.w3c.dom.NamedNodeMap nodeMap = null;
for(int i=0; i < nodes.getLength(); i++)
{
    node = nodes.item(i);
    nodeMap = node.getAttributes();
    if(nodeMap.getNamedItem("name")
        .getNodeValue().equals("File"))
    {
        node = nodeMap.getNamedItem("value");
        outputFile = new File(getServletContext().getRealPath(node.getNodeValue()));
        outPutPath = outputFile.getAbsolutePath();
        outPutPath = outPutPath.replace('', '/');
        node.setNodeValue(outPutPath);
    }
}

Then you can set the log location to outPutPath.

Enjoy 🙂

Facebooktwitterredditlinkedinmail
© Scott S. Nelson

Suppress Eclipse Warnings Annotation

When I search for something too many times, I try to remember to post it here.  So, to drop the warnings for type safety when you know all is fine, you can do the following:

@SuppressWarnings("unchecked")
List<CustomerBean>    customers    = (List<CustomerBean>)session.getAttribute(CSRD_CONTACT_LIST);

 

Facebooktwitterredditlinkedinmail
© Scott S. Nelson

Permission Error on Delete Directory for Java Projects on Windows

I wish I had take a screen shot of the error to make this easier for folks to say “oh, yeah, I have that problem”. The thing is, when you build some J2EE applications from a project on the Desktop or My Documents (which I am only now starting to use out of convenience for back up programs that think that is where things should be stored) and then are done with them you find you can’t delete them.  You get this annoying warning that you might not have permission to do so, even though your are the administrator in Windows and the owner of the files.

This is because of a bug in Windows meant to annoy those of use who like having the directory structure match the namespace.  The generated name spaces are often too long when combined with all that extra path crud that goes to My Documents or Desktop for the OS to handle. So, the simple solution is take the folder and drag it into the root of the drive and then delete it. Apparently, only delete is crippled by this bug and not move. Go figure.

And, yes, I know that Microsoft bashing is cheap and easy. So am I, which is why I do it. I am fully aware that if it were not for Microsoft I would most likely be living in a trailer wondering why a guy that likes and understands something as complex as programming is ignored by Corporate America and treated like a warehouse worker (the way it was before Bill was a Billionaire).  Thank you Bill, for everything 🙂

PS: Microsoft bugs still piss me off.

Facebooktwitterredditlinkedinmail
© Scott S. Nelson