Month: November 2004

Safari, Fun with struts, recommended books

Safari

If you do a lot of software development and get a lot of books from O’Reilly I recommend Safari.

Struts

Decided to use struts to do my project because it works and I like the architecture.

I’ve spent the last week or so getting my head round how it works, how to use Ant (like it), and how to get Netbeans to work with it.

Not that hard, any of it, but I’m basically using Netbeans as a glorified editor and not using any of the advanced features (like the in-line J2EE container, for example).

Netbeans 4 beta is a little flakey when you try and create new Java classes and doesn’t seem to want to save them. If you press save all it will, though.

Books

If you want to get started with struts try:

Struts Kick Start
ByJames Turner, Kevin Bedell

Also has the best potted explanation of how HTTP, JSP. JSTL tags etc. I’ve seen. Wish I’d read this a couple of years ago.

I’ve got a lot from

Information Architecture for the World Wide Web, 2nd Edition
ByPeter Morville, Louis Rosenfeld

Ignore the first 3 chapters unless you are a total newbie. Useful content there.

Overcoming the stupids

I had an attack of the stupids. I was wrapping a form served by a different server into a portlet and passing what it had been sent back. It had about 15 arguments and I had dutifully created one argument in my portlet for each thing that could be sent. Most of these were <input type=”image”> tags, which send an X and Y co-ordinate of where the image was clicked, but only if the image was clicked.

I was faced with going through all of these by hand and if one of them was set, that was the one to send to the remote machine.

Now, to pass things around Portal they need to be page parameters also. This meant I was faced with creating 15 page parameters to get the portlet working.

Then, to add to the fun, Portal won’t let you create page parameters of the form userClick.x; it doesn’t like the ‘.’ in the name.

Aha, says I, I will create a redirect servlet that will transliterate userClick.x (and the other pile of arguments) into userClickX and then send it back. Fortunately there are methods that will allow you to generate a URL to do a callback like this.

Then in the portlet, if the argument was set, translate it back again so I can call out to the external server.

Phew!

This was my attack of the stupids. Looking at things like they are significant when they’re not. What was significant was the URL being sent to the back-end map server, which the browser had aldready constructed for me.

Instead all I needed to do was send the two housekeeping variables back (cookies and some portal-specific things) and a new variable called mapParams, which it constructed from looping through the parameters and ignoring the ones I wanted to send back in their own right. This was then escaped using the URL encode and placed in the variable.

Back in the portlet, just append this to the string to go to the back-end and off y’go.

So, 15 arguments and a lot of repeated code down to 3. Just make sure you are looking at the right problem.

Back to writing functional specs, can’t have fun all day.

Permission and Change

If you hurt someone else you give them permission to hurt you back because you make hurting people OK. This is quite tricky to understand. It’s one of the ways that karma works. Karma just means “actio”, like Newton’s law:

For every action there is an equal and opposite reaction

What a lot of people don’t realise is that this is true for everything. This is what the Buddha discovered two and a half thousand years ago. Think about what this means in Iraq and other places. Normalising terror and so on. I look at the world around me and worry where the self-righteous crap people are peddling will take us all, without most of us even realising it.

I am trying to stop giving people the finger when they try and drive me off the road and also not rise to their bad driving. This means that I must be much calmer. Returning their aggression gives them permission to carry on being and behaving the way they do. Everything involves you giving someone else permission in some way. That’s why I’d rather be kind any time.

Change is odd, as well. It gives permission to think about things differently and it also turns things on their head. The fear of change is one of the things that you need to learn to do without. It’ll happen anyway – start thinking differently now, be ready. None of the things you hold dear need be true, even slightly; more worryingly they may be true in the senses that you can’t conceive of.

The phenomenal world we live in is constructed in your mind by processes we don’t understand. We agree with each other on the labels for things and not much else. For example in the Buddhist canon there is the following example around a glass of water:

  • A god would see it as ambrosia
  • A human would see it as water
  • A hell-being would see it as something burining and nasty to drink

Even a human being would see different things, depending upon how thirsty they were, or even if they had contracted a disease where they have hydrophobia.

Now look what you made me do

I once lived with someone whose entire life revolved around these words. The only thing that she was responsible for was having a dim view of everyone else.  You can’t win with someone who uses this as their mantra, so don’t even try. If you live your life like this expect to be unhappy. Unhappiness starts to go when you manage to take responsibility for things and stop assuming that the world revolves around you. It never did. Get over it.

More fun with Oracle Java Portlets

The manual is incredibly opaque. I’m beginning to realise why paper manuals weren’t so bad, it was easier to work through things.

I can’t work out how to pass parameters into a porltet or default them in from the page (so you can pass them between portlets). Starting again with the noddy example from the manual to see where I’ve gone wrong.

This works … adding another parameter needed a refresh of the provider – of course – obvious. It looks like I needed to refresh the provider. Naively assumed that it was doing this on the fly when I reloaded the J2EE app. It must cache the provider.xml file in the portal (of course it does dumbo).

