Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

tweek::BeanDeliverySubjectImpl Class Reference

Implementation of tweek::BeanDeliverySubject IDL interface. More...

#include <BeanDeliverySubjectImpl.h>

Inheritance diagram for tweek::BeanDeliverySubjectImpl:

Inheritance graph
[legend]
Collaboration diagram for tweek::BeanDeliverySubjectImpl:

Collaboration graph
[legend]
List of all members.

Public Methods

 BeanDeliverySubjectImpl ()
 Default constructor. More...

virtual ~BeanDeliverySubjectImpl ()
 Default destructor. More...

virtual BeanNameListgetAllBeanNames ()
 Returns a list of all the Beans currently known to this subject. More...

virtual BeanInfogetBean (const char *beanName)
 Returns all the relevant information for the named JavaBean, including the complete JAR file. More...

virtual CORBA::Boolean hasActiveBean ()
 Determines if we have an "active" JavaBean or not. More...

virtual BeanInfogetActiveBeanInfo ()
 Returns all the relevant information for the current "active" JavaBean. More...

void addBean (const std::string &beanName, const BeanData &beanData)
 Adds the given JavaBean information to the local database. More...

void setActiveBean (const std::string &beanName)
 Marks the named JavaBean as "active". More...

void removeActiveBean ()
 Deactivates the JavaBean that had previously been marked as active. More...

tweek::BeanDeliverySubject_ptr _this ()

Detailed Description

Implementation of tweek::BeanDeliverySubject IDL interface.

This class manages delivery of JavaBeans to clients via standard Java JAR files. It also allows the C++ code to make a single JavaBean active. The idea is that the client code can change itself appropriately based on whatever JavaBean is currently active.

Date:
September 17, 2002

Definition at line 66 of file BeanDeliverySubjectImpl.h.


Constructor & Destructor Documentation

tweek::BeanDeliverySubjectImpl::BeanDeliverySubjectImpl   [inline]
 

Default constructor.

Definition at line 93 of file BeanDeliverySubjectImpl.h.

00093                              : mHasActiveBean(false)
00094    {
00095       ;
00096    }

virtual tweek::BeanDeliverySubjectImpl::~BeanDeliverySubjectImpl   [inline, virtual]
 

Default destructor.

Definition at line 101 of file BeanDeliverySubjectImpl.h.

00102    {
00103       ;
00104    }


Member Function Documentation

BeanNameList * tweek::BeanDeliverySubjectImpl::getAllBeanNames   [virtual]
 

Returns a list of all the Beans currently known to this subject.

This is part of the IDL interface, and if it is used by C++ code on the server side, that code is responsible for freeing the memory allocated herein.

Definition at line 53 of file BeanDeliverySubjectImpl.cpp.

References tweek::BeanNameList.

00054 {
00055    BeanNameList* bean_names = new BeanNameList();
00056 
00057    // Lock down the Bean collection before using it.
00058    {
00059       vpr::Guard<vpr::Mutex> lock(mBeanCollectionLock);
00060 
00061       bean_names->length(mBeanCollection.size());
00062 
00063       std::map<std::string, BeanData>::iterator i;
00064       int j;
00065 
00066       for ( i = mBeanCollection.begin(), j = 0; i != mBeanCollection.end(); ++i, ++j )
00067       {
00068          (*bean_names)[j] = CORBA::string_dup((*i).first.c_str());
00069       }
00070    }
00071 
00072    return bean_names;
00073 }

BeanInfo * tweek::BeanDeliverySubjectImpl::getBean const char *    beanName [virtual]
 

Returns all the relevant information for the named JavaBean, including the complete JAR file.

This is part of the IDL interface, and if it is used by C++ code on the server side, that code is responsible for freeing the memory allocated herein.

Postcondition:
If the named JavaBean's JAR file has not been loaded into memory, it happens now.
Note:
This method does some work to cache the JAR files for the various JavaBeans. We do lazy loading of the JAR files to conserve memory, and once we have a JAR file loaded, we store it in case it is requested again. This is done to reduce disk activity

Definition at line 75 of file BeanDeliverySubjectImpl.cpp.

References tweekDBG_CORBA.

