KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > binding > xquarebc > listeners > DelimiterNewDataStrategy


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id : $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.binding.xquarebc.listeners;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32
33 import javax.xml.transform.Source JavaDoc;
34
35 import org.objectweb.petals.binding.xquarebc.XQuareBCException;
36 import org.xquark.bridge.XQBridge;
37
38 /**
39  * Implements a strategy for detecting "new data",
40  * getting it (as XML) and updating what data is known, by monitoring
41  * a given field as its "new data delimiter".
42  *
43  * What it actually does is first executing two SQL queries to
44  * get the "new", current data delimiter value and the "old",
45  * known data delimiter value. Then it executes an XQuery through
46  * the XQuareBCJBIProcessor, after having the "##OLD_DATA_DELIMITER##"
47  * and "##OLD_DATA_DELIMITER##" expressions replaced accordingly
48  * within its XQuery code. Finally it executes an SQL insertion
49  * to update the "old", known data delimiter to the current "new" one.
50  *
51  * NB. All These SQL and XQuery queries, insertions and initial value
52  * are configured in XQuareBC's listener's configuration. Look in the
53  * javadoc of XQuareBCListener for more details.
54  *
55  * NB. Must be instanciated for each new data detection.
56  * NB. This is the only implementation of NewDataStrategy for now.
57  *
58  * @version $Rev: 250 $Date: {date}
59  * @since Petals 1.0
60  * @author Marc Dutoo - Open Wide
61  *
62  */

