Friday, April 25, 2008

Castor Xml mapping: mapping.loadMapping() gives IOException for filepath

This is very know problem i think and many of you know it but then also i am writing it here.

For convert a Value Object class directly in pre-defined XML format we use castor mapping.We give xml mapping file (as we have seen in earlier tutorial) to Mapping class object as below which will write xml string in strWriter object from Object obj.
Mapping mapping = new Mapping();
mapping.loadMapping(mappingXmlFilename);
strWriter = new StringWriter();
Marshaller marshaller = new Marshaller(strWriter);
marshaller.setMapping(mapping);
marshaller.marshal(obj);
But when i given directly the filename to loadMapping method, it gives IOException as below:
IOException : \shared\p1\p2\shared\p1\p2\lai-sup-mapping.xml
i have given absolute filepath '\\shared\p1\p2\lai-sup-mapping.xml' and then also it is generating it using relative one.So throwing IOException for file.

Then i have changed the thing and instead of giving filepath, i have made a org.xml.sax.InputSource object from the xml file and passed that xml source to loadMapping() as below:

Mapping mapping = new Mapping();
InputSourcemappingSource =
newInputSource(new FileInputStream(mappingXmlFilename));

mapping.loadMapping(mappingSource);
strWriter = new StringWriter();
Marshaller marshaller = new Marshaller(strWriter);
marshaller.setMapping(mapping);
marshaller.marshal(obj);
And its working fine.Now its generating correct XML file from the given Value object.

Thursday, April 24, 2008

Servicemix Queue filled - Queue size increased

I have been using servicemix since more then 1 year.As we have seen in urlier articles that servicemix uses ActiveMQ for Message queuing. The messages sent are filled in the Queue of destined component which will be fetched later on.

When we started using Servicemix, we faced a strange problem.After running for sometimes,our application stops processing messages.When we looked in the JMS Queue, we found that after certain period the messages in the queue are not removing.So new messages can't be processed.The Queue is full and we must restart the application to get rid of this.

Then we dig into the source code of servicemix and found how queue is given size.We increased the size as per our need and the problem was solved.

Basically when message processed by the component, it will be removed from its queue.But for some reason, if message not processed or not removed from the queue then further messages will be stored in the queue and processing stops.The application seems to be running but messages are not processed.

The class org.apache.servicemix.jbi.util.BoundedLinkedQueue contains the property capacity_
Constructor of this class contains the magic which initializes the capacity of queue size.

By default it is 1024.you can change it as per your need. I mean how much messages you think will be processed after every restart.

code:
public BoundedLinkedQueue() {
this(1024);
}
public BoundedLinkedQueue(int capacity) {
if (capacity <= 0)
throw new IllegalArgumentException();
capacity_ = capacity;
putSidePutPermits_ = capacity;
head_ = new LinkedNode(null);
last_ = head_;
}
I just simply changes the 1024 to bigger number like 1024*10 etc...

Try this.may be it can solve your problem also.

And others who know about servicemix and want to share their problems and tweaks are welcome.Put your comments here i will post it.

Java Web application cookie lost!

I got one new change to do in our java web application.I have not been in it since 2 or 4 months and forgot some of its settings.

First of all my jboss refuses to run from eclipse!i was very worried about it, but later i re-configured it as a "dynamic web application" project in eclipse.

So when jboss runs finally, i logged in it and a big blast! it redirected me to error page. I checked the logs but i can't find anything.I was thinking that even i have not chnaged the things in code,configuration etc. then also it stucks here?

But suddenly, a spark happned and i remembered that we set domain for cookies in most web applications.And that thing sucks in local environment.
cookie setting code:
Cookie c1 = new Cookie(name, value);
c1.setDomain(domainName);
So i just commented out c1.setDomain(domainName); and my application is ok.

Monday, April 21, 2008

Java Mail API with Encoded Attachment FileNames

This is very strange problem i faces as per me. May be because different email clients have different encodings or something else, but we will get encoded filename header from some email clients like lotus, blackberry phone etc.

Message header for attachment:

Content-Transfer-Encoding: base64
Content-Type: image/jpeg
Content-Disposition:attachment;
filename="=?Windows-1252?B?SU1HMDAwMTYuanBn?="

Then i was stucked with the thing that how to decode the filename.I have the filename only in subject line.But i can not trust that because say if 3 attachments are there, then there will be 3 filenames in subject in order.But we should not depend on the subject of the email.

javax.mail.internet.MimeUtility util class which helps to decode the encoded strings in message header values as below

Method:
javax.mail.internet.MimeUtility.decodeText(strValueToBeDecoded);
But most of the encodings with java 1.3.1 are not supported. At least 1.4.1 must be used.

Friday, April 11, 2008

Eclipse Shortcuts

I was free as my code with CR is done. i was thinking wht to write and it just sparked in my mind that this can be a good thing though many of us has explored this.But then also it can help some newbies.
Maximize/minimize the selected window - Ctrl +M
Generate getters/setter - Alt+Shift+s + r
Override/implement methods - Alt+Shift+s + v
Show type implementation - Ctrl+T on selected type in java file
quick class member browser - Ctrl+o
search usage of selected member - Ctrl+Alt+H
comment/uncomment selected lines - ctrl+/
Open declaration - F3
This is what most used ones. You also can submit if i missed important ones in commnets.