Marshals the given bean into the given map.
@param bean the bean
@return the content builder
@throws StorageException when a storage problem occurs while storing a bean
17820
public static XContentBuilder marshall(ApiVersionBean bean) throws StorageException {
        try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
            ApiBean api = bean.getApi();
            OrganizationBean org = api.getOrganization();
            preMarshall(bean);
            builder
                .startObject()
                    .field("organizationId", org.getId())
                    .field("organizationName", org.getName())
                    .field("apiId", api.getId())
                    .field("apiName", api.getName())
                    .field("apiDescription", api.getDescription())
                    .field("version", bean.getVersion())
                    .field("status", bean.getStatus())
                    .field("createdBy", bean.getCreatedBy())
                    .field("createdOn", bean.getCreatedOn().getTime())
                    .field("modifiedBy", bean.getModifiedBy())
                    .field("modifiedOn", bean.getModifiedOn().getTime())
                    .field("publishedOn", bean.getPublishedOn() != null ? bean.getPublishedOn().getTime() : null)
                    .field("retiredOn", bean.getRetiredOn() != null ? bean.getRetiredOn().getTime() : null)
                    .field("publicAPI", bean.isPublicAPI())
                    .field("endpoint", bean.getEndpoint())
                    .field("endpointType", bean.getEndpointType())
                    .field("endpointContentType", bean.getEndpointContentType())
                    .field("parsePayload", bean.isParsePayload())
                    .field("definitionType", bean.getDefinitionType());
            Set<ApiGatewayBean> gateways = bean.getGateways();
            if (gateways != null) {
                builder.startArray("gateways");
                for (ApiGatewayBean gateway : gateways) {
                    builder.startObject()
                        .field("gatewayId", gateway.getGatewayId())
                    .endObject();
                }
                builder.endArray();
            }
            Set<ApiPlanBean> plans = bean.getPlans();
            if (plans != null) {
                builder.startArray("plans");
                for (ApiPlanBean plan : plans) {
                    builder.startObject()
                        .field("planId", plan.getPlanId())
                        .field("version", plan.getVersion())
                    .endObject();
                }
                builder.endArray();
            }
            Map<String, String> endpointProperties = bean.getEndpointProperties();
            if (endpointProperties != null) {
                builder.startObject("endpointProperties");
                for (Entry<String, String> property : endpointProperties.entrySet()) {
                    builder.field(property.getKey(), property.getValue());
                }
                builder.endObject();
            }
            builder.endObject();
            postMarshall(bean);
            return builder;
        } catch (IOException e) {
            throw new StorageException(e);
        }
    }
669778
public static <T> CheckResult<T> checkBean(Object bean) {
        return checkBean(bean, new HashMap<>(ValueConsts.TWO_INT));
    }
1178121
public static BeanMapping create(String mappingName) {
        BeanMappingObject config = BeanMappingConfigHelper.getInstance().getBeanMappingObject(mappingName);
        if (config == null) {
            throw new BeanMappingException("can not found mapping config for name[" + mappingName + "]");
        }

        return new BeanMapping(config);
    }
856974
public static Map<String, Object> getBeanProperties(Object bean) {
        Map<String, Object> props = new HashMap<>();

        if (bean == null) {
            return props;
        }

        Class<?> clazz = bean.getClass();
        try {
            //Public fields:
            Field[] fields = clazz.getFields();
            for (Field field : fields) {
                String name = field.getName();
                if (!name.equals("class")) {
                    props.put(field.getName(), field.get(bean));
                }
            }

            //Getters:
            BeanInfo info = Introspector.getBeanInfo(clazz);
            for (PropertyDescriptor desc : info.getPropertyDescriptors()) {
                Method readMethod = desc.getReadMethod();
                String name = desc.getName();
                if (readMethod != null && !name.equals("class")) {
                    props.put(desc.getName(), readMethod.invoke(bean));
                }
            }

            return props;
        } catch (IntrospectionException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
            throw new UniformException("Error while getting bean object properties of class" + clazz.getName(), ex);
        }
    }
