Complete Contents
Introduction
Chapter 1 About Netscape Application Server Extensions
Chapter 2 About the Netscape Extension Builder
Chapter 3 Introduction to Netscape's Interface Definition Language
Chapter 4 Designing a Netscape Extension
Chapter 5 Generating Output Files
Chapter 6 Completing Method Stubs
Chapter 7 Using Template Streaming
Chapter 8 Managing State and Session Information
Chapter 9 Using Object Pools
Chapter 10 Compiling the Extension Source Code
Chapter 11 Deploying and Managing a Netscape Extension
Chapter 12 Example Extension: HelloWorld
Appendix A C++ Helper Functions
Appendix B Java Helper Static Methods
Appendix C Java Class Decorations
Appendix D Reserved Words
Appendix E The ConnManager.cpp File
Glossary
Previous Next Contents Index


The ConnManager.cpp File

This appendix contains sample source code.

The following code sample is found in the file:

sample\poolsample\cpp\connService\ConnManager.cpp

This file is used by PoolSampleExt, the sample extension described in Using Object Pools.

//

// This file is initially generated by KIDL - Edit as

// necessary to complete

//

#include <stdio.h>

#include <gxplat.h>

#include <gxutil.h>

#include <gxdlm.h>

#include "connservice.h"

#include "gxiextdata.h"

#include "gxextutil.h"

LPSTR ConnManager::m_appNameLocal = "ConnManagerLocal";

LPSTR ConnManager::m_appNameDistributed = "ConnManagerDistributed";

LPSTR ConnManager::m_appNameCluster = "ConnManagerCluster";

/*

[uuid(55863a3d-bacd-1505-f0f3-0800208055c0),

wrapper_uuid(55873c6d-bacd-1505-f0f3-0800208055c0)]

*/

GUID ConnManagerGUID =

{ 0x55863a3d,0xbacd,0x1505,{0xf0,0xf3,0x08,0x00,0x20,0x80,0x55,0xc0} };

// The following must belong in one, and only one, C++ file

GXDLM_IMPLEMENT_BEGIN()

GXDLM_IMPLEMENT(ConnManager, ConnManagerGUID);

GXDLM_IMPLEMENT_END();

ConnManager::ConnManager()

{

GXDllLockInc();

m_nInits=0;

m_pContext=NULL;

//***

//*** Add your own code to the constructor here.

//*** Note: You can change this constructor or add new

//*** constructors

//***

GXSYNC_INIT(&(Connection::G_Sync));

}

ConnManager::~ConnManager()

{

if(m_pContext)

m_pContext->Release();

//***

//*** Add your own code to the destructor here.

//***

GXSYNC_DESTROY(&(Connection::G_Sync));

GXDllLockDec();

}

HRESULT

ConnManager::Init(IGXObject *pObj)

{

// If we haven't been initialized ...

if(!m_nInits) {

// If we have a context already, release it

if(m_pContext)

m_pContext->Release();

// Pull the context from the argument

m_pContext = GXGetContext(pObj);

if (m_pContext) {

// And set myself in the context, so other folks

// can see me

m_pContext->SetObject("IID_IConnManager", 0, 0, 0,

(IGXObject*)(IConnManager*)this, 0, 0, 0);

}

//***

//*** Add your own service initialization code here.

//*** This method (Init) gets called by the runtime

//*** when the service is initially loaded by the

//*** Extension Manager

//***

}

m_nInits++;

return GXE_SUCCESS;

}

HRESULT

ConnManager::Uninit(IGXObject *pObj)

{

if(m_nInits > 0)

m_nInits--;

return GXE_SUCCESS;

}

// Stub method bodies for interface: IConnManager

HRESULT

ConnManager::CreateConnection(

/* [in] */ unsigned long uid,

/* [in] */ LPSTR passwd,

/* [in] */ unsigned long flags,

/* [out] */ IFakeConnection **ppConn)

{

HRESULT hr=GXE_SUCCESS;

//***

//*** Provide your implementation here

//*** and remove the GXASSERT below

//***

*ppConn = new Connection(this, uid, passwd, flags);

return hr;

}

HRESULT

ConnManager::CreateConnectionIntrospection(

/* [in] */ unsigned long uid,

/* [in] */ LPSTR passwd,

/* [in] */ unsigned long flags,

/* [out] */ IInfo **ppConn)

{

HRESULT hr=GXE_SUCCESS;

//***

//*** Provide your implementation here

//*** and remove the GXASSERT below

//***

*ppConn = new Info(uid, passwd, flags);

return hr;

}

HRESULT

ConnManager::CreateTxConnection(

/* [in] */ unsigned long uid,

/* [in] */ LPSTR passwd,

/* [in] */ unsigned long flags,

/* [out] */ IFakeConnection **ppConn)

