On Wed, Oct 29, 2014 at 12:41 AM, Afkham Azeez <[email protected]> wrote:
> Here you go. This is the implementation we require. I have not tested this
> but the logic flow should be correct.
>
>
> /**
> * Obtain the first OSGi service found for interface or class
> <code>clazz</code>
> * @param clazz The type of the OSGi service
> * @return The OSGi service
> */
> public Object getOSGiService(Class clazz) {
> if(!allowedOSGiServices.contains(clazz.getName())) {
> throw new SecurityException("OSGi service " + clazz + " cannot
> be accessed via CarbonContext");
> }
> BundleContext bundleContext = dataHolder.getBundleContext();
> ServiceTracker serviceTracker = new ServiceTracker(bundleContext,
> clazz, null);
> try {
> serviceTracker.open();
> return serviceTracker.getServices()[0];
> } finally {
> serviceTracker.close();
> }
> }
>
> private static List<String> allowedOSGiServices = new
> ArrayList<String>();
>
> static {
> FileInputStream fileInputStream = null;
> String osgiServicesFilename =
> CarbonUtils.getEtcCarbonConfigDirPath() + File.separator +
> "carboncontext-osgi-services.properties";
> try {
> Properties osgiServices = new Properties();
> if(new File(osgiServicesFilename).exists()) { // this file is
> an optional file
> fileInputStream = new
> FileInputStream(osgiServicesFilename);
> osgiServices.load(fileInputStream);
> Set<String> propNames = osgiServices.stringPropertyNames();
> for (String propName : propNames) {
> allowedOSGiServices.add(propName);
>
Oops... bug here... here is the correct code:
allowedOSGiServices.add(osgiServices.getProperty(propName));
> }
> }
> } catch (IOException e) {
> log.fatal("Cannot load " + osgiServicesFilename, e);
> } finally {
> if(fileInputStream != null){
> try {
> fileInputStream.close();
> } catch (IOException e) {
> log.warn("Could not close FileInputStream of file " +
> osgiServicesFilename, e);
> }
> }
> }
> }
>
> /**
> * Obtain the OSGi services found for interface or class
> <code>clazz</code>
> * @param clazz The type of the OSGi service
> * @return The List of OSGi services
> */
> public List<Object> getOSGiServices(Class clazz) {
> if(!allowedOSGiServices.contains(clazz.getName())) {
> throw new SecurityException("OSGi service " + clazz + " cannot
> be accessed via CarbonContext");
> }
> BundleContext bundleContext = dataHolder.getBundleContext();
> ServiceTracker serviceTracker = new ServiceTracker(bundleContext,
> clazz, null);
> List<Object> services = new ArrayList<Object>();
> try {
> serviceTracker.open();
> Collections.addAll(services, serviceTracker.getServices());
> } finally {
> serviceTracker.close();
> }
> return services;
> }
>
>
--
*Afkham Azeez*
Director of Architecture; WSO2, Inc.; http://wso2.com
Member; Apache Software Foundation; http://www.apache.org/
* <http://www.apache.org/>*
*email: **[email protected]* <[email protected]>
* cell: +94 77 3320919blog: **http://blog.afkham.org*
<http://blog.afkham.org>
*twitter: **http://twitter.com/afkham_azeez*
<http://twitter.com/afkham_azeez>
*linked-in: **http://lk.linkedin.com/in/afkhamazeez
<http://lk.linkedin.com/in/afkhamazeez>*
*Lean . Enterprise . Middleware*
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev