Search in sources :

Example 1 with ResourceLoader

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

the class DroolsTicketerServiceLayerTest method setUp.

@Before
public void setUp() throws Exception {
    m_eventIpcManager = new MockEventIpcManager();
    EventIpcManagerFactory.setIpcManager(m_eventIpcManager);
    MockLogAppender.setupLogging();
    ResourceLoader loader = new DefaultResourceLoader();
    Resource resource = loader.getResource("classpath:/drools-ticketer-rules.drl");
    m_easyMockUtils = new EasyMockUtils();
    m_configDao = m_easyMockUtils.createMock(DroolsTicketerConfigDao.class);
    EasyMock.expect(m_configDao.getRulesFile()).andReturn(resource.getFile()).times(1);
    EasyMock.replay(m_configDao);
    m_alarmDao = m_easyMockUtils.createMock(AlarmDao.class);
    m_ticketerPlugin = m_easyMockUtils.createMock(Plugin.class);
    m_droolsTicketerServiceLayer = new DroolsTicketerServiceLayer(m_configDao);
    m_droolsTicketerServiceLayer.setAlarmDao(m_alarmDao);
    m_droolsTicketerServiceLayer.setTicketerPlugin(m_ticketerPlugin);
    EasyMock.reset(m_configDao);
    m_alarm = new OnmsAlarm();
    m_alarm.setId(1);
    m_alarm.setLogMsg("Test Logmsg");
    m_alarm.setDescription("Test Description");
    m_alarm.setUei("uei.opennms.org/nodes/nodeDown");
    m_ticket = new Ticket();
    m_ticket.setId("4");
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) Ticket(org.opennms.api.integration.ticketing.Ticket) MockEventIpcManager(org.opennms.netmgt.dao.mock.MockEventIpcManager) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) Resource(org.springframework.core.io.Resource) EasyMockUtils(org.opennms.test.mock.EasyMockUtils) AlarmDao(org.opennms.netmgt.dao.api.AlarmDao) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) Plugin(org.opennms.api.integration.ticketing.Plugin) Before(org.junit.Before)

Example 2 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project grails-core by grails.

the class DefaultGroovyPageLocator method findResource.

protected Resource findResource(List<String> searchPaths) {
    Resource foundResource = null;
    Resource resource;
    for (ResourceLoader loader : resourceLoaders) {
        for (String path : searchPaths) {
            resource = loader.getResource(path);
            if (resource != null && resource.exists()) {
                foundResource = resource;
                break;
            }
        }
        if (foundResource != null)
            break;
    }
    return foundResource;
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) GrailsResource(org.grails.core.io.GrailsResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource)

Example 3 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project cas by apereo.

the class CasCoreAuthenticationPolicyConfiguration method authenticationPolicyExecutionPlanConfigurer.

@ConditionalOnMissingBean(name = "authenticationPolicyExecutionPlanConfigurer")
@Bean
public AuthenticationEventExecutionPlanConfigurer authenticationPolicyExecutionPlanConfigurer() {
    return plan -> {
        final AuthenticationPolicyProperties police = casProperties.getAuthn().getPolicy();
        if (police.getReq().isEnabled()) {
            LOGGER.debug("Activating authentication policy [{}]", RequiredHandlerAuthenticationPolicy.class.getSimpleName());
            plan.registerAuthenticationPolicy(new RequiredHandlerAuthenticationPolicy(police.getReq().getHandlerName(), police.getReq().isTryAll()));
        } else if (police.getAll().isEnabled()) {
            LOGGER.debug("Activating authentication policy [{}]", AllAuthenticationPolicy.class.getSimpleName());
            plan.registerAuthenticationPolicy(new AllAuthenticationPolicy());
        } else if (police.getNotPrevented().isEnabled()) {
            LOGGER.debug("Activating authentication policy [{}]", NotPreventedAuthenticationPolicy.class.getSimpleName());
            plan.registerAuthenticationPolicy(notPreventedAuthenticationPolicy());
        } else if (police.getUniquePrincipal().isEnabled()) {
            LOGGER.debug("Activating authentication policy [{}]", UniquePrincipalAuthenticationPolicy.class.getSimpleName());
            plan.registerAuthenticationPolicy(new UniquePrincipalAuthenticationPolicy(ticketRegistry.getIfAvailable()));
        } else if (!police.getGroovy().isEmpty()) {
            LOGGER.debug("Activating authentication policy [{}]", GroovyScriptAuthenticationPolicy.class.getSimpleName());
            police.getGroovy().forEach(groovy -> plan.registerAuthenticationPolicy(new GroovyScriptAuthenticationPolicy(resourceLoader, groovy.getScript())));
        } else if (!police.getRest().isEmpty()) {
            LOGGER.debug("Activating authentication policy [{}]", RestfulAuthenticationPolicy.class.getSimpleName());
            police.getRest().forEach(r -> plan.registerAuthenticationPolicy(new RestfulAuthenticationPolicy(new RestTemplate(), r.getEndpoint())));
        } else if (police.getAny().isEnabled()) {
            LOGGER.debug("Activating authentication policy [{}]", AnyAuthenticationPolicy.class.getSimpleName());
            plan.registerAuthenticationPolicy(new AnyAuthenticationPolicy(police.getAny().isTryAll()));
        }
    };
}
Also used : CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) AuthenticationPolicy(org.apereo.cas.authentication.AuthenticationPolicy) GeoLocationService(org.apereo.cas.authentication.adaptive.geo.GeoLocationService) Autowired(org.springframework.beans.factory.annotation.Autowired) ObjectProvider(org.springframework.beans.factory.ObjectProvider) TicketRegistry(org.apereo.cas.ticket.registry.TicketRegistry) NotPreventedAuthenticationPolicy(org.apereo.cas.authentication.policy.NotPreventedAuthenticationPolicy) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) Qualifier(org.springframework.beans.factory.annotation.Qualifier) DefaultAdaptiveAuthenticationPolicy(org.apereo.cas.authentication.adaptive.DefaultAdaptiveAuthenticationPolicy) RestTemplate(org.springframework.web.client.RestTemplate) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) UniquePrincipalAuthenticationPolicy(org.apereo.cas.authentication.policy.UniquePrincipalAuthenticationPolicy) ResourceLoader(org.springframework.core.io.ResourceLoader) RequiredHandlerAuthenticationPolicyFactory(org.apereo.cas.authentication.policy.RequiredHandlerAuthenticationPolicyFactory) AnyAuthenticationPolicy(org.apereo.cas.authentication.policy.AnyAuthenticationPolicy) GroovyScriptAuthenticationPolicy(org.apereo.cas.authentication.policy.GroovyScriptAuthenticationPolicy) AuthenticationPolicyProperties(org.apereo.cas.configuration.model.core.authentication.AuthenticationPolicyProperties) ApplicationContext(org.springframework.context.ApplicationContext) AllAuthenticationPolicy(org.apereo.cas.authentication.policy.AllAuthenticationPolicy) Configuration(org.springframework.context.annotation.Configuration) Slf4j(lombok.extern.slf4j.Slf4j) ContextualAuthenticationPolicyFactory(org.apereo.cas.authentication.ContextualAuthenticationPolicyFactory) RequiredHandlerAuthenticationPolicy(org.apereo.cas.authentication.policy.RequiredHandlerAuthenticationPolicy) Bean(org.springframework.context.annotation.Bean) AuthenticationEventExecutionPlanConfigurer(org.apereo.cas.authentication.AuthenticationEventExecutionPlanConfigurer) RestfulAuthenticationPolicy(org.apereo.cas.authentication.policy.RestfulAuthenticationPolicy) AdaptiveAuthenticationPolicy(org.apereo.cas.authentication.adaptive.AdaptiveAuthenticationPolicy) AnyAuthenticationPolicy(org.apereo.cas.authentication.policy.AnyAuthenticationPolicy) RequiredHandlerAuthenticationPolicy(org.apereo.cas.authentication.policy.RequiredHandlerAuthenticationPolicy) RestTemplate(org.springframework.web.client.RestTemplate) GroovyScriptAuthenticationPolicy(org.apereo.cas.authentication.policy.GroovyScriptAuthenticationPolicy) RestfulAuthenticationPolicy(org.apereo.cas.authentication.policy.RestfulAuthenticationPolicy) AuthenticationPolicyProperties(org.apereo.cas.configuration.model.core.authentication.AuthenticationPolicyProperties) UniquePrincipalAuthenticationPolicy(org.apereo.cas.authentication.policy.UniquePrincipalAuthenticationPolicy) AllAuthenticationPolicy(org.apereo.cas.authentication.policy.AllAuthenticationPolicy) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 4 with ResourceLoader

