KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > jmi > util > MetamodelManager


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 package org.netbeans.lib.jmi.util;
20
21 import java.io.InputStream JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.jar.Attributes JavaDoc;
30 import java.util.jar.Manifest JavaDoc;
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 /**
40  *
41  * @author Martin Matula
42  */

43 public class MetamodelManager {
44     protected final MDRepository repository;
45     private final Class JavaDoc cls;
46     private Map JavaDoc cache;
47     private ModelPackage extent;
48     private String JavaDoc extentName;
49     private String JavaDoc version;
50     private String JavaDoc fileName;
51     
52     /** Creates a new instance of MetamodelManager */
53     public MetamodelManager(MDRepository repository, Class JavaDoc cls) {
54         this.repository = repository;
55         this.cls = cls;
56     }
57     
58     public synchronized MofPackage findRootPackage(String JavaDoc packageName) {
59         if (extent == null) {
60             update();
61         }
62         if (cache == null) {
63             cache = new HashMap JavaDoc();
64             for (Iterator JavaDoc 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 JavaDoc getVersion() {
76         initCheck();
77         return version;
78     }
79     
80     protected synchronized String JavaDoc getExtentName() {
81         initCheck();
82         return extentName;
83     }
84     
85     private void initCheck() {
86         if (fileName == null) {
87             try {
88                 Manifest JavaDoc mf = new Manifest JavaDoc(cls.getResourceAsStream("/META-INF/MANIFEST.MF"));
89                 Attributes JavaDoc attrs = mf.getMainAttributes();
90                 extentName = attrs.getValue("NBMDR-Metamodel");
91                 if (extentName == null) {
92                     Enumeration JavaDoc e = cls.getClassLoader().getResources("META-INF/MANIFEST.MF");
93                     while (e.hasMoreElements() && extentName == null) {
94                         URL JavaDoc u = (URL JavaDoc) e.nextElement();
95                         InputStream JavaDoc is = u.openStream();
96                         mf = new Manifest JavaDoc(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 JavaDoc e) {
104                 throw new RuntimeException JavaDoc(e.getMessage());
105             }
106         }
107     }
108     
109     private void update() {
110         initCheck();
111         boolean fail = true;
112         repository.beginTrans(true);
113         try {
114
115             // [PENDING] should check whether the package is really instance of MOF
116
Logger.getDefault().log("Looking for extent " + extentName + " in repository " + repository);
117             extent = (ModelPackage) repository.getExtent(extentName);
118             
119             if (extent != null) {
120                 // extent was found -> check if it contains correct versions of packages
121
Tag tag = null;
122                 for (Iterator JavaDoc 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 the version tag was not found, or the version is incorrect,
130
// reinstall the model
131
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 JavaDoc 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 needs to be created
150
extent = (ModelPackage) repository.createExtent(extentName);
151                 XmiReader xmr = (XmiReader) Lookup.getDefault().lookup(XmiReader.class);
152                 Collection JavaDoc outermostElements = xmr.read(cls.getResource(fileName).toString(), extent);
153                 extent.getTag().createTag("org.netbeans.version", "", "org.netbeans.version", Arrays.asList(new String JavaDoc[] {version}));
154                 // cache outermost packages
155
cache = new HashMap JavaDoc();
156                 for (Iterator JavaDoc it = outermostElements.iterator(); it.hasNext();) {
157                     Object JavaDoc temp = it.next();
158                     if (temp instanceof MofPackage) {
159                         cache.put(((MofPackage) temp).getName(), temp);
160                     }
161                 }
162             }
163             fail = false;
164         } catch (Exception JavaDoc ex) {
165             throw (IllegalStateException JavaDoc) Logger.getDefault().annotate(new IllegalStateException JavaDoc("Metamodel XMI malformed."), ex);
166         } finally {
167             repository.endTrans(fail);
168         }
169     }
170 }
171
Popular Tags