📄 xmla_soap.java
字号:
/*
* ====================================================================
* This software is subject to the terms of the Common Public License
* Agreement, available at the following URL:
* http://www.opensource.org/licenses/cpl.html .
* Copyright (C) 2003-2004 TONBELLER AG.
* All Rights Reserved.
* You must accept the terms of that agreement to use this software.
* ====================================================================
*
*
*/
package com.tonbeller.jpivot.xmla;
import java.io.StringWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.xml.soap.Detail;
import javax.xml.soap.DetailEntry;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.Name;
import javax.xml.soap.Node;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFault;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import org.apache.log4j.Logger;
import com.tonbeller.jpivot.olap.model.OlapDiscoverer;
import com.tonbeller.jpivot.olap.model.OlapException;
import com.tonbeller.jpivot.olap.model.OlapItem;
import com.tonbeller.jpivot.olap.model.QueryResultHandler;
/**
* Handling XMLA SOAP calls
*/
public class XMLA_SOAP implements OlapDiscoverer {
static final String MDD_URI = "urn:schemas-microsoft-com:xml-analysis:mddataset";
static final String ROWS_URI = "urn:schemas-microsoft-com:xml-analysis:rowset";
static final String XMLA_URI = "urn:schemas-microsoft-com:xml-analysis";
static final String XSI_URI = "http://www.w3.org/2001/XMLSchema-instance";
static Logger logger = Logger.getLogger(XMLA_SOAP.class);
private SOAPConnectionFactory scf = null;
private MessageFactory mf = null;
private int provider = 0;
private String uri;
private URL url;
private String dataSource;
private String user;
private String password;
interface Rowhandler {
void handleRow(SOAPElement eRow, SOAPEnvelope envelope);
}
/**
* c'tor
* set URI, password, user and create URL
* no datasource, no provider, will be determined by discover datasource
* @param uri
* @param user
* @param password
*/
public XMLA_SOAP(String uri, String user, String password) throws OlapException {
logger.debug("Constructor: straight DiscoverDS");
init(uri, user, password);
setProviderAndDataSource(this.discoverDS());
}
/**
* c'tor
* set URI, password, user and create URL
* datasource given, provider will be determined from datasource
* @param uri
* @param user
* @param password
* @param dataSource
*/
public XMLA_SOAP(String uri, String user, String password, String dataSource)
throws OlapException {
logger.debug("Constructor: given dataSource= " + dataSource);
init(uri, user, password);
this.dataSource = dataSource;
provider = determineProvider(dataSource);
}
/**
* c'tor
* set URI, password, user and create URL
* provider given, datasource will be determined by discover datasource
* @param uri
* @param user
* @param password
* @param provider
*/
public XMLA_SOAP(String uri, String user, String password, int newProvider) throws OlapException {
logger.debug("Constructor: given provider= " + newProvider);
init(uri, user, password);
provider = newProvider;
setProviderAndDataSource(discoverDS());
}
/*
* init
*/
private void init(String uri, String user, String password) throws OlapException {
try {
scf = SOAPConnectionFactory.newInstance();
mf = MessageFactory.newInstance();
} catch (UnsupportedOperationException e) {
throw new OlapException(e);
} catch (SOAPException e) {
throw new OlapException(e);
}
this.uri = uri;
this.user = user;
this.password = password;
try {
url = new URL(uri);
} catch (MalformedURLException e1) {
throw new OlapException(e1);
}
if (user != null && user.length() > 0) {
String newUri = url.getProtocol() + "://" + user;
if (password != null && password.length() > 0) {
newUri += ":" + password;
}
newUri += "@" + url.getHost() + ":" + url.getPort() + url.getPath();
try {
url = new URL(newUri);
} catch (MalformedURLException e2) {
throw new OlapException(e2);
}
}
}
/**
* retrieve catalogs in data source
* @return List of OlapItems for the catalogs
* @see DataSourceBrowser
*/
public List discoverCat() throws OlapException {
final List cats = new ArrayList();
// restrictions
HashMap rHash = new HashMap(); // empty
// properties
HashMap pHash = new HashMap();
pHash.put("DataSourceInfo", dataSource);
pHash.put("Content", "SchemaData");
Rowhandler rh = new Rowhandler() {
public void handleRow(SOAPElement eRow, SOAPEnvelope envelope) {
XMLA_OlapItem oi = new XMLA_OlapItem(OlapItem.TYPE_CATALOG);
cats.add(oi);
Iterator it = eRow.getChildElements();
while (it.hasNext()) {
Object o = it.next();
if (!(o instanceof SOAPElement))
continue;
SOAPElement e = (SOAPElement) o;
String lname = e.getElementName().getLocalName();
if (lname.equals("CATALOG_NAME"))
oi.setName(e.getValue());
else
oi.setProperty(lname, e.getValue());
}
}
};
discover("DBSCHEMA_CATALOGS", url, rHash, pHash, rh);
logger.debug("DBSCHEMA_CATALOGS: found " + cats.size());
return cats;
}
/**
* retrieve datasource properties
* @return List of OlapItems for the datasource properties
* @see DataSourceBrowser
*/
public List discoverDSProps() throws OlapException {
final List props = new ArrayList();
// restrictions
HashMap rHash = new HashMap(); // empty
// properties
HashMap pHash = new HashMap();
pHash.put("DataSourceInfo", dataSource);
pHash.put("Content", "SchemaData");
Rowhandler rh = new Rowhandler() {
public void handleRow(SOAPElement eRow, SOAPEnvelope envelope) {
XMLA_OlapItem oi = new XMLA_OlapItem(OlapItem.TYPE_PROPERTY);
props.add(oi);
Iterator it = eRow.getChildElements();
while (it.hasNext()) {
Object o = it.next();
if (!(o instanceof SOAPElement))
continue;
SOAPElement e = (SOAPElement) o;
String lname = e.getElementName().getLocalName();
if ( lname.equals("PropertyName") )
oi.setName( e.getValue());
oi.setProperty(lname, e.getValue());
}
}
};
discover("DISCOVER_PROPERTIES", url, rHash, pHash, rh);
logger.debug("DISCOVER_PROPERTIES: found " + props.size());
return props;
}
/**
* retrieve cubes in data source
* @return List of OlapItems for the cubes
* @see DataSourceBrowser
*/
public List discoverCube(String cat) throws OlapException {
final List cubes = new ArrayList();
// restrictions
HashMap rHash = new HashMap();
rHash.put("CATALOG_NAME", cat);
// properties
HashMap pHash = new HashMap();
pHash.put("DataSourceInfo", dataSource);
pHash.put("Content", "SchemaData");
pHash.put("Catalog", cat); // needed, or else can only discover first catalog's cubes
Rowhandler rh = new Rowhandler() {
public void handleRow(SOAPElement eRow, SOAPEnvelope envelope) {
XMLA_OlapItem oi = new XMLA_OlapItem(OlapItem.TYPE_CUBE);
cubes.add(oi);
Iterator it = eRow.getChildElements();
while (it.hasNext()) {
Object o = it.next();
if (!(o instanceof SOAPElement))
continue;
SOAPElement e = (SOAPElement) o;
String lname = e.getElementName().getLocalName();
if (lname.equals("CUBE_NAME"))
oi.setName(e.getValue());
else
oi.setProperty(lname, e.getValue());
}
}
};
discover("MDSCHEMA_CUBES", url, rHash, pHash, rh);
logger.debug("MDSCHEMA_CUBES: found " + cubes.size());
return cubes;
}
/**
* retrieve dimensions in data source
* @return List of OlapItems for the dimensions
* @see DataSourceBrowser
*/
public List discoverDim(String cat, String cube) throws OlapException {
final List dims = new ArrayList();
// restrictions
HashMap rHash = new HashMap();
rHash.put("CATALOG_NAME", cat);
rHash.put("CUBE_NAME", cube);
// properties
HashMap pHash = new HashMap();
pHash.put("DataSourceInfo", dataSource);
pHash.put("Catalog", cat); // neccessary ???
pHash.put("Content", "SchemaData");
Rowhandler rh = new Rowhandler() {
public void handleRow(SOAPElement eRow, SOAPEnvelope envelope) {
XMLA_OlapItem oi = new XMLA_OlapItem(OlapItem.TYPE_DIMENSION);
dims.add(oi);
Iterator it = eRow.getChildElements();
while (it.hasNext()) {
Object o = it.next();
if (!(o instanceof SOAPElement))
continue;
SOAPElement e = (SOAPElement) o;
String lname = e.getElementName().getLocalName();
if (lname.equals("DIMENSION_UNIQUE_NAME")) {
oi.setUniqueName(e.getValue());
} else if (lname.equals("DIMENSION_CAPTION")) {
oi.setCaption(e.getValue());
} else if (lname.equals("DIMENSION_NAME")) {
oi.setName(e.getValue());
} else {
oi.setProperty(lname, e.getValue());
}
}
}
};
discover("MDSCHEMA_DIMENSIONS", url, rHash, pHash, rh);
logger.debug("MDSCHEMA_DIMENSIONS: found " + dims.size());
if (dims.size() == 0) {
throw new OlapException("No metadata schema dimensions for catalog: " + cat + " and cube: " + cube);
}
return dims;
}
/**
* retrieve hierarchies in data source
* @return List of OlapItems for the hierarchies
* @see DataSourceBrowser
*/
public List discoverHier(String cat, String cube, String dimension) throws OlapException {
final List hiers = new ArrayList();
// restrictions
HashMap rHash = new HashMap();
rHash.put("CATALOG_NAME", cat);
rHash.put("CUBE_NAME", cube);
if (dimension != null)
rHash.put("DIMENSION_UNIQUE_NAME", dimension);
// properties
HashMap pHash = new HashMap();
pHash.put("DataSourceInfo", dataSource);
pHash.put("Catalog", cat); // neccessary ???
pHash.put("Content", "SchemaData");
Rowhandler rh = new Rowhandler() {
public void handleRow(SOAPElement eRow, SOAPEnvelope envelope) {
XMLA_OlapItem oi = new XMLA_OlapItem(OlapItem.TYPE_HIERARCHY);
hiers.add(oi);
Iterator it = eRow.getChildElements();
while (it.hasNext()) {
Object o = it.next();
if (!(o instanceof SOAPElement))
continue;
SOAPElement e = (SOAPElement) o;
String lname = e.getElementName().getLocalName();
if (lname.equals("HIERARCHY_UNIQUE_NAME")) {
oi.setUniqueName(e.getValue());
} else if (lname.equals("HIERARCHY_CAPTION")) {
oi.setCaption(e.getValue());
} else if (lname.equals("HIERARCHY_NAME")) {
oi.setName(e.getValue());
} else {
oi.setProperty(lname, e.getValue());
}
}
}
};
discover("MDSCHEMA_HIERARCHIES", url, rHash, pHash, rh);
logger.debug("MDSCHEMA_HIERARCHIES: found " + hiers.size());
if (hiers.size() == 0) {
throw new OlapException("No metadata schema hierarchies for catalog: " + cat + " and cube: " + cube);
}
return hiers;
}
/**
* retrieve levels in data source
* @return List of OlapItems for the levels
* @see DataSourceBrowser
*/
public List discoverLev(String cat, String cube, String dimension, String hier)
throws OlapException {
final List levels = new ArrayList();
// restrictions
HashMap rHash = new HashMap();
rHash.put("CATALOG_NAME", cat);
rHash.put("CUBE_NAME", cube);
if (dimension != null)
rHash.put("DIMENSION_UNIQUE_NAME", dimension);
if (hier != null)
rHash.put("HIERARCHY_UNIQUE_NAME", dimension);
// properties
HashMap pHash = new HashMap();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -