protected static boolean checkIdle(final Point p) throws NimbitsException { final Calendar c = Calendar.getInstance(); c.add(Calendar.SECOND, p.getIdleSeconds() * -1); boolean retVal = false; final List<Entity> result = EntityServiceFactory.getInstance() .getEntityByKey( UserServiceFactory.getServerInstance().getAdmin(), p.getOwner(), EntityType.user); if (!result.isEmpty()) { final User u = (User) result.get(0); final List<Value> v = ValueServiceFactory.getInstance().getCurrentValue(p); if (p.getIdleSeconds() > 0 && !v.isEmpty() && v.get(0).getTimestamp().getTime() <= c.getTimeInMillis() && !p.getIdleAlarmSent()) { p.setIdleAlarmSent(true); EntityServiceFactory.getInstance().addUpdateEntity(u, p); // PointServiceFactory.getInstance().updatePoint(u, p); final Value va = ValueFactory.createValueModel(v.get(0), AlertType.IdleAlert); SubscriptionServiceFactory.getInstance().processSubscriptions(u, p, va); retVal = true; } } return retVal; }
/* * return 0 is success, otherwise failure */ public final int runAdminCommandOnRemoteNode( Node thisNode, StringBuilder output, List<String> args, List<String> stdinLines) throws SSHCommandExecutionException, IllegalArgumentException, UnsupportedOperationException { String humanreadable = null; try { this.node = thisNode; dcomInfo = new DcomInfo(node); List<String> fullcommand = new ArrayList<String>(); WindowsRemoteAsadmin asadmin = dcomInfo.getAsadmin(); if (stdinLines != null && !stdinLines.isEmpty()) setupAuthTokenFile(fullcommand, stdinLines); fullcommand.addAll(args); humanreadable = dcomInfo.getNadminPath() + " " + commandListToString(fullcommand); // This is where the rubber meets the road... String out = asadmin.run(fullcommand); output.append(out); logger.info(Strings.get("remote.command.summary", humanreadable, out)); return determineStatus(args); } catch (WindowsException ex) { throw new SSHCommandExecutionException( Strings.get("remote.command.error", ex.getMessage(), humanreadable), ex); } finally { teardownAuthTokenFile(); } }
/** @param aProperties the updated properties. */ @SuppressWarnings("rawtypes") final void setProperties(final Dictionary aProperties) { final Map<String, String> newProps = new HashMap<String, String>(); Enumeration keys = aProperties.keys(); while (keys.hasMoreElements()) { final String key = (String) keys.nextElement(); if (!KNOWN_KEYS.contains(key) && !IGNORED_KEYS.contains(key)) { LOG.log(Level.WARNING, "Unknown/unsupported profile key: " + key); continue; } final String value = aProperties.get(key).toString(); newProps.put(key, value.trim()); } // Verify whether all known keys are defined... final List<String> checkedKeys = new ArrayList<String>(KNOWN_KEYS); checkedKeys.removeAll(newProps.keySet()); if (!checkedKeys.isEmpty()) { throw new IllegalArgumentException( "Profile settings not complete! Missing keys are: " + checkedKeys.toString()); } this.properties.putAll(newProps); LOG.log( Level.INFO, "New device profile settings applied for {1} ({0}) ...", // new Object[] {getType(), getDescription()}); }
@Override public void processCalculations(final User u, final Entity point, final Value value) throws NimbitsException { final List<Entity> calculations = EntityServiceFactory.getInstance().getEntityByTrigger(u, point, EntityType.calculation); for (final Entity entity : calculations) { Calculation c = (Calculation) entity; try { final List<Entity> target = EntityServiceFactory.getInstance().getEntityByKey(u, c.getTarget(), EntityType.point); if (target.isEmpty()) { log.severe("Point target was null " + c.getTarget()); log.severe(c.getFormula()); log.severe("trigger: " + c.getTrigger()); disableCalc(u, c); } else { log.info("Solving calc" + c.getFormula()); final Value result = solveEquation(u, c); log.info("result" + result); ValueServiceFactory.getInstance().recordValue(u, target.get(0), result); } } catch (NimbitsException e1) { LogHelper.logException(this.getClass(), e1); disableCalc(u, c); } } }
@Override public void processingInstruction(String target, String data) throws SAXException { _logger.fine("Processing Instruction " + target); _logger.fine("Processing Instruction data: " + data); if (target.equals("assemble")) { if (!_stack.isEmpty()) { ElementInfo element = _stack.get(_stack.size() - 1); Matcher matcher = PROCESSING_INSTRUCTION.matcher(data); while (matcher.find()) { if (matcher.groupCount() == 2) { String name = matcher.group(1); if (name.charAt(0) == '@') { element.inst.put(name, matcher.group(2)); } else { element.args.add(guessUntypedValue(name, matcher.group(2))); } _logger.fine( "Processing Instruction for " + element.data.getClass() + "\n\ttarget = " + target + "\n\t" + name + "=" + matcher.group(2)); } } } } }
/** * Receives a <tt>DatagramPacket</tt> from a specific list of <tt>DatagramPacket</tt>s if it is * not empty or from the network if the specified list is empty. When this method returns, the * <tt>DatagramPacket</tt>'s buffer is filled with the data received. The datagram packet also * contains the sender's IP address, and the port number on the sender's machine. * * @param received the list of previously received <tt>DatagramPacket</tt> from which the first is * to be removed and returned if available * @param p the <tt>DatagramPacket</tt> into which to place the incoming data * @throws IOException if an I/O error occurs */ private void receive(List<DatagramPacket> received, DatagramPacket p) throws IOException { DatagramPacket r = null; do { boolean doReceive; synchronized (receiveSyncRoot) { if (received.isEmpty()) { if (inReceive) { doReceive = false; try { receiveSyncRoot.wait(); } catch (InterruptedException iex) { continue; } } else { doReceive = true; inReceive = true; } } else { doReceive = false; r = received.remove(0); } } if (doReceive) { try { super.receive(p); synchronized (receiveSyncRoot) { synchronized (socketsSyncRoot) { boolean accepted = false; for (MultiplexedDatagramSocket socket : sockets) if (socket.getFilter().accept(p)) { socket.received.add(clone(p)); accepted = true; /* * Emil: Don't break because we want all * filtering sockets to get this. */ // break; } if (!accepted) this.received.add(clone(p)); } } } finally { synchronized (receiveSyncRoot) { inReceive = false; receiveSyncRoot.notify(); } } } } while (r == null); copy(r, p); }
@Override public Value solveEquation(final User user, final Calculation calculation) throws NimbitsException { final MathEvaluator m = new MathEvaluatorImpl(calculation.getFormula()); log.info(calculation.getFormula()); if (!Utils.isEmptyString(calculation.getX()) && calculation.getFormula().contains("x")) { // Point p = PointServiceFactory.getInstance().getPointByKey(calculation.getX()); final Entity p = EntityServiceFactory.getInstance() .getEntityByKey(user, calculation.getX(), EntityType.point) .get(0); if (p != null) { log.info("calc has an x car and i found " + p.getName()); final List<Value> val = ValueServiceFactory.getInstance().getCurrentValue(p); final double d = val.isEmpty() ? 0.0 : val.get(0).getDoubleValue(); m.addVariable("x", d); } else { log.severe("calc has an x car and x not found"); } } if (!Utils.isEmptyString(calculation.getY()) && calculation.getFormula().contains("y")) { final Entity p = EntityServiceFactory.getInstance() .getEntityByKey(user, calculation.getY(), EntityType.point) .get(0); // Point p = PointServiceFactory.getInstance().getPointByKey(calculation.getY()); if (p != null) { final List<Value> val = ValueServiceFactory.getInstance().getCurrentValue(p); final double d = val.isEmpty() ? 0.0 : val.get(0).getDoubleValue(); m.addVariable("y", d); } } if (!Utils.isEmptyString(calculation.getZ()) && calculation.getFormula().contains("z")) { final Entity p = EntityServiceFactory.getInstance() .getEntityByKey(user, calculation.getZ(), EntityType.point) .get(0); // Point p = PointServiceFactory.getInstance().getPointByKey(calculation.getZ()); if (p != null) { final List<Value> val = ValueServiceFactory.getInstance().getCurrentValue(p); final double d = val.isEmpty() ? 0.0 : val.get(0).getDoubleValue(); m.addVariable("z", d); } } final Double retVal = m.getValue(); if (retVal == null) { throw new NimbitsException("Formula returned a null value: " + calculation.getFormula()); } return ValueFactory.createValueModel(retVal, "CV"); }
/** Receive notification of the end of an element. */ @Override public void endElement(String uri, String l, String q) { /* * 1. If current element is a String, update its value from the string buffer. * 2. Add the element to parent. */ ElementInfo element = _stack.remove(_stack.size() - 1); _logger.fine("endElement " + element); if (element.type == null) { _logger.warning("Element " + element.name + " not created "); return; } else if (_chars.length() > 0) { try { injectProperty(element.data, String.class, _chars.toString(), null, null); } catch (Exception x) { if (!_lenient) { throw new BeanAssemblyException( "Failed to set characters to object " + element.type.getName(), x); } else { _logger.warning("Failed to set characters to parent " + element.data); } } } _chars.setLength(0); _logger.fine( "<<ElementInfo: " + element.type.getName() + " in " + element + "\n @as is " + element.inst.get("@as") + "\n @id is " + element.inst.get("@id")); if (List.class.isAssignableFrom(element.data.getClass()) && element.name.endsWith("...")) { List<?> list = (List<?>) element.data; Object array = Array.newInstance(element.type, list.size()); for (int i = 0; i < list.size(); ++i) { Array.set(array, i, list.get(i)); } element.data = array; } String id = element.inst.get("@id"); if (id != null) { // locally stored object - not added to the parent _local.put(id, element); } else if (!_stack.isEmpty()) { // inject into the parent as a property ElementInfo parent = _stack.get(_stack.size() - 1); _logger.fine("Parent is " + parent.data.getClass().getName()); try { String as = element.inst.get("@as"); if (as != null) { injectProperty( parent.data, element.type, element.data, Strings.toCamelCase(as, '-', false), element.args.complete()); } else { injectProperty(parent.data, element.type, element.data, null, element.args.complete()); } } catch (Exception x) { if (!_lenient) { throw new BeanAssemblyException( "Failed to set value " + element.data + " to parent " + parent.data, x); } else { _logger.log( Level.WARNING, "Failed to set value " + element.data + " to parent " + parent.data, x); } } } _top = element.data; }
/** Receive notification of the start of an element. */ @Override public void startElement(String uri, String l, String q, Attributes a) { /* * 1. Load a class that matches the element name. * 2. If no class found, assume the element maps to a String. * 3. Otherwise, construct a new object of the class with element attributes. */ _logger.fine( S.fine(_logger) ? "Consider element " + l + "\n uri " + uri + "\n q " + q : null); ElementInfo info = new ElementInfo(); // Record java packages defined on this element as xmlns for (int i = 0; i < a.getLength(); ++i) { _logger.fine( S.fine(_logger) ? " attr " + a.getQName(i) + "=" + a.getValue(i) + "\n " + a.getQName(i) + ":" + a.getURI(i) : null); if (a.getQName(i).startsWith("xmlns:") && a.getValue(i).startsWith("java://")) { info.pkgs.put(a.getQName(i).substring(6), a.getValue(i).substring(7)); } } // Resolve the package name of this element, which could be empty (default package) int colon = q.indexOf(':'); if (colon > 0) { String xmlns = q.substring(0, colon); // is it defined right here? info.jpkg = info.pkgs.get(xmlns); // find a matching namespace from ancesters if (info.jpkg == null && !_stack.isEmpty()) { for (int i = _stack.size() - 1; i >= 0; --i) { info.jpkg = _stack.get(i).pkgs.get(xmlns); if (info.jpkg != null) { break; } } } } else if (isPrimitiveType(q)) { info.jpkg = "java.lang"; } else if (!_stack.isEmpty()) { info.jpkg = _stack.get(_stack.size() - 1).jpkg; } else { info.jpkg = _jpkg; } _logger.fine("to create element with package = " + info.jpkg); try { info.name = (info.jpkg != null) ? info.jpkg + '.' + Strings.toCamelCase(l) : Strings.toCamelCase(l); try { if (info.name.endsWith("...")) { // Array construction info.type = Class.forName(info.name.substring(0, info.name.length() - 3)); info.data = new ArrayList<Object>(); } else { // Non-array construction int size = a.getLength(); TypedValueGroup arguments = new TypedValueGroup(); for (int i = 0; i < size; ++i) { if (!a.getQName(i).startsWith("xmlns:") && !a.getQName(i).equals("xmlns")) { arguments.add(guessUntypedValue(a.getQName(i), a.getValue(i))); } } arguments.complete(); _logger.fine(S.fine(_logger) ? "arguments=" + arguments : null); if (arguments.size() > 0) { if (arguments.size() == 1 && "java.lang".equals(info.jpkg)) { info.inst.put( "@as", Strings.toCamelCase( arguments.get(0).name, '-', false)); // respect original spelling info.data = arguments.get(0).get(0).data; info.type = arguments.get(0).get(0).type; } else { Exception last = null; Object[] args = new Object[arguments.size()]; while (arguments.load(args, 0)) { try { _logger.fine( S.fine(_logger) ? "to create " + info.name + " with args: " + args.length + args(args) : null); info.data = _factory.create(info.name, args); info.type = info.data.getClass(); break; } catch (InvocationTargetException x) { throw x; } catch (Exception x) { last = x; _logger.fine( "failure in creating " + info.name + ": probing for other constructors"); } } if (info.data == null) { throw last; } } } else { _logger.fine("Create " + info.name + " with the default constructor"); info.data = _factory.create(info.name); info.type = info.data.getClass(); } } } catch (ClassNotFoundException x) { // no class by the element name is found, assumed String if (!_lenient) { throw new BeanAssemblyException("No class associated with element " + q); } else { _logger.log(Level.WARNING, "can't find class " + info.name, x); } } _stack.add(info); // _logger.fine(">>ElementInfo: " + info.type.getName() + " in " + info); // all other exceptions indicate mismatches between the beans and the XML schema } catch (Exception x) { if (!_lenient) { throw new BeanAssemblyException("Failed to assemble bean from element " + q, x); } else { _logger.log(Level.SEVERE, "can't create object for this element", x); } } }