001: /*
002: * File: Connection.java
003: * Project: jMOS, com.aranova.java.jmos
004: * Revision: 0.9 - Inicial
005: * Date: 30-sep-2005 12:50:32
006: *
007: * Copyright (C) Aragón Innovación Tecnológica S.L.L.
008: * All rights reserved.
009: *
010: * This software is distributed under the terms of the Aranova License version 1.0.
011: * See the terms of the Aranova License in the documentation provided with this software.
012: */
013:
014: package com.aranova.java.jmos;
015:
016: import java.io.IOException;
017: import java.io.InputStream;
018: import java.io.OutputStream;
019: import java.net.Socket;
020: import java.net.UnknownHostException;
021:
022: import javax.xml.parsers.SAXParser;
023: import javax.xml.stream.XMLStreamException;
024: import javax.xml.stream.XMLStreamWriter;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.xml.sax.InputSource;
029: import org.xml.sax.SAXException;
030:
031: import com.aranova.java.jmos.enums.TypeMOS;
032: import com.aranova.java.jmos.handler.MOSHandler;
033: import com.aranova.java.jmos.io.XMLInputStream;
034: import com.aranova.java.jmos.messages.Message;
035: import com.aranova.java.jmos.util.Common;
036: import com.aranova.java.jmos.util.Config;
037: import com.aranova.java.jmos.util.ConnectionException;
038:
039: /**
040: * Clase para la gestión de la conexión de un cliente a un MOS o NCS.
041: *
042: * @author <a href="http://www.aranova.net/contactar/">Daniel Sánchez</a>
043: * @version 0.9.1
044: * @since 0.9
045: */
046: public class Connection {
047: private static final Log _log = LogFactory.getLog(Connection.class);
048: private final Socket _socket;
049: private final OutputStream _outputStream;
050: private final InputStream _inputStream;
051: private final XMLStreamWriter _writer;
052: private final XMLInputStream _XMLis;
053: private final InputSource _inputSource;
054: private final MOSHandler _handler;
055: private final SAXParser _reader;
056: private final String _mosID;
057: private final String _ncsID;
058:
059: public Connection(final String url, final String name,
060: final TypeMOS type) throws Exception {
061: super ();
062: try {
063: if (type == TypeMOS.MOS) {
064: _mosID = name;
065: _ncsID = Config.getConfig().getString("ncsID");
066: // TODO Error si no esta especificado en el config
067: } else {
068: _ncsID = name;
069: _mosID = Config.getConfig().getString("mosID");
070: //TODO Error si no esta especificado en el config
071: }
072: _socket = new Socket(url, Config.getConfig().getInt(
073: type.name() + "Port"));
074: _outputStream = _socket.getOutputStream();
075: _inputStream = _socket.getInputStream();
076: _writer = Common.getOutputFactory().createXMLStreamWriter(
077: _outputStream, Common.getEncoding());
078: _XMLis = new XMLInputStream(_inputStream);
079: _inputSource = new InputSource(_XMLis);
080: _inputSource.setEncoding(Common.getEncoding());
081: _handler = new MOSHandler(_XMLis);
082: _reader = Common.getParserFactory().newSAXParser();
083: } catch (UnknownHostException e) {
084: e.printStackTrace();
085: throw e;
086: } catch (IOException e) {
087: e.printStackTrace();
088: throw e;
089: }
090: }
091:
092: /**
093: * Cierra la conexión con el servidor.
094: */
095: public final void close() {
096: try {
097: _outputStream.close();
098: _inputStream.close();
099: _socket.close();
100: } catch (IOException e) {
101: _log.error(
102: "Se ha producidoo un error al cerrar la conexion",
103: e);
104: }
105: }
106:
107: /**
108: * @param message
109: * @return InfoMessage
110: * @throws ConnectionException
111: */
112: public Message send(final Message message)
113: throws ConnectionException {
114: long start = System.currentTimeMillis();
115: Message response = null;
116: try {
117: message.setMosID(_mosID);
118: message.setNcsID(_ncsID);
119: message.write(_writer);
120: _writer.flush();
121: try {
122: _reader.parse(_inputSource, _handler);
123: response = _handler.getMessage();
124: } catch (SAXException e) {
125: if (!e.getMessage().equals("createMessage")) {
126: throw e;
127: }
128: _log.warn("Message del servidor no reconocido", e);
129: }
130: _reader.reset();
131: } catch (XMLStreamException e) {
132: _log.warn("Error al escribir", e);
133: throw new ConnectionException("Error al escribir", e);
134: } catch (IOException e) {
135: _log.warn("Error al escribir", e);
136: throw new ConnectionException("Error al escribir", e);
137: } catch (SAXException e) {
138: _log.warn("Error al leer", e);
139: throw new ConnectionException("Error al leer", e);
140: }
141: if (_log.isTraceEnabled()) {
142: long time = System.currentTimeMillis() - start;
143: _log.trace("Total time: " + time + "ms - Parse time: "
144: + _handler.getTime() + "ms");
145: }
146: return response;
147: }
148: }
|