alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.mdrant;

import java.util.ArrayList;
import java.util.Iterator;

import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;

import org.openide.util.Lookup;
import org.openide.util.lookup.ProxyLookup;
import org.openide.util.lookup.Lookups;

import org.netbeans.api.mdr.*;
import org.netbeans.mdr.NBMDRManagerImpl;
import org.netbeans.lib.jmi.xmi.*;
import org.netbeans.lib.jmi.mapping.JMIMapperImpl;

/** Main task for working with MDR. Specifies the repository to work
 *  on.
 *
 * @author  Petr Hrebejk
 */
public class MdrTask extends Task {
        
    // List of subtasks to be executed
    private ArrayList subtasks;
    
    // Where the storage files reside
    private String storageFile;
    
    // The repository to work on
    MDRepository mdr;
    
        
    /** Creates a new instance of Main */
    public MdrTask() {
        subtasks = new ArrayList();
    }
        
    // Implementation of Ant Task ----------------------------------------------

    /** Executes all subtasks
     */
    public void execute() throws BuildException {
                
        MDRManager mm = funnyClassLoaders(); // Beautiful hack to make the
                                             // NetBeans lookup work        
        mdr = mm.getDefaultRepository();
                
                
        for( Iterator it = subtasks.iterator(); it.hasNext(); ) {
            Sub subtask = (Sub)it.next();

            // beginTrans outside of try since beginTrans failure shouldn't
            // be rolled back
            mdr.beginTrans( true ); // Each subtask runs in it's own trans.
            try {
                subtask.execute();
            }
            catch ( Throwable t ) {
                mdr.endTrans( true ); // Exception => Rollback transaction
                t.printStackTrace();
                throw new BuildException( t );
            }
            mdr.endTrans( false ); // OK => Commit transaction
        }               
        
        mm.shutdownAll();
    }
    
    /** Initializes the repository.
     */
    public void init() throws BuildException {          
                
        
    }
    
    // ANT task attributes -----------------------------------------------------
    
    public void setStorageFile( String storageFile ) {
        this.storageFile = storageFile;
    }
                              
    // Creation and addition of subtasks ---------------------------------------
    
        
    public PrintExtentNames createPrintExtentNames( ) {
        return new PrintExtentNames();
    }
    
    public void addPrintExtentNames( PrintExtentNames subtask ) {
        subtask.setTask( this );
        subtasks.add( subtask );
    }
    
    public Instantiate createInstantiate( ) {
        return new Instantiate();
    }
    
    public void addInstantiate( Instantiate subtask ) {
        subtask.setTask( this );
        subtasks.add( subtask );
    }
    
    public ReadXMI createReadXMI( ) {
        return new ReadXMI();
    }
    
    public void addReadXMI( ReadXMI subtask ) {
        subtask.setTask( this );
        subtasks.add( subtask );
    }
    
    public WriteXMI createWriteXMI( ) {
        return new WriteXMI();
    }
    
    public void addWriteXMI( WriteXMI subtask ) {
        subtask.setTask( this );
        subtasks.add( subtask );
    }
    
    
    public WriteDTD createWriteDTD( ) {
        return new WriteDTD();
    }
    
    public void addWriteDTD( WriteDTD subtask ) {
        subtask.setTask( this );
        subtasks.add( subtask );
    }
    
    public MapJava createMapJava( ) {
        return new MapJava();
    }
    
    public void addMapJava( MapJava subtask ) {
        subtask.setTask( this );
        subtasks.add( subtask );
    }
    
    public MapClass createMapClass( ) {
        return new MapClass();
    }
    
    public void addMapClass( MapClass subtask ) {
        subtask.setTask( this );
        subtasks.add( subtask );
    }
            
    // Public innerclasses -----------------------------------------------------
    
    public static abstract class Sub {
    
        protected MdrTask task;
            
        public Sub() {
        }

        void setTask( MdrTask task ) {
            this.task = task;
        }
        
        public abstract void execute() throws Exception;
        
        public final MDRepository getRepository() {
            return task.mdr;
        }
    }
    
    // private methods ---------------------------------------------------------
    
    private MDRManager funnyClassLoaders() {
        
        ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();                
        Thread.currentThread().setContextClassLoader( this.getClass().getClassLoader() );
                
        if ( storageFile != null ) {
            System.getProperties().put( "org.netbeans.mdr.persistence.Dir", storageFile );
        }
        
        MDRManager mm = MDRManager.getDefault();
        
        Thread.currentThread().setContextClassLoader( oldLoader );
        
        return mm;
    }
        
    
    // For testing purposes only -----------------------------------------------
    
    public static void main( String args[] ) {
        
        MdrTask mt = new MdrTask();
        mt.execute();
        
    }
    
}
    
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.