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);
                }
            }
        } 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;
    }
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to