1 | package LATMOS::Accounts::SynchAccess::base; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use vars qw($AUTOLOAD); |
---|
6 | use Carp; |
---|
7 | use LATMOS::Accounts::Log; |
---|
8 | |
---|
9 | our $VERSION = (q$Rev: 1405 $ =~ /^Rev: (\d+) /)[0]; |
---|
10 | |
---|
11 | =head1 NAME |
---|
12 | |
---|
13 | LATMOS::Accounts::SynchAccess::base - Fake base object for sync access |
---|
14 | |
---|
15 | =head1 DESCRIPTION |
---|
16 | |
---|
17 | This module fake a base object to send same actions tu multiple base. This is |
---|
18 | used for actions basic synchronisation process cannot handle. |
---|
19 | |
---|
20 | This module contain common functions between Base and Object managment. |
---|
21 | |
---|
22 | =head1 FUNCTIONS |
---|
23 | |
---|
24 | =cut |
---|
25 | |
---|
26 | sub AUTOLOAD { |
---|
27 | my ($self, @args) = @_; |
---|
28 | my ($constname, $sub) = $AUTOLOAD =~ m/(.*)::([^:]+)/; |
---|
29 | if ($sub =~ /^(wexported)/) { |
---|
30 | $self->_no_trap($sub, @args); |
---|
31 | } elsif ($sub =~ /^(check_password)$/) { |
---|
32 | $self->_run_first($sub, @args); |
---|
33 | } else { |
---|
34 | $self->_trap_false($sub, @args); |
---|
35 | } |
---|
36 | } |
---|
37 | |
---|
38 | =head2 new( \@bases, %config ) |
---|
39 | |
---|
40 | Create a new SynchAccess instance over given bases |
---|
41 | |
---|
42 | C<%config>: |
---|
43 | |
---|
44 | =over 4 |
---|
45 | |
---|
46 | =item cb |
---|
47 | |
---|
48 | Callback, see L<SetCB> |
---|
49 | |
---|
50 | =back |
---|
51 | |
---|
52 | =cut |
---|
53 | |
---|
54 | sub new { |
---|
55 | my ($class, $bases, %config) = @_; |
---|
56 | bless { |
---|
57 | bases => [ @{ $bases } ], |
---|
58 | %config |
---|
59 | }, $class; |
---|
60 | } |
---|
61 | |
---|
62 | =head2 bases |
---|
63 | |
---|
64 | Return lists of bases in ths instance. |
---|
65 | |
---|
66 | =cut |
---|
67 | |
---|
68 | sub bases { |
---|
69 | my ($self) = @_; |
---|
70 | @{ $self->{bases} || []} |
---|
71 | } |
---|
72 | |
---|
73 | =head2 SetCB ( $callback ) |
---|
74 | |
---|
75 | Set a callback to receive the object being call, return the previous one. |
---|
76 | |
---|
77 | =cut |
---|
78 | |
---|
79 | sub SetCB { |
---|
80 | my ( $self, $cb ) = @_; |
---|
81 | |
---|
82 | my $oldCB = $self->{cb}; |
---|
83 | $self->{cb} = $cb; |
---|
84 | return $oldCB; |
---|
85 | } |
---|
86 | |
---|
87 | sub _return_all { |
---|
88 | my ($self, $sub, @args) = @_; |
---|
89 | my @allres; |
---|
90 | foreach my $ba ($self->bases) { |
---|
91 | my @res = $_->$sub(@args); |
---|
92 | $self->{cb}->($ba, @res) if($self->{cb}); |
---|
93 | push(@allres, @res); |
---|
94 | } |
---|
95 | @allres; |
---|
96 | } |
---|
97 | |
---|
98 | sub _run_first { |
---|
99 | my ($self, $sub, @args) = @_; |
---|
100 | my ($fbase) = $self->bases; |
---|
101 | $fbase->$sub(@args); |
---|
102 | } |
---|
103 | |
---|
104 | sub _no_trap { |
---|
105 | my ($self, $sub, @args) = @_; |
---|
106 | foreach ($self->bases) { |
---|
107 | $_->$sub(@args); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | sub _trap_false { |
---|
112 | my ($self, $sub, @args) = @_; |
---|
113 | my $failure = 0; |
---|
114 | foreach ($self->bases) { |
---|
115 | my $res = $_->$sub(@args); |
---|
116 | $self->{cb}->($_, $res) if($self->{cb}); |
---|
117 | $res or do { |
---|
118 | la_log(LA_ERR, "SyncA: Erreur call $sub() for %s", $_); |
---|
119 | $failure = 1; |
---|
120 | }; |
---|
121 | } |
---|
122 | $failure ? 0 : 1; |
---|
123 | } |
---|
124 | |
---|
125 | sub _trap_true { |
---|
126 | my ($self, $sub, @args) = @_; |
---|
127 | my $failure = 0; |
---|
128 | foreach ($self->bases) { |
---|
129 | my $res = $_->$sub(@args) and $failure = 1; |
---|
130 | $self->{cb}->($_, $res) if($self->{cb}); |
---|
131 | } |
---|
132 | $failure ? 0 : 1; |
---|
133 | } |
---|
134 | |
---|
135 | =head2 rename_object |
---|
136 | |
---|
137 | =cut |
---|
138 | |
---|
139 | sub rename_object { |
---|
140 | my ($self, $otype, $uid, $newuid) = @_; |
---|
141 | foreach ($self->bases) { |
---|
142 | if ($_->is_supported_object($otype)) { |
---|
143 | my $res = $_->rename_object($otype, $uid, $newuid); |
---|
144 | $self->{cb}->($_, $res) if($self->{cb}); |
---|
145 | $res or do { |
---|
146 | la_log(LA_ERR, |
---|
147 | "Cannot get object $otype/$uid in %s/%s", |
---|
148 | $_->type, |
---|
149 | $_->label |
---|
150 | ); |
---|
151 | return; |
---|
152 | }; |
---|
153 | } |
---|
154 | } |
---|
155 | return 1; |
---|
156 | } |
---|
157 | |
---|
158 | sub DESTROY {} |
---|
159 | |
---|
160 | 1; |
---|
161 | |
---|
162 | __END__ |
---|
163 | |
---|
164 | =head1 SEE ALSO |
---|
165 | |
---|
166 | L<LATMOS::Accounts>, L<LATMOS::Accounts::Bases> |
---|
167 | |
---|
168 | =head1 AUTHOR |
---|
169 | |
---|
170 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
171 | |
---|
172 | =head1 COPYRIGHT AND LICENSE |
---|
173 | |
---|
174 | Copyright (C) 2012 CNRS SA/CETP/LATMOS |
---|
175 | |
---|
176 | This library is free software; you can redistribute it and/or modify |
---|
177 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
178 | at your option, any later version of Perl 5 you may have available. |
---|
179 | |
---|
180 | =cut |
---|