OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESServiceRegistry.cc
Go to the documentation of this file.
1 // BESServiceRegistry.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include "BESServiceRegistry.h"
34 #include "BESInfo.h"
35 #include "BESInternalError.h"
36 
37 BESServiceRegistry *BESServiceRegistry::_instance = 0 ;
38 
40 {
41 }
42 
44 {
45 }
46 
52 void
53 BESServiceRegistry::add_service( const string &name )
54 {
55  map<string,map<string,service_cmd> >::iterator i = _services.find( name ) ;
56  if( i == _services.end() )
57  {
58  map<string,service_cmd> cmds ;
59  _services[name] = cmds ;
60  }
61  else
62  {
63  string err = (string)"The service " + name
64  + " has already been registered" ;
65  throw BESInternalError( err, __FILE__, __LINE__ ) ;
66  }
67 }
68 
83 void
84 BESServiceRegistry::add_to_service( const string &service,
85  const string &cmd,
86  const string &cmd_descript,
87  const string &format )
88 {
89  map<string,map<string,service_cmd> >::iterator si ;
90  si = _services.find( service ) ;
91  if( si != _services.end() )
92  {
93  map<string,service_cmd>::const_iterator ci ;
94  ci = (*si).second.find( cmd ) ;
95  if( ci != (*si).second.end() )
96  {
97  string err = (string)"Attempting to add command "
98  + (*ci).first + " to the service "
99  + service + ", command alrady exists" ;
100  throw BESInternalError( err, __FILE__, __LINE__ ) ;
101  }
102  service_cmd sc ;
103  sc._description = cmd_descript ;
104  sc._formats[format] = format ;
105  (*si).second[cmd] = sc ;
106  }
107  else
108  {
109  string err = (string)"Attempting to add commands to the service "
110  + service + " that has not yet been registered" ;
111  throw BESInternalError( err, __FILE__, __LINE__ ) ;
112  }
113 }
114 
123 void
124 BESServiceRegistry::add_format( const string &service,
125  const string &cmd,
126  const string &format )
127 {
128  map<string,map<string,service_cmd> >::iterator si ;
129  si = _services.find( service ) ;
130  if( si != _services.end() )
131  {
132  map<string,service_cmd>::iterator ci = (*si).second.find( cmd ) ;
133  if( ci != (*si).second.end() )
134  {
135  map<string,string>::iterator fi ;
136  fi = (*ci).second._formats.find( format ) ;
137  if( fi == (*ci).second._formats.end() )
138  {
139  (*ci).second._formats[format] = format ;
140  }
141  else
142  {
143  string err = (string)"Attempting to add format "
144  + format + " to command " + cmd
145  + " for service " + service
146  + " where the format has already been registered" ;
147  throw BESInternalError( err, __FILE__, __LINE__ ) ;
148  }
149  }
150  else
151  {
152  string err = (string)"Attempting to add a format " + format
153  + " to command " + cmd + " for service " + service
154  + " where the command has not been registered" ;
155  throw BESInternalError( err, __FILE__, __LINE__ ) ;
156  }
157  }
158  else
159  {
160  string err = (string)"Attempting to add a format " + format
161  + " to command " + cmd + " for a service " + service
162  + " that has not been registered" ;
163  throw BESInternalError( err, __FILE__, __LINE__ ) ;
164  }
165 }
166 
175 void
176 BESServiceRegistry::remove_service( const string &service )
177 {
178  map<string,map<string,service_cmd> >::iterator i ;
179  i = _services.find( service ) ;
180  if( i != _services.end() )
181  {
182  // erase the service from the registry
183  _services.erase( i ) ;
184 
185  // remove the service from the _handles list as well, so that if
186  // asked, the handlers no longer handler the service because it no
187  // longer exists.
188  map<string,map<string,string> >::iterator hi = _handles.begin() ;
189  map<string,map<string,string> >::iterator he = _handles.end() ;
190  for( ; hi != he; hi++ )
191  {
192  map<string,string>::iterator hsi = (*hi).second.find( service ) ;
193  if( hsi != (*hi).second.end() )
194  {
195  (*hi).second.erase( hsi ) ;
196  }
197  }
198  }
199 }
200 
215 bool
217  const string &cmd,
218  const string &format )
219 {
220  bool isit = false ;
221  map<string,map<string,service_cmd> >::iterator si ;
222  si = _services.find( service ) ;
223  if( si != _services.end() )
224  {
225  if( !cmd.empty() )
226  {
227  map<string,service_cmd>::iterator ci = (*si).second.find( cmd ) ;
228  if( ci != (*si).second.end() )
229  {
230  if( !format.empty() )
231  {
232  map<string,string>::iterator fi ;
233  fi = (*ci).second._formats.find( format ) ;
234  if( fi != (*ci).second._formats.end() )
235  {
236  isit = true ;
237  }
238  }
239  else
240  {
241  isit = true ;
242  }
243  }
244  }
245  else
246  {
247  isit = true ;
248  }
249  }
250  return isit ;
251 }
252 
264 void
265 BESServiceRegistry::handles_service( const string &handler,
266  const string &service )
267 {
268  map<string,map<string,service_cmd> >::iterator si ;
269  si = _services.find( service ) ;
270  if( si == _services.end() )
271  {
272  string err = (string)"Registering a handler to handle service "
273  + service + " that has not yet been registered" ;
274  throw BESInternalError( err, __FILE__, __LINE__ ) ;
275  }
276 
277  map<string,map<string,string> >::iterator hi = _handles.find( handler ) ;
278  if( hi == _handles.end() )
279  {
280  map<string,string> services ;
281  services[service] = service ;
282  _handles[handler] = services ;
283  }
284  else
285  {
286  map<string,string>::iterator ci = (*hi).second.find( service ) ;
287  if( ci == (*hi).second.end() )
288  {
289  (*hi).second[service] = service ;
290  }
291  }
292 }
293 
302 bool
304  const string &service )
305 {
306  bool handled = false ;
307  map<string,map<string,string> >::iterator hi = _handles.find( handler ) ;
308  if( hi != _handles.end() )
309  {
310  map<string,string>::iterator si = (*hi).second.find( service ) ;
311  if( si != (*hi).second.end() )
312  {
313  handled = true ;
314  }
315  }
316  return handled ;
317 }
318 
327 void
329  list<string> &services )
330 {
331  map<string,map<string,string> >::iterator hi = _handles.find( handler ) ;
332  if( hi != _handles.end() )
333  {
334  map<string,string>::const_iterator si = (*hi).second.begin() ;
335  map<string,string>::const_iterator se = (*hi).second.end() ;
336  for( ; si != se; si++ )
337  {
338  services.push_back( (*si).second ) ;
339  }
340  }
341 }
342 
351 void
353 {
354  map<string,map<string,service_cmd> >::iterator si = _services.begin() ;
355  map<string,map<string,service_cmd> >::iterator se = _services.end() ;
356  for( ; si != se; si++ )
357  {
358  map<string,string> props ;
359  props["name"] = (*si).first ;
360  info.begin_tag( "serviceDescription", &props ) ;
361  map<string,service_cmd>::iterator ci = (*si).second.begin() ;
362  map<string,service_cmd>::iterator ce = (*si).second.end() ;
363  for( ; ci != ce; ci++ )
364  {
365  map<string,string> cprops ;
366  cprops["name"] = (*ci).first ;
367  info.begin_tag( "command", &cprops ) ;
368  info.add_tag( "description", (*ci).second._description ) ;
369  map<string,string>::iterator fi = (*ci).second._formats.begin() ;
370  map<string,string>::iterator fe = (*ci).second._formats.end() ;
371  for( ; fi != fe; fi++ )
372  {
373  map<string,string> fprops ;
374  fprops["name"] = (*fi).first ;
375  info.add_tag( "format", "", &fprops ) ;
376  }
377  info.end_tag( "command" ) ;
378  }
379  info.end_tag( "serviceDescription" ) ;
380  }
381 }
382 
390 void
391 BESServiceRegistry::dump( ostream &strm ) const
392 {
393  strm << BESIndent::LMarg << "BESServiceRegistry::dump - ("
394  << (void *)this << ")" << endl ;
396  strm << BESIndent::LMarg << "registered services" << endl ;
397  BESIndent::Indent() ;
398  map<string,map<string,service_cmd> >::const_iterator si ;
399  si = _services.begin() ;
400  map<string,map<string,service_cmd> >::const_iterator se ;
401  se = _services.end() ;
402  for( ; si != se; si++ )
403  {
404  strm << BESIndent::LMarg << (*si).first << endl ;
405  BESIndent::Indent() ;
406  map<string,service_cmd>::const_iterator ci = (*si).second.begin() ;
407  map<string,service_cmd>::const_iterator ce = (*si).second.end() ;
408  for( ; ci != ce; ci++ )
409  {
410  strm << BESIndent::LMarg << (*ci).first << endl ;
411  BESIndent::Indent() ;
412  strm << BESIndent::LMarg << "description: "
413  << (*ci).second._description << endl ;
414  strm << BESIndent::LMarg << "formats:" << endl ;
415  BESIndent::Indent() ;
416  map<string,string>::const_iterator fi ;
417  fi = (*ci).second._formats.begin() ;
418  map<string,string>::const_iterator fe ;
419  fe = (*ci).second._formats.end() ;
420  for( ; fi != fe; fi++ )
421  {
422  strm << BESIndent::LMarg << (*fi).first << endl ;
423  }
426  }
428  }
430  strm << BESIndent::LMarg << "services provided by handler" << endl ;
431  BESIndent::Indent() ;
432  map<string,map<string,string> >::const_iterator hi = _handles.begin() ;
433  map<string,map<string,string> >::const_iterator he = _handles.end() ;
434  for( ; hi != he; hi++ )
435  {
436  strm << BESIndent::LMarg << (*hi).first ;
437  map<string,string>::const_iterator hsi = (*hi).second.begin() ;
438  map<string,string>::const_iterator hse = (*hi).second.end() ;
439  bool isfirst = true ;
440  for( ; hsi != hse; hsi++ )
441  {
442  if( !isfirst ) strm << ", " ;
443  else strm << ": " ;
444  strm << (*hsi).first ;
445  isfirst = false ;
446  }
447  strm << endl ;
448  }
451 }
452 
455 {
456  if( _instance == 0 )
457  {
458  _instance = new BESServiceRegistry ;
459  }
460  return _instance ;
461 }
462