63 public class DelimiterNewDataStrategy implements NewDataStrategy {
64     
65     public static final String JavaDoc OLD_DATA_DELIMITER_SELECT_PROP = "oldDataDelimiterSelect";
66     public static final String JavaDoc NEW_DATA_DELIMITER_SELECT_PROP = "newDataDelimiterSelect";
67     public static final String JavaDoc INIT_DATA_DELIMITER_PROP = "initDataDelimiter";
68     public static final String JavaDoc NEW_DATA_XQUERY_PROP = "newDataXquery";
69     public static final String JavaDoc NEW_DATA_DELIMITER_UPDATES_PROP = "newDataDelimiterUpdates";
70
71     protected static final String JavaDoc DELIMITER_PROP_PREFIX = "delimiter.";
72     protected static final String JavaDoc CUSTOM_TABLE_SUFFIX = "_xqbc";
73     protected static final String JavaDoc OLD_DATA_DELIMITER_TOKEN = "##OLD_DATA_DELIMITER##";
74     protected static final String JavaDoc NEW_DATA_DELIMITER_TOKEN = "##NEW_DATA_DELIMITER##";
75
76     private Logger JavaDoc logger;
77     protected Connection JavaDoc conn;
78     
79     protected String JavaDoc oldDataDelimiterSelect;
80     protected String JavaDoc newDataDelimiterSelect;
81     protected String JavaDoc initDataDelimiter;
82     protected ArrayList JavaDoc<String JavaDoc> newDataDelimiterUpdates = new ArrayList JavaDoc<String JavaDoc>();
83     protected String JavaDoc newDataXquery;
84     
85     protected String JavaDoc oldDataDelimiter;
86     protected String JavaDoc newDataDelimiter;
87
88     protected XQuareBCJBIProcessor processor;
89     public Properties JavaDoc serviceProps;
90     protected XQBridge xqBridge;
91
92     
93     /**
94      * Creates a new DelimiterNewDataStrategy
95      * @param processor
96      * @param conn
97      * @param serviceProps
98      * @param listenerPropPrefix
99      * @param logger
100      * @throws XQuareBCException
101      */

102     public DelimiterNewDataStrategy(XQuareBCJBIProcessor processor,
103         Connection JavaDoc conn, Properties JavaDoc serviceProps, String JavaDoc listenerPropPrefix,
104         Logger JavaDoc logger)
105         throws XQuareBCException {
106         this.logger = logger;
107         this.conn = conn;
108         
109         // for query :
110
this.processor = processor;
111         this.serviceProps = serviceProps;
112         this.xqBridge= processor.getXQBridge(serviceProps);
113         
114         // init from props :
115
listenerPropPrefix = listenerPropPrefix + DELIMITER_PROP_PREFIX;
116         this.oldDataDelimiterSelect = (String JavaDoc) serviceProps.get(
117             listenerPropPrefix + OLD_DATA_DELIMITER_SELECT_PROP);
118         this.newDataDelimiterSelect = (String JavaDoc) serviceProps.get(
119             listenerPropPrefix + NEW_DATA_DELIMITER_SELECT_PROP);
120         this.initDataDelimiter = (String JavaDoc) serviceProps.get(
121             listenerPropPrefix + INIT_DATA_DELIMITER_PROP);
122         this.newDataXquery = (String JavaDoc) serviceProps.get(
123             listenerPropPrefix + NEW_DATA_XQUERY_PROP);
124         
125         // initing new data delimiter update statements :
126
String JavaDoc newDataDelimiterUpdateString = (String JavaDoc) serviceProps.get(
127             listenerPropPrefix + NEW_DATA_DELIMITER_UPDATES_PROP);
128         StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(newDataDelimiterUpdateString, ";");
129         // splitting it to execute multiple updates if any
130
// ex. for delimiter = max id : 1. cleaning the new data detection table, 2. filling it with the new max id
131
while (stok.hasMoreTokens()) {
132             String JavaDoc newDataDelimiterUpdate = stok.nextToken().trim();
133             newDataDelimiterUpdates.add(newDataDelimiterUpdate);
134         }
135     }
136     
137     
138     /* (non-Javadoc)
139      * @see org.objectweb.petals.binding.xquarebc.listeners.NewDataStrategy#detectNewData()
140      */

141     public boolean detectNewData() throws Exception JavaDoc {
142         // getting the old data delimiter :
143
Statement JavaDoc newDataStmt = conn.createStatement();
144         newDataStmt.execute(this.oldDataDelimiterSelect);
145         ResultSet JavaDoc newDataRs = newDataStmt.getResultSet();
146         if (newDataRs.next()) {
147             oldDataDelimiter = newDataRs.getString(1);
148         }
149         if (oldDataDelimiter == null) {
150             oldDataDelimiter = initDataDelimiter;
151         }
152
153         // getting the new data delimiter :
154
Statement JavaDoc getNewMaxIdStmt = conn.createStatement();
155         getNewMaxIdStmt.execute(this.newDataDelimiterSelect);
156         ResultSet JavaDoc getNewMaxIdRs = getNewMaxIdStmt.getResultSet();
157         if (getNewMaxIdRs.next()) {
158             newDataDelimiter = getNewMaxIdRs.getString(1);
159         }
160         
161         // there is new data if they are different
162
return (newDataDelimiter != null
163                 && !newDataDelimiter.equals(oldDataDelimiter));
164     }
165
166     
167     /* (non-Javadoc)
168      * @see org.objectweb.petals.binding.xquarebc.listeners.NewDataStrategy#getNewData()
169      */

170     public Source JavaDoc getNewData() throws Exception JavaDoc {
171         // formatting result message body :
172
String JavaDoc formattedXquery = replaceDelimiterTokens(newDataXquery);
173         logger.log(Level.FINE, "DelimiterNewDataStrategy formattedXquery : "
174             + formattedXquery);
175         Source JavaDoc bodySource = processor.doQuery(formattedXquery,
176             xqBridge, serviceProps);
177         return bodySource;
178     }
179     
180
181     /* (non-Javadoc)
182      * @see org.objectweb.petals.binding.xquarebc.listeners.NewDataStrategy#updateDataKnown()
183      */

184     public void updateDataKnown() throws Exception JavaDoc {
185         // building SQL for updating the data delimiter to the new value
186
// splitting it to execute multiple updates if any
187
// ex. for delimiter = max id : 1. cleaning the new data detection table, 2. filling it with the new max id
188
for (String JavaDoc newDataDelimiterUpdate : newDataDelimiterUpdates) {
189             newDataDelimiterUpdate = replaceDelimiterTokens(newDataDelimiterUpdate);
190             conn.createStatement().executeUpdate(newDataDelimiterUpdate);
191             // conn.commit(); // would require autocommit, but there is no need
192
}
193     }
194
195
196     protected String JavaDoc replaceDelimiterTokens(String JavaDoc sqlOrXquery) {
197         sqlOrXquery = sqlOrXquery.replaceAll(OLD_DATA_DELIMITER_TOKEN, oldDataDelimiter);
198         sqlOrXquery = sqlOrXquery.replaceAll(NEW_DATA_DELIMITER_TOKEN, newDataDelimiter);
199         return sqlOrXquery;
200     }
201     
202 }
203
Popular Tags