deployerConfigContext.xml has no effect - spring

I tried to get a CAS-server up and running using https://github.com/apereo/cas-overlay-template for a side project (I'm a student), but I have never used maven or spring before.
I'm trying to hook my own IPersonAttributeDao into the CAS-server. However, when I put my deployerConfigContext.xml under src/main/webapp/WEB-INF/, nothing actually changed when I repackaged (using the build script) and deployed to tomcat8. (Yes, I did restart tomcat).
Below the contents of my deployerConfigContent.xml file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<bean id="authenticationManager" class="org.jasig.cas.authentication.AuthenticationManagerImpl">
<property name="credentialsToPrincipalResolvers">
<list>
<bean id="primaryPrincipalResolver"
class="org.jasig.cas.authentication.principal.PersonDirectoryPrincipalResolver">
<property name="attributeRepository" ref="attributeRepository"/>
</bean>
<bean class="org.jasig.cas.authentication.principal.UsernamePasswordCredentialsToPrincipalResolver">
<property name="attributeRepository" ref="attributeRepository"/>
</bean>
<bean class="org.jasig.cas.authentication.principal.HttpBasedServiceCredentialsToPrincipalResolver">
<property name="attributeRepository" ref="attributeRepository"/>
</bean>
</list>
</property>
<property name="authenticationHandlers">
<list>
<bean id="primaryAuthenticationHandler"
class="org.jasig.cas.authentication.AcceptUsersAuthenticationHandler">
<property name="users">
<map>
<!-- Login stays the default casuser:Mellon, no idea why -->
<entry key="test" value="1234"/>
</map>
</property>
</bean>
<!-- DO NOT EVER PUT THIS BEAN IN PRODUCTION!!! -->
<bean class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler"/>
</list>
</property>
</bean>
<!-- <bean id="attributeRepository" class="class.i.am.trying.to.hook.in">
</bean>-->
<!-- This doesn't seem to work -->
<bean id="attributeRepository"
class="org.jasig.services.persondir.support.StubPersonAttributeDao">
<property name="backingMap">
<map>
<entry key="uid" value="uid" />
<entry key="eduPersonAffiliation" value="eduPersonAffiliation" />
<entry key="groupMembership" value="groupMembership" />
</map>
</property>
</bean>
</beans>
What am I missing?
UPDATE:
I reread the documentation (https://apereo.github.io/cas/5.0.x/), and found that deployerConfigContext.xml was supposed to be in resources, and not in webapp/WEB-INF as I found on the internet. I moved it, and now I'm finally getting errors in the log (which means the file is being read).
Error below:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.jasig.cas.authentication.PolicyBasedAuthenticationManager] for bean with name 'authenticationManager' defined in class path resource [deployerConfigContext.xml]
I found a reference to PolicyBasedAuthenticationManager in the docs, but trying that gave the same error (with the other classname ofcourse).

Bean classes of enabled beans must be deployed in bean archives.
A library jar, EJB jar, application client jar or rar archive is a
bean archive if it has a file named beans.xml in the META-INF
directory. The WEB-INF/classes directory of a war is a bean archive if
there is a file named beans.xml in the WEB-INF directory of the war. A
directory in the JVM classpath is a bean archive if it has a file
named beans.xml in the META-INF directory.
One suggestion, if you are using Spring, you can also set #Annotation to define beans.

Related

Liferay 7 MessageListener osgi module

When I worked with the liferay 6.1 I created an application that received the message.
Java class:
public class MailMessageBus implements MessageListener
\src\main\webapp\WEB-INF\src\META-INF\messaging-spring.xml file:
<beans
default-destroy-method="destroy"
default-init-method="afterPropertiesSet"
xmlns="http://www.springframework.org/schema/beans"
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-3.0.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="messagingConfigurator" class="com.liferay.portal.kernel.messaging.config.PluginMessagingConfigurator">
<property name="messageListeners">
<map key-type="java.lang.String" value-type="java.util.List">
<entry key="mail-send-message">
<list value-type="com.liferay.portal.kernel.messaging.MessageListener">
<ref bean="messageListener.mail_listener" />
</list>
</entry>
</map>
</property>
<property name="destinations">
<list>
<ref bean="destination.mail"/>
</list>
</property>
</bean>
<!-- Destination class -->
<bean id="destination.mail" class="com.liferay.portal.kernel.messaging.ParallelDestination">
<property name="name" value="mail-send-message" />
</bean>
<!-- Listener class -->
<bean id="messageListener.mail_listener" class="customportlet.AMessageBusListener.MailMessageBus" />
and \src\main\webapp\WEB-INF\web.xml file:
...
<context-param>
<param-name>portalContextConfigLocation</param-name>
<param-value>/WEB-INF/src/META-INF/messaging-spring.xml</param-value>
</context-param>
...
How can I do the same for liferay 7 in the OSGi module? OSGi module does not have a web.xml file.
Thank you very much!
I'm trying to accomplish the same thing, and based on what I've found so far the web.xml is unnecessary. OSGi modules seem to detect spring configuration automatically, according to this resource:
https://docs.spring.io/spring-osgi/docs/current/reference/html/app-deploy.html
Just put your configuration in xml files located at META-INF/spring

Spring does not find messages.properties

I'm working on a maven project using Spring framework.
Instead of writing raw text in JSPs I prefer to use <spring:message .../> tag and register my messages in a .properties file.
I get this warning when requesting a page :
ResourceBundle [messages] not found for MessageSource: Can't
find bundle for base name messages, locale fr
Which then brings an exception about the message not being found (obviously).
Here's my project's hierarchy :
my project's hierarchy
Here's my springapp-servlet.xml :
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="app.core" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/">
</property>
<property name="suffix" value=".jsp">
</property>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="fr" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
</bean>
<mvc:resources mapping="/public/**" location="/WEB-INF/resources/"
cache-period="31556926"/>
<mvc:annotation-driven />
</beans>
From the documentation:
The basenames follow ResourceBundle conventions: essentially, a fully-qualified classpath location. If it doesn't contain a package qualifier (such as org.mypackage), it will be resolved from the classpath root.
(emphasis mine)
So it should be under src/main/resources.