265104
void addBean(Bean bean) {
		if (bean == null) return;
		this.beans.add(new BeanDefinition(bean.name(), bean.clazz()));
	}
618801
private void populateMap(Object bean) {
		final Collection<PropDesc> props = BeanUtil.getBeanDesc(bean.getClass()).getProps();

		Method getter;
		Object value;
		for (PropDesc prop : props) {
			// 得到property对应的getter方法
			getter = prop.getGetter();
			if (null == getter) {
				// 无Getter跳过
				continue;
			}

			// 只读取有getter方法的属性
			try {
				value = getter.invoke(bean);
			} catch (Exception ignore) {
				// 忽略读取失败的属性
				continue;
			}

			if (ObjectUtil.isNull(value) && this.config.isIgnoreNullValue()) {
				// 值为null且用户定义跳过则跳过
				continue;
			}

			if (value != bean) {
				// 防止循环引用
				this.rawHashMap.put(prop.getFieldName(), JSONUtil.wrap(value, this.config.isIgnoreNullValue()));
			}
		}
	}
280574
@Override
    public void create(Collection<Bean> beans) throws AbortRuntimeException {
        init();
        Set<HBeanRow> beanRows = new HashSet<>();
        Map<HBeanRow, HBeanRow> refRows = new HashMap<>();
        Map<BeanId, Bean> beansMap = BeanUtils.uniqueIndex(beans);
        try {
            for (Bean bean : beans) {
                beanRows.add(new HBeanRow(bean, uids));
                for (BeanId ref : bean.getReferences()) {
                    Bean refBean = beansMap.get(ref);
                    HBeanRow row;
                    if (refBean == null) {
                        row = new HBeanRow(ref, uids);
                    } else {
                        row = new HBeanRow(refBean, uids);
                    }
                    refRows.put(row, row);
                }
            }
            table.getLazy(beanRows);
            throw Events.CFG303_BEAN_ALREADY_EXIST(beans.iterator().next().getId());
        } catch (HBeanNotFoundException e) {
            // ensure beans to be create does not exist
        }

        // ensure references exist either in memory or storage
        try {
            refRows = table.getLazyAsMap(refRows);
        } catch (HBeanNotFoundException e) {
            // only throw exception if the bean was not provided
            // as an argument to the create method.
            if (!idsFoundIn(beans, e)) {
                throw Events.CFG301_MISSING_RUNTIME_REF(e.getNotFound().iterator().next());
            }
            // make sure that references found in storage override the refRows
            for (HBeanRow row : e.getFound().keySet()) {
                refRows.put(row, row);
            }

        }
        // select the rows to update predecessors
        for (HBeanRow beanRow : beanRows) {
            for (HBeanRow refRow : beanRow.getReferenceRows()) {
                refRow = refRows.get(refRow);
                refRow.addPredecessor(beanRow);
            }
        }

        table.put(beanRows);
        table.put(Sets.newHashSet(refRows.values()));
    }
747179
public boolean destroy(Contextual bean)
    {
        ContextualStorage storage = getContextualStorage(false);
        if (storage == null)
        {
            return false;
        }
        ContextualInstanceInfo<?> contextualInstanceInfo = storage.getStorage().get(storage.getBeanKey(bean));

        if (contextualInstanceInfo == null)
        {
            return false;
        }

        bean.destroy(contextualInstanceInfo.getContextualInstance(), contextualInstanceInfo.getCreationalContext());

        return true;
    }
627866
@SuppressWarnings("unchecked")
	private Map<String, Object> safeSerialize(ObjectMapper mapper, Object bean,
			String prefix) {
		try {
			return new HashMap<>(mapper.convertValue(bean, Map.class));
		}
		catch (Exception ex) {
			return new HashMap<>(Collections.singletonMap("error",
					"Cannot serialize '" + prefix + "'"));
		}
	}
