org.fourthline.cling.binding.LocalServiceBindingException Java Examples

The following examples show how to use org.fourthline.cling.binding.LocalServiceBindingException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SempDiscovery.java    From SmartApplianceEnabler with GNU General Public License v2.0 6 votes vote down vote up
private LocalDevice createDevice()
        throws ValidationException, LocalServiceBindingException, IOException {

    DeviceIdentity identity =
            new DeviceIdentity(
                    UDN.uniqueSystemIdentifier(SmartApplianceEnabler.class.getSimpleName())
            );

    DeviceType type = new SmartApplianceEnablerDeviceType();

    DeviceDetails details =
            new DeviceDetails(
                    SmartApplianceEnabler.class.getSimpleName(),
                    new ManufacturerDetails(SmartApplianceEnabler.MANUFACTURER_NAME, URI.create(SmartApplianceEnabler.MANUFACTURER_URI)),
                    new ModelDetails(
                            SmartApplianceEnabler.class.getSimpleName(),
                            SmartApplianceEnabler.DESCRIPTION,
                            SmartApplianceEnabler.VERSION,
                            URI.create(SmartApplianceEnabler.MODEL_URI)
                    )
            );

    return new LocalDevice(identity, type, details, (Icon) null, (LocalService) null);
}
 
Example #2
Source File: AnnotationStateVariableBinder.java    From DroidDLNA with GNU General Public License v3.0 6 votes vote down vote up
protected StateVariableAllowedValueRange getAllowedRangeFromProvider() throws  LocalServiceBindingException {
    Class provider = getAnnotation().allowedValueRangeProvider();
    if (!AllowedValueRangeProvider.class.isAssignableFrom(provider))
        throw new LocalServiceBindingException(
            "Allowed value range provider is not of type " + AllowedValueRangeProvider.class + ": " + getName()
        );
    try {
        AllowedValueRangeProvider providerInstance =
            ((Class<? extends AllowedValueRangeProvider>) provider).newInstance();
        return getAllowedValueRange(
            providerInstance.getMinimum(),
            providerInstance.getMaximum(),
            providerInstance.getStep()
        );
    } catch (Exception ex) {
        throw new LocalServiceBindingException(
            "Allowed value range provider can't be instantiated: " + getName(), ex
        );
    }
}
 
Example #3
Source File: AnnotationStateVariableBinder.java    From DroidDLNA with GNU General Public License v3.0 6 votes vote down vote up
protected String[] getAllowedValues(Class enumType) throws LocalServiceBindingException {

        if (!enumType.isEnum()) {
            throw new LocalServiceBindingException("Allowed values type is not an Enum: " + enumType);
        }

        log.finer("Restricting allowed values of state variable to Enum: " + getName());
        String[] allowedValueStrings = new String[enumType.getEnumConstants().length];
        for (int i = 0; i < enumType.getEnumConstants().length; i++) {
            Object o = enumType.getEnumConstants()[i];
            if (o.toString().length() > 32) {
                throw new LocalServiceBindingException(
                        "Allowed value string (that is, Enum constant name) is longer than 32 characters: " + o.toString()
                );
            }
            log.finer("Adding allowed value (converted to string): " + o.toString());
            allowedValueStrings[i] = o.toString();
        }

        return allowedValueStrings;
    }
 
Example #4
Source File: AnnotationStateVariableBinder.java    From DroidDLNA with GNU General Public License v3.0 6 votes vote down vote up
protected String createDefaultValue(Datatype datatype) throws LocalServiceBindingException {

        // Next, the default value of the state variable, first the declared one
        if (getAnnotation().defaultValue().length() != 0) {
            // The declared default value needs to match the datatype
            try {
                datatype.valueOf(getAnnotation().defaultValue());
                log.finer("Found state variable default value: " + getAnnotation().defaultValue());
                return getAnnotation().defaultValue();
            } catch (Exception ex) {
                throw new LocalServiceBindingException(
                        "Default value doesn't match datatype of state variable '" + getName() + "': " + ex.getMessage()
                );
            }
        }

        return null;
    }
 
Example #5
Source File: AnnotationLocalServiceBinder.java    From DroidDLNA with GNU General Public License v3.0 6 votes vote down vote up
protected Map<Action, ActionExecutor> readActions(Class<?> clazz,
                                                  Map<StateVariable, StateVariableAccessor> stateVariables,
                                                  Set<Class> stringConvertibleTypes)
        throws LocalServiceBindingException {

    Map<Action, ActionExecutor> map = new HashMap();

    for (Method method : Reflections.getMethods(clazz, UpnpAction.class)) {
        AnnotationActionBinder actionBinder =
                new AnnotationActionBinder(method, stateVariables, stringConvertibleTypes);
        Action action = actionBinder.appendAction(map);
        if(isActionExcluded(action)) {
        	map.remove(action);
        }
    }

    return map;
}
 
Example #6
Source File: AnnotationLocalServiceBinder.java    From DroidDLNA with GNU General Public License v3.0 6 votes vote down vote up
protected Set<Class> readStringConvertibleTypes(Class[] declaredTypes) throws LocalServiceBindingException {

        for (Class stringConvertibleType : declaredTypes) {
            if (!Modifier.isPublic(stringConvertibleType.getModifiers())) {
                throw new LocalServiceBindingException(
                        "Declared string-convertible type must be public: " + stringConvertibleType
                );
            }
            try {
                stringConvertibleType.getConstructor(String.class);
            } catch (NoSuchMethodException ex) {
                throw new LocalServiceBindingException(
                        "Declared string-convertible type needs a public single-argument String constructor: " + stringConvertibleType
                );
            }
        }
        Set<Class> stringConvertibleTypes = new HashSet(Arrays.asList(declaredTypes));

        // Some defaults
        stringConvertibleTypes.add(URI.class);
        stringConvertibleTypes.add(URL.class);
        stringConvertibleTypes.add(CSV.class);

        return stringConvertibleTypes;
    }
 
Example #7
Source File: AnnotationLocalServiceBinder.java    From DroidDLNA with GNU General Public License v3.0 6 votes vote down vote up
public LocalService read(Class<?> clazz, ServiceId id, ServiceType type,
                               boolean supportsQueryStateVariables, Set<Class> stringConvertibleTypes)
        throws LocalServiceBindingException {

    Map<StateVariable, StateVariableAccessor> stateVariables = readStateVariables(clazz, stringConvertibleTypes);
    Map<Action, ActionExecutor> actions = readActions(clazz, stateVariables, stringConvertibleTypes);

    // Special treatment of the state variable querying action
    if (supportsQueryStateVariables) {
        actions.put(new QueryStateVariableAction(), new QueryStateVariableExecutor());
    }

    try {
        return new LocalService(type, id, actions, stateVariables, stringConvertibleTypes, supportsQueryStateVariables);

    } catch (ValidationException ex) {
        log.severe("Could not validate device model: " + ex.toString());
        for (ValidationError validationError : ex.getErrors()) {
            log.severe(validationError.toString());
        }
        throw new LocalServiceBindingException("Validation of model failed, check the log");
    }
}
 
Example #8
Source File: AnnotationActionBinder.java    From DroidDLNA with GNU General Public License v3.0 6 votes vote down vote up
public Action appendAction(Map<Action, ActionExecutor> actions) throws LocalServiceBindingException {

        String name;
        if (getAnnotation().name().length() != 0) {
            name = getAnnotation().name();
        } else {
            name = AnnotationLocalServiceBinder.toUpnpActionName(getMethod().getName());
        }

        log.fine("Creating action and executor: " + name);

        List<ActionArgument> inputArguments = createInputArguments();
        Map<ActionArgument<LocalService>, StateVariableAccessor> outputArguments = createOutputArguments();

        inputArguments.addAll(outputArguments.keySet());
        ActionArgument<LocalService>[] actionArguments =
                inputArguments.toArray(new ActionArgument[inputArguments.size()]);

        Action action = new Action(name, actionArguments);
        ActionExecutor executor = createExecutor(outputArguments);

        actions.put(action, executor);
        return action;
    }
 
Example #9
Source File: AnnotationActionBinder.java    From TVRemoteIME with GNU General Public License v2.0 6 votes vote down vote up
public Action appendAction(Map<Action, ActionExecutor> actions) throws LocalServiceBindingException {

        String name;
        if (getAnnotation().name().length() != 0) {
            name = getAnnotation().name();
        } else {
            name = AnnotationLocalServiceBinder.toUpnpActionName(getMethod().getName());
        }

        log.fine("Creating action and executor: " + name);

        List<ActionArgument> inputArguments = createInputArguments();
        Map<ActionArgument<LocalService>, StateVariableAccessor> outputArguments = createOutputArguments();

        inputArguments.addAll(outputArguments.keySet());
        ActionArgument<LocalService>[] actionArguments =
                inputArguments.toArray(new ActionArgument[inputArguments.size()]);

        Action action = new Action(name, actionArguments);
        ActionExecutor executor = createExecutor(outputArguments);

        actions.put(action, executor);
        return action;
    }
 
Example #10
Source File: AnnotationStateVariableBinder.java    From TVRemoteIME with GNU General Public License v2.0 6 votes vote down vote up
protected StateVariableAllowedValueRange getAllowedRangeFromProvider() throws  LocalServiceBindingException {
    Class provider = getAnnotation().allowedValueRangeProvider();
    if (!AllowedValueRangeProvider.class.isAssignableFrom(provider))
        throw new LocalServiceBindingException(
            "Allowed value range provider is not of type " + AllowedValueRangeProvider.class + ": " + getName()
        );
    try {
        AllowedValueRangeProvider providerInstance =
            ((Class<? extends AllowedValueRangeProvider>) provider).newInstance();
        return getAllowedValueRange(
            providerInstance.getMinimum(),
            providerInstance.getMaximum(),
            providerInstance.getStep()
        );
    } catch (Exception ex) {
        throw new LocalServiceBindingException(
            "Allowed value range provider can't be instantiated: " + getName(), ex
        );
    }
}
 
Example #11
Source File: AnnotationStateVariableBinder.java    From TVRemoteIME with GNU General Public License v2.0 6 votes vote down vote up
protected String createDefaultValue(Datatype datatype) throws LocalServiceBindingException {

        // Next, the default value of the state variable, first the declared one
        if (getAnnotation().defaultValue().length() != 0) {
            // The declared default value needs to match the datatype
            try {
                datatype.valueOf(getAnnotation().defaultValue());
                log.finer("Found state variable default value: " + getAnnotation().defaultValue());
                return getAnnotation().defaultValue();
            } catch (Exception ex) {
                throw new LocalServiceBindingException(
                        "Default value doesn't match datatype of state variable '" + getName() + "': " + ex.getMessage()
                );
            }
        }

        return null;
    }
 
Example #12
Source File: AnnotationStateVariableBinder.java    From TVRemoteIME with GNU General Public License v2.0 6 votes vote down vote up
protected String[] getAllowedValues(Class enumType) throws LocalServiceBindingException {

        if (!enumType.isEnum()) {
            throw new LocalServiceBindingException("Allowed values type is not an Enum: " + enumType);
        }

        log.finer("Restricting allowed values of state variable to Enum: " + getName());
        String[] allowedValueStrings = new String[enumType.getEnumConstants().length];
        for (int i = 0; i < enumType.getEnumConstants().length; i++) {
            Object o = enumType.getEnumConstants()[i];
            if (o.toString().length() > 32) {
                throw new LocalServiceBindingException(
                        "Allowed value string (that is, Enum constant name) is longer than 32 characters: " + o.toString()
                );
            }
            log.finer("Adding allowed value (converted to string): " + o.toString());
            allowedValueStrings[i] = o.toString();
        }

        return allowedValueStrings;
    }
 
Example #13
Source File: AnnotationLocalServiceBinder.java    From TVRemoteIME with GNU General Public License v2.0 6 votes vote down vote up
protected Map<Action, ActionExecutor> readActions(Class<?> clazz,
                                                  Map<StateVariable, StateVariableAccessor> stateVariables,
                                                  Set<Class> stringConvertibleTypes)
        throws LocalServiceBindingException {

    Map<Action, ActionExecutor> map = new HashMap();

    for (Method method : Reflections.getMethods(clazz, UpnpAction.class)) {
        AnnotationActionBinder actionBinder =
                new AnnotationActionBinder(method, stateVariables, stringConvertibleTypes);
        Action action = actionBinder.appendAction(map);
        if(isActionExcluded(action)) {
        	map.remove(action);
        }
    }

    return map;
}
 
