Class: RibitServlet

Inherits:
HTTPServlet::AbstractServlet
  • Object
show all
Includes:
Assert
Defined in:
lib/ribit/ribit.rb

Constant Summary collapse

@@instance =

The singleton instance of this servlet

nil

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Assert

assert, #assert, assert_nil, #assert_nil, #assert_not_nil, assert_not_nil, raise_exception

Constructor Details

#initialize(server, options) ⇒ RibitServlet

Returns a new instance of RibitServlet.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ribit/ribit.rb', line 65

def initialize( server, options )
  super( server, options ) 

  # RibitConfig and ContentStore are required, 
  # we can't proceed if it is missing
  
  if ( @options[0] == nil )
    raise RibitException, "No RibitConfig (1) found from servlet options", caller
  end
  @ribitConfig = options[0]
  
  if ( options[1] == nil )
    raise RibitException, "No ContentStore (2) found from servlet options", caller
  end
  @contentStore = options[1]
  @ribitData = options[2]
  
  # TODO: servlet should be changed that all actions are initialized only once => reload is not currently realible with caching
  if ( @ribitConfig[RibitConfig::PAGE_CACHE_DIR] != nil )
    dir = @ribitConfig[RibitConfig::PAGE_CACHE_DIR]
    @logger.info( 'Cache path defined => page caching is enabled' )
    
    # make sure dir exists
    FileUtils.mkdir( dir ) unless File.exist?( dir )
    storageHandler = FilePersistentStorageHandler.new( dir )
    
    # TODO: pages in memory should be configurable
    @pageCache = DataStorage.new( 10, -1, storageHandler )
  else
    # no cache path defined => no cache
    @logger.info( 'No cache path defined => page caching is disabled' )
    @pageCache = nil
  end
  
  # create all data structures  
  init
  
end

Class Method Details

.get_instance(config, *options) ⇒ Object

Return the one and only instanceof RibitServlet. WEBrick calls this method every time when request is made.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ribit/ribit.rb', line 41

def self.get_instance( config, *options )
  
  # 'config' is instance of WEBrick::HTTPServer, stored to @server
  # config[:Logger] contains ref to WEBrick::Log, stored to @logger
  # 'options' is Array
  if ( @@instance == nil )
    logger = config[:Logger]
     logger.info( 'Creating a new servlet instance' )
  
    begin
      @@instance = self.new(config, options)
    rescue Exception => error
      logger.fatal( "Got error during servlet creation. No Servlet created" )
      logger.fatal( format_exception( error ) )

      # throw upwards, don't know what to do
      raise error
    end
  end
  
  return @@instance
end

Instance Method Details

#do_GET(req, res) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ribit/ribit.rb', line 105

def do_GET(req, res)
  
  begin
    @logger.debug( "Processing GET" )
  
    pageRequest = PageRequest.new( req )
    pageResponse = PageResponse.new( res )
    
    buildPage( pageRequest, pageResponse )

    # used only for debugging
    #@logger.debug( "Page=" + res.body )      
  rescue Exception => error
    @logger.fatal( 'Got error during service request' )
    @logger.fatal( format_exception( error ) )

    # throw upwards, don't know what to do
    raise error, error.message, error.backtrace
  end
  
  #@logger.debug( "content type = " + res['Content-Type'] )
  #@logger.debug( "body = " + res.body )

end

#do_POST(req, res) ⇒ Object



131
132
133
134
# File 'lib/ribit/ribit.rb', line 131

def do_POST( req, res )
  # forward call
  return do_GET( req, res )
end