简体   繁体   中英

Casting from string variable Java

I've got this method for external clients to execute methods using RMI. I've been unable to cast the variable Naming returns to the type defined in the String returned by servidor.getNombreRegistrado() . I'm building a broker in which servers can register themselves and their services into the broker to make them accesible for clients. That's why I need to cast the result from Naming.lookup to the type the server registered with. In that way I'll be able to invoke its methods using RMI.

public String ejecutar_servicio(String nom_servicio, Vector<String> parametros_servicio) throws RemoteException{
    try {
        for(Servidor servidor : servidores){
            for(Servicio servicio : servidor.getServicios()){
                if(servicio.getNombre().equals(nom_servicio) && parametros_servicio.size() == servicio.numParametros()){
                    try{
                        Object server = (Object) Naming.lookup("//" + servidor.getHostname() + "/" + servidor.getNombreRegistrado());
                        server = (servidor.getNombreRegistrado()) server;
                        server.dar_fecha();
                        return "";
                    }catch(Exception ex){
                        System.out.println(ex);
                        return "Excepcion";
                    }
                }
            }
        }
        return "Servicio no encontrado";
    }
    catch (SecurityException ex) {
        return ex.toString();
    }
}

How could I achieve that behaviour?

This is Servidor class:

private class Servidor{
    private String hostname;
    private String nombreRegistrado;
    private List<Servicio> servicios = new ArrayList<>();

    public Servidor(String hostname, String nombreRegistrado){
        this.hostname = hostname;
        this.nombreRegistrado = nombreRegistrado;
    }

    void registrarServicio(Servicio servicio){
        servicios.add(servicio);
    }

    String getNombreRegistrado() {
        return nombreRegistrado;
    }

    List<Servicio> getServicios(){
        return servicios;
    }

    String getHostname(){
        return hostname;
    }
}

This is Servicio class:

private class Servicio{
    private String nombreServicio;
    private Vector listaParametros;
    private String tipoRetorno;

    public Servicio(String nombreServicio, Vector<String> listaParametros, String tipoRetorno){
        this.nombreServicio = nombreServicio;
        this.listaParametros = listaParametros;
        this.tipoRetorno = tipoRetorno;
    }

    public String getNombre(){
        return this.nombreServicio;
    }

    public int numParametros(){
        return this.listaParametros.size();
    }

    public String getTipoRetorno(){
        return this.tipoRetorno;
    }
}

Here:

server = (servidor.getNombreRegistrado())

That is simply not possible in Java. A cast is first of all an operation at compile time. You tell the compiler that you know that some object has a specific type. Meaning: your idea to cast an object dynamically to the result of some method call at runtime, that doesn't make any sense.

That method returns a string, a sequence of characters. A string is just that, a string. You can't turn a string into anything else. Similar to: the string "Ferrari" might denote a famous sports car, but it is still just a string. You can't sit into "Ferrari" and drive away. Because it is a string, not a car! (or an object of a class that models cars)

I think the real answer for you is to step back. You try to do complex things here, but you lack very basic knowledge of Java. It seems you are overburdening yourself. Step back, and study basic Java for some days. Then come back and revisit rmi.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM