Using NETBEANS6 to develop OSGi applications (2)--secondosgi[88250 original]

Source: Internet
Author: User

Reprint please retain the author information:

Author: 88250

blog:http:/blog.csdn.net/dl88250

MSN & Gmail & qq:dl88250@gmail.com


SummaryThe last time we learned about the background of OSGi and used NETBEANS6, we completed the first OSGi application--firstosgi based on Knopflerfish (an RI of OSGi). This time, we'll do a little bit of research on OSGi to learn--secondosgi, let's master the call between bundles.

Ready toDitto:-)

Start: 1. Create the projectOpen the NETBEANS6 IDE and create two common Java App--secondosgi, secondosgiclient. Copy KF sources to two projects (remember to join asm3.0 jar):



The two Java files (Demo.java, Demofactory.java) under the Secondosgiclient Project's demo package are exactly the same as those under the SECONDOSGI Project demo package, which is lazy to release the compiled class byte File to client, directly to the interface source files.

2. Preparation of MANIFEST.MFTo switch the project view to files, rewrite MANIFEST.MF as follows:





About MANIFEST.MF all kinds of properties will not wordy, there are many documents can refer to:-)

3. Preparation of ActivatorThe following is a source file, which corresponds to the above engineering structure diagram.

Service provider, i.e. under the SECONDOSGI project:

/*
* @ (#) Demo.java
*
* This are free software; You can redistribute it and/or modify
* It under the terms of the GNU general public License as published by
* the free Software Foundation; Either version 3 of the License, or
* (at your option) any later version.
*
* This are distributed in the hope that it'll be useful,
* but without any WARRANTY; Without even the implied warranty of
* merchantability or FITNESS for A particular purpose. The
* GNU Library general public License for more details.
*
* You should have received a copy of the GNU general public License
* Along with this program; If not, write to the free Software
* Foundation, Inc., Temple Place-suite, Boston, MA 02111-1307, USA.
*/
Package Secondosgi.service.demo;


/**
* A Simple Demo service.
* @author 88250
* @version 1.0.0.0, Feb 14, 2008
*/
Public interface Demo {

/**
* Add two integers and return the result.
* @param a
* @param b
* @return
*/
public int Add (int a, int b);
}

/*
* @ (#) Demofactory.java
*
* This are free software; You can redistribute it and/or modify
* It under the terms of the GNU general public License as published by
* the free Software Foundation; Either version 3 of the License, or
* (at your option) any later version.
*
* This are distributed in the hope that it'll be useful,
* but without any WARRANTY; Without even the implied warranty of
* merchantability or FITNESS for A particular purpose. The
* GNU Library general public License for more details.
*
* You should have received a copy of the GNU general public License
* Along with this program; If not, write to the free Software
* Foundation, Inc., Temple Place-suite, Boston, MA 02111-1307, USA.
*/
Package Secondosgi.service.demo;

/**
* <b>Another</b> very simple demo service API.
* <p>
* The intentions of this interface class are to show that different bundles
* 'll get different instances of a service by the means of a
* Servicefactory.
* </p>
* @author 88250
* @version 1.0.0.0, Feb 14, 2008
*/
Public interface Demofactory {

/**
* Say Hello.
*/
void Hello ();
}

/*
* @ (#) Demoimpl.java
*
* This are free software; You can redistribute it and/or modify
* It under the terms of the GNU general public License as published by
* the free Software Foundation; Either version 3 of the License, or
* (at your option) any later version.
*
* This are distributed in the hope that it'll be useful,
* but without any WARRANTY; Without even the implied warranty of
* merchantability or FITNESS for A particular purpose. The
* GNU Library general public License for more details.
*
* You should have received a copy of the GNU general public License
* Along with this program; If not, write to the free Software
* Foundation, Inc., Temple Place-suite, Boston, MA 02111-1307, USA.
*/
Package Secondosgi.service.demo.impl;

Import Secondosgi.service.demo.Demo;

