I am struggling to understand how to take advantage/use the MBeanInfo (and related MBeanAttributeInfo, ModelMBeanInfo etc.) and related to it Descriptors.
As far as I understand MBean*Info is simply like a Class object containing information on available methods/attributes/constructors etc. Also, as far as I see it, this information is immutable and generated for us in the process of MBeanServer.registerMBean or using StandardMBean class (maybe the MBeanServer actually uses the StandardMbean class?) which uses reflection to generate this information. Is this more or less correct?
Also, I guess there exist a concept of a ModelMBean, to which we are able to pass our own MBeanInfo programmatically. What is the point of doing so if the same can be achieved via a StandardMbean with reflection?
Finally, Descriptors were put in place to allow users to extend information on the mbeans by providing (name,value) pairs in the form of descriptor. This can be attached to any of the MBean*Info classes (constructor, attribute, operation etc.) However, it can be attached during the construction of the Info object, as it is immutable. Therefore, using StandardMbean, that generates this info on its own, there is no way to inject extra information, as the descriptor we get from the MBeanInfo is immutable too... Am I missing something here? What would be the easy way to add extra information to descriptors of contructors/fields/methods on a MBean?
... this information is immutable and generated for us in the process of MBeanServer.registerMBean or using StandardMBean class ... which uses reflection to generate this information. Is this more or less correct?
Yes. The information is built about the bean and then registered with the server to it can be published to the clients. Not sure if the MBeanServer creates a StandardMBean under the covers.
What is the point of doing so if the same can be achieved via a StandardMbean with reflection?
The MBean*Info classes allow you to programmatically publish a JMX mapping to a concrete type without the need of the interface/impl. This allows external packages such as Spring to be able to detect and publish JMX beans that call through to discovered beans with special #ManagedResource attributes.
This is also how my SimpleJMX package is able to publish beans with its annotations. All you need to do is add the following to one of your classes and SimpleJMX programmatically creates MbeanInfo instances which describe the class. You do not need to define a JMX interface/impl yourself.
#JmxResource(description = "Lookup cache", domainName = "j256")
public class LookupCache {
#JmxAttributeField(description = "Number of hits in the cache")
private int hitCount;
...
Internally, SimpleJMX creates a instance of its class that implements javax.management.DynamicMBean. That interface is what exposes the get, set, invoke action methods. It also returns the MBeanInfo which describes the class for JMX publishing.
Am I missing something here? What would be the easy way to add extra information to descriptors of contructors/fields/methods on a MBean?
I don't use Descriptors with SimpleJMX so I don't know how they are used. They look to be meta information about a method or parameter such as deprecated, defaultValue, etc.. This seems to be static information so immutable as well.
Related
Just another Java problem (I'm a noob, I know): is it possible to use dynamic property binding in a Custom Control with a dynamic property getter in a Java bean?
I'll explain. I use this feature extensively in my Custom Controls:
<xp:inputTextarea id="DF_TiersM">
<xp:this.value><![CDATA[#{compositeData.dataSource[compositeData.fieldName]}]]></xp:this.value>
This is used in a control where both datasource and the name of the field are passed as parameters. This works, so far so good.
Now, in some cases, the datasource is a managed bean. When the above lines are interpreted, apparently code is generated to get or set the value of ... something. But what exactly?
I get this error: Error getting property 'SomeField' from bean of type com.sjef.AnyRecord which I guess is correct for there is no public getSomeField() in my bean. All properties are defined dynamically in the bean.
So how can I make XPages read the properties? Is there a universal getter (and setter) that allows me to use the name of a property as a parameter instead of the inclusion in a fixed method name? If XPages doesn't find getSomeField(), will it try something else instead, e.g. just get(String name) or so?
As always: I really appreciate your help and answers!
The way the binding works depends on whether or not your Java object implements a supported interface. If it doesn't (if it's just some random Java object), then any properties are treated as "bean-style" names, so that, if you want to call ".getSomeField()", then the binding would be like "#{obj.someField}" (or "#{obj['someField']}", or so forth).
If you want it to fall back to a common method, that's a job for either the DataObject or Map interfaces - Map is larger to implement, but is more standard (and you could inherit from AbstractMap if applicable), while DataObject is basically an XPages-ism but one I'm a big fan of (for reference, document data sources are DataObjects). Be warned, though: if you implement one of those, EL will only bind to the get or getValue method and will ignore normal setters and getters. If you want to use those when present, you'll have to write reflection code to do that (I recommend using Apache BeanUtils).
I have a post describing this in more detail on my blog: https://frostillic.us/f.nsf/posts/expanding-your-use-of-el-%28part-1%29
So let's say you have a class called Human, and a bunch of other classes that all extend Human (John, Sally, Mark). Human has an annotation "#Tags({"human"})", John has annotation "#Tags({"john"})", etc.. All classes have their corresponding tags.
My question is: how can I use these tags to figure out which object to make?
So like, a user will input either "john" "sally" or "mark" and I want to make that corresponding object...
Look at the Strategy Pattern:
a software design pattern, whereby an algorithm's behaviour can be selected at runtime
You could use this pattern to match input to a factory or as part of an enum that maps the name to a given class type (though there are several "hard-coding" issues with this approach).
As for construction of object instances, I recommend you also look at the Factory Pattern and combine the two to create these objects in an abstract manner.
I also recommend you don't tag your objects lik this, as you're hard-coding user input expectations against your codebase. At least externalise such expectations to a configuration file (.properties or .xml) so the mappings of input can change independently of the code.
I had faced similar requirements in a Project.
This is what I did,
Design a strategy to read the Class(s) : This is required because the classes are not referenced in any of your code directly. Hence these classes wont be processed by any ClassLoader automatically. Load class dynamically using Class.forName()
Generate a Factory : Make the scope of your annotation as Runtime. This will allow you to process the class files that you have read and extract your custom annotation value out of it. Then you can register the name of this class against the annotation in the factory class.
Use the factory to generate the Objects : Now you have a factory which can return the objects of your class when you pass a tag name. Use reflection to create a Object.
To know more about factory pattern please refer the link : Factory Pattern
I'm working with an application that uses JBPM 3.1 and MySQL. The core problem is that there are processes instances with variables that contain an older version of an external, non-JBPM Serializable class. When the main application is upgraded, these processes instances cause an exception to be thrown by JBPM since the SUID of a specific class instance has changed in the main application.
I believe I have a method for fixing the deserialization process using the technique described in the following:
How to deserialize an object persisted in a db now when the object has different serialVersionUID
However, my problem is figuring out where in MySQL JBPM stores process instance variables, so I can write a program that can interate over all the variables for all instances, an reserialize the variables so the offending class will have the new SUID, so JBPM can operate against the processes.
My initial looking at the JBPM tables, it appears that the JBPM_BYTEARRAY and/or JBPM_BYTEBLOCK may be the tables to operate against. However, I'm unsure how to proceed. I'm guessing each process variable is stored in a wrapping container class. Is that class org.jbpm.context.exe.VariableInstance? Or is it something else?
I figure if I have the proper jar files in the class path, and I know what the main class instance is that JBPM uses to store process variables in MySQL, I can deserialize the class (which will fix the SUID problem with the embedded problem class instance), and reserialize the class back. Since JBPM documentation does mention stuff about converters, I'm unsure if I have to replicate the conversion process JPBM does when deserializing, or if standard java deserialization is enough.
Some analysis of JBPM indicates that binary data may be split across multiple records. This may not be the case for mysql itself, but the JPBM code is written to support multiple RDBMs, and some have limits on the size of binary records.
Since the question earned me a tumbleweed reward, I was not going to get a usable mysql-based answer in within the deadline I had to meet, so I re-considered the core problem and the operating context the problem occurs, and came up with a solution that avoided the needed to perform direct mysql operations.
The main application in question already has some customize modifications to JBPM, so the solution I implemented altered JBPM source which performs the deserialization of process instance variables. This avoids the need to deal with JBPM logic that extracts the deserialized binary data from the RDBMs.
In the class org.jbpm.context.exe.converter.SerializableToByteArrayConverter, I modifed the code to use a custom ObjectInputStream class that returns the latest SUID of a class. The technique of just replacing the descriptor with the latest version of the class as described in the post referenced in the question does not work if the new class includes new fields. Doing so causes an end-of-data exception since the base deserialization code tries to access the "new" fields in the old, deserialized version of the class.
Therefore, I just need to replace the SUID, but keep all other parts of the descriptor the same. Since the JDK does not make ObjectStreamClass extensible, I created a sub-class of ObjectInputStream that returns the new SUID based upon a given calling pattern the java library executes against ObjectInputStream when deserialzing data.
The pattern: When reading the header of a deserialized object, the readUTF() function is called (to obtain the class name) followed by a readLong() call. Therefore, if this calling sequence occurs, and if the readUTF() returned the class name I want to change the SUID of, I return the newer SUID in the readLong() call.
The custom code reads a configuration file that specifies class names and associated SUIDs that should be mapped to the latest SUIDs for the classes listed. This allows mapping of alternate classes in the future w/o modifying the custom code.
Note, this approach is applicable to general deserialization operations, where one needs to map old SUIDs to the latest SUIDs of specified classes, and leaving the other parts of the serialized class descriptor alone to avoid end-of-data problems if the newer class definition includes additional field declarations not present in the older class definition.
Do you know if you made changes that break the contract or is it just simple adding new fields ? If it is simply adding new fields, then just define prior serialversionuid.. Otherwise.. you will have to read all the variables that have different serialversionids and save them under the new class because you are the only person who knows how to convert them.
Is there way to get properties files as strongly typed classes?
I guess there are code generators but doing it with annotations would be much cooler.
What I mean is;
foo.properties file
keyFoo = valuefoo
keyBar = valuebar
maybe with
#properties(file="foo.properties")
class foo { }
becomes
class foo {
String getKeyFoo() { }
String getKeyBar() { }
}
if not shall I start an open source project for that?
ADDITION TO QUESTION;
Think we have a foo.properties file with let say more than 10 entries;
and think it is used as a simple configuration file. What I believe is that this configuration entries should be provided as a configuration class with related getXXX methods to other parts of the design. Then rest of the system accesses the configuration via provided class instead of dealing with key names and don't need to bother where configuration comes. Then you can replace this class with a mock when you are testing callers and dependency to file system goes away. On the other hand it is really nice to
get all entries in a strongly typed fashion.
So this issue is a code generation issue behind the scenes, it is nothing related to runtime. But code generation with an external something instead of annotations didn't seemed nice to me. Although I am not very much familiar with annotations, I guess this could be achieved (but I'll keep in mind that annotations can not generate classes as McDowell points)
There are countless of framework that achieve that for XML with various degree of configuration needed. The standard one bundled with Java is JaxB but it is not exactly a one liner xml persistence framework ...
The problem is that using properties file will only works better than XML (or JSON, ...) on the most trivial classes. When the class become a bit more complex, the properties file will become a nightmare. Another problem is that with trivial classes - there is not much difference between Xml and properties.
That means that the scope of the project will be rather limited. Mostly useful for project having loads of simple properties files.
In big application I worked with, strongly-type reading of properties file is done quite often using a simple factory-method.
Foo foo = Foo.loadFrom("foo.properties");
class Foo {
static Foo loadFrom(String fileName) {
Properties props = new Properties();
props.load(...);
Foo foo = new Foo();
foo.setKeyFoo(props.get("KeyFoo"));
...
return foo;
}
...
}
There is a somewhat similar project for doing configuration as statically typed files. It requires to declare an interface, but it fills in the implementation itself:
public interface AppConfig extends Config {
long getTimeout ();
URL getURL ();
Class getHandlerClass ();
}
The Annotation Processing Tool (apt) cannot modify classes (though it can create new ones). In order to modify the class at compile time, you'd probably need to edit the AST (as Project Lombok does). The simplest approach would probably be to generate the classes and then use the generated library as a dependency for other code.
Yet another way is to use a data binding framework that does this. Even one that does not seem to directly support that could work: for example, Jackson JSON processor would allow this to be done by something like:
ObjectMapper m = new ObjectMapper();
MyBean bean = m.convertValue(properties, MyBean.class);
// (note: requires latest code from trunk; otherwise need to write first, read back)
which works as long as entries in Properties map match logical bean properties, and String values can be converted to matching underlying values.
Something like JFig (ugly IMO), Commons Configuration or EasyConf?
If you want to do it statically, its a code generation problem that may be solved quite easily (for each item in file, produce a new getXXX method).
But if you want this at runtime, then you have the problem of having your code referencing method that did not exists at compile time; I don't think it can be done.
(Note that if you are looking for a project idead, the reverse, having an interface with accessor method and annotation, and an implementation generated at runtime, that relies on the annotated methods, can be done.)
The OP would like to map a property file to a Java API such that each named property in the file corresponds to a similarly named getter method in the API. I presume that an application would then use this API to get property values without having to use property name strings.
The conceptual problem is that a property file is fundamentally not a statically typed entity. Each time someone edits a property file they could add new properties, and hence change the "type" of the property file ... and by implication, the signature of the corresponding API. If we checked that there were no unexpected properties when the Java app loaded the properties file, then we've got an explicit dynamic type-check. If we don't check for unexpected (e.g. misnamed) properties, we've got a source of errors. Things get even messier if you want the types of property values to be something other than a String.
The only way you could do this properly would be to invent the concept of a schema for a property file that specified the property names and the types of the property values. Then implement a property file editor that ensures that the user cannot add properties that conflict with the schema.
And at this point we should recognize that a better solution would be to use XML as the property file representation, an XML schema driven editor for editing property files, and JAXP or something like it to map the property file to Java APIs.
I think this will solve your problem
I have written on this property framework for the last year.
It will provide of multiple ways to load properties, and have them strongly typed as well.
Have a look at http://sourceforge.net/projects/jhpropertiestyp/
It is open sourced and fully documented
Here is my short description from SourceForge:
JHPropertiesTyped will give the developer strongly typed properties. Easy to integrate in existing projects. Handled by a large series for property types. Gives the ability to one-line initialize properties via property IO implementations. Gives the developer the ability to create own property types and property io's. Web demo is also available, screenshots shown above. Also have a standard implementation for a web front end to manage properties, if you choose to use it.
Complete documentation, tutorial, javadoc, faq etc is a available on the project webpage.
Ladies & Gentlemen,
I´m new to Java, forgive me if it´s obvious, but I haven´t found much about it.
I´d like to create dynamic properties (variables) for a class at runtime (define an object that can be altered at runtime by adding or changing properties and methods).
Reason: I want to store a data model in GAE that can be extended dynamically after compilation of the app (yes, the DataStore allows that). What properties should be added are stored in the DataStore as well (It´s like using Robots to built Robots...funny).
Python allows me to add properties at Runtime. Groovy seems to allow that, too. The only thing in the "pure" Java world indicating in that direction seems to be "Dynamic Proxies".
But I couldn´t figure out yet if they do the trick.
Java doesn't have the capability to dynamically add properties. Nor does it have the ability to dynamically create classes at runtime or change them at runtime. Java is strongly and statically typed. The best you can do is put such properties into a Map or similar.
Edit: Ok, apparently some clarifications are in order. The OP specifically mentioned GAE, which none of these methods will work on but I'll mention them since some seem to take exception to their absence.
The Java Compiler API (Java 6+) allows you to compile Java classes at runtime. Technically you could write out a Java source file to look exactly how you want, compile it and load it.
Java bytecode libraries can rewrite classes at runtime. This is used by such libraries as JPA (and others). You could modify classes this way.
What the OP is referring to however is a) in reference to working on GAE and b) more in the order of how Javascript allows you to modify classes or particular instances at runtime by dynamically adding, removing or change properties. Java certainly doesn't do this and specifically doesn't on the GAE.
The above is not an exception to this just like casting a class to char * in C++ so you can read private members doesn't mean C++ doesn't have private members. You're essentially bypassing the Java runtime with both of these methods even though they're part of Java.
Java doesn't support it. Your best bet is to store/manage in some external datastore which you can access from inside the Java code. As a basic and builtin example, you can make use of java.util.Properties API which you load on every request, or cache and reload at timed intervals, or reload programmatically. You can then store the key-value pairs in a .properties file which you just place in the classpath. Here is a Sun tutorial about the subject.
A properties file can look like
key1=value1
key2=value2
key3=value3
If you put it in the classpath, then you can load it as
Properties properties = new Properties();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
properties.load(classLoader.getResourceAsStream("file.properties"));
String key1 = properties.getProperty("key1"); // value1
Other alternatives are for example XML files (which you can access using any Java XML API) or just a database (which you can access using JDBC API).
I don't know if this is an option on GAE (I didn't checked the restrictions) and if this will suit your needs but maybe have a look at the BeanGenerator class from CGLIB (an alternative to the ugly DynaBean from BeanUtils). Quoting "Death to DynaBeans" (have a look at the post):
Not one to let my CGLIB Golden Hammer
go to waste, I have checked
in a BeanGenerator class into CVS. You
use it like so:
BeanGenerator bg = new BeanGenerator();
bg.addProperty("foo", Double.TYPE);
bg.addProperty("bar", String.class);
Object bean = bg.create();
The generated class is an real
JavaBean, which means you can use
standard bean utilities. This includes
all of the classes in the
net.sf.cglib.beans package
(BeanCopier, BeanMap, and BulkBean).
Do your part to end the tyranny of
DynaBeans!
It is possible using Dynamic Proxies. It is also possible to do this on GAE.
First create the class "SomeObject" that exposes methods to get and set property values (i.e. getProperty(name) and setProperty(name, value)).
Then, create an interface "PropertyModel" that contains the methods that you would like your generated objects to have.
Call TransparentProxy.newInstance(someObjectInstance, MyPropertyModel.class) to create a dynamic proxy.
What happens is that Java will extend your object someObjectInstance with the specified interface (btw. you could specify more than one). When you call a method on the proxy object, the method invocation will be redirected to the "invoke(...)" method defined below, you'll need to modify that code to handle both getters and setters and include some exception handling etc. But in general, this is the way dynamic proxies work in Java.
public class TransparentProxy implements InvocationHandler
{
private final SomeObject someObject;
private TransparentProxy(SomeObject someObject)
{
this.someObject = someObject;
}
public static Object newInstance(SomeObject someObject,
Class<? extends PropertyModel> propertyModel)
{
return Proxy.newProxyInstance(someObject.getClass().getClassLoader(),
new Class[] { propertyModel }, new TransparentProxy(someObject));
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
return this.someObject.getProperty(method.getName());
}
}
There is DynaClass library that can be used this way to dynamically create JavaBeans
Map<Object, Object> properties = new HashMap<Object, Object>();
roperties.put("title", "The Italian Job");
roperties.put("dateOfRelease", "new GregorianCalendar(1969, 0, 1).getTime()");
Object movieBean = BeanCreator.createBeanFromMap(properties);