1 19 package org.netbeans.lib.jmi.util; 20 21 import java.io.InputStream ; 22 import java.net.URL ; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.Enumeration ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import java.util.jar.Attributes ; 30 import java.util.jar.Manifest ; 31 import javax.jmi.model.ModelPackage; 32 import javax.jmi.model.MofPackage; 33 import javax.jmi.model.Tag; 34 import javax.jmi.reflect.RefPackage; 35 import javax.jmi.xmi.XmiReader; 36 import org.netbeans.api.mdr.MDRepository; 37 import org.openide.util.Lookup; 38 39 43 public class MetamodelManager { 44 protected final MDRepository repository; 45 private final Class cls; 46 private Map cache; 47 private ModelPackage extent; 48 private String extentName; 49 private String version; 50 private String fileName; 51 52 53 public MetamodelManager(MDRepository repository, Class cls) { 54 this.repository = repository; 55 this.cls = cls; 56 } 57 58 public synchronized MofPackage findRootPackage(String packageName) { 59 if (extent == null) { 60 update(); 61 } 62 if (cache == null) { 63 cache = new HashMap (); 64 for (Iterator it = extent.getMofPackage().refAllOfClass().iterator(); it.hasNext();) { 65 MofPackage pkg = (MofPackage) it.next(); 66 if (pkg.getContainer() == null) { 67 cache.put(pkg.getName(), pkg); 68 } 69 } 70 } 71 72 return (MofPackage) cache.get(packageName); 73 } 74 75 protected synchronized String getVersion() { 76 initCheck(); 77 return version; 78 } 79 80 protected synchronized String getExtentName() { 81 initCheck(); 82 return extentName; 83 } 84 85 private void initCheck() { 86 if (fileName == null) { 87 try { 88 Manifest mf = new Manifest (cls.getResourceAsStream("/META-INF/MANIFEST.MF")); 89 Attributes attrs = mf.getMainAttributes(); 90 extentName = attrs.getValue("NBMDR-Metamodel"); 91 if (extentName == null) { 92 Enumeration e = cls.getClassLoader().getResources("META-INF/MANIFEST.MF"); 93 while (e.hasMoreElements() && extentName == null) { 94 URL u = (URL ) e.nextElement(); 95 InputStream is = u.openStream(); 96 mf = new Manifest (is); 97 attrs = mf.getMainAttributes(); 98 extentName = attrs.getValue("NBMDR-Metamodel"); 99 } 100 } 101 fileName = "/" + attrs.getValue("NBMDR-Metamodel-File"); 102 version = attrs.getValue("NBMDR-Metamodel-Version"); 103 } catch (java.io.IOException e) { 104 throw new RuntimeException (e.getMessage()); 105 } 106 } 107 } 108 109 private void update() { 110 initCheck(); 111 boolean fail = true; 112 repository.beginTrans(true); 113 try { 114 115 Logger.getDefault().log("Looking for extent " + extentName + " in repository " + repository); 117 extent = (ModelPackage) repository.getExtent(extentName); 118 119 if (extent != null) { 120 Tag tag = null; 122 for (Iterator it = extent.getTag().refAllOfClass().iterator(); it.hasNext();) { 123 Tag temp = (Tag) it.next(); 124 if ("org.netbeans.version".equals(temp.getTagId())) { 125 tag = temp; 126 break; 127 } 128 } 129 if (tag == null || tag.getValues().isEmpty() || 132 !tag.getValues().iterator().next().equals(version)) { 133 Logger.getDefault().log("Metamodel version is outdated - it needs to be reloaded."); 134 String names[] = repository.getExtentNames(); 135 for (int i = 0; i < names.length; i++) { 136 RefPackage tmp = repository.getExtent(names[i]); 137 if (!(tmp instanceof ModelPackage)) { 138 tmp.refDelete(); 139 } 140 } 141 extent.refDelete(); 142 extent = null; 143 } 144 } else { 145 Logger.getDefault().log("Extent not found."); 146 } 147 148 if (extent == null) { 149 extent = (ModelPackage) repository.createExtent(extentName); 151 XmiReader xmr = (XmiReader) Lookup.getDefault().lookup(XmiReader.class); 152 Collection outermostElements = xmr.read(cls.getResource(fileName).toString(), extent); 153 extent.getTag().createTag("org.netbeans.version", "", "org.netbeans.version", Arrays.asList(new String [] {version})); 154 cache = new HashMap (); 156 for (Iterator it = outermostElements.iterator(); it.hasNext();) { 157 Object temp = it.next(); 158 if (temp instanceof MofPackage) { 159 cache.put(((MofPackage) temp).getName(), temp); 160 } 161 } 162 } 163 fail = false; 164 } catch (Exception ex) { 165 throw (IllegalStateException ) Logger.getDefault().annotate(new IllegalStateException ("Metamodel XMI malformed."), ex); 166 } finally { 167 repository.endTrans(fail); 168 } 169 } 170 } 171 | Popular Tags |