ServiceLocator.java 10.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
/*
* Copyright (C) 2007-2007 the GSAN - Sistema Integrado de Gestão de Serviços de Saneamento
*
* This file is part of GSAN, an integrated service management system for Sanitation
*
* GSAN is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* GSAN is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/

/*
* GSAN - Sistema Integrado de Gestão de Serviços de Saneamento
* Copyright (C) <2007> 
* Adriano Britto Siqueira
* Alexandre Santos Cabral
* Ana Carolina Alves Breda
* Ana Maria Andrade Cavalcante
* Aryed Lins de Araújo
* Bruno Leonardo Rodrigues Barros
* Carlos Elmano Rodrigues Ferreira
* Cláudio de Andrade Lira
* Denys Guimarães Guenes Tavares
* Eduardo Breckenfeld da Rosa Borges
* Fabíola Gomes de Araújo
* Flávio Leonardo Cavalcanti Cordeiro
* Francisco do Nascimento Júnior
* Homero Sampaio Cavalcanti
* Ivan Sérgio da Silva Júnior
* José Edmar de Siqueira
* José Thiago Tenório Lopes
* Kássia Regina Silvestre de Albuquerque
* Leonardo Luiz Vieira da Silva
* Márcio Roberto Batista da Silva
* Maria de Fátima Sampaio Leite
* Micaela Maria Coelho de Araújo
* Nelson Mendonça de Carvalho
* Newton Morais e Silva
* Pedro Alexandre Santos da Silva Filho
* Rafael Corrêa Lima e Silva
* Rafael Francisco Pinto
* Rafael Koury Monteiro
* Rafael Palermo de Araújo
* Raphael Veras Rossiter
* Roberto Sobreira Barbalho
* Rodrigo Avellar Silveira
* Rosana Carvalho Barbosa
* Sávio Luiz de Andrade Cavalcante
* Tai Mu Shih
* Thiago Augusto Souza do Nascimento
* Tiago Moreno Rodrigues
* Vivianne Barbosa Sousa
*
* Este programa é software livre; você pode redistribuí-lo e/ou
* modificá-lo sob os termos de Licença Pública Geral GNU, conforme
* publicada pela Free Software Foundation; versão 2 da
* Licença.
* Este programa é distribuído na expectativa de ser útil, mas SEM
* QUALQUER GARANTIA; sem mesmo a garantia implícita de
* COMERCIALIZAÇÃO ou de ADEQUAÇÃO A QUALQUER PROPÓSITO EM
* PARTICULAR. Consulte a Licença Pública Geral GNU para obter mais
* detalhes.
* Você deve ter recebido uma cópia da Licença Pública Geral GNU
* junto com este programa; se não, escreva para Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307, USA.
*/  
package gcom.util;

import gcom.fachada.Fachada;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import javax.ejb.EJBLocalHome;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

/**
 * Efetua a localização, instanciamento e cache de serviços
 * 
 * @author Administrador
 */

public class ServiceLocator {

	private static ServiceLocator instancia = null;

	private static Context contexto = null;

	private static Map mapaHomes = null;

	private static ConnectionFactory connectionFactoryJms = null;

	private static String empresa = null;

