users@glassfish.java.net

How to add Java network server into OSGI bundle

From: <forums_at_java.net>
Date: Thu, 9 Feb 2012 11:13:41 -0600 (CST)

Hi,

    I created this Java network server:

import java.net.*;
import java.io.*;
public class EchoServer
{       
    ServerSocket m_ServerSocket;
   
   
    public EchoServer()
    {
        try
        {
            // Create the server socket.
            m_ServerSocket = new ServerSocket(12111);
        }
        catch(IOException ioe)
        {
            System.out.println("Could not create server socket at
12111. Quitting.");
            System.exit(-1);
        }
       
        System.out.println("Listening for clients on 12111...");
       
        // Successfully created Server Socket. Now wait for
connections.
        int id = 0;
        while(true)
        {                       
            try
            {
                // Accept incoming connections.
                Socket clientSocket = m_ServerSocket.accept();
                           
                ClientServiceThread cliThread = new
ClientServiceThread(clientSocket, id++);
                cliThread.start();
            }
            catch(IOException ioe)
            {
                System.out.println("Exception encountered on
accept. Ignoring. Stack Trace :");
                ioe.printStackTrace();
            }
        }
    }
   
    public static void main (String[] args)
    {
        new EchoServer();   
    }
   
   
    class ClientServiceThread extends Thread
    {
        Socket m_clientSocket;       
        int m_clientID = -1;
        boolean m_bRunThread = true;
       
        ClientServiceThread(Socket s, int clientID)
        {
            m_clientSocket = s;
            m_clientID = clientID;
        }
      
        public void run()
        {           
            // Obtain the input stream and the output stream for
the socket
            // A good practice is to encapsulate them with a
BufferedReader
            // and a PrintWriter as shown below.
            BufferedReader in = null;
            PrintWriter out = null;
           
            // Print out details of this connection
            System.out.println("Accepted Client : ID - " +
m_clientID + " : Address - " +
                            
m_clientSocket.getInetAddress().getHostName());
               
            try
           
{                               
                in = new BufferedReader(new
InputStreamReader(m_clientSocket.getInputStream()));
                out = new PrintWriter(new
OutputStreamWriter(m_clientSocket.getOutputStream()));
               
                // At this point, we can read for input and
reply with appropriate output.
               
                // Run in a loop until m_bRunThread is set to
false
                while(m_bRunThread)
                {                   
                    // read incoming stream
                    String clientCommand = in.readLine();
                   
                    System.out.println("Client Says :" +
clientCommand);
                   
                   
                   
if(clientCommand.equalsIgnoreCase("quit"))
                    {
                        // Special command. Quit this
thread
                        m_bRunThread = false;  
                        System.out.print("Stopping
client thread for client : " + m_clientID);
                    }
                    else
                    {
                        // Echo it back to the client.
                        out.println(clientCommand);
                        out.flush();
                    }
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                // Clean up
                try
                {                   
                    in.close();
                    out.close();
                    m_clientSocket.close();
                    System.out.println("...Stopped");
                }
                catch(IOException ioe)
                {
                    ioe.printStackTrace();
                }
            }
        }
    }
}

I want to insert this code into OSGI bundle. At the moment I don't have
configured Glassfish with datasource to test the bundle. What I have to do
in order to configure the Java server to listen for incomming connections
when I deploy it on Glassfish server?

Do I need to add somenthing into the OSGI bundle activator to make the Java
server listen for incomming connections?

Best Wishes

 


--
[Message sent by forum member 'rcbandit']
View Post: http://forums.java.net/node/883397