Spring scopes in embedded web app in Jetty server

I configured Jetty to start web app from spring context, e.i. there are two contexts:
external context which runs Jetty
internal which is run by Spring DispatcherServlet
Here is the definition of external context:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<bean id="servletHolder" class="org.eclipse.jetty.servlet.ServletHolder">
<constructor-arg name="name" value="dispatcher"/>
<constructor-arg name="servlet" value="org.springframework.web.servlet.DispatcherServlet"/>
<property name="initParameters">
<map>
<entry key="contextConfigLocation" value="classpath:dispatcher-context.xml"/>
</map>
</property>
<property name="initOrder" value="0"/>
</bean>
<bean id="servletHandler" class="org.eclipse.jetty.servlet.ServletHandler">
<property name="servlets">
<array>
<ref bean="servletHolder"/>
</array>
</property>
<property name="servletMappings">
<list>
<bean class="org.eclipse.jetty.servlet.ServletMapping">
<property name="servletName" value="dispatcher"/>
<property name="pathSpec" value="/*"/>
</bean>
</list>
</property>
</bean>
<bean id="contextHandler" class="org.eclipse.jetty.servlet.ServletContextHandler">
<property name="contextPath" value="/spring"/>
<property name="servletHandler" ref="servletHandler"/>
</bean>
<bean class="org.eclipse.jetty.server.Server" init-method="start" destroy-method="stop">
<constructor-arg name="port" value="9999"/>
<property name="handler" ref="contextHandler"/>
</bean>
</beans>
It refers to dispatcher-context.xml which is "internal".
So far it work fine unless I need to inject beans from external context into controller's beans from internal context.
Is there any way to define bean in outer scope and inject it into controller?
It would be useful in unit-test or using this server as a part of bigger existing app.
The full code is available here: https://github.com/zjor/embedded-jetty/tree/master/spring-mvc-jetty
So far I've found a workaround, I've used a combination of Jersey with Jetty and configured everything via Spring.
Code is available here: https://github.com/zjor/embedded-jetty/tree/master/jersey-jetty

Properties not being substituted in applicationContext.xml

I'm facing a very weird issue. I'm tring to configure the properties of my datasource declared in my spring applicationcontext.xml file via a PropertyPlaceholderConfigurer class. The application context file looks like this:-
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ws="http://jax-ws.dev.java.net/spring/core" xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://jax-ws.dev.java.net/spring/core http://jax-ws.dev.java.net/spring/core.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.4.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Spring configurations used by LCM Impl classes -->
<bean id="lcmPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/activemq.properties</value>
<value>classpath:/jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy- method="close">
<property name="driverClassName" value="${db.driverClassName}" />
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
I have tried using the context:property-placeholder tag as well but it doesn't seem to have any effect. The above mentioned properties files are deployed successfully under the WEB-INF/classes directory as well.
For some reason, the spring container is able to load the properties files(checked with an invalide properties file and it throws a FNF exception) but not able to substitute the property placeholders with their values.
I'm using a tomcat 7 WS with the CATALINA.BASE pointing to my runtime. Has anybody faced this issue before? Any solutions?
Take that leading slash out of the values:
<value>classpath:activemq.properties</value>
<value>classpath:jdbc.properties</value>
Try the following configuration.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:activemq.properties</value>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
The issue is likely the fact that you even named the PropertyPlaceholderConfigurer bean. Most Spring internal beans are not given an id, as the framework is configured to look for certain bean names, and often the default bean name is the auto-generated name based on the object type.

Load webserver context.xml using Spring

Fairly new to Spring, so I'm having some trouble with this. I'm trying to use LDAP security with Spring. I can use a properties file I created inside the webapp itself. But what I would like to do is load and read the context.xml file of the server (it has all the values I need for this and other applications).
This is what I have:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="searchContextAttributes" value="true"/>
<property name="contextOverride" value="true"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>/WEB-INF/properties/dataUploadProperties.properties</value>
<value>/WEB-INF/properties/globalProperties.properties</value>
<value>context.xml</value>
</list>
</property>
</bean>
I'm able to load and read the 2 properties files, but the context.xml is not found. Does it need to be the absolute path on the server?
Thanks
Chris
So the first thing I would recommend is to use Spring Security. It has an already build in LDAP support.
but the context.xml is not found
Normally this (reading the context.xml directly) is not the way you should go.
Instead, define some properties and or JNDI resources in the context.xml and then use them in the spring configuration.
For example:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jee="http://www.springframework.org/schema/jee"
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-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<!-- access via jndi -->
<jee:jndi-lookup id="jndiEmailSession"
jndi-name="java:comp/env/email/session/myEmailSession" />
<!-- direct access for properties required the SERVLET contect property
place older configurer, then it works like properties from normal
property files -->
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"> <property name="locations" value="classpath*:META-INF/spring/*.properties" /> </bean>
<bean class=Demo>
<property name="someString" value="${simpleValue}" />
</bean>
</beans>
context.xml:
<Resource name="email/session/myEmailSession"
type="javax.mail.Session"
auth="Container"
password="secret"
mail.debug="false"
mail.transport.protocol="smtp"
mail.smtp.auth="true"
mail.smtp.user="test#example.com"
mail.smtp.host="mail.example.com"
mail.smtp.from="test#example.com"/>
<Parameter name="simpleValue" value="any" override="false" />

Resources