public Object getValue() { if (value == null) { // lets convert the text into the correct type value = TypeConverters.stringToType(text, type); } return value; }
private Object getAttribute( final ResourceAndRegistration reg, final PathAddress address, final ObjectName name, final String attribute, final ResourceAccessControl accessControl) throws ReflectionException, AttributeNotFoundException, InstanceNotFoundException { final ImmutableManagementResourceRegistration registration = getMBeanRegistration(address, reg); final DescriptionProvider provider = registration.getModelDescription(PathAddress.EMPTY_ADDRESS); if (provider == null) { throw MESSAGES.descriptionProviderNotFound(address); } final ModelNode description = provider.getModelDescription(null); final String attributeName = findAttributeName(description.get(ATTRIBUTES), attribute); if (!accessControl.isReadableAttribute(attributeName)) { throw MESSAGES.notAuthorizedToReadAttribute(attributeName); } ModelNode op = new ModelNode(); op.get(OP).set(READ_ATTRIBUTE_OPERATION); op.get(OP_ADDR).set(address.toModelNode()); op.get(NAME).set(attributeName); ModelNode result = execute(op); String error = getFailureDescription(result); if (error != null) { throw new AttributeNotFoundException(error); } return converters.fromModelNode( description.require(ATTRIBUTES).require(attributeName), result.get(RESULT)); }
private OpenMBeanParameterInfo[] getParameterInfos(ModelNode opNode) { if (!opNode.hasDefined(REQUEST_PROPERTIES)) { return EMPTY_PARAMETERS; } List<OpenMBeanParameterInfo> params = new ArrayList<OpenMBeanParameterInfo>(); for (Property prop : opNode.get(REQUEST_PROPERTIES).asPropertyList()) { ModelNode value = prop.getValue(); final String paramName = NameConverter.convertToCamelCase(prop.getName()); Map<String, String> descriptions = new HashMap<String, String>(); descriptions.put( DESC_EXPRESSIONS_ALLOWED, String.valueOf( prop.getValue().hasDefined(EXPRESSIONS_ALLOWED) && prop.getValue().get(EXPRESSIONS_ALLOWED).asBoolean())); params.add( new OpenMBeanParameterInfoSupport( paramName, getDescription(prop.getValue()), converters.convertToMBeanType(value), new ImmutableDescriptor(descriptions))); } return params.toArray(new OpenMBeanParameterInfo[params.size()]); }
@Test public void testProperPropertyValueTypeExpressionConverter() throws Exception { ModelNode description = createDescription(ModelType.PROPERTY, ModelType.INT); TypeConverter converter = TypeConverters.createLegacyTypeConverters(true).getConverter(description); CompositeType type = assertCast(CompositeType.class, converter.getOpenType()); Set<String> keys = type.keySet(); Assert.assertEquals(2, keys.size()); assertCompositeType(type, "name", String.class.getName(), "The property name"); assertCompositeType(type, "value", Integer.class.getName(), "The property value"); CompositeData data = assertCast( CompositeData.class, converter.fromModelNode( new ModelNode().setExpression("one", "${this.should.not.exist.!!!!!:1}"))); Assert.assertEquals(type, data.getCompositeType()); Assert.assertEquals("one", data.get("name")); Assert.assertEquals(1, data.get("value")); data = new CompositeDataSupport( type, new String[] {"name", "value"}, new Object[] {"two", Integer.valueOf(2)}); ModelNode newNode = converter.toModelNode(data); Assert.assertEquals(ModelType.PROPERTY, newNode.getType()); Assert.assertEquals(new ModelNode().set("two", 2), newNode); assertToArray(converter, data); }
@Test public void testProperPropertyTypeConverter() throws Exception { ModelNode description = createDescription(ModelType.PROPERTY); TypeConverter converter = TypeConverters.createLegacyTypeConverters(true).getConverter(description); CompositeType type = assertCast(CompositeType.class, converter.getOpenType()); Set<String> keys = type.keySet(); Assert.assertEquals(2, keys.size()); assertCompositeType(type, "name", String.class.getName(), "The property name"); assertCompositeType(type, "value", String.class.getName(), "The property value"); CompositeData data = assertCast(CompositeData.class, converter.fromModelNode(new ModelNode().set("one", "uno"))); Assert.assertEquals(type, data.getCompositeType()); Assert.assertEquals("one", data.get("name")); Assert.assertEquals("uno", data.get("value")); data = new CompositeDataSupport(type, new String[] {"name", "value"}, new String[] {"two", "dos"}); ModelNode newNode = converter.toModelNode(data); Assert.assertEquals(ModelType.PROPERTY, newNode.getType()); Assert.assertEquals(new ModelNode().set("two", "dos"), newNode); assertToArray(converter, data); }
private OpenType<?> getReturnType(ModelNode opNode) { if (!opNode.hasDefined(REPLY_PROPERTIES)) { return SimpleType.VOID; } if (opNode.get(REPLY_PROPERTIES).asList().size() == 0) { return SimpleType.VOID; } // TODO might have more than one REPLY_PROPERTIES? ModelNode reply = opNode.get(REPLY_PROPERTIES); return converters.convertToMBeanType(reply); }
@Test public void testConvert() throws Exception { final Object actual = TypeConverters.convert(value, clazz, defaultValue); final String assertionMessage = "\nGiven: " + value + "\nDefault: " + defaultValue; if (expected != null && expected instanceof char[]) { assertArrayEquals(assertionMessage, (char[]) expected, (char[]) actual); } else if (expected != null && expected instanceof byte[]) { assertArrayEquals(assertionMessage, (byte[]) expected, (byte[]) actual); } else { assertEquals(assertionMessage, expected, actual); } }
private void setAttribute( final ResourceAndRegistration reg, final PathAddress address, final ObjectName name, final Attribute attribute, ResourceAccessControl accessControl) throws InvalidAttributeValueException, AttributeNotFoundException, InstanceNotFoundException { final ImmutableManagementResourceRegistration registration = getMBeanRegistration(address, reg); final DescriptionProvider provider = registration.getModelDescription(PathAddress.EMPTY_ADDRESS); if (provider == null) { throw MESSAGES.descriptionProviderNotFound(address); } final ModelNode description = provider.getModelDescription(null); final String attributeName = findAttributeName(description.get(ATTRIBUTES), attribute.getName()); if (!standalone) { throw MESSAGES.attributeNotWritable(attribute); } if (!accessControl.isWritableAttribute(attributeName)) { throw MESSAGES.notAuthorizedToWriteAttribute(attributeName); } ModelNode op = new ModelNode(); op.get(OP).set(WRITE_ATTRIBUTE_OPERATION); op.get(OP_ADDR).set(address.toModelNode()); op.get(NAME).set(attributeName); try { op.get(VALUE) .set( converters.toModelNode( description.require(ATTRIBUTES).require(attributeName), attribute.getValue())); } catch (ClassCastException e) { throw MESSAGES.invalidAttributeType(e, attribute.getName()); } ModelNode result = execute(op); String error = getFailureDescription(result); if (error != null) { // Since read-resource-description does not know the parameters of the operation, i.e. if a // vault expression is used or not, // check the error code // TODO add a separate authorize step where we check ourselves that the operation will pass // authorization? if (isVaultExpression(attribute.getValue()) && error.contains(AUTHORIZED_ERROR)) { throw MESSAGES.notAuthorizedToWriteAttribute(attributeName); } throw new InvalidAttributeValueException(error); } }
private OpenMBeanAttributeInfo getAttribute(String name) { final String escapedName = NameConverter.convertToCamelCase(name); ModelNode attribute = providedDescription.require(ATTRIBUTES).require(name); AttributeAccess access = resourceRegistration.getAttributeAccess(PathAddress.EMPTY_ADDRESS, name); final boolean writable; if (!standalone) { writable = false; } else { writable = access != null ? access.getAccessType() == AccessType.READ_WRITE : false; } return new OpenMBeanAttributeInfoSupport( escapedName, getDescription(attribute), converters.convertToMBeanType(attribute), true, writable, false, createAttributeDescriptor(attribute)); }
private Object invoke( final OperationEntry entry, final String operationName, PathAddress address, Object[] params) throws InstanceNotFoundException, MBeanException, ReflectionException { if (!standalone && !entry.getFlags().contains(OperationEntry.Flag.READ_ONLY)) { throw MESSAGES.noOperationCalled(operationName); } ResourceAccessControl accessControl; if (operationName.equals("add")) { accessControl = accessControlUtil.getResourceAccess(address, true); } else { ObjectName objectName = ObjectNameAddressUtil.createObjectName(operationName, address); accessControl = accessControlUtil.getResourceAccessWithInstanceNotFoundExceptionIfNotAccessible( objectName, address, true); } if (!accessControl.isExecutableOperation(operationName)) { throw MESSAGES.notAuthorizedToExecuteOperation(operationName); } final ModelNode description = entry.getDescriptionProvider().getModelDescription(null); ModelNode op = new ModelNode(); op.get(OP).set(operationName); op.get(OP_ADDR).set(address.toModelNode()); if (params.length > 0) { ModelNode requestProperties = description.require(REQUEST_PROPERTIES); Set<String> keys = requestProperties.keys(); if (keys.size() != params.length) { throw MESSAGES.differentLengths("params", "description"); } Iterator<String> it = requestProperties.keys().iterator(); for (int i = 0; i < params.length; i++) { String attributeName = it.next(); ModelNode paramDescription = requestProperties.get(attributeName); op.get(attributeName).set(converters.toModelNode(paramDescription, params[i])); } } ModelNode result = execute(op); String error = getFailureDescription(result); if (error != null) { if (error.contains(AUTHORIZED_ERROR)) { for (Object param : params) { // Since read-resource-description does not know the parameters of the operation, i.e. if // a vault expression is used or not, // check the error code // TODO add a separate authorize step where we check ourselves that the operation will // pass authorization? if (isVaultExpression(param)) { throw MESSAGES.notAuthorizedToExecuteOperation(operationName); } } } throw new ReflectionException(null, error); } if (!description.hasDefined(REPLY_PROPERTIES)) { return null; } // TODO we could have more than one reply property return converters.fromModelNode(description.get(REPLY_PROPERTIES), result.get(RESULT)); }
private TypeConverter getConverter(ModelNode description) { return TypeConverters.createLegacyTypeConverters(false).getConverter(description); }