Extending the Hermes FTP Server

Its IoC based architecture allows for extending or customizing the Hermes FTP Server easily. Moreover, if you need to embed the ftp server in your own application you certainly can do so. The following paragraphs describe both of the scenarios.

Customizing the FTP Server

In this example we extend the ftp server by an SITE command returning server specific information to the caller.

Open the application context hermesftp-ctx.xml and find the parser bean. Now add the highlighted line to the list of properties:

    <bean id="parser" singleton="true" 
             class="net.sf.hermesftp.parser.impl.FtpCmdParserImpl">
		<property name="commands">
			<map>
				...
				<entry key="SITE" value="cmdSite" />
				...
			</map>
		</property>
	</bean>

Add a new bean to the application context and name it cmdSite.

   	<bean id="cmdSite" class="example.FtpCmdSite" singleton="false" />

Now open your IDE (if not already done) and extend the class net.sf.hermesftp.cmd.AbstractFtpCmd as outlined in the snippet below.


package example;

import net.sf.hermesftp.cmd.AbstractFtpCmd;
import net.sf.hermesftp.exception.FtpCmdException;

public class FtpCmdSite
    extends AbstractFtpCmd {

    /**
     * {@inheritDoc}
     */
    public void execute() throws FtpCmdException {

        // handle command

        if (ok) {
          msgOut(MSG200_NOTED);
        } else {
          msgOut(MSG500;
        }
        
    }

    /**
     * {@inheritDoc}
     */
    public String getHelp() {
        return "My custom site command.";
    }

    /**
     * {@inheritDoc}
     */
    public boolean isAuthenticationRequired() {
        return true;
    }


After wrapping the compiled class into a JAR file you must copy the archive into the folder $HERMES_HOME/plugins. All classes found in this folder are loaded automatically after starting up the application. In the same way all parts of Hermes FTP Server represented by a Spring bean can be replaced.

Embedding the FTP Server

The snippet below demonstrates how to fire up Hermes FTP Server from within your own application.

    private void startServer(String beanRes) throws FtpException {

        ApplicationContext appContext = 
           new FileSystemXmlApplicationContextEx (new String[] {beanRes});

        FtpServer svr = (FtpServer) appContext.getBean(
            BeanConstants.BEAN_SERVER);
        Thread svrThread = new Thread(svr);
        svrThread.start();
    }