NullPointerException - ServiceLocator BEA WebLogic

12 respostas
evertonsilvagomesjav

Pessoal, estou usando ServiceLocator para pegar minha interface EJB, mas ta dando NullPointer alguem ajuda?

<07/02/2012 18h45min41s BRST> <Error> <HTTP> <BEA-101017> <[weblogic.servlet.internal.WebAppServletContext@19494f6 - appName: 'TesteEAR', name: 'Teste', context-path: '/Teste'] Root cause of ServletException. javax.faces.el.EvaluationException: java.lang.NullPointerException at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98) at javax.faces.component.UICommand.broadcast(UICommand.java:311) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246) Truncated. see log file for complete stacktrace java.lang.NullPointerException at utils.ServiceLocator.jndiLookup(ServiceLocator.java:102) at utils.ServiceLocator.getLocalBean(ServiceLocator.java:83) at managedBeans.Teste.logar(Teste.java:28) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) Truncated. see log file for complete stacktrace

No meu servidor esta o projeto EAR que contem um WEB e um EJB

12 Respostas

Hebert_Coelho

oq tem nessa linha?
utils.ServiceLocator.jndiLookup(ServiceLocator.java:102)

evertonsilvagomesjav
Isso aqui;
private Object jndiLookup(String service){
		try {
			System.out.println(service);
			return context.lookup(service);
		} catch (NamingException e) {
			System.out.println(e.getMessage());
			throw null; 
	   }

	}
o e.getMessage ali gera esse erro:
While trying to lookup 'LogarServiceBean.local' didn't find subcontext 'LogarServiceBean'. Resolved ''
Hebert_Coelho

Como você declarou esse cara? LogarServiceBean

evertonsilvagomesjav

Criei Session Bean EJB

package session;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import daos.DAOFactoryVivo;
import entity.Logar;

/**
 * Session Bean implementation class LogarServiceBean
 */
@Stateless
public class LogarServiceBean implements LogarService {

	@EJB
	DAOFactoryVivo dao;
	
    /**
     * Default constructor. 
     */
    public LogarServiceBean() {
        // TODO Auto-generated constructor stub
    }

	public void insereLogar(Logar logar) {
		// TODO Auto-generated method stub
		dao.getVivoDAO().atualiza(logar);
	}

}
Hebert_Coelho

Como que tá esse cara aqui? LogarService

Você ta fazendo look up na interface ou no bean direto?

evertonsilvagomesjav

LogarService ta assim:

package session;
import javax.ejb.Local;

import entity.Logar;

@Local
public interface LogarService {

	public void insereLogar(Logar logar);
	
}

O look up ta na interface assim:

LogarService logarService = ServiceLocatorApp.getInstance().getLocalBean(LogarService.class);
Hebert_Coelho

Cara, nunca vi lookup sendo feito assim não: ServiceLocatorApp.getInstance().getLocalBean(LogarService.class);

Geralmente na hora do lookup é através de uma String que registra a classe no JNDI.

evertonsilvagomesjav
jakefrog:
Cara, nunca vi lookup sendo feito assim não: ServiceLocatorApp.getInstance().getLocalBean(LogarService.class);

Geralmente na hora do lookup é através de uma String que registra a classe no JNDI.

Tem algum exemplo que eu possa testar aqui?

Posso passar aqui pra ver se vc pode me ajudar?

ServiceLocatorApp

package utils;

public class ServiceLocatorApp extends ServiceLocator {
	
	private final String NOME_APLICACAO = "";
	private final String SUFIXO_LOCAL = "Bean/local";
	private final String SUFIXO_REMOTO = "Bean/remote";
	
	private static ServiceLocatorApp instance = null;
	
	private ServiceLocatorApp(){
		super.setup(NOME_APLICACAO, SUFIXO_LOCAL, SUFIXO_REMOTO);
	}
	
	public static ServiceLocatorApp getInstance(){
		if (instance == null){
		
			instance = new ServiceLocatorApp();
		}
		return instance;
	}

}
ServiceLocator
package utils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

public class ServiceLocator {
	
	private String NOME_APLICACAO = "";
	private String SUFIXO_LOCAL = "Bean/local";
	private String SUFIXO_REMOTO = "Bean/remote";
	
	InitialContext context = null;
	Map<String,Object> cache = null;
		
	
	/**
	 * Inicializa service locator. 
	 * Assume como sufixo dos beans: SUFIXO_LOCAL = "Bean/local" e SUFIXO_REMOTO = "Bean/remote".
	 * 
	 * @param nomeAplicacao nome da aplicação 
	 */
	public void setup(String nomeAplicacao){
		
		try {
			context = new InitialContext();
			cache = Collections.synchronizedMap(new HashMap<String,Object>());
		} catch  (NamingException e) {
			throw new RuntimeException(e);
		}
		
		if (!(nomeAplicacao == null || nomeAplicacao.trim().equals(""))){
			NOME_APLICACAO = nomeAplicacao + "/";
		}
	}
		
	/**
	 * Inicializa service locator
	 * 
	 * @param nomeAplicacao nome da aplicação
	 * @param sufixoLocal sufixo dos beans locais
	 * @param sufixoRemoto sufixo dos beans remotos
	 */
	public void setup(String nomeAplicacao, String sufixoLocal, String sufixoRemoto){
		setup(nomeAplicacao);
		
		SUFIXO_LOCAL =  sufixoLocal;
		SUFIXO_REMOTO = sufixoRemoto;
	}

	
	/**
	 * Obtem bean remoto 
	 * @param interfaceRemota tipo do bean
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> T getRemoteBean(Class<T> interfaceRemota){
		String serviceName = getServiceName(interfaceRemota,false);
		T servico = (T)cache.get(serviceName);
		if (servico == null) {
			Object ref = jndiLookup(serviceName);
			servico = (T) PortableRemoteObject.narrow(ref, interfaceRemota);
			cache.put(serviceName, servico);
		}
		return servico;
	}
	
	/**
	 * Obtem bean local
	 * @param interfaceRemota tipo do bean
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> T getLocalBean(Class<T>interfaceLocal){
		String serviceName = getServiceName(interfaceLocal,true);
		T servico = (T)cache.get(serviceName);
		if (servico == null) {
			Object ref = jndiLookup(serviceName);
			servico = (T) PortableRemoteObject.narrow(ref, interfaceLocal);
			cache.put(serviceName, servico);
		}
		return servico;
	}

	private String getServiceName(Class<?> beanInterface, final boolean isLocal) {
		if (isLocal){
			return  NOME_APLICACAO + beanInterface.getSimpleName() + SUFIXO_LOCAL; 
		} else {
			return  NOME_APLICACAO + beanInterface.getSimpleName() + SUFIXO_REMOTO;
		}
	}

	private Object jndiLookup(String service){
		try {
			System.out.println(service);
			return context.lookup(service);
		} catch (NamingException e) {
			System.out.println(e.getMessage());
			throw null; 
	   }

	}

}
evertonsilvagomesjav

Tem alguma coisa haver com meu EJB esta dentro do EAR?

evertonsilvagomesjav

Preciso criar algo no ejb-jar.xml?

Hebert_Coelho

Isso varia de servidor para servidor.

Achei esse post aqui que eu acho que irá te ajudar: http://www.guj.com.br/java/132452-lookup-de-ejb-local-no-weblogic-103

evertonsilvagomesjav

Consegui com esse post mesmo Jake hehe, valeu!!

Criado 7 de fevereiro de 2012
Ultima resposta 8 de fev. de 2012
Respostas 12
Participantes 2