Sunday, November 28, 2010

Securing Resources Using Roles and Policies for Oracle WebLogic Server

Securing Resources Using Roles and Policies for Oracle WebLogic Server
http://download.oracle.com/docs/cd/E14571_01/web.1111/e13747/toc.htm

Securing Enterprise JavaBeans (EJBs)

http://download.oracle.com/docs/cd/E14571_01/web.1111/e13711/ejb_client.htm#SCPRG286


externally-defined

The externally-defined element lets you explicitly indicate that you want the security roles defined by the role-name element in the weblogic-ejb-jar.xml deployment descriptors to use the mappings specified in the Administration Console. The element gives you the flexibility of not having to specify a specific security role mapping for each security role defined in the deployment descriptors for a particular Web application. Therefore, within the same security realm, deployment descriptors can be used to specify and modify security for some applications while the Administration Console can be used to specify and modify security for others.

Defined in descriptor
ejb-jar.xml entries:
...
<assembly-descriptor>
<security-role>
<role-name>manger</role-name>
</security-role>
<security-role>
<role-name>east</role-name>
</security-role>
<method-permission>
<role-name>manager</role-name>
<role-name>east</role-name>
<method>
<ejb-name>accountsPayable</ejb-name>
<method-name>getReceipts</method-name>
</method>
</method-permission>
...
</assembly-descriptor>
...
weblogic-ejb-jar.xml entries:
<security-role-assignment>
<role-name>manager</role-name>
<principal-name>joe</principal-name>
<principal-name>Bill</principal-name>
<principal-name>Mary</principal-name>

...
</security-role-assignment>



Externally defined in Admin Console
ejb-jar.xml entries:
...
<assembly-descriptor>
<security-role>
<role-name>manger</role-name>
</security-role>
<security-role>
<role-name>east</role-name>
</security-role>
<method-permission>
<role-name>manager</role-name>
<role-name>east</role-name>
<method>
<ejb-name>accountsPayable</ejb-name>
<method-name>getReceipts</method-name>
</method>
</method-permission>
...
</assembly-descriptor>
...
weblogic-ejb-jar.xml entries:
<security-role-assignment>
<role-name>manager</role-name>
<externally-defined/>

...
</security-role-assignment>




Using run-as-role in case anonymous access is forbidden
In the ejb-jar.xml file:
// Beans "A_EJB_with_runAs_role_X" and "B_EJB_with_runAs_role_X"
// specify a security-identity run-as role-name "runAs_role_X".
// Bean "C_EJB_with_runAs_role_Y" specifies a security-identity
// run-as role-name "runAs_role_Y".
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>SecurityEJB</ejb-name>
<home>weblogic.ejb20.SecuritySLHome</home>
<remote>weblogic.ejb20.SecuritySL</remote>
<local-home>
weblogic.ejb20.SecurityLocalSLHome
</local-home>
<local>weblogic.ejb20.SecurityLocalSL</local>
<ejb-class>weblogic.ejb20.SecuritySLBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
<message-driven>
<ejb-name>SecurityEJB</ejb-name>
<ejb-class>weblogic.ejb20.SecuritySLBean</ejb-class>
<transaction-type>Container</transaction-type>
<security-identity>
<run-as>
<role-name>runAs_role_X</role-name>
</run-as>
</security-identity>
<security-identity>
<run-as>
<role-name>runAs_role_Y</role-name>
</run-as>
</security-identity>
</message-driven>
</enterprise-beans>
</ejb-jar>

weblogic-ejb-jar file:

<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>A_EJB_with_runAs_role_X</ejb-name>
</weblogic-enterprise-bean>
<weblogic-enterprise-bean>
<ejb-name>B_EJB_with_runAs_role_X</ejb-name>
<run-as-principal-name>Joe</run-as-principal-name>
</weblogic-enterprise-bean>
<weblogic-enterprise-bean>
<ejb-name>C_EJB_with_runAs_role_Y</ejb-name>
</weblogic-enterprise-bean>
<security-role-assignment>
<role-name>runAs_role_Y</role-name>
<principal-name>Harry</principal-name>
<principal-name>John</principal-name>
</security-role-assignment>
<run-as-role-assignment>
<role-name>runAs_role_X</role-name>
<run-as-principal-name>Fred</run-as-principal-name>
</run-as-role-assignment>
</weblogic-ejb-jar>




If you see this error in logs/DefaultAuditRecorder.log when anonymous access to WLS JNDI tree is disabled

#### Audit Record Begin Nov 30, 2010 1:42:14 PM Severity =FAILURE Event Type = Authorization Audit Event V2 Subject: 0
ONCEjndi type= jndi , application=, path={weblogic}, action=lookup Audit Record End ####


you can disable internal WLS application
Async web service support is not fully configured. The async response web service /AsyncResponseServiceSoap12Https for this server was not fully deployed because the JMS reliability queue was not defined/deployed: weblogic.wsee.DefaultQueue. The server will periodically retry completing the deploy for the service. This message can usually be ignored unless there are async web service applications. To completely disable async web service support, thus avoiding this message, set -Dweblogic.wsee.skip.async.response=true

Internal WLS applications deployed on each server
./servers/ServerA1/tmp/_WL_internal/bea_wls_deployment_internal/voxsyv/war/WEB-INF/web.xml
./servers/ServerA1/tmp/_WL_internal/bea_wls9_async_response/39a4jn/war/WEB-INF/webservices.xml
./servers/ServerA1/tmp/_WL_internal/bea_wls9_async_response/39a4jn/war/WEB-INF/weblogic-webservices-policy.xml
./servers/ServerA1/tmp/_WL_internal/bea_wls9_async_response/39a4jn/war/WEB-INF/AsyncResponseService.xml
./servers/ServerA1/tmp/_WL_internal/bea_wls9_async_response/39a4jn/war/WEB-INF/web.xml
./servers/ServerA1/tmp/_WL_internal/bea_wls9_async_response/39a4jn/war/WEB-INF/AsyncResponseService-annotation.xml
./servers/ServerA1/tmp/_WL_internal/bea_wls9_async_response/39a4jn/war/WEB-INF/AsyncResponseServiceSoap12.xml
./servers/ServerA1/tmp/_WL_internal/bea_wls9_async_response/39a4jn/war/WEB-INF/weblogic.xml
./servers/ServerA1/tmp/_WL_internal/bea_wls9_async_response/39a4jn/war/WEB-INF/AsyncResponseServiceSoap12-annotation.xml
./servers/ServerA1/tmp/_WL_internal/bea_wls9_async_response/39a4jn/war/WEB-INF/weblogic-webservices.xml
./servers/ServerA1/tmp/_WL_internal/bea_wls_internal/hta2i5/war/WEB-INF/web.xml
./servers/ServerA1/tmp/_WL_internal/bea_wls_internal/hta2i5/war/WEB-INF/weblogic.xml
./servers/ServerA1/tmp/_WL_internal/bea_wls_cluster_internal/px8ma5/war/WEB-INF/web.xml
./servers/ServerA1/tmp/_WL_internal/bea_wls_cluster_internal/px8ma5/war/WEB-INF/weblogic.xml

Saturday, November 27, 2010

Weblogic server diagnostic image

Take diagnostic image


 unzip -l diagnostic_image_AdminServer_2010_11_27_12_44_12.zip 
Archive: diagnostic_image_AdminServer_2010_11_27_12_44_12.zip
Length Date Time Name
--------- ---------- ----- ----
4440 11-27-2010 12:44 JTA.img
161 11-27-2010 12:44 WatchSource.img
6298 11-27-2010 12:44 configuration.img
3369 11-27-2010 12:44 WORK_MANAGER.img
11619 11-27-2010 12:44 JNDI_IMAGE_SOURCE.img
215601 11-27-2010 12:44 APPLICATION.img
181 11-27-2010 12:44 InstrumentationImageSource.img
586 11-27-2010 12:44 SAF.img
368637 11-27-2010 12:44 Logging.img
12372 11-27-2010 12:44 PERSISTENT_STORE.img
27011 11-27-2010 12:44 JDBC.img
66 11-27-2010 12:44 PathService.img
429 11-27-2010 12:44 JMS.img
6154 11-27-2010 12:44 Deployment.img
34792 11-27-2010 12:44 JVM.img
223 11-27-2010 12:44 CONNECTOR.img
29455 11-27-2010 12:44 image.summary
--------- -------
721394 17 files



JDBC.img
more JDBC.img 
Dumping Resource Pool:XE
Resource Pool:XE:dumpPool Current Capacity = 15
Resource Pool:XE:dumpPool dumping available resources, #entries = 0
Resource Pool:XE:dumpPool dumping reserved resources, #entries = 15
Resource Pool:XE:dumpPool reserved[0] = autoCommit=true,enabled=true,isXA=true,isJTS=false,vendorID=0,connUsed=true,doInit=false,'null',destroyed=false,poolname=XE,appname=null,moduleName=null,connectTi
me=126,dirtyIsolationLevel=false,initialIsolationLevel=2,infected=false,lastSuccessfulConnectionUse=1290858199133,secondsToTrustAnIdlePoolConnection=10,currentUser=java.lang.Exception
at weblogic.jdbc.common.internal.ConnectionEnv.setup(ConnectionEnv.java:318)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:344)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:322)
at weblogic.jdbc.common.internal.ConnectionPool.reserve(ConnectionPool.java:438)
at weblogic.jdbc.common.internal.ConnectionPool.reserve(ConnectionPool.java:317)
at weblogic.jdbc.common.internal.ConnectionPoolManager.reserve(ConnectionPoolManager.java:93)
at weblogic.jdbc.common.internal.ConnectionPoolManager.reserve(ConnectionPoolManager.java:61)
at weblogic.jdbc.jta.DataSource.getXAConnectionFromPool(DataSource.java:1670)
at weblogic.jdbc.jta.DataSource.refreshXAConnAndEnlist(DataSource.java:1468)
at weblogic.jdbc.wrapper.JTAConnection.getXAConn(JTAConnection.java:199)
at weblogic.jdbc.wrapper.JTAConnection.checkConnection(JTAConnection.java:68)
at weblogic.jdbc.wrapper.Connection.preInvocationHandler(Connection.java:93)
at weblogic.jdbc.wrapper.Connection.prepareStatement(Connection.java:480)
at dave.DbExecutor.run(DbExecutor.java:24)
at java.lang.Thread.run(Thread.java:619)
,currentThread=Thread[Thread-131,5,Pooled Threads],lastUser=null,currentError=null,currentErrorTimestamp=null,JDBC4Runtime=true,supportStatementPoolable=true,needRestoreClientInfo=false,defaultClientInf
o={},supportIsValid=true
Resource Pool:XE:dumpPool reserved[1] = autoCommit=true,enabled=true,isXA=true,isJTS=false,vendorID=0,connUsed=true,doInit=false,'null',destroyed=false,poolname=XE,appname=null,moduleName=null,connectTi
me=219,dirtyIsolationLevel=false,initialIsolationLevel=2,infected=false,lastSuccessfulConnectionUse=1290858199286,secondsToTrustAnIdlePoolConnection=10,currentUser=java.lang.Exception
at weblogic.jdbc.common.internal.ConnectionEnv.setup(ConnectionEnv.java:318)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:344)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:322)
at weblogic.jdbc.common.internal.ConnectionPool.reserve(ConnectionPool.java:438)
at weblogic.jdbc.common.internal.ConnectionPool.reserve(ConnectionPool.java:317)
at weblogic.jdbc.common.internal.ConnectionPoolManager.reserve(ConnectionPoolManager.java:93)
at weblogic.jdbc.common.internal.ConnectionPoolManager.reserve(ConnectionPoolManager.java:61)
at weblogic.jdbc.jta.DataSource.getXAConnectionFromPool(DataSource.java:1670)
at weblogic.jdbc.jta.DataSource.refreshXAConnAndEnlist(DataSource.java:1468)
at weblogic.jdbc.wrapper.JTAConnection.getXAConn(JTAConnection.java:199)
at weblogic.jdbc.wrapper.JTAConnection.checkConnection(JTAConnection.java:68)
at weblogic.jdbc.wrapper.Connection.preInvocationHandler(Connection.java:93)
at weblogic.jdbc.wrapper.Connection.prepareStatement(Connection.java:480)
at dave.DbExecutor.run(DbExecutor.java:24)
at java.lang.Thread.run(Thread.java:619)
,currentThread=Thread[Thread-128,5,Pooled Threads],lastUser=null,currentError=null,currentErrorTimestamp=null,JDBC4Runtime=true,supportStatementPoolable=true,needRestoreClientInfo=false,defaultClientInf
o={},supportIsValid=true
Resource Pool:XE:dumpPool reserved[2] = autoCommit=true,enabled=true,isXA=true,isJTS=false,vendorID=0,connUsed=true,doInit=false,'null',destroyed=false,poolname=XE,appname=null,moduleName=null,connectTi
me=232,dirtyIsolationLevel=false,initialIsolationLevel=2,infected=false,lastSuccessfulConnectionUse=1290858199279,secondsToTrustAnIdlePoolConnection=10,currentUser=java.lang.Exception
at weblogic.jdbc.common.internal.ConnectionEnv.setup(ConnectionEnv.java:318)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:344)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:322)
at weblogic.jdbc.common.internal.ConnectionPool.reserve(ConnectionPool.java:438)
at weblogic.jdbc.common.internal.ConnectionPool.reserve(ConnectionPool.java:317)
at weblogic.jdbc.common.internal.ConnectionPoolManager.reserve(ConnectionPoolManager.java:93)
at weblogic.jdbc.common.internal.ConnectionPoolManager.reserve(ConnectionPoolManager.java:61)
at weblogic.jdbc.jta.DataSource.getXAConnectionFromPool(DataSource.java:1670)
at weblogic.jdbc.jta.DataSource.refreshXAConnAndEnlist(DataSource.java:1468)
at weblogic.jdbc.wrapper.JTAConnection.getXAConn(JTAConnection.java:199)
at weblogic.jdbc.wrapper.JTAConnection.checkConnection(JTAConnection.java:68)
at weblogic.jdbc.wrapper.Connection.preInvocationHandler(Connection.java:93)
at weblogic.jdbc.wrapper.Connection.prepareStatement(Connection.java:480)
at dave.DbExecutor.run(DbExecutor.java:24)
at java.lang.Thread.run(Thread.java:619)
,currentThread=Thread[Thread-126,5,Pooled Threads],lastUser=null,currentError=null,currentErrorTimestamp=null,JDBC4Runtime=true,supportStatementPoolable=true,needRestoreClientInfo=false,defaultClientInf
o={},supportIsValid=true
Resource Pool:XE:dumpPool reserved[3] = autoCommit=true,enabled=true,isXA=true,isJTS=false,vendorID=0,connUsed=true,doInit=false,'null',destroyed=false,poolname=XE,appname=null,moduleName=null,connectTi
me=206,dirtyIsolationLevel=false,initialIsolationLevel=2,infected=false,lastSuccessfulConnectionUse=1290858199262,secondsToTrustAnIdlePoolConnection=10,currentUser=java.lang.Exception
at weblogic.jdbc.common.internal.ConnectionEnv.setup(ConnectionEnv.java:318)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:344)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:322)
at weblogic.jdbc.common.internal.ConnectionPool.reserve(ConnectionPool.java:438)
at weblogic.jdbc.common.internal.ConnectionPool.reserve(ConnectionPool.java:317)
at weblogic.jdbc.common.internal.ConnectionPoolManager.reserve(ConnectionPoolManager.java:93)
at weblogic.jdbc.common.internal.ConnectionPoolManager.reserve(ConnectionPoolManager.java:61)
at weblogic.jdbc.jta.DataSource.getXAConnectionFromPool(DataSource.java:1670)
at weblogic.jdbc.jta.DataSource.refreshXAConnAndEnlist(DataSource.java:1468)
at weblogic.jdbc.wrapper.JTAConnection.getXAConn(JTAConnection.java:199)
at weblogic.jdbc.wrapper.JTAConnection.checkConnection(JTAConnection.java:68)
at weblogic.jdbc.wrapper.Connection.preInvocationHandler(Connection.java:93)
at weblogic.jdbc.wrapper.Connection.prepareStatement(Connection.java:480)
at dave.DbExecutor.run(DbExecutor.java:24)
at java.lang.Thread.run(Thread.java:619)

Connection pool overload on Weblogic server - forcibly release inactive connection

The number of inactive seconds on a reserved connection before WebLogic Server reclaims the connection and releases it back into the connection pool.

You can use the Inactive Connection Timeout feature to reclaim leaked connections - connections that were not explicitly closed by the application. Note that this feature is not intended to be used in place of properly closing connections.

When set to 0, the feature is disabled.

MBean Attribute (Does not apply to application modules) :
JDBCConnectionPoolParamsBean.InactiveConnectionTimeoutSeconds



Test classes which overload connection pool by holding connections
package dave;

import java.sql.Connection;
import java.sql.SQLException;

import javax.ejb.Stateless;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
* Session Bean implementation class TestSesssionEJB
*/
@Stateless(mappedName = "TestSesssionEJB")

public class TestSesssionEJB implements TestSesssionEJBRemote, TestSesssionEJBLocal {

/**
* Default constructor.
*/
public TestSesssionEJB() {


}

//@Resource (name="jdbc/XE")
//javax.sql.DataSource ds;

public String hello(String name){

System.out.println("Hello" + name);

InitialContext ic;
DataSource dsLookup = null;
try {
ic = new InitialContext();

dsLookup = (DataSource) ic
.lookup("jdbc/XE");
} catch (NamingException e) {
e.printStackTrace();
}


for(int i=0; i<15;i++){
Connection conn = null;
try {
conn = dsLookup.getConnection();
DbExecutor de = new DbExecutor(conn);
new Thread(de).start();
} catch (SQLException e) {
e.printStackTrace();
}

}

System.out.println("Main: started all");

return name;

}



}



