This the complete source code:
http://www.2shared.com/file/jnGjnZjZ/CB_27.html
How I must edit the code in order to work?
I'm new to java.
Christian Schneider wrote:
>
> I see one error in your Activator.
>
> You should store the ServiceRegistration that you get from
> registerService in a variable of the activator.
> In the stop method you should then call unregister on the
> ServiceRegistration. The call to ungetService is not suitable for the
> serviceprovider but only
> for the consumer.
>
> This is not the problem you observe though. I assume that one of the
> "new" calls to either CBridgeImpl or EchoService does not return. The
> activator is called synchronously by
> the OSGi framework. It is very important that it does not "hang" or
> consume too much time as this will affect the whole framework. Can you
> also post the content of these classes?
>
> The other question is if you need to register an OSGi service at all. If
> you only want to open a network port then you can just do this from the
> start method. OSGi services only make sense if there is also a consumer
> of the service inside the OSGi framework.
>
> Christian
>
>
> Am 11.02.2012 10:57, schrieb rcbandit2:
>> I register the Java network server as service. When I try to deploy the
>> OSGI
>> bundle with the java server Glassfish freezes. But I can successfully
>> make a
>> connection with the java network client. It seems that when I try to
>> deploy
>> the OSGI bundle there is a infinite loop. How I can solve the problem?
>> This
>> is the code of the Activator:
>>
>> package org.DX_57.osgi.CB_27.impl;
>>
>> import java.util.Properties;
>> import org.DX_57.osgi.CB_27.api.CBridge;
>> import org.osgi.framework.BundleActivator;
>> import org.osgi.framework.BundleContext;
>>
>>
>>
>>
>> public class CBridgeApp implements BundleActivator {
>>
>> public void start(BundleContext bc) throws Exception {
>> bc.registerService(CBridge.class.getName(), new CBridgeImpl(),
>> new
>> Properties());
>> bc.registerService(EchoServer.class.getName(), new EchoServer(),
>> new
>> Properties());
>>
>>
>>
>> }
>>
>> public void stop(BundleContext bc) throws Exception {
>>
>> bc.ungetService(bc.getServiceReference(CBridge.class.getName()));
>> }
>> }
>>
>>
>>
>>
>>
>>
>> rcbandit2 wrote:
>>> This is my simple example of OSGI bundle:
>>> http://www.2shared.com/file/h6lFjppY/CB_27.html
>>> Where I have to put the source code of the java network server in order
>>> to
>>> work?
>>>
>>>
>>>
>>> nfndx wrote:
>>>> It does not seem like you need your network server to be a osgi
>>>> service.
>>>> So
>>>> if you just need your network server to be started then create a osgi
>>>> bundle. Also create an activator for this bundle( remember to set this
>>>> activator in the manifest) . In the start method if the activator you
>>>> should be able to start your network server
>>>>
>>>> Hope that helps
>>>> Regards
>>>> Ivanhoe
>>>> On Feb 10, 2012 5:32 PM, "rcbandit2"<[email protected]> wrote:
>>>>
>>>>> Any ideas or reply?
>>>>>
>>>>>
>>>>>
>>>>> rcbandit2 wrote:
>>>>>> 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
>>>>>>
>>>>> --
>>>>> View this message in context:
>>>>> http://old.nabble.com/How-to-add-Java-network-server-into-OSGI-bundle-tp33295152p33301156.html
>>>>> Sent from the Apache Felix - Users mailing list archive at Nabble.com.
>>>>>
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: [email protected]
>>>>> For additional commands, e-mail: [email protected]
>>>>>
>>>>>
>>>>
>>>
>
>
> --
> Christian Schneider
> http://www.liquid-reality.de
>
> Open Source Architect
> Talend Application Integration Division http://www.talend.com
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
>
--
View this message in context:
http://old.nabble.com/How-to-add-Java-network-server-into-OSGI-bundle-tp33295152p33306304.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]