JNDI Lookup and PARENT_LAST with Websphere - java

I'm experiencing a side effect with the following scenario:
I'm looking up a URL via JNDI in a J2EE 5.0 web application running on Websphere Application Server 8.5.5 this way:
URL jndiLmcUrl = null;
try {
Context initialContext = new InitialContext();
jndiLmcUrl = (URL) initialContext.lookup("java:comp/env/url/ENLMC);
} catch (NamingException ne) {
String message = "Unable to found middleware configuration file specified with Jndi property java:/comp/env/url/" + systemPropertyName + " as URI/URL. Trying as system property";
LOG.error(message);
}
My web.xml configuration is:
<resource-ref>
<res-ref-name>url/ENLMC</res-ref-name>
<res-type>java.net.URL</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
In addition i have defined the URL Resource using WAS Console under Resources/URL/Urls with server scope.
ALL IS WORKING FINE as expected.
But, for some reasons (primarily WS Metro usage) i've to move the application to PARENT_LAST using MyApplication/ManageModules/Class loaded with local class loader first (parent last).
After this operation the JNDI lookup fails raising a Naming Exception.
Thank you for your precious help.

Related

Weblogic exception : javax.naming.NameNotFoundException: Unable to resolve 'jdbc.payment'. Resolved 'jdbc'; remaining name 'payment'

I have this exception when I lookup jndi datasource from weblogic from spring boot application...only after one successful deployment...I mean from the second deployment on.If I restart the container, it will work fine for the first deployment only.
Caused by: javax.naming.NameNotFoundException: Unable to resolve 'jdbc.payment'. Resolved 'jdbc'; remaining name 'payment'
the datasource with the same name and attached to the admin server.
I use docker image : store/oracle/weblogic:12.2.1.4-dev with environment variable PRODUCTION_MODE=dev
update : if I deattach the data source from the server then reattche it again then start the war, It runs successfully for one time againز
update : switched to local installation of weblogic not dockerized any more and the behavior still happens
It's a spring issue...has nothing to do with weblogic.
In the war shutdown, Spring remove the data source form the server JNDI tree, however the data source still up and running on the server.
The action of recreating or even reattaching the data source to target server, add it again to the JNDI tree.
The workaround to solve this behavior is to prevent spring from calling the destroy method of the data source bean
#Primary
#Bean(name = "dataSource",destroyMethod = "")
#Profile("weblogic")
public DataSource dataSourceWeblogic() throws NamingException {
JndiTemplate jndiTemplate = new JndiTemplate();
InitialContext ctx = (InitialContext) jndiTemplate.getContext();
return (javax.sql.DataSource) ctx.lookup(jndi);
}

Cannot cast from ConnectionWrapper to oracle.jdbc.OracleConnection using JAVA1.8 and Tomcat 8.5.28

Why connection is not working after few seconds? Application is hanging up and not running as expected and returning the below error.
java.lang.ClassCastException:
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
cannot be cast to org.apache.tomcat.dbcp.dbcp2.DelegatingConnection
Below is the code that is used to get the connection:
OracleConnection oracleConnection = (OracleConnection)
((DelegatingConnection)connection).getInnermostDelegate();
using libraries: commons-pool1.6.jar for encryption & tomcat-dbcp.jar for database.
Using encrypted username and password in Tomcat context.xml.
Also, using accessToUnderlyingConnectionAllowed=true in context.xml file.
Issue is with JAVA8 and Tomcat8. Able to work properly with plain credentials, the only issue happens with encrypted credentials.
You shouldn't do casting or unwrapping. Use correct DataSource type in Tomcat 'conf/context.xml' file. In case of Oracle it is: oracle.jdbc.pool.OracleDataSource.
Set also correct driver and factory.
Look at this example of mine:
<Context>
<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<Resource name="UNCUNC"
auth="Container"
type="oracle.jdbc.pool.OracleDataSource"
factory="oracle.jdbc.pool.OracleDataSourceFactory"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:#p260unc4.big.ru:1566:uncunc"
user="dsserv"
password="dsservPass"
connectionProperties="SetBigStringTryClob=true"
maxTotal="20" maxIdle="10"
maxWaitMillis="-1"/>
<JarScanner scanManifest="false"/>
Later in the java code use it like this (don't cast):
try {
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("UNCUNC");
} catch (NamingException e) {
logger.error("DATASOURCE error", e);
}
Connection conn = ds.getConnection();
Should work just fine. Take attention in different versions of Tomcat you need to use 'username' instead 'user' field.
I faced the same issue. After a lot of analysis realized it was classloading issue. Fixed the issue by providing ojdbc jar in shared.loaded (in conf/catalina.properties)
shared.loader="/path/to/ojdbcN_jar/ojdbcN.jar"
This will make sure that the OracleConnection classes are loaded from the same jar in Tomcat and in the deployed webapp.
And in the application where OracleConnection is needed, use the below:
OracleConnection oracleConnection = connection.unwrap(OracleConnection.class);
Note: In my application I have ojdbc jar so that my application compiles fine, but when deployed, the jar used will be the one mentioned in shared loader.
Also don't forget to enable accessToUnderlyingConnectionAllowed when creating the Tomcat JDBC connection pool

Problems accessing Context.xml Mail resource in OpenShift

Please help,
I have a Java Spring MVC application (Tomcat 7) on OpenShift which is trying to use a resource lookup in Context.xml. I keep getting 500 errors when it tries to create the session session = (Session) envContext.lookup("mail/Session"); ( see code below ). There are no exceptions in the logs in app-root/logs/jbossews.log, e.printStackTrace(); doesn't print anything in the catch block. Do exceptions get logged somewhere else perhaps? I'm used to having a access.log and error.log in apache/tomcat, jbossews.log doesn't see to tell me anything (FYI I am not using RHC tools, I'm just tailing the log over ssh).
My project runs fine on my local system in eclipse with no issues.
Please help,
Thanks
Session session = null;
Context initalContext = new InitialContext();
Context envContext = (Context) initalContext.lookup("java:comp/env");
try{
// Blows up here with a 500 error
session = (Session) envContext.lookup("mail/Session");
}
catch (Exception e) {
// No StackTrace printed
e.printStackTrace();
}
Here is my resource located in jbossews/conf/context.xml
<Resource
name="mail/Session"
type="javax.mail.Session"
auth="Container"
mail.smtp.host="smtp.gmail.com"
mail.smtp.socketFactory.port="465"
mail.smtp.socketFactory.class="javax.net.ssl.SSLSocketFactory"
mail.smtp.auth="true"
mail.smtp.port="465"
mail.from="XXXXXX#mailserver.com"
mail.user="XXXXXX#mailserver.com"
password="XXXXXX"
/>
I tried adding the following to my web.xml to see if that would fix the problem (It didn't).
<resource-ref>
<description>Resource reference to a factory for javax.mail.Session instances that may be used for sending electronic mail messages, preconfigured to connect to the appropriate SMTP server.</description>
<res-ref-name>mail/Session</res-ref-name>
<res-type>javax.mail.Session</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Figured out my issue was due to an issue in my pom.xml (Not related to accessing Context.xml after all). Annoyingly Exceptions weren't being logged in jbossews.log so I had to explicitly log them via Exception exception = (Exception) httpRequest.getAttribute("javax.servlet.error.exception");
exception.printStackTrace(); ( httpRequest being a HttpServletRequest Object)
The problem in my pom.xml was this, I tried using:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.1</version>
<scope>provided</scope>
</dependency>
Using the following dependency solved the issue.
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.2</version>
</dependency>
Note: In order for the Context.xml "mail/Session" resource to work on OpenShift I had to create a lib folder in src/main/webapp/WEB-INF/ and place javax.mail-api.jar inside.
Hope this helps anyone else experiencing problems.

JBOSS 7.0 - unable to connect to server using http-remoting protocol

All,
I am trying to invoke an EJB from remote standalone program. EJB is properly deployed on JBOSS 7.0. I am using the below code to do the lookup:
Properties jndiProps = new Properties();
jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
jndiProps.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
jndiProps.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
jndiProps.put(Context.SECURITY_PRINCIPAL, "user1");
jndiProps.put(Context.SECURITY_CREDENTIALS, "password#123?");
jndiProps.put("jboss.naming.client.ejb.context", true);
try {
Context ctx = new InitialContext(jndiProps);
HelloWorld d= (HelloWorld) ctx.lookup("HelloWorldSessionBean/HelloWorldBean!com.ibytecode.business.HelloWorld");
// String s=d.sayHello();
// System.out.println(s);
}
catch(Exception ne) {
ne.printStackTrace();
}
But I am getting the below exception and unable to proceed further. Can anyone let me know what I am missing:
Failed to connect to any server. Servers tried: [http-remoting://localhost:8080]
javax.naming.NamingException: Failed to connect to any server. Servers tried: [http-remoting://localhost:8080]
My Server is up and running and I am using standalone-full.xml file
I think http-remoting: protocol is not available in your version of JBoss (AS 7).
Change your provider URL from
jndiProps.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
to
jndiProps.put(Context.PROVIDER_URL, "remote://localhost:4447");
Also, make sure you have all the necessary JBoss EJB remoting project libraries in your classpath of your client.

Error while runing java EE project with Jboss and Wildfly 9.x

I have error while runing my Project it's correctly deployed but i have this error
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
here is my main Code :
try {
Context context = new InitialContext();
UserServicesRemote userservicesremote = (UserServicesRemote) context.lookup("behealthy-ear/behealthy-ejb/UserServices!services.UserServicesRemote");
} catch (NamingException e) {
e.printStackTrace();
}
My canonical adress is true :
Suggestion:
Perhaps you should add jboss-client.jar into your client environment.
../bin/client/
this exception (NamingException) is indicating that the name of service not is correct. When you server is deployed, you can see in the log the names of services deployed. Por example:
java:global/backend-ear/backend-ejb/UserBOEJB!org.backend.bo.UserBOLocal
java:app/backend-ejb/UserBOEJB!org.backend.bo.UserBOLocal
java:module/UserBOEJB!org.backend.bo.UserBOLocal
java:global/backend-ear/backend-ejb/UserBOEJB
java:app/backend-ejb/UserBOEJB
java:module/UserBOEJB
You can use the first line of your log (global) for the lookup.
The error is not from the actual lookup, but it can't initialise the JNDI-system properly.
When you have a standalone-client you need to add a file jndi.properties like this:
java.naming.factory.url.pkgs=org.jboss.ejb.client.naming
and a file jboss-ejb-client.properties like this:
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=localhost
remote.connection.default.port = 8080
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
You have to add jboss-client.jar to the classpath and use the correct JNDI-name in the ejb:-namespace, not the one in the java:-namespace from the logfile. It looks like this:
ejb:backend-ear/backend-ejb/UserBOEJB!org.backend.bo.UserBOLocal

Categories