package dave;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DbExecutor implements Runnable {

Connection conn;

public DbExecutor(Connection conn) {
this.conn = conn;
}

@Override
public void run() {
try {
Thread thread = Thread.currentThread();

System.out.println("Start thread.id=" + thread.getId());

PreparedStatement stmt = conn
.prepareStatement("select * from dave");



System.out.println("Thread id=" + thread.getId() +
" Query dave db using container injected ds thread.id=");

stmt.execute();

ResultSet rs = stmt.getResultSet();

try {
System.out.println("Thread id=" + thread.getId() +
" gor result, going to sleep for 60 s");
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}

while (rs.next()) {
System.out.println("Thread id=" +"dave=" + rs.getString("DSTR") + " id="
+ rs.getInt("ID"));
}

} catch (SQLException e) {
e.printStackTrace();
}

}





}


Overloaded JDBC connection pool statistics in Admin console



Admin server log for connection forcibly taken back by Weblogic
<Nov 27, 2010 12:22:25 PM CET> <Warning> <Common> <BEA-000620> <Forcibly releasing inactive resource "autoCommit=true,enabled=true,isXA=true,isJTS=false,vendorID=0,connUsed=false,doInit=false,'null',destroyed=false,poolname=XE,appname=null,moduleName=null,connectTime=100,dirtyIsolationLevel=false,initialIsolationLevel=2,infected=false,lastSuccessfulConnectionUse=1290856766085,secondsToTrustAnIdlePoolConnection=10,currentUser=java.lang.Exception
at weblogic.jdbc.common.internal.ConnectionEnv.setup(ConnectionEnv.java:318)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:344)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:322)
at weblogic.jdbc.common.internal.ConnectionPool.reserve(ConnectionPool.java:438)
at weblogic.jdbc.common.internal.ConnectionPool.reserve(ConnectionPool.java:317)
at weblogic.jdbc.common.internal.ConnectionPoolManager.reserve(ConnectionPoolManager.java:93)
at weblogic.jdbc.common.internal.ConnectionPoolManager.reserve(ConnectionPoolManager.java:61)
at weblogic.jdbc.jta.DataSource.getXAConnectionFromPool(DataSource.java:1670)
at weblogic.jdbc.jta.DataSource.refreshXAConnAndEnlist(DataSource.java:1468)
at weblogic.jdbc.wrapper.JTAConnection.getXAConn(JTAConnection.java:199)
at weblogic.jdbc.wrapper.JTAConnection.checkConnection(JTAConnection.java:68)
at weblogic.jdbc.wrapper.Connection.preInvocationHandler(Connection.java:93)
at weblogic.jdbc.wrapper.Connection.prepareStatement(Connection.java:480)
at dave.DbExecutor.run(DbExecutor.java:24)
at java.lang.Thread.run(Thread.java:619)
,currentThread=Thread[Thread-70,5,Pooled Threads],lastUser=null,currentError=null,currentErrorTimestamp=null,JDBC4Runtime=true,supportStatementPoolable=true,needRestoreClientInfo=false,defaultClientInfo={},supportIsValid=true" back into the pool "XE".>
<Nov 27, 2010 12:22:25 PM CET> <Warning> <Common> <BEA-000620> <Forcibly releasing inactive resource "autoCommit=true,enabled=true,isXA=true,isJTS=false,vendorID=0,connUsed=false,doInit=false,'null',destroyed=false,poolname=XE,appname=null,moduleName=null,connectTime=337,dirtyIsolationLevel=false,initialIsolationLevel=2,infected=false,lastSuccessfulConnectionUse=1290856766082,secondsToTrustAnIdlePoolConnection=10,currentUser=java.lang.Exception
at weblogic.jdbc.common.internal.ConnectionEnv.setup(ConnectionEnv.java:318)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:344)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:322)
at weblogic.jdbc.common.internal.ConnectionPool.reserve(ConnectionPool.java:438)
at weblogic.jdbc.common.internal.ConnectionPool.reserve(ConnectionPool.java:317)
at weblogic.jdbc.common.internal.ConnectionPoolManager.reserve(ConnectionPoolManager.java:93)
at weblogic.jdbc.common.internal.ConnectionPoolManager.reserve(ConnectionPoolManager.java:61)
at weblogic.jdbc.jta.DataSource.getXAConnectionFromPool(DataSource.java:1670)
at weblogic.jdbc.jta.DataSource.refreshXAConnAndEnlist(DataSource.java:1468)
at weblogic.jdbc.wrapper.JTAConnection.getXAConn(JTAConnection.java:199)
at weblogic.jdbc.wrapper.JTAConnection.checkConnection(JTAConnection.java:68)
at weblogic.jdbc.wrapper.Connection.preInvocationHandler(Connection.java:93)
at weblogic.jdbc.wrapper.Connection.prepareStatement(Connection.java:480)
at dave.DbExecutor.run(DbExecutor.java:24)
at java.lang.Thread.run(Thread.java:619)
,currentThread=Thread[Thread-116,5,Pooled Threads],lastUser=java.lang.Throwable,currentError=null,currentErrorTimestamp=null,JDBC4Runtime=true,supportStatementPoolable=true,needRestoreClientInfo=false,defaultClientInfo={},supportIsValid=true" back into the pool "XE".>



Connection Pool Diagnostics

Inactive Connection Timeout

Create Java EE project using Eclipse Helios 3.6. and Weblogic 10.3.3

Add Weblogic 10.3.3 runtime in Servers tab

Create EAR project


Create EJB project

Create session EJB





Add Session EJB method implementation
package dave;

import javax.ejb.Stateless;

/**
* Session Bean implementation class TestSesssionEJB
*/
@Stateless(mappedName = "TestSesssionEJB")
public class TestSesssionEJB implements TestSesssionEJBRemote, TestSesssionEJBLocal {

/**
* Default constructor.
*/
public TestSesssionEJB() {


}

public String hello(String name){

System.out.println("Hello" + name);

return name;

}

}





Add EJB project into EAR


Run on Server - deployment




Create wlfullclient.jar by running in $WLS_HOME/server/lib
java -jar wljarbuilder.jar

Add wlfullclient.jar and testEJB project to client classpath


Create client class


Get EJB name from JNDI tree


Create client implementation
package dave;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TestClient {

private static String INITIAL_CONTEXT_NAME = "weblogic.jndi.WLInitialContextFactory";
private static String PROVIDER_URL = "t3://localhost:7001";
private static String JNDI_NAME = "TestSesssionEJB#dave.TestSesssionEJBRemote";

public static void main(String[] args) throws Exception {

Context ctx = getInitialContext();

TestSesssionEJBRemote testEJB = (TestSesssionEJBRemote) ctx
.lookup(JNDI_NAME);

System.out.println("Say hello: " + testEJB.hello("dave"));

}

private static Context getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_NAME);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
return new InitialContext(env);
}

}




Check server log
<Nov 27, 2010 8:44:32 AM CET> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to RUNNING> 
<Nov 27, 2010 8:44:32 AM CET> <Notice> <WebLogicServer> <BEA-000360> <Server started in RUNNING mode>
Nov 27, 2010 8:44:42 AM com.sun.faces.config.ConfigureListener contextInitialized
INFO: Initializing Sun's JavaServer Faces implementation (1.2_03-b04-FCS) for context '/console'
Nov 27, 2010 8:44:42 AM com.sun.faces.config.ConfigureListener contextInitialized
INFO: Completed initializing Sun's JavaServer Faces implementation (1.2_03-b04-FCS) for context '/console'

Hellodave

Thursday, November 18, 2010

Set JVM default property with _JAVA_OPTIONS

export _JAVA_OPTIONS=-Djava.awt.headless=true

java |more
Picked up _JAVA_OPTIONS: -Djava.awt.headless=true


Setting JVM -Djava.awt.headless=true is necessary if X server is not available.
Without this property following error is thrown:

Exception in thread "main" java.lang.InternalError: Can't connect to X11 window server using '' as the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
at sun.awt.X11GraphicsEnvironment.access$100(X11GraphicsEnvironment.java:52)
at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:155)
at java.security.AccessController.doPrivileged(Native Method)
at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:131)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:68)
at sun.font.FontManager.initSGEnv(FontManager.java:1307)
at sun.font.FontManager.findFont2D(FontManager.java:1984)
at java.awt.Font.getFont2D(Font.java:455)
at java.awt.Font.canDisplay(Font.java:1904)
at java.awt.Font.canDisplayUpTo(Font.java:1970)
at java.awt.font.TextLayout.singleFont(TextLayout.java:451)
at java.awt.font.TextLayout.<init>(TextLayout.java:509)
at org.apache.poi.hssf.usermodel.HSSFSheet.autoSizeColumn(HSSFSheet.java:1675)

Thursday, November 4, 2010

Parse connection string using Java RegEx

http://download.oracle.com/javase/tutorial/essential/regex/index.html

http://download.oracle.com/javase/6/docs/api/java/util/regex/package-summary.html

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestRegEx {

/**
* @param args
*/
public static void main(String[] args) {

String input = "dbuser:passwd@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(Host=dbhost)(PORT=1520)))(CONNECT_DATA=(SID=MY_DB)))";

getParam(input, "Host");
getParam(input, "HOST");
// getParam(input,"xHOST");

Pattern pattern = Pattern.compile("(\\w+)@(\\w+)/(\\S+)");
Matcher matcher = pattern.matcher("username@hostname//dir1/dir2");

if (matcher.matches()) {
System.out.println("User " + matcher.group(1));
System.out.println("Host " + matcher.group(2));
System.out.println("Dir " + matcher.group(3));
}

}

public static String getParam(String input, String paramName) {

Pattern pattern = Pattern.compile(paramName + "=(\\w+)",
Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);

System.out.println("Matching " + pattern.pattern());

if (matcher.find()) {
System.out.println("Matched " + matcher.group(1) + " in "
+ matcher.group(0));
return matcher.group(1);
} else {
throw new RuntimeException("Cannot find db connection parameter "
+ paramName + " in " + input);
}

}

}