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

BeanDeliverySubjectImpl.cpp

Go to the documentation of this file.
00001 /***************** <Tweek heading BEGIN do not edit this line> ****************
00002  * Tweek
00003  *
00004  * -----------------------------------------------------------------
00005  * File:          $RCSfile: BeanDeliverySubjectImpl.cpp,v $
00006  * Date modified: $Date: 2003/08/03 00:08:28 $
00007  * Version:       $Revision: 1.6 $
00008  * -----------------------------------------------------------------
00009  ***************** <Tweek heading END do not edit this line> *****************/
00010 
00011 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00012  *
00013  * VR Juggler is (C) Copyright 1998-2003 by Iowa State University
00014  *
00015  * Original Authors:
00016  *   Allen Bierbaum, Christopher Just,
00017  *   Patrick Hartling, Kevin Meinert,
00018  *   Carolina Cruz-Neira, Albert Baker
00019  *
00020  * This library is free software; you can redistribute it and/or
00021  * modify it under the terms of the GNU Library General Public
00022  * License as published by the Free Software Foundation; either
00023  * version 2 of the License, or (at your option) any later version.
00024  *
00025  * This library is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00028  * Library General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU Library General Public
00031  * License along with this library; if not, write to the
00032  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00033  * Boston, MA 02111-1307, USA.
00034  *
00035  *************** <auto-copyright.pl END do not edit this line> ***************/
00036 
00037 #include <tweek/tweekConfig.h>
00038 
00039 #include <fstream>
00040 
00041 #include <vpr/vpr.h>
00042 #include <vpr/Sync/Guard.h>
00043 #include <vpr/Util/Assert.h>
00044 #include <vpr/Util/Debug.h>
00045 
00046 #include <tweek/Util/Debug.h>
00047 #include <tweek/CORBA/BeanDeliverySubjectImpl.h>
00048 
00049 
00050 namespace tweek
00051 {
00052 
00053 BeanNameList* BeanDeliverySubjectImpl::getAllBeanNames()
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 }
00074 
00075 BeanInfo* BeanDeliverySubjectImpl::getBean(const char* beanName)
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 }
00159 
00160 BeanInfo* BeanDeliverySubjectImpl::getActiveBeanInfo()
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 }
00191 
00192 void BeanDeliverySubjectImpl::addBean(const std::string& beanName,
00193                                       const BeanData& beanData)
00194 {
00195    mBeanCollectionLock.acquire();
00196    {
00197       mBeanCollection[beanName] = beanData;
00198    }
00199    mBeanCollectionLock.release();
00200 
00201    tweek::SubjectImpl::notify();
00202 }
00203 
00204 void BeanDeliverySubjectImpl::setActiveBean(const std::string& beanName)
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 }
00223 
00224 void BeanDeliverySubjectImpl::removeActiveBean()
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 }
00236 
00237 // ============================================================================
00238 // Private methods follow.
00239 // ============================================================================
00240 
00241 BeanDeliverySubjectImpl::BeanDeliverySubjectImpl(const BeanDeliverySubjectImpl& subj)
00242    :
00243 #ifdef OMNIORB_VER
00244      omniServant(subj)
00245    , tweek::_impl_Subject(subj)
00246    , tweek::_impl_BeanDeliverySubject(subj)
00247    ,
00248 #endif
00249      PortableServer::ServantBase(subj)
00250    , POA_tweek::Subject(subj)
00251    , POA_tweek::BeanDeliverySubject(subj)
00252    , tweek::SubjectImpl(subj)
00253 {
00254    /* Do nothing. */ ;
00255 }
00256 
00257 }  // End of tweek namespace

Generated on Sun May 2 14:41:03 2004 for Tweek by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002