Wildfly - Bind a data source in runtime - wildfly

In AS6/7, I was able to bind a Data Source object in runtime like this -
Context ctx = new InitialContext();
NonSerializableFactory.rebind(ctx, jndiName, ds);
But NonSerializableFactory class is no more available in wild-fly and there is no way to bind non-serializable object in ctx in runtime. I could not find any replacement of NonSerializableFactory.
Instead I tried the following -
ctx.rebind(jndiName, ds)
But the above is not working. I don't see datasource bound and I am not able to access it. Will greatly appreciate any help!

Related

Autofac: How do I register generic collections?

I have a couple of classes that take IList<IHero> in the constructor. I would like to register the generic collection List<IHero> in Autofac so that whenever Autofac needs to resolve a service that takes IList<IHero, it returns a new instance of List<IHero>. The code below compiles but I get a ton of error messages at run-time.
builder.RegisterType<List<IHero>>().As<IList<IHero>>();
My current workaround is as follows:
var printer = scope.Reseolve<IPrinter>(new TypedParameter(typeof(IList<IHero>), new List<IHero>();
var newEngine = scope.Resolve<IEngine>(new TypedParameter(typeof(IPrinter), printer));
Don't register collections yourself. Autofac handles collections for you.

HikariPool vs HikariDataSource

I'm going to use HikariCP instead of c3p0 in my WEB application. Seems, it's super. But for me the questionable place still exist in the HikariCP interface. It contain two classes - HikariPool and HikariDataSource that contain almost the similar functionality. Looking into sources I have detected that HikariDataSource is like the wrapper for HikariPool. For instance, please find below the interesting part of code:
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/mydb?user=aaa&password=xxx&autoReconnectForPools=true&autoReconnect=true&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8");
config.setMaximumPoolSize(20);
config.setMinimumIdle(2);
HikariPool pool = new HikariPool(config);//using HikariPool class
// HikariDataSource pool = new HikariDataSource(config);// using HikariDataSource class
try (Connection conn = pool.getConnection();) {
// execute some query...
}
Both classes work perfectly.
So, the question is the following: which one is recommended to use mostly and why?
Thank you in advance,
Simon
correct way (API) is to always get connection from data source as:
HikariDataSource hds = new HikariDataSource(config);
hds.getConnection()
be protected by coding to API instead of implementation.
HikariPool is not data source. it is used by HikariDataSource.

Unmarshalling saml metadata extensions using opensaml

I have created some extensions in saml metadata. I'm trying to unmarshall the xml using opensaml2. I have created the interface, implementation class, builder, marshaller and unmarshaller of the extension. Then I registered the object providers using Configuration.registerObjectProvider
Configuration.registerObjectProvider(RequestedAudiences.TYPE_NAME, new RequestedAudiencesBuilder(), new RequestedAudiencesMarshaller(), new RequestedAudiencesUnmarshaller());
When I try to get the extensions using the bellow code segment
List<XMLObject> extensions = spssoDescriptor.getExtensions().getUnknownXMLObjects();
It returns objects of the type
org.opensaml.xml.schema.impl.XSAnyImpl
So now I can't read any value from the object. I want to get an object of the actual extension implementation class I have created.
Can anyone suggest what I am doing wrong?
The problem was I have registered the object providers after creating the metadata object. So at the time of creating the metadata object, opensaml does not know how to create the required extension object (RequestedAudiences object). Registering the object providers before creating the metadata object resolved the problem.
You can use below scala code to extract the information.
val dato = descriptor.getExtensions().getUnknownXMLObjects.get(0).asInstanceOf[XSAny]
println(dato.getTextContent)

How to use GWT SerializationStreamFactory

I am trying to serialize a object in GWT using SerializationFactory, but I am not able to get it working. Here is the sample code of my POC:
import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.client.rpc.SerializationStreamFactory;
import com.google.gwt.user.client.rpc.SerializationStreamReader;
import com.google.gwt.user.client.rpc.SerializationStreamWriter;
...........
Some code here....
.........
......
SerializationStreamFactory factory = (SerializationStreamFactory) GWT.create(MyClass.class);
SerializationStreamWriter writer = factory.createStreamWriter();
try {
writer.writeObject(new MyClass("anirudh"));
String value = writer.toString();
SerializationStreamReader reader = factory.createStreamReader(value);
MyClass myObj = (MyClass) reader.readObject();
System.out.println(myObj.getName());
} catch (SerializationException e) {
e.printStackTrace();
}
It gave me the following exception
Caused by: java.lang.RuntimeException: Deferred binding failed for 'com.anirudh..client.MyClass' (did you forget to inherit a required module?)
also in my code the class whose object I am trying to serialize implements IsSerializable
MyClass implements IsSerializable
I don't want to use GWT Auto-Bean framework because it does not fit my use case. Also I am not using GWT-RPC framework and right now I am quite adamant about using SerializationStreamFactory :D because I seriously want to know how this thing works.
Can anyone share a working example of SerializationStreamFactory or help me out pointing any mistake(s) I did.
Thanks in advance
SerializationStreamFactory factory = (SerializationStreamFactory) GWT.create(MyClass.class);
What are you expecting this line to do? GWT will attempt to find a replace-with or generate-with rule that matches this class (either when-type-assignable or when-type-is), or failing that will attempt to invoke a zero-arg constructor on MyClass, effectively new MyClass(). Is this what you are expecting?
The selected exception you've pasted suggests that MyClass may not be on the source path that GWT has been given to compile from, but the full error log will provide more information.
It looks as though you are trying to mimic the generated RPC code, where a *Async rpc interface would be implemented by code that extends from com.google.gwt.user.client.rpc.impl.RemoteServiceProxy (which implements SerializationStreamFactory). That base implementation is extended further to initialize several fields such as the com.google.gwt.user.client.rpc.impl.Serializer instance, actually responsible for serializing and deserializing object streams.
Serializers are created (by default) from the base class of com.google.gwt.user.client.rpc.impl.SerializerBase, through the rebind class com.google.gwt.user.rebind.rpc.TypeSerializerCreator. If you've build your own generator for MyClass, you should be kicking this off to get the work done as ProxyCreator already should be doing.
Remember when building your own serialization/deserialization mechanism that you need to decide which types can be marshalled within this system - if you open it to all types, then you will need to generate FieldSerializer types for all possible objects on the source path. This will greatly expand the size of your compiled code.
If your main goal is learning how this 'magic' works, dig into the generators and associated code that live in the com.google.gwt.user.rebind.rpc package. There are other libraries that leverage these ideas such as the gwt-atmosphere project (see https://github.com/Atmosphere/atmosphere to get started). Also review the generated code that GWT creates when it builds a 'tradition' RPC interface.

How to carry out custom initialisation with autofac

I'm adding autofac to an existing project and some of the service implementations require their Initialize method to be called and passed configuration information. Currently I'm using the code:
builder.Register(context =>
{
var service =
new SqlTaxRateProvider(context.Resolve<IUserProvider>());
service.Initialize(config);
return service;
}
).As<ITaxService>()
.SingleInstance();
which works but I'm still creating the object myself which is what I'm trying to get away from this and allow autofac to handle it for me. Is it possible to configure a post create operation that would carry out the custom initialisation?
To give you an idea of what I'm after ideally this would be the code:
builder.RegisterType<SqlTaxRateProvider>()
.As<ITaxService>()
.OnCreated(service=> service.Initialize(config))
.SingleInstance();
Update:
I am using Autofac-2.1.10.754-NET35
.OnActivating(e => e.Instance.Initialize(...))
should do the trick.
You might also investigate the Startable module (see the Startable entry in the Autofac wiki).
Mark's suggestion to do initialisation in the constructor is also a good one. In that case use
.WithParameter(new NamedParameter("config", config))
to merge the config parameter in with the other constructor dependencies.