Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

FactoryLibrary_ltdl.cc

Go to the documentation of this file.
00001 /*
00002  * FactoryLibrary_ltdl.cc
00003  *
00004  * Copyright (c) 2003, 2004 The University of Utah and the Flux Group.
00005  * All rights reserved.
00006  *
00007  * This file is licensed under the terms of the GNU Public License.  
00008  * See the file "license.terms" for restrictions on redistribution 
00009  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00010  */
00011 
00012 /**
00013  * @file FactoryLibrary_ltdl.cc
00014  *
00015  * Implementation file for the FactoryLibrary_ltdl class.
00016  */
00017 
00018 #include "config.h"
00019 
00020 #include <assert_pp.h>
00021 #include <factory_library.h>
00022 
00023 #include <sstream>
00024 
00025 #include "FactoryLibrary_ltdl.hh"
00026 
00027 #ifndef __XSTRING
00028 /**
00029  * Convert a macro argument to a string.
00030  *
00031  * @param x The macro to expand to a string.
00032  */
00033 #define __XSTRING(x) __STRING(x)
00034 #endif
00035 
00036 map<string, FactoryLibrary_ltdl *> FactoryLibrary_ltdl::open_libraries;
00037 
00038 CORBA::ORB_var FactoryLibrary_ltdl::orb;
00039 
00040 FactoryLibrary_ltdl::FactoryLibrary_ltdl(PortableServer::POA_ptr poa,
00041                                          const char *name,
00042                                          lt_dlhandle dlh,
00043                                          factory_method_t fm) :
00044     fl_POA(PortableServer::POA::_duplicate(poa)),
00045     fl_Name(name),
00046     fl_OpenCount(1),
00047     fl_Library(dlh),
00048     fl_Method(fm)
00049 {
00050     require(name != NULL);
00051     require(dlh != NULL);
00052     require(fm != NULL);
00053 }
00054 
00055 FactoryLibrary_ltdl::~FactoryLibrary_ltdl(void)
00056 {
00057     lt_dlclose(this->fl_Library);
00058     this->fl_Library = NULL;
00059     this->fl_Method = NULL;
00060 }
00061 
00062 char *FactoryLibrary_ltdl::Name(void)
00063     throw (CORBA::SystemException)
00064 {
00065     CORBA::String_var retval;
00066 
00067     retval = this->fl_Name;
00068     return( retval._retn() );
00069 }
00070 
00071 CORBA::Long FactoryLibrary_ltdl::Hey(const edu::utah::pces::ArgV &args,
00072                                      CORBA::String_out o,
00073                                      CORBA::String_out e)
00074     throw (CORBA::SystemException)
00075 {
00076     HeyParser hp(args.length(), args.get_buffer());
00077     CORBA::Long retval = EINVAL;
00078 
00079     /* String streams for stdout and stderr */
00080     stringstream out, err;
00081     string so, se;
00082 
00083     try
00084     {
00085         /* Pass the request to the library. */
00086         retval = flFactoryMethod(this->fl_Method, FLO_HEY,
00087                                  FMA_ORB, FactoryLibrary_ltdl::orb.in(),
00088                                  FMA_POA, this->fl_POA.in(),
00089                                  FMA_HeyParser, &hp,
00090                                  FMA_StandardOut, &out,
00091                                  FMA_StandardError, &err,
00092                                  FMA_TAG_DONE);
00093     }
00094     catch(const HeyParserException &e)
00095     {
00096         err << e << endl;
00097     }
00098 
00099     /* Terminate the stdio strings. */
00100     out << ends;
00101     err << ends;
00102 
00103     /* Pass them back out. */
00104     so = out.str();
00105     se = err.str();
00106     o = so.c_str();
00107     e = se.c_str();
00108     return( retval );
00109 }
00110 
00111 edu::utah::pces::FactoryLibrary_ptr
00112 FactoryLibrary_ltdl::OpenLibrary(PortableServer::POA_ptr poa,
00113                                  const char *name)
00114     throw (edu::utah::pces::NoSuchLibrary,
00115            CORBA::SystemException)
00116 {
00117     edu::utah::pces::FactoryLibrary_var retval;
00118     lt_dlhandle dlh;
00119 
00120     require(name != NULL);
00121 
00122     /* Do some initialization... */
00123     {
00124         static bool init_done = false;
00125 
00126         if( !init_done )
00127         {
00128             int argc = 0;
00129             
00130             if( lt_dlinit() != 0 )
00131             {
00132                 cerr << "lt_dlinit: " << lt_dlerror << endl;
00133                 throw CORBA::NO_MEMORY(); /** @todo Throw something else... */
00134             }
00135             orb = CORBA::ORB_init(argc, NULL);
00136             init_done = true;
00137         }
00138     }
00139 
00140     /* Check if it is already open, */
00141     if( FactoryLibrary_ltdl::open_libraries.count(name) )
00142     {
00143         FactoryLibrary_ltdl *fl;
00144 
00145         fl = FactoryLibrary_ltdl::open_libraries[name];
00146         fl->fl_OpenCount += 1;
00147         retval = edu::utah::pces::FactoryLibrary::
00148             _narrow(poa->servant_to_reference(fl));
00149         fl = NULL;
00150     }
00151     /* ... or is a valid path to a library. */
00152     else if( (dlh = lt_dlopenext(name)) != NULL )
00153     {
00154         PortableServer::ObjectId_var oid;
00155         FactoryLibrary_ltdl *fl;
00156         factory_method_t fm;
00157 
00158         /* Check if it is a real factory library then */
00159         if( (fm = (factory_method_t)
00160              lt_dlsym(dlh, __XSTRING(FACTORY_METHOD_SYMBOL))) == NULL )
00161         {
00162             lt_dlclose(dlh);
00163             throw edu::utah::pces::NoSuchLibrary(name, "No factory_method_t");
00164         }
00165 
00166         /* ... create the object and */
00167         fl = new FactoryLibrary_ltdl(poa, name, dlh, fm);
00168         /* ... add it to the map. */
00169         FactoryLibrary_ltdl::open_libraries[name] = fl;
00170         oid = poa->activate_object(fl);
00171         retval = edu::utah::pces::FactoryLibrary::
00172             _narrow(poa->id_to_reference(oid.in()));
00173         fl = NULL;
00174     }
00175     else
00176     {
00177         throw edu::utah::pces::NoSuchLibrary(name, lt_dlerror());
00178     }
00179     return( retval._retn() );
00180 }
00181 
00182 void FactoryLibrary_ltdl::Shutdown(void)
00183 {
00184     lt_dlexit();
00185 }

Generated on Fri Oct 22 07:50:24 2004 for CPU Broker by  doxygen 1.3.9.1