	static {

		try {

			Hashtable env = new Hashtable();

			env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
					"org.jnp.interfaces.NamingContextFactory");

			env.put("java.naming.factory.url.pkgs",
					"org.jboss.naming:org.jnp.interfaces");

			env.put(javax.naming.Context.URL_PKG_PREFIXES,
					"org.jboss.naming:org.jnp.interfaces");

			// env.put(javax.naming.Context.PROVIDER_URL, "localhost:1099");
			contexto = new InitialContext(env);

			mapaHomes = Collections.synchronizedMap(new HashMap());

			instancia = new ServiceLocator();

			connectionFactoryJms = (ConnectionFactory) contexto
					.lookup("java:JmsXA");

		} catch (Exception e) {

			throw new SistemaException(e);
		}

	}

	/**
	 * Retorna a única instância do ServiceLocator
	 * 
	 * @return A instância do ServiceLocator
	 * @exception ServiceLocatorException
	 *                Exceção levantada caso ocorra algum erro de localização ou
	 *                instanciamento
	 */

	public static ServiceLocator getInstancia() throws ServiceLocatorException {

		return instancia;
	}

	/**
	 * Construtor da classe ServiceLocator
	 */

	private ServiceLocator() {
	}

	/**
	 * Retorna a interface Home de um Entreprise Java Bean
	 * 
	 * @param jndiName
	 *            O jndiName do EJB
	 * @param classe
	 *            O nome do .class da interface home
	 * @return A interface Home de um Entreprise Java Bean
	 * @exception ServiceLocatorException
	 *                Exceção levantada caso ocorra algum erro de localização ou
	 *                instanciamento
	 */

	public Object getHome(String jndiName, Class classe)
			throws ServiceLocatorException {

		Object home = mapaHomes.get(jndiName);

		if (home == null) {

			try {

				Object referencia = contexto.lookup(jndiName);

				home = PortableRemoteObject.narrow(referencia, classe);

				mapaHomes.put(jndiName, home);

			} catch (NamingException e) {

				throw new ServiceLocatorException(e, e.getMessage());
			}

		}

		return home;
	}

	/**
	 * will get the ejb Local home factory. clients need to cast to the type of
	 * EJBHome they desire
	 * 
	 * @param jndiHomeName
	 *            Descrição do parâmetro
	 * @return the Local EJB Home corresponding to the homeName
	 * @exception ServiceLocatorException
	 *                Descrição da exceção
	 */
	public EJBLocalHome getLocalHome(String jndiHomeName)
			throws ServiceLocatorException {

		try {

			return (EJBLocalHome) contexto.lookup(jndiHomeName);
		} catch (NamingException e) {

			throw new ServiceLocatorException(e, e.getMessage());
		}

	}

	public EJBLocalHome getLocalHomePorEmpresa(String jndiHomeName)
			throws ServiceLocatorException {

		try {
			if (empresa == null) {
				empresa = Fachada.getInstancia().pesquisarParametrosDoSistema()
						.getNomeControlador();
			}

			jndiHomeName = jndiHomeName.replace("GCOM", empresa);
			return (EJBLocalHome) contexto.lookup(jndiHomeName);
		} catch (NamingException e) {

			throw new ServiceLocatorException(e, e.getMessage());
		}

	}

	/**
	 * Retorna a instância do serviço de componente solicitado.
	 * 
	 * @param nome
	 *            Nome do serviço de componente necessário.
	 * @return Object Uma instância do serviço de componente solicitado
	 * @exception ServiceLocatorException
	 *                Exceção levantada caso ocorra algum erro de localização ou
	 *                instanciamento.
	 */

	public Object getComponente(String nome) throws ServiceLocatorException {
		return this.carregaClasse(nome);
	}

	/**
	 * Realiza a carga e instanciamento de uma classe
	 * 
	 * @param nome
	 *            O nome da classe a ser carregada
	 * @return Uma instância da classe solicitada
	 * @exception ServiceLocatorException
	 *                Exceção levantada caso ocorra algum erro de localização ou
	 *                instanciamento
	 */

	private Object carregaClasse(String nome) throws ServiceLocatorException {
		Object classe = null;
		Class classAuxiliar = null;
		Method metodo = null;

		try {
			classAuxiliar = Class.forName(nome);
			metodo = classAuxiliar.getMethod("getInstancia", (Class[]) null);

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw new ServiceLocatorException(e, "Classe não encontrada");
		} catch (NoSuchMethodException e) {

			try {
				metodo = classAuxiliar.getMethod("getInstance", (Class[]) null);

			} catch (NoSuchMethodException ex) {
				ex.printStackTrace();
				throw new ServiceLocatorException(ex);
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceLocatorException(e);
		}

		try {
			classe = metodo.invoke(null, (Object[]) null);

		} catch (IllegalAccessException e) {
			e.printStackTrace();
			throw new ServiceLocatorException(e);
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			throw new ServiceLocatorException(e);
		}

		return classe;
	}

	public void enviarMensagemJms(int metodo, String queueMDB,
			Object[] parametros) throws ServiceLocatorException {

		Connection connection = null;
		Session session = null;

		try {
			// Direciona a fila para o controlador
			Queue destination = (Queue) contexto.lookup(queueMDB);

			connection = connectionFactoryJms.createConnection();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			MessageProducer messageProducer = session
					.createProducer(destination);
			ObjectMessage message = session.createObjectMessage();

			// Monta a mensagem
			message.setIntProperty("nomeMetodo", metodo);

			// Passa os parametros

			message.setObject(parametros);

			// Manda a mensagem
			messageProducer.send(message);

		} catch (NamingException e) {
			throw new ServiceLocatorException(e, e.getMessage());
		} catch (JMSException e) {
			throw new ServiceLocatorException(e, e.getMessage());
		} finally {
			try {

				session.close();
				connection.close();

			} catch (JMSException e) {
				throw new ServiceLocatorException(e, e.getMessage());
			}

		}

	}

	public void enviarMensagemJms(String queueMDB, Object[] parametros)
			throws ServiceLocatorException {

		Connection connection = null;
		Session session = null;

		try {
			// Direciona a fila para o controlador
			Queue destination = (Queue) contexto.lookup(queueMDB);

			connection = connectionFactoryJms.createConnection();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			MessageProducer messageProducer = session
					.createProducer(destination);
			ObjectMessage message = session.createObjectMessage();

			// Passa os parametros
			message.setObject(parametros);

			// Manda a mensagem
			messageProducer.send(message);

		} catch (NamingException e) {
			throw new ServiceLocatorException(e, e.getMessage());
		} catch (JMSException e) {
			throw new ServiceLocatorException(e, e.getMessage());
		} finally {
			try {

				session.close();
				connection.close();

			} catch (JMSException e) {
				throw new ServiceLocatorException(e, e.getMessage());
			}

		}

	}
	
	public static Object getResource(String jndiName) throws ServiceLocatorException {

		Object resource = null;

		try {

			resource = contexto.lookup(jndiName);
		
		} catch (NamingException e) {

			//throw new ServiceLocatorException(e, e.getMessage());
		}

		return resource;
	}
}