How to mole SharePointServiceLocator to work with the logger - moles

I trying to figure out how to mole the
Microsoft.Practices.SharePoint.Common.ServiceLocation.SharePointServiceLocator.GetCurrentFarmLocator
because I trying to use
Microsoft.Practices.SharePoint.Common.Logging.SharePointLogger.WriteToDeveloperTrace
some help or example?

I found a solution by myself using the SharePointServiceLocator.
Basically I implemented this on my code:
var replaceLocator = new ActivatingServiceLocator ( );
SharePointServiceLocator.ReplaceCurrentServiceLocator ( replaceLocator );
replaceLocator.RegisterTypeMapping < ILogger, MockLogger > ( InstantiationType.AsSingleton );
replaceLocator.RegisterTypeMapping < ITraceLogger, MockLogger > ( InstantiationType.AsSingleton );
replaceLocator.RegisterTypeMapping < IEventLogLogger, MockLogger > ( InstantiationType.AsSingleton );

Related

Is there any way to take control over Fluentbit open descriptors?

i am trying to fix the next issue:
A lot of file descriptors opened by fluent-bit are kept always opened, even after calling flb_destroy().
Scenario:
I am developing a C++ application that wraps fluent-bit as library, stopping and starting the fluent-bit engine upon received events.
Problem:
Fluent-bit runs out of available file descriptors some time later, after restarting the engine several times without stopping the C++ app, ending with the error trace "lib backend failed".
Details:
I extracted a couple of methods available in the fluent-bit.c source file of the project as a helper on loading the configuration for runtime, by calling the next one:
flb_service_conf("path/to/fluent-bit.conf")
Every time an event is received, flb_stop() and flb_destroy() are called first, doing the same with flb_service_conf() and flb_start() inmediately after.
I got stucked at this point and am still researching about how could fix the issue.
Any clue, idea or suggestion would be very appreciated.
Here is a bit of code, hoping it helps to explain:
bool FluentBit::start( ) {
if ( isStopped() ) {
LOG_DEBUG( ) << "Starting fluent-bit";
m_ctx = ::flb_create( );
if( m_ctx == nullptr ) {
throw std::bad_alloc( );
}
running = configureEngine() && startEngine();
}
return running;
}
void FluentBit::stop( ) {
if ( isRunning() ) {
LOG_DEBUG( ) << "Stopping fluent-bit";
::flb_stop( m_ctx );
while( m_ctx->status == FLB_LIB_OK ) {
std::this_thread::sleep_for( std::chrono::seconds( 1 ) );
}
::flb_destroy( m_ctx );
running = false;
}
}
bool FluentBit::isRunning( ) {
return running;
}
bool FluentBit::isStopped( ) {
return ( !running ) ;
}
bool FluentBit::configureEngine( ) {
auto returnConf = ::flb_service_conf( m_ctx->config, settings.filePath() );
if ( returnConf < 0 ) {
LOG_ERROR() << "Failed loading fluent-bit configuration file: " << settings.filePath();
::flb_destroy( m_ctx );
}
return ( returnConf < 0 ) ? false : true;
}
bool FluentBit::startEngine( ) {
LOG_DEBUG( ) << "Starting fluent-bit engine";
auto retStart = ::flb_start( m_ctx );
if( retStart < 0 ) {
LOG_ERROR( ) << "Could not start fluent-bit engine";
::flb_destroy( m_ctx );
}
running = ( retStart < 0 ) ? false : true;
return running;
}
Thanks in advance for your (possible) replies :)
Kind Regards,
Pablo.

Salesforce Trigger - Prevent Parent Case Closure if Parent Case has open Child Cases

