You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Pierre De Rop <pi...@alcatel-lucent.fr> on 2009/03/19 09:45:28 UTC

Allow DM to inject service properties using a Map

Hi everyone,

This mail is most likely for Marcel and propose to improve 
DependencyManager in the following way:
Currently, when injecting dependencies with callbacks, DM searches for 
method signatures in this order:

  1. ServiceReference, TheService
  2. ServiceReference, Object
  3. ServiceReference
  4. TheService
  5. Object
  6. (void)

I propose to add the following signature, which provides service 
properties wrapped behing a basic Map:

  7. (Map, TheService)

The purpose of this modification is to be able to inject service 
properties without coupling my POJOs
with the OSGi API (that is: ServiceReference class, and 
ServiceReference.getProperty method).

I think the following patch (applicable in ServiceDependency.java) will 
do the trick:

   private void invokeCallbackMethod(Object instance, String methodName, 
ServiceReference reference, Object service) throws NoSuchMethodException {
       Class currentClazz = instance.getClass();
       boolean done = false;
       while (!done && currentClazz != null) {
           Class trackedServiceName;
           synchronized (this) {
               trackedServiceName = m_trackedServiceName;
           }
           done = invokeMethod(instance, currentClazz, methodName,
       new Class[][] {{ServiceReference.class, trackedServiceName},
                  {ServiceReference.class, Object.class},
                  {ServiceReference.class}, {trackedServiceName},
                  {Object.class},
                  {Map.class, trackedServiceName},
                  {}},
       new Object[][] {{reference, service},
               {reference, service},
               {reference},
               {service},
               {service},
               {new ServicePropertiesMap(reference), service},
               {}},
               false);
           if (!done) {
               currentClazz = currentClazz.getSuperclass();
           }
       }
       if (!done && currentClazz == null) {
           throw new NoSuchMethodException(methodName);
       }
   }

And here is the Map that wraps the ServiceReference properties:

   static class ServicePropertiesMap extends AbstractMap {
     private ServiceReference _ref;
     public ServicePropertiesMap(ServiceReference ref) {
   _ref = ref;
     }
     public Object get(Object key) { return 
_ref.getProperty(key.toString()); }
     public int size() { return _ref.getPropertyKeys().length; }
     public Set entrySet() {
   Set set = new HashSet();
   String[] keys = _ref.getPropertyKeys();
   for(int i = 0; i < keys.length; i++) {
     set.add(new ServicePropertiesMapEntry(keys[i], 
_ref.getProperty(keys[i])));
   }
   return set;
     }
   }

   static class ServicePropertiesMapEntry implements Map.Entry {
     String key;
     Object value;

     public ServicePropertiesMapEntry(String key, Object value) {
   this.key   = key;
   this.value = value;
     }

     public Object getKey() { return key; }
     public Object getValue() { return value; }
     public String toString() { return key + "=" + value; }


     public Object setValue(Object value) {
   Object oldValue = this.value;
   this.value = value;
   return oldValue;
     }

     public boolean equals(Object o) {
   if (!(o instanceof Map.Entry))
     return false;
   Map.Entry e = (Map.Entry)o;
   return eq(key, e.getKey()) && eq(value, e.getValue());
     }

     public int hashCode() {
   return ((key   == null)   ? 0 :   key.hashCode()) ^
     ((value == null)   ? 0 : value.hashCode());
     }

     private static boolean eq(Object o1, Object o2) {
   return (o1 == null ? o2 == null : o1.equals(o2));
     }
   }

-> Marcel ? are you ok to add this patch in DM ?

Best Regards
/pierre


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Allow DM to inject service properties using a Map

Posted by Stuart McCulloch <mc...@gmail.com>.
2009/3/19 Pierre De Rop <pi...@alcatel-lucent.fr>

