@Override public void stop(ModuleContext context) throws Exception { sreg.unregister(); } }
@Override public void unregister() { delegate.unregister(); }
@Override protected void doShutdown() { super.doShutdown(); for (ServiceRegistration<?> sreg : systemServices) { sreg.unregister(); } }
@Test public void testURLHandlerService() throws Exception { try { new URL("foo://hi"); Assert.fail("No handler registered. Should throw a MalformedURLException."); } catch (MalformedURLException mue) { // expected } Runtime runtime = RuntimeLocator.getRequiredRuntime(); ModuleContext syscontext = runtime.getModuleContext(); Dictionary<String, Object> props = new Hashtable<>(); props.put(Constants.URL_HANDLER_PROTOCOL, "foo"); ServiceRegistration<URLStreamHandler> sreg = syscontext.registerService(URLStreamHandler.class, new MyHandler(), props); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copyStream(new URL("foo://somehost").openStream(), baos); Assert.assertEquals("somehost", new String(baos.toByteArray())); } finally { sreg.unregister(); } try { new URL("foo://hi").openConnection(); Assert.fail("No handler registered any more."); } catch (IOException ex) { // expected } }