forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcci.xml
More file actions
1195 lines (953 loc) · 51.1 KB
/
cci.xml
File metadata and controls
1195 lines (953 loc) · 51.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<chapter xml:id="cci"
xmlns="http://docbook.org/ns/docbook" version="5.0"
xmlns:xl="http://www.w3.org/1999/xlink"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://docbook.org/ns/docbook http://www.docbook.org/xml/5.0/xsd/docbook.xsd
http://www.w3.org/1999/xlink http://www.docbook.org/xml/5.0/xsd/xlink.xsd">
<title>JCA CCI</title>
<section xml:id="cci-introduction">
<title>Introduction</title>
<para>Java EE provides a specification to standardize access to enterprise
information systems (EIS): the JCA (J2EE Connector Architecture). This
specification is divided into several different parts:</para>
<itemizedlist>
<listitem>
<para>SPI (Service provider interfaces) that the connector provider
must implement. These interfaces constitute a resource adapter which
can be deployed on a Java EE application server. In such a scenario, the
server manages connection pooling, transaction and security (managed
mode). The application server is also responsible for managing the
configuration, which is held outside the client application. A
connector can be used without an application server as well; in this
case, the application must configure it directly (non-managed
mode).</para>
</listitem>
<listitem>
<para>CCI (Common Client Interface) that an application can use to
interact with the connector and thus communicate with an EIS. An API
for local transaction demarcation is provided as well.</para>
</listitem>
</itemizedlist>
<para>The aim of the Spring CCI support is to provide classes to access a
CCI connector in typical Spring style, leveraging the Spring Framework's
general resource and transaction management facilities.</para>
<note>
<para>The client side of connectors doesn't alway use CCI. Some
connectors expose their own APIs, only providing JCA resource adapter to
use the system contracts of a Java EE container (connection pooling, global
transactions, security). Spring does not offer special support for such
connector-specific APIs.</para>
</note>
</section>
<section xml:id="cci-config">
<title>Configuring CCI</title>
<section xml:id="cci-config-connector">
<title>Connector configuration</title>
<para>The base resource to use JCA CCI is the
<interfacename>ConnectionFactory</interfacename> interface. The
connector used must provide an implementation of this interface.</para>
<para>To use your connector, you can deploy it on your application
server and fetch the <interfacename>ConnectionFactory</interfacename>
from the server's JNDI environment (managed mode). The connector must be
packaged as a RAR file (resource adapter archive) and contain a
<filename>ra.xml</filename> file to describe its deployment
characteristics. The actual name of the resource is specified when you
deploy it. To access it within Spring, simply use Spring's
<classname>JndiObjectFactoryBean</classname> /
<literal><jee:jndi-lookup></literal> fetch the factory by its JNDI
name.</para>
<para>Another way to use a connector is to embed it in your application
(non-managed mode), not using an application server to deploy and
configure it. Spring offers the possibility to configure a connector as
a bean, through a provided <literal>FactoryBean</literal>
(<classname>LocalConnectionFactoryBean</classname>). In this manner, you
only need the connector library in the classpath (no RAR file and no
<filename>ra.xml</filename> descriptor needed). The library must be
extracted from the connector's RAR file, if necessary.</para>
<para>Once you have got access to your
<interfacename>ConnectionFactory</interfacename> instance, you can
inject it into your components. These components can either be coded
against the plain CCI API or leverage Spring's support classes for CCI
access (e.g. <classname>CciTemplate</classname>).</para>
<note>
<para>When you use a connector in non-managed mode, you can't use
global transactions because the resource is never enlisted / delisted
in the current global transaction of the current thread. The resource
is simply not aware of any global Java EE transactions that might be
running.</para>
</note>
</section>
<section xml:id="cci-config-connectionfactory">
<title><interfacename>ConnectionFactory</interfacename> configuration in
Spring</title>
<para>In order to make connections to the EIS, you need to obtain a
<interfacename>ConnectionFactory</interfacename> from the application
server if you are in a managed mode, or directly from Spring if you are
in a non-managed mode.</para>
<para>In a managed mode, you access a
<interfacename>ConnectionFactory</interfacename> from JNDI; its
properties will be configured in the application server.</para>
<programlisting language="xml"><jee:jndi-lookup id="eciConnectionFactory" jndi-name="eis/cicseci"/></programlisting>
<para>In non-managed mode, you must configure the
<interfacename>ConnectionFactory</interfacename> you want to use in the
configuration of Spring as a JavaBean. The
<classname>LocalConnectionFactoryBean</classname> class offers this
setup style, passing in the
<classname>ManagedConnectionFactory</classname> implementation of your
connector, exposing the application-level CCI
<interfacename>ConnectionFactory</interfacename>.</para>
<programlisting language="xml"><bean id="eciManagedConnectionFactory" class="com.ibm.connector2.cics.ECIManagedConnectionFactory">
<property name="serverName" value="TXSERIES"/>
<property name="connectionURL" value="tcp://localhost/"/>
<property name="portNumber" value="2006"/>
</bean>
<bean id="eciConnectionFactory" class="org.springframework.jca.support.LocalConnectionFactoryBean">
<property name="managedConnectionFactory" ref="eciManagedConnectionFactory"/>
</bean></programlisting>
<note>
<para>You can't directly instantiate a specific
<interfacename>ConnectionFactory</interfacename>. You need to go
through the corresponding implementation of the
<classname>ManagedConnectionFactory</classname> interface for your
connector. This interface is part of the JCA SPI specification.</para>
</note>
</section>
<section xml:id="cci-config-cci-connections">
<title>Configuring CCI connections</title>
<para>JCA CCI allow the developer to configure the connections to the
EIS using the <interfacename>ConnectionSpec</interfacename>
implementation of your connector. In order to configure its properties,
you need to wrap the target connection factory with a dedicated adapter,
<classname>ConnectionSpecConnectionFactoryAdapter</classname>. So, the
dedicated <interfacename>ConnectionSpec</interfacename> can be
configured with the property <literal>connectionSpec</literal> (as an
inner bean).</para>
<para>This property is not mandatory because the CCI
<interfacename>ConnectionFactory</interfacename> interface defines two
different methods to obtain a CCI connection. Some of the
<interfacename>ConnectionSpec</interfacename> properties can often be
configured in the application server (in managed mode) or on the
corresponding local <classname>ManagedConnectionFactory</classname>
implementation.</para>
<programlisting language="java">public interface ConnectionFactory implements Serializable, Referenceable {
...
Connection getConnection() throws ResourceException;
Connection getConnection(ConnectionSpec connectionSpec) throws ResourceException;
...
}</programlisting>
<para>Spring provides a
<classname>ConnectionSpecConnectionFactoryAdapter</classname> that
allows for specifying a <interfacename>ConnectionSpec</interfacename>
instance to use for all operations on a given factory. If the adapter's
<literal>connectionSpec</literal> property is specified, the adapter
uses the <literal>getConnection</literal> variant with the
<interfacename>ConnectionSpec</interfacename> argument, otherwise
the variant without argument.</para>
<programlisting language="xml"><bean id="managedConnectionFactory"
class="com.sun.connector.cciblackbox.CciLocalTxManagedConnectionFactory">
<property name="connectionURL" value="jdbc:hsqldb:hsql://localhost:9001"/>
<property name="driverName" value="org.hsqldb.jdbcDriver"/>
</bean>
<bean id="targetConnectionFactory"
class="org.springframework.jca.support.LocalConnectionFactoryBean">
<property name="managedConnectionFactory" ref="managedConnectionFactory"/>
</bean>
<bean id="connectionFactory"
class="org.springframework.jca.cci.connection.ConnectionSpecConnectionFactoryAdapter">
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
<property name="connectionSpec">
<bean class="com.sun.connector.cciblackbox.CciConnectionSpec">
<property name="user" value="sa"/>
<property name="password" value=""/>
</bean>
</property>
</bean></programlisting>
</section>
<section xml:id="cci-config-single-connection">
<title>Using a single CCI connection</title>
<para>If you want to use a single CCI connection, Spring provides a
further <interfacename>ConnectionFactory</interfacename> adapter to
manage this. The <classname>SingleConnectionFactory</classname> adapter
class will open a single connection lazily and close it when this bean
is destroyed at application shutdown. This class will expose special
<interfacename>Connection</interfacename> proxies that behave
accordingly, all sharing the same underlying physical connection.</para>
<programlisting language="xml"><bean id="eciManagedConnectionFactory"
class="com.ibm.connector2.cics.ECIManagedConnectionFactory">
<property name="serverName" value="TEST"/>
<property name="connectionURL" value="tcp://localhost/"/>
<property name="portNumber" value="2006"/>
</bean>
<bean id="targetEciConnectionFactory"
class="org.springframework.jca.support.LocalConnectionFactoryBean">
<property name="managedConnectionFactory" ref="eciManagedConnectionFactory"/>
</bean>
<bean id="eciConnectionFactory"
class="org.springframework.jca.cci.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="targetEciConnectionFactory"/>
</bean></programlisting>
<note>
<para>This <interfacename>ConnectionFactory</interfacename> adapter
cannot directly be configured with a
<interfacename>ConnectionSpec</interfacename>. Use an intermediary
<classname>ConnectionSpecConnectionFactoryAdapter</classname> that the
<classname>SingleConnectionFactory</classname> talks to if you require
a single connection for a specific
<interfacename>ConnectionSpec</interfacename>.</para>
</note>
</section>
</section>
<section xml:id="cci-using">
<title>Using Spring's CCI access support</title>
<section xml:id="cci-record-creator">
<title>Record conversion</title>
<para>One of the aims of the JCA CCI support is to provide convenient
facilities for manipulating CCI records. The developer can specify the
strategy to create records and extract datas from records, for use with
Spring's <classname>CciTemplate</classname>. The following interfaces
will configure the strategy to use input and output records if you don't
want to work with records directly in your application.</para>
<para>In order to create an input <interfacename>Record</interfacename>,
the developer can use a dedicated implementation of the
<interfacename>RecordCreator</interfacename> interface.</para>
<programlisting language="java">public interface RecordCreator {
Record createRecord(RecordFactory recordFactory) throws ResourceException, DataAccessException;
}</programlisting>
<para>As you can see, the <literal>createRecord(..)</literal> method
receives a <interfacename>RecordFactory</interfacename> instance as
parameter, which corresponds to the
<interfacename>RecordFactory</interfacename> of the
<interfacename>ConnectionFactory</interfacename> used. This reference
can be used to create <interfacename>IndexedRecord</interfacename> or
<interfacename>MappedRecord</interfacename> instances. The following
sample shows how to use the <interfacename>RecordCreator</interfacename>
interface and indexed/mapped records.</para>
<programlisting language="java">public class MyRecordCreator implements RecordCreator {
public Record createRecord(RecordFactory recordFactory) throws ResourceException {
IndexedRecord input = recordFactory.createIndexedRecord("input");
input.add(new Integer(id));
return input;
}
}</programlisting>
<para>An output <interfacename>Record</interfacename> can be used to
receive data back from the EIS. Hence, a specific implementation of the
<interfacename>RecordExtractor</interfacename> interface can be passed
to Spring's <classname>CciTemplate</classname> for extracting data from
the output <interfacename>Record</interfacename>.</para>
<programlisting language="java">public interface RecordExtractor {
Object extractData(Record record) throws ResourceException, SQLException, DataAccessException;
}</programlisting>
<para>The following sample shows how to use the
<interfacename>RecordExtractor</interfacename> interface.</para>
<programlisting language="java">public class MyRecordExtractor implements RecordExtractor {
public Object extractData(Record record) throws ResourceException {
CommAreaRecord commAreaRecord = (CommAreaRecord) record;
String str = new String(commAreaRecord.toByteArray());
String field1 = string.substring(0,6);
String field2 = string.substring(6,1);
return new OutputObject(Long.parseLong(field1), field2);
}
}</programlisting>
</section>
<section xml:id="cci-using-template">
<title>The <classname>CciTemplate</classname></title>
<para>The <classname>CciTemplate</classname> is the central class of the
core CCI support package
(<literal>org.springframework.jca.cci.core</literal>). It simplifies the
use of CCI since it handles the creation and release of resources. This
helps to avoid common errors like forgetting to always close the
connection. It cares for the lifecycle of connection and interaction
objects, letting application code focus on generating input records from
application data and extracting application data from output
records.</para>
<para>The JCA CCI specification defines two distinct methods to call
operations on an EIS. The CCI <interfacename>Interaction</interfacename>
interface provides two execute method signatures:</para>
<programlisting language="java">public interface javax.resource.cci.Interaction {
...
boolean execute(InteractionSpec spec, Record input, Record output) throws ResourceException;
Record execute(InteractionSpec spec, Record input) throws ResourceException;
...
}</programlisting>
<para>Depending on the template method called,
<classname>CciTemplate</classname> will know which
<literal>execute</literal> method to call on the interaction. In any
case, a correctly initialized
<interfacename>InteractionSpec</interfacename> instance is
mandatory.</para>
<para><literal>CciTemplate.execute(..)</literal> can be used in two
ways:</para>
<itemizedlist>
<listitem>
<para>With direct <interfacename>Record</interfacename> arguments.
In this case, you simply need to pass the CCI input record in, and
the returned object be the corresponding CCI output record.</para>
</listitem>
<listitem>
<para>With application objects, using record mapping. In this case,
you need to provide corresponding
<interfacename>RecordCreator</interfacename> and
<interfacename>RecordExtractor</interfacename> instances.</para>
</listitem>
</itemizedlist>
<para>With the first approach, the following methods of the template
will be used. These methods directly correspond to those on the
<interfacename>Interaction</interfacename> interface.</para>
<programlisting language="java">public class CciTemplate implements CciOperations {
public Record execute(InteractionSpec spec, Record inputRecord)
throws DataAccessException { ... }
public void execute(InteractionSpec spec, Record inputRecord, Record outputRecord)
throws DataAccessException { ... }
}</programlisting>
<para>With the second approach, we need to specify the record creation
and record extraction strategies as arguments. The interfaces used are
those describe in the previous section on record conversion. The
corresponding <classname>CciTemplate</classname> methods are the
following:</para>
<programlisting language="java">public class CciTemplate implements CciOperations {
public Record execute(InteractionSpec spec, RecordCreator inputCreator)
throws DataAccessException { ... }
public Object execute(InteractionSpec spec, Record inputRecord, RecordExtractor outputExtractor)
throws DataAccessException { ... }
public Object execute(InteractionSpec spec, RecordCreator creator, RecordExtractor extractor)
throws DataAccessException { ... }
}</programlisting>
<para>Unless the <literal>outputRecordCreator</literal> property is set
on the template (see the following section), every method will call the
corresponding <literal>execute</literal> method of the CCI
<interfacename>Interaction</interfacename> with two parameters:
<interfacename>InteractionSpec</interfacename> and input
<interfacename>Record</interfacename>, receiving an output
<interfacename>Record</interfacename> as return value.</para>
<para><classname>CciTemplate</classname> also provides methods to create
<literal>IndexRecord</literal> and <literal>MappedRecord</literal>
outside a <interfacename>RecordCreator</interfacename> implementation,
through its <literal>createIndexRecord(..)</literal> and
<literal>createMappedRecord(..)</literal> methods. This can be used
within DAO implementations to create
<interfacename>Record</interfacename> instances to pass into
corresponding <literal>CciTemplate.execute(..)</literal> methods.</para>
<programlisting language="java">public class CciTemplate implements CciOperations {
public IndexedRecord createIndexedRecord(String name) throws DataAccessException { ... }
public MappedRecord createMappedRecord(String name) throws DataAccessException { ... }
}</programlisting>
</section>
<section xml:id="cci-using-dao">
<title>DAO support</title>
<para>Spring's CCI support provides a abstract class for DAOs,
supporting injection of a
<interfacename>ConnectionFactory</interfacename> or a
<classname>CciTemplate</classname> instances. The name of the class is
<classname>CciDaoSupport</classname>: It provides simple
<literal>setConnectionFactory</literal> and
<literal>setCciTemplate</literal> methods. Internally, this class will
create a <classname>CciTemplate</classname> instance for a passed-in
<interfacename>ConnectionFactory</interfacename>, exposing it to
concrete data access implementations in subclasses.</para>
<programlisting language="java">public abstract class CciDaoSupport {
public void setConnectionFactory(ConnectionFactory connectionFactory) { ... }
public ConnectionFactory getConnectionFactory() { ... }
public void setCciTemplate(CciTemplate cciTemplate) { ... }
public CciTemplate getCciTemplate() { ... }
}</programlisting>
</section>
<section xml:id="automatic-output-generation">
<title>Automatic output record generation</title>
<para>If the connector used only supports the
<methodname>Interaction.execute(..)</methodname> method with input and
output records as parameters (that is, it requires the desired output
record to be passed in instead of returning an appropriate output
record), you can set the <literal>outputRecordCreator</literal> property
of the <classname>CciTemplate</classname> to automatically generate an
output record to be filled by the JCA connector when the response is
received. This record will be then returned to the caller of the
template.</para>
<para>This property simply holds an implementation of the
<interfacename>RecordCreator</interfacename> interface, used for that
purpose. The <interfacename>RecordCreator</interfacename> interface has
already been discussed in <xref linkend="cci-record-creator" />. The
<literal>outputRecordCreator</literal> property must be directly
specified on the <classname>CciTemplate</classname>. This could be done
in the application code like so:</para>
<programlisting language="java">cciTemplate.setOutputRecordCreator(new EciOutputRecordCreator());</programlisting>
<para>Or (recommended) in the Spring configuration, if the
<classname>CciTemplate</classname> is configured as a dedicated bean
instance:</para>
<programlisting language="xml"><bean id="eciOutputRecordCreator" class="eci.EciOutputRecordCreator"/>
<bean id="cciTemplate" class="org.springframework.jca.cci.core.CciTemplate">
<property name="connectionFactory" ref="eciConnectionFactory"/>
<property name="outputRecordCreator" ref="eciOutputRecordCreator"/>
</bean></programlisting>
<note>
<para>As the <classname>CciTemplate</classname> class is thread-safe,
it will usually be configured as a shared instance.</para>
</note>
</section>
<section xml:id="template-summary">
<title>Summary</title>
<para>The following table summarizes the mechanisms of the
<classname>CciTemplate</classname> class and the corresponding methods
called on the CCI <interfacename>Interaction</interfacename>
interface:<table frame="all" xml:id="cci-interaction-execute-methods">
<title>Usage of <interfacename>Interaction</interfacename> execute
methods</title>
<tgroup cols="3">
<thead>
<row>
<entry align="center">CciTemplate method signature</entry>
<entry align="center">CciTemplate outputRecordCreator
property</entry>
<entry align="center">execute method called on the CCI
Interaction</entry>
</row>
</thead>
<tbody>
<row>
<entry align="center">Record execute(InteractionSpec,
Record)</entry>
<entry align="center">not set</entry>
<entry align="center">Record execute(InteractionSpec,
Record)</entry>
</row>
<row>
<entry align="center">Record execute(InteractionSpec,
Record)</entry>
<entry align="center">set</entry>
<entry align="center">boolean execute(InteractionSpec, Record,
Record)</entry>
</row>
<row>
<entry align="center">void execute(InteractionSpec, Record,
Record)</entry>
<entry align="center">not set</entry>
<entry align="center">void execute(InteractionSpec, Record,
Record)</entry>
</row>
<row>
<entry align="center">void execute(InteractionSpec, Record,
Record)</entry>
<entry align="center">set</entry>
<entry align="center">void execute(InteractionSpec, Record,
Record)</entry>
</row>
<row>
<entry align="center">Record execute(InteractionSpec,
RecordCreator)</entry>
<entry align="center">not set</entry>
<entry align="center">Record execute(InteractionSpec,
Record)</entry>
</row>
<row>
<entry align="center">Record execute(InteractionSpec,
RecordCreator)</entry>
<entry align="center">set</entry>
<entry align="center">void execute(InteractionSpec, Record,
Record)</entry>
</row>
<row>
<entry align="center">Record execute(InteractionSpec, Record,
RecordExtractor)</entry>
<entry align="center">not set</entry>
<entry align="center">Record execute(InteractionSpec,
Record)</entry>
</row>
<row>
<entry align="center">Record execute(InteractionSpec, Record,
RecordExtractor)</entry>
<entry align="center">set</entry>
<entry align="center">void execute(InteractionSpec, Record,
Record)</entry>
</row>
<row>
<entry align="center">Record execute(InteractionSpec,
RecordCreator, RecordExtractor)</entry>
<entry align="center">not set</entry>
<entry align="center">Record execute(InteractionSpec,
Record)</entry>
</row>
<row>
<entry align="center">Record execute(InteractionSpec,
RecordCreator, RecordExtractor)</entry>
<entry align="center">set</entry>
<entry align="center">void execute(InteractionSpec, Record,
Record)</entry>
</row>
</tbody>
</tgroup>
</table></para>
</section>
<section xml:id="cci-straight">
<title>Using a CCI <interfacename>Connection</interfacename> and
<interfacename>Interaction</interfacename> directly</title>
<para><classname>CciTemplate</classname> also offers the possibility to
work directly with CCI connections and interactions, in the same manner
as <classname>JdbcTemplate</classname> and
<classname>JmsTemplate</classname>. This is useful when you want to
perform multiple operations on a CCI connection or interaction, for
example.</para>
<para>The interface <interfacename>ConnectionCallback</interfacename>
provides a CCI <interfacename>Connection</interfacename> as argument, in
order to perform custom operations on it, plus the CCI
<interfacename>ConnectionFactory</interfacename> which the
<interfacename>Connection</interfacename> was created with. The latter
can be useful for example to get an associated
<interfacename>RecordFactory</interfacename> instance and create
indexed/mapped records, for example.</para>
<programlisting language="java">public interface ConnectionCallback {
Object doInConnection(Connection connection, ConnectionFactory connectionFactory)
throws ResourceException, SQLException, DataAccessException;
}</programlisting>
<para>The interface <interfacename>InteractionCallback</interfacename>
provides the CCI <interfacename>Interaction</interfacename>, in order to
perform custom operations on it, plus the corresponding CCI
<interfacename>ConnectionFactory</interfacename>.</para>
<programlisting language="java">public interface InteractionCallback {
Object doInInteraction(Interaction interaction, ConnectionFactory connectionFactory)
throws ResourceException, SQLException, DataAccessException;
}</programlisting>
<note>
<para><interfacename>InteractionSpec</interfacename> objects can
either be shared across multiple template calls or newly created
inside every callback method. This is completely up to the DAO
implementation.</para>
</note>
</section>
<section xml:id="cci-template-example">
<title>Example for <classname>CciTemplate</classname> usage</title>
<para>In this section, the usage of the
<classname>CciTemplate</classname> will be shown to acces to a CICS with
ECI mode, with the IBM CICS ECI connector.</para>
<para>Firstly, some initializations on the CCI
<interfacename>InteractionSpec</interfacename> must be done to specify
which CICS program to access and how to interact with it.</para>
<programlisting language="java">ECIInteractionSpec interactionSpec = new ECIInteractionSpec();
interactionSpec.setFunctionName("MYPROG");
interactionSpec.setInteractionVerb(ECIInteractionSpec.SYNC_SEND_RECEIVE);</programlisting>
<para>Then the program can use CCI via Spring's template and specify
mappings between custom objects and CCI
<literal>Records</literal>.</para>
<programlisting language="java">public class MyDaoImpl extends CciDaoSupport implements MyDao {
public OutputObject getData(InputObject input) {
ECIInteractionSpec interactionSpec = ...;
OutputObject output = (ObjectOutput) getCciTemplate().execute(interactionSpec,
new RecordCreator() {
public Record createRecord(RecordFactory recordFactory) throws ResourceException {
return new CommAreaRecord(input.toString().getBytes());
}
},
new RecordExtractor() {
public Object extractData(Record record) throws ResourceException {
CommAreaRecord commAreaRecord = (CommAreaRecord)record;
String str = new String(commAreaRecord.toByteArray());
String field1 = string.substring(0,6);
String field2 = string.substring(6,1);
return new OutputObject(Long.parseLong(field1), field2);
}
});
return output;
}
}</programlisting>
<para>As discussed previously, callbacks can be used to work directly on
CCI connections or interactions.</para>
<programlisting language="java">public class MyDaoImpl extends CciDaoSupport implements MyDao {
public OutputObject getData(InputObject input) {
ObjectOutput output = (ObjectOutput) getCciTemplate().execute(
new ConnectionCallback() {
public Object doInConnection(Connection connection, ConnectionFactory factory)
throws ResourceException {
<lineannotation>// do something...</lineannotation>
}
});
}
return output;
}
}</programlisting>
<note>
<para>With a <interfacename>ConnectionCallback</interfacename>, the
<interfacename>Connection</interfacename> used will be managed and
closed by the <classname>CciTemplate</classname>, but any interactions
created on the connection must be managed by the callback
implementation.</para>
</note>
<para>For a more specific callback, you can implement an
<interfacename>InteractionCallback</interfacename>. The passed-in
<interfacename>Interaction</interfacename> will be managed and closed by
the <classname>CciTemplate</classname> in this case.</para>
<programlisting language="java">public class MyDaoImpl extends CciDaoSupport implements MyDao {
public String getData(String input) {
ECIInteractionSpec interactionSpec = ...;
String output = (String) getCciTemplate().execute(interactionSpec,
new InteractionCallback() {
public Object doInInteraction(Interaction interaction, ConnectionFactory factory)
throws ResourceException {
Record input = new CommAreaRecord(inputString.getBytes());
Record output = new CommAreaRecord();
interaction.execute(holder.getInteractionSpec(), input, output);
return new String(output.toByteArray());
}
});
return output;
}
}</programlisting>
<para>For the examples above, the corresponding configuration of the
involved Spring beans could look like this in non-managed mode:</para>
<programlisting language="xml"><bean id="managedConnectionFactory" class="com.ibm.connector2.cics.ECIManagedConnectionFactory">
<property name="serverName" value="TXSERIES"/>
<property name="connectionURL" value="local:"/>
<property name="userName" value="CICSUSER"/>
<property name="password" value="CICS"/>
</bean>
<bean id="connectionFactory" class="org.springframework.jca.support.LocalConnectionFactoryBean">
<property name="managedConnectionFactory" ref="managedConnectionFactory"/>
</bean>
<bean id="component" class="mypackage.MyDaoImpl">
<property name="connectionFactory" ref="connectionFactory"/>
</bean></programlisting>
<para>In managed mode (that is, in a Java EE environment), the
configuration could look as follows:</para>
<programlisting language="xml"><jee:jndi-lookup id="connectionFactory" jndi-name="eis/cicseci"/>
<bean id="component" class="MyDaoImpl">
<property name="connectionFactory" ref="connectionFactory"/>
</bean></programlisting>
</section>
</section>
<section xml:id="cci-object">
<title>Modeling CCI access as operation objects</title>
<para>The <literal>org.springframework.jca.cci.object</literal> package
contains support classes that allow you to access the EIS in a different
style: through reusable operation objects, analogous to Spring's JDBC
operation objects (see JDBC chapter). This will usually encapsulate the
CCI API: an application-level input object will be passed to the operation
object, so it can construct the input record and then convert the received
record data to an application-level output object and return it.</para>
<para><emphasis>Note</emphasis>: This approach is internally based on the
<classname>CciTemplate</classname> class and the
<interfacename>RecordCreator</interfacename> /
<interfacename>RecordExtractor</interfacename> interfaces, reusing the
machinery of Spring's core CCI support.</para>
<section xml:id="cci-object-mapping-record">
<title><classname>MappingRecordOperation</classname></title>
<para><classname>MappingRecordOperation</classname> essentially performs
the same work as <classname>CciTemplate</classname>, but represents a
specific, pre-configured operation as an object. It provides two
template methods to specify how to convert an input object to a input
record, and how to convert an output record to an output object (record
mapping):</para>
<itemizedlist>
<listitem>
<para><literal>createInputRecord(..)</literal> to specify how to
convert an input object to an input
<interfacename>Record</interfacename></para>
</listitem>
<listitem>
<para><literal>extractOutputData(..)</literal> to specify how to
extract an output object from an output
<interfacename>Record</interfacename></para>
</listitem>
</itemizedlist>
<para>Here are the signatures of these methods:</para>
<programlisting language="java">public abstract class MappingRecordOperation extends EisOperation {
...
protected abstract Record createInputRecord(RecordFactory recordFactory, Object inputObject)
throws ResourceException, DataAccessException { ... }
protected abstract Object extractOutputData(Record outputRecord)
throws ResourceException, SQLException, DataAccessException { ... }
...
}</programlisting>
<para>Thereafter, in order to execute an EIS operation, you need to use
a single execute method, passing in an application-level input object
and receiving an application-level output object as result:</para>
<programlisting language="java">public abstract class MappingRecordOperation extends EisOperation {
...
public Object execute(Object inputObject) throws DataAccessException {
...
}</programlisting>
<para>As you can see, contrary to the <classname>CciTemplate</classname>
class, this <methodname>execute(..)</methodname> method does not have an
<interfacename>InteractionSpec</interfacename> as argument. Instead, the
<interfacename>InteractionSpec</interfacename> is global to the
operation. The following constructor must be used to instantiate an
operation object with a specific
<interfacename>InteractionSpec</interfacename>:</para>
<programlisting language="java">InteractionSpec spec = ...;
MyMappingRecordOperation eisOperation = new MyMappingRecordOperation(getConnectionFactory(), spec);
...</programlisting>
</section>
<section xml:id="cci-object-mapping-comm-area">
<title><classname>MappingCommAreaOperation</classname></title>
<para>Some connectors use records based on a COMMAREA which represents
an array of bytes containing parameters to send to the EIS and data
returned by it. Spring provides a special operation class for working
directly on COMMAREA rather than on records. The
<classname>MappingCommAreaOperation</classname> class extends the
<classname>MappingRecordOperation</classname> class to provide such
special COMMAREA support. It implicitly uses the
<classname>CommAreaRecord</classname> class as input and output record
type, and provides two new methods to convert an input object into an
input COMMAREA and the output COMMAREA into an output object.</para>
<programlisting language="java">public abstract class MappingCommAreaOperation extends MappingRecordOperation {
...
protected abstract byte[] objectToBytes(Object inObject)
throws IOException, DataAccessException;
protected abstract Object bytesToObject(byte[] bytes)
throws IOException, DataAccessException;
...
}</programlisting>
</section>
<section xml:id="cci-automatic-record-gen">
<title>Automatic output record generation</title>
<para>As every <classname>MappingRecordOperation</classname> subclass is
based on CciTemplate internally, the same way to automatically generate
output records as with <classname>CciTemplate</classname> is available.
Every operation object provides a corresponding
<literal>setOutputRecordCreator(..)</literal> method. For further
information, see <xref linkend="automatic-output-generation" />.</para>
</section>
<section xml:id="cci-object-summary">
<title>Summary</title>
<para>The operation object approach uses records in the same manner as
the <classname>CciTemplate</classname> class.</para>
<table frame="all" xml:id="cci-interaction-methods">
<title>Usage of Interaction execute methods</title>
<tgroup cols="3">
<thead>
<row>
<entry
align="center"><classname>MappingRecordOperation</classname>
method signature</entry>
<entry
align="center"><classname>MappingRecordOperation</classname>
<literal>outputRecordCreator</literal> property</entry>
<entry align="center">execute method called on the CCI
<interfacename>Interaction</interfacename></entry>
</row>
</thead>
<tbody>
<row>
<entry align="center">Object execute(Object)</entry>
<entry align="center">not set</entry>
<entry align="center">Record execute(InteractionSpec,
Record)</entry>
</row>
<row>
<entry align="center">Object execute(Object)</entry>
<entry align="center">set</entry>
<entry align="center">boolean execute(InteractionSpec, Record,
Record)</entry>
</row>
</tbody>
</tgroup>
</table>
</section>
<section xml:id="cci-objects-mappring-record-example">
<title>Example for <classname>MappingRecordOperation</classname>
usage</title>
<para>In this section, the usage of the
<classname>MappingRecordOperation</classname> will be shown to access a
database with the Blackbox CCI connector.</para>
<note>
<para>The original version of this connector is provided by the Java EE
SDK (version 1.3), available from Sun.</para>
</note>
<para>Firstly, some initializations on the CCI
<interfacename>InteractionSpec</interfacename> must be done to specify
which SQL request to execute. In this sample, we directly define the way
to convert the parameters of the request to a CCI record and the way to
convert the CCI result record to an instance of the
<classname>Person</classname> class.</para>
<programlisting language="java">public class PersonMappingOperation extends MappingRecordOperation {
public PersonMappingOperation(ConnectionFactory connectionFactory) {
setConnectionFactory(connectionFactory);
CciInteractionSpec interactionSpec = new CciConnectionSpec();
interactionSpec.setSql("select * from person where person_id=?");
setInteractionSpec(interactionSpec);
}
protected Record createInputRecord(RecordFactory recordFactory, Object inputObject)
throws ResourceException {
Integer id = (Integer) inputObject;
IndexedRecord input = recordFactory.createIndexedRecord("input");
input.add(new Integer(id));
return input;
}
protected Object extractOutputData(Record outputRecord)
throws ResourceException, SQLException {
ResultSet rs = (ResultSet) outputRecord;
Person person = null;
if (rs.next()) {
Person person = new Person();
person.setId(rs.getInt("person_id"));
person.setLastName(rs.getString("person_last_name"));
person.setFirstName(rs.getString("person_first_name"));
}
return person;
}
}</programlisting>
<para>Then the application can execute the operation object, with the
person identifier as argument. Note that operation object could be set
up as shared instance, as it is thread-safe.</para>
<programlisting language="java">public class MyDaoImpl extends CciDaoSupport implements MyDao {
public Person getPerson(int id) {
PersonMappingOperation query = new PersonMappingOperation(getConnectionFactory());
Person person = (Person) query.execute(new Integer(id));
return person;
}
}</programlisting>