예제 #1
0
파일: Server.java 프로젝트: hlambertOld/ctk
  /**
   * This method is called to aggregate the list of non cosntant attributes that the widgets
   * relevant to this server provide. This should be called after a constructor sets the widget
   * handles to use or after setWidgets() has been called.
   *
   * @return the server attributes
   */
  protected Attributes initAttributes() {
    // this protects us against the Widget constructor
    // that calls us too early (we havent' got the widgets yet)
    // it's good practice anyway
    if (widgets == null) {
      return null;
    }

    Attributes atts = new Attributes();
    for (int i = 0; i < widgets.size(); i++) {
      WidgetHandle handle = widgets.getWidgetHandleAt(i);
      System.out.println(handle);
      DataObject widgetAtts =
          getWidgetAttributes(handle.getHostName(), handle.getPort(), handle.getId());

      String error = new Error(widgetAtts).getError();
      if (error != null) {
        if (error.equals(Error.NO_ERROR)) {
          Attributes wAtts = new Attributes(widgetAtts);
          for (int j = 0; j < wAtts.numAttributes(); j++) {
            Attribute wAtt = wAtts.getAttributeAt(j);
            if (atts.getAttribute(wAtt.getName()) == null) {
              atts.addAttribute(wAtt);
            }
          }
        }
      }
    }

    atts.addAttributes(setServerAttributes());

    return atts;
  }
  @Override
  protected void init() {
    addAttribute(Attribute.instance(WARNING_FIRE, Boolean.class));
    addAttribute(Attribute.instance(WARNING_AIR_POLLUTION, Boolean.class));
    addAttribute(Attribute.instance(WARNING_TEMPERATURE, Boolean.class));

    addAttribute(AttributeNameValue.instance(AMBIENT, ambient), true);
  }
예제 #3
0
 /**
  * This method checks the list of incoming attributes to ensure that the interpreter can handle
  * these attributes.
  *
  * @param inAtts List of incoming attributes to check
  * @return whether the list of attributes is valid
  */
 private boolean canHandle(Attributes inAtts) {
   for (int i = 0; i < inAtts.numAttributes(); i++) {
     Attribute inAtt = inAtts.getAttributeAt(i);
     if (!isInAttribute(inAtt.getName())) {
       return false;
     }
   }
   return true;
 }
예제 #4
0
파일: Server.java 프로젝트: hlambertOld/ctk
 /**
  * This method sets up the server for use. This includes getting the attributes and services
  * information from relevant widgets.
  *
  * <p>Modification made by Agathe
  *
  * @see #setAttributes()
  * @see #setServices()
  */
 protected void serverSetup() {
   attributes = initAttributes();
   constantAttributes = initConstantAttributes(); // added by Agathe
   attributesCache = new Attributes();
   attributeTypes = attributes.toTypesHashtable();
   // constantAttributeTypes = constantAttributes.toTypesHashtable(); // Added by agathe
   // ??
   for (int i = 0; i < attributes.numAttributes(); i++) {
     Attribute att = attributes.getAttributeAt(i);
     attributesCache.addAttributeNameValue(att.getName(), att.getSubAttributes(), att.getType());
   }
   attributesTimes = new Hashtable();
   Long long1 = new Long(0);
   for (Enumeration e = attributeTypes.keys(); e.hasMoreElements(); ) {
     attributesTimes.put(e.nextElement(), long1);
   }
   if (storage != null) {
     storage.setAttributes(attributes, attributeTypes);
     // storage.setConstantAttributes(constantAttributes,cosntantAttributeTypes); // Added by
     // agathe
   }
   services = initServices();
 }
  public void insertComponentUpdateEntry(
      String in_componentId,
      String in_updateName,
      List<ComponentDescription> in_componentDescriptions)
      throws LoggingException {

    Session session = null;
    session = HibernateUtils.getNewSession();

    if (session == null) return;

    ArrayList<CUDestination> cuDestinationList = new ArrayList<CUDestination>();
    ArrayList<CUAttribute> cuAttributeList = new ArrayList<CUAttribute>();

    ComponentUpdate componentUpdateEntry = new ComponentUpdate();
    componentUpdateEntry.setComponentid(in_componentId);
    componentUpdateEntry.setUpdatename(in_updateName);
    componentUpdateEntry.setUpdatetime(new Date());

    for (ComponentDescription compDescr : in_componentDescriptions) {
      // Set up the cuDestination entry
      CUDestination cuDestinationEntry = new CUDestination();
      cuDestinationEntry.setComponentUpdate(componentUpdateEntry);
      cuDestinationEntry.setDestinationcomponentid(compDescr.id);
      cuDestinationEntry.setSuccess(new Boolean(true));
      cuDestinationList.add(cuDestinationEntry);

      AttributeNameValue<?> attributeNameValue;

      // Set up the constant attribute entries
      for (Attribute<?> attribute : compDescr.getConstantAttributes()) {
        CUAttribute cuAttributeEntry = new CUAttribute();
        cuAttributeEntry.setAttributename(attribute.getName());
        cuAttributeEntry.setAttributetype(attribute.getType());
        cuAttributeEntry.setConstant(true);
        cuAttributeEntry.setCUDestination(cuDestinationEntry);

        // AttributeNameValue is a subclass of Attribute
        if (attribute instanceof AttributeNameValue<?>) {
          // check the value associated with this AttributeNameValue
          attributeNameValue = (AttributeNameValue<?>) attribute;
          if (attributeNameValue.getType().equals(String.class)) {
            cuAttributeEntry.setAttributevaluestring((String) attributeNameValue.getValue());
          } else if (attributeNameValue.getType().isInstance(Number.class)) {
            cuAttributeEntry.setAttributevaluenumeric(
                Float.valueOf(attributeNameValue.getValue().toString()));
          }
        }
        cuAttributeList.add(cuAttributeEntry);
      }

      // Set up the non constant attribute entries
      for (Attribute<?> attribute : compDescr.getNonConstantAttributes().values()) {
        CUAttribute cuAttributeEntry = new CUAttribute();
        cuAttributeEntry.setAttributename(attribute.getName());
        cuAttributeEntry.setAttributetype(attribute.getType());
        cuAttributeEntry.setConstant(false);
        cuAttributeEntry.setCUDestination(cuDestinationEntry);

        if (attribute instanceof AttributeNameValue<?>) {
          attributeNameValue = (AttributeNameValue<?>) attribute;
          if (attributeNameValue.getType().equals(String.class)) {
            cuAttributeEntry.setAttributevaluestring((String) attributeNameValue.getValue());
          } else if (attributeNameValue.getType().isInstance(Number.class)) {
            cuAttributeEntry.setAttributevaluenumeric(
                Float.valueOf(attributeNameValue.getValue().toString()));
          }
        }
        cuAttributeList.add(cuAttributeEntry);
      }
    }

    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      session.save(componentUpdateEntry);

      for (int i = 0; i < cuDestinationList.size(); i++) {
        session.save(cuDestinationList.get(i));
      }

      for (int i = 0; i < cuAttributeList.size(); i++) {
        session.save(cuAttributeList.get(i));
      }

      tx.commit(); // flush the Session and commit the transaction
    } catch (Exception e) {
      try {
        if (tx != null) tx.rollback(); // rollback the transaction
      } catch (Exception x) {
        throw new LoggingException(x);
      }
    } finally {
      try {
        session.close();
      } catch (Exception e) {
        throw new LoggingException(e);
      }
    }
  }