deployed example with correct JNDI name
-----------------------------------------------------------
[#|2012-02-03T13:12:37.837+0200|INFO|glassfish3.1.1|org.glassfish.admingui|_ThreadID=93;_ThreadName=Thread-2;|GUI
deployment: uploadToTempfile|#]
[#|2012-02-03T13:12:37.846+0200|INFO|glassfish3.1.1|org.glassfish.admingui|_ThreadID=93;_ThreadName=Thread-2;|uploadFileName=sample.osgijdbc.helloworld.jar|#]
[#|2012-02-03T13:12:38.001+0200|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=91;_ThreadName=Thread-2;|Installed
org.glassfish.fighterfish.sample.osgijdbc.helloworld [271] from
reference:file:/opt/glassfish3/glassfish/domains/domain1/applications/sample.osgijdbc.helloworld/|#]
[#|2012-02-03T13:12:38.005+0200|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=91;_ThreadName=Thread-2;|JDBCTestBundleActivator:
Activator started|#]
[#|2012-02-03T13:12:38.008+0200|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=91;_ThreadName=Thread-2;|JDBCTestBundleActivator:
com.sun.gjc.spi.jdbc40.ConnectionWrapper40_at_31888179|#]
[#|2012-02-03T13:12:38.010+0200|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=91;_ThreadName=Thread-2;|Started
org.glassfish.fighterfish.sample.osgijdbc.helloworld [271]|#]
[#|2012-02-03T13:12:38.070+0200|INFO|glassfish3.1.1|javax.enterprise.system.tools.admin.org.glassfish.deployment.admin|_ThreadID=91;_ThreadName=Thread-2;|sample.osgijdbc.helloworld
was successfully deployed in 201 milliseconds.|#]
deployed example with wrong JNDI name ----------------
[#|2012-02-03T13:17:45.915+0200|INFO|glassfish3.1.1|org.glassfish.admingui|_ThreadID=99;_ThreadName=Thread-2;|GUI
deployment: uploadToTempfile|#]
[#|2012-02-03T13:17:45.916+0200|INFO|glassfish3.1.1|org.glassfish.admingui|_ThreadID=99;_ThreadName=Thread-2;|uploadFileName=sample.osgijdbc.helloworld.jar|#]
[#|2012-02-03T13:17:45.961+0200|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=77;_ThreadName=Thread-2;|Installed
org.glassfish.fighterfish.sample.osgijdbc.helloworld [273] from
reference:file:/opt/glassfish3/glassfish/domains/domain1/applications/sample.osgijdbc.helloworld/|#]
[#|2012-02-03T13:17:45.964+0200|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=77;_ThreadName=Thread-2;|JDBCTestBundleActivator:
Activator started|#]
[#|2012-02-03T13:17:45.966+0200|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=77;_ThreadName=Thread-2;|Started
org.glassfish.fighterfish.sample.osgijdbc.helloworld [273]|#]
[#|2012-02-03T13:17:45.978+0200|INFO|glassfish3.1.1|javax.enterprise.system.tools.admin.org.glassfish.deployment.admin|_ThreadID=77;_ThreadName=Thread-2;|sample.osgijdbc.helloworld
was successfully deployed in 53 milliseconds.|#]
This is missing: com.sun.gjc.spi.jdbc40.ConnectionWrapper40_at_31888179
So this example works.
my example with correct JNDI name -------------------------
[#|2012-02-03T13:25:09.746+0200|WARNING|glassfish3.1.1|org.apache.catalina.connector.Request|_ThreadID=77;_ThreadName=Thread-2;|PWC4011:
Unable to set request character encoding to UTF-8 from context , because
request parameters have already been read, or ServletRequest.getReader() has
already been called|#]
[#|2012-02-03T13:25:09.831+0200|INFO|glassfish3.1.1|org.glassfish.admingui|_ThreadID=77;_ThreadName=Thread-2;|GUI
deployment: uploadToTempfile|#]
[#|2012-02-03T13:25:09.831+0200|INFO|glassfish3.1.1|org.glassfish.admingui|_ThreadID=77;_ThreadName=Thread-2;|uploadFileName=SH_27-impl-1.0-SNAPSHOT.jar|#]
[#|2012-02-03T13:25:09.881+0200|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=92;_ThreadName=Thread-2;|Installed
SH_27-impl [274] from
reference:file:/opt/glassfish3/glassfish/domains/domain1/applications/SH_27-impl-1.0-SNAPSHOT/|#]
[#|2012-02-03T13:25:09.885+0200|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=92;_ThreadName=Thread-2;|JDBCTestBundleActivator:
Activator started|#]
[#|2012-02-03T13:25:09.890+0200|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=92;_ThreadName=Thread-2;|JDBCTestBundleActivator:
com.sun.gjc.spi.jdbc40.ConnectionWrapper40_at_3fbbb2f3|#]
[#|2012-02-03T13:25:09.891+0200|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=92;_ThreadName=Thread-2;|Started
SH_27-impl [274]|#]
[#|2012-02-03T13:25:09.904+0200|INFO|glassfish3.1.1|javax.enterprise.system.tools.admin.org.glassfish.deployment.admin|_ThreadID=92;_ThreadName=Thread-2;|SH_27-impl-1.0-SNAPSHOT
was successfully deployed in 61 milliseconds.|#]
The activator is started:
com.sun.gjc.spi.jdbc40.ConnectionWrapper40_at_3fbbb2f3. I suppose that the
error is somewhere else.
error log after I try to run the OSGI bundle with datasource
-------------------------------
[#|2012-02-03T13:27:10.189+0200|WARNING|glassfish3.1.1|javax.enterprise.resource.webcontainer.jsf.lifecycle|_ThreadID=165;_ThreadName=Thread-2;|#{loginController.userCompare}:
java.lang.reflect.UndeclaredThrowableException
javax.faces.FacesException: #{loginController.userCompare}:
java.lang.reflect.UndeclaredThrowableException
at
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at
org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at
org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at
com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at
org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at
com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at
com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at
com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
at
com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at
com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at
com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at
com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at
com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at
com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at
com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at
com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at
com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
Caused by: javax.faces.el.EvaluationException:
java.lang.reflect.UndeclaredThrowableException
at
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
... 32 more
Caused by: java.lang.reflect.UndeclaredThrowableException
at $Proxy198.CheckUserDB(Unknown Source)
at com.DX_57.SR_57.userCheck.CallOSGI(userCheck.java:60)
at com.DX_57.SR_57.userCheck.userCompare(userCheck.java:234)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
at
com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
at
com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 33 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at
org.glassfish.osgicdi.impl.OSGiServiceFactory$DynamicInvocationHandler.invoke(OSGiServiceFactory.java:234)
... 44 more
Caused by: java.sql.SQLException: No data source
at
org.DX_57.osgi.SH_27.impl.SessionHandleImpl.CheckUserDB(SessionHandleImpl.java:22)
... 49 more
|#]
Source code of the managed bean which calls the OSGI bundle with the
datasource ----------------------------------------
package com.DX_57.SR_57;
/** include default packages for Beans */
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
// or import javax.faces.bean.SessionScoped;
import javax.inject.Named;
/** include package for SHA-256 encryption */
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/** include SQL Packages */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import javax.annotation.Resource;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
// or import javax.faces.bean.ManagedBean;
import org.DX_57.osgi.CL_27.api.CryptoSHA;
import org.glassfish.osgicdi.OSGiService;
import org.DX_57.osgi.SH_27.api.SessionHandle;
@Named("loginController")
@SessionScoped
public class userCheck implements Serializable {
private String user = null;
private String password = null;
private String error_Message = null;
private String error_Database = null;
public userCheck(){
}
/** Call the Oracle JDBC Connection driver */
@Resource(name="jdbc/Oracle")
private DataSource ds;
/** Call OSGI Bundle SH_27 Session Handle */
/** Use transient in order to disable serialization when calling
OSGI Bundle */
@Inject @OSGiService(dynamic=true) transient SessionHandle
hello;
public String CallOSGI() throws SQLException{
return hello.CheckUserDB("admin");
}
/** Call OSGI Bundle CL_27 Crypto Library */
@Inject @OSGiService(dynamic=true) transient CryptoSHA
cryptoString;
public String zwwe(){
return cryptoString.sayHello("Duke");
}
/** get the content of the variables from the JSF Login page */
public void setUser(String newValue) {
user = newValue;
}
public String getUser(){
return user;
}
public void setPassword(String newValue) {
password = newValue;
}
public String getPassword(){
return password;
}
public String geterror_Database(){
return error_Database;
}
public String geterror_Message(){
return error_Message;
}
/** method for converting simple string into SHA-256 hash */
public String string_hash(String hash) throws
NoSuchAlgorithmException{
MessageDigest md =
MessageDigest.getInstance("SHA-256");
md.update(hash.getBytes());
byte byteData[] = md.digest();
/** convert the byte to hex format */
StringBuilder sb = new StringBuilder();
for (int i = 0; i < byteData.length; i++) {
sb.append(Integer.toString((byteData[i] & 0xff) +
0x100, 16).substring(1));
}
return sb.toString();
}
/** method for checking password into the Oracle database */
public String CheckUserDB(String userToCheck) throws
SQLException {
String storedPassword = null;
error_Message = null;
String SQL_Statement = null;
if (ds == null) throw new SQLException( error_Database
= "No data source");
Connection conn = ds.getConnection();
if (conn == null) throw new SQLException(
error_Database = "No connection");
try {
conn.setAutoCommit(false);
boolean committed = false;
try {
SQL_Statement = "SELECT Passwd
from USERS WHERE Username = ?";
PreparedStatement passwordQuery
= conn.prepareStatement(SQL_Statement);
passwordQuery.setString(1,
userToCheck);
ResultSet result =
passwordQuery.executeQuery();
if(result.next()){
storedPassword =
result.getString("Passwd");
}
conn.commit();
committed = true;
} finally {
if (!committed) conn.rollback();
}
}
finally {
conn.close();
}
/** if the user is not found or password don't match display
error message*/
if (storedPassword == null){
error_Message = "Invalid Username!";
} else {
error_Message = "Invalid Password!";
}
return storedPassword;
}
/** method for inserting user credentials into user sessions
table */
*/
public void SessionRegister (String UpdateUser) throws
SQLException{
String SQL_Statement = null;
error_Message = null;
/** get user session id */
FacesContext fCtx = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession)
fCtx.getExternalContext().getSession(false);
String sessionId = session.getId();
/** get user IP address */
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request =
(HttpServletRequest)context.getExternalContext().getRequest();
String remoteHost = request.getRemoteAddr();
if (ds == null) throw new SQLException( error_Database
= "No data source");
Connection conn = ds.getConnection();
if (conn == null) throw new SQLException(
error_Database = "No connection");
try {
conn.setAutoCommit(false);
boolean committed = false;
try { /* insert into
Oracle the default system(Linux) time */
SQL_Statement = "INSERT INTO
ACTIVESESSIONS (SessionId, UserId, LoginTime, LastRefreshTime,"
+ "UserIP,
UserBrowserID)VALUES ('" + sessionId + "', '" + UpdateUser
+ "' ,
current_timestamp, current_timestamp, '" + remoteHost + "', 'jjjk')";
PreparedStatement insertQuery =
conn.prepareStatement(SQL_Statement);
insertQuery.executeUpdate();
conn.commit();
committed = true;
} finally {
if (!committed) conn.rollback();
}
}
finally {
conn.close();
}
return;
}
/** compare the user and the password */
public String userCompare() throws NoSuchAlgorithmException,
SQLException {
String hash_passwd = null;
String passwdQuery = null;
/** check the password into Oracle using the username
*/
passwdQuery = CheckUserDB(user);
/** convert the plain password in SHA-256 hash */
hash_passwd = string_hash(password);
/** compare the encrypted passwords */
if (password.equals(passwdQuery)){ //
naro4no nekriptirani se sravnqvat
/** insert into users session table the time
when the user login */
SessionRegister(user);
/* success */
/* Call the OSGI bundle with the datasource */
String stri = CallOSGI();
return "0";
} else {
/* failer */
return "1";
}
}
}
Source code of the OSGI bundle which calls the datasource and makes SQL
queries ----------------------------------------
package org.DX_57.osgi.SH_27.impl;
import java.sql.Connection;
import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.annotation.Resource;
import org.DX_57.osgi.SH_27.api.SessionHandle;
public class SessionHandleImpl implements SessionHandle {
@Resource(name="jdbc/Oracle")
DataSource ds;
public String CheckUserDB(String userToCheck) throws SQLException {
String storedPassword = null;
String error_Message = null;
String SQL_Statement = null;
String error_Database = null;
if (ds == null) throw new SQLException( error_Database
= "No data source");
Connection conn = ds.getConnection();
if (conn == null) throw new SQLException(
error_Database = "No connection");
try {
conn.setAutoCommit(false);
boolean committed = false;
try {
SQL_Statement = "SELECT Passwd
from USERS WHERE Username = ?";
PreparedStatement passwordQuery
= conn.prepareStatement(SQL_Statement);
passwordQuery.setString(1,
userToCheck);
ResultSet result =
passwordQuery.executeQuery();
if(result.next()){
storedPassword =
result.getString("Passwd");
}
conn.commit();
committed = true;
} finally {
if (!committed) conn.rollback();
}
}
finally {
conn.close();
}
return storedPassword;
}
}
Source code of the OSGI activator
----------------------------------------------------
package org.DX_57.osgi.SH_27.impl;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;
import org.DX_57.osgi.SH_27.api.SessionHandle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
public class SessionHandleApp implements BundleActivator {
private static final String DSNAME = "jdbc/Oracle";
private ServiceTracker st;
@Override
public void start(final BundleContext bc) throws Exception {
debug("Activator started");
bc.registerService(SessionHandle.class.getName(), new
SessionHandleImpl(), new Properties());
Filter filter = bc.createFilter("(&" + "(" +
Constants.OBJECTCLASS
+ "=" + DataSource.class.getName() + ")" +
"(jndi-name="
+ DSNAME + ")" + ")");
st = new ServiceTracker(bc, filter, null) {
@Override
public Object addingService(ServiceReference reference)
{
DataSource ds = (DataSource)
bc.getService(reference);
try {
debug(ds.getConnection().toString());
} catch (SQLException e) {
}
return super.addingService(reference);
}
@Override
public void removedService(ServiceReference reference,
Object service) {
super.removedService(reference, service);
}
};
st.open();
}
public void stop(BundleContext bc) throws Exception {
bc.ungetService(bc.getServiceReference(SessionHandle.class.getName()));
st.close();
}
private void debug(String msg) {
System.out.println("JDBCTestBundleActivator: " + msg);
}
}
Definitely the error is somewhere else.
--
[Message sent by forum member 'rcbandit']
View Post: http://forums.java.net/node/883210