OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimStreamFactoryRegistry.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 //
3 // License: MIT
4 //
5 // Author: Garrett Potts
6 //
7 //*******************************************************************
8 // $Id$
9 
16 
17 #include <fstream>
18 #include <algorithm>
19 #include <ossim/base/ossimTrace.h>
20 
21 
23 static const char* ISTREAM_BUFFER_KW = "ossim.stream.factory.registry.istream.buffer";
24 static ossimTrace traceDebug("ossimStreamFactoryRegistry:debug");
25 static std::mutex m_instanceMutex;
27 {
28 }
29 
30 
32 {
33 }
34 
36 {
37  // because of the loadPreferences for this factory and the fact that
38  // create stream could be recursive the lock and unlock are
39  // a little different here. We will unlock as quickly as we can
40  // After the unlock then call load preferences
41  m_instanceMutex.lock();
42  if(!m_instance)
43  {
44  m_instance = new ossim::StreamFactoryRegistry();
45  m_instance->registerFactory(ossim::StreamFactory::instance());
46  m_instanceMutex.unlock();
47  m_instance->loadPreferences();
48  }
49  else
50  {
51  m_instanceMutex.unlock();
52  }
53 
54  return m_instance;
55 }
56 
58 {
59  if(traceDebug())
60  {
62  << "ossim::StreamFactoryRegistry::loadPreferences: ....... entered!\n";
63  }
64  std::vector<ossimString> sortedList;
66  .getSortedList(sortedList, ISTREAM_BUFFER_KW);
67  if(sortedList.size())
68  {
69  m_bufferInfoList.resize(sortedList.size());
70  }
71  else
72  {
73  m_bufferInfoList.clear();
74  }
75  ossim_uint32 idx=0;
76  for(std::vector<ossimString>::const_iterator iter = sortedList.begin();
77  iter!=sortedList.end();
78  ++iter,++idx)
79  {
80  ossimString prefix = *iter;
81  ossimString bufferIStreamEnabled = ossimPreferences::instance()->findPreference(prefix+".enabled");
82  ossimString bufferIStreamBlockEnabled = ossimPreferences::instance()->findPreference(prefix+".enableBlocked");
83  ossimString bufferIStreamIncludePattern = ossimPreferences::instance()->findPreference(prefix+".includePattern");
84  ossimString bufferIStreamSize = ossimPreferences::instance()->findPreference(prefix+".size");
85 
86  if(!bufferIStreamSize.empty())
87  {
88  m_bufferInfoList[idx].m_size = bufferIStreamSize.toUInt64();
89  }
90  if(!bufferIStreamEnabled.empty())
91  {
92  m_bufferInfoList[idx].m_enabled = bufferIStreamEnabled.toBool();
93  }
94  if(!bufferIStreamBlockEnabled.empty())
95  {
96  m_bufferInfoList[idx].m_enableBlocked = bufferIStreamBlockEnabled.toBool();
97  }
98  if(!bufferIStreamIncludePattern.empty())
99  {
100  m_bufferInfoList[idx].m_pattern = bufferIStreamIncludePattern;
101  }
102  if(traceDebug())
103  {
105  << "ossim::StreamFactoryRegistry adding BufferInfo: \n"
106  << "enabled: " << ossimString::toString(m_bufferInfoList[idx].m_enabled)<< "\n"
107  << "enableBlocked: " << ossimString::toString(m_bufferInfoList[idx].m_enableBlocked)<< "\n"
108  << "size: " << m_bufferInfoList[idx].m_size << "\n"
109  << "pattern: " << m_bufferInfoList[idx].m_pattern << "\n";
110  }
111 
112  }
113  if(traceDebug())
114  {
116  << "ossim::StreamFactoryRegistry::loadPreferences: ....... leaving!\n";
117  }
118 }
119 
121  const ossimString& connectionString)const
122 {
123  bool result = false;
124 
125  for(std::vector<BufferInfo>::const_iterator iter = m_bufferInfoList.begin();
126  iter != m_bufferInfoList.end();
127  ++iter)
128  {
129  if(iter->m_enabled)
130  {
131  m_patternMatcher.compile(iter->m_pattern);
132  if(m_patternMatcher.is_valid())
133  {
134  if(m_patternMatcher.find(connectionString.c_str()))
135  {
136  bufferInfo = *iter;
137  result = true;
138  break;
139  }
140  }
141  }
142  }
143 
144  return result;
145 }
146 
147 
148 std::shared_ptr<ossim::istream> ossim::StreamFactoryRegistry::createIstream(
149  const std::string& connectionString,
150  const ossimKeywordlist& options,
151  std::ios_base::openmode openMode) const
152 {
153  std::shared_ptr<ossim::istream> result(0);
154  if(traceDebug())
155  {
157  << "ossim::StreamFactoryRegistry::createIstream: ....... entered: "<< connectionString << "\n";
158  }
159 
160  ossim_uint32 i = 0;
161  for(i = 0; (i < m_factoryList.size())&&(!result); ++i)
162  {
163  result = m_factoryList[i]->createIstream(connectionString, options, openMode);
164 
165  if(result)
166  {
167  BufferInfo bufferInfo;
168  if(getBufferInfo(bufferInfo, connectionString))
169  {
170  if(bufferInfo.m_enableBlocked)
171  {
172  result = std::make_shared<ossim::BlockIStream>(result, bufferInfo.m_size);
173  }
174  else
175  {
176  result = std::make_shared<ossimBufferedInputStream>(result, bufferInfo.m_size);
177  }
178  }
179  }
180  }
181 
182  if(traceDebug())
183  {
185  << "ossim::StreamFactoryRegistry::createIstream: ....... leaving!\n";
186  }
187 
188  return result;
189 }
190 
191 std::shared_ptr<ossim::ostream> ossim::StreamFactoryRegistry::createOstream(
192  const std::string& /*connectionString*/,
193  const ossimKeywordlist& /* options */,
194  std::ios_base::openmode /*openMode*/) const
195 {
196  std::shared_ptr<ossim::ostream> result(0);
197  return result;
198 }
199 
200 std::shared_ptr<ossim::iostream> ossim::StreamFactoryRegistry::createIOstream(
201  const std::string& /*connectionString*/,
202  const ossimKeywordlist& /* options */,
203  std::ios_base::openmode /*openMode*/) const
204 {
205  std::shared_ptr<ossim::iostream> result(0);
206  return result;
207 }
208 
209 bool ossim::StreamFactoryRegistry::exists(const std::string& connectionString) const
210 {
211  bool continueFlag = true;
212  return exists( connectionString, continueFlag );
213 }
214 
215 bool ossim::StreamFactoryRegistry::exists(const std::string& connectionString,
216  bool& continueFlag) const
217 {
218  if(traceDebug())
219  {
221  << "ossim::StreamFactoryRegistry::exists: ....... entered: "<< connectionString << "\n";
222  }
223  bool result = false;
224  for(auto factory:m_factoryList)
225  {
226 
227  result = factory->exists( connectionString, continueFlag );
228  if ( ( result == true ) || (continueFlag == false ) ) break;
229  }
230 
231  if(traceDebug())
232  {
234  << "ossim::StreamFactoryRegistry::createIstream: ....... leaving!\n";
235  }
236  return result;
237 }
238 
240 {
241  std::vector<ossim::StreamFactoryBase*>::iterator iter = std::find(
242  m_factoryList.begin(), m_factoryList.end(), factory);
243  if(iter == m_factoryList.end())
244  {
245  m_factoryList.push_back(factory);
246  }
247 }
248 
250 {
251  // std::lock_guard<std::mutex> lock(m_factoryListMutex);
252  std::vector<ossim::StreamFactoryBase*>::iterator iter = std::find(
253  m_factoryList.begin(), m_factoryList.end(), factory);
254  if(iter != m_factoryList.end())
255  {
256  m_factoryList.erase( iter );
257  }
258 }
const ossimKeywordlist & preferencesKWL() const
Represents serializable keyword/value map.
static ossimString toString(bool aValue)
Numeric to string methods.
static StreamFactoryRegistry * instance()
virtual std::shared_ptr< ossim::iostream > createIOstream(const std::string &connectionString, const ossimKeywordlist &options=ossimKeywordlist(), std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out|std::ios_base::binary) const
Will try to creates an iostream interface to the connectionString passed in.
static StreamFactoryRegistry * m_instance
const char * findPreference(const char *key) const
bool exists(const std::string &connectionString) const
Methods to test if connection exists.
This is an internal class used to store the buffer information loaded from the preferences.
bool toBool() const
String to numeric methods.
void registerFactory(StreamFactoryBase *factory)
Registers a stream factory.
unsigned int ossim_uint32
static ossimPreferences * instance()
bool getBufferInfo(BufferInfo &bufferInfo, const ossimString &connectionString) const
static StreamFactory * instance()
This is a generic stream registry.
void getSortedList(std::vector< ossimString > &prefixValues, const ossimString &prefixKey) const
This return the sorted keys if you have a list.
ossim_uint64 toUInt64() const
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
bool empty() const
Definition: ossimString.h:411
virtual std::shared_ptr< ossim::ostream > createOstream(const std::string &connectionString, const ossimKeywordlist &options=ossimKeywordlist(), std::ios_base::openmode mode=std::ios_base::out|std::ios_base::binary) const
Will try to creates an ostream interface to the connectionString passed in.
void unregisterFactory(StreamFactoryBase *factory)
Removes a factory from the registry.
virtual std::shared_ptr< ossim::istream > createIstream(const std::string &connectionString, const ossimKeywordlist &options=ossimKeywordlist(), std::ios_base::openmode mode=std::ios_base::in|std::ios_base::binary) const
Will try to creates an istream interface to the connectionString passed in.
void loadPreferences()
This will load the buffer information from the preferences.
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)