My requirement is to deploy war files into OSGI at runtime.
For to achieve that I am trying through below codes:
BundleContext bundlecontext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
Bundle b = bundlecontext.installBundle("file:" + "./war/testwar.war");
b.start();
But I am unable to achieve that, by using the above codes.
As I am new to OSGI I have very less idea of OSGi.
So I need help on this to achieve.
OSGi runtime is not an application server but environment that manages modules (bundles). A war is not (usually) an OSGi bundle and can not be installed the way you do it.
There are many different ways to achieve what you want and it's hard to suggest something. Here are some of the them:
Convert the WAR to WAB (Web Application Bundle). This way you can install it into any OSGi runtime. You would also need to install other bundles that can handle web requests (HTTP Whiteboard implementation for example)
Use an OSGi based product that can automatically convert wars to bundles. Liferay Portal does that but it may be too much to learn it if all you want is to run one app.
If you tagged the question with apache-karaf because that is your environment, then have a look at War deployer. That feature will allow you to install war files in Karaf. If you want to do it from code, you can have a look at the code of that feature and follow the same approach.
We often have Custom Mediators (Java classes) that are performing transformations or other things. Each time we want to change only one little thing inside the class, we need to restart the whole WSO2 ESB.
May the "custom mediator" approach is wrong, but we'd like to keep the java classes, but de-coupled from the whole server - like proxies or endpoints. (Our classes are inside a .jar in /components/lib)
How can a custom mediator or java class added to the WSO2 ESB without restarting?
<class name="my.domain.MyJavaClassThatMustBeUpdatedWithoutRestart"/>
You can create an osgi bundle from your class and then you can control the classes behavior from the OSGI Console. You need to start esb with -DosgiConsole option. You can find more information from this blog post.
http://lalajisureshika.blogspot.co.uk/2013/03/some-useful-osgi-commands-to-find.html
While starting up we make OSGI bundles out of the non-OSGI jars in components/libs.
So you must restart if you want to change the custom mediator jars.
As per the below coment explaining the answer,
WSO2 Products are running on an OSGI based platform. So if you change an OSGI bundle you can restart the bundle from the OSGI Console without restarting the whole server. Also the OSGI Container used by WSO2 is Eclipse Equinox OSGI Container. It provides the ability to add non-OSGI jars to product with the feature of, converting those jars in to OSGI Bundles. And that feature works only at the server startup. So if you want to add/change those jars you should restart the server. In runtime you can change OSGI bundles, but you can not convert non-OSGI bundeles to OSGI in runtime.
Need some advice on how to setup maven-build
of the client-jar for java-first webservices...
Previously, I have been working with wsdl-first webservices,
where you simply save the wsdl in version-control
and then generate the java client classes from it.
But recently I switched to another project,
where most webservices are already existing as java-first,
and we should now convert the builds from Ant to Maven.
What is the best way to set this up in maven and version-control
to ensure you get the latest version of the wsdl?
Obviously you dont want to save the wsdl into version-control,
since it is generated at runtime...
I guess one way is in your maven-build to start app-server on localhost,
deploy the webservice to it,
then build the client-jar from the localhost-url.
Problem is the build might then fail if some dependencies
(db-connections, other webservice, etc) are not available at build-time.
So is there some better way to do this?
I guess one way is in your maven-build to start app-server on localhost, deploy the webservice to it, then build the client-jar from the localhost-url.
Instead you could use maven wsgen plugin to generate the client artifacts in the directory of your choice. wsdl generation would be optional here.
There is nothing wrong in checking in the WSDL in version control. every time some one changes the public methods they need to update the WSDL manually or else your option of doing it by deploying it to local host is also nice idea but the client-generation would not be that frequent so you can rely on this too.
Usually we start/stop bundles via OSGI console, but for web application, how to do that once it's deployed in a container ?
Regards,
Setya
Also not sure if I got you right, but anyway:
I think it depends on how you deploy a OSGi web application. The only way I already did was that I ran Tomcat within the OSGi context. Then Tomcat scans for bundles with a ".war" ending, so its basically started as soon as it's deployed and you can control it normally via the OSGi console
not sure If I got you right, but...
Mostly applications are started just after deployment(war or eclipse)... you don't need to do anything.
but in case you want to you can do exactly the same thing you do to bundles... go to the web console of your chosen AS and start it there.
Depends what you're using - please update question with container/framework details.
Otherwise, using Karaf you may simply SSH into the framework's shell and execute commands as normal.
Another alternative would be to use WebConsole from Apache Felix - this gives you a lot of control wrt bundles (you can even upload to deploy), and also gives you an embedded shell. It's quite extensible, you can write plugins to create new tabs etc. (AFAIK this will run fine in any framework).
Currently, I am working on a new version control system as part of a final year project at University. The idea is to make it highly adaptable and pluggable.
We're using the OSGi framework (Equinox implementation) to manage our plug ins. My problem is that I can't find a simple & easy to use method for testing OSGi bundles.
Currently, I have to build the bundle using Maven and then execute a test harness. I'm looking for something like the JUnit test runner for Eclipse, as it will save me a bunch of time.
Is there a quick and easy way to test OSGi bundles?
EDIT: I don't need something to test Eclipse plug ins or GUI components, just OSGi bundles.
EDIT2: Is there some framework that supports JUnit4?
More recently, you should have a look at Pax Exam:
http://team.ops4j.org/wiki/display/paxexam/Pax+Exam
This is the current effort at OPS4J related to testing.
Here are some tools not mentioned yet:
I'm using Tycho, which is a tool for using Maven to build Eclipse plugins. If you create tests inside their own plug-ins, or plug-in fragments, Tycho can run each set of tests inside its own OSGi instance, with all its required dependencies. Intro and further info. This is working quite well for me.
jUnit4OSGI looks straightforward. You make subclasses of OSGiTestCase, and you get methods like getServiceReference(), etc.
Pluginbuilder, a headless build system for OSGi bundles / Eclipse plug-ins, has a test-running framework called Autotestsuite. It runs the tests in the context of the OSGi environment, after the build step. But, it doesn't seem to have been maintained for several years. I think that many Eclipse projects are migrating from Pluginbuilder to Tycho.
Another option is to start an instance of an OSGi container within your unit test, which you run directly, as explained here.
Here's someone who's written a small bundle test collector, which searches for JUnit (3) tests and runs them.
Spring Dynamic Modules has excellent support for testing OSGi bundles.
There is a dedicated open source OSGi testing framework on OPS4J (ops4j.org) called Pax Drone.
You might want to have a look at Pax Drone ([http://wiki.ops4j.org/confluence/x/KABo]) which enables you to use all Felix Versions as well as Equinox and Knopflerfish in your tests.
Cheers,
Toni
Eclipse has a launch configuration type for running JUnit tests in the context of an Eclipse (i.e. OSGi) application:
http://help.eclipse.org/stable/index.jsp?topic=/org.eclipse.pde.doc.user/guide/tools/launchers/junit_launcher.htm
If you need to test GUI components I've found SWTBot gets the job done.
Treaty is a contract(testing) framework that is pretty academic but has some nice ideas. There are papers that are published on it, and the people currently working on improving it.
The ProSyst Test Execution Environment is a useful test tool for OSGi bundles. It also supports JUnit tests as one of the possible test models.
For unit tests use the EasyMock framework or create your own implementations of the required interfaces for testing .
I think we met the same issue and we made our own solution. There are different parts of the solution:
A junit4runner that catches all OSGi services that has a special property defined. It runs these caught services with JUnit4 engine. JUnit annotations should be placed into interfaces that the services implement.
A maven plugin that starts an OSGi framework (a custom framework can be created as maven dependency) and runs the unit tests inside the integration-test maven lifecycle.
A deployer OSGi bundle. If this is dropped into your OSGi container a simple always-on-top window will be opened where you can drop your project folders (from total commander or from eclipse). This will then redeploy that bundle.
With the tools you can do TDD and have the written tests always run inside the maven integration-phase as well. It is recommended to use eclipse with m2e and maven-bundle-plugin as in this case the target/classes/META-INF/MANIFEST.MF is regenerated as soon as you save a class in your source so you can drag the project and drop to the deployer window. The OSGi bundles you develop do not have to have any special feature (like being an eclipse plugin or something).
The whole solution is OpenSource. You can find a tutorial at http://cookbook.everit.org
During the last couple of years Tycho - a new Maven based build system for OSGi - has become rather popular among the Eclipse Foundation. This framework also includes method to use Maven Surefire to test OSGi bundles in separate testbeds...
There are many ways to test OSGi components, I suppose. One way of doing the testing is to use Robot Framework. What I've done is made my tests with Robot Framework and have the remote libraries either installed in OSGi or have them talk to OSGi-test components through sockets and robot would talk to these modules and run tests through them.
So, basically your OSGi-modules should have interfaces that do something and produce some output. So, in my setup I had a test components that would make service calls to the actual OSGi-component and then there would be a listening-service that would catch the events/service calls (made by the module under test) and those results could be asked by the robot. So basically this way you can split a massive system in small components and have the system run in production/production like enviroment and have it tested automatically on component level or have some of the real components be tested in unison.
Along with others mentioned mockito is very handy to mock plugin dependencies(references etc). see https://www.baeldung.com/mockito-annotations
How about bnd-testing-maven-plugin?
It allow running JUnit inside a running container like Felix or Equinox.
If you used the BNDTools for eclipse this is very similar but just maven withpout eclipse and without a UI.
https://github.com/bndtools/bnd/tree/master/maven/bnd-testing-maven-plugin
also look at the effectiveosgi archetype for maven. This will give you a good starting point to build your project or just add tests.
https://github.com/effectiveosgi