987106
private static Object fillBean(final OptionVisitor visitor, final Type type, final Object bean, final List<ParameterMetadata> metadatas) {
        if (!Class.class.isInstance(type)) { // can't do anything
            return null;
        }

        List<Object> constructorParam = bean == null ? new ArrayList<>() : null;
        try {
            final Constructor constructor = Class.class.cast(type).getConstructors()[0];
            final BeanInfo descriptor = Introspector.getBeanInfo(Class.class.cast(type));
            final Map<String, PropertyDescriptor> descriptorMap = new HashMap<>();
            for (final PropertyDescriptor d : descriptor.getPropertyDescriptors()) {
                descriptorMap.put(d.getName(), d);
            }
            Object value = bean;
            int consIdx = -1;
            for (final ParameterMetadata metadata : metadatas) {
                consIdx++;
                if (metadata.getType() != ParameterMetadata.ParamType.OPTION) { // we support only 1 level for now
                    continue;
                }


                final String name = metadata.getName().substring(metadata.getName().lastIndexOf('.') + 1);
                if (!descriptorMap.containsKey(name)) {
                    continue;
                }

                final PropertyDescriptor propertyDescriptor = descriptorMap.get(name);
                if (propertyDescriptor == null) {
                    continue;
                }

                if (bean != null) { // check it is not already set
                    final Method readMethod = propertyDescriptor.getReadMethod();
                    if (readMethod == null) {
                        continue;
                    }
                    try {
                        final Object val = readMethod.invoke(bean);
                        if (val != null) {
                            continue;
                        }
                    } catch (final IllegalAccessException e) {
                        throw new IllegalStateException(e);
                    } catch (InvocationTargetException e) {
                        throw new IllegalStateException(e.getCause());
                    }
                }

                final Method write = propertyDescriptor.getWriteMethod();
                if (write != null) {
                    final Object val = visitor.onOption(consIdx, metadata, new AnnotatedElementImpl(write.getParameterAnnotations()[0]));
                    if (val != null) {
                        try {
                            if (value == null) {
                                constructorParam.add(val);
                            } else {
                                write.invoke(value, val);
                            }
                        } catch (final IllegalAccessException e) {
                            throw new IllegalStateException(e);
                        } catch (final InvocationTargetException e) {
                            throw new IllegalStateException(e.getCause());
                        }
                    }

                }
            }
            if (value == null) {
                try {
                    value = constructor.newInstance(constructorParam.toArray(new Object[constructorParam.size()]));
                } catch (final IllegalAccessException e) {
                    throw new IllegalStateException(e);
                } catch (final InstantiationException e) {
                    throw new IllegalStateException(e);
                } catch (final InvocationTargetException e) {
                    throw new IllegalStateException(e.getCause());
                }
            }
            return value;
        } catch (final IntrospectionException e) {
            throw new IllegalStateException(e);
        }
    }
217581
public static XContentBuilder marshall(PluginBean bean) throws StorageException {
        try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
            preMarshall(bean);
            builder
                .startObject()
                    .field("id", bean.getId())
                    .field("name", bean.getName())
                    .field("description", bean.getDescription())
                    .field("createdBy", bean.getCreatedBy())
                    .field("createdOn", bean.getCreatedOn().getTime())
                    .field("groupId", bean.getGroupId())
                    .field("artifactId", bean.getArtifactId())
                    .field("version", bean.getVersion())
                    .field("classifier", bean.getClassifier())
                    .field("type", bean.getType())
                    .field("deleted", bean.isDeleted())
                .endObject();
            postMarshall(bean);
            return builder;
        } catch (IOException e) {
            throw new StorageException(e);
        }
    }
1236297
public static <T> T fillBeanWithMap(Map<?, ?> map, T bean, boolean isToCamelCase, boolean isIgnoreError) {
		return fillBeanWithMap(map, bean, isToCamelCase, CopyOptions.create().setIgnoreError(isIgnoreError));
	}
334534
@Override
	public void setBeanProvider(final BeanProvider beanProvider) {
		if (isLocked() && beanProvider != null && !(beanProvider instanceof Serializable)) {
			throw new SystemException(
					"Unable to store bean provider in user's session as it is not serializable: " + beanProvider.
					getClass());
		}

		getOrCreateComponentModel().setBeanProvider(beanProvider);
		String beanProperty = getBeanProperty();

		if (beanProperty == null) {
			setBeanProperty(".");
		}

		// Remove values in scratch map
		removeBeanFromScratchMap();
	}