Example #14
Source File: AnnotationLocalServiceBinder.java    From TVRemoteIME with GNU General Public License v2.0 6 votes vote down vote up
protected Set<Class> readStringConvertibleTypes(Class[] declaredTypes) throws LocalServiceBindingException {

        for (Class stringConvertibleType : declaredTypes) {
            if (!Modifier.isPublic(stringConvertibleType.getModifiers())) {
                throw new LocalServiceBindingException(
                        "Declared string-convertible type must be public: " + stringConvertibleType
                );
            }
            try {
                stringConvertibleType.getConstructor(String.class);
            } catch (NoSuchMethodException ex) {
                throw new LocalServiceBindingException(
                        "Declared string-convertible type needs a public single-argument String constructor: " + stringConvertibleType
                );
            }
        }
        Set<Class> stringConvertibleTypes = new HashSet(Arrays.asList(declaredTypes));

        // Some defaults
        stringConvertibleTypes.add(URI.class);
        stringConvertibleTypes.add(URL.class);
        stringConvertibleTypes.add(CSV.class);

        return stringConvertibleTypes;
    }
 
Example #15
Source File: AnnotationLocalServiceBinder.java    From TVRemoteIME with GNU General Public License v2.0 6 votes vote down vote up
public LocalService read(Class<?> clazz, ServiceId id, ServiceType type,
                               boolean supportsQueryStateVariables, Set<Class> stringConvertibleTypes)
        throws LocalServiceBindingException {

    Map<StateVariable, StateVariableAccessor> stateVariables = readStateVariables(clazz, stringConvertibleTypes);
    Map<Action, ActionExecutor> actions = readActions(clazz, stateVariables, stringConvertibleTypes);

    // Special treatment of the state variable querying action
    if (supportsQueryStateVariables) {
        actions.put(new QueryStateVariableAction(), new QueryStateVariableExecutor());
    }

    try {
        return new LocalService(type, id, actions, stateVariables, stringConvertibleTypes, supportsQueryStateVariables);

    } catch (ValidationException ex) {
        log.severe("Could not validate device model: " + ex.toString());
        for (ValidationError validationError : ex.getErrors()) {
            log.severe(validationError.toString());
        }
        throw new LocalServiceBindingException("Validation of model failed, check the log");
    }
}
 
Example #16
Source File: AnnotationStateVariableBinder.java    From TVRemoteIME with GNU General Public License v2.0 5 votes vote down vote up
protected StateVariableAllowedValueRange getAllowedValueRange(long min,
                                                              long max,
                                                              long step) throws LocalServiceBindingException {
    if (max < min) {
        throw new LocalServiceBindingException(
                "Allowed value range maximum is smaller than minimum: " + getName()
        );
    }

    return new StateVariableAllowedValueRange(min, max, step);
}
 
Example #17
Source File: AnnotationStateVariableBinder.java    From TVRemoteIME with GNU General Public License v2.0 5 votes vote down vote up
protected String[] getAllowedValuesFromProvider() throws LocalServiceBindingException {
    Class provider = getAnnotation().allowedValueProvider();
    if (!AllowedValueProvider.class.isAssignableFrom(provider))
        throw new LocalServiceBindingException(
            "Allowed value provider is not of type " + AllowedValueProvider.class + ": " + getName()
        );
    try {
        return ((Class<? extends AllowedValueProvider>) provider).newInstance().getValues();
    } catch (Exception ex) {
        throw new LocalServiceBindingException(
            "Allowed value provider can't be instantiated: " + getName(), ex
        );
    }
}
 
Example #18
Source File: AnnotationStateVariableBinder.java    From DroidDLNA with GNU General Public License v3.0 5 votes vote down vote up
protected String[] getAllowedValuesFromProvider() throws LocalServiceBindingException {
    Class provider = getAnnotation().allowedValueProvider();
    if (!AllowedValueProvider.class.isAssignableFrom(provider))
        throw new LocalServiceBindingException(
            "Allowed value provider is not of type " + AllowedValueProvider.class + ": " + getName()
        );
    try {
        return ((Class<? extends AllowedValueProvider>) provider).newInstance().getValues();
    } catch (Exception ex) {
        throw new LocalServiceBindingException(
            "Allowed value provider can't be instantiated: " + getName(), ex
        );
    }
}
 
