22.08.2013 Views

ColdFusion Developer's Guide

ColdFusion Developer's Guide

ColdFusion Developer's Guide

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Beta Beta Beta Beta Beta Beta Beta Beta Beta Beta<br />

■ MyGateway(String gatewayID, String configurationFile)<br />

■ MyGateway(String gatewayID)<br />

■ MyGateway()<br />

When <strong>ColdFusion</strong> starts, it calls the constructor for each event gateway instance that you<br />

configure in <strong>ColdFusion</strong>. (<strong>ColdFusion</strong> also calls the gateway Start method after the event<br />

gateway is instantiated.). <strong>ColdFusion</strong> first attempts to use the two-parameter constructor.<br />

Because each event gateway instance must have a unique ID, <strong>ColdFusion</strong> provides redundant<br />

support for providing the ID. If the event gateway implements only the default constructor,<br />

<strong>ColdFusion</strong> provides the ID by calling the event gateway’s setGatewayID method.<br />

If the event gateway does not implement the two-parameter constructor, it does not get<br />

configuration file information from <strong>ColdFusion</strong>.<br />

The constructor normally calls the static GatewayServices.getGatewayServices method<br />

to access <strong>ColdFusion</strong> event gateway services. Although you need not do this, it is a good<br />

coding practice.<br />

A minimal constructor that takes only a gateway ID might look like the following:<br />

public MyGateway(String gatewayID) {<br />

this.gatewayID = gatewayID;<br />

this.gatewayService = GatewayServices.getGatewayServices();<br />

}<br />

The gateway constructor must throw a coldfusion.server.ServiceRuntimeException exception<br />

if there is an error that otherwise cannot be handled. For example, you should throw this<br />

exception if the event gateway requires a configuration file and cannot read the file contents.<br />

If your gateway uses a configuration file, the constructor should load the file, even if the<br />

Start method also loads the file. You should do this because the constructor does not run in<br />

an independent thread, and <strong>ColdFusion</strong> can display an error in the <strong>ColdFusion</strong> Administrator<br />

of the file fails to load. If the Start method, which does run in a separate thread, fails to load<br />

the file, <strong>ColdFusion</strong> logs the error, but it cannot provide immediate feedback in the<br />

administrator.<br />

The sample Socket event gateway has a single constructor that takes two parameters. It tries to<br />

load a configuration file. If you specify a configuration file in the <strong>ColdFusion</strong> Administrator,<br />

or the file path is invalid, it gets an IO exception. It then uses the default port and logs a<br />

message indicating what it did. The following example shows the Gateway constructor code<br />

and the loadProperties method it uses:<br />

public SocketGateway(String id, String configpath)<br />

{<br />

gatewayID = id;<br />

gatewayService = GatewayServices.getGatewayServices();<br />

// log things to socket-gateway.log in the CF log directory<br />

1608 Creating Custom Event Gateways

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!