Search in sources :

Example 16 with DefaultResourceLoader

use of org.springframework.core.io.DefaultResourceLoader in project spring-security by spring-projects.

the class SecurityMockMvcRequestPostProcessors method x509.

/**
	 * Finds an X509Cetificate using a resoureName and populates it on the request.
	 *
	 * @param resourceName the name of the X509Certificate resource
	 * @return the
	 * {@link org.springframework.test.web.servlet.request.RequestPostProcessor} to use.
	 * @throws IOException
	 * @throws CertificateException
	 */
public static RequestPostProcessor x509(String resourceName) throws IOException, CertificateException {
    ResourceLoader loader = new DefaultResourceLoader();
    Resource resource = loader.getResource(resourceName);
    InputStream inputStream = resource.getInputStream();
    CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
    X509Certificate certificate = (X509Certificate) certFactory.generateCertificate(inputStream);
    return x509(certificate);
}
Also used : DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) ResourceLoader(org.springframework.core.io.ResourceLoader) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader)

Example 17 with DefaultResourceLoader

use of org.springframework.core.io.DefaultResourceLoader in project gwt-test-utils by gwt-test-utils.

the class GwtTestContextLoader method createBeanDefinitionReader.

@Override
protected BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context) {
    XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(context);
    beanDefinitionReader.setResourceLoader(new DefaultResourceLoader(GwtFactory.get().getClassLoader()));
    return beanDefinitionReader;
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader)

Example 18 with DefaultResourceLoader

use of org.springframework.core.io.DefaultResourceLoader in project opennms by OpenNMS.

the class Migrator method getMigrationResourceLoader.

/**
     * <p>getMigrationResourceLoader</p>
     *
     * @param migration a {@link org.opennms.core.schema.Migration} object.
     * @return a {@link org.springframework.core.io.ResourceLoader} object.
     */
protected ResourceLoader getMigrationResourceLoader(final Migration migration) {
    final File changeLog = new File(migration.getChangeLog());
    final List<URL> urls = new ArrayList<URL>();
    try {
        if (changeLog.exists()) {
            urls.add(changeLog.getParentFile().toURI().toURL());
        }
    } catch (final MalformedURLException e) {
        LOG.info("unable to figure out URL for {}", migration.getChangeLog(), e);
    }
    final ClassLoader cl = new URLClassLoader(urls.toArray(new URL[0]), this.getClass().getClassLoader());
    return new DefaultResourceLoader(cl);
}
Also used : MalformedURLException(java.net.MalformedURLException) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader) File(java.io.File) URL(java.net.URL) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader)

Example 19 with DefaultResourceLoader

use of org.springframework.core.io.DefaultResourceLoader in project opennms by OpenNMS.

the class JUnitSnmpAgentExecutionListener method handleSnmpAgent.

private void handleSnmpAgent(final TestContext testContext, final JUnitSnmpAgent config, boolean useMockSnmpStrategy, MockSnmpDataProvider provider) throws IOException, UnknownHostException, InterruptedException {
    if (config == null)
        return;
    String factoryClassName = "unknown";
    try {
        final SnmpAgentConfigFactory factory = testContext.getApplicationContext().getBean("snmpPeerFactory", SnmpAgentConfigFactory.class);
        factoryClassName = factory.getClass().getName();
    } catch (final Exception e) {
    // ignore
    }
    if (!factoryClassName.contains("ProxySnmpAgentConfigFactory")) {
        LOG.warn("SNMP Peer Factory ({}) is not the ProxySnmpAgentConfigFactory -- did you forget to include applicationContext-proxy-snmp.xml?", factoryClassName);
    }
    LOG.debug("handleSnmpAgent(testContext, {}, {})", config, useMockSnmpStrategy);
    String host = config.host();
    if (host == null || "".equals(host)) {
        /*
             * NOTE: This call produces different results on different platforms so make
             * sure your client code is aware of this. If you use the {@link ProxySnmpAgentConfigFactory}
             * by including the <code>classpath:/META-INF/opennms/applicationContext-proxy-snmp.xml</code>
             * Spring context, you probably won't need to deal with this. It will override the
             * SnmpPeerFactory with the correct values.
             * 
             * Linux: 127.0.0.1
             * Mac OS: primary external interface
             */
        host = InetAddressUtils.getLocalHostAddressAsString();
    //host = "127.0.0.1";
    }
    final ResourceLoader loader = new DefaultResourceLoader();
    final Resource resource = loader.getResource(config.resource());
    // NOTE: The default value for config.port is specified inside {@link JUnitSnmpAgent}
    final InetAddress hostAddress = addr(host);
    final int port = config.port();
    final SnmpAgentAddress agentAddress = new SnmpAgentAddress(hostAddress, port);
    final SnmpAgentConfigProxyMapper mapper = SnmpAgentConfigProxyMapper.getInstance();
    if (useMockSnmpStrategy) {
        // since it's all virtual, the "mapped" port just points to the real agent address
        mapper.addProxy(hostAddress, agentAddress);
    } else {
        MockSnmpAgent agent = null;
        try {
            agent = MockSnmpAgent.createAgentAndRun(resource.getURL(), str(InetAddress.getLocalHost()) + "/0");
        } catch (Throwable e) {
            agent = MockSnmpAgent.createAgentAndRun(resource.getURL(), str(InetAddressUtils.ONE_TWENTY_SEVEN) + "/0");
        }
        SnmpAgentAddress listenAddress = new SnmpAgentAddress(agent.getInetAddress(), agent.getPort());
        mapper.addProxy(hostAddress, listenAddress);
        testContext.setAttribute(IPADDRESS_KEY, listenAddress.getAddress());
        testContext.setAttribute(PORT_KEY, listenAddress.getPort());
        LOG.debug("using MockSnmpAgent on {} for 'real' address {}", listenAddress, agentAddress);
        provider.addAgent(agentAddress, agent);
    }
    provider.setDataForAddress(agentAddress, resource);
}
Also used : SnmpAgentAddress(org.opennms.netmgt.snmp.SnmpAgentAddress) ResourceLoader(org.springframework.core.io.ResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) MockSnmpAgent(org.opennms.mock.snmp.MockSnmpAgent) SnmpAgentConfigFactory(org.opennms.netmgt.config.api.SnmpAgentConfigFactory) Resource(org.springframework.core.io.Resource) InetAddress(java.net.InetAddress) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader)