{

HRESULT hr=GXE_SUCCESS;

//***

//*** Provide your implementation here

//*** and remove the GXASSERT below

//***

*ppConn = new TxConnection(this, uid, passwd, flags);

return hr;

}

HRESULT

ConnManager::CreateTxConnectionIntrospection(

/* [in] */ unsigned long uid,

/* [in] */ LPSTR passwd,

/* [in] */ unsigned long flags,

/* [out] */ IInfo **ppConn)

{

HRESULT hr=GXE_SUCCESS;

//***

//*** Provide your implementation here

//*** and remove the GXASSERT below

//***

*ppConn = new Info(uid, passwd, flags);

return hr;

}

HRESULT

ConnManager::CreateNonPooledConnection(

/* [in] */ unsigned long uid,

/* [in] */ LPSTR passwd,

/* [in] */ unsigned long flags,

/* [out] */ IFakeConnection **ppConn)

{

HRESULT hr=GXE_SUCCESS;

//***

//*** Provide your implementation here

//*** and remove the GXASSERT below

//***

*ppConn = new Connection(this, uid, passwd, flags);

return hr;

}

// Stub method bodies for interface: IGXObjectEvaluation

HRESULT

ConnManager::MatchObject(

/* [in] */ LPSTR poolName,

/* [in] */ IGXObject *pVirtual,

/* [in] */ IGXObject *pPhysical,

/* [out] */ BOOL *pMatches)

{

HRESULT hr=GXE_SUCCESS;

//***

//*** Provide your implementation here

//*** and remove the GXASSERT below

//***

// Depending on poolName, determine if the objects match:

// Navigate to the introspection interface of the

// virtual & physical objects, get their attributes,

// and see if they match.

// CONN_POOL: match on userid & passwd

// TXCONN_POOL: match on userid, passwd AND flags

// (match criteria is stricter than CONN_POOL)

if (!strcmp(poolName, "CONN_POOL"))

{

*pMatches = FALSE;

IInfo* pV = NULL;

IInfo* pP = NULL;

// Navigate to IInfo interfaces

if((((hr=pVirtual->QueryInterface(IID_IInfo,

(LPVOID*)&pV))==GXE_SUCCESS)&&(pV))

&&(((hr=pPhysical->QueryInterface(IID_IInfo,

(LPVOID*)&pP))==GXE_SUCCESS)&&(pP)))

{

ULONG PUid = 0;

ULONG VUid = 0;

char PPasswd[256];

char VPasswd[256];

// Get uid, passwd for both virtual

// & physical objects

if(hr==GXE_SUCCESS)

hr=pV->GetUid(&VUid);

if(hr==GXE_SUCCESS)

hr=pV->GetPasswd(VPasswd, 256);

if(hr==GXE_SUCCESS)

hr=pP->GetUid(&PUid);

if(hr==GXE_SUCCESS)

hr=pP->GetPasswd(PPasswd, 256);

// Check if userid & password match

if ((hr==GXE_SUCCESS)

&& (PUid==VUid)

&& (!strcmp(PPasswd, VPasswd)))

*pMatches = TRUE;

}

// Release interfaces

if (pP)

pP->Release();

if (pV)

pV->Release();

}

else // TXCONN_POOL

{

*pMatches = FALSE;

IInfo* pV = NULL;

IInfo* pP = NULL;

// Navigate to IInfo interfaces

if((((hr=pVirtual->QueryInterface(IID_IInfo,

(LPVOID*)&pV))==GXE_SUCCESS)&&(pV))

&&(((hr=pPhysical->QueryInterface(IID_IInfo,

(LPVOID*)&pP))==GXE_SUCCESS)&&(pP)))

{

ULONG PUid = 0;

ULONG VUid = 0;

ULONG PFlags = 0;

ULONG VFlags = 0;

char PPasswd[256];

char VPasswd[256];

if(hr==GXE_SUCCESS)

hr=pV->GetUid(&VUid);

if(hr==GXE_SUCCESS)

hr=pV->GetPasswd(VPasswd, 256);

if(hr==GXE_SUCCESS)

hr=pP->GetUid(&PUid);

if(hr==GXE_SUCCESS)

hr=pP->GetPasswd(PPasswd, 256);

if(hr==GXE_SUCCESS)

hr=pP->GetFlags(&PFlags);

if(hr==GXE_SUCCESS)

hr=pV->GetFlags(&VFlags);

// Check if userid, password & flags match

if ((hr==GXE_SUCCESS)

&& (PUid==VUid)

&& (PFlags==VFlags)

&& (!strcmp(PPasswd, VPasswd)))

*pMatches = TRUE;

}

// Release interfaces

if (pP)

pP->Release();

if (pV)

pV->Release();

}

return hr;

}

