1 |
#!/usr/bin/perl |
2 |
|
3 |
## -------------------------------------------------------------------------------- |
4 |
## $Id: AbstractFeeder.pm,v 1.5 2003/02/22 16:53:42 joko Exp $ |
5 |
## -------------------------------------------------------------------------------- |
6 |
## $Log: AbstractFeeder.pm,v $ |
7 |
## Revision 1.5 2003/02/22 16:53:42 joko |
8 |
## minor update: debugging |
9 |
## |
10 |
## Revision 1.4 2003/02/20 21:11:15 joko |
11 |
## renamed module |
12 |
## modified runtime namespace hierarchy |
13 |
## |
14 |
## Revision 1.3 2003/02/14 14:18:36 joko |
15 |
## + new get-/setter-methods |
16 |
## |
17 |
## Revision 1.2 2003/02/11 09:48:57 joko |
18 |
## - moved notice output out of this module |
19 |
## + some fixes |
20 |
## |
21 |
## Revision 1.1 2003/02/09 16:27:06 joko |
22 |
## + initial commit |
23 |
## |
24 |
## -------------------------------------------------------------------------------- |
25 |
|
26 |
|
27 |
package OEF::Script::AbstractFeeder; |
28 |
|
29 |
use strict; |
30 |
use warnings; |
31 |
|
32 |
#use base 'OEF::Component::Task'; |
33 |
use base qw( |
34 |
DesignPattern::Object |
35 |
DesignPattern::Logger |
36 |
); |
37 |
|
38 |
|
39 |
use Data::Dumper; |
40 |
use Getopt::Long; |
41 |
use Data::Transfer::Sync; |
42 |
|
43 |
|
44 |
# get logger instance |
45 |
my $logger = Log::Dispatch::Config->instance; |
46 |
|
47 |
sub usage { |
48 |
my $self = shift; |
49 |
print "\n"; |
50 |
print <<EOU; |
51 |
usage: |
52 |
feed.pl --source=<dbkey> --node=<node> --target=<dbkey> --action=<action> [--mapping=] |
53 |
|
54 |
source: dbkey links to a configuration resource representing a storage (~= database) |
55 |
node: the name of a node on the first level below the source storage |
56 |
target: dbkey links to a configuration resource representing a storage |
57 |
action: <action> is issued on given dbkey as source node |
58 |
sync FULL sync (PULL & PUSH) |
59 |
load|save PULL or PUSH syncs |
60 |
import|export IMPORT and EXPORT syncs |
61 |
mapping: future use: specify name of a perl module / xml file which describes/outlines the mapping |
62 |
EOU |
63 |
exit; |
64 |
} |
65 |
|
66 |
sub readoptions { |
67 |
my $self = shift; |
68 |
|
69 |
GetOptions( |
70 |
'source=s' => \$self->{opt}->{source}, |
71 |
'source-type=s' => \$self->{opt}->{'source-type'}, |
72 |
'source-node=s' => \$self->{opt}->{'source-node'}, |
73 |
'target=s' => \$self->{opt}->{target}, |
74 |
'target-node=s' => \$self->{opt}->{'target-node'}, |
75 |
'action=s' => \$self->{opt}->{action}, |
76 |
'mapping-module=s' => \$self->{opt}->{'mapping-module'}, |
77 |
'prepare' => \$self->{opt}->{prepare}, |
78 |
'fresh' => \$self->{opt}->{fresh}, |
79 |
'help' => \&usage, |
80 |
# v1-API-spec backward compatibility |
81 |
'node=s' => \$self->{opt}->{'node'}, |
82 |
); |
83 |
} |
84 |
|
85 |
sub getoptions { |
86 |
my $self = shift; |
87 |
return $self->{opt}; |
88 |
} |
89 |
|
90 |
sub setoptions { |
91 |
my $self = shift; |
92 |
my $options = shift; |
93 |
# FIXME: is this really true? |
94 |
$self->{opt} = $options; |
95 |
} |
96 |
|
97 |
sub getoptions_old { |
98 |
my $self = shift; |
99 |
GetOptions( |
100 |
'source=s' => \$self->{opt}->{source}, |
101 |
'node=s' => \$self->{opt}->{node}, |
102 |
'target=s' => \$self->{opt}->{target}, |
103 |
'action=s' => \$self->{opt}->{action}, |
104 |
'mapping=s' => \$self->{opt}->{mapping}, |
105 |
'prepare' => \$self->{opt}->{prepare}, |
106 |
'fresh' => \$self->{opt}->{fresh}, |
107 |
); |
108 |
|
109 |
# TODO: use some core function/method (e.g. 'init_hash($hashref, $value, $force = 0)') |
110 |
$self->{opt}->{source} ||= ''; |
111 |
$self->{opt}->{node} ||= ''; |
112 |
$self->{opt}->{target} ||= ''; |
113 |
$self->{opt}->{action} ||= ''; |
114 |
$self->{opt}->{mapping} ||= ''; |
115 |
$self->{opt}->{prepare} ||= ''; |
116 |
$self->{opt}->{fresh} ||= ''; |
117 |
|
118 |
} |
119 |
|
120 |
|
121 |
sub set { |
122 |
my $self = shift; |
123 |
my $data = { @_ }; |
124 |
#return bless $self, $class; |
125 |
foreach (keys %$data) { |
126 |
#print $_, "\n"; |
127 |
$self->{$_} = $data->{$_}; |
128 |
} |
129 |
} |
130 |
|
131 |
sub run { |
132 |
my $self = shift; |
133 |
$self->_before_run(); |
134 |
$self->prepare(); |
135 |
#$self->tellWhatIAmDoing(); |
136 |
#$self->_tellWhatIWillDo(); |
137 |
$self->sync(); |
138 |
} |
139 |
|
140 |
sub prepare { |
141 |
my $self = shift; |
142 |
|
143 |
#print Dumper($self->{opt}); |
144 |
#exit; |
145 |
|
146 |
# TODO: |
147 |
# - move this to Data::Transfer::Sync::checkOptions!!! |
148 |
# - use 'syncable'??? |
149 |
|
150 |
#if ($self->{app}->{config}->{databases}->{$self->{opt}->{target}}->{syncable}) { |
151 |
$self->{opt}->{action} ||= ''; |
152 |
|
153 |
my $mode = ''; |
154 |
my $erase = 0; |
155 |
my $import = 0; |
156 |
$mode = 'PUSH' if $self->{opt}->{action} eq 'save'; |
157 |
$mode = 'PULL' if $self->{opt}->{action} eq 'load'; |
158 |
if ($self->{opt}->{action} eq 'import') { |
159 |
$mode = 'PULL'; |
160 |
$erase = 1; |
161 |
$import = 1; |
162 |
} |
163 |
if ($self->{opt}->{action} eq 'export') { |
164 |
$mode = 'PUSH'; |
165 |
$erase = 1; |
166 |
#$import = 1; |
167 |
} |
168 |
$self->{opt}->{mode} = $mode; |
169 |
$self->{opt}->{erase} = $erase; |
170 |
$self->{opt}->{import} = $import; |
171 |
|
172 |
} |
173 |
|
174 |
sub sync { |
175 |
my $self = shift; |
176 |
|
177 |
# trace |
178 |
#print Dumper($self); |
179 |
#print Dumper($self->{opt}); |
180 |
#exit; |
181 |
|
182 |
# proposal to introduce full backward-compatibility mechanism |
183 |
# "just" have many APIs with different versions accessing the very same core ... |
184 |
# is api-version specified? |
185 |
my $sync_version = $self->{opt}->{sv}; |
186 |
$sync_version ||= 'V1'; |
187 |
#$sync_version = 'V2'; |
188 |
|
189 |
#print "version: $sync_version", "\n"; |
190 |
|
191 |
# create a new synchronization object |
192 |
my $sync = Data::Transfer::Sync->new( 'sync_version' => $sync_version, __parent => $self, verbose => 1 ); |
193 |
|
194 |
# trace |
195 |
#print Dumper($self); |
196 |
#print Dumper($self); |
197 |
#exit; |
198 |
|
199 |
# checks |
200 |
if (!$self->{app}->{storage}->{$self->{opt}->{source}}) { |
201 |
$logger->critical("Sync source storage handle undefined!"); |
202 |
} |
203 |
if (!$self->{app}->{storage}->{$self->{opt}->{target}}) { |
204 |
$logger->critical("Sync target storage handle undefined!"); |
205 |
} |
206 |
|
207 |
# configure the synchronization-object |
208 |
$sync->configure( |
209 |
source => { |
210 |
storage => { |
211 |
#handle => $mapiStorage, |
212 |
handle => $self->{app}->{storage}->{$self->{opt}->{source}}, |
213 |
#isIdentAuthority => $self->{app}->{config}->{{$self->{opt}->{source}}, |
214 |
#isChecksumAuthority => 1, |
215 |
#writeProtected => 1, |
216 |
}, |
217 |
}, |
218 |
target => { |
219 |
storage => { |
220 |
#handle => $ldapStorage, |
221 |
handle => $self->{app}->{storage}->{$self->{opt}->{target}}, |
222 |
#idAuthority => 1, |
223 |
#isChecksumAuthority => 1, |
224 |
#isWriteProtected => 0, |
225 |
}, |
226 |
}, |
227 |
verbose => 1, |
228 |
); |
229 |
|
230 |
# TODO: |
231 |
=pod |
232 |
$sync->configure( |
233 |
'source.storage.handle' => ..., |
234 |
'target.storage.handle' => ..., |
235 |
); |
236 |
=cut |
237 |
|
238 |
# trace |
239 |
#print Dumper($sync); |
240 |
#exit; |
241 |
|
242 |
# read, mungle & check the options |
243 |
$sync->setArguments($self->{opt}); |
244 |
$sync->readArguments(); |
245 |
|
246 |
# trace |
247 |
#print Dumper($sync); |
248 |
|
249 |
# finally, run the sync ... |
250 |
$sync->syncNodes(); |
251 |
|
252 |
} |
253 |
|
254 |
|
255 |
1; |