EJB / EE :: Inject DataSource Based On Runtime JNDI Name Variable
Sep 10, 2014
I need to inject a DataSource, but the catch is the JNDI isn't known until Runtime. I was hoping the EJB could get a System Property of the JNDI name and use that to Inject the DataSource.
And I could either @Inject or on postContstruct() set the myRuntimeJNDI variable to the JNDI name that is configured on the App Server via System Properties or some other mechanism. But the JNDI may be different depending on what environment the application is deployed to.
Is this possible? I would like to have the same EAR deployed in all our environments and not have to create a different version of the application for each environment.
I am setting up a standalone JNDI and loading a Datasource to the JNDI. DataSource I use is:
org.apache.commons.dbcp.BasicDataSource
The JNDI is set up as follows
String detectorHost = InetAddress.getLocalHost().getHostName(); System.out.println("detectorHost: " + detectorHost); System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); final NamingBeanImpl namingInfo = new NamingBeanImpl(); namingInfo.start();
[Code] ....
I get the following exception
javax.naming.CommunicationException [Root exception is java.io.NotSerializableException: org.apache.commons.dbcp.BasicDataSource] at org.jnp.interfaces.NamingContext.bind(NamingContext.java:677) at org.jnp.interfaces.NamingContext.bind(NamingContext.java:611) at javax.naming.InitialContext.bind(Unknown Source) at com.lombardrisk.reform.integration.ReformIntegration.createJNDIServer(ReformIntegration.java:93)
[Code] ....
I don't quite follow why I am getting a NotSerializableException exception, this is a local JNDI in the same JVM and not a remote JNDI. Not sure why this occurs.
Let's say I have a Junit4 class FooTest.java and variable token. At some point in class the token gets instantiated.
Java Code:
public class Foo { private String token; @Before public void setUp() { // serious is steps to get the application to a certain state where token can get extracted .... token = extractToken(); } } mh_sh_highlight_all('java');
Now I have another class User.java where I need to use this token. Can I inject this token somehow into that class? There is no relation between Foo and User. I can't use Provider method in the configure file because extraction of the token depends on certain system's state and can't be called anywhere anytime.
How can i take run time value for static final variable...my lecturer said first time assignment is possible for declared final variable but in my case it shows compile time error..I'm placing my program below with error message
class Sample { static final String cname; void print() { System.out.println(cname); } public static void main(String args[]) { cname=args[0]; Sample s=new Sample(); s.print(); } }
Sample.java:11: cannot assign a value to final variable cname. cname=args[0];
I am new to java. I have recently learned JDBC connection pool in tomcat. To make code reuse I want to share the connection among all servlets without any conflict.
Here My code snippet:
public class GetConnection{ private DataSource ds; public Connection getConnection(){ try { InitialContext initialContext = new InitialContext(); Context context = (Context) initialContext.lookup("java:comp/env"); ds = (DataSource) context.lookup("connpool");
[Code] .....
Is this right way to do. Or I will get any problem due to concurrent threads.
Though it may seem strange but in one of the application i work on still uses EJB 2.1 entity beans.While looking at the deployment log, seems like each Entity bean is registered using both remote-home and remote interfaces.
Using the remote-home's JNDI lookup i was able to get the EJBObject proxy and subsequently create and use the entity.But what about the remote interface JNDI lookup ? Reason i am asking is that one needs to create an entity before use it. That said, how to use the object that i get from remote interface JNDI lookup ? Note that the class of the returned object says its "com.sun.proxy.$Proxy13" type.The JNDI location i am using "java:app/EJBApp/Entity!com.abc.remote.Remote"
There is a weblogic server running at remote place and i need to access the API's in that remote method using JNDI lookup. My application is configured in Spring Tool Suite IDE with java 6 and tomcat 7 and I have used Spring to perform the jndi lookup of weblogic server. In spring i have used simpleremotestatelesssessionproxyfactorybean class to lookup a weblogic server using jndi and get the remote object. But somehow on doing it i'm getting the following error.
org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 203 completed: Maybe at com.sun.corba.se.impl.logging.ORBUtilSystemException.writeErrorSend(Unknown Source) at com.sun.corba.se.impl.logging.ORBUtilSystemException.writeErrorSend(Unknown Source) at com.sun.corba.se.impl.transport.SocketOrChannelConnectionImpl.sendWithoutLock(Unknown Source) at com.sun.corba.se.impl.encoding.BufferManagerWriteStream.sendFragment(Unknown Source)
[Code] ....
The exception is been thrown at com.sun.corba.se.impl.encoding.CDROutputObject method when calling writeTo(). Why I'm getting this error and can I do anything to remediate it. Irrespective of java this error occurs, i tried with java 5, 6 and 7 but still getting the same error.
The term "Local variable" is related to scope. That is a local variable is one which is defined in a certain block of code, and its scope is confined inside that block of code.And a "Member variable" is simple an instance variable.
I read in a discussion forum that when local variables are declared (example code below), their name reservation takes place in memory but they are not automatically initialized to anything. On the other hand, when member variables are declared, they are automatically initialized to null by default.
Java Code: public void myFunction () { int [] myInt; // A local, member variable (because "static" keyword is not there) declared } mh_sh_highlight_all('java');
So it seems that they are comparing local variables and member variables. While I think a member variable can also be be local in a block of code, isn't it?
when we create another variable and set it equal to the first : Car c2 = c1;
we're pointing c2 at the same car object that c1 points to (as opposed to pointing c2 at c1, which in turn points at the car). So if we have code like,
Car c1 = new Car(); Car[] cA = {c1, c1, c1, c1};
are we doing the same? Are we creating four *new* reference variables, each of which points at the same car (again as opposed to pointing them at c1 itself)? I think so, but want to make sure I'm understanding this correctly.
I have a question about updating a grid during runtime in GridWorld. I'm making a game called Flood-It (basically, you click on squares to change their color and attempt to get all of the squares the same color in the grid) and I'm having trouble with changing the grid size. I made my own world class, called CellWorld.
Whenever a user wants to change the grid size after playing the game, this is supposed to set the grid to the new updated size, but it never changes in the actual game, i.e. the user just won a 2x2 game, attempts to change the size to 10x10, but the grid stays 2x2. By debug testing, I can say for certain that everything else works, such as the maxStepCalc and loading the grid. The only issue is the new grid not showing up.
I am using Swing, I have a JPanel and in it there is a JTextArea and a JButton. I want the JTextArea to move when the button is clicked on. I'm Not really sure how to do the action listener for the button. at the moment the JTextArea only moves once when the button is clicked on, but i want it to move every time the button is clicked on.
This is what i have so far:
moveButton = new JButton("MOVE"); moveButton.setName("move"); moveButton.setBounds(20, 140, 70, 40); text = new JTextArea("hello"); text.setEditable(false); text.setBounds(x, 50, 40, 20); panel.add(moveButton); panel.add(text);
In the actionPerformed method this is what it does:
There is no permission for me that I can change SQL query(or anything like this), and I've to return the modified ResultSet(not TableModel). I googled a lot to implement AbstractResultSet, but no luck for me. How can I achieve this ?
Iam getting this error at run time while using this command "java mypack.Accountbalance". I have compiled this source code and got its class file that I have put in mypack package but now its showing this error ..
Here's the code : package mypack; class Balance { String name; double bal; Balance(String n ,double b)
[Code] .....
Error: exception in thread "main" java.lang.NoClassDefFoundError: mypack/Ba at mypack.Accountbalance.main(Accountbalance.java:25) used by: java.lang.ClassNotFoundException: mypack.Balance
During runtime, I need to load the JAR files and relevant config files( .cfg files and .properties file) into CLASSPATH and run a specific java program from one of the JAR which is available in CLASSPATH.
Any relevant Java API details or a sample java program to implement the above use case.
I am looking for a way to compile Java Source-Files at runtime and save them all in an executable jar; almost like an IDE would do. I know that there is the javax.tools package which provides a JavaCompiler interface and you can use ToolProvider.getSystemJavaCompiler() to get an instance of a compiler. However, this method has one important problem: it only works on machines that have the JDK installed. Not when only the JRE is installed.
I guess at this point that I need some kind of third party library that offers an implementation of a JavaCompiler. Unfortunately, this is really complicated to search for on the internet since all top listings when searching "compile java at runtime jre" do not really provide a solution to the problem.
I am writing a (somewhat) complex simulation software right now which is supposed to be used by people who have absolutely no knowledge of programming. At the same time, this software should provide the user with a certain amount of flexibility and control over the flow of the simulation.
My previous take on this problem was to build a complex system to interprete user settings from a GUI. I would basically read the GUI input, output it to some kind of own scripting syntax which I just quickly made up and have that interpreted at runtime. Then I realized, that is a silly concept and I threw it out before I got far into the developement. The much better solution I came up with is taking the input from the GUI, create java source code from it and compile it at run-time. Seems much cleaner and nicer to me; will also probably have a better performance, but thats not really an issue anyways.
I am having some issues with this code its a a DB generator that creates tables and populates them. There seems to be no compiler errors, but there is a runtime error. I have added the error and the code below :
Runtime error:
Exception in thread "main" java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6964)
[Code] .....
My Code:
MakeDB.java import java.sql.*; import java.io.*; public class MakeDB { public static void main(String[]args) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
I have to build a server application. My issue is that it can never shutdown/restart. But I still need to update it.After some research I learned about OSGI where I can add/remove a bundle of code while the application is running. (update to a new version)Can I use JavaWS to update my OSGI application without having to close and restart it? I'm new to OSGI/JavaWS.