While developing in Oracle SOA Suite most of us come across a common challenge on how to fetch artifacts from MDS.
MDS is a out of the box governance provided by Oracle, whenever we install Oracle SOA suite MDS gets automatically created and this MDS is used mainly at runtime.
Sometimes we may require to access this artifacts directly from MDS using mds apis provided by oracle. The challange of using the MDS api provided by oracle is that the api exposed by oracle does not provide the flexibility to use them as per user's requirement.
In this blog i will provide the java code base which helped me to traverse the MDS package and fetch the required documents.
ref : http://java.net/projects/mds-explorer
MDS Runtime Dependencies and MDS Runtime library required to run the code.
public class ReadMdsData {
private DocumentBuilderFactory dbFactory =
DocumentBuilderFactoryImpl.newInstance();
private DocumentBuilder dBuilder;
private GenericBean genBean = new GenericBean();
private DBMetadataStore dbMetadataStore;
private MDSInstance dbMDSInstance;
private DocumentBuilderFactory dbFactory =
DocumentBuilderFactoryImpl.newInstance();
private DocumentBuilder dBuilder;
private GenericBean genBean = new GenericBean();
private DBMetadataStore dbMetadataStore;
private MDSInstance dbMDSInstance;
public Document readData(String strResourcePath) throws Exception {
try{
Document doc = null;
String strRelativePath = "/";
// If the resource path contaiins oramds then remove the path
if (strResourcePath.contains(":"))
strRelativePath =
strResourcePath.substring(strResourcePath.indexOf(":") + 1);
else
strRelativePath = strResourcePath;
if(dbMetadataStore==null)
createConnection();
DocumentName docName = DocumentName.create(strRelativePath);
MDSSession mdsSession =
dbMDSInstance.createSession(new SessionOptions(IsolationLevel.READ_COMMITTED,
null, null), null);
PManager mdsPManager = mdsSession.getPersistenceManager();
PContext mdsContext = mdsSession.getPContext();
PDocument mdsDocument = mdsPManager.getDocument(mdsContext, docName);
if (mdsDocument.isXML()||mdsDocument.isDocument()) {
doc = dBuilder.parse(mdsDocument.read());
}
return doc;
}finally{
dbMetadataStore.getConnection().close();
}
}
try{
Document doc = null;
String strRelativePath = "/";
// If the resource path contaiins oramds then remove the path
if (strResourcePath.contains(":"))
strRelativePath =
strResourcePath.substring(strResourcePath.indexOf(":") + 1);
else
strRelativePath = strResourcePath;
if(dbMetadataStore==null)
createConnection();
DocumentName docName = DocumentName.create(strRelativePath);
MDSSession mdsSession =
dbMDSInstance.createSession(new SessionOptions(IsolationLevel.READ_COMMITTED,
null, null), null);
PManager mdsPManager = mdsSession.getPersistenceManager();
PContext mdsContext = mdsSession.getPContext();
PDocument mdsDocument = mdsPManager.getDocument(mdsContext, docName);
if (mdsDocument.isXML()||mdsDocument.isDocument()) {
doc = dBuilder.parse(mdsDocument.read());
}
return doc;
}finally{
dbMetadataStore.getConnection().close();
}
}
public List traverseMds(String strPackage, String strSearchString) throws InvalidReferenceException, InvalidReferenceTypeException,MDSException {
if(dbMetadataStore==null)
createConnection();
PackageName packageName = PackageName.createPackageName(strPackage);
NameCondition condition =
ConditionFactory.createNameCondition(packageName.getAbsoluteName(),
"%"+strSearchString);
ResourceQuery query =
QueryFactory.createResourceQuery(dbMDSInstance, condition);
Iterator<QueryResult> contents = query.execute();
List<ResourceName> resources = new ArrayList<ResourceName>();
QueryResult result;
while (contents.hasNext()) {
result = contents.next();
if (result.getResultType() == QueryResult.ResultType.PACKAGE_RESULT) {
PackageResult pack = (PackageResult)result;
resources.add(pack.getPackageName());
} else {
DocumentResult doc1 = (DocumentResult)result;
resources.add(doc1.getDocumentName());
}
}
List lstResource = new ArrayList();
Object objArr[] = resources.toArray();
for (Object obj : objArr) {
ResourceName rcNm = (ResourceName)obj;
lstResource.add(rcNm.getAbsoluteName());
}
return lstResource;
}
if(dbMetadataStore==null)
createConnection();
PackageName packageName = PackageName.createPackageName(strPackage);
NameCondition condition =
ConditionFactory.createNameCondition(packageName.getAbsoluteName(),
"%"+strSearchString);
ResourceQuery query =
QueryFactory.createResourceQuery(dbMDSInstance, condition);
Iterator<QueryResult> contents = query.execute();
List<ResourceName> resources = new ArrayList<ResourceName>();
QueryResult result;
while (contents.hasNext()) {
result = contents.next();
if (result.getResultType() == QueryResult.ResultType.PACKAGE_RESULT) {
PackageResult pack = (PackageResult)result;
resources.add(pack.getPackageName());
} else {
DocumentResult doc1 = (DocumentResult)result;
resources.add(doc1.getDocumentName());
}
}
List lstResource = new ArrayList();
Object objArr[] = resources.toArray();
for (Object obj : objArr) {
ResourceName rcNm = (ResourceName)obj;
lstResource.add(rcNm.getAbsoluteName());
}
return lstResource;
}
public void createConnection()throws MDSException {
String userName = "MDS DB User Name";
String password = "MDS DB Password";
String strIp = "MDS DB ip";
String strPort = "MDS DB Port";
String strSid = "MDS DB SID";
String strPartition = "MDS partition name";
String jdbcConnectionString =
"jdbc:oracle:thin:@" + strIp.trim() + ":" + strPort.trim() + ":" +
strSid.trim();
dbMetadataStore =
new DBMetadataStore(userName, password, jdbcConnectionString,
strPartition);
NamespaceConfig dbNamespaceConfig =
new NamespaceConfig(Namespace.create("/"), dbMetadataStore);
PConfig persistenceConfig =
new PConfig(new NamespaceConfig[] { dbNamespaceConfig });
MDSConfig dbConfig = new MDSConfig(null, persistenceConfig, null);
dbMDSInstance =
MDSInstance.getOrCreateInstance("Metadata", dbConfig, true);
}
String userName = "MDS DB User Name";
String password = "MDS DB Password";
String strIp = "MDS DB ip";
String strPort = "MDS DB Port";
String strSid = "MDS DB SID";
String strPartition = "MDS partition name";
String jdbcConnectionString =
"jdbc:oracle:thin:@" + strIp.trim() + ":" + strPort.trim() + ":" +
strSid.trim();
dbMetadataStore =
new DBMetadataStore(userName, password, jdbcConnectionString,
strPartition);
NamespaceConfig dbNamespaceConfig =
new NamespaceConfig(Namespace.create("/"), dbMetadataStore);
PConfig persistenceConfig =
new PConfig(new NamespaceConfig[] { dbNamespaceConfig });
MDSConfig dbConfig = new MDSConfig(null, persistenceConfig, null);
dbMDSInstance =
MDSInstance.getOrCreateInstance("Metadata", dbConfig, true);
}
No comments:
Post a Comment