00076 {
00077    std::string bean_name_str(beanName);
00078    vprASSERT(mBeanCollection.count(bean_name_str) > 0 && "Unknown Bean requested");
00079 
00080    // Lock down the Bean collection before accessing it.  We basically have to
00081    // hold the lock for the duration of this method because we get back a
00082    // reference to data held by the collection (see bean_data below).
00083    vpr::Guard<vpr::Mutex> lock(mBeanCollectionLock);
00084 
00085    // It's important that this be a reference to avoid copying around the
00086    // (possibly large) fileContents vector.
00087    BeanData& bean_data = mBeanCollection[bean_name_str];
00088 
00089    // If the file's byte cache is empty, read everything into bean_data's
00090    // fileContents data member.
00091    if ( bean_data.fileContents.empty() )
00092    {
00093       vprDEBUG(tweekDBG_CORBA, vprDBG_STATE_LVL)
00094          << "Reading JAR file from disk" << std::endl << vprDEBUG_FLUSH;
00095 
00096       std::ifstream bean_file;
00097       bean_file.open(bean_data.filename.c_str(),
00098                      std::ios::binary | std::ios::in);
00099 
00100       if ( bean_file.good() )
00101       {
00102          // First, we have to size the sequence so that it can hold all the bytes
00103          // read from the file.
00104          bean_file.seekg(0, std::ios::end);
00105          std::streampos file_size = bean_file.tellg();
00106 
00107          // Allocate the space for reading in the file contents.
00108          bean_data.fileContents.resize(file_size);
00109 
00110          vprDEBUG(tweekDBG_CORBA, vprDBG_VERB_LVL)
00111             << "JAR file buffer size: " << file_size << std::endl
00112             << vprDEBUG_FLUSH;
00113          vprASSERT(bean_data.fileContents.size() == (unsigned long) file_size && "Buffer resize failed");
00114 
00115          bean_file.seekg(0, std::ios::beg);
00116          vprASSERT((unsigned long) bean_file.tellg() == 0 && "Failed to restore stream pointer to beginning of file");
00117 
00118          // Read in the whole JAR file in one big swoop.
00119          bean_file.read((char*) &bean_data.fileContents[0],
00120                         bean_data.fileContents.size());
00121          bean_file.close();
00122       }
00123       else
00124       {
00125          vprDEBUG(tweekDBG_CORBA, vprDBG_CRITICAL_LVL)
00126             << clrOutNORM(clrRED, "ERROR") << ": Could not open file '"
00127             << mBeanCollection[bean_name_str].filename
00128             << "' for reading\n" << vprDEBUG_FLUSH;
00129       }
00130    }
00131 
00132    vprDEBUG(tweekDBG_CORBA, vprDBG_STATE_LVL)
00133       << "Using cached copy of JAR file" << std::endl << vprDEBUG_FLUSH;
00134 
00135    // Allocate memory for the object to be returned.
00136    BeanInfo* bean_info = new BeanInfo();
00137 
00138    // Size bean_info->jarFile so that it can hold the complete JAR file.
00139    bean_info->jarFile.length(bean_data.fileContents.size());
00140    vprASSERT(bean_info->jarFile.length() == bean_data.fileContents.size() && "Buffer resize failed");
00141 
00142    // Read the cached JAR file into bean_info->jarFile.
00143    std::vector<CORBA::Octet>::iterator i = bean_data.fileContents.begin();
00144    unsigned long j(0);
00145 
00146    for ( ; i != bean_data.fileContents.end(); ++i, ++j )
00147    {
00148       bean_info->jarFile[j] = *i;
00149    }
00150 
00151    // XXX: It's a little weird to send the beanName argument back to the
00152    // caller...
00153    bean_info->name = CORBA::string_dup(beanName);
00154    bean_info->introspectorClassName =
00155       CORBA::string_dup(bean_data.introspectorClassName.c_str());
00156 
00157    return bean_info;
00158 }

virtual CORBA::Boolean tweek::BeanDeliverySubjectImpl::hasActiveBean   [inline, virtual]
 

Determines if we have an "active" JavaBean or not.

Returns:
true is returned if a JavaBean is active; false otherwise.

Definition at line 135 of file BeanDeliverySubjectImpl.h.

00136    {
00137       vpr::Guard<vpr::Mutex> lock(mActiveBeanLock);
00138       return mHasActiveBean;
00139    }

BeanInfo * tweek::BeanDeliverySubjectImpl::getActiveBeanInfo   [virtual]
 

Returns all the relevant information for the current "active" JavaBean.

Precondition:
A JavaBean has been marked as active.
See also:
setActiveBean()

Definition at line 160 of file BeanDeliverySubjectImpl.cpp.

