How to make my root jaxb generated class extend my own class - java

I want my xjc generated root class extend one of my class A, which is nothing but a wrapper around the root class. Can i do that without modifying the xsd file (how to force schema compiled classes to extend specific class outside schema)

You can define a super-root class (see http://jaxb.java.net/nonav/2.0.2/docs/vendorCustomizations.html). Every generated class (in that xjc run) will be extending that root class.
The required global binding can be declared in an external binding file instead of inside the xsd (see http://java.sun.com/webservices/docs/1.4/tutorial/doc/JAXBUsing4.html#wp148515). Then you can pass it to the xjc generator together with the xsd.

Related

Load dynamic library java that inherit interface

I want to load dynamic library where classes inherit from an interface/abstract class on my core project, so I can load my classes at runtime and use it. How can i do that ?
Example:
Core: ITrigger (interface)
Library: {MyTriggerOne extends ITrigger} {MyTriggerTwo extends ITrigger}
If you want to load a class/library dynamically use Class.forName('class name') method to load.
I had the same requirement and I used the library Reflections.
Very simple code snippet:
public Set<Class<? extends ITrigger>> getITriggerClasses() {
final Reflections reflections = new Reflections("package.where.to.find.implementations");
return reflections.getSubTypesOf(ITrigger.class);
}
Then you can use the method Class::newInstance to create the ITrigger(s).
This is a very simple example, there are several options to initialize the Reflections class (not only with one package name).
Java's SPI(Service Provider Interface) libraries allow you to load classes dynamically based on the interfaces they implement, that can be done with the help of META-INF/services.
You can create a interface like
package com.test.dynamic;
public interface ITrigger {
String getData();
String setData();
}
you can use the ServiceLoader class to load the interface like below code
ServiceLoader<ITrigger> loader = ServiceLoader.load(ITrigger.class);
then you can perform all the operation on it.
If you have some other implementing classes on your classpath, they register themselves in META-INF/services.
you need to create a file in META-INF/services in your classpath with the following properties
The name of the file is the fully qualified class name of the
interface, in this case, it's com.test.dynamic.ITrigger
The file contains a newline-separated list of implementations, so
for the example implementation, it would contain one line:
com.test.dynamic.impl.SomeITriggerImplementation class.

JMeter Beanshell share class definitions

is there a way to share class definitions between scripts created purely in jmeter? For instance if I had the following structure:
Thread1
-BSSampler
-BSSample2
How can I create a class in BSSampler and use that same class definition in BSSample2 explicitly? Or would I have to push the class definition out to a file and use
${__beanShell(source("filename.bsh"))}
to share the same class definitions? Right now it's saying it doesn't recognize the class definition because it's a different namespace.
You can declare your classes and functions in a bsh file that you reference in user.properties through:
beanshell.server.file=../extras/startup.bsh
You can have a look at this file in extras/startup.bsh

Passing a customized object to Scala template

I have an object profileModel in my profile package and my profile.scala.html file have following code
#(model: ProfileModel)
when I compiles, it is giving an error recursive value model needs type
But when I moved this class to models with my application.conf as
ebean.default="models.*"
it works.
My guess is scala compiler automatically adds models.* to class path at the time of compilation
Is there a way to make this work without moving the class back to models package ?
I am using play 2.2.1 built with Scala 2.10.2
If I understand you right, if your ProfileModel exists in profile package correct declaration in the view should be:
#(myProfile: profile.ProfileModel)
And 'yes', Play imports automatically all models and controllers (and also other well known types), but if you want to use any type in custom package (or ie. imported lib) you need to use full qualified path to it.

jaxb.index and nested classes (and OSGi)

When I try to refer to nested classes within my jaxb.index file, an exception is thrown during serialization. How can this be avoided?
This is in an Eclipse RCP application. The classes causing the exception are in a different plug-in than the one that creates the JAXB context and initiates serialization. The classes are in one of the plug-in's exported packages.
The class structure looks like this (names have been changed):
#XmlRootElement(name="foo")
#XmlAccessorType (XmlAccessType.FIELD)
public class Foo extends AbstractFoo {
...
#XmlRootElement(name="fooMetric")
#XmlAccessorType (XmlAccessType.FIELD)
public static class FooMetric implements IFooMetric {
...
}
}
The jaxb.index file contains these:
Foo
Foo.FooMetric
During serialization, the exception says to use "OuterClass.InnerClass" -- which I'm doing.
javax.xml.bind.JAXBException: error loading class "Foo.FooMetric" listed in com/mypackage/jaxb.index, make sure that entries are accessable on CLASSPATH and of the form "ClassName" or "OuterClass.InnerClass", not "ClassName.class" or "fully.qualified.ClassName"
- with linked exception:
[java.lang.ClassNotFoundException: com.mypackage.Foo.FooMetric]
The javadocs ("Format for jaxb.index") also suggests that jaxb.index can contain entries of the form OuterClass.InnerClass.
Constraints on class name occuring in a jaxb.index file are:
Must not end with ".class".
Class names are resolved relative to package containing jaxb.index file. Only classes occuring directly in package containing jaxb.index file are allowed.
Fully qualified class names are not allowed. A qualified class name,relative to current package, is only allowed to specify a nested or inner class.
However, this does not appear to work. What will make it work?
The solution I found (by trial and error) was to use OuterClass$InnerClass in jaxb.index instead of OuterClass.InnerClass. This allows serialization to complete successfully.
However, I haven't found any authoritative source that recommends this.
[I'm posting this solution per stackoverflow guidelines, but would love to see and accept a better answer.]

WSImports specify package name for a class

I have following scenario, I have two packages say com.ws.a and com.ws.b.
Both the packages having web-services classes. com.ws.a contain class TestServoceA which is having method which returns class TestA.
I want to pass this generated class to TestServiceB which is in com.ws.b.
But wsimport tool generate two different classes for both packages like:
com.ws.a.TestA and
com.ws.b.TestA
I want to generate only one class com.ws.a.TestA as a proxy and use it in both namespaces.
How can I do this? If custom binding can help me please give me example.
Thanks.
wsimport will use JAXB for data binding, follow this documentation to specify custom bindings:
http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/2.0/tutorial/doc/JAXBUsing4.html

Categories

Resources