HRESULT

ConnManager::StealObject(

/* [in] */ LPSTR poolName,

/* [in] */ IGXObject *pVirtual,

/* [in] */ IGXObject *pPhysical,

/* [out] */ BOOL *pCanSteal)

{

HRESULT hr=GXE_SUCCESS;

//***

//*** Provide your implementation here

//*** and remove the GXASSERT below

//***

// Allow any connection to be replaced.

*pCanSteal = TRUE;

return hr;

}

HRESULT

ConnManager::GetHint(

/* [in] */ LPSTR poolName,

/* [in] */ IGXObject *pVirtual,

/* [out] */ LPSTR hint,

/* [in] */ unsigned long Size)

{

HRESULT hr=GXE_SUCCESS;

//***

//*** Provide your implementation here

//*** and remove the GXASSERT below

//***

// Don't want to provide any hint.

strcpy(hint, "");

return hr;

}

HRESULT

ConnManager::CreateObject(

/* [in] */ LPSTR poolName,

/* [in] */ IGXObject *pVirtual,

/* [out] */ IGXPoolObject **ppPhysical)

{

HRESULT hr=GXE_SUCCESS;

//***

//*** Provide your implementation here

//*** and remove the GXASSERT below

//***

// Depending on the poolName, navigate to the

// introspection interface of the virtual object,

// get the encapsulated attributes,

// and create the appropriate physical object with the

// same attributes.

if (!strcmp(poolName, "CONN_POOL"))

{

IInfo* pV = NULL;

IFakeConnection* pConn = NULL;

ULONG uid = 0;

ULONG flags = 0;

char passwd[256];

// Navigate to IInfo interface

if(((hr=pVirtual->QueryInterface(IID_IInfo,

(LPVOID*)&pV))==GXE_SUCCESS)&&(pV))

{

// Get the uid, passwd, flags

if(hr==GXE_SUCCESS)

hr=pV->GetUid(&uid);

if(hr==GXE_SUCCESS)

hr=pV->GetPasswd(passwd, 256);

if(hr==GXE_SUCCESS)

hr=pV->GetFlags(&flags);

}

// Create a matching Connection object.

if (hr==GXE_SUCCESS)

{

hr = CreateConnection(uid, passwd, flags, &pConn);

// QueryInterface to IGXPoolObject

if((hr==GXE_SUCCESS)&& pConn)

hr=pConn->QueryInterface(IID_IGXPoolObject,

(LPVOID*)ppPhysical);

}

// Release interfaces

if (pV)

pV->Release();

if (pConn)

pConn->Release();

}

else // TXCONN_POOL

{

IInfo* pV = NULL;

IFakeConnection* pConn = NULL;

ULONG uid = 0;

ULONG flags = 0;

char passwd[256];

// Get to IInfo interface

if(((hr=pVirtual->QueryInterface(IID_IInfo,

(LPVOID*)&pV))==GXE_SUCCESS)&&(pV))

{

// Get uid, passwd, flags

if(hr==GXE_SUCCESS)

hr=pV->GetUid(&uid);

if(hr==GXE_SUCCESS)

hr=pV->GetPasswd(passwd, 256);

if(hr==GXE_SUCCESS)

hr=pV->GetFlags(&flags);

}

// Create a matching TxConnection object.

if (hr==GXE_SUCCESS)

{

hr = CreateTxConnection(uid, passwd, flags, &pConn);

// QueryInterface to IGXPoolObject

if((hr==GXE_SUCCESS)&& pConn)

hr=pConn->QueryInterface(IID_IGXPoolObject,

(LPVOID*)ppPhysical);

}

// Release interfaces

if (pV)

pV->Release();

if (pConn)

pConn->Release();

}

return hr;

}

HRESULT

ConnManager::InitObject(

/* [in] */ LPSTR poolName,

/* [in] */ IGXObject *pVirtual,

/* [in] */ IGXObject *pPhysical)

{

HRESULT hr=GXE_SUCCESS;

//***

//*** Provide your implementation here

//*** and remove the GXASSERT below

//***

// Remember: the match criteria for objects in

// CONN_POOL did not include flag settings.

// We therefore need to restore the flag settings

// of the physical object, to match those of the

// virtual object.

// For CONN_POOL objects, do flag settings here.

if (!strcmp(poolName, "CONN_POOL"))

{

// Get to IInfo interface

IInfo* pV = NULL;

if(((hr=pVirtual->QueryInterface(IID_IInfo,

(LPVOID*)&pV))==GXE_SUCCESS)&&(pV))

{

ULONG flags = 0;

hr=pV->GetFlags(&flags);

// Get Connection implementation

IFakeConnection* pIConn=NULL;

if(((hr=pPhysical->QueryInterface

(IID_IFakeConnection,(LPVOID*)&pIConn))

==GXE_SUCCESS)&&(pIConn))

{

// Set flags

pIConn->SetFlags(flags);

}

}

}

return hr;

}