00161 {
00162    BeanInfo* bean_info = new BeanInfo();
00163 
00164    // Lock down the data relating to the active Bean.  We basically have to
00165    // hold the lock for the duration of this method because we get back a
00166    // reference to data held by the collection (see bean_data below).
00167    vpr::Guard<vpr::Mutex> lock(mActiveBeanLock);
00168 
00169    // It's important that this be a reference to avoid copying around the
00170    // (possibly large) fileContents vector unnecessarily.
00171    BeanData& bean_data = mBeanCollection[mActiveBean];
00172 
00173    bean_info->name                  = CORBA::string_dup(bean_data.filename.c_str());
00174    bean_info->introspectorClassName = CORBA::string_dup(bean_data.introspectorClassName.c_str());
00175 
00176    // Size bean_info->jarFile so that it can hold the complete JAR file.
00177    bean_info->jarFile.length(bean_data.fileContents.size());
00178    vprASSERT(bean_info->jarFile.length() == bean_data.fileContents.size() && "Buffer resize failed");
00179 
00180    // Read the cached JAR file into bean_info->jarFile.
00181    std::vector<CORBA::Octet>::iterator i = bean_data.fileContents.begin();
00182    unsigned long j(0);
00183 
00184    for ( ; i != bean_data.fileContents.end(); ++i, ++j )
00185    {
00186       bean_info->jarFile[j] = *i;
00187    }
00188 
00189    return bean_info;
00190 }

void tweek::BeanDeliverySubjectImpl::addBean const std::string &    beanName,
const BeanData   beanData
 

Adds the given JavaBean information to the local database.

The JAR file named in the given BeanData object is not loaded into memory. This happens later.

Parameters:
beanName  The unique name of the JavaBean being added.
beanData  Relevant information required for telling observers about the named JavaBean.
Note:
Typically, the fileContents member of beanData should be empty when it is passed in to this method. The JAR file information will be loaded on demand by this class when appropriate.

Definition at line 192 of file BeanDeliverySubjectImpl.cpp.

References tweek::SubjectImpl::notify.

00194 {
00195    mBeanCollectionLock.acquire();
00196    {
00197       mBeanCollection[beanName] = beanData;
00198    }
00199    mBeanCollectionLock.release();
00200 
00201    tweek::SubjectImpl::notify();
00202 }

void tweek::BeanDeliverySubjectImpl::setActiveBean const std::string &    beanName
 

Marks the named JavaBean as "active".

Any observers are notified of this change in state, and they can respond accordingly.

Precondition:
The named Bean is already known to this subject.
Parameters:
beanName  The name of the JavaBean to mark as active.

Definition at line 204 of file BeanDeliverySubjectImpl.cpp.

References tweek::SubjectImpl::notify.

00205 {
00206 #ifdef TWEEK_DEBUG
00207    mBeanCollectionLock.acquire();
00208    {
00209       vprASSERT(mBeanCollection.count(beanName) > 0 && "Tried to make unknown Bean active");
00210    }
00211    mBeanCollectionLock.release();
00212 #endif
00213 
00214    mActiveBeanLock.acquire();
00215    {
00216       mHasActiveBean = true;
00217       mActiveBean    = beanName;
00218    }
00219    mActiveBeanLock.release();
00220 
00221    tweek::SubjectImpl::notify();
00222 }

void tweek::BeanDeliverySubjectImpl::removeActiveBean  
 

Deactivates the JavaBean that had previously been marked as active.

Any observers are notified of this change in state, and they can respond accordingly.

Precondition:
A Bean has been marked as active.
Parameters:
beanName  The name of the JavaBean to mark as active.
See also:
setActiveBean()

Definition at line 224 of file BeanDeliverySubjectImpl.cpp.

References tweek::SubjectImpl::notify.

00225 {
00226    mActiveBeanLock.acquire();
00227    {
00228       vprASSERT(mHasActiveBean && "No active Bean to remove");
00229       mHasActiveBean = false;
00230       mActiveBean    = std::string("");
00231    }
00232    mActiveBeanLock.release();
00233 
00234    tweek::SubjectImpl::notify();
00235 }

tweek::BeanDeliverySubject_ptr tweek::BeanDeliverySubjectImpl::_this   [inline]
 

Definition at line 188 of file BeanDeliverySubjectImpl.h.

00189    {
00190       return POA_tweek::BeanDeliverySubject::_this();
00191    }


The documentation for this class was generated from the following files:
Generated on Sun May 2 14:41:09 2004 for Tweek by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002