Hi,

@Azeez
I have updated the getOSGiService(Class clazz) method to
getOSGiService(Class clazz, Hashtable<String, String> props) so that we can
pass properties when access to osgi services registered under a specific
interface and matching a given filter. I have updated the name of the file
to carboncontext-osgi-services.properties.
I have used following syntax for the carboncontext-osgi-services.properties
file,

allowed.osgi.services=\
    org.wso2.carbon.user.core.service.RealmService.class,\
    org.wso2.carbon.utils.ConfigurationContextService,\
    ...

Thus I have used
 allowedOSGiServices =
Arrays.asList(properties.get(CarbonConstants.OSGI_SERVICE_LIST).toString().split(","));

I will keep the 2 methods in PCC and add above methods to CC. We wanted to
improve above methods to use filters to access osgi services. I will update
the fix with your suggestions.

Thanks,
Nipuni

On Wed, Oct 29, 2014 at 1:20 PM, Afkham Azeez <[email protected]> wrote:

>
> 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 3320919 <%2B94%2077%203320919>blog: *
> *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*
>



-- 
Nipuni Perera
Software Engineer; WSO2 Inc.; http://wso2.com
Email: [email protected]
Git hub profile: https://github.com/nipuni
Mobile: +94 (71) 5626680
<http://wso2.com>
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to