public String getPropertyValue(String managed_element_uuid, String name) {
   ManagedElement me1 = getLocalElement(managed_element_uuid);
   if (me1 != null) {
     // System.out.println(".... get property value (local) of element: " + managed_element_uuid);
     return me1.getProperty(name);
   } else {
     // System.out.println(".... get property value (remote) of element: " + managed_element_uuid);
     String auri = agent.getExternalAgentUri(managed_element_uuid);
     if (auri != null) {
       CMessage msg = new CMessage();
       msg.setTo(auri);
       msg.setObject("runtimemodel");
       msg.setBody("getPropertyValue");
       msg.addHeader("uuid", managed_element_uuid);
       msg.addHeader("name", name);
       try {
         CMessage resultmsg = sendAndWait(msg);
         if (resultmsg != null) {
           if (resultmsg.getBody() != null) {
             return resultmsg.getBody().toString();
           }
         }
       } catch (TimeOutException e) {
         e.printStackTrace();
       }
     } else {
       System.out.println(
           ".... get property value: impossible to find agent uri of instance "
               + managed_element_uuid);
     }
   }
   return null;
 }
  public boolean addProperty(String managed_element_uuid, String name, String value)
      throws PropertyExistException, InvalidNameException {
    ManagedElement me1 = getLocalElement(managed_element_uuid);
    if (me1 != null) {
      return me1.addProperty(name, value);
    } else {
      String auri = agent.getExternalAgentUri(managed_element_uuid);
      if (auri != null) {
        CMessage msg = new CMessage();
        msg.setTo(auri);
        msg.setObject("runtimemodel");
        msg.setBody("addProperty");
        msg.addHeader("uuid", managed_element_uuid);
        msg.addHeader("name", name);
        msg.addHeader("value", value);
        try {
          CMessage resultmsg = sendAndWait(msg);
          if (resultmsg != null) {
            if (resultmsg.getBody() != null) {
              if (resultmsg.getBody().toString().equalsIgnoreCase("true")) {
                return true;
              }
            }
          }
        } catch (TimeOutException e) {
          e.printStackTrace();
        }
      }
    }

    return false; // To change body of implemented methods use File | Settings | File Templates.
  }
 public boolean hasReferencedElement(
     String managed_element_uuid, String reference_name, String referenced_element_uuri) {
   ManagedElement me1 = getLocalElement(managed_element_uuid);
   if (me1 != null) {
     Reference r = me1.getReference(reference_name);
     if (r != null) {
       return r.hasReferencedElement(referenced_element_uuri);
     }
   } else {
     String auri = agent.getExternalAgentUri(managed_element_uuid);
     if (auri != null) {
       CMessage msg = new CMessage();
       msg.setTo(auri);
       msg.setObject("runtimemodel");
       msg.setBody("hasReferencedElement");
       msg.addHeader("uuid", managed_element_uuid);
       msg.addHeader("name", reference_name);
       msg.addHeader("refuuid", referenced_element_uuri);
       try {
         CMessage resultmsg = sendAndWait(msg);
         if (resultmsg != null) {
           if (resultmsg.getBody() != null) {
             if (resultmsg.getBody().toString().equalsIgnoreCase("true")) {
               return true;
             }
           }
         }
       } catch (TimeOutException e) {
         e.printStackTrace();
       }
     }
   }
   return false;
 }
 public boolean addReferencedElement(
     String managed_element_uuid,
     String reference_name,
     boolean onlyone,
     String referenced_element_uuid)
     throws InvalidNameException {
   ManagedElement me1 = getLocalElement(managed_element_uuid);
   if (me1 != null) {
     // System.out.println("add Local Reference Element ...");
     Reference r = me1.getReference(reference_name);
     if (r == null) {
       // System.out.println("Reference '"+reference_name+"' does not exists! we will create it!");
       r = me1.addReference(reference_name, onlyone);
     }
     if (r != null) {
       r.addReferencedElement(referenced_element_uuid);
       return true;
     }
   } else {
     // System.out.println("add Remote Reference Element ...");
     String auri = agent.getExternalAgentUri(managed_element_uuid);
     if (auri != null) {
       CMessage msg = new CMessage();
       msg.setTo(auri);
       msg.setObject("runtimemodel");
       msg.setBody("addReferencedElement");
       msg.addHeader("uuid", managed_element_uuid);
       msg.addHeader("name", reference_name);
       msg.addHeader("onlyone", onlyone == true ? "true" : "false");
       msg.addHeader("refuuid", referenced_element_uuid);
       try {
         CMessage resultmsg = sendAndWait(msg);
         if (resultmsg != null) {
           if (resultmsg.getBody() != null) {
             if (resultmsg.getBody().toString().equalsIgnoreCase("true")) {
               return true;
             }
           }
         }
       } catch (TimeOutException e) {
         e.printStackTrace();
       }
     } else {
       System.out.println("External agent of element '" + managed_element_uuid + "' not found!");
     }
   }
   return false;
 }
  public List<String> getReferencedElements(String managed_element_uuid, String reference_name) {

    List<String> result = new ArrayList<String>();
    ManagedElement me1 = getLocalElement(managed_element_uuid);
    if (me1 != null) {
      Reference r = me1.getReference(reference_name);
      if (r != null) {
        return r.getReferencedElements();
      } else {
        // System.out.println("CONTROLLER: no reference with name: " + reference_name);
      }
    } else {
      String auri = agent.getExternalAgentUri(managed_element_uuid);
      if (auri != null) {
        CMessage msg = new CMessage();
        msg.setTo(auri);
        msg.setObject("runtimemodel");
        msg.setBody("getReferencedElements");
        msg.addHeader("uuid", managed_element_uuid);
        msg.addHeader("name", reference_name);
        try {
          CMessage resultmsg = sendAndWait(msg);
          if (resultmsg != null) {
            if (resultmsg.getBody() != null) {
              String[] tmp = resultmsg.getBody().toString().split(",");
              for (int i = 0; i < tmp.length; i++) {
                if (tmp[i] != null && tmp[i].length() > 0) {
                  String[] tmp2 = tmp[i].split("###");
                  String agenturi = tmp2[0];
                  String elementuuid = tmp2[1];
                  if (!agenturi.equalsIgnoreCase(getCubeAgent().getUri())) {
                    this.agent.addExternalElement(elementuuid, agenturi);
                  }
                  result.add(elementuuid);
                }
              }
            }
          }
        } catch (TimeOutException e) {
          e.printStackTrace();
        }
      }
    }
    return result;
  }
 public ManagedElement getRemoteElement(String managed_element_uuid) {
   String auri = agent.getExternalAgentUri(managed_element_uuid);
   if (auri != null) {
     CMessage msg = new CMessage();
     msg.setTo(auri);
     msg.setObject("runtimemodel");
     msg.setBody("getRemoteElement");
     msg.addHeader("uuid", managed_element_uuid);
     try {
       CMessage resultmsg = sendAndWait(msg);
       if (resultmsg != null) {
         return resultmsg.getAttachement();
       }
     } catch (TimeOutException e) {
       e.printStackTrace();
     }
   }
   return null;
 }
  protected void handleMessage(CMessage msg) throws Exception {

    if (msg != null) {
      if (msg.getBody() != null) {
        if (msg.getBody().toString().equalsIgnoreCase("getPropertyValue")) {
          Object uuid = msg.getHeader("uuid");
          Object name = msg.getHeader("name");

          String p = null;
          if (uuid != null && name != null) {
            p = getPropertyValue(uuid.toString(), name.toString());
          }
          CMessage resmsg = new CMessage();
          resmsg.setTo(msg.getFrom());
          resmsg.setCorrelation(msg.getCorrelation());
          resmsg.setObject(msg.getObject());
          resmsg.setBody(p);
          try {
            getCubeAgent().getCommunicator().sendMessage(resmsg);
          } catch (CommunicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        } else if (msg.getBody().toString().equalsIgnoreCase("addProperty")) {
          Object uuid = msg.getHeader("uuid");
          Object name = msg.getHeader("name");
          Object value = msg.getHeader("value");

          boolean p = false;
          if (uuid != null && name != null && value != null) {
            p = addProperty(uuid.toString(), name.toString(), value.toString());
          }
          CMessage resmsg = new CMessage();
          resmsg.setTo(msg.getFrom());
          resmsg.setCorrelation(msg.getCorrelation());
          resmsg.setObject(msg.getObject());
          if (p == true) resmsg.setBody("true");
          else resmsg.setBody("false");
          try {
            getCubeAgent().getCommunicator().sendMessage(resmsg);
          } catch (CommunicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        } else if (msg.getBody().toString().equalsIgnoreCase("getReferencedElements")) {
          Object uuid = msg.getHeader("uuid");
          Object name = msg.getHeader("name");

          String resultat = "";
          if (uuid != null && name != null) {
            List<String> res = getReferencedElements(uuid.toString(), name.toString());
            for (String r : res) {
              resultat +=
                  this.agent.getRuntimeModelController().getAgentOfElement(r) + "###" + r + ",";
            }
          }
          CMessage resmsg = new CMessage();
          resmsg.setTo(msg.getFrom());
          resmsg.setCorrelation(msg.getCorrelation());
          resmsg.setObject(msg.getObject());
          resmsg.setBody(resultat);

          try {
            getCubeAgent().getCommunicator().sendMessage(resmsg);
          } catch (CommunicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        } else if (msg.getBody().toString().equalsIgnoreCase("addReferencedElement")) {
          Object uuid = msg.getHeader("uuid");
          Object name = msg.getHeader("name");
          Object onlyone = msg.getHeader("onlyone");
          Object refuuid = msg.getHeader("refuuid");

          boolean p = false;
          if (uuid != null && name != null && refuuid != null) {
            this.agent.addExternalElement(refuuid.toString(), msg.getFrom().toString());
            p =
                addReferencedElement(
                    uuid.toString(),
                    name.toString(),
                    onlyone.toString().equalsIgnoreCase("true") ? true : false,
                    refuuid.toString());
          }
          CMessage resmsg = new CMessage();
          resmsg.setTo(msg.getFrom());
          resmsg.setCorrelation(msg.getCorrelation());
          resmsg.setObject(msg.getObject());
          if (p == true) resmsg.setBody("true");
          else resmsg.setBody("false");
          try {
            getCubeAgent().getCommunicator().sendMessage(resmsg);
          } catch (CommunicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        } else if (msg.getBody().toString().equalsIgnoreCase("removeReferencedElement")) {
          Object uuid = msg.getHeader("uuid");
          Object name = msg.getHeader("name");
          Object refuuid = msg.getHeader("refuuid");

          boolean p = false;
          if (uuid != null && name != null && refuuid != null) {
            p = removeReferencedElement(uuid.toString(), name.toString(), refuuid.toString());
          }
          CMessage resmsg = new CMessage();
          resmsg.setTo(msg.getFrom());
          resmsg.setCorrelation(msg.getCorrelation());
          resmsg.setObject(msg.getObject());
          if (p == true) resmsg.setBody("true");
          else resmsg.setBody("false");
          try {
            getCubeAgent().getCommunicator().sendMessage(resmsg);
          } catch (CommunicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        } else if (msg.getBody().toString().equalsIgnoreCase("hasReferencedElement")) {
          Object uuid = msg.getHeader("uuid");
          Object name = msg.getHeader("name");
          Object refuuid = msg.getHeader("refuuid");

          boolean p = false;
          if (uuid != null && name != null && refuuid != null) {
            p = hasReferencedElement(uuid.toString(), name.toString(), refuuid.toString());
          }
          CMessage resmsg = new CMessage();
          resmsg.setTo(msg.getFrom());
          resmsg.setCorrelation(msg.getCorrelation());
          resmsg.setObject(msg.getObject());
          if (p == true) resmsg.setBody("true");
          else resmsg.setBody("false");
          try {
            getCubeAgent().getCommunicator().sendMessage(resmsg);
          } catch (CommunicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        } else if (msg.getBody().toString().equalsIgnoreCase("getRemoteElement")) {
          Object uuid = msg.getHeader("uuid");
          ManagedElement me = null;
          if (uuid != null) {
            me = getLocalElement(uuid.toString());
          }
          CMessage resmsg = new CMessage();
          resmsg.setTo(msg.getFrom());
          resmsg.setCorrelation(msg.getCorrelation());
          resmsg.setAttachement(me);
          resmsg.setObject(msg.getObject());
          try {
            getCubeAgent().getCommunicator().sendMessage(resmsg);
          } catch (CommunicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }
      }
    }
  }