Example 20 with DefaultResourceLoader

use of org.springframework.core.io.DefaultResourceLoader in project opennms by OpenNMS.

the class ProvisionerIT method testProvisionerServiceRescanAfterAddingSnmpNms7838.

// fail if we take more than five minutes
@Test(timeout = 300000)
// Start the test with an empty SNMP agent
@JUnitSnmpAgent(host = "198.51.100.201", port = 161, resource = "classpath:/snmpwalk-empty.properties")
public void testProvisionerServiceRescanAfterAddingSnmpNms7838() throws Exception {
    importFromResource("classpath:/requisition_then_scan2.xml", Boolean.TRUE.toString());
    final List<OnmsNode> nodes = getNodeDao().findAll();
    final OnmsNode node = nodes.get(0);
    runPendingScans();
    m_nodeDao.flush();
    assertEquals(1, getInterfaceDao().countAll());
    // Make sure that the OnmsIpInterface doesn't have an ifIndex
    assertNull(getInterfaceDao().get(node, "198.51.100.201").getIfIndex());
    // Make sure that no OnmsSnmpInterface records exist for the node
    assertNull(getSnmpInterfaceDao().findByNodeIdAndIfIndex(node.getId(), 4));
    assertNull(getSnmpInterfaceDao().findByNodeIdAndIfIndex(node.getId(), 5));
    LOG.info("******************** ADDING SNMP DATA ********************");
    // Add some SNMP data to the agent
    m_mockSnmpDataProvider.setDataForAddress(new SnmpAgentAddress(addr("198.51.100.201"), 161), new DefaultResourceLoader().getResource("classpath:/snmpTestData3.properties"));
    // Rescan
    m_mockEventIpcManager.sendEventToListeners(nodeUpdated(node.getId()));
    runPendingScans();
    m_nodeDao.flush();
    // Make sure that a second interface was added from the SNMP agent data
    assertEquals(2, getInterfaceDao().countAll());
    // Verify the ifIndex entries
    assertEquals(5, getInterfaceDao().get(node, "198.51.100.201").getIfIndex().intValue());
    assertEquals(5, getInterfaceDao().get(node, "198.51.100.201").getSnmpInterface().getIfIndex().intValue());
    assertEquals(4, getInterfaceDao().get(node, "198.51.100.204").getIfIndex().intValue());
    assertEquals(4, getInterfaceDao().get(node, "198.51.100.204").getSnmpInterface().getIfIndex().intValue());
}
Also used : SnmpAgentAddress(org.opennms.netmgt.snmp.SnmpAgentAddress) OnmsNode(org.opennms.netmgt.model.OnmsNode) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) Test(org.junit.Test) JUnitSnmpAgent(org.opennms.core.test.snmp.annotations.JUnitSnmpAgent)

Aggregations

DefaultResourceLoader (org.springframework.core.io.DefaultResourceLoader)39 Test (org.junit.Test)24 ResourceLoader (org.springframework.core.io.ResourceLoader)8 Before (org.junit.Before)5 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)5 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)5 Resource (org.springframework.core.io.Resource)4 URLClassLoader (java.net.URLClassLoader)3 DefaultNamedComponent (example.scannable.DefaultNamedComponent)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 X509Certificate (java.security.cert.X509Certificate)2 SnmpAgentAddress (org.opennms.netmgt.snmp.SnmpAgentAddress)2 LoggerContext (ch.qos.logback.classic.LoggerContext)1 Context (ch.qos.logback.core.Context)1 DevComponent (example.profilescan.DevComponent)1 ProfileAnnotatedComponent (example.profilescan.ProfileAnnotatedComponent)1 ProfileMetaAnnotatedComponent (example.profilescan.ProfileMetaAnnotatedComponent)1 CustomStereotype (example.scannable.CustomStereotype)1 FooDao (example.scannable.FooDao)1