use of java.net.URLStreamHandlerFactory in project robovm by robovm.
the class OldURLTest method testSetURLStreamHandlerFactory.
public void testSetURLStreamHandlerFactory() throws MalformedURLException, IOException, IllegalArgumentException, IllegalAccessException {
URLStreamHandlerFactory factory = new MyURLStreamHandlerFactory();
Field streamHandlerFactoryField = null;
int counter = 0;
File sampleFile = createTempHelloWorldFile();
URL fileURL = sampleFile.toURL();
Field[] fields = URL.class.getDeclaredFields();
for (Field f : fields) {
if (URLStreamHandlerFactory.class.equals(f.getType())) {
counter++;
streamHandlerFactoryField = f;
}
}
if (counter != 1) {
fail("Error in test setup: not Factory found");
}
streamHandlerFactoryField.setAccessible(true);
URLStreamHandlerFactory old = (URLStreamHandlerFactory) streamHandlerFactoryField.get(null);
try {
streamHandlerFactoryField.set(null, factory);
BufferedReader buf = new BufferedReader(new InputStreamReader(fileURL.openStream()), helloWorldString.getBytes().length);
String nextline;
while ((nextline = buf.readLine()) != null) {
assertEquals(helloWorldString, nextline);
}
buf.close();
} finally {
streamHandlerFactoryField.set(null, old);
}
}
use of java.net.URLStreamHandlerFactory in project xwiki-platform by xwiki.
the class URIClassLoaderTest method testFindResource.
/**
* Verify that resource located in a URI with an attachmentjar protocol can be found.
*/
@Test
public void testFindResource() throws Exception {
URLStreamHandlerFactory urlStreamHandlerFactory = getComponentManager().getInstance(URLStreamHandlerFactory.class);
URIClassLoader cl = new URIClassLoader(new URI[] { new URI("attachmentjar://page%40filename1"), new URI("http://some/url"), new URI("attachmentjar://filename2") }, urlStreamHandlerFactory);
Assert.assertEquals(3, cl.getURLs().length);
Assert.assertEquals("attachmentjar://page%40filename1", cl.getURLs()[0].toString());
Assert.assertEquals("http://some/url", cl.getURLs()[1].toString());
Assert.assertEquals("attachmentjar://filename2", cl.getURLs()[2].toString());
final AttachmentReference attachmentName1 = new AttachmentReference("filename1", new DocumentReference("wiki", "space", "page"));
final AttachmentReference attachmentName2 = new AttachmentReference("filename2", new DocumentReference("wiki", "space", "page"));
getMockery().checking(new Expectations() {
{
allowing(URIClassLoaderTest.this.arf).resolve("page@filename1");
will(returnValue(attachmentName1));
oneOf(URIClassLoaderTest.this.dab).getAttachmentContent(attachmentName1);
will(returnValue(new ByteArrayInputStream(createJarFile("/nomatch"))));
allowing(URIClassLoaderTest.this.arf).resolve("filename2");
will(returnValue(attachmentName2));
oneOf(URIClassLoaderTest.this.dab).getAttachmentContent(attachmentName2);
will(returnValue(new ByteArrayInputStream(createJarFile("/something"))));
}
});
Assert.assertEquals("jar:attachmentjar://filename2!/something", cl.findResource("/something").toString());
}
use of java.net.URLStreamHandlerFactory in project fabric8 by fabric8io.
the class URLs method setURLStreamHandlerFactory.
/**
* The code barfs if we've already set the factory so we have to clear it via reflection first.
* <p/>
* Hacks FTW :)
*/
static void setURLStreamHandlerFactory(URLStreamHandlerFactory newFactory) {
String fieldName = "factory";
Class<URL> clazz = URL.class;
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
URLStreamHandlerFactory oldValue = (URLStreamHandlerFactory) field.get(null);
if (oldValue != null) {
field.set(null, null);
}
} catch (NoSuchFieldException e) {
LOG.error("Could not find field " + fieldName + " in class " + clazz.getName() + ". " + e, e);
} catch (IllegalAccessException e) {
LOG.error("Could not access field " + fieldName + " in class " + clazz.getName() + ". " + e, e);
}
if (newFactory != null) {
URL.setURLStreamHandlerFactory(newFactory);
}
}
use of java.net.URLStreamHandlerFactory in project fabric8 by fabric8io.
the class URLs method getURLStreamHandlerFactory.
/**
* For some odd reason this is private so lets use lovely reflection as a hack!
*/
static URLStreamHandlerFactory getURLStreamHandlerFactory() {
String fieldName = "factory";
Class<URL> clazz = URL.class;
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
return (URLStreamHandlerFactory) field.get(null);
} catch (NoSuchFieldException e) {
LOG.error("Could not find field " + fieldName + " in class " + clazz.getName() + ". " + e, e);
} catch (IllegalAccessException e) {
LOG.error("Could not access field " + fieldName + " in class " + clazz.getName() + ". " + e, e);
}
return null;
}
use of java.net.URLStreamHandlerFactory in project activemq-artemis by apache.
the class RequestReplyNoAdvisoryNetworkTest method testNonAdvisoryNetworkRequestReplyXmlConfig.
public void testNonAdvisoryNetworkRequestReplyXmlConfig() throws Exception {
final String xmlConfigString = new String("<beans" + " xmlns=\"http://www.springframework.org/schema/beans\"" + " xmlns:amq=\"http://activemq.apache.org/schema/core\"" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + " xsi:schemaLocation=\"http://www.springframework.org/schema/beans" + " http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" + " http://activemq.apache.org/schema/core" + " http://activemq.apache.org/schema/core/activemq-core.xsd\">" + " <broker xmlns=\"http://activemq.apache.org/schema/core\" id=\"broker\"" + " allowTempAutoCreationOnSend=\"true\" schedulePeriodForDestinationPurge=\"1000\"" + " brokerName=\"%HOST%\" persistent=\"false\" advisorySupport=\"false\" useJmx=\"false\" >" + " <destinationPolicy>" + " <policyMap>" + " <policyEntries>" + " <policyEntry optimizedDispatch=\"true\" gcInactiveDestinations=\"true\" gcWithNetworkConsumers=\"true\" inactiveTimoutBeforeGC=\"1000\">" + " <destination>" + " <tempQueue physicalName=\"" + replyQWildcard.getPhysicalName() + "\"/>" + " </destination>" + " </policyEntry>" + " </policyEntries>" + " </policyMap>" + " </destinationPolicy>" + " <networkConnectors>" + " <networkConnector uri=\"multicast://default\">" + " <staticallyIncludedDestinations>" + " <queue physicalName=\"" + sendQ.getPhysicalName() + "\"/>" + " <tempQueue physicalName=\"" + replyQWildcard.getPhysicalName() + "\"/>" + " </staticallyIncludedDestinations>" + " </networkConnector>" + " </networkConnectors>" + " <transportConnectors>" + " <transportConnector uri=\"tcp://0.0.0.0:0\" discoveryUri=\"multicast://default\" />" + " </transportConnectors>" + " </broker>" + "</beans>");
final String localProtocolScheme = "inline";
URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
if (localProtocolScheme.equalsIgnoreCase(protocol)) {
return new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL u) throws IOException {
return new URLConnection(u) {
@Override
public void connect() throws IOException {
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(xmlConfigString.replace("%HOST%", url.getFile()).getBytes(StandardCharsets.UTF_8));
}
};
}
};
}
return null;
}
});
a = new XBeanBrokerFactory().createBroker(new URI("xbean:" + localProtocolScheme + ":A"));
b = new XBeanBrokerFactory().createBroker(new URI("xbean:" + localProtocolScheme + ":B"));
brokers.add(a);
brokers.add(b);
doTestNonAdvisoryNetworkRequestReply();
}
Aggregations