站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > NetBeans API Javadoc 5.0.0

org.netbeans.api.mdr (NetBeans MDR API) - NetBeans API Javadoc 5.0.0

 

Package org.netbeans.api.mdr

API classes and interfaces that serve as an entry point to a metadata repository.

See:
          Description

Interface Summary
MDRepository Interface for accessing content of a metadata repository.
MDRObject Interface implemented by each repository object (besides the standard JMI interfaces).
 

Class Summary
DTDProducer DTD generation utility.
JMIMapper JMI mapping utility.
JMIStreamFactory Class used by JMI mapper to obtain output streams for generating JMI interfaces.
MDRManager Entry point to the metadata repositories.
 

Exception Summary
CreationFailedException Exception thrown when creation of a new repository package instance fails.
 

Package org.netbeans.api.mdr Description

API classes and interfaces that serve as an entry point to a metadata repository. The fundamental classes in this regard are MDRManager and MDRepository.

MDRManager is used to access repositories (represented by instances of MDRepository, these can then be used to access instances of MOF metamodels (represented by javax.jmi.reflect.RefPackage instances corresponding to package proxies for outermost packages of the metamodels). MDRManager is a singleton. There can be several repositories in the system (accessible via MDRManager) and each repository may contain any number of instances of any number of metamodels.

Creating Metamodel Instances

Here is an example code showing how to create a new empty instance of MOF metamodel that can be used e.g. to load XMI files conforming to MOF:

    // get default repository
    MDRepository repository = MDRManager.getDefault().getDefaultRepository();
    // create a new instance of MOF metamodel - name it MyInstanceOfMOF
    RefPackage mof = null;
    try {
        mof = repository.createExtent("MyInstanceOfMOF");
    } catch (CreationFailedException e) {
        // handle the failure - happens if extent (i.e. metamodel instance) named
        // MyInstanceOfMOF already exists in the repository, or if some other fatal
        // error occurs
        ...
    }
    ...

Accessing Metamodel Instances

Instances of metamodels in the repository are also called extents. Following is an example code that retrieves a reference to an extent created in the first example from the repository:

    // get default repository
    MDRepository repository = MDRManager.getDefault().getDefaultRepository();
    // get an extent named MyInstanceOfMOF
    RefPackage mof = repository.getExtent("MyInstanceOfMOF");
    if (mof == null) {
        // the previous call returned null - it means that an extent with a given
        // name was not found
        System.out.println("Extent named MyInstanceOfMOF not found in the repository.");
        ...
    }
    ...

Working with Transactions

MDRepository interfaces supports transactional processing. Simple atomic per-repository transactions are assumed. Capability of real nested transactions is optional (see description of beginTrans and endTrans method of MDRepository class for more information.

When working with transactions it is important to strictly enforce the following pattern:

  1. Start a transaction
  2. Start a try block
  3. Do the work
  4. Finalize the transaction in the finally clause of the try block
When not using explicit transactions, it is assumed that all JMI calls are auto-commited.

It is essential to use transactions in multithreaded applications especially when iterating through the content of collections returned from JMI calls (since these collections are live) to avoid concurrent modifications. Here is an example code that iterates through all class proxies in a given extent and uses a read-only transaction to ensure that the collection is not modified by other thread during the iteration:

    // get the default repository
    MDRepository repository = MDRManager.getDefault().getDefaultRepository();
    // start a read-only transaction
    repository.beginTrans(false);
    try {
        // get an extent named MyInstanceOfMOF (assuming that this extent was already created by
        // other part of the application
        RefPackage mof = repository.getExtent("MyInstanceOfMOF");
        for (Iterator it = mof.refAllClasses(); it.hasNext();) {
            RefClass clsProxy = (RefClass) it.next();
            // print name of the class proxy meta-object
            System.out.println(((MofClass) clsProxy.refMetaObject()).getName());
        }
    } finally {
        // release the transaction
        repository.endTrans();
    }

Other Use-Cases

Please see MDR XMI API description for more info on how to load/save XMI files into/from repository extents.

Also for a more complete example of an application that uses MDR to load UML metamodel, instantiate it in the repository, load an UML model from XMI to the instance of UML metamodel, transform it to MOF (into an instance of MOF metamodel) and serialize it to XMI as an instance of MOF, please refer to the source code of the Main class of UML2MOF tool.


 

Built on May 3 2007.  |  Portions Copyright 1997-2005 Sun Microsystems, Inc. All rights reserved.