1 19 20 package org.netbeans.mdrant; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 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 42 public class MdrTask extends Task { 43 44 static MdrTask currentTask; 45 46 private ArrayList subtasks; 48 49 private String storageFile; 51 52 MDRepository mdr; 54 55 56 57 public MdrTask() { 58 subtasks = new ArrayList (); 59 } 60 61 63 65 public void execute() throws BuildException { 66 67 MdrantLogger.setTask(this); 69 70 try { 73 MDRManager mm = funnyClassLoaders(); mdr = mm.getDefaultRepository(); 76 77 78 for( Iterator it = subtasks.iterator(); it.hasNext(); ) { 79 Sub subtask = (Sub)it.next(); 80 81 mdr.beginTrans( true ); boolean rollback = true; 85 try { 86 subtask.execute(); 87 rollback = false; 88 } 89 finally { 90 if ( rollback ) { 91 mdr.endTrans( true ); 93 } 94 } 95 mdr.endTrans( false ); } 97 98 mm.shutdownAll(); 99 } catch ( Throwable t ) { 100 t.printStackTrace(); 101 throw new BuildException(t); 102 } finally { 103 MdrantLogger.setTask(null); 104 } 105 } 106 107 109 public void init() throws BuildException { 110 111 112 } 113 114 116 public void setStorageFile( String storageFile ) { 117 this.storageFile = storageFile; 118 } 119 120 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 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 ; 201 202 public final MDRepository getRepository() { 203 return task.mdr; 204 } 205 } 206 207 209 private MDRManager funnyClassLoaders() { 210 211 ClassLoader 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 228 public static void main( String args[] ) { 229 230 MdrTask mt = new MdrTask(); 231 mt.execute(); 232 233 } 234 235 } 236 237 | Popular Tags |