/*
* Copyright 2001-2006 C:1 Financial Services GmbH
*
* This software is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License Version 2.1, as published by the Free Software Foundation.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
*/
package de.finix.contelligent.components.query;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.xmlio.in.DefaultSimpleImportHandler;
import org.apache.commons.xmlio.in.SimpleImporter;
import org.apache.commons.xmlio.in.SimplePath;
import org.apache.log4j.Logger;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import de.finix.contelligent.AbstractComponent;
import de.finix.contelligent.CallData;
import de.finix.contelligent.logging.LoggingService;
public class RemoteQuery extends AbstractComponent implements Query {
final static private Logger log = LoggingService.getLogger(RemoteQuery.class);
private String queryProvider;
private int updateInterval;
private long lastUpdate = 0;
private List queryResult;
/**
* @return Returns the queryProvider.
*/
public String getQueryProvider() {
return queryProvider;
}
/**
* @param queryProvider
* The queryProvider to set.
*/
public void setQueryProvider(String queryProvider) {
this.queryProvider = queryProvider;
}
/**
* @return Returns the updateInterval.
*/
public int getUpdateInterval() {
return updateInterval;
}
/**
* @param updateInterval
* The updateInterval to set.
*/
public void setUpdateInterval(int updateInterval) {
this.updateInterval = updateInterval;
}
public List getQueryResult(CallData callData) {
long currentTimeMillis = System.currentTimeMillis();
if ((currentTimeMillis - lastUpdate) / 1000 > updateInterval) {
lastUpdate = currentTimeMillis;
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Create a method instance.
GetMethod method = new GetMethod(queryProvider);
queryResult = new ArrayList();
try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
log.error("Failed to get remote query: " + method.getStatusLine());
} else {
InputStream responseStream = method.getResponseBodyAsStream();
SimpleImporter importer = new SimpleImporter();
importer.setIncludeLeadingCDataIntoStartElementCallback(true);
importer.addSimpleImportHandler(new QueryResultParser(queryResult));
importer.parse(new InputSource(responseStream));
}
} catch (HttpException e) {
log.error("Fatal protocol violation!", e);
} catch (IOException e) {
log.error("Fatal transport error!", e);
} catch (ParserConfigurationException e) {
log.error("Parser configuration failed!", e);
} catch (SAXException e) {
log.error("Exception while parsing provided query result!", e);
} finally {
// Release the connection.
method.releaseConnection();
}
}
return queryResult;
}
class QueryResultParser extends DefaultSimpleImportHandler {
private List queryResult;
private BasicQueryResult result;
/**
* @param queryResult
*/
public QueryResultParser(List queryResult) {
this.queryResult = queryResult;
}
public void startElement(SimplePath path, String name, AttributesImpl attributes, String leadingCDdata) {
if (path.matches(QueryResultProvider.QUERY_RESULT_ELEMENT)) {
result = new BasicQueryResult();
} else if (path.matches(QueryResultProvider.QUERY_RESULT_ENTRY_ELEMENT)) {
String key = attributes.getValue(QueryResultProvider.QUERY_RESULT_ENTRY_KEY_ATTRIBUTE);
result.put(key, leadingCDdata);
}
}
public void endElement(SimplePath path, String name) {
if (path.matches(QueryResultProvider.QUERY_RESULT_ELEMENT)) {
queryResult.add(result);
}
}
}
}
|