[mdr-users] Fwd: how can I load two xmi files in repository?

"Marcelo Pitanga" <[email protected]> Mon, 18 Sep 2006 09:55:05 -0300
Newsgroups gmane.comp.java.netbeans.modules.mdr.user
Message-ID <[email protected]>
------=_Part_47854_23942098.1158584105350
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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 =3D null;
   private static MDRepositoryFacade repository2 =3D null;
   private static org.omg.uml.UmlPackage umlPackage1 =3D null;
   private static CorePackage corePackage1 =3D null;
   private static org.omg.uml.UmlPackage umlPackage2 =3D null;
   private static CorePackage corePackage2 =3D null;

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

   private static Model getModel(org.omg.uml.UmlPackage umlPackage, String
model) {
      ModelManagementPackage modelManagementPackage =3D
umlPackage.getModelManagement();
        for (Iterator iter =3D
modelManagementPackage.getModel().refAllOfType().iterator();
iter.hasNext();) {
           Model m =3D (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 =3D
"C:/Java/workspace/CrossMDA/model_aspects.zuml";
         String fileXmi =3D "C:/Java/workspace/CrossMDA/model_teste.zuml";

         repository1 =3D 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 =3D
(org.omg.uml.UmlPackage)repository1.getModel().getModel();

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

         System.out.println("Open model 2");
         repository2 =3D 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 =3D
(org.omg.uml.UmlPackage)repository2.getModel().getModel();

         corePackage2 =3D umlPackage2.getModelManagement().getCore();
         Model m2 =3D getModel(umlPackage2,"Data");
         if (m2 !=3D 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.jav=
a: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?

------=_Part_47854_23942098.1158584105350
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

<span class=3D"gmail_quote"></span>Hi All,
<br><div><span>

<br>
Somebody can help me?
<br>

<br>
I need to load two models (for example model1.xmi and model2.xmi) in reposi=
tory at the same time.
<br>

<br>
my code:
<br>
</span><table align=3D"center" border=3D"0" cellpadding=3D"3" cellspacing=
=3D"1" width=3D"90%"><tbody><tr> =09  <td><span><b>Code:</b></span></td>=09=
</tr>=09<tr>=09  <td>
<br>

<br>
&nbsp; &nbsp;private static MDRepositoryFacade repository1 =3D null;
<br>
&nbsp; &nbsp;private static MDRepositoryFacade repository2 =3D null;
<br>
&nbsp; &nbsp;private static org.omg.uml.UmlPackage umlPackage1 =3D null;
<br>
&nbsp; &nbsp;private static CorePackage corePackage1 =3D null;
<br>
&nbsp; &nbsp;private static org.omg.uml.UmlPackage umlPackage2 =3D null;
<br>
&nbsp; &nbsp;private static CorePackage corePackage2 =3D null;
<br>

<br>
&nbsp; &nbsp;private static URL getUrl(String fileName)
<br>
&nbsp; &nbsp; {
<br>
&nbsp; &nbsp; &nbsp; &nbsp; try
<br>
&nbsp; &nbsp; &nbsp; &nbsp; {
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fileName =3D fileName.replace(&quo=
t;\\&quot;, &quot;/&quot;);
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String jarUrl =3D fileName;
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (!fileName.startsWith(&quot;fil=
e:/&quot;)) {
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;jarUrl =3D &quot;file=
:/&quot;+fileName;
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
<br>
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;if (fileName.endsWith(&quot;.zargo&=
quot;) || (fileName.endsWith(&quot;.zuml&quot;))|| (fileName.endsWith(&quot=
;.zip&quot;))) {
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String[] temp =3D fi=
leName.split(&quot;/&quot;);
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String arquivo =3D t=
emp[(temp.length-1)];
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String[] ext =3D arq=
uivo.split(&quot;\\.&quot;);
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arquivo =3D arquivo.=
substring(0, arquivo.lastIndexOf(ext[ext.length-1])-1);
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String extensao =3D =
&quot;.xmi&quot;;
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (fileName.endsWit=
h(&quot;.zip&quot;)) {
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;extensa=
o =3D &quot;&quot;;
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;jarUrl =
=3D &quot;jar:&quot; + jarUrl + &quot;!/&quot;+arquivo+extensao;
<br>
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;}
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;URL&nbsp; modelUri =3D new URL(jar=
Url);
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (modelUri =3D=3D null)
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;throw new RuntimeExce=
ption(&quot;N=E3o foi possivel carregar '&quot; + jarUrl + &quot;'&quot;);
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return modelUri;
<br>
&nbsp; &nbsp; &nbsp; &nbsp; }
<br>
&nbsp; &nbsp; &nbsp; &nbsp; catch (Throwable th)
<br>
&nbsp; &nbsp; &nbsp; &nbsp; {
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String errMsg =3D &quot;Erro ao e=
xecutar getUrl() &quot;;
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException(errMsg=
, th);
<br>
&nbsp; &nbsp; &nbsp; &nbsp; }
<br>
&nbsp; &nbsp; }
<br>

<br>
&nbsp; &nbsp;private static Model getModel(org.omg.uml.UmlPackage umlPackag=
e, String model) {
<br>
&nbsp; &nbsp;&nbsp; &nbsp;ModelManagementPackage modelManagementPackage =3D=
 umlPackage.getModelManagement();
<br>
&nbsp; &nbsp; &nbsp; &nbsp; for (Iterator iter =3D modelManagementPackage.g=
etModel().refAllOfType().iterator(); iter.hasNext();) {
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Model m =3D (Model)iter.next();=20
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (m.getName().equals(model)) {
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;return m;
<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
<br>
&nbsp; &nbsp; &nbsp; &nbsp; }
<br>
&nbsp; &nbsp; &nbsp; &nbsp;return null;
<br>
&nbsp; &nbsp; }
<br>
&nbsp; &nbsp;
<br>
&nbsp; &nbsp;/**
<br>
&nbsp; &nbsp; * @param args
<br>
&nbsp; &nbsp; */
<br>
&nbsp; &nbsp;public static void main(String[] args) {
<br>
&nbsp; &nbsp;&nbsp; &nbsp;// TODO Auto-generated method stub
<br>
&nbsp; &nbsp;&nbsp; &nbsp;try {
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;String fileAspectXmi =3D &quot;C:/Ja=
va/workspace/CrossMDA/model_aspects.zuml&quot;;
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;String fileXmi =3D &quot;C:/Java/wor=
kspace/CrossMDA/model_teste.zuml&quot;;
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;repository1 =3D new MDRepositoryFaca=
de();
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;repository1.open();
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;System.out.println(&quot;Open model =
1&quot;);
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;repository1.readModel(getUrl(fileAsp=
ectXmi), new String[] {});
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (!(repository1.getModel().getMode=
l() instanceof org.omg.uml.UmlPackage)) {
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;throw new Exception(&qu=
ot;Model1 is invalid!&quot;);
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;umlPackage1 =3D (org.omg.uml.UmlPack=
age)repository1.getModel().getModel();
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;corePackage1 =3D umlPackage1.getMode=
lManagement().getCore();
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Model m1 =3D getModel(umlPackage1,&q=
uot;ModelAspects&quot;);
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (m1 !=3D null) {
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;System.out.println(m1.g=
etName()+&quot; found.&quot;);
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;} else {
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;System.out.println(&quo=
t;Modelo1 not found.&quot;);
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;System.out.println(&quot;Open model =
2&quot;);
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;repository2 =3D new MDRepositoryFaca=
de();
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;repository2.open();
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;repository2.readModel(getUrl(fileXmi=
), new String[] {});
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (!(repository2.getModel().getMode=
l() instanceof org.omg.uml.UmlPackage)) {
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;throw new Exception(&qu=
ot;Model2 is invalid!&quot;);
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;umlPackage2 =3D (org.omg.uml.UmlPack=
age)repository2.getModel().getModel();
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;corePackage2 =3D umlPackage2.getMode=
lManagement().getCore();
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Model m2 =3D getModel(umlPackage2,&q=
uot;Data&quot;);
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (m2 !=3D null) {
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;System.out.println(m2.g=
etName()+&quot; found.&quot;);
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;} else {
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;System.out.println(&quo=
t;Modelo2 not found&quot;);
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;// printing model name from reposito=
ry1
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;System.out.println(m1.getName());
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;repository1.clear();
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;repository1.close();
<br>

<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;repository2.clear();
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;repository2.close();
<br>

<br>

<br>
&nbsp; &nbsp;&nbsp; &nbsp;} catch (Exception e) {
<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;e.printStackTrace();
<br>
&nbsp; &nbsp;&nbsp; &nbsp;}
<br>

<br>
&nbsp; &nbsp;}
<br>
</td>=09</tr></tbody></table><span>
<br>

<br>
Result after execution
<br>

<br>
</span><table align=3D"center" border=3D"0" cellpadding=3D"3" cellspacing=
=3D"1" width=3D"90%"><tbody><tr> =09  <td><span><b>Quote:</b></span></td>=
=09</tr>=09<tr>=09  <td>
<br>
Open model 1
<br>
ModelAspects found.
<br>
Open model 2
<br>
log4j:WARN No appenders could be found for logger (org.andromda.repositorie=
s.mdr.MDRepositoryFacade).
<br>
log4j:WARN Please initialize the log4j system properly.
<br>
Data found.
<br><br>
javax.jmi.reflect.InvalidObjectException: Object with MOFID .:0000000000000=
7CA no longer exists.
<br>
=09at org.netbeans.mdr.handlers.BaseObjectHandler._getDelegate(BaseObjectHa=
ndler.java:353)
<br>
=09at org.netbeans.mdr.handlers.InstanceHandler.getInstanceDelegate(Instanc=
eHandler.java:4
<br>
=09at org.netbeans.mdr.handlers.InstanceHandler._handleGet(InstanceHandler.=
java:193)
<br>
=09at org.omg.uml.modelmanagement.Model$Impl.getName(Unknown Source)
<br>
=09at br.ufrj.nce.crossmda.teste.TesteCarga.main(TesteCarga.java:121)
<br>
</td>=09</tr></tbody></table><span>
<br>

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

<br>
Some idea to solve it?<br><br></span>

</div>

------=_Part_47854_23942098.1158584105350--