LoadableDevice_impl.h
Go to the documentation of this file.
1 /*
2  * This file is protected by Copyright. Please refer to the COPYRIGHT file
3  * distributed with this source distribution.
4  *
5  * This file is part of REDHAWK core.
6  *
7  * REDHAWK core is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * REDHAWK core is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program. If not, see http://www.gnu.org/licenses/.
19  */
20 
21 #ifndef LOADABLE_DEVICE_IMPL_H
22 #define LOADABLE_DEVICE_IMPL_H
23 
24 #include <vector>
25 #include <map>
26 #include "Resource_impl.h"
27 #include "Device_impl.h"
28 #include "CF/cf.h"
29 #include <boost/filesystem/path.hpp>
30 #include "ossie/Autocomplete.h"
31 
32 typedef std::multimap<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string, std::string> > >
34 
35 /*
36  * EnvironmentPathParser provides operations to read, write, and modify
37  * environment path strings (e.g. LD_LIBRARY_PATH).
38  */
40 {
41 public:
42  /*
43  * Constructor from std::string. Extracts paths from path.
44  * @param path Path string in the format PATH1:PATH2:PATH3
45  */
46  EnvironmentPathParser( const std::string& path="" );
47 
48  /*
49  * Constructor from const char*. Supports NULL pointers, as returned from
50  * getenv() on unknown environment variable.
51  * @param path Path string in the format PATH1:PATH2:PATH3
52  */
53  EnvironmentPathParser( const char* path );
54 
55  // Update the path with a string
56  void from_string( const std::string& path );
57  // Return a string version of the path
58  std::string to_string() const;
59 
60  // Prepend the given path argument to the current path
61  void merge_front( const std::string& path );
62 
63 private:
64  void strip_empty_paths();
65  std::vector<std::string> paths;
66 };
67 
69 public:
70  // Set the filename for the shared library
71  void setFilename(std::string _filename) {
72  filename = _filename;
73  }
74  // Define the path modification for the given filename
75  void addModification(const std::string _path_to_modify, const std::string &_path_modification) {
76  modifications.push_back(std::make_pair(_path_to_modify, _path_modification));
77  };
78  // Filename for the shared package
79  std::string filename;
80  // Set of path modifications that apply
81  std::vector<std::pair<std::string,std::string> > modifications;
82 };
83 
84 /*
85  * This class stores the current LD_LIBRARY_PATH, PYTHONPATH, CLASSPATH, and OCTAVE_PATH
86  * when it is instantiated. It can then be used to restore the system settings
87  */
89 public:
91  if (getenv("LD_LIBRARY_PATH"))
92  ld_lib_path = getenv("LD_LIBRARY_PATH");
93  else
94  ld_lib_path.clear();
95  if (getenv("PYTHONPATH"))
96  pythonpath = getenv("PYTHONPATH");
97  else
98  pythonpath.clear();
99  if (getenv("CLASSPATH"))
100  classpath = getenv("CLASSPATH");
101  else
102  classpath.clear();
103  if (getenv("OCTAVE_PATH"))
104  octave_path = getenv("OCTAVE_PATH");
105  else
106  octave_path.clear();
107  };
108 
109  /*
110  * Set the system LD_LIBRARY_PATH, PYTHONPATH, CLASSPATH, and OCTAVE_PATH
111  * to what they were when this class was instantiated
112  */
113  void set() {
114  setenv("LD_LIBRARY_PATH", ld_lib_path.c_str(), 1);
115  setenv("PYTHONPATH", pythonpath.c_str(), 1);
116  setenv("CLASSPATH", classpath.c_str(), 1);
117  setenv("OCTAVE_PATH", octave_path.c_str(), 1);
118  };
119 
121 };
122 
123 /*
124  * This class stores the current LD_LIBRARY_PATH, PYTHONPATH, CLASSPATH, and OCTAVE_PATH
125  * when it is instantiated and restores it when it is destroyed
126  */
127 class envState {
128 public:
130  if (getenv("LD_LIBRARY_PATH"))
131  ld_lib_path = getenv("LD_LIBRARY_PATH");
132  else
133  ld_lib_path.clear();
134  if (getenv("PYTHONPATH"))
135  pythonpath = getenv("PYTHONPATH");
136  else
137  pythonpath.clear();
138  if (getenv("CLASSPATH"))
139  classpath = getenv("CLASSPATH");
140  else
141  classpath.clear();
142  if (getenv("OCTAVE_PATH"))
143  octave_path = getenv("OCTAVE_PATH");
144  else
145  octave_path.clear();
146  };
148  setenv("LD_LIBRARY_PATH", ld_lib_path.c_str(), 1);
149  setenv("PYTHONPATH", pythonpath.c_str(), 1);
150  setenv("CLASSPATH", classpath.c_str(), 1);
151  setenv("OCTAVE_PATH", octave_path.c_str(), 1);
152  };
153 private:
154  std::string ld_lib_path, pythonpath, classpath, octave_path;
155 };
156 
157 /* CLASS DEFINITION *******************************************************************************
158  ************************************************************************************************ */
160 #ifdef BEGIN_AUTOCOMPLETE_IGNORE
161  public virtual POA_CF::LoadableDevice,
162 #endif
163  public Device_impl
164 {
165  ENABLE_LOGGING
166 
167 public:
168  LoadableDevice_impl (char*, char*, char*, char*);
169  LoadableDevice_impl (char*, char*, char*, char*, CF::Properties capacities);
170  LoadableDevice_impl (char*, char*, char*, char*, char*);
171  LoadableDevice_impl (char*, char*, char*, char*, CF::Properties capacities, char*);
172  virtual ~LoadableDevice_impl ();
173  void configure (const CF::Properties& configProperties)
174  throw (CF::PropertySet::PartialConfiguration,
175  CF::PropertySet::InvalidConfiguration, CORBA::SystemException);
176 
177  // Externally visible function call to load a file
178  void load (CF::FileSystem_ptr fs, const char* fileName,
179  CF::LoadableDevice::LoadType loadKind)
180  throw (CF::LoadableDevice::LoadFail, CF::InvalidFileName,
181  CF::LoadableDevice::InvalidLoadKind, CF::Device::InvalidState,
182  CORBA::SystemException);
183  // Internal operation that loads the file
184  void do_load (CF::FileSystem_ptr fs, const char* fileName, CF::LoadableDevice::LoadType loadKind)
185  throw (CF::LoadableDevice::LoadFail, CF::InvalidFileName,
186  CF::LoadableDevice::InvalidLoadKind, CF::Device::InvalidState,
187  CORBA::SystemException);
188 
189  // Externally visible function call to unload a file
190  void unload (const char* fileName)
191  throw (CF::InvalidFileName, CF::Device::InvalidState,
192  CORBA::SystemException);
193  // Internal operation that unloads the file
194  void do_unload (const char* fileName)
195  throw (CF::InvalidFileName, CF::Device::InvalidState,
196  CORBA::SystemException);
197 
198  // Check to see if a particular file has already been loaded
199  bool isFileLoaded (const char* fileName);
200 
201  // Set the transfer size when loading files
202  void setTransferSize( uint64_t xfersize ){
203  if ( xfersize > 0 ) transferSize=xfersize;
204  }
205 
206 protected:
207 
208  // Increment the loadedFiles counter
209  void incrementFile (std::string);
210  // Decrement the loadedFiles counter
211  void decrementFile (std::string);
212  void removeDuplicateFiles(std::string fileName);
213  // Map that keeps track of how many times a file was loaded
214  std::map<std::string, int> loadedFiles;
215  // Data structure that keeps track of the type of file that was loaded
216  std::map<std::string, CF::FileSystem::FileType> fileTypeTable;
217  // Map that keeps track of the files that have been copied
219  boost::recursive_mutex load_execute_lock;
220  void update_ld_library_path (CF::FileSystem_ptr fs, const char* fileName, CF::LoadableDevice::LoadType loadKind) throw (CORBA::SystemException, CF::Device::InvalidState, CF::LoadableDevice::InvalidLoadKind, CF::InvalidFileName, CF::LoadableDevice::LoadFail);
221  void update_octave_path (CF::FileSystem_ptr fs, const char* fileName, CF::LoadableDevice::LoadType loadKind) throw (CORBA::SystemException, CF::Device::InvalidState, CF::LoadableDevice::InvalidLoadKind, CF::InvalidFileName, CF::LoadableDevice::LoadFail);
222  void merge_front_environment_path( const char* environment_variable, const std::string& path ) const;
223  std::map<std::string, sharedLibraryStorage> sharedPkgs;
225  void update_path(sharedLibraryStorage &packageDescription);
226  void update_selected_paths(std::vector<sharedLibraryStorage> &paths);
227  // Transfer size when loading files
228  CORBA::LongLong transferSize; // block transfer size when loading files
229 
230  private:
231  LoadableDevice_impl(); // No default constructor
233  void _init();
234  std::map<std::string, time_t> cacheTimestamps;
235  std::map<std::string, std::vector<std::string> > duplicate_filenames;
236 
237  void _loadTree(CF::FileSystem_ptr fs, std::string remotePath, boost::filesystem::path& localPath, std::string fileKey);
238  void _deleteTree(const std::string &fileKey);
239  bool _treeIntact(const std::string &fileKey);
240  void _copyFile(CF::FileSystem_ptr fs, const std::string &remotePath, const std::string &localPath, const std::string &fileKey);
241 
242 
243 
244 };
245 
246 #endif
247 
void from_string(const std::string &path)
void update_selected_paths(std::vector< sharedLibraryStorage > &paths)
Definition: LoadableDevice_impl.h:68
virtual ~LoadableDevice_impl()
Definition: LoadableDevice_impl.h:127
std::string octave_path
Definition: LoadableDevice_impl.h:118
Definition: LoadableDevice_impl.h:88
std::multimap< std::string, std::string, std::less< std::string >, std::allocator< std::pair< std::string, std::string > > > copiedFiles_type
Definition: LoadableDevice_impl.h:33
std::string ld_lib_path
Definition: LoadableDevice_impl.h:118
Definition: LoadableDevice_impl.h:39
std::map< std::string, sharedLibraryStorage > sharedPkgs
Definition: LoadableDevice_impl.h:223
void setTransferSize(uint64_t xfersize)
Definition: LoadableDevice_impl.h:202
std::string pythonpath
Definition: LoadableDevice_impl.h:118
std::map< std::string, CF::FileSystem::FileType > fileTypeTable
Definition: LoadableDevice_impl.h:216
void update_octave_path(CF::FileSystem_ptr fs, const char *fileName, CF::LoadableDevice::LoadType loadKind)
void unload(const char *fileName)
boost::recursive_mutex load_execute_lock
Definition: LoadableDevice_impl.h:219
envState()
Definition: LoadableDevice_impl.h:129
std::string classpath
Definition: LoadableDevice_impl.h:118
std::map< std::string, int > loadedFiles
Definition: LoadableDevice_impl.h:214
void do_load(CF::FileSystem_ptr fs, const char *fileName, CF::LoadableDevice::LoadType loadKind)
void removeDuplicateFiles(std::string fileName)
void load(CF::FileSystem_ptr fs, const char *fileName, CF::LoadableDevice::LoadType loadKind)
void merge_front_environment_path(const char *environment_variable, const std::string &path) const
void set()
Definition: LoadableDevice_impl.h:113
Definition: Device_impl.h:40
void update_path(sharedLibraryStorage &packageDescription)
std::vector< std::pair< std::string, std::string > > modifications
Definition: LoadableDevice_impl.h:81
EnvironmentPathParser(const std::string &path="")
void incrementFile(std::string)
bool isFileLoaded(const char *fileName)
envStateContainer initialState
Definition: LoadableDevice_impl.h:224
void decrementFile(std::string)
envStateContainer()
Definition: LoadableDevice_impl.h:90
void addModification(const std::string _path_to_modify, const std::string &_path_modification)
Definition: LoadableDevice_impl.h:75
std::string filename
Definition: LoadableDevice_impl.h:77
void update_ld_library_path(CF::FileSystem_ptr fs, const char *fileName, CF::LoadableDevice::LoadType loadKind)
void do_unload(const char *fileName)
Definition: LoadableDevice_impl.h:159
~envState()
Definition: LoadableDevice_impl.h:147
CORBA::LongLong transferSize
Definition: LoadableDevice_impl.h:228
std::string to_string() const
void setFilename(std::string _filename)
Definition: LoadableDevice_impl.h:71
void merge_front(const std::string &path)
copiedFiles_type copiedFiles
Definition: LoadableDevice_impl.h:218
void configure(const CF::Properties &configProperties)