Example #19
Source File: AnnotationStateVariableBinder.java    From DroidDLNA with GNU General Public License v3.0 5 votes vote down vote up
protected StateVariableAllowedValueRange getAllowedValueRange(long min,
                                                              long max,
                                                              long step) throws LocalServiceBindingException {
    if (max < min) {
        throw new LocalServiceBindingException(
                "Allowed value range maximum is smaller than minimum: " + getName()
        );
    }

    return new StateVariableAllowedValueRange(min, max, step);
}
 
Example #20
Source File: AnnotationActionBinder.java    From DroidDLNA with GNU General Public License v3.0 5 votes vote down vote up
protected void validateType(StateVariable stateVariable, Class type) throws LocalServiceBindingException {

        // Validate datatype as good as we can
        // (for enums and other convertible types, the state variable type should be STRING)

        Datatype.Default expectedDefaultMapping =
                ModelUtil.isStringConvertibleType(getStringConvertibleTypes(), type)
                        ? Datatype.Default.STRING
                        : Datatype.Default.getByJavaType(type);

        log.finer("Expecting '" + stateVariable + "' to match default mapping: " + expectedDefaultMapping);

        if (expectedDefaultMapping != null &&
                !stateVariable.getTypeDetails().getDatatype().isHandlingJavaType(expectedDefaultMapping.getJavaType())) {

            // TODO: Consider custom types?!
            throw new LocalServiceBindingException(
                    "State variable '" + stateVariable + "' datatype can't handle action " +
                            "argument's Java type (change one): " + expectedDefaultMapping.getJavaType()
            );

        } else if (expectedDefaultMapping == null && stateVariable.getTypeDetails().getDatatype().getBuiltin() != null) {
            throw new LocalServiceBindingException(
                    "State variable '" + stateVariable  + "' should be custom datatype " +
                            "(action argument type is unknown Java type): " + type.getSimpleName()
            );
        }

        log.finer("State variable matches required argument datatype (or can't be validated because it is custom)");
    }
 
Example #21
Source File: AnnotationActionBinder.java    From TVRemoteIME with GNU General Public License v2.0 5 votes vote down vote up
protected void validateType(StateVariable stateVariable, Class type) throws LocalServiceBindingException {

        // Validate datatype as good as we can
        // (for enums and other convertible types, the state variable type should be STRING)

        Datatype.Default expectedDefaultMapping =
                ModelUtil.isStringConvertibleType(getStringConvertibleTypes(), type)
                        ? Datatype.Default.STRING
                        : Datatype.Default.getByJavaType(type);

        log.finer("Expecting '" + stateVariable + "' to match default mapping: " + expectedDefaultMapping);

        if (expectedDefaultMapping != null &&
                !stateVariable.getTypeDetails().getDatatype().isHandlingJavaType(expectedDefaultMapping.getJavaType())) {

            // TODO: Consider custom types?!
            throw new LocalServiceBindingException(
                    "State variable '" + stateVariable + "' datatype can't handle action " +
                            "argument's Java type (change one): " + expectedDefaultMapping.getJavaType()
            );

        } else if (expectedDefaultMapping == null && stateVariable.getTypeDetails().getDatatype().getBuiltin() != null) {
            throw new LocalServiceBindingException(
                    "State variable '" + stateVariable  + "' should be custom datatype " +
                            "(action argument type is unknown Java type): " + type.getSimpleName()
            );
        }

        log.finer("State variable matches required argument datatype (or can't be validated because it is custom)");
    }
 
