class CustomDataSourceContextStrategy implements
        ContextManagementStrategy<ClientCustomDataSource, ClientCustomDataSource>,
        GenericTypeMetadataBuilder<ClientCustomDataSource>,
        ContextParametrizedMetadataBuilder<ClientCustomDataSource, PartialMetadataOptions> {

    @Override
    public ClientCustomDataSource createContext(ClientCustomDataSource contextDescription, Map<String, Object> data) throws IllegalParameterValueException {
        boolean passed = false;
        Exception exception = null;

        try {
            final CustomReportDataSource reportDataSource = toServer(contextDescription);
            ReportDataSourceService service = customDataSourceFactory.createService(reportDataSource);

            if (service instanceof ConnectionTestingDataSourceService) {
                passed = ((ConnectionTestingDataSourceService) service).testConnection();
            } else {
                passed = true;
            }

            if (service instanceof ReportDataAdapterService) {
                // try to generate metadata to validate if file is accessible and valid
                engine.getMetaDataFromConnector(reportDataSource);
            }
        } catch (Exception e) {
            exception = e;
        }
        if (!passed || exception != null) {
            if (exception instanceof ErrorDescriptorException) {
                throw (ErrorDescriptorException) exception;
            } else {
                throw new ContextCreationFailedException(contextDescription, null, "Connection failed", exception, secureExceptionHandler);
            }
        }
        return contextDescription;
    }

}