<property name="transportType">
<bean id="com.ibm.mq.jms.JMSC.MQJMS_TP_CLIENT_MQ_TCPIP"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
</property>
Tons Notebook
Ton van Bart's brain dump on the net: mostly Java and computer stuff.
Tuesday, September 06, 2011
Retrieving static constants with Spring
In a Spring configuration it is possible to inject constant values defined in some class (or interface). For this you don't need to know the actual value of the constant, but can instruct Spring to retrieve it. For instance for an IBM MQ TransportType:
Unit testing with JavaMail
It can be inconvenient to unit test a class which uses Javamail (needs
SMTP server, hard to verify email contents).
It is possible however to replace the standard mail Transport with a
mock implementation.
First you need two files in META-INF which make sure the mock gets used;
in a Maven project, src/test/resources would be a good place for these.
The file javamail.providers gives the transport class to use for the smtp protocol:
# set the javamail default provider to a mock implementation for SMTP only. # protocol=smtp; type=transport; class=com.myCompany.MockTransport; vendor=Acme Corporation;In addition, the file javamail.address.map maps address types to the transport to use, its contents are:
rfc822=smtpFinally, you need a mock Transport subclass which overrides the connect() methods and adds an implementation for sendMessage(). This example stores the last message sent in a static member; this makes it possible to retrieve the message afterwards in a unit test by using MockTransport.getLastMessage().
package com.myCompany;
import javax.mail.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Mock implementation of {@link Transport} for unit testing. This needs property file overrides
* in a META-INF on the classpath in order to work; this is in src/test/resources .
* See http://sujitpal.blogspot.com/2006/12/mock-objects-for-javamail-unit-tests.html for more information.
* This implementation stores the last message sent and adds a getter for retrieval and verification.
* @author bartas
*/
public class MockTransport extends Transport {
private static final Logger logger = LoggerFactory.getLogger(MockTransport.class);
private static Message lastMessage;
public MockTransport(Session session, URLName urlName) {
super(session, urlName);
logger.warn("constructed MockTransport instance - javamail is mocked");
}
/**
* Stores the message to send in this instance.
* @see javax.mail.Transport#sendMessage(javax.mail.Message, javax.mail.Address[])
*/
@Override
public void sendMessage(Message arg0, Address[] arg1)
throws MessagingException {
String subject = arg0.getSubject();
StringBuffer addresses = new StringBuffer("[");
for (int i=0; i<arg1.length; i++) {
addresses.append(arg1[i]);
if (i<arg1.length-1) addresses.append(",");
}
addresses.append("]");
logger.debug("sendMessage(\"{}\",{})", subject, addresses.toString());
lastMessage = arg0;
}
@Override
public void connect() throws MessagingException {
logger.debug("connect()");
}
@Override
public void connect(String arg0, int arg1, String arg2, String arg3)
throws MessagingException {
logger.debug("connect({},{},{},{})", new Object[] {arg0,arg1,arg2,arg3});
}
@Override
public void connect(String arg0, String arg1, String arg2)
throws MessagingException {
logger.debug("connect({},{},{})", new Object[] {arg0,arg1,arg2});
}
@Override
public void connect(String arg0, String arg1) throws MessagingException {
logger.debug("connect({},{})", arg0,arg1);
}
@Override
public synchronized void close() throws MessagingException {
logger.debug("close()");
}
public static Message getLastMessage() {
return lastMessage;
}
}
Thursday, March 29, 2007
Wednesday, March 14, 2007
Decompiling a jar file on windows
One way to do this (and I can never remember the command, which is why I blog it):
There are of course other decompilers available but this will do it in a pinch.
If you are using Vim, be sure to also check out the
jad plugin for Vim. With this, you just right-click on a .class file, select "edit with Vim", and have the decompiled class show up in your editor. If you are on Eclipse (or IBM RAD), use the JadClipse plugin to decompile right in you IDE.
- unpack the jarfile using winzip or the "compressed file" tool on WinXP.
- use the MS-DOS "for" command combined with the JAD decompiler to decompile:
for /R %F in (*.class) do jad -r -ff -s java %F
This will recurse over all the classfiles, decompiling them in turn and leaving the original package/directory structure intact.
There are of course other decompilers available but this will do it in a pinch.
If you are using Vim, be sure to also check out the
jad plugin for Vim. With this, you just right-click on a .class file, select "edit with Vim", and have the decompiled class show up in your editor. If you are on Eclipse (or IBM RAD), use the JadClipse plugin to decompile right in you IDE.
Sunday, February 26, 2006
Running https on port 443 in a WSAD test server
Having started a new job in Februari, it has been a busy few weeks with not much time (or energy) for blogging.
Setting up a development environment for one of my new projects last week, I found that I had problems getting a test server in WSAD to run https on port 443. Any other port (like the default 9443) would work just fine; but if I added a https transport on port 443 the server log would show "unable to start transport https" and the server would fail to start. The stack trace, as well as any mail thread I could find, would indicate that most likely something else was already running on port 443. None of my new collegues knew what could be wrong, although several remembered having had trouble setting up their own environment.
I had already triple-checked and was sure port 443 was not in use. Furthermore, near the top of the stacktrace, I saw a NullPointerException which led me to believe something was somehow wrong with the https transport itself.
I must have spent a day searching and pulling my hair out before I stumbled across this article on IBM Developerworks; look about halfway down the page. As it turns out, you cannot add secure ports in the server configuration panel in WSAD; you can only modify the port number of the existing one. If you throw away the original https transport, as I did on one of my test server configurations, you will not be able to add a https transport on any port (not even 9443) and have the server start; at least not using the server configuration panel. If you need https on port 443, the simplest way is to create a server using the default ports (9080 and 9443) and then modify the https to use 443. I did this and added http on port 80, after which the server ran without problems.
Setting up a development environment for one of my new projects last week, I found that I had problems getting a test server in WSAD to run https on port 443. Any other port (like the default 9443) would work just fine; but if I added a https transport on port 443 the server log would show "unable to start transport https" and the server would fail to start. The stack trace, as well as any mail thread I could find, would indicate that most likely something else was already running on port 443. None of my new collegues knew what could be wrong, although several remembered having had trouble setting up their own environment.
I had already triple-checked and was sure port 443 was not in use. Furthermore, near the top of the stacktrace, I saw a NullPointerException which led me to believe something was somehow wrong with the https transport itself.
I must have spent a day searching and pulling my hair out before I stumbled across this article on IBM Developerworks; look about halfway down the page. As it turns out, you cannot add secure ports in the server configuration panel in WSAD; you can only modify the port number of the existing one. If you throw away the original https transport, as I did on one of my test server configurations, you will not be able to add a https transport on any port (not even 9443) and have the server start; at least not using the server configuration panel. If you need https on port 443, the simplest way is to create a server using the default ports (9080 and 9443) and then modify the https to use 443. I did this and added http on port 80, after which the server ran without problems.
Tuesday, January 31, 2006
Mastering Ajax, Part 2: Make asynchronous requests with JavaScript and Ajax
IBM Developerworks has a handy little tutorial, titled
Mastering Ajax, Part 2: Make asynchronous requests with JavaScript and Ajax. There's nothing really new or earth-shattering there; it does, however, have all the information about creating and handling cross-browser XMLHttpRequest objects in one place.
Mastering Ajax, Part 2: Make asynchronous requests with JavaScript and Ajax. There's nothing really new or earth-shattering there; it does, however, have all the information about creating and handling cross-browser XMLHttpRequest objects in one place.
Sunday, January 29, 2006
Running Cygwin from USB
Normally, installing cygwin on a PC means that you'll need administrative privileges on the system. This is needed because the cygwin startup needs several mount points to be set, which are stored in the Windows registry.
However, it is possible to install cygwin on a USB stick, and run it on any host PC without the need for admin privileges and/or registry hacks. The following batch script will figure out the correct drive letter for the stick, create the mount points, and start cygwin, and has been tested in Windows 2000 and XP; you only need to have write access to c:\temp on the host machine.
However, it is possible to install cygwin on a USB stick, and run it on any host PC without the need for admin privileges and/or registry hacks. The following batch script will figure out the correct drive letter for the stick, create the mount points, and start cygwin, and has been tested in Windows 2000 and XP; you only need to have write access to c:\temp on the host machine.
@echo off
for /F "delims=\: usebackq" %%i in (`cd`) do SET USB_DRIVE=%%i
echo USB driveletter is %USB_DRIVE%
REM pause
SET CYGWIN=tty title
SET PATH=%USB_DRIVE%:\cygwin\bin;%PATH%
echo PATH has been set to %PATH%
echo.
@echo on
mount -f -u -b %USB_DRIVE%:/cygwin /
mount -f -u -b %USB_DRIVE%:/cygwin/bin /usr/bin
mount -f -u -b %USB_DRIVE%:/cygwin/lib /usr/lib
@echo off
echo User mount points have been created.
echo.
echo @ECHO OFF > c:\temp\usbcygwin.bat
echo %USB_DRIVE%: >> c:\temp\usbcygwin.bat
echo CHDIR \cygwin\bin >> c:\temp\usbcygwin.bat
echo bash --login -i >> c:\temp\usbcygwin.bat
echo created c:\temp\usbcygwin.bat, starting cygwin.
echo.
c:\temp\usbcygwin.bat
Subscribe to:
Posts (Atom)