Adds SimpleServiceActivator utility class

cusax-fix
paweldomas 12 years ago
parent dfd5a88621
commit 1a94b834dd

@ -9,59 +9,31 @@
import net.java.sip.communicator.service.browserlauncher.*;
import net.java.sip.communicator.util.*;
import org.osgi.framework.*;
/**
* Implements <tt>BundleActivator</tt> for the browserlauncher bundle.
*
* @author Yana Stamcheva
* @author Lubomir Marinov
* @author Pawel Domas
*/
public class BrowserLauncherActivator
implements BundleActivator
extends SimpleServiceActivator<BrowserLauncherImpl>
{
/**
* The <tt>Logger</tt> instance used by the
* <tt>BrowserLauncherActivator</tt> class and its instances for logging
* output.
*/
private static final Logger logger
= Logger.getLogger(BrowserLauncherActivator.class);
/**
* Initialize and start the service.
*
* @param bundleContext the <tt>BundleContext</tt>
* @throws Exception if initializing and starting this service fails
* Creates new instance of <tt>BrowserLauncherActivator</tt>.
*/
public void start(BundleContext bundleContext)
throws Exception
public BrowserLauncherActivator()
{
//Create the browser launcher service
BrowserLauncherService browserLauncher = new BrowserLauncherImpl();
if (logger.isInfoEnabled())
logger.info("Browser Launcher Service STARTED");
bundleContext
.registerService(
BrowserLauncherService.class.getName(),
browserLauncher,
null);
if (logger.isInfoEnabled())
logger.info("Browser Launcher Service REGISTERED");
super(BrowserLauncherService.class, "Browser Launcher Service");
}
/**
* Stops this bundle.
*
* @param bundleContext the <tt>BundleContext</tt>
* @throws Exception if the stop operation goes wrong
* {@inheritDoc}
*/
public void stop(BundleContext bundleContext)
throws Exception
@Override
protected BrowserLauncherImpl createServiceImpl()
{
return new BrowserLauncherImpl();
}
}

@ -8,46 +8,34 @@
import net.java.sip.communicator.util.*;
import org.jitsi.service.configuration.*;
import org.jitsi.service.resources.*;
import org.osgi.framework.*;
/**
* Starts Resource Management Service.
* @author Damian Minkov
* @author Pawel Domas
*/
public class ResourceManagementActivator
implements BundleActivator
extends SimpleServiceActivator<ResourceManagementServiceImpl>
{
private Logger logger =
Logger.getLogger(ResourceManagementActivator.class);
static BundleContext bundleContext;
private ResourceManagementServiceImpl resPackImpl = null;
private static ConfigurationService configService;
/**
* Starts this bundle.
*
* @param bc the osgi bundle context
* @throws Exception
* Creates new instance of <tt>ResourceManagementActivator</tt>
*/
public void start(BundleContext bc) throws Exception
public ResourceManagementActivator()
{
bundleContext = bc;
resPackImpl =
new ResourceManagementServiceImpl();
super(ResourceManagementService.class, "Resource manager");
}
bundleContext.registerService(
ResourceManagementService.class.getName(),
resPackImpl,
null);
@Override
public void start(BundleContext bc)
throws Exception
{
bundleContext = bc;
if (logger.isInfoEnabled())
logger.info("Resource manager ... [REGISTERED]");
super.start(bc);
}
/**
@ -58,27 +46,15 @@ public void start(BundleContext bc) throws Exception
*/
public void stop(BundleContext bc) throws Exception
{
bc.removeServiceListener(resPackImpl);
configService = null;
bc.removeServiceListener(serviceImpl);
}
/**
* Returns the <tt>ConfigurationService</tt> obtained from the bundle
* context.
* @return the <tt>ConfigurationService</tt> obtained from the bundle
* context
* {@inheritDoc}
*/
public static ConfigurationService getConfigurationService()
@Override
protected ResourceManagementServiceImpl createServiceImpl()
{
if(configService == null) {
ServiceReference configReference = bundleContext
.getServiceReference(ConfigurationService.class.getName());
configService = (ConfigurationService) bundleContext
.getService(configReference);
}
return configService;
return new ResourceManagementServiceImpl();
}
}

@ -0,0 +1,100 @@
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.util;
import org.osgi.framework.*;
/**
* Base class for activators which only register new service in bundle context.
* Service registration activity is logged on <tt>INFO</tt> level.
*
* @param <T> service implementation template type
* (for convenient instance access)
*
* @author Pawel Domas
*/
public abstract class SimpleServiceActivator<T>
implements BundleActivator
{
/**
* The <tt>Logger</tt> instance used for logging output.
*/
private final Logger logger;
/**
* Class of the service
*/
private final Class<?> serviceClass;
/**
* Service name that will be used in log messages
*/
private final String serviceName;
/**
* Instance of service implementation
*/
protected T serviceImpl;
/**
* Creates new instance of <tt>SimpleServiceActivator</tt>
*
* @param serviceClass class of service that will be registered on bundle
* startup
* @param serviceName service name that wil be used in log messages
*/
public SimpleServiceActivator(Class<?> serviceClass, String serviceName)
{
this.serviceClass = serviceClass;
this.serviceName = serviceName;
logger = Logger.getLogger(this.getClass().getName());
}
/**
* Initialize and start the service.
*
* @param bundleContext the <tt>BundleContext</tt>
* @throws Exception if initializing and starting this service fails
*/
public void start(BundleContext bundleContext)
throws Exception
{
//Create the service impl
serviceImpl = createServiceImpl();
if (logger.isInfoEnabled())
logger.info(serviceName+" STARTED");
bundleContext
.registerService(
serviceClass.getName(),
serviceImpl,
null);
if (logger.isInfoEnabled())
logger.info(serviceName+" REGISTERED");
}
/**
* Stops this bundle.
*
* @param bundleContext the <tt>BundleContext</tt>
* @throws Exception if the stop operation goes wrong
*/
public void stop(BundleContext bundleContext)
throws Exception
{
}
/**
* Called on bundle startup in order to create service implementation
* instance.
*
* @return should return new instance of service implementation.
*/
protected abstract T createServiceImpl();
}
Loading…
Cancel
Save