1  /*
     2   * Copyright the original author or authors.
     3   * 
     4   * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   * 
     8   *      http://www.mozilla.org/MPL/MPL-1.1.html
     9   * 
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import org.as2lib.util.Stringifier;
    18  import org.as2lib.env.except.IllegalArgumentException;
    19  import org.as2lib.env.log.LogHandler;
    20  import org.as2lib.env.log.LogMessage;
    21  import org.as2lib.env.log.handler.AbstractLogHandler;
    22  
    23  /**
    24   * {@code XmlSocketHandler} uses the {@code XMLSocket} to log the message.
    25   * 
    26   * <p>It was originally designed to work with the POWERFLASHER's SOS
    27   * XML-Socket-Server but you can use it for any output device that is accessible
    28   * over the XML socket.
    29   * 
    30   * @author Simon Wacker
    31   * @see SosSocketHandler
    32   * @see <a href="http://sos.powerflasher.com">SOS - SocketOutputServer</a>
    33   */
    34  class org.as2lib.env.log.handler.XmlSocketHandler extends AbstractLogHandler implements LogHandler {
    35  	
    36  	/** Socket to connect to the specified host. */
    37  	private var socket:XMLSocket;
    38  	
    39  	/**
    40  	 * Constructs a new {@code XmlSocketHandler} instance.
    41  	 * 
    42  	 * @param host a fully qualified DNS domain name
    43  	 * @param port the TCP port number on the host used to establish a connection
    44  	 * @param messageStringifier (optional) the log message stringifier to use
    45  	 * @throws IllegalArgumentException if the passed-in {@code port} is {@code null}
    46  	 * or less than 1024
    47  	 * @todo throw exception when unable to connect
    48  	 */
    49  	public function XmlSocketHandler(host:String, port:Number, messageStringifier:Stringifier) {
    50  		if (port == null || port < 1024) {
    51  			throw new IllegalArgumentException("Argument 'port' [" + port + "] must not be 'null' nor less than 1024.", this, arguments);
    52  		}
    53  		this.socket = new XMLSocket();
    54  		this.socket.connect(host, port);
    55  		this.messageStringifier = messageStringifier;
    56  	}
    57  	
    58  	/**
    59  	 * Uses the xml socket connection to log the passed-in message.
    60  	 *
    61  	 * <p>The string representation of the {@code message} to log is obtained via
    62  	 * the {@code convertMessage} method.
    63  	 *
    64  	 * @param message the message to log
    65  	 */
    66  	public function write(message:LogMessage):Void {
    67  		this.socket.send(convertMessage(message) + "\n");
    68  	}
    69  	
    70  }