I now have the wrapper working after a fashion. I have no idea why this has to be so hard.

Letter to “spiked online”

This is in response to this article:

I always find myself a bit worried when people use words like progress and enlightenment like they’re in some sense automatic.

Another example is when people use “evolve” to mean things getting better, where “better” is a value judgement, when all it means is that they get better adapted to the prevailing conditions, which in themselves have no intrinsic value at all.

We haven’t progressed from anywhere. The project to better the human condition is a human construct, not an absolute, and so is enlightenment. This is laudable and I agree with it, but I don’t use progress in what is a naive Victorian way that makes it some sort of moral absolute (sorry Karl).

I did agree with the main thrust of the article’s attack on the silly self-serving abdication of personal responsibility, which is so much a part of life now. It also begs the question, that if I apparently know more than a journalist who specialises in something, what the hell does he or she do all day? If I were an editor I would get someone else! But the culture of “shut up, shape up or ship out” seems to have gone as well.

Seems like a charter for the work shy. Maybe I’m old fashioned.

Search and replace on a stream using 1.4 JDK CharSequence from an InputBuffer

My friend Roger informs me that this is a single library call in PHP. I once did this by using jtidy so I could get the html tags using an XML DOM, but that was very silly.

Been tearing my hair out – I want to be able to take the output from a URL and put it into a portlet. I therefore want to rewrite where the <form> tag sends back from one server so that I can put the portlet on the front of it.

1.4 JDK has a lovely regular expression thing so I don’t have to import the GNU RegExp libraries.

Great says I, only problem is that it uses the new CharSequence interface.

All of the examples are about reading files, not the contents of a URL, which I only seem to be able to get as an InputStream, which isn’t the same as a FileInputStream, and there’re all kinds of hoops you have to jump through that InputStream doesn’t have methods to deal with.

OK, have a look at the example in Java Examples in a Nutshell. It uses a byte[] buffer to read from the URL’s inputstream. Maybe I can put this into a StringBuffer and then use it because it implements the interface.

StringBuffer, quite logically, doesn’t have a way of appending arbitrary numbers of bytes from an array, only chars.

Then, dear reader, I realised that an InputStreamReader class will read chars. Finally, after about 2 hours googling my bonce off:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class RewriteURL 
{
  /**
   * 
   * @param in : the char sequence to replace
   * @param searchFor : what to search for
   * @param replaceWith : what to replace it with
   * 
   * Adapted from http://java.sun.com/developer/JDCTechTips/2002/tt0604.html#tip2
   */
  private static String replace
    ( CharSequence in
    , String searchFor
    , String replaceWith
    )
  {
    StringBuffer out = new StringBuffer() ;
    // Create a pattern to match search
    Pattern p = Pattern.compile(searchFor);
    // Create a matcher with an input string
    Matcher m = p.matcher(in);
    boolean result = m.find();
    // Loop through and create a new String 
    // with the replacements
    while(result) {
        m.appendReplacement(out, replaceWith);
        result = m.find();
    }
    // Add the last segment of input to 
    // the new String
    m.appendTail(out);
    
    return out.toString();
  }

  /**
   * 
   * @param args
   */
  public static void main(String[] args) throws Exception
  {
    
    URL url = new URL("http://some-server/someurl") ;
    InputStreamReader urlContents = new InputStreamReader(url.openStream());
    
    StringBuffer sb = new StringBuffer() ;
    
    char [] buffer = new char[4096] ;
    
    int bytes_read;
    while( ( bytes_read = urlContents.read(buffer)) != -1 )
    {
      sb.append(buffer,0,bytes_read) ;
    }
    
    System.out.println(replace( sb, "mvtest2.jsp", "someOtherForm.jsp" ));
}

I’m not sure how well this would work with a large file, but for trivial stuff it’s OK. There are some useful things on the GNU Regexp site as well

Firefox

I decided to try using Mozilla Firefox as my browser of choice. After I got the Java and Flash plug-ins installed it works fine. I like it a lot, particularly the tabbed interface, where you aren’t constantly going back to favorites over and over again.

I really like right-click, open in new tab!

One thing about tabs, though, there is a security vulnerability which is currently being fixed in that a naughty website can pop-up a confirm your password dialog box while you are in a secure tab. So, if you want to talk to your bank etc. then close all tabs or start a new window without them.

I hate these hacker bastards, wasting my time and energy.

Oracle JInitiator

Found this on another web site, you just need to go to c:program filesOracleJInitiatorx… (there’s one for each JInit you’ve installed) and copy the NPJinitXXXX.dll’s into the firefox plug in directory. These must be the JInitiatior files for NetScape, which Firefox understands.

Blog City

I’m quite happily using Firefox with the Java plug-in except for a minor thing with pressing the return key not inserting <p> tags. I’ve raised it but it’s not that much of a problem TBH.