<commit_before>package cfwrapper

import (
	"github.com/venicegeo/pzsvc-exec/pzsvc"
)

type Factory interface {
	GetSession() (CFSession, error)
	RefreshCachedClient() error
}

// DefaultFactory encapsulates functionality to lazily generate CFSession objects
type DefaultFactory struct {
	pzSession     *pzsvc.Session
	config        *FactoryConfig
	cachedSession CFSession
	createSession createSessionFunc
}

// NewFactory creates a new factory object for lazily creating cfclient.Client objects
func NewFactory(pzSession *pzsvc.Session, config *FactoryConfig) *DefaultFactory {
	return &DefaultFactory{
		pzSession:     pzSession,
		config:        config,
		createSession: config.createSessionFunc,
	}
}

// GetSession returns a lazily generated CFSession, verified for validity
func (f *DefaultFactory) GetSession() (CFSession, error) {
	refreshSession := false

	if f.cachedSession == nil {
		refreshSession = true
	} else if valid, err := f.cachedSession.IsValid(); err != nil {
		return nil, err
	} else if !valid {
		refreshSession = true
	}

	if refreshSession {
		err := f.RefreshCachedClient()
		if err != nil {
			return nil, err
		}
	}

	return f.cachedSession, nil
}

// RefreshCachedClient replaces the cached client with a new one based on the
// factory's stored cfclient.Config and expiration duration
func (f *DefaultFactory) RefreshCachedClient() error {
	pzsvc.LogInfo(*f.pzSession, "Regenerating Cloud Foundry Client.")
<commit_msg>Debug print for nil creation function<commit_after>package cfwrapper

import (
	"fmt"

	"github.com/venicegeo/pzsvc-exec/pzsvc"
)

type Factory interface {
	GetSession() (CFSession, error)
	RefreshCachedClient() error
}

// DefaultFactory encapsulates functionality to lazily generate CFSession objects
type DefaultFactory struct {
	pzSession     *pzsvc.Session
	config        *FactoryConfig
	cachedSession CFSession
	createSession createSessionFunc
}

// NewFactory creates a new factory object for lazily creating cfclient.Client objects
func NewFactory(pzSession *pzsvc.Session, config *FactoryConfig) *DefaultFactory {
	return &DefaultFactory{
		pzSession:     pzSession,
		config:        config,
		createSession: config.createSessionFunc,
	}
}

// GetSession returns a lazily generated CFSession, verified for validity
func (f *DefaultFactory) GetSession() (CFSession, error) {
	refreshSession := false

	if f.cachedSession == nil {
		refreshSession = true
	} else if valid, err := f.cachedSession.IsValid(); err != nil {
		return nil, err
	} else if !valid {
		refreshSession = true
	}

	if refreshSession {
		err := f.RefreshCachedClient()
		if err != nil {
			return nil, err
		}
	}

	return f.cachedSession, nil
}

// RefreshCachedClient replaces the cached client with a new one based on the
// factory's stored cfclient.Config and expiration duration
func (f *DefaultFactory) RefreshCachedClient() error {
	pzsvc.LogInfo(*f.pzSession, fmt.Sprintf("Regenerating Cloud Foundry Client with create function: %v", f.createSession))

{'fraction_non_alphanumeric': 0.08494983277591973, 'fraction_numerical': 0.0, 'mean_word_length': 4.448087431693989, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1661905', 'n_tokens_mistral': 928, 'n_tokens_neox': 892, 'n_words': 279}