I have the following Apex trigger which should prevent a Parent Case from closing if the parent case has open child cases. Kindly assist with trouble shooting as the Apex Trigger is not firing.
trigger CaseTriggerCloseChild on Case ( before update ) {
Set < Id > setCaseIds = new Set < Id >();
Map < Id, Integer > mapOpenCaseCount = new Map < Id, Integer >();
for ( Case objCase : trigger.new ) {
if ( objCase.Status == 'Closed' ) {
setCaseIds.add( objCase.Id );
}
}
for ( Case objCase : [ SELECT ParentId FROM Case WHERE ParentId IN: setCaseIds AND IsClosed = false ] ) {
if ( mapOpenCaseCount.containsKey( objCase.ParentId ) ) {
mapOpenCaseCount.put( objCase.ParentId, mapOpenCaseCount.get( objCase.ParentId ) + 1 );
} else {
mapOpenCaseCount.put( objCase.ParentId, 1 );
}
}
for ( Case objCase : trigger.new ) {
if ( objCase.Status == 'Closed' ) {
if ( mapOpenCaseCount.containsKey( objCase.Id ) ) {
objCase.addError( 'You cannot close this Case. It has ' + mapOpenCaseCount.get( objCase.Id ) + ' open Child Cases.' );
}
}
}
}
Looks like your map of open cases is keyed on the parent ID and you are using the current case ID to look for open cases. You'll need to restructure it some.

Mbedtls entropy generation runs forever

I'm trying to write an test function for mbedtls which randomly generates a key for AES encryption.
I use the original tutorial Code from mbedtls.
My Programm always stops when executing "mbedtls_ctr_drbg_seed()".
About my environmet: Basic Sourcefiles from STM_CUBEmx, Board: ST32F767 Nucleo, Compiling based on Makefile from STM_Cube
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_entropy_context entropy;
char *pers="anything";
int ret;
//Start
mbedtls_entropy_init(&entropy);
debugPrintln("Init entropy done");
mbedtls_ctr_drbg_init(&ctr_drbg);
debugPrintln("Init ctr_drbg done");
if((ret=mbedtls_ctr_drbg_seed(&ctr_drbg,mbedtls_entropy_func,&entropy,(unsigned char *) pers,strlen(pers)))!=0){
//Error info
debugPrintln("ERROR ctr_drbg_seed ");
return -1;
}
debugPrintln("Init ctr_drbg_seed done");
if((ret=mbedtls_ctr_drbg_random(&ctr_drbg,key,32))!=0){
return -1;
}
Thank you in advance
From your description,
I am assuming that your application is stuck in the call to mbedtls_ctr_drbg_seed().
Most probable reason, IMHO, is in the functionmbedtls_entropy_func():
do
{
if( count++ > ENTROPY_MAX_LOOP )
{
ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
goto exit;
}
if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
goto exit;
done = 1;
for( i = 0; i < ctx->source_count; i++ )
if( ctx->source[i].size < ctx->source[i].threshold )
done = 0;
}
while( ! done );
You should check that your entropy collection increases the collected size, that the threshold is not MAX_INT or something of the sort, and that your hw entropy collector actually returns entropy data.
I have found the reason
STM32 Cube MX places the HAL Init function for the RNG after the mbedtls init
So when I call mbedtls_ctr_drbg_seed() inside mbedtls_init() the RNG hasn't yet been initialized and it iterates forever inside:
do
{
if( count++ > ENTROPY_MAX_LOOP )
{
ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
goto exit;
}
if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
goto exit;
done = 1;
for( i = 0; i < ctx->source_count; i++ )
if( ctx->source[i].size < ctx->source[i].threshold )
done = 0;
}
while( ! done );
Solution
swap the lines

Perl Net::SSH::Any with Key Exchange

I need to be able to use Perl to determine the version of python running on remote servers which I have key exchanges with. To try to accomplish this, I've been using Net::SSH::Any but want to use a key exchange instead of specifying the password in the script. I've looked at cpan's example which specifies the password.
The following is a snippet from my Perl code, which I'm trying to determine if python 2.6 is installed on the remote server.
my $ssh = Net::SSH::Any->new( $curIP, 'root' );
my #out = $ssh->capture("python -V");
my $outSize = #out;
my $ver26 = 0;
for ( my $j = 0; $j < $outSize; $j++ ) {
if ( index( $out[$j], "2.6." ) != -1 ) {
$ver26 = 1;
}
}
When I run this, it complains and says a host needs to be specified:
mandatory parameter host missing at ./GenerateConfig.pl line 162
I modified the code to look similar to how this is done in a different post (How can I FTP a file with an SSH key instead of a password, in Perl?) but it also fails:
my $ssh = Net::SSH::Any->new( $curIP, {'root'} );
my #out = $ssh->capture("python -V");
my $outSize = #out;
my $ver26 = 0;
for ( my $j = 0; $j < $outSize; $j++ ) {
if ( index( $out[$j], "2.6." ) != -1 ) {
$ver26 = 1;
}
}
How can I do this, or is there an alternative to accomplish the same thing?
According to the documentation for Net::SSH::Any, you need to pass it the hostname and pairs of options:
my $ssh = Net::SSH::Any->new($curIP, user => 'root');

