Mercurial > pub > Impl
annotate Lib/IMPL/Web/Application.pm @ 272:47db27ed5b43
sync
author | sergey |
---|---|
date | Mon, 28 Jan 2013 17:24:37 +0400 |
parents | a02b110da931 |
children | 546957c50a36 |
rev | line source |
---|---|
49 | 1 package IMPL::Web::Application; |
2 use strict; | |
3 use warnings; | |
4 | |
198 | 5 use CGI; |
6 use Carp qw(carp); | |
233 | 7 use IMPL::Const qw(:prop); |
58 | 8 |
198 | 9 use IMPL::declare { |
10 require => { | |
244 | 11 Locator => 'IMPL::Web::AutoLocator', |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
12 TAction => 'IMPL::Web::Application::Action', |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
13 HttpResponse => 'IMPL::Web::HttpResponse', |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
14 TFactory => '-IMPL::Object::Factory', |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
15 Exception => 'IMPL::Exception', |
230 | 16 InvalidOperationException => '-IMPL::InvalidOperationException', |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
17 Loader => 'IMPL::Code::Loader' |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
18 }, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
19 base => [ |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
20 'IMPL::Config' => '@_', |
198 | 21 'IMPL::Object::Singleton' => '@_' |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
22 ], |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
23 props => [ |
244 | 24 baseUrl => PROP_RW, |
233 | 25 actionFactory => PROP_RW, |
26 handlers => PROP_RW | PROP_LIST, | |
27 security => PROP_RW, | |
28 options => PROP_RW, | |
242 | 29 requestCharset => PROP_RW, |
244 | 30 output => PROP_RW, |
31 location => PROP_RO | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
32 ] |
198 | 33 }; |
49 | 34 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
35 sub CTOR { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
36 my ($this) = @_; |
198 | 37 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
38 die IMPL::InvalidArgumentException->new( "handlers", |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
39 "At least one handler should be supplied" ) |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
40 unless $this->handlers->Count; |
49 | 41 |
244 | 42 $this->baseUrl('/') unless $this->baseUrl; |
43 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
44 $this->actionFactory(TAction) unless $this->actionFactory; |
244 | 45 $this->location(Locator->new(base => $this->baseUrl)); |
62 | 46 } |
47 | |
49 | 48 sub Run { |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
49 my ($this) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
50 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
51 my $handler; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
52 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
53 $handler = _ChainHandler( $_, $handler ) foreach $this->handlers; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
54 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
55 while ( my $query = $this->FetchRequest() ) { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
56 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
57 my $action = $this->actionFactory->new( |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
58 query => $query, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
59 application => $this, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
60 ); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
61 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
62 eval { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
63 my $result = $handler->($action); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
64 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
65 die InvalidOperationException->new( |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
66 "Invalid handlers result. A reference to IMPL::Web::HttpResponse is expexted." |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
67 ) unless eval { $result->isa(HttpResponse) }; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
68 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
69 $result->PrintResponse( $this->output ); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
70 }; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
71 if ($@) { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
72 my $e = $@; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
73 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
74 HttpResponse->InternalError( |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
75 type => 'text/plain', |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
76 charset => 'utf-8', |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
77 body => $e |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
78 )->PrintResponse( $this->output ); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
79 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
80 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
81 } |
49 | 82 } |
83 | |
198 | 84 sub _ChainHandler { |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
85 my ( $handler, $next ) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
86 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
87 if ( ref $handler eq 'CODE' ) { |
198 | 88 return sub { |
89 my ($action) = @_; | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
90 return $handler->( $action, $next ); |
198 | 91 }; |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
92 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
93 elsif ( eval { $handler->can('Invoke') } ) { |
198 | 94 return sub { |
95 my ($action) = @_; | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
96 return $handler->Invoke( $action, $next ); |
198 | 97 }; |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
98 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
99 elsif ( eval { $handler->isa(TFactory) } ) { |
198 | 100 return sub { |
101 my ($action) = @_; | |
102 my $inst = $handler->new(); | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
103 return $inst->Invoke( $action, $next ); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
104 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
105 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
106 elsif ( $handler |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
107 and not ref $handler |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
108 and $handler =~ m/^(-)?(\w+(?:::\w+)*)$/ ) |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
109 { |
198 | 110 my $class = $2; |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
111 if ( not $1 ) { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
112 Loader->safe->Require($class); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
113 die IMPL::InvalidArgumentException->( |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
114 "An invalid handler supplied", $handler |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
115 ) unless $class->can('Invoke'); |
198 | 116 } |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
117 |
198 | 118 return sub { |
119 my ($action) = @_; | |
120 my $inst = $class->new(); | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
121 return $inst->Invoke( $action, $next ); |
198 | 122 }; |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
123 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
124 else { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
125 die new IMPL::InvalidArgumentException( "An invalid handler supplied", |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
126 $handler ); |
198 | 127 } |
128 } | |
129 | |
97 | 130 sub FetchRequest { |
244 | 131 |
132 return; | |
138 | 133 } |
134 | |
49 | 135 1; |
136 | |
52 | 137 __END__ |
138 | |
49 | 139 =pod |
140 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
141 =head1 NAME |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
142 |
244 | 143 C<IMPL::Web::Application> Базовай класс для создания экземпляров приложения |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
144 |
49 | 145 =head1 SYNOPSIS |
146 | |
67 | 147 =begin code |
148 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
149 use IMPL::require { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
150 App => 'IMPL::Web::Application' |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
151 }; |
67 | 152 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
153 my $instance = App->spawn(); # will use ./IMPL/Web/Application.xml as configuration |
67 | 154 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
155 $instance->Run; |
67 | 156 |
157 =end code | |
49 | 158 |
159 =head1 DESCRIPTION | |
160 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
161 Создает экземпляр объекта, который получает и обрабатывает C<HTTP> запрос. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
162 Приложение можно загрузить из C<xml> файла в котором описано состояние свойств, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
163 для этого используется механизм C<IMPL::Serialization>. |
67 | 164 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
165 Приложение представлет собой модульную конструкцию, которая состоит из цепочки |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
166 обработчиков. Цепочка обработчиков вызывается снизу вверх, при этом каждый |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
213
diff
changeset
|
167 обработчик самостоятельно рекурсивно вызывает следующий (более высокого уровня). |
49 | 168 |
244 | 169 См. также C<IMPL::Web::CGIApplication> |
170 | |
49 | 171 =cut |