Example #22
Source File: AnnotationActionBinder.java    From TVRemoteIME with GNU General Public License v2.0 4 votes vote down vote up
protected List<ActionArgument> createInputArguments() throws LocalServiceBindingException {

        List<ActionArgument> list = new ArrayList();

        // Input arguments are always method parameters
        int annotatedParams = 0;
        Annotation[][] params = getMethod().getParameterAnnotations();
        for (int i = 0; i < params.length; i++) {
            Annotation[] param = params[i];
            for (Annotation paramAnnotation : param) {
                if (paramAnnotation instanceof UpnpInputArgument) {
                    UpnpInputArgument inputArgumentAnnotation = (UpnpInputArgument) paramAnnotation;
                    annotatedParams++;

                    String argumentName =
                            inputArgumentAnnotation.name();

                    StateVariable stateVariable =
                            findRelatedStateVariable(
                                    inputArgumentAnnotation.stateVariable(),
                                    argumentName,
                                    getMethod().getName()
                            );

                    if (stateVariable == null) {
                        throw new LocalServiceBindingException(
                                "Could not detected related state variable of argument: " + argumentName
                        );
                    }

                    validateType(stateVariable, getMethod().getParameterTypes()[i]);

                    ActionArgument inputArgument = new ActionArgument(
                            argumentName,
                            inputArgumentAnnotation.aliases(),
                            stateVariable.getName(),
                            ActionArgument.Direction.IN
                    );

                    list.add(inputArgument);
                }
            }
        }
        // A method can't have any parameters that are not annotated with @UpnpInputArgument - we wouldn't know what
        // value to pass when we invoke it later on... unless the last parameter is of type RemoteClientInfo
        if (annotatedParams < getMethod().getParameterTypes().length
            && !RemoteClientInfo.class.isAssignableFrom(method.getParameterTypes()[method.getParameterTypes().length-1])) {
            throw new LocalServiceBindingException("Method has parameters that are not input arguments: " + getMethod().getName());
        }

        return list;
    }
 
Example #23
Source File: AnnotationActionBinder.java    From TVRemoteIME with GNU General Public License v2.0 4 votes vote down vote up
protected Map<ActionArgument<LocalService>, StateVariableAccessor> createOutputArguments() throws LocalServiceBindingException {

        Map<ActionArgument<LocalService>, StateVariableAccessor> map = new LinkedHashMap(); // !!! Insertion order!

        UpnpAction actionAnnotation = getMethod().getAnnotation(UpnpAction.class);
        if (actionAnnotation.out().length == 0) return map;

        boolean hasMultipleOutputArguments = actionAnnotation.out().length > 1;

        for (UpnpOutputArgument outputArgumentAnnotation : actionAnnotation.out()) {

            String argumentName = outputArgumentAnnotation.name();

            StateVariable stateVariable = findRelatedStateVariable(
                    outputArgumentAnnotation.stateVariable(),
                    argumentName,
                    getMethod().getName()
            );

            // Might-just-work attempt, try the name of the getter
            if (stateVariable == null && outputArgumentAnnotation.getterName().length() > 0) {
                stateVariable = findRelatedStateVariable(null, null, outputArgumentAnnotation.getterName());
            }

            if (stateVariable == null) {
                throw new LocalServiceBindingException(
                        "Related state variable not found for output argument: " + argumentName
                );
            }

            StateVariableAccessor accessor = findOutputArgumentAccessor(
                    stateVariable,
                    outputArgumentAnnotation.getterName(),
                    hasMultipleOutputArguments
            );

            log.finer("Found related state variable for output argument '" + argumentName + "': " + stateVariable);

            ActionArgument outputArgument = new ActionArgument(
                    argumentName,
                    stateVariable.getName(),
                    ActionArgument.Direction.OUT,
                    !hasMultipleOutputArguments
            );

            map.put(outputArgument, accessor);
        }

        return map;
    }
 
