Spring 3 JNDI look up in glassfish3 - spring

I want to look up some properties from JNDI configured in glassfish v3 server. I want to do it using spring. Here is my spring configuration:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<!--
JNDI look ups.
!-->
<jee:jndi-lookup id="properties"
jndi-name="java:comp/env/jndi/ws_properties"
expected-type="java.util.Properties"/>
</beans>
I have mapped jndi/ws_properties in sun-web.xml and web.xml files. Problem is that this lookup always gives me null properties. But if I do it in java code:
try {
InitialContext context = new InitialContext();
properties = (Properties) context.lookup("jndi/ws_properties");
} catch (NamingException e) {
LOGGER.error("", e);
}
It is ok. I see my properties keys and values.
Could somebody tell me where is the problem here?

This is probably because of your "jndi-name" property.
You don't have to put "java:comp/env/" in the name.
The "resource-ref" property defaults to true and unless you set it to false, it will automatically add the java:comp/env to the name.

Related

Spring AOP intercepting some Public Method but not other

I have placed and interceptor in my aspect class as
#Before("execution( * com.mycompany.a.service.weblab.b.doesTreatmentEqualsAndTrigger(..))"){
.........
}
this interceptor works fine but when i place this interceptor
#Before("execution( * mycompany.a.b.bsf.c.requestReply(..))"){
............
}
the code is not intercepted.
My aspect class is in com.mycompany.x package and both the function on which i am trying to place the interceptor are public as needed by spring AOP.
My .xml is as follows:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="no">
<context:annotation-config/>
<aop:aspectj-autoproxy/>
<bean name="com.a.serviceInterceptor" class="com.mycompany.x.RequestSpecificServiceAspect"/>
</beans>
Why is the second function not getting intercepted? Is it because of the vastly different package since they don't have anything in common? If so then how i can resolve this ?

codahale metrics aren't being recorded

I'm experimenting with the codahale metrics and metrics-spring packages. I've been unable to get the metrics recorded.
Function Call
#Counted
public Response getSomething(#Context final HttpServletRequest request) {
return Response.ok(doWork()).build();
}
metrics-spring.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:metrics="http://www.ryantenney.com/schema/metrics"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.ryantenney.com/schema/metrics
http://www.ryantenney.com/schema/metrics/metrics-3.0.xsd">
<!-- Registry should be defined in only one context XML file -->
<metrics:metric-registry id="metrics"/>
<!-- annotation-driven must be included in all context files -->
<metrics:annotation-driven metric-registry="metrics"/>
<!-- (Optional) Registry should be defined in only one context XML file -->
<metrics:reporter type="console" metric-registry="metrics" period="10s"/>
</beans>
beans.xml
<?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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:metrics-spring.xml"/>
...snip....
Output
When I run this in a war file and hit the endpoint, the counter isn't incremented. It stays at 0
5/19/15 5:01:48 PM =============================================================
-- Counters --------------------------------------------------------------------
package.getSomething
count = 0
It seems like everything is wired together correctly. Data is getting dumped to the console and it knows about the counter. What am I doing wrong?

Replace cxf parameters with spring (dynamically)

I have following cxf.xml:
<?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:jaxws="http://cxf.apache.org/jaxws"
xmlns:http="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd">
<http:conduit name="http://localhost:8080/mockInterfaceSoap">
<http:client ReceiveTimeout="5000" />
</http:conduit>
</beans>
Is there any way to replace hardcoded ReceiveTimeout parameter value with spring?
EDIT:
I want to change it dynamically in runtime. Change is quite rare so app can change existing endpoint or create new one.
Maybe org.apache.cxf.jaxrs.client.WebClient helps. I have found some time ago a reference to it and have used it in the following example:
String endpointAddress = "*";
WebClient client = WebClient.create(endpointAddress);
HTTPConduit httpConduit = WebClient.getConfig(client).getHttpConduit();
httpConduit.setTlsClientParameters(new TLSClientParameters());
httpConduit.getTlsClientParameters().setUseHttpsURLConnectionDefaultHostnameVerifier(true);
httpConduit.getTlsClientParameters().setUseHttpsURLConnectionDefaultSslSocketFactory(true);
return httpConduit;
You can use it as follows.
<jaxrs-client:client id="clientId" serviceName="clientServiceName"
address="${urlService}" >
</<jaxrs-client:client>
<http-conf:conduit name="*clientServiceName*.http-conduit" >
<http-conf:client ReceiveTimeout="1"/>
</http-conf:conduit>

