/* * add a new resource to the node. E.g. the resource temperature or * humidity. If the path is /readings/temp, temp will be a subResource * of readings, which is a subResource of the node. */ public ResourceBase addNodeResource(String path) { Scanner scanner = new Scanner(path); scanner.useDelimiter("/"); String next = ""; boolean resourceExist = false; Resource resource = this; // It's the resource that represents the endpoint ResourceBase subResource = null; while (scanner.hasNext()) { resourceExist = false; next = scanner.next(); for (Resource res : resource.getChildren()) { if (res.getName().equals(next)) { subResource = (ResourceBase) res; resourceExist = true; } } if (!resourceExist) { subResource = new RDTagResource(next, true, this); resource.add(subResource); } resource = subResource; } subResource.setPath(resource.getPath()); subResource.setName(next); scanner.close(); return subResource; }
/** * Creates a new subResource for each resource the node wants register. Each resource is separated * by ",". E.g. A node can register a resource for reading the temperature and another one for * reading the humidity. */ private boolean updateEndpointResources(String linkFormat) { Scanner scanner = new Scanner(linkFormat); scanner.useDelimiter(","); List<String> pathResources = new ArrayList<String>(); while (scanner.hasNext()) { pathResources.add(scanner.next()); } for (String p : pathResources) { scanner = new Scanner(p); /* * get the path of the endpoint's resource. E.g. from * </readings/temp> it will select /readings/temp. */ String path = "", pathTemp = ""; if ((pathTemp = scanner.findInLine("</.*?>")) != null) { path = pathTemp.substring(1, pathTemp.length() - 1); } else { scanner.close(); return false; } ResourceBase resource = addNodeResource(path); /* * Since created the subResource, get all the attributes from * the payload. Each parameter is separated by a ";". */ scanner.useDelimiter(";"); while (scanner.hasNext()) { LinkAttribute attr = LinkAttribute.parse(scanner.next()); if (attr.getValue() == null) resource.getAttributes().addAttribute(attr.getName()); else resource.getAttributes().addAttribute(attr.getName(), attr.getValue()); } resource.getAttributes().addAttribute(LinkFormat.END_POINT, getEndpointIdentifier()); } scanner.close(); return true; }
@Override public void delete() { LOGGER.info("Removing endpoint: " + getContext()); if (lifetimeTimer != null) { lifetimeTimer.cancel(); } if (validationTimer != null) { validationTimer.cancel(); } super.delete(); }