Example #24
Source File: AnnotationStateVariableBinder.java    From DroidDLNA with GNU General Public License v3.0 4 votes vote down vote up
protected Datatype createDatatype() throws LocalServiceBindingException {

        String declaredDatatype = getAnnotation().datatype();

        if (declaredDatatype.length() == 0 && getAccessor() != null) {
            Class returnType = getAccessor().getReturnType();
            log.finer("Using accessor return type as state variable type: " + returnType);

            if (ModelUtil.isStringConvertibleType(getStringConvertibleTypes(), returnType)) {
                // Enums and toString() convertible types are always state variables with type STRING
                log.finer("Return type is string-convertible, using string datatype");
                return Datatype.Default.STRING.getBuiltinType().getDatatype();
            } else {
                Datatype.Default defaultDatatype = Datatype.Default.getByJavaType(returnType);
                if (defaultDatatype != null) {
                    log.finer("Return type has default UPnP datatype: " + defaultDatatype);
                    return defaultDatatype.getBuiltinType().getDatatype();
                }
            }
        }

        // We can also guess that if the allowed values are set then it's a string
        if ((declaredDatatype == null || declaredDatatype.length() == 0) &&
                (getAnnotation().allowedValues().length > 0 || getAnnotation().allowedValuesEnum() != void.class)) {
            log.finer("State variable has restricted allowed values, hence using 'string' datatype");
            declaredDatatype = "string";
        }

        // If we still don't have it, there is nothing more we can do
        if (declaredDatatype == null || declaredDatatype.length() == 0) {
            throw new LocalServiceBindingException("Could not detect datatype of state variable: " + getName());
        }

        log.finer("Trying to find built-in UPnP datatype for detected name: " + declaredDatatype);

        // Now try to find the actual UPnP datatype by mapping the Default to Builtin
        Datatype.Builtin builtin = Datatype.Builtin.getByDescriptorName(declaredDatatype);
        if (builtin != null) {
            log.finer("Found built-in UPnP datatype: " + builtin);
            return builtin.getDatatype();
        } else {
            // TODO
            throw new LocalServiceBindingException("No built-in UPnP datatype found, using CustomDataType (TODO: NOT IMPLEMENTED)");
        }
    }
 
Example #25
Source File: AnnotationLocalServiceBinder.java    From TVRemoteIME with GNU General Public License v2.0 4 votes vote down vote up
public LocalService read(Class<?> clazz, ServiceId id, ServiceType type,
                         boolean supportsQueryStateVariables, Class[] stringConvertibleTypes) throws LocalServiceBindingException {
    return read(clazz, id, type, supportsQueryStateVariables, new HashSet<Class>(Arrays.asList(stringConvertibleTypes)));
}
 
Example #26
Source File: AnnotationLocalServiceBinder.java    From DroidDLNA with GNU General Public License v3.0 4 votes vote down vote up
public LocalService read(Class<?> clazz, ServiceId id, ServiceType type,
                         boolean supportsQueryStateVariables, Class[] stringConvertibleTypes) throws LocalServiceBindingException {
    return read(clazz, id, type, supportsQueryStateVariables, new HashSet<Class>(Arrays.asList(stringConvertibleTypes)));
}
 
Example #27
Source File: AnnotationActionBinder.java    From DroidDLNA with GNU General Public License v3.0 4 votes vote down vote up
protected Map<ActionArgument<LocalService>, StateVariableAccessor> createOutputArguments() throws LocalServiceBindingException {

        Map<ActionArgument<LocalService>, StateVariableAccessor> map = new LinkedHashMap(); // !!! Insertion order!

        UpnpAction actionAnnotation = getMethod().getAnnotation(UpnpAction.class);
        if (actionAnnotation.out().length == 0) return map;

        boolean hasMultipleOutputArguments = actionAnnotation.out().length > 1;

        for (UpnpOutputArgument outputArgumentAnnotation : actionAnnotation.out()) {

            String argumentName = outputArgumentAnnotation.name();

            StateVariable stateVariable = findRelatedStateVariable(
                    outputArgumentAnnotation.stateVariable(),
                    argumentName,
                    getMethod().getName()
            );

            // Might-just-work attempt, try the name of the getter
            if (stateVariable == null && outputArgumentAnnotation.getterName().length() > 0) {
                stateVariable = findRelatedStateVariable(null, null, outputArgumentAnnotation.getterName());
            }

            if (stateVariable == null) {
                throw new LocalServiceBindingException(
                        "Related state variable not found for output argument: " + argumentName
                );
            }

            StateVariableAccessor accessor = findOutputArgumentAccessor(
                    stateVariable,
                    outputArgumentAnnotation.getterName(),
                    hasMultipleOutputArguments
            );

            log.finer("Found related state variable for output argument '" + argumentName + "': " + stateVariable);

            ActionArgument outputArgument = new ActionArgument(
                    argumentName,
                    stateVariable.getName(),
                    ActionArgument.Direction.OUT,
                    !hasMultipleOutputArguments
            );

            map.put(outputArgument, accessor);
        }

        return map;
    }
 