use of org.springframework.core.io.ResourceLoader 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 5 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project com.revolsys.open by revolsys.

the class ModuleImport method getApplicationContext.

protected GenericApplicationContext getApplicationContext(final BeanDefinitionRegistry parentRegistry) {
    if (this.applicationContext == null) {
        this.applicationContext = new GenericApplicationContext();
        if (parentRegistry instanceof ResourceLoader) {
            final ResourceLoader resourceLoader = (ResourceLoader) parentRegistry;
            final ClassLoader classLoader = resourceLoader.getClassLoader();
            this.applicationContext.setClassLoader(classLoader);
        }
        AnnotationConfigUtils.registerAnnotationConfigProcessors(this.applicationContext, null);
        final DefaultListableBeanFactory beanFactory = this.applicationContext.getDefaultListableBeanFactory();
        final BeanFactory parentBeanFactory = (BeanFactory) parentRegistry;
        for (final String beanName : parentRegistry.getBeanDefinitionNames()) {
            final BeanDefinition beanDefinition = parentRegistry.getBeanDefinition(beanName);
            final String beanClassName = beanDefinition.getBeanClassName();
            if (beanClassName != null) {
                if (beanClassName.equals(AttributeMap.class.getName())) {
                    registerTargetBeanDefinition(this.applicationContext, parentBeanFactory, beanName, beanName);
                    this.beanNamesNotToExport.add(beanName);
                } else if (beanClassName.equals(MapFactoryBean.class.getName())) {
                    final PropertyValue targetMapClass = beanDefinition.getPropertyValues().getPropertyValue("targetMapClass");
                    if (targetMapClass != null) {
                        final Object mapClass = targetMapClass.getValue();
                        if (AttributeMap.class.getName().equals(mapClass)) {
                            registerTargetBeanDefinition(this.applicationContext, parentBeanFactory, beanName, beanName);
                            this.beanNamesNotToExport.add(beanName);
                        }
                    }
                }
            }
        }
        beanFactory.addPropertyEditorRegistrar(this.resourceEditorRegistrar);
        final AttributesBeanConfigurer attributesConfig = new AttributesBeanConfigurer(this.applicationContext, this.parameters);
        this.applicationContext.addBeanFactoryPostProcessor(attributesConfig);
        for (final String beanName : this.importBeanNames) {
            registerTargetBeanDefinition(this.applicationContext, parentBeanFactory, beanName, beanName);
            this.beanNamesNotToExport.add(beanName);
        }
        for (final Entry<String, String> entry : this.importBeanAliases.entrySet()) {
            final String beanName = entry.getKey();
            final String aliasName = entry.getValue();
            registerTargetBeanDefinition(this.applicationContext, parentBeanFactory, beanName, aliasName);
            this.beanNamesNotToExport.add(aliasName);
        }
        final XmlBeanDefinitionReader beanReader = new XmlBeanDefinitionReader(this.applicationContext);
        for (final Resource resource : this.resources) {
            beanReader.loadBeanDefinitions(resource);
        }
        this.applicationContext.refresh();
    }
    return this.applicationContext;
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) AttributesBeanConfigurer(com.revolsys.spring.config.AttributesBeanConfigurer) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Resource(com.revolsys.spring.resource.Resource) PropertyValue(org.springframework.beans.PropertyValue) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) AttributeMap(com.revolsys.collection.map.AttributeMap) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory)

Aggregations

ResourceLoader (org.springframework.core.io.ResourceLoader)90 Resource (org.springframework.core.io.Resource)51 Test (org.junit.Test)35 ClassRelativeResourceLoader (org.springframework.core.io.ClassRelativeResourceLoader)34 File (java.io.File)33 DefaultResourceLoader (org.springframework.core.io.DefaultResourceLoader)26 Test (org.junit.jupiter.api.Test)15 IOException (java.io.IOException)9 Environment (org.springframework.core.env.Environment)9 ByteArrayResource (org.springframework.core.io.ByteArrayResource)7 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)6 FileSystemResource (org.springframework.core.io.FileSystemResource)6 HashMap (java.util.HashMap)5 Configuration (freemarker.template.Configuration)4 Template (freemarker.template.Template)4 InputStream (java.io.InputStream)4 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)4 InputStreamReader (java.io.InputStreamReader)3 Properties (java.util.Properties)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3