1
2
3
4
5
6
7
8
9
10
11 package it.imolinfo.jbi4cics.connection.jdbc;
12
13 import it.imolinfo.jbi4cics.connection.ConnectionManager;
14 import it.imolinfo.jbi4cics.connection.jdbc.util.DisconnectedPreparedStatement;
15 import it.imolinfo.jbi4cics.exception.ConnectionException;
16 import it.imolinfo.jbi4cics.locator.ServiceLocation;
17 import it.imolinfo.jbi4cics.messageformat.jdbc.JdbcOutputMessage;
18 import it.imolinfo.jbi4cics.messageformat.jdbc.JdbcStatementDescriptor;
19 import it.imolinfo.jbi4cics.service.ServiceContext;
20
21 import java.sql.Connection;
22 import java.sql.ResultSet;
23 import java.sql.SQLException;
24
25 import javax.naming.InitialContext;
26 import javax.naming.NamingException;
27 import javax.sql.DataSource;
28 import it.imolinfo.jbi4cics.Logger;
29 import it.imolinfo.jbi4cics.LoggerFactory;
30
31
32
33
34
35 public class JdbcConnectionManager implements ConnectionManager {
36
37
38
39
40 private static final Logger LOG
41 = LoggerFactory.getLogger(JdbcConnectionManager.class);
42
43
44
45
46 public JdbcConnectionManager() {
47 super();
48
49 }
50
51
52
53
54 public void handleCall(ServiceContext serviceContext) throws ConnectionException {
55 Connection connection=null;
56 DisconnectedPreparedStatement disconnectedStatement=null;
57 try{
58
59 connection=createConnection(serviceContext);
60 Object inputMessage=serviceContext.getInputMessage();
61
62 if (!(inputMessage instanceof DisconnectedPreparedStatement)){
63 LOG.error("CIC000600_Expected_disconnected_statement_input_message", new Object[] {inputMessage.getClass()});
64 throw new ConnectionException("CIC000600_Expected_disconnected_statement_input_message", new Object[] {inputMessage.getClass()});
65 }
66 disconnectedStatement=(DisconnectedPreparedStatement)inputMessage;
67
68 if (!(serviceContext.getInputMappingDescriptor() instanceof JdbcStatementDescriptor)) {
69 LOG.error("CIC000601_Expected_jdbc_statement_descriptor_input_message", new Object[] {serviceContext.getInputMappingDescriptor().getClass()});
70 throw new ConnectionException("CIC000601_Expected_jdbc_statement_descriptor_input_message", new Object[] {serviceContext.getInputMappingDescriptor().getClass()});
71 }
72 JdbcStatementDescriptor jdbcStatementDescriptor=(JdbcStatementDescriptor)serviceContext.getInputMappingDescriptor();
73 ResultSet resultSet=null;
74 disconnectedStatement.setConnection(connection);
75 try {
76 disconnectedStatement.setConnected(true);
77 }
78 catch (SQLException e){
79 LOG.error("CIC000602_Error_preparing_statement", new Object[] {e.getMessage()},e);
80 throw new ConnectionException("CIC000602_Error_preparing_statement", new Object[] {e.getMessage()},e);
81 }
82 try {
83 switch (jdbcStatementDescriptor.getSqlType()) {
84 case JdbcStatementDescriptor.SELECT_SQL : {
85 resultSet=disconnectedStatement.executeQuery();
86 break;
87 }
88 case JdbcStatementDescriptor.UPDATE_SQL : {
89 boolean result=disconnectedStatement.execute();
90
91 if (result){
92
93 resultSet=disconnectedStatement.getResultSet();
94 }
95 break;
96 }
97 default : {
98 LOG.error("CIC000603_Unexpected_sql_type", new Object[] {jdbcStatementDescriptor.getSqlType()});
99 throw new ConnectionException("CIC000603_Unexpected_sql_type", new Object[] {jdbcStatementDescriptor.getSqlType()});
100 }
101 }
102 }
103 catch (SQLException e){
104
105 LOG.error("CIC000604_Error_executing_statement", new Object[] {e.getMessage()}, e);
106 throw new ConnectionException("CIC000604_Error_executing_statement", new Object[] {e.getMessage()}, e);
107 }
108 JdbcOutputMessage jdbcOutputMessage=new JdbcOutputMessage();
109 jdbcOutputMessage.setDisconnectedSatetement(disconnectedStatement);
110 jdbcOutputMessage.setResultSet(resultSet);
111 serviceContext.setOutputMessage(jdbcOutputMessage);
112 }
113 finally{
114 releaseResources(connection,disconnectedStatement);
115 }
116
117 }
118
119 private Connection createConnection(ServiceContext serviceContext) throws ConnectionException {
120
121 ServiceLocation serviceLocation=serviceContext.getServiceLocation();
122 if (serviceLocation.getConnectionType()!=ServiceLocation.JDBC){
123 throw new ConnectionException("CIC000605_Expected_jdbc_connection_type", new Object[] {serviceLocation.getConnectionType()});
124 }
125
126 String jndiConnectionName=serviceLocation.getLocationName();
127 DataSource dataSource=null;
128 try {
129 InitialContext initialContext = new javax.naming.InitialContext();
130 dataSource =(DataSource)initialContext.lookup(jndiConnectionName);
131 }
132 catch (NamingException e) {
133 LOG.error("CIC000606_Error_retrieving_data_source", new Object[] {e.getMessage()}, e);
134 throw new ConnectionException("CIC000606_Error_retrieving_data_source", new Object[] {e.getMessage()}, e);
135 }
136 if (dataSource == null) {
137 throw new ConnectionException("CIC000607_Lookup_failed", new Object[] {jndiConnectionName});
138 }
139 try {
140 Connection connection = dataSource.getConnection();
141 return connection;
142 }
143 catch (SQLException e) {
144 LOG.error("CIC000608_Error_getting_connection_data_source", new Object[] {e.getMessage()}, e);
145 throw new ConnectionException("CIC000608_Error_getting_connection_data_source", new Object[] {e.getMessage()}, e);
146 }
147 }
148
149 private void releaseResources(Connection connection,DisconnectedPreparedStatement disconnectedPreparedStatement) throws ConnectionException {
150 try {
151 if (disconnectedPreparedStatement!=null){
152 disconnectedPreparedStatement.setConnected(false);
153 }
154 if (connection!=null){
155 connection.close();
156 }
157 }
158 catch (SQLException e){
159 LOG.error("CIC000609_Error_releasing_resources", new Object[] {e.getMessage()}, e);
160 throw new ConnectionException("CIC000609_Error_releasing_resources", new Object[] {e.getMessage()}, e);
161 }
162 }
163
164 }