HRESULT

ConnManager::UninitObject(

/* [in] */ LPSTR poolName,

/* [in] */ IGXObject *pPhysical)

{

HRESULT hr=GXE_SUCCESS;

//***

//*** Provide your implementation here

//*** and remove the GXASSERT below

//***

// Nothing to do.

return hr;

}

HRESULT

ConnManager::ReleaseObject(

/* [in] */ LPSTR poolName,

/* [in] */ IGXObject *pPhysical,

/* [in] */ unsigned long reason)

{

HRESULT hr=GXE_SUCCESS;

//***

//*** Provide your implementation here

//*** and remove the GXASSERT below

//***

// Would have closed any backend connection here.

// Nothing to do in this dummy implementation.

return hr;

}

// Stub method bodies for interface: IGXCreateIntrospection

HRESULT

ConnManager::CreateIntrospectObject(

/* [in] */ LPSTR methodname,

/* [in] */ LPSTR signature,

/* [in] */ LPVOID ParamStack)

{

if (!strcmp(methodname,"CreateConnection"))

{

if (strcmp(signature, "unsigned long.LPSTR.unsigned

long.IInfo*."))

return GXE_FAIL;

return CreateConnectionIntrospection(

*((unsigned long*)((LPVOID*)ParamStack)[0])

,*((LPSTR*)((LPVOID*)ParamStack)[1])

,*((unsigned long*)((LPVOID*)ParamStack)[2])

,((IInfo**)((LPVOID*)ParamStack)[3])

);

}

if (!strcmp(methodname,"CreateTxConnection"))

{

if (strcmp(signature, "unsigned long.LPSTR.unsigned

long.IInfo*."))

return GXE_FAIL;

return CreateTxConnectionIntrospection(

*((unsigned long*)((LPVOID*)ParamStack)[0])

,*((LPSTR*)((LPVOID*)ParamStack)[1])

,*((unsigned long*)((LPVOID*)ParamStack)[2])

,((IInfo**)((LPVOID*)ParamStack)[3])

);

}

return GXE_FAIL;

}

STDMETHODIMP

ConnManager::QueryInterface(REFIID riid, LPVOID *ppvObject)

{

if(!ppvObject)

return GXE_INVALID_ARG;

*ppvObject=NULL;

if(GXGUID_EQUAL(riid, IID_IUnknown))

*ppvObject=(LPVOID)(IUnknown*)(IConnManager*)this;

if(GXGUID_EQUAL(riid, IID_IGXObject))

*ppvObject=(LPVOID)(IGXObject*)(IConnManager*)this;

if(GXGUID_EQUAL(riid, IID_IGXModule))

*ppvObject=(LPVOID)(IGXModule*)this;

if(GXGUID_EQUAL(riid, IID_IConnManager))

*ppvObject=(LPVOID)(IConnManager*)this;

if(GXGUID_EQUAL(riid, IID_IGXObjectEvaluation))

*ppvObject=(LPVOID)(IGXObjectEvaluation*)this;

if(GXGUID_EQUAL(riid, IID_IGXCreateIntrospection))

*ppvObject=(LPVOID)(IGXCreateIntrospection*)this;

if(*ppvObject) {

((IUnknown*)*ppvObject)->AddRef();

return NOERROR;

}

return E_NOINTERFACE;

}

STDMETHODIMP_(ULONG)

ConnManager::AddRef()

{

GXUTIL_ADDREF();

}

STDMETHODIMP_(ULONG)

ConnManager::Release()

{

GXUTIL_RELEASE();

}

STDMETHODIMP

ConnManager::GetThreadSession(DWORD sessionType, IGXSession2 **ppSession)

{

LPSTR appName;

DWORD dwFlags;

if (sessionType & GXSESSION_LOCAL)

{

appName = m_appNameLocal;

dwFlags = GXSESSION_LOCAL;

}

else if (sessionType & GXSESSION_CLUSTER)

{

appName = m_appNameCluster;

dwFlags = GXSESSION_CLUSTER;

}

else

{

appName = m_appNameDistributed;

dwFlags = GXSESSION_DISTRIB;

}

return GXContextGetThreadSession(m_pContext, dwFlags, appName, ppSession);

}

 

Copyright © 2000 Sun Microsystems, Inc. Some preexisting portions Copyright © 2000 Netscape Communications Corp. All rights reserved.