> Hi everyone,
>
> This mail is most likely for Marcel and propose to improve
> DependencyManager in the following way:
> Currently, when injecting dependencies with callbacks, DM searches for
> method signatures in this order:
>
>  1. ServiceReference, TheService
>  2. ServiceReference, Object
>  3. ServiceReference
>  4. TheService
>  5. Object
>  6. (void)
>
> I propose to add the following signature, which provides service properties
> wrapped behing a basic Map:
>
>  7. (Map, TheService)
>
> The purpose of this modification is to be able to inject service properties
> without coupling my POJOs
> with the OSGi API (that is: ServiceReference class, and
> ServiceReference.getProperty method).
>
> I think the following patch (applicable in ServiceDependency.java) will do
> the trick:
>
>  private void invokeCallbackMethod(Object instance, String methodName,
> ServiceReference reference, Object service) throws NoSuchMethodException {
>      Class currentClazz = instance.getClass();
>      boolean done = false;
>      while (!done && currentClazz != null) {
>          Class trackedServiceName;
>          synchronized (this) {
>              trackedServiceName = m_trackedServiceName;
>          }
>          done = invokeMethod(instance, currentClazz, methodName,
>      new Class[][] {{ServiceReference.class, trackedServiceName},
>                 {ServiceReference.class, Object.class},
>                 {ServiceReference.class}, {trackedServiceName},
>                 {Object.class},
>                 {Map.class, trackedServiceName},
>                 {}},
>      new Object[][] {{reference, service},
>              {reference, service},
>              {reference},
>              {service},
>              {service},
>              {new ServicePropertiesMap(reference), service},
>              {}},
>              false);
>          if (!done) {
>              currentClazz = currentClazz.getSuperclass();
>          }
>      }
>      if (!done && currentClazz == null) {
>          throw new NoSuchMethodException(methodName);
>      }
>  }
>
> And here is the Map that wraps the ServiceReference properties:
>
>  static class ServicePropertiesMap extends AbstractMap {
>    private ServiceReference _ref;
>    public ServicePropertiesMap(ServiceReference ref) {
>  _ref = ref;
>    }
>    public Object get(Object key) { return _ref.getProperty(key.toString());
> }
>    public int size() { return _ref.getPropertyKeys().length; }
>    public Set entrySet() {
>  Set set = new HashSet();
>  String[] keys = _ref.getPropertyKeys();
>  for(int i = 0; i < keys.length; i++) {
>    set.add(new ServicePropertiesMapEntry(keys[i],
> _ref.getProperty(keys[i])));
>  }
>  return set;
>    }
>  }
>
>  static class ServicePropertiesMapEntry implements Map.Entry {
>    String key;
>    Object value;
>
>    public ServicePropertiesMapEntry(String key, Object value) {
>  this.key   = key;
>  this.value = value;
>    }
>
>    public Object getKey() { return key; }
>    public Object getValue() { return value; }
>    public String toString() { return key + "=" + value; }
>
>
>    public Object setValue(Object value) {
>  Object oldValue = this.value;
>  this.value = value;
>  return oldValue;
>    }
>
>    public boolean equals(Object o) {
>  if (!(o instanceof Map.Entry))
>    return false;
>  Map.Entry e = (Map.Entry)o;
>  return eq(key, e.getKey()) && eq(value, e.getValue());
>    }
>
>    public int hashCode() {
>  return ((key   == null)   ? 0 :   key.hashCode()) ^
>    ((value == null)   ? 0 : value.hashCode());
>    }
>
>    private static boolean eq(Object o1, Object o2) {
>  return (o1 == null ? o2 == null : o1.equals(o2));
>    }
>  }
>
> -> Marcel ? are you ok to add this patch in DM ?
>

Hi Pierre,

You should really open a JIRA issue against DM and attach this patch
(remember to tick the box granting a license to the ASF for inclusion)

Then Marcel can review it and everything gets tracked properly.

-- 
Thanks in advance, Stuart


> Best Regards
> /pierre
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>