Monday, February 8, 2016

How to Read MDS File content using Java code - SOA 11g

We can use the below Java Code to connect with MDS database & retrieve the content based on file name and display it as a text.

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import oracle.mds.config.MDSConfig;
import oracle.mds.config.PConfig;
import oracle.mds.core.*;
import oracle.mds.naming.PackageName;
import oracle.mds.naming.ResourceName;
import oracle.mds.persistence.stores.db.DBMetadataStore;
import oracle.mds.query.*;
import oracle.xml.parser.v2.DOMParser;
import oracle.xml.parser.v2.XMLDocument;
import org.w3c.dom.Document;

public class RetrieveMDSContent
{
public static void main(String args[]) throws Exception
{
   String MDSFileName="AIAConfigurationProperties.xml"; // MDS File Name
   String connName="MDSConnectionName"; //Dont change it
        MDSInstance mdsInstance = null;
        DBMetadataStore store = getMDSDBHandle();
        PConfig pConfig = new PConfig(store);
        MDSConfig config = new MDSConfig(null, pConfig, null);
        mdsInstance = MDSInstance.getOrCreateInstance(connName, config);
        XMLDocument xmlDoc = null;
        List list = findResource(mdsInstance, MDSFileName, true);
        for(Iterator iterator = list.iterator(); iterator.hasNext();)
        {
            ResourceName rn = (ResourceName)iterator.next();
            mSession = mdsInstance.createSession(new SessionOptions(IsolationLevel.READ_COMMITTED, null, null), null);
            MetadataObject mo = mSession.getMetadataObject(rn.getAbsoluteName());
            String textContent = getDocAsXML(mo.getDocument());
            System.out.print(textContent); // Display the content
        }
    }

    public static String getDocAsXML(Document doc)
        throws Exception
    {
        TransformerFactory tfactory = TransformerFactory.newInstance();
        Transformer xform = tfactory.newTransformer();
        javax.xml.transform.Source src = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        javax.xml.transform.Result result = new StreamResult(writer);
        xform.transform(src, result);
        System.out.print(writer.toString());
        return writer.toString();
    }

    public static List findResource(MDSInstance mdsInstance, String regExpr, boolean stopAtFirstMatch)
        throws Exception
    {
        List rnList = new ArrayList();
        Pattern pattern = Pattern.compile(regExpr);
        findRecursively(mdsInstance, pattern, null, rnList, stopAtFirstMatch);
        return rnList;
    }

    private static void findRecursively(MDSInstance instance, Pattern pattern, String pName, List rnList, boolean stopWhenFound)
        throws Exception
    {
        PackageName packageName = null;
        if(pName != null)
            packageName = PackageName.createPackageName(pName);
        List list = queryMDS(instance, packageName);
        for(Iterator iterator = list.iterator(); iterator.hasNext();)
        {
            ResourceName rn = (ResourceName)iterator.next();
            Matcher matcher = pattern.matcher(rn.getAbsoluteName());
            boolean matchFound = matcher.find();
            if(matchFound)
            {
                rnList.add(rn);
                if(stopWhenFound)
                    break;
            }
            if(rn.isPackageName())
                findRecursively(instance, pattern, rn.getAbsoluteName(), rnList, stopWhenFound);
        }

    }

    public static List queryMDS(MDSInstance mdsInstance, PackageName packageName)
        throws Exception
    {
        if(packageName == null)
            packageName = PackageName.createPackageName("/");
        NameCondition condition = ConditionFactory.createNameCondition(packageName.getAbsoluteName(), "%");
        ResourceQuery query = QueryFactory.createResourceQuery(mdsInstance, condition);
        Iterator contents = query.execute();
        List resources = new ArrayList();
        if(contents == null)
            return resources;
        while(contents.hasNext())
        {
            QueryResult result = (QueryResult)contents.next();
            if(result.getResultType() == oracle.mds.query.QueryResult.ResultType.PACKAGE_RESULT)
            {
                PackageResult pack = (PackageResult)result;
                resources.add(pack.getPackageName());
            } else
            {
                DocumentResult doc = (DocumentResult)result;
                resources.add(doc.getDocumentName());
            }
        }
        return resources;
    }

    public static DBMetadataStore getMDSDBHandle()
        throws Exception
    {
        DBMetadataStore store = new DBMetadataStore("mds_user","mdspassword", "jdbc:oracle:thin:@XXXX:1521/servicename", "soa-infra");
        return store;
    }

    private static MDSSession mSession = null;

}


Use the attached library Jar files to run this Java code.

1 comment:

  1. Thanks a lot!! Got exactly what I have been looking upon.

    ReplyDelete