简体   繁体   中英

How to send commands and receive responses to OSGi console via a Java Socket?

I want to run OSGi framework on another computer (in a main method). So I wanted to know is there any way to connect to the OSGi console from the other computer and manage bundles?

I thought maybe using a java.net.Socket would help, and that's how I implemented that. I've used 2 threads. one for processing user input stream, and the other one that processes OSGi Console response. This is the first thread (processes user input stream):

    configMap.put("osgi.console", "6666");
    Framework fwk = ff.newFramework(configMap);
    try {
        fwk.start();
    } catch (BundleException e) {
        e.printStackTrace();
    }

//__________________________________________________________________//

    try {
        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        Socket socket = new Socket(InetAddress.getByName("0.0.0.0"), 6666);
        printlnInfo("Socket has been created: " + socket.getInetAddress() + ":" + socket.getPort());
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        ConsoleOutputReciever fr = new ConsoleOutputReciever();
        new Thread(fr).start();
        while (true) {
            String userInput = "";
            while ((userInput = stdIn.readLine()) != null) {
                System.out.println("--> " + userInput);
                out.write(userInput + "\n");
                out.flush();
            }
            System.out.println("2");
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }

This is the second thread (processes OSGi Console response):

public class ConsoleOutputReciever implements Runnable {

public Scanner in = null;

@Override
public void run() {
    printlnInfo("ConsoleOutputReciever Started");
    try {
        Socket socket = new Socket(InetAddress.getByName("0.0.0.0"), 6666);
        printlnInfo("Socket has been created: " + socket.getInetAddress() + ":" + socket.getPort());
        String osgiResponse = "";
        in = new Scanner(socket.getInputStream());
        try {
            while (true) {
                in = new Scanner(socket.getInputStream());
                while (in.hasNext()) {
                    System.out.println("-- READ LOOP");
                    osgiResponse = in.nextLine();
                    System.out.println("-- " + osgiResponse);
                }
            }
        } catch (IllegalBlockingModeException e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

   }
}

but I only receive the first response of the OSGi console. like this:


--READ LOOP

--

--READ LOOP

ss

--> ss


Any ideas about the problem or any other way to connect to OSGi console remotely?

you are using blocking io, thus your inner while loop will never finish until the socket is closed. you need 2 threads to accomplish this with blocking io streams. 1 thread reads from stdin and writes to the socket output stream, the other thread reads from the socket input stream and writes to stdout.

also, you probably want to write a newline after sending the userInput to the osgi console (Scanner.nextLine() eats the newline).

lastly, you don't generally want to use the Print* classes when working with sockets as they hide IOExceptions.

除了构建自己的东西外,您可能希望使用可用的远程外壳之一,例如,Apache Felix的http://felix.apache.org/site/apache-felix-remote-shell.html

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