Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/48#discussion_r15048426
  
    --- Diff: core/src/main/java/brooklyn/entity/effector/AddSensor.java ---
    @@ -53,10 +52,23 @@ public AddSensor(T sensor) {
         public void apply(EntityLocal entity) {
             ((EntityInternal)entity).getMutableEntityType().addSensor(sensor);
         }
    -    
    -    public static <T> AttributeSensor<T> newSensor(Class<T> type, 
ConfigBag params) {
    +
    +    public static <T> AttributeSensor<T> newSensor(Class<T> type, 
ConfigBag params){
             String name = Preconditions.checkNotNull(params.get(SENSOR_NAME), 
"name must be supplied when defining a sensor");
    -        return Sensors.newSensor(type, name); 
    +        return Sensors.newSensor(type, name);
    +    }
    +
    +    public static <T> AttributeSensor<T> newSensor(ConfigBag params) {
    +        String name = Preconditions.checkNotNull(params.get(SENSOR_NAME), 
"name must be supplied when defining a sensor");
    +        String className = 
Preconditions.checkNotNull(params.get(SENSOR_TYPE), "target class must be 
supplied when defining a sensor");
    +        Class<T> type = null;
    +
    +        try {
    +            type = (Class<T>) Class.forName(className);
    --- End diff --
    
    Unfortunately this won't work well with the OSGi work that is progressing. 
The `Class.forName` will use the class loader of the `AddSensor` class, which 
is fine if there is a flat classpath. However, with OSGi then the entity's 
classes (including those for custom sensor types) could be in OSGi bundles that 
are added on-the-fly. This classloading with then fail.
    
    However, this code will work for all the standard types like `String`, 
`URI`, etc so I'm inclined to include it. If @ahgittin agrees, then could you 
add some javadoc to this method like:
    
        /**
         * Creates a new sensor. The configuration can include the sensor 
{@code name} and {@code targetType}.
         * For the targetType, currently this only supports classes on the 
initial classpath 
         * (e.g. not those in OSGi bundles added at runtime).
         */
        @Beta


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to