Search in sources :

Example 1 with ApplicationContext

use of org.springframework.context.ApplicationContext in project cas by apereo.

the class AbstractRegisteredServiceAttributeReleasePolicy method getReleasedByDefaultAttributes.

/**
     * Determines a default bundle of attributes that may be released to all services
     * without the explicit mapping for each service.
     *
     * @param p          the principal
     * @param attributes the attributes
     * @return the released by default attributes
     */
protected Map<String, Object> getReleasedByDefaultAttributes(final Principal p, final Map<String, Object> attributes) {
    final ApplicationContext ctx = ApplicationContextProvider.getApplicationContext();
    if (ctx != null) {
        LOGGER.debug("Located application context. Retrieving default attributes for release, if any");
        final CasConfigurationProperties props = ctx.getAutowireCapableBeanFactory().getBean(CasConfigurationProperties.class);
        final Set<String> defaultAttrs = props.getAuthn().getAttributeRepository().getDefaultAttributesToRelease();
        LOGGER.debug("Default attributes for release are: [{}]", defaultAttrs);
        final Map<String, Object> defaultAttributesToRelease = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
        defaultAttrs.stream().forEach(key -> {
            if (attributes.containsKey(key)) {
                LOGGER.debug("Found and added default attribute for release: [{}]", key);
                defaultAttributesToRelease.put(key, attributes.get(key));
            }
        });
        return defaultAttributesToRelease;
    }
    return new TreeMap<>();
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) TreeMap(java.util.TreeMap)

Example 2 with ApplicationContext

use of org.springframework.context.ApplicationContext in project cas by apereo.

the class PrincipalAttributeRegisteredServiceUsernameProvider method getPrincipalAttributes.

/**
     * Gets principal attributes. Will attempt to locate the principal
     * attribute repository from the context if one is defined to use
     * that instance to locate attributes. If none is available,
     * will use the default principal attributes.
     *
     * @param p       the principal
     * @param service the service
     * @return the principal attributes
     */
protected Map<String, Object> getPrincipalAttributes(final Principal p, final Service service) {
    final ApplicationContext context = ApplicationContextProvider.getApplicationContext();
    if (context != null) {
        LOGGER.debug("Located application context to locate the service registry entry");
        final ServicesManager servicesManager = context.getBean(ServicesManager.class);
        if (servicesManager != null) {
            final RegisteredService registeredService = servicesManager.findServiceBy(service);
            if (registeredService != null && registeredService.getAccessStrategy().isServiceAccessAllowed()) {
                LOGGER.debug("Located service [{}] in the registry. Attempting to resolve attributes for [{}]", registeredService, p.getId());
                if (registeredService.getAttributeReleasePolicy() == null) {
                    LOGGER.debug("No attribute release policy is defined for [{}]. Returning default principal attributes", service.getId());
                    return p.getAttributes();
                }
                return registeredService.getAttributeReleasePolicy().getAttributes(p, registeredService);
            }
        }
        LOGGER.debug("Could not locate service [{}] in the registry.", service.getId());
        throw new UnauthorizedServiceException(UnauthorizedServiceException.CODE_UNAUTHZ_SERVICE);
    }
    LOGGER.warn("No application context could be detected. Returning default principal attributes");
    return p.getAttributes();
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext)

Example 3 with ApplicationContext

use of org.springframework.context.ApplicationContext in project sharding-jdbc by dangdangdotcom.

the class SpringNamespaceWithMasterSlaveMain method main.

// CHECKSTYLE:OFF
public static void main(final String[] args) throws SQLException {
    // CHECKSTYLE:ON
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF/applicationContextWithMasterSlave.xml");
    OrderService orderService = applicationContext.getBean(OrderService.class);
    orderService.insert();
    orderService.select();
    orderService.delete();
    orderService.select();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) OrderService(com.dangdang.ddframe.rdb.sharding.example.config.spring.masterslave.service.OrderService)

Example 4 with ApplicationContext

use of org.springframework.context.ApplicationContext in project druid by alibaba.

the class DruidStatServiceTest method test_statService_getResetAll.

