Monthly Archive: February 2014

Database authentication on JBoss EAP 5.0

Step 1 Create tables in the database and insert user, group and role information

 

CREATE TABLE USERS (
U_NAME VARCHAR(200) NOT NULL,
U_PASSWORD VARCHAR(50) NOT NULL,
U_DESCRIPTION VARCHAR(1000))
;

CREATE TABLE GROUPS (
G_NAME VARCHAR(200) NOT NULL,
G_DESCRIPTION VARCHAR(1000) NULL)
;

CREATE TABLE ROLES (
U_NAME VARCHAR(200) NOT NULL,
R_NAME VARCHAR(200) NOT NULL,
G_NAME VARCHAR(1000) NULL)
;

Insert into USERS values('faisal','faisal');
Insert into GROUPS values('Admin','Admin');
Insert into ROLES values('faisal','Administrators','Admin');

Step 2 Create a datasource pointing to that database. I am using postgres in this example.

<datasources>
  <local-tx-datasource>
    <jndi-name>jdbc/postgressds</jndi-name>
    <connection-url>jdbc:postgresql://localhost:5432/postgres</connection-url>
    <driver-class>org.postgresql.Driver</driver-class>
    <user-name>postgres</user-name>
    <password>postgres</password>

        <!-- sql to call when connection is created
        <new-connection-sql>some arbitrary sql</new-connection-sql>
        -->

        <!-- sql to call on an existing pooled connection when it is obtained from pool 
        <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
        -->

      <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->

  </local-tx-datasource>

</datasources>

 

Step 3 Add the authentication policy in the login-config.xml file

	<application-policy name="databaseauth">
    <authentication>
      <login-module code="org.jboss.security.auth.spi.DatabaseServerLoginModule" flag="required">
			<module-option name="dsJndiName">java:jdbc/postgressds</module-option>
			<module-option name="principalsQuery">select U_PASSWORD from USERS where U_NAME=?</module-option>
			<module-option name="rolesQuery">select R_NAME,'Roles' from ROLES where U_NAME=?</module-option>
      </login-module>
    </authentication>
 </application-policy>

Step 4) Access protected page on the application and log in with faisal/faisal.
Note: The role should be Administrators.

Let us know if you face any issues. We’ll be happy to help.

Cheers!
Wonders Team

Topic Send and Topic Receive demo on JBoss Application Server

1 )Create a topic

Add the following in the \deploy\messaging\destinations-service.xml file

 

    <mbean code="org.jboss.jms.server.destination.TopicService"
      name="jboss.messaging.destination:service=Topic,name=Topic1"
      xmbean-dd="xmdesc/Topic-xmbean.xml">
	<depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
	<depends>jboss.messaging:service=PostOffice</depends>
   </mbean>

2) Copy the TopicSend and TopicReceive programs to a folder.

TopicSend

import java.io.*;
import java.util.*;
import javax.transaction.*;
import javax.naming.*;
import javax.jms.*;
import javax.rmi.PortableRemoteObject;

public class TopicSend
{
// Defines the JNDI context factory.
public final static String JNDI_FACTORY=”org.jnp.interfaces.NamingContextFactory”;
// Defines the JMS connection factory.
public final static String JMS_FACTORY=”ConnectionFactory”;
// Defines the topic.
public final static String TOPIC=”/topic/Topic1″;

protected TopicConnectionFactory tconFactory;
protected TopicConnection tcon;
protected TopicSession tsession;
protected TopicPublisher tpublisher;
protected Topic topic;
protected TextMessage msg;

public void init(Context ctx, String topicName)
throws NamingException, JMSException
{
tconFactory = (TopicConnectionFactory)
PortableRemoteObject.narrow(ctx.lookup(JMS_FACTORY),
TopicConnectionFactory.class);
tcon = tconFactory.createTopicConnection();
tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = (Topic) PortableRemoteObject.narrow(ctx.lookup(topicName), Topic.class);
tpublisher = tsession.createPublisher(topic);
msg = tsession.createTextMessage();
tcon.start();
}

public void send(String message) throws JMSException {
msg.setText(message);
tpublisher.publish(msg);
}

public void close() throws JMSException {
tpublisher.close();
tsession.close();
tcon.close();
}

public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println(“Usage: java TopicSend jboss_url”);
return;
}
InitialContext ic = getInitialContext(args[0]);
TopicSend ts = new TopicSend();
ts.init(ic, TOPIC);
readAndSend(ts);
ts.close();
}

