Java VM Object references - java

I got a question: Is it possible to get a list of currently instantiated objects from the VM?
I am using a framework and try to implement an event handler (Hibernate, Interceptor). My problem now is that I need a Properties file during the execution of this handler. I cannot pass a reference to the Interceptor class, because Hibernate or the interface does not have such a method.
I now thought, why not trying to get the reference another way? Or is it possible to register global available objects during the runtime of an application?
Thanks for advice and regards from Germany,
Marco

I now thought, why not trying to get
the reference another way? Or is it
possible to register global available
objects during the runtime of an
application?
You can use a public static field. This belongs to the class rather than to any instance of the class. Very simple example:
public class Properties {
public static String PROPERTY = "abc";
}
You will then be able to access it from anywhere with:
Properties.PROPERTY
You can find more here:
http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html

Related

How instantiate classes dynamically based on configuration in Java

Is there any way to change which class is going to be instantiate in Java (in Spring If it is possible).
For example, using Refection and reading configuration from a database it could be possible but Reflection is heavy technique because the number of created object is going to be very high.
So that, what I need is create objects in runtime based in a external configuraciĆ³n. If I change the configutation, in runtime and without redeploying I would like yo create objects of a diferent class than before. For example, a process is creating objects of class A, an after changing the configutation without redeploying, the process must create objects of class B
You can use newInstance method.
Class.forName("<<your class name>>").newInstance()
But you need to typecast the new instance.

Create a class that is accessible to the whole application

In android there is a mechanism of ensuring that only one instance of a class is available to the whole application. This can be done by deriving that class from Application.
Can some thing similar be done in servlets? I want to initialize a class when the application is deployed. This class should have only one instance. So that what all the servlets can access it.
I came to know that you can store your data in the servlet context's hashmap. But I don't want to do that. I want to write my own class with its functions. How should this be done?
I think what you're after is simply a singleton.
This is best implemented by defining an enum with a single instance. (Note that enums allow you to have member functions just as classes.)
public enum YourSingleton {
INSTANCE;
// Your methods...
}
and then you access it as
YourSingleton.INSTANCE
So, create whatever class you want with its own functions or whatever you like, and put that in the ServletContext at startup. You can use a ServletContextListener to initialize and remove it. What's limiting about that?
Use singleton pattern so the first call to instance method (say YourClass.getInstance()) will create the instance and it will be reused across the application.

Accessing the Global object in play! 2.0 with Java

I have extended the GlobalSettings class as shown in the tutorial Application global settings.
How do I then gain access to the instance of this class, let's say from a view method?
I'm assuming that one instance was created when the application was started and that it is probably a member of some object.
I would have expected for example to find it here:
Play.application().getGlobalSettings()
or something similar, but I couldn't find it anywhere in the Play.* hierarchy.
Any ideas?
Thanks.
I'm new to Play 2.0, however, I think you're better served by using plugin injection. Check this out:
https://github.com/typesafehub/play-plugins/tree/master/inject
Using this approach you simply add the following line to your controller (and some other configuration, as documented in the link above):
#Inject static MyStaticObj obj;
And all the rest is done automatically using the injection framework. No need to worry about global, etc.
That said, like you I spent a lot of time trying to figure out how to use the GlobalSettings object for this before discovering the plugin injection framework.
My sense is that given how Global is implemented (as class in the default/unnamed package) it's not possible to reference it anywhere in the application code. I'm not sure if this was by design or by accident (it seems that the Play folks are thinking about Scala quite a bit these days...). Fortunately the plugin approach seems to be better way to handle these shared globals.
Just reference the Global object directly.
For example don't write :
public class Global extends GlobalSettings {}
write
object Global extends GlobalSettings {}
or
object Global extends Global {}
class Global extends GlobalSettings {}
and than you can reference Global object anywhere in your code just write :
Global.someMethod()

Best Pattern to create Global Objects in Java

My application requires access to certain Objects throughout the application. These objects are populated through the DB.
I would like to populate the objects on server startup , and then in the application on a need -to -do basis.
What is the best pattern to implement this, i have read about the Registry pattern, but not sure that is exactly what i want.
I am also using Spring 3.x in my application , is there something in Spring that can help me?
Since you say you are using Spring. The best solution is to use FactoryBean. What this class allows you to do is create a bean of whatever type you like and return it to the Spring context. Spring will manage this bean and expose it to other beans by making it a singleton. An example:
public class MyObjectFooFactoryBean extends SimpleJdbcDaoSupport implements FactoryBean<ObjectFoo> {
private String query;
#Override
public String getObject() throws Exception {
return an ObjectFoo here using SimpleJdbSupport (Because I also extended SimpleJdbcDaoSupport)
}
#Override
public Class<?> getObjectType() {
return ObjectFoo.class;
}
#Override
public boolean isSingleton() {
return true;
}
}
Above does two things. 1) Extends SimpleJdbcSupport which allows you to have access to the databse, and 2) Implements FactoryBean which returns the object to Spring's context. After this is done, you can #Autowire it or use it in the xml file.
Are you sure your application really requires that?
Global objects are artifacts from the past of programming, they shouldn't exist anymore in a proper object-oriented environment, and are usually considered an anti-pattern.
Surely you don't need to access these objects everywhere. Why don't you just inject them in the classes that need them? If you use Spring, that's even easier, only have to declare the dependency in the context.
If there are many of these objects that you need, you could wrap them all in one holder class, that you inject as needed.
When I use global objects I usually wrap each object in a singleton pattern. It's the simplest solution, and you can even lazy load it only when needed.
In some cases this is a very valid pattern. For example in any iphone app you have a singleton Application object which is accessible globally through a public static method
This can get a bit hairy if your application is deployed in multiple servers (each with its own VM, thus having singletons as static fields will get messy, and having context-based attributes won't work unless the servers are set to replicate and that's unreliable as well). I'd recommend using a database memory table instead, or some other sort of server-independant memory cache. ( http://memcached.org/ for instance)
What about defining a global object to store these information? You can control access to this object. If you want to monitor this object's state, you can use observer patterns.

Serialize KeyguardLock

I am writing an android application and I need to have two classes use the same KeyguardLock object but I am experiencing extreme difficulty in sharing (via serialization) that object. I have tried using the serialization stackoverflow example link but that didn't work at all. I get a "not serializable" IO exception trying to save the object. I have also tried using JSONObject.
Any ideas? Has anyone run into a similar problem?
Why are you trying to serialize it? A object can only be serialize if it implements Serializable which KeyguardLock doesn't.
If you're trying to pass it around Activities, either create a custom Application object and store it there. Or use a public static variable in a class and access it via that. The static variable is probably the better option for this.

Categories

Resources