Search in sources :

Example 1 with URLStreamHandlerFactory

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);
    }
}
Also used : Field(java.lang.reflect.Field) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) URLStreamHandlerFactory(java.net.URLStreamHandlerFactory) File(java.io.File) URL(java.net.URL)

Example 2 with URLStreamHandlerFactory

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());
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) Expectations(org.jmock.Expectations) ByteArrayInputStream(java.io.ByteArrayInputStream) URLStreamHandlerFactory(java.net.URLStreamHandlerFactory) URI(java.net.URI) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 3 with URLStreamHandlerFactory

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);
    }
}
Also used : Field(java.lang.reflect.Field) URLStreamHandlerFactory(java.net.URLStreamHandlerFactory) URL(java.net.URL)

Example 4 with URLStreamHandlerFactory

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;
}
Also used : Field(java.lang.reflect.Field) URLStreamHandlerFactory(java.net.URLStreamHandlerFactory) URL(java.net.URL)

Example 5 with URLStreamHandlerFactory

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();
}
Also used : URLStreamHandler(java.net.URLStreamHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) URLStreamHandlerFactory(java.net.URLStreamHandlerFactory) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) URLConnection(java.net.URLConnection) XBeanBrokerFactory(org.apache.activemq.xbean.XBeanBrokerFactory)

Aggregations

URLStreamHandlerFactory (java.net.URLStreamHandlerFactory)13 URL (java.net.URL)6 Field (java.lang.reflect.Field)5 URLStreamHandler (java.net.URLStreamHandler)5 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 IOException (java.io.IOException)2 URI (java.net.URI)2 Statement (org.junit.runners.model.Statement)2 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URLConnection (java.net.URLConnection)1 ObjectName (javax.management.ObjectName)1 NamingException (javax.naming.NamingException)1 XBeanBrokerFactory (org.apache.activemq.xbean.XBeanBrokerFactory)1