| 1 | #=============================================================================== | ||
| 2 | # | ||
| 3 | # FILE: Feature.pm | ||
| 4 | # | ||
| 5 | # DESCRIPTION: Abstract application feature class. | ||
| 6 | # | ||
| 7 | # NOTES: --- | ||
| 8 | # AUTHOR: Michael Bochkaryov (RATTLER), <misha@rattler.kiev.ua> | ||
| 9 | # COMPANY: Net.Style | ||
| 10 | # CREATED: 14.09.2008 12:32:03 EEST | ||
| 11 | #=============================================================================== | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | NetSDS::Feature - abstract application feature | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | package NetSDS::Feature::DBI; | ||
| 20 | |||
| 21 | use DBI; | ||
| 22 | use base 'NetSDS::Feature'; | ||
| 23 | |||
| 24 | sub init { | ||
| 25 | my ($self) = @_; | ||
| 26 | |||
| 27 | my $dsn = $self->conf->{dsn}; | ||
| 28 | my $user = $self->conf->{user}; | ||
| 29 | my $passwd = $self->conf->{passwd}; | ||
| 30 | |||
| 31 | $self->{dbconn} = DBI->connect($dsn, $user, $passwd); | ||
| 32 | |||
| 33 | } | ||
| 34 | |||
| 35 | # Sample method - DBI::do proxy | ||
| 36 | sub do { | ||
| 37 | |||
| 38 | my $self = shift @_; | ||
| 39 | return $self->{dbconn}->do(@_); | ||
| 40 | } | ||
| 41 | |||
| 42 | 1; | ||
| 43 | |||
| 44 | |||
| 45 | =head1 DESCRIPTION | ||
| 46 | |||
| 47 | Application C<features> are Perl5 packages with unified API for easy | ||
| 48 | integration of some functionality into NetSDS applications infrastructure. | ||
| 49 | |||
| 50 | C<NetSDS::Feature> module contains superclass for application features | ||
| 51 | providing the following common feature functionality: | ||
| 52 | |||
| 53 | * class construction | ||
| 54 | * initialization stub | ||
| 55 | * logging | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | package NetSDS::Feature; | ||
| 60 | |||
| 61 | 2 | ▶ | use 5.8.0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 62 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 63 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 64 | |||
| 65 | 2 | ▶ | use base qw(Class::Accessor Class::ErrorHandler); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 66 | |||
| 67 | |||
| 68 | use version; our $VERSION = '1.301'; | ||
| 69 | |||
| 70 | #=============================================================================== | ||
| 71 | |||
| 72 | =head1 CLASS METHODS | ||
| 73 | |||
| 74 | =over | ||
| 75 | |||
| 76 | =item B<create($app, $conf)> - feature constructor | ||
| 77 | |||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | #----------------------------------------------------------------------- | ||
| 82 | sub create { | ||
| 83 | |||
| 84 | my ( $class, $app, $conf ) = @_; | ||
| 85 | |||
| 86 | my $self = { | ||
| 87 | app => $app, | ||
| 88 | conf => $conf, | ||
| 89 | }; | ||
| 90 | |||
| 91 | bless $self, $class; | ||
| 92 | |||
| 93 | # Module specific initialization | ||
| 94 | $self->init(); | ||
| 95 | |||
| 96 | return $self; | ||
| 97 | |||
| 98 | } | ||
| 99 | |||
| 100 | #*********************************************************************** | ||
| 101 | |||
| 102 | =item B<init()> - feature initialization | ||
| 103 | |||
| 104 | This method should be rewritten with feature functionality implementation. | ||
| 105 | It's possibly to use application and configuration handlers at this time. | ||
| 106 | |||
| 107 | Example: | ||
| 108 | |||
| 109 | sub init { | ||
| 110 | my ($self) = @_; | ||
| 111 | |||
| 112 | $self->{answer} = $self->conf->{answer} || '42'; | ||
| 113 | |||
| 114 | my $pid = $self->app->pid(); | ||
| 115 | |||
| 116 | if ($self->app->daemon()) { | ||
| 117 | $self->log("info", "Seems we are in a daemon mode"); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | #----------------------------------------------------------------------- | ||
| 124 | |||
| 125 | sub init { | ||
| 126 | |||
| 127 | my ($self) = @_; | ||
| 128 | |||
| 129 | } | ||
| 130 | |||
| 131 | #*********************************************************************** | ||
| 132 | |||
| 133 | |||
| 134 | =back | ||
| 135 | |||
| 136 | =head1 OBJECT METHODS | ||
| 137 | |||
| 138 | =over | ||
| 139 | |||
| 140 | =item B<app()> - application object | ||
| 141 | |||
| 142 | This method allows to use application methods and properties. | ||
| 143 | |||
| 144 | print "Feature included from app: " . $self->app->name; | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | #----------------------------------------------------------------------- | ||
| 149 | __PACKAGE__->mk_ro_accessors('app'); | ||
| 150 | |||
| 151 | #*********************************************************************** | ||
| 152 | |||
| 153 | =item B<conf()> - feature configuration | ||
| 154 | |||
| 155 | This method provides access to feature configuration. | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | #----------------------------------------------------------------------- | ||
| 160 | |||
| 161 | __PACKAGE__->mk_ro_accessors('conf'); | ||
| 162 | |||
| 163 | #*********************************************************************** | ||
| 164 | |||
| 165 | =item B<log($level, $message)> - implements logging | ||
| 166 | |||
| 167 | Example: | ||
| 168 | |||
| 169 | # Write log message | ||
| 170 | $self->log("info", "Application does something interesting."); | ||
| 171 | |||
| 172 | See L<NetSDS::Logger> documentation for details. | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | #----------------------------------------------------------------------- | ||
| 177 | |||
| 178 | sub log { | ||
| 179 | |||
| 180 | my ($self) = shift @_; | ||
| 181 | |||
| 182 | return $self->app->log(@_); | ||
| 183 | |||
| 184 | } | ||
| 185 | |||
| 186 | 1; | ||
| 187 | |||
| 188 | __END__ | ||