package com.ejie.ad74a.mdb;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Ad74aJMSSender {
	// Defines the JNDI context factory.
	public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
	// Defines the JMS context factory.
	public final static String JMS_FACTORY = "ad74a.ad74aJMSFactory";
	// Defines the queue.
	public final static String QUEUE = "ad74a.ad74aPapel0Queue";

	private QueueConnectionFactory qconFactory;
	private QueueConnection qcon;
	private QueueSession qsession;
	private QueueSender qsender;
	private Queue queue;
	// private ObjectMessage msg;
	private TextMessage txtmsg;

	public static void main(String[] args) throws Exception {
		InitialContext ic = getInitialContext("t3://localhost:7001");
		Ad74aJMSSender qs = new Ad74aJMSSender();
		qs.init(ic, QUEUE);
		readAndSend(qs);
		//qs.sendText("asdasdasdas", 0);
		qs.close();
	}

	public void init(Context ctx, String queueName) throws NamingException, JMSException {
		qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
		qcon = qconFactory.createQueueConnection();
		qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
		queue = (Queue) ctx.lookup(queueName);
		qsender = qsession.createSender(queue);
		// msg = qsession.createObjectMessage();
		txtmsg = qsession.createTextMessage();
		qcon.start();
	}

//	public void send(Serializable serializable, int counter) throws JMSException {
//		msg.setObject(serializable);
//		msg.setIntProperty("counter", counter);
//		qsender.send(msg);
//	}

	public void sendText(String txt, int counter) throws JMSException {
		// ((ObjectMessage) txtmsg).setObject(serializable);
		txtmsg.setText(txt);
		txtmsg.setIntProperty("counter", counter);
		qsender.send(txtmsg);
	}

	public void close() throws JMSException {
		qsender.close();
		qsession.close();
		qcon.close();
	}

	private static void readAndSend(Ad74aJMSSender qs) throws IOException, JMSException {
		String basePath = "D:/taadmin/trunk/datos/ad74a/file/";
		String nombre = "evento_CDAR_Prueba.xml";
		String eventoXML = leerFicheroIS(basePath + nombre);
		qs.sendText(eventoXML, 0);
	}

	private static String leerFicheroIS(String rutaFichero) {
		BufferedReader b;
		String cadena;
		StringBuilder resultXml = new StringBuilder();
		try {
			b = new BufferedReader(new InputStreamReader(new FileInputStream(rutaFichero), "utf-8"));
			while ((cadena = b.readLine()) != null) {
				resultXml.append(cadena);
			}
			b.close();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return resultXml.toString();
	}

	private static InitialContext getInitialContext(String url) throws NamingException {
		Hashtable env = new Hashtable();
		env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
		env.put(Context.PROVIDER_URL, url);
		return new InitialContext(env);
	}

}