984636
public <T> T toBeanWithCamelCase(T bean) {
		BeanUtil.fillBeanWithMap(this, bean, true, false);
		return bean;
	}
676995
protected BeanDefinitionBuilder getApiHelperBeanDefinitionBuilder(Map<String, Object> allAttributes)
	{
		return BeanDefinitionBuilder.genericBeanDefinition(apiHelperClass)
				.addConstructorArgReference("usersConnectionRepository")
				.addConstructorArgReference("userIdSource");
	}
1198928
protected ApiVersionBean createApiVersionInternal(NewApiVersionBean bean,
            ApiBean api, GatewaySummaryBean gateway) throws Exception, StorageException {
        if (!BeanUtils.isValidVersion(bean.getVersion())) {
            throw new StorageException("Invalid/illegal API version: " + bean.getVersion()); //$NON-NLS-1$
        }

        ApiVersionBean newVersion = new ApiVersionBean();
        newVersion.setVersion(bean.getVersion());
        newVersion.setCreatedBy(securityContext.getCurrentUser());
        newVersion.setCreatedOn(new Date());
        newVersion.setModifiedBy(securityContext.getCurrentUser());
        newVersion.setModifiedOn(new Date());
        newVersion.setStatus(ApiStatus.Created);
        newVersion.setApi(api);
        newVersion.setEndpoint(bean.getEndpoint());
        newVersion.setEndpointType(bean.getEndpointType());
        newVersion.setEndpointContentType(bean.getEndpointContentType());
        if (bean.getPublicAPI() != null) {
            newVersion.setPublicAPI(bean.getPublicAPI());
        }
        if (bean.getParsePayload() != null) {
            newVersion.setParsePayload(bean.getParsePayload());
        }
        if (bean.getPlans() != null) {
            newVersion.setPlans(bean.getPlans());
        }
        if (bean.getDefinitionType() != null) {
            newVersion.setDefinitionType(bean.getDefinitionType());
        }

        if (gateway != null && newVersion.getGateways() == null) {
            newVersion.setGateways(new HashSet<>());
            ApiGatewayBean sgb = new ApiGatewayBean();
            sgb.setGatewayId(gateway.getId());
            newVersion.getGateways().add(sgb);
        }

        if (apiValidator.isReady(newVersion)) {
            newVersion.setStatus(ApiStatus.Ready);
        } else {
            newVersion.setStatus(ApiStatus.Created);
        }

        // Ensure all of the plans are in the right status (locked)
        Set<ApiPlanBean> plans = newVersion.getPlans();
        if (plans != null) {
            for (ApiPlanBean splanBean : plans) {
                String orgId = newVersion.getApi().getOrganization().getId();
                PlanVersionBean pvb = storage.getPlanVersion(orgId, splanBean.getPlanId(), splanBean.getVersion());
                if (pvb == null) {
                    throw new StorageException(Messages.i18n.format("PlanVersionDoesNotExist", splanBean.getPlanId(), splanBean.getVersion())); //$NON-NLS-1$
                }
                if (pvb.getStatus() != PlanStatus.Locked) {
                    throw new StorageException(Messages.i18n.format("PlanNotLocked", splanBean.getPlanId(), splanBean.getVersion())); //$NON-NLS-1$
                }
            }
        }

        storage.createApiVersion(newVersion);
        storage.createAuditEntry(AuditUtils.apiVersionCreated(newVersion, securityContext));

        if (bean.getDefinitionUrl() != null) {
            InputStream definition = null;
            try {
                definition = new URL(bean.getDefinitionUrl()).openStream();
                storage.updateApiDefinition(newVersion, definition);
            } catch (Exception e) {
                log.error("Unable to store API definition from: " + bean.getDefinitionUrl(), e); //$NON-NLS-1$
            } finally {
                IOUtils.closeQuietly(definition);
            }
        }

        return newVersion;
    }
612184
public Map<String, Object> write(Bean bean) {
        JodaBeanUtils.notNull(bean, "bean");
        return writeBean(bean, bean.getClass());
    }