Discussion:
[mdr-users] Fwd: how can I load two xmi files in repository?
Marcelo Pitanga
2006-09-18 12:55:05 UTC
Permalink
Hi All,

Somebody can help me?

I need to load two models (for example model1.xmi and model2.xmi) in
repository at the same time.

my code:
*Code:*

private static MDRepositoryFacade repository1 = null;
private static MDRepositoryFacade repository2 = null;
private static org.omg.uml.UmlPackage umlPackage1 = null;
private static CorePackage corePackage1 = null;
private static org.omg.uml.UmlPackage umlPackage2 = null;
private static CorePackage corePackage2 = null;

private static URL getUrl(String fileName)
{
try
{
fileName = fileName.replace("\\", "/");
String jarUrl = fileName;
if (!fileName.startsWith("file:/")) {
jarUrl = "file:/"+fileName;
}
if (fileName.endsWith(".zargo") || (fileName.endsWith(".zuml"))||
(fileName.endsWith(".zip"))) {
String[] temp = fileName.split("/");
String arquivo = temp[(temp.length-1)];
String[] ext = arquivo.split("\\.");
arquivo = arquivo.substring(0, arquivo.lastIndexOf(ext[
ext.length-1])-1);
String extensao = ".xmi";
if (fileName.endsWith(".zip")) {
extensao = "";
}
jarUrl = "jar:" + jarUrl + "!/"+arquivo+extensao;
}
URL modelUri = new URL(jarUrl);
if (modelUri == null)
{
throw new RuntimeException("Não foi possivel carregar '" +
jarUrl + "'");
}
return modelUri;
}
catch (Throwable th)
{
String errMsg = "Erro ao executar getUrl() ";
throw new RuntimeException(errMsg, th);
}
}

private static Model getModel(org.omg.uml.UmlPackage umlPackage, String
model) {
ModelManagementPackage modelManagementPackage =
umlPackage.getModelManagement();
for (Iterator iter =
modelManagementPackage.getModel().refAllOfType().iterator();
iter.hasNext();) {
Model m = (Model)iter.next();
if (m.getName().equals(model)) {
return m;
}
}
return null;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String fileAspectXmi =
"C:/Java/workspace/CrossMDA/model_aspects.zuml";
String fileXmi = "C:/Java/workspace/CrossMDA/model_teste.zuml";

repository1 = new MDRepositoryFacade();
repository1.open();
System.out.println("Open model 1");
repository1.readModel(getUrl(fileAspectXmi), new String[] {});
if (!(repository1.getModel().getModel() instanceof
org.omg.uml.UmlPackage)) {
throw new Exception("Model1 is invalid!");
}

umlPackage1 =
(org.omg.uml.UmlPackage)repository1.getModel().getModel();

corePackage1 = umlPackage1.getModelManagement().getCore();
Model m1 = getModel(umlPackage1,"ModelAspects");
if (m1 != null) {
System.out.println(m1.getName()+" found.");
} else {
System.out.println("Modelo1 not found.");
}

System.out.println("Open model 2");
repository2 = new MDRepositoryFacade();
repository2.open();
repository2.readModel(getUrl(fileXmi), new String[] {});
if (!(repository2.getModel().getModel() instanceof
org.omg.uml.UmlPackage)) {
throw new Exception("Model2 is invalid!");
}
umlPackage2 =
(org.omg.uml.UmlPackage)repository2.getModel().getModel();

corePackage2 = umlPackage2.getModelManagement().getCore();
Model m2 = getModel(umlPackage2,"Data");
if (m2 != null) {
System.out.println(m2.getName()+" found.");
} else {
System.out.println("Modelo2 not found");
}

// printing model name from repository1
System.out.println(m1.getName());

repository1.clear();
repository1.close();

repository2.clear();
repository2.close();


} catch (Exception e) {
e.printStackTrace();
}

}


Result after execution

*Quote:*
Open model 1
ModelAspects found.
Open model 2
log4j:WARN No appenders could be found for logger (
org.andromda.repositories.mdr.MDRepositoryFacade).
log4j:WARN Please initialize the log4j system properly.
Data found.

javax.jmi.reflect.InvalidObjectException: Object with MOFID
.:00000000000007CA no longer exists.
at org.netbeans.mdr.handlers.BaseObjectHandler._getDelegate(
BaseObjectHandler.java:353)
at org.netbeans.mdr.handlers.InstanceHandler.getInstanceDelegate(
InstanceHandler.java:4
at org.netbeans.mdr.handlers.InstanceHandler._handleGet(InstanceHandler.java:193)

at org.omg.uml.modelmanagement.Model$Impl.getName(Unknown Source)
at br.ufrj.nce.crossmda.teste.TesteCarga.main(TesteCarga.java:121)


This error occurs when I try access at some element from first model after
second model loaded.

Some idea to solve it?

Loading...