public void test_statService_getResetAll() throws Exception {
    // data source mock
    String sql = "select 1";
    Connection conn = dataSource.getConnection();
    PreparedStatement stmt = conn.prepareStatement(sql);
    ResultSet rs = stmt.executeQuery();
    rs.next();
    rs.close();
    stmt.close();
    conn.close();
    String resultSQL = DruidStatService.getInstance().service("/sql.json");
    Map<String, Object> resultSQLMap = (Map<String, Object>) JSONUtils.parse(resultSQL);
    List<Map<String, Object>> sqlList = (List<Map<String, Object>>) resultSQLMap.get("Content");
    assertThat(sqlList.size(), equalTo(1));
    Map<String, Object> sqlStat = sqlList.get(0);
    assertThat((Integer) sqlStat.get("RunningCount"), equalTo(0));
    // http request mock
    String uri = "/";
    MockServletContext servletContext = new MockServletContext();
    MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
    WebStatFilter filter = new WebStatFilter();
    filter.init(filterConfig);
    // first request test
    MockHttpServletRequest request = new MockHttpServletRequest("GET", uri);
    MockHttpSession session = new MockHttpSession();
    request.setSession(session);
    String sessionId = session.getId();
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();
    filter.doFilter(request, response, chain);
    String resultWebSession = DruidStatService.getInstance().service("/websession.json");
    Map<String, Object> resultWebSessionMap = (Map<String, Object>) JSONUtils.parse(resultWebSession);
    List<Map<String, Object>> contentWebSessionList = (List<Map<String, Object>>) resultWebSessionMap.get("Content");
    assertThat(contentWebSessionList.size(), equalTo(1));
    Map<String, Object> contentWebSessionMap = contentWebSessionList.get(0);
    assertThat((String) contentWebSessionMap.get("SESSIONID"), equalTo(sessionId));
    // spring mock
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/alibaba/druid/stat/spring-config-stat.xml");
    UserService userService = (UserService) context.getBean("userService");
    userService.save();
    String resultSpring = DruidStatService.getInstance().service("/spring.json");
    Map<String, Object> resultSpringMap = (Map<String, Object>) JSONUtils.parse(resultSpring);
    List<Map<String, Object>> contentSpringList = (List<Map<String, Object>>) resultSpringMap.get("Content");
    assertThat(contentSpringList.size(), equalTo(1));
    Map<String, Object> contentMap = contentSpringList.get(0);
    assertThat((String) contentMap.get("Class"), is(not(nullValue())));
    assertThat((Integer) contentMap.get("ExecuteCount"), equalTo(1));
    // reset all test
    String result = DruidStatService.getInstance().service("/reset-all.json");
    Map<String, Object> resultMap = (Map<String, Object>) JSONUtils.parse(result);
    assertThat(resultMap.get("content"), is(nullValue()));
    // assert sql
    resultSQL = DruidStatService.getInstance().service("/sql.json");
    resultSQLMap = (Map<String, Object>) JSONUtils.parse(resultSQL);
    sqlList = (List<Map<String, Object>>) resultSQLMap.get("Content");
    assertThat(sqlList, is(nullValue()));
    // assert web session
    resultWebSession = DruidStatService.getInstance().service("/websession.json");
    resultWebSessionMap = (Map<String, Object>) JSONUtils.parse(resultWebSession);
    contentWebSessionList = (List<Map<String, Object>>) resultWebSessionMap.get("Content");
    assertThat(contentWebSessionList, is(nullValue()));
    // assert spring
    resultSpring = DruidStatService.getInstance().service("/spring.json");
    resultSpringMap = (Map<String, Object>) JSONUtils.parse(resultSpring);
    contentSpringList = (List<Map<String, Object>>) resultSpringMap.get("Content");
    assertThat(contentSpringList, is(nullValue()));
}
Also used : UserService(com.alibaba.druid.stat.spring.UserService) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) MockServletContext(org.springframework.mock.web.MockServletContext) WebStatFilter(com.alibaba.druid.support.http.WebStatFilter) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ResultSet(java.sql.ResultSet) MockHttpSession(org.springframework.mock.web.MockHttpSession) List(java.util.List) MockFilterChain(org.springframework.mock.web.MockFilterChain) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 5 with ApplicationContext

use of org.springframework.context.ApplicationContext in project druid by alibaba.

the class DruidStatServiceTest method test_statService_getSpring.

public void test_statService_getSpring() throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/alibaba/druid/stat/spring-config-stat.xml");
    UserService userService = (UserService) context.getBean("userService");
    userService.save();
    String result = DruidStatService.getInstance().service("/spring.json");
    Map<String, Object> resultMap = (Map<String, Object>) JSONUtils.parse(result);
    List<Map<String, Object>> contentList = (List<Map<String, Object>>) resultMap.get("Content");
    assertThat(contentList.size(), equalTo(1));
    Map<String, Object> contentMap = contentList.get(0);
    assertThat((String) contentMap.get("Class"), is(not(nullValue())));
    assertThat((Integer) contentMap.get("ExecuteCount"), equalTo(1));
    // second test
    userService.save();
    result = DruidStatService.getInstance().service("/spring.json");
    resultMap = (Map<String, Object>) JSONUtils.parse(result);
    contentList = (List<Map<String, Object>>) resultMap.get("Content");
    assertThat(contentList.size(), equalTo(1));
    contentMap = contentList.get(0);
    assertThat((String) contentMap.get("Class"), is(not(nullValue())));
    assertThat((Integer) contentMap.get("ExecuteCount"), equalTo(2));
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) UserService(com.alibaba.druid.stat.spring.UserService) List(java.util.List) Map(java.util.Map)

Aggregations

ApplicationContext (org.springframework.context.ApplicationContext)532 Test (org.junit.Test)256 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)161 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)38 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)32 ConfigurableMessenger (org.springframework.scripting.ConfigurableMessenger)28 File (java.io.File)24 Messenger (org.springframework.scripting.Messenger)24 DataSource (javax.sql.DataSource)23 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)21 Refreshable (org.springframework.aop.target.dynamic.Refreshable)20 StubCloudConnectorTest (org.springframework.cloud.StubCloudConnectorTest)17 SchedulerException (org.quartz.SchedulerException)16 HashMap (java.util.HashMap)15 Map (java.util.Map)14 WebApplicationContext (org.springframework.web.context.WebApplicationContext)14 MovieMapper (com.mapper.MovieMapper)13 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)13 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)13 ArrayList (java.util.ArrayList)12