Which Perl module should I use to generate a validating CRUD webform?

Has anyone successfully used something like DBIx::Class::WebForm or CatalystX-CRUD to automagically build a self-validating webform from a database table?
I'm imagining a module that reads a database table schema, reads the constraints for each column, and generates some abstract representation of a webform, with fields for error messages, etc. I'm using Catalyst and Plack with a big existing codebase.
I don't want to code up an HTML webform, nor any validation logic. I'm aiming to write as little code as possible, in the style of Ruby on Rails. Which Perl module is best for this?
UPDATE: I've solved the webform side with HTML::FormFu, but it's still clunky mapping the form inputs onto the database, e.g. date_start and date_end both relate to the 'created' column, and comment should match using 'LIKE %foo%', etc. Where's the 'DBICFu'?
UPDATE: This is for a web application, the webform should not look like a database table. I'm not looking for a database management tool.
You can use use HTML::FormHandler::Moose and HTML::FormHandler::Model::DBIC and get some nice forms.
As a simple example:
The form definition:
package MyStats::Form::Datetime ;
use HTML::FormHandler::Moose ;
extends 'HTML::FormHandler::Model::DBIC' ;
use Date::Calc qw(Today_and_Now) ;
has_field 'datetimeid' => ( label => 'ID' ) ;
has_field 'datetime' => ( type => 'Text',
apply => [ { transform => \&transform_dt } ] ,
deflation => \&deflation_dt ,
required => 1 ) ;
has_field 'submit' => ( type => 'Submit' ,
value => 'Speichern' ) ;
# These are the fields of the table datetime
sub transform_dt {
my ( $dt ) = #_ ;
my #d = ( $dt =~ m/(\d{1,2})\.(\d{1,2})\.(\d{4})\s+(\d{1,2}):(\d{1,2})/ ) ;
return sprintf( '%04d-%02d-%02d %02d:%02d:00' , #d[2,1,0,3,4] ) ;
}
sub deflation_dt {
my ( $dt ) = #_ ;
my #d = ( $dt =~ m/(\d{4})-(\d{2})-(\d{2})\s+(\d{1,2}):(\d{1,2})/ ) ;
if( ! #d ) {
#d = Today_and_Now() ;
}
return sprintf( '%02d.%02d.%04d %02d:%02d:00' , #d[2,1,0,3,4] ) ;
}
1 ;
And the usage in a controller:
package MyStats::Controller::Datetime ;
use Moose ;
use namespace::autoclean ;
BEGIN { extends 'Catalyst::Controller' ; }
use MyStats::Form::Datetime ;
has 'form' => ( isa => 'MyStats::Form::Datetime' ,
is => 'rw' ,
lazy => 1 ,
default => \&new_datetime_form ) ;
sub new_datetime_form {
MyStats::Form::Datetime->new( css_class => 'datetimeform' ,
name => 'datetimeform' ) ;
}
...
sub add :Local :Args(0) {
my ( $self , $ctx ) = #_ ;
my $data = $ctx->model( 'MyStatsDB::Datetime' )->new_result( {} ) ;
$ctx->stash( template => 'datetime/add.tt2' ,
form => $self->form ) ;
$ctx->bread_crumb( { name => 'Datum/Zeit eingeben' ,
location => '/datetime/add' } ) ;
$ctx->req->param( 'datetimeid' , undef ) if $ctx->req->param( 'datetimeid' ) ;
return unless $self->form->process( item => $data ,
params => $ctx->req->params ) ;
$ctx->flash( message => 'Neuer Datensatz ' . $data->datetimeid .
' angelegt.' ,
id_add => $data->datetimeid ) ;
$ctx->res->redirect( $ctx->uri_for( '/datetime' ) ) ;
}
...
__PACKAGE__->meta->make_immutable ;
1 ;
Works good.
I've used HTML::FormHandler to generate forms for me in this fashion. It needs some tweaking, but it does 90% of the work for you. Separately DBIx::Class offers a similar tool.
There are a number of crud options on the Catalyst wiki.
It sounds like AutoCrud would fit your needs.