protected static void readAndSend(TopicSend ts)
throws IOException, JMSException
{
BufferedReader msgStream = new BufferedReader (new InputStreamReader(System.in));
String line=null;
do {
System.out.print(“Enter message (\”quit\” to quit): \n”);
line = msgStream.readLine();
if (line != null && line.trim().length() != 0) {
ts.send(line);
System.out.println(“JMS Message Sent: “+line+”\n”);
}
} while (line != null && ! line.equalsIgnoreCase(“quit”));
}

protected static InitialContext getInitialContext(String url)
throws NamingException
{
Hashtable<String,String> env = new Hashtable<String,String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);

return new InitialContext(env);
}

}

TopicReceive

import java.io.*;
import java.util.*;
import javax.transaction.*;
import javax.naming.*;
import javax.jms.*;
import javax.rmi.PortableRemoteObject;

public class TopicReceive implements MessageListener {

// Defines the JNDI context factory.
public final static String JNDI_FACTORY=”org.jnp.interfaces.NamingContextFactory”;
// Defines the JMS connection factory for the topic.
public final static String JMS_FACTORY=”ConnectionFactory”;
// Defines the topic.
public final static String TOPIC=”/topic/Topic1″;

private TopicConnectionFactory tconFactory;
private TopicConnection tcon;
private TopicSession tsession;
private TopicSubscriber tsubscriber;
private Topic topic;
private boolean quit = false;

/**
* Message listener interface.
* @param msg message
*/
public void onMessage(Message msg) {
try {
String msgText;

if (msg instanceof TextMessage) {
msgText = ((TextMessage)msg).getText();
} else {
msgText = msg.toString();
}

System.out.println(“JMS Message Received: “+ msgText );

if (msgText.equalsIgnoreCase(“quit”)) {
synchronized(this) {
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
} catch (JMSException jmse) {
System.err.println(“An exception occurred: “+jmse.getMessage());
}
}

public void init(Context ctx, String topicName)
throws NamingException, JMSException
{
tconFactory = (TopicConnectionFactory)
PortableRemoteObject.narrow(ctx.lookup(JMS_FACTORY),
TopicConnectionFactory.class);
tcon = tconFactory.createTopicConnection();
tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = (Topic)
PortableRemoteObject.narrow(ctx.lookup(topicName),
Topic.class);
tsubscriber = tsession.createSubscriber(topic);
tsubscriber.setMessageListener(this);
tcon.start();
}

/**
* Closes JMS objects.
*
* @exception JMSException if JMS fails to close objects due to internal error
*/
public void close() throws JMSException {
tsubscriber.close();
tsession.close();
tcon.close();
}

public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println(“Usage: java TopicReceive jboss_url”);
return;
}
InitialContext ic = getInitialContext(args[0]);
TopicReceive tr = new TopicReceive();
tr.init(ic, TOPIC);

System.out.println(“JMS Ready To Receive Messages (To quit, send a \”quit\” message).”);

// Wait until a “quit” message has been received.
synchronized(tr) {
while (! tr.quit) {
try {
tr.wait();
} catch (InterruptedException ie) {}
}
}
tr.close();
}

private static InitialContext getInitialContext(String url)
throws NamingException
{
Hashtable<String,String> env = new Hashtable<String,String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);

return new InitialContext(env);
}

}

 

3) Start your Jboss Server

D:\JBossEnterprisePlatform-5.1.1_New\jboss-eap-5.1\jboss-as\bin>run -c server1 -b 0.0.0.0

4) Open two command prompts, compile and run the two programs as follows.

C:\JBoss\LABS>set classpath=D:\JBossEnterprisePlatform-5.1.1_New\jboss-eap-5.1\jboss-as\client\*;.;
C:\JBoss\LABS>javac TopicReceive.java
C:\JBoss\LABS>java TopicReceive jnp://localhost:1099

C:\JBoss\LABS>set classpath=D:\JBossEnterprisePlatform-5.1.1_New\jboss-eap-5.1\jboss-as\client\*;.;
C:\JBoss\LABS>javac TopicSend.java
C:\JBoss\LABS>java TopicSend jnp://localhost:1099

You can have multiple receivers.

Please let us know if you face any issues.

Cheers!
Wonders Team