@Inject for manually instantiated object?

1

I have a class called Application which is a listener , it instantiates objects of ServidorRadius where it needs to access repositories that are injected with CDI , however radiusRepository is returning null / p>

Application :

@ApplicationScoped
public class Application implements ServletContextListener {

    @Inject
    private Logger log;

    @Inject
    private  RadiusRepository radiusRepository;

    @Inject
    private  RadiusRegistration radiusRegistration;

    private  static List<Radius> radius;
    private  static List<ServidorRadius> servidor = new ArrayList<ServidorRadius>();

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try{
            radius = radiusRepository.findAllRadius();
            for(Radius r : radius){
                ServidorRadius radiusS = new ServidorRadius(r.getMetodoDeAutenticacao(), r.getAcctPort(), r.getAuthPort());
                if(r.getStart())
                    radiusS.start(true, true);
                servidor.add(radiusS);
            }
        } catch (NoResultException ne){
            //
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        try{
            for(ServidorRadius sr : servidor){
                sr.stop();
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void addServidorRadius(Radius radius){
        servidor.add(new ServidorRadius(radius.getMetodoDeAutenticacao(), radius.getAcctPort(), radius.getAuthPort()));
    }

    public static void stopRadius(Radius radius){
        try{
            for(ServidorRadius sr : servidor){
                if(sr.getAcctPort() == radius.getAcctPort() && sr.getAuthPort() == radius.getAuthPort())
                    sr.stop();
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void startRadius(Radius radius){
        try{
            Boolean started = false;
            for(ServidorRadius sr : servidor){
                if(sr.getAcctPort() == radius.getAcctPort() && sr.getAuthPort() == radius.getAuthPort()){
                    sr.start(true, true);
                    started = true;
                }
            }
            if(!started){
                addServidorRadius(radius);
                startRadius(radius);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

}

ServidorRadius :

public class ServidorRadius extends RadiusServer {

    private MetodoDeAutenticacao metodoDeAutenticacao;

    @Inject
    private RadiusRepository radiusRepository;

    @Inject
    private LoginRepository loginRepository;

    @Inject
    private RadiusRegistration radiusRegistration;

    private Login login;

    private AccessRequest accessRequest;

    private Concentrador concentrador;

    public ServidorRadius(MetodoDeAutenticacao metodoDeAutenticacao, Integer acctPort, Integer authPort){
        this.metodoDeAutenticacao = metodoDeAutenticacao;
        this.setAcctPort(acctPort);
        this.setAuthPort(authPort);
    }

    @Override
    public String getSharedSecret(InetSocketAddress client) {
        if(radiusRepository == null)
            System.out.println("Is null");
        try{
            concentrador = radiusRepository.findConcentradorByIp(client.getAddress().getHostAddress());
            if(concentrador.getIp().equals(client.getAddress().getHostAddress()))
                return concentrador.getSenha();
        } catch (NoResultException ne){
            // TODO
        } catch (Exception e){
            //TODO

            e.printStackTrace();
        }

        return null;
    }

}

Output

18:46:21,741 INFO  [stdout] (Radius Auth Listener) Is null
18:46:21,742 ERROR [stderr] (Radius Auth Listener)    java.lang.NullPointerException
18:46:21,742 ERROR [stderr] (Radius Auth Listener)  at com.authnet.util.ServidorRadius.getSharedSecret(ServidorRadius.java:60)
18:46:21,742 ERROR [stderr] (Radius Auth Listener)  at org.tinyradius.util.RadiusServer.listen(RadiusServer.java:332)
18:46:21,742 ERROR [stderr] (Radius Auth Listener)  at org.tinyradius.util.RadiusServer.listenAuth(RadiusServer.java:289)
18:46:21,742 ERROR [stderr] (Radius Auth Listener)  at org.tinyradius.util.RadiusServer$1.run(RadiusServer.java:108)

I think the problem is when I instantiate ServidorRadius with the command new , but I do not know any other alternative.

    
asked by anonymous 08.10.2015 / 23:49

1 answer

1

You need to identify the radiusRepository scopo or create a producer:

@ApplicationScoped
public class RadiusRepository {
}
    
20.10.2015 / 22:06