Archive for the ‘osgi’ Category

Start carbon servers with OSGI console

./wso2server.sh -DosgiConsole

You will see below are the set of commands that are currently available

—Server Admin (WSO2 Carbon)—
listAdminServices – List admin services deployed on this Carbon instance
listHiddenServices – List hidden services deployed on this Carbon instance
listSystemServicesInfo – List all the systems services deployed on this Carbon instance
dumpAdminServices – Dump all the wsdls of admin services on this Carbon instance
restartCarbon – Forcefully restart this Carbon instance
restartCarbonGracefully – Gracefully restart this Carbon instance. All client connections will be served before restarting the server
shutdownCarbon – Forcefully shutdown this Carbon instance
shutdownCarbonGracefully – Gracefully shutdown this Carbon instance. All client connections will be served before shutting down the server
startCarbonMaintenance – Switch a Carbon instance to maintenance mode.
endCarbonMaintenance – Change the state of a Carbon instance from “maintenance” to “normal”

However you may want to add a command of your own. It may be helpfull for you to view runtime data.

Add a custom command
Write the command  class

You are required to implement org.eclipse.osgi.framework.console.CommandProvider interface in order to add a command of your own. You need to implement

String getHelp()This return the help text of the command which will be showned when you type “help customCommand”.Now comes your command. You must add a public method with void as the return type and method name must start with the _(underscore) before the name of the command. Method argument must be “org.eclipse.osgi.framework.console.CommandInterpreter” type. As an example if I want to add a “sayHello” command, my method is like below.public void _sayHello (CommandInterpreter ci)

package org.apache.stratos.autoscaler.commands;

import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;

public class EchoCommand implements CommandProvider{

public String getHelp() {
return "\necho - echoes the String provided. \n" + "\t parameters : \n" + "\t\t String text to be echoed.\n";
}
public void _echo(CommandInterpreter cli){
String text = ci.nextArgument();
cli.println(text);
}
}

nextArgument() gets the argument the user has given with the command (Ex “echo hellowWorld”)
println prints the text in the console.

Register the new service in OSGI

The below code will inform the OSGI framework about the new command class.

package org.apache.stratos.autoscaler.internal;
public class CommandBundleActivater implements BundleActivator{

private static final Log log = LogFactory.getLog(CommandBundleActivater.class);

@Override
public void start(BundleContext context) throws Exception {
if(log.isDebugEnabled())
log.debug("AutoScaler bundle is activated.");

context.registerService(CommandProvider.class.getName(),new EchoCommand(), null);
}
}
Add the bundle activater

Now add the foloowing lines to the required pom.xml file, so

<Bundle-Activator>org.apache.stratos.autoscaler.internal.CommandBundleActivater</Bundle-Activator>

Below are the output of the command.

osgi> help echo

echo - echoes the String provided.
 parameters :
 String text to be echoed.

osgi> echo HellowWorld
HellowWorld
Advertisements