/**
* Implementation of the Demo service.
* @author 88250
* @version 1.0.0.0, Feb 14, 2008
*/
public class Demoimpl implements Demo {

public int Add (int a, int b) {
return a + B;
}
}

/*
* @ (#) Demofactoryimpl.java
*
* This are free software; You can redistribute it and/or modify
* It under the terms of the GNU general public License as published by
* the free Software Foundation; Either version 3 of the License, or
* (at your option) any later version.
*
* This are distributed in the hope that it'll be useful,
* but without any WARRANTY; Without even the implied warranty of
* merchantability or FITNESS for A particular purpose. The
* GNU Library general public License for more details.
*
* You should have received a copy of the GNU general public License
* Along with this program; If not, write to the free Software
* Foundation, Inc., Temple Place-suite, Boston, MA 02111-1307, USA.
*/
Package Secondosgi.service.demo.impl;

Import Secondosgi.service.demo.DemoFactory;
Import Org.osgi.framework.Bundle;

/**
* Implementation of the Demofactory service. The intentions of this
* Class is to show that different bundles'll get different instances
* of a service by the means of a servicefactory.
* @author 88250
* @version 1.0.0.0, Feb 14, 2008
*/
public class Demofactoryimpl implements Demofactory {

Private Bundle B;

/**
* Constructor with argument.
* @param b a <code>Bundle</code>
*/
Public Demofactoryimpl (Bundle b) {
this. B = b;
}

public void Hello () {
System.out.println ("Hello Bundle #" + B.getbundleid ());
}
}

Critical activator to implement service registration and cancellation:
/*
* @ (#) Activator.java
*
* This are free software; You can redistribute it and/or modify
* It under the terms of the GNU general public License as published by
* the free Software Foundation; Either version 3 of the License, or
* (at your option) any later version.
*
* This are distributed in the hope that it'll be useful,
* but without any WARRANTY; Without even the implied warranty of
* merchantability or FITNESS for A particular purpose. The
* GNU Library general public License for more details.
*
* You should have received a copy of the GNU general public License
* Along with this program; If not, write to the free Software
* Foundation, Inc., Temple Place-suite, Boston, MA 02111-1307, USA.
*/
Package Secondosgi.service.demo.impl;

Import Secondosgi.service.demo.Demo;
Import Secondosgi.service.demo.DemoFactory;
Import java.util.Hashtable;
Import Org.osgi.framework.Bundle;
Import Org.osgi.framework.BundleActivator;
Import Org.osgi.framework.BundleContext;
Import Org.osgi.framework.ServiceFactory;
Import org.osgi.framework.ServiceRegistration;

/**
* Activator which creates and registers a Demo1 service.
* @author 88250
* @version 1.0.0.0, Feb 14, 2008
*/
public class Activator implements Bundleactivator {

Private demo demo;

public void start (bundlecontext BC) {
System.out.println ("Start" + getclass (). GetName ());
Demo = new Demoimpl ();
Bc.registerservice (Demo. class. GetName (),
Demo
New Hashtable ());

Create a service factory for Demofactory implementations
Servicefactory factory = new Servicefactory () {

Hashtable Services = new Hashtable ();
'll get called when a bundle request a service
@SuppressWarnings ("Unchecked")
Public Object GetService (Bundle B,
Serviceregistration reg) {
System.out.println ("Get from" + B.getbundleid ());

Create when necessary
Demofactory Impl = (demofactory) services.get (b);
if (Impl = = null) {
Impl = new Demofactoryimpl (b);
Services.put (b, Impl);
}
return impl;
}

Would get called when a bundle ungets a service or stops
public void Ungetservice (Bundle B,
Serviceregistration Reg,
Object service) {
System.out.println ("Unget from" + B.getbundleid ());
Services.remove (b);
}
};

Factory only implements Servicefactory,
But we still register as DemoFactory1 service
Bc.registerservice (Demofactory. Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.