KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdrant > MdrTask


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.mdrant;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.tools.ant.Task;
26 import org.apache.tools.ant.BuildException;
27
28 import org.openide.util.Lookup;
29 import org.openide.util.lookup.ProxyLookup;
30 import org.openide.util.lookup.Lookups;
31
32 import org.netbeans.api.mdr.*;
33 import org.netbeans.mdr.NBMDRManagerImpl;
34 import org.netbeans.lib.jmi.xmi.*;
35 import org.netbeans.lib.jmi.mapping.JMIMapperImpl;
36
37 /** Main task for working with MDR. Specifies the repository to work
38  * on.
39  *
40  * @author Petr Hrebejk
41  */

42 public class MdrTask extends Task {
43
44     static MdrTask currentTask;
45         
46     // List of subtasks to be executed
47
private ArrayList JavaDoc subtasks;
48     
49     // Where the storage files reside
50
private String JavaDoc storageFile;
51     
52     // The repository to work on
53
MDRepository mdr;
54     
55         
56     /** Creates a new instance of Main */
57     public MdrTask() {
58         subtasks = new ArrayList JavaDoc();
59     }
60         
61     // Implementation of Ant Task ----------------------------------------------
62

63     /** Executes all subtasks
64      */

65     public void execute() throws BuildException {
66
67         // integrate logging between MDR and ant
68
MdrantLogger.setTask(this);
69
70         // wrap all MDR access in try/catch since methods like beginTrans
71
// can throw exceptions
72
try {
73             MDRManager mm = funnyClassLoaders(); // Beautiful hack to make the
74
// NetBeans lookup work
75
mdr = mm.getDefaultRepository();
76                 
77                 
78             for( Iterator JavaDoc it = subtasks.iterator(); it.hasNext(); ) {
79                 Sub subtask = (Sub)it.next();
80
81                 // beginTrans outside of try since beginTrans failure shouldn't
82
// be rolled back
83
mdr.beginTrans( true ); // Each subtask runs in it's own trans.
84
boolean rollback = true;
85                 try {
86                     subtask.execute();
87                     rollback = false;
88                 }
89                 finally {
90                     if ( rollback ) {
91                         // Exception => Rollback transaction
92
mdr.endTrans( true );
93                     }
94                 }
95                 mdr.endTrans( false ); // OK => Commit transaction
96
}
97         
98             mm.shutdownAll();
99         } catch ( Throwable JavaDoc t ) {
100             t.printStackTrace();
101             throw new BuildException(t);
102         } finally {
103             MdrantLogger.setTask(null);
104         }
105     }
106     
107     /** Initializes the repository.
108      */

109     public void init() throws BuildException {
110                 
111         
112     }
113     
114     // ANT task attributes -----------------------------------------------------
115

116     public void setStorageFile( String JavaDoc storageFile ) {
117         this.storageFile = storageFile;
118     }
119                               
120     // Creation and addition of subtasks ---------------------------------------
121

122         
123     public PrintExtentNames createPrintExtentNames( ) {
124         return new PrintExtentNames();
125     }
126     
127     public void addPrintExtentNames( PrintExtentNames subtask ) {
128         subtask.setTask( this );
129         subtasks.add( subtask );
130     }
131     
132     public Instantiate createInstantiate( ) {
133         return new Instantiate();
134     }
135     
136     public void addInstantiate( Instantiate subtask ) {
137         subtask.setTask( this );
138         subtasks.add( subtask );
139     }
140     
141     public ReadXMI createReadXMI( ) {
142         return new ReadXMI();
143     }
144     
145     public void addReadXMI( ReadXMI subtask ) {
146         subtask.setTask( this );
147         subtasks.add( subtask );
148     }
149     
150     public WriteXMI createWriteXMI( ) {
151         return new WriteXMI();
152     }
153     
154     public void addWriteXMI( WriteXMI subtask ) {
155         subtask.setTask( this );
156         subtasks.add( subtask );
157     }
158     
159     
160     public WriteDTD createWriteDTD( ) {
161         return new WriteDTD();
162     }
163     
164     public void addWriteDTD( WriteDTD subtask ) {
165         subtask.setTask( this );
166         subtasks.add( subtask );
167     }
168     
169     public MapJava createMapJava( ) {
170         return new MapJava();
171     }
172     
173     public void addMapJava( MapJava subtask ) {
174         subtask.setTask( this );
175         subtasks.add( subtask );
176     }
177     
178     public MapClass createMapClass( ) {
179         return new MapClass();
180     }
181     
182     public void addMapClass( MapClass subtask ) {
183         subtask.setTask( this );
184         subtasks.add( subtask );
185     }
186             
187     // Public innerclasses -----------------------------------------------------
188

189     public static abstract class Sub {
190     
191         protected MdrTask task;
192             
193         public Sub() {
194         }
195
196         void setTask( MdrTask task ) {
197             this.task = task;
198         }
199         
200         public abstract void execute() throws Exception JavaDoc;
201         
202         public final MDRepository getRepository() {
203             return task.mdr;
204         }
205     }
206     
207     // private methods ---------------------------------------------------------
208

209     private MDRManager funnyClassLoaders() {
210         
211         ClassLoader JavaDoc oldLoader = Thread.currentThread().getContextClassLoader();
212         Thread.currentThread().setContextClassLoader( this.getClass().getClassLoader() );
213                 
214         if ( storageFile != null ) {
215             System.getProperties().put( "org.netbeans.mdr.persistence.Dir", storageFile );
216         }
217         
218         MDRManager mm = MDRManager.getDefault();
219         
220         Thread.currentThread().setContextClassLoader( oldLoader );
221         
222         return mm;
223     }
224         
225     
226     // For testing purposes only -----------------------------------------------
227

228     public static void main( String JavaDoc args[] ) {
229         
230         MdrTask mt = new MdrTask();
231         mt.execute();
232         
233     }
234     
235 }
236     
237
Popular Tags