Example #28
Source File: AnnotationActionBinder.java    From DroidDLNA with GNU General Public License v3.0 4 votes vote down vote up
protected List<ActionArgument> createInputArguments() throws LocalServiceBindingException {

        List<ActionArgument> list = new ArrayList();

        // Input arguments are always method parameters
        int annotatedParams = 0;
        Annotation[][] params = getMethod().getParameterAnnotations();
        for (int i = 0; i < params.length; i++) {
            Annotation[] param = params[i];
            for (Annotation paramAnnotation : param) {
                if (paramAnnotation instanceof UpnpInputArgument) {
                    UpnpInputArgument inputArgumentAnnotation = (UpnpInputArgument) paramAnnotation;
                    annotatedParams++;

                    String argumentName =
                            inputArgumentAnnotation.name();

                    StateVariable stateVariable =
                            findRelatedStateVariable(
                                    inputArgumentAnnotation.stateVariable(),
                                    argumentName,
                                    getMethod().getName()
                            );

                    if (stateVariable == null) {
                        throw new LocalServiceBindingException(
                                "Could not detected related state variable of argument: " + argumentName
                        );
                    }

                    validateType(stateVariable, getMethod().getParameterTypes()[i]);

                    ActionArgument inputArgument = new ActionArgument(
                            argumentName,
                            inputArgumentAnnotation.aliases(),
                            stateVariable.getName(),
                            ActionArgument.Direction.IN
                    );

                    list.add(inputArgument);
                }
            }
        }
        // A method can't have any parameters that are not annotated with @UpnpInputArgument - we wouldn't know what
        // value to pass when we invoke it later on... unless the last parameter is of type RemoteClientInfo
        if (annotatedParams < getMethod().getParameterTypes().length
            && !RemoteClientInfo.class.isAssignableFrom(method.getParameterTypes()[method.getParameterTypes().length-1])) {
            throw new LocalServiceBindingException("Method has parameters that are not input arguments: " + getMethod().getName());
        }

        return list;
    }
 
Example #29
Source File: AnnotationStateVariableBinder.java    From TVRemoteIME with GNU General Public License v2.0 4 votes vote down vote up
protected Datatype createDatatype() throws LocalServiceBindingException {

        String declaredDatatype = getAnnotation().datatype();

        if (declaredDatatype.length() == 0 && getAccessor() != null) {
            Class returnType = getAccessor().getReturnType();
            log.finer("Using accessor return type as state variable type: " + returnType);

            if (ModelUtil.isStringConvertibleType(getStringConvertibleTypes(), returnType)) {
                // Enums and toString() convertible types are always state variables with type STRING
                log.finer("Return type is string-convertible, using string datatype");
                return Datatype.Default.STRING.getBuiltinType().getDatatype();
            } else {
                Datatype.Default defaultDatatype = Datatype.Default.getByJavaType(returnType);
                if (defaultDatatype != null) {
                    log.finer("Return type has default UPnP datatype: " + defaultDatatype);
                    return defaultDatatype.getBuiltinType().getDatatype();
                }
            }
        }

        // We can also guess that if the allowed values are set then it's a string
        if ((declaredDatatype == null || declaredDatatype.length() == 0) &&
                (getAnnotation().allowedValues().length > 0 || getAnnotation().allowedValuesEnum() != void.class)) {
            log.finer("State variable has restricted allowed values, hence using 'string' datatype");
            declaredDatatype = "string";
        }

        // If we still don't have it, there is nothing more we can do
        if (declaredDatatype == null || declaredDatatype.length() == 0) {
            throw new LocalServiceBindingException("Could not detect datatype of state variable: " + getName());
        }

        log.finer("Trying to find built-in UPnP datatype for detected name: " + declaredDatatype);

        // Now try to find the actual UPnP datatype by mapping the Default to Builtin
        Datatype.Builtin builtin = Datatype.Builtin.getByDescriptorName(declaredDatatype);
        if (builtin != null) {
            log.finer("Found built-in UPnP datatype: " + builtin);
            return builtin.getDatatype();
        } else {
            // TODO
            throw new LocalServiceBindingException("No built-in UPnP datatype found, using CustomDataType (TODO: NOT IMPLEMENTED)");
        }
    }