@Override public <T> String getControlId(String type, String alias, Class<T> controlInterface) { String controlId = getControlId(type, null, alias); return getControlMBean(controlId, controlInterface) != null ? controlId : null; }
@Override public <T> T getControl(String type, String alias, Class<T> controlInterface) { String controlId = getControlId(type, null, alias); ControlMBean<?> bean = getControlMBean(controlId, controlInterface); return bean != null ? controlInterface.cast(bean.getControl()) : null; }
private synchronized <T> ControlMBean<?> getControlMBean(String controlId, Class<T> controlInterface) { ControlMBean<?> bean = mbeans.get(controlId); if (bean == null) return null; if (bean.getControlInterface() != controlInterface) return null; return bean; } }
@Test public void testNoArg() throws Exception { JsonControlService control = new JsonControlService(); MyBeanImpl cb1 = new MyBeanImpl(); MyBeanImpl cb2 = new MyBeanImpl(); assertFalse(cb1.isDoneIt()); assertFalse(cb2.isDoneIt()); control.registerControl("myb", "1", null, MyBean.class, cb1); control.registerControl("myb", "2", null, MyBean.class, cb2); assertFalse(cb1.isDoneIt()); assertFalse(cb2.isDoneIt()); JsonObject req = new JsonObject(); req.addProperty(JsonControlService.TYPE_KEY, "myb"); req.addProperty(JsonControlService.ALIAS_KEY, "1"); req.addProperty(JsonControlService.OP_KEY, "doIt"); control.controlRequest(req); assertTrue(cb1.isDoneIt()); assertFalse(cb2.isDoneIt()); req = new JsonObject(); req.addProperty(JsonControlService.TYPE_KEY, "myb"); req.addProperty(JsonControlService.ALIAS_KEY, "2"); req.addProperty(JsonControlService.OP_KEY, "doIt"); control.controlRequest(req); assertTrue(cb1.isDoneIt()); assertTrue(cb2.isDoneIt()); }
final String type = request.get(TYPE_KEY).getAsString(); String alias = request.get(ALIAS_KEY).getAsString(); final String controlId = getControlId(type, null, alias); Method method = findMethod(mbean.getControlInterface(), methodName, argumentCount); logger.warn("Unable to find method \"{}\" with {} args in {}", methodName, argumentCount, mbean.getControlInterface().getName()); return new JsonPrimitive(Boolean.FALSE); executeMethod(method, mbean.getControl(), getArguments(method, args));
@Test public void testAppService() throws Exception { DirectProvider provider = new DirectProvider(); JsonControlService control = new JsonControlService(); provider.getServices().addService(ControlService.class, control); AppService.createAndRegister(provider, provider); IotTestApps.registerApplications(provider); JsonObject submitAppOne = newSubmitRequest("AppOne"); JsonElement crr = control.controlRequest(submitAppOne); assertTrue(crr.getAsBoolean()); }
public DirectProvider() { this.services = new ServiceContainer(); getServices().addService(ControlService.class, new JsonControlService()); }
/** * {@inheritDoc} * <P> * All control service MBeans must be valid according * to {@link Controls#isControlServiceMBean(Class)}. * </P> * * @see Controls#isControlServiceMBean(Class) */ @Override public synchronized <T> String registerControl(String type, String id, String alias, Class<T> controlInterface, T control) { if (!Controls.isControlServiceMBean(controlInterface)) throw new IllegalArgumentException(); final String controlId = getControlId(type, id, alias); if (mbeans.containsKey(controlId)) { logger.error("Control id: {} already exists", controlId); throw new IllegalStateException(); } logger.trace("Register control id: {}", controlId); mbeans.put(controlId, new ControlMBean<T>(controlInterface, control)); return controlId; }
/** * Submit the specified application previously registered * via {@link #registerTopology(String, BiConsumer) registerTopology}. * @param appName name of registered application * @param config See {@link #submit(Topology, JsonObject) submit}. May be null. * * @throws Exception on failure starting applications. */ private void submitApplication(String appName, JsonObject config) throws Exception { if (systemApps.contains(appName)) { throw new IllegalArgumentException("appName"); } ApplicationServiceMXBean bean = getControlService().getControl(ApplicationServiceMXBean.TYPE, ApplicationService.ALIAS, ApplicationServiceMXBean.class); bean.submit(appName, config==null ? null : config.toString()); } }
/** * Handle a JSON control request. * * The control action is executed directly * using the calling thread. * * @param request the request * @return JSON response, JSON null if the request was not recognized. * @throws Exception on failure */ public JsonElement controlRequest(JsonObject request) throws Exception { if (request.has(OP_KEY)) return controlOperation(request); return JsonNull.INSTANCE; }
final String type = request.get(TYPE_KEY).getAsString(); String alias = request.get(ALIAS_KEY).getAsString(); final String controlId = getControlId(type, null, alias); Method method = findMethod(mbean.getControlInterface(), methodName, argumentCount); logger.warn("Unable to find method \"{}\" with {} args in {}", methodName, argumentCount, mbean.getControlInterface().getName()); return new JsonPrimitive(Boolean.FALSE); executeMethod(method, mbean.getControl(), getArguments(method, args));
@Override public <T> T getControl(String type, String alias, Class<T> controlInterface) { String controlId = getControlId(type, null, alias); ControlMBean<?> bean = getControlMBean(controlId, controlInterface); return bean != null ? controlInterface.cast(bean.getControl()) : null; }
public DirectProvider() { this.services = new ServiceContainer(); getServices().addService(ControlService.class, new JsonControlService()); }
@Override public <T> String getControlId(String type, String alias, Class<T> controlInterface) { String controlId = getControlId(type, null, alias); return getControlMBean(controlId, controlInterface) != null ? controlId : null; }
/** * {@inheritDoc} * <P> * All control service MBeans must be valid according * to {@link Controls#isControlServiceMBean(Class)}. * </P> * * @see Controls#isControlServiceMBean(Class) */ @Override public synchronized <T> String registerControl(String type, String id, String alias, Class<T> controlInterface, T control) { if (!Controls.isControlServiceMBean(controlInterface)) throw new IllegalArgumentException(); final String controlId = getControlId(type, id, alias); if (mbeans.containsKey(controlId)) { logger.error("Control id: {} already exists", controlId); throw new IllegalStateException(); } logger.trace("Register control id: {}", controlId); mbeans.put(controlId, new ControlMBean<T>(controlInterface, control)); return controlId; }
/** * Submit the specified application previously registered * via {@link #registerTopology(String, BiConsumer) registerTopology}. * @param appName name of registered application * @param config See {@link #submit(Topology, JsonObject) submit}. May be null. * * @throws Exception on failure starting applications. */ private void submitApplication(String appName, JsonObject config) throws Exception { if (systemApps.contains(appName)) { throw new IllegalArgumentException("appName"); } ApplicationServiceMXBean bean = getControlService().getControl(ApplicationServiceMXBean.TYPE, ApplicationService.ALIAS, ApplicationServiceMXBean.class); bean.submit(appName, config==null ? null : config.toString()); } }
private synchronized <T> ControlMBean<?> getControlMBean(String controlId, Class<T> controlInterface) { ControlMBean<?> bean = mbeans.get(controlId); if (bean == null) return null; if (bean.getControlInterface() != controlInterface) return null; return bean; } }
/** * Handle a JSON control request. * * The control action is executed directly * using the calling thread. * * @param request the request * @return JSON response, JSON null if the request was not recognized. * @throws Exception on failure */ public JsonElement controlRequest(JsonObject request) throws Exception { if (request.has(OP_KEY)) return controlOperation(request); return JsonNull.INSTANCE; }
/** * Start this provider by starting its system applications * and any autoSubmit-enabled registered applications. * * @throws Exception on failure starting applications. */ public void start() throws Exception { ApplicationServiceMXBean bean = getControlService().getControl(ApplicationServiceMXBean.TYPE, ApplicationService.ALIAS, ApplicationServiceMXBean.class); for (String systemAppName : systemApps) { bean.submit(systemAppName, null /* no config */); } for (Entry<String,JsonObject> e : autoSubmitApps.entrySet()) { submitApplication(e.getKey(), e.getValue()); } }
/** * Start this provider by starting its system applications * and any autoSubmit-enabled registered applications. * * @throws Exception on failure starting applications. */ public void start() throws Exception { ApplicationServiceMXBean bean = getControlService().getControl(ApplicationServiceMXBean.TYPE, ApplicationService.ALIAS, ApplicationServiceMXBean.class); for (String systemAppName : systemApps) { bean.submit(systemAppName, null /* no config */); } for (Entry<String,JsonObject> e : autoSubmitApps.entrySet()) { submitApplication(e.getKey(), e.getValue()); } }