Adding Xml configurations to a Spring ApplicationContext that is already setup

I was wondering if there is a way to do the following:
have my wep app startup with its servle-context.xml
When, at a certain point, one particular bean in this xml config is instantiated, it will add it's own xml configuration to the application context (or to a child perhaps?).
I'm asking this because I want to pack some functionality in a stand alone library and then reuse it in different projects, so that initializing a bean of this library will load its xml config.
What I wrote is:
public class IrisLibHelper {
ApplicationContext context;
ApplicationContext irisContext;
#Required
#Autowired
public void setContext(ApplicationContext ctx){
this.context = ctx;
ClassPathXmlApplicationContext xap = new ClassPathXmlApplicationContext(ctx);
xap.setConfigLocation("classpath:com/dariodario/irislib/xmldefs/irisconfig.xml");
this.irisContext = xap;
}
public ApplicationContext getIrisContext() {
return irisContext;
}
public void setIrisContext(ApplicationContext irisContext) {
this.irisContext = irisContext;
}
}
and the irisconfig.xml is:
<?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:mvc="http://www.springframework.org/schema/mvc"
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- <context:component-scan base-package="com.dariodario"></context:component-scan> -->
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
p:synchronizeOnSession="true" />
<context:component-scan base-package="com.dariodario.iris.controllers"></context:component-scan>
</beans>
The problem is that it doesn't seem to scan the com.dariodario.iris.controllers package, in fact the controllers don't get mapped! (I've logging debugging on and I don't seen anything).
Why not use the tag <import resource="classpath:applicationConfig.xml" /> ? You can load a spring configuration file which is in jar. In a jar, the Spring XML configuration is always at the root. But if not, you can use this notation: <import resource="${configurablePath}/applicationConfig.xml" /> where configurablePath can be reach by a property place holder or other.
I think this way is cleaner than merging two Spring context.

loading multiple properties files from projects in workspace

I have three projects - proj-a, proj-b, and main such that main depends on proj-a and proj-b.
proj-a and proj-b each contains a module-context.xml and properties file.
proj-a module-context.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="classpath:/META-INF/proj-a.properties"/>
<bean ... someProperty="${property-a}" />
</bean>
proj-a.properties
property-a=hello-a
proj-b's configuration is identical except a is replaced by b.
proj-b module-context.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="classpath:/META-INF/proj-b.properties"/>
<bean ... someProperty="${property-b}" />
</bean>
proj-b.properties
property-b=hello-b
A Class in main wants to create an ApplicationContext that consists of both proj-a and proj-b's module-context.xml. The problem is that only one of the properties file is processed by spring. If proj-a's module-context.xml's loaded first, proj-b's properties file is never read.
The following snippet throws an Exception.
public static void main( String[] args ) throws IOException {
ApplicationContext context = new FileSystemXmlApplicationContext( new String[] {
"../proj-a/src/main/resources/META-INF/spring/module-context.xml",
"../proj-b/src/main/resources/META-INF/spring/module-context.xml"
} );
}
throws
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name '...' defined in file [...\proj-b\src\main\resources\META-INF\spring\module-context.xml]: Could not resolve placeholder 'property-b'
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:272)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:624)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:599)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:398)
How can I properly load the properties files? Merging them is not a solution because properties are project specific.
I found a solution - set property ignoreUnresolvablePlaceholders to true.

Resources