JBoss: Binding values into JNDI in JBoss EAP 6 similar to JNDIBindingServiceMgr - java

How do I bind an arbitrary string to JNDI in JBoss EAP 6? I used to
do it through org.jboss.naming.JNDIBindingServiceMgr MBean in
previous EAP version.
Is there anything similar to org.jboss.naming.JNDIBindingServiceMgr
in JBoss EAP 6?
We are migrating applications from jboss-5.1.EAP to jboss-eap-6.1.
We need to bind some things into JNDI, so applications can look up
values of environment variables.
Many thanks.

You can do the following:
standalone.xml:
<subsystem xmlns="urn:jboss:domain:naming:1.2">
<bindings>
<simple name="java:global/user" value="newUser"/>
</bindings>
</subsystem>
and in spring context:
<bean class="java.util.Properties">
<constructor-arg>
<map>
<entry key="user">
<jee:jndi-lookup jndi-name="java:global/user" />
</entry>
</map>
</constructor-arg>
</bean>

In your app configuration you can have things in ejb-jar.xml deployment descriptor like
<javaee:env-entry>
<javaee:description>JNDI logging context for this app</javaee:description>
<javaee:env-entry-name>logback/context-name</javaee:env-entry-name>
<javaee:env-entry-type>java.lang.String</javaee:env-entry-type>
<javaee:env-entry-value>our-app-context</javaee:env-entry-value>
</javaee:env-entry>
or, if you prefer to have it in the server standalone.xml, do
<subsystem xmlns="urn:jboss:domain:naming:1.1">
<bindings>
<simple name="my/jndi/key" value="MyJndiValue"/>
</bindings>
</subsystem>
the latter (standalone.xml) is a JBoss 7.1 feature, so available in EAP 6.0. In JBoss AS 7.0, a dummy application needs to be used according to this thread.

What if simply:
InitialContext ctx = new InitialContext();
ctx.bind("varName", "value");
If you use that code inside of a JBoss instance you can bind variables into jndi. Remember to use the correct format for varName to bind the variable in the desired scope.

Related

Log4jConfigurer in Spring 5.0.2

I was using Spring 4.X.X and used the below setup to configure Log4j. Now am upgrading it to Spring 5.0.2 where the Log4jConfigurer class has been removed. How can I do it in Spring 5.0.2?
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="org.springframework.util.Log4jConfigurer"/>
<property name="targetMethod" value="initLogging"/>
<property name="arguments">
<list>
<value>classpath:log4j.properties</value>
</list>
</property>
</bean>
The Log4JConfigurer was required for non default Log4j initialisation e.g. if using a custom config file name/location but your config file is located in the default location: classpath:log4j.properties so you can simply remove the Log4jConfigurer declaration and Spring will auto discover your log4j.properties.
There is one possible caveat here; Spring 5 uses Log4j v2 (following Apache's EOL declaration for log4j 1.x) so as long as you are using Log4j v2 then Spring 5 will auto detect it and your log4j.properties file without any need to declare a Log4JConfigurer. If you are not currently using Log4j v2 then I think you'll need to upgrade since Spring 5 does not support the use of Log4j v1.x.

Connecting to activeMQ using camel blueprint

I'm using Camel blueprint on JBoss Developer studio, which is a new challenge for me.
I've googled and found stuff like this: http://camel.apache.org/activemq.html but what I'm trying to figure out is how you define your activeMQ connection if you're using the blueprint. Everything references and activeMQ bean, but nothing shows how to define it in the blueprint.
You should create an ActiveMQComponent outside the the camel context :
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<bean id="activemq"
class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="brokerURL"
value="tcp://localhost:61616" />
</bean>
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
...
</camelContext>
</blueprint>
Note that this is described as "spring XML configuration" in http://camel.apache.org/activemq.html. The blueprint XML schema is mostly the same as the one for Spring (you can list main differences at http://camel.apache.org/using-osgi-blueprint-with-camel.html), so in most case you can use what is described as "spring xml" in blueprint.

Tomcat connection pool with MyBatis

I was wondering if anyone has any code examples for setting up a connection pool in Tomcat (7 or later) using MyBatis as the ORM.
I presume I need to add a resource to my context.xml file in my Tomcat conf folder and then link that to MyBatis. I've had a look and any tutorials I have found seem to be Spring specific. Does anyone have a simple tutorial or can they outline the steps required to get this up and running?
There is this old entry in the iBatis FAQ that should still be applicable to myBatis: How do I use a JNDI DataSource with iBATIS in Tomcat?.
You can google for the details to configure a datasource in your Tomcat version, then configure MyBatis as well (it's different than the iBatis configuration):
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="JNDI">
<property name="data_source" value="java:comp/env/jdbc/yourDatasourceName"/>
</dataSource>
</environment>
There are a number of ways you can do this. I am assuming that you mean to implement Tomcat (as opposed to Spring) managed connection pooling. I have tested on MySQl/Spring 4/Mybatis-spring 1.1 stack.
Figure out the connection pooling mechanism you want to implement (C3P0/DBCP etc).
If you want to use your database as a JNDI data source, then you have to declare it as a resource in Tomcat settings. There are a number of ways to do this. You can follow this well written guide Tomcat JNDI connection pooling. I generally add the following entry in context.xml located in the META-INF directory of my app:
<Context>
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxTotal="100" maxIdle="30" maxWaitMillis="10000" username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/javatest"/>
</Context>
This file is used by Tomcat to invoke its own default connection pool, which in this case would be DBCP 2. You can tweak its settings by adding more parameters (like removeAbandonedOnBorrow=true) in the Resource element above. You should put the driver jar (eg MySQL Connector Jar) of your database in Tomcat's lib folder.
After this, depending on your Mybatis implementation (XML or bean based), you can inject this data source into Mybatis. If you are doing it the bean way, you can do it like this:
<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/TestDB"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dbDataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dbDataSource" />
<property name="typeAliasesPackage" value="com.example.model"/>
<property name="mapperLocations" value="classpath*:*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.dao" />
</bean>
MapperScannerConfigurer is necessary because it searches the directories given to it to find mapper xml files. You can also use the old way of defining a mybatis-config.xml file by injecting the file's location in the sqlSessionFactory bean in its configLocation property.

WildFly LDAP Connection JNDI

We are in the process of moving from Glassfish to WildFly. In Glassfish we setup our LDAP server and use it for authentication and we also reference it with JNDI for us within the application for things such as searching users, etc. I am wondering if there is a way to setup the LDAP connection in the standalone.xml file in WildFly for reference via JNDI in the application like we currently do. I have setup LDAP for authentication and that works but I do not know how to reference that connection for use in our application.
In Wildfly you can use Naming Subsystem for binding a Ldap context, in particular External Context Federation binding type:
External Context Federation
Federation of external JNDI contexts, such as a LDAP context, are
achieved by adding External Context bindings to the global bindings
configuration, through the external-context XML element
For example:
<subsystem xmlns="urn:jboss:domain:naming:2.0">
<bindings>
<external-context name="java:global/federation/ldap/example" class="javax.naming.directory.InitialDirContext" module="org.jboss.as.naming" cache="true">
<environment>
<property name="java.naming.factory.initial" value="com.sun.jndi.ldap.LdapCtxFactory" />
<property name="java.naming.provider.url" value="ldap://ldap.example.com:389" />
<property name="java.naming.security.authentication" value="simple" />
<property name="java.naming.security.principal" value="uid=admin,ou=system" />
<property name="java.naming.security.credentials" value="secret" />
</environment>
</external-context>
</bindings>
<remote-naming/>
</subsystem>
Ref: WildFly 8
- Naming Subsystem Configuration
I hope this help.

StackOverflowError coming when cxf security authentication webservice deployed in jboss 5.0

deployed webservice in jboss 5 with ws authentication related stuff mentioned in
beans.xml file.
same service worked well in tomcat 6 and when i deploy same service getting
java.lang.StackOverflowError
in jboss 5.in server.log apart form this error i didn't find any error.
when i comment from to then its working
fine.
I suspect jars conflict with cxf jars and jboss jars.
<jaxws:endpoint id="ping" implementor="cxfdemo.ws10.impl.PingServiceImpl"
address="/PingService">
<jaxws:inInterceptors>
<bean id="xxx" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
<property name="properties">
<map>
<entry key="action" value="UsernameToken"/>
<entry key="passwordType" value="PasswordText" />
</map>
</property>
</bean>
</jaxws:inInterceptors>
</jaxws:endpoint>
2013-01-22 03:20:08,667 INFO QuartzScheduler_BpmClusteredScheduler-that missed their
scheduled fire-time.
2013-01-22 03:20:32,559 ERROR http-0.0.0.0-38080-3 [org.apache.catalina.core.ContainerBase
.[jboss.web].[localhost].[/PingWS].[cxf]] Servlet.service() for servlet cxf threw exception
java.lang.StackOverflowError
at java.security.AccessController.doPrivileged(Native Method)
at java.io.PrintWriter.<init>(PrintWriter.java:78)
at java.io.PrintWriter.<init>(PrintWriter.java:62)
</code>
Yes, the conflict is with the jars that come with JBoss as part of its webservices library. I tried the following and it worked:
Remove jbossws.deployer from /deployers
Remove jbossws.sar from /deploy

Categories

Resources