Get or inject Lagom application context LagomApplicationContext - applicationcontext

Exist any way to get LagomApplicationContext? I would like to use play configuration object play.api.Configuration form playContext - composition:
sealed trait LagomApplicationContext {
/**
* The Play application loader context.
*/
val playContext: Context
}
Any ideas or suggestion how to do it? Is Exist some DI?
I need to use values from application.conf the same like in this example:
https://www.webkj.com/play-framework/play-scala-2.5-reading-config-using-di

Yes it is true - but only it is possible to inject in controller level:
class OnlineDiscountTagImpl #Inject() (cache : CacheApi) extends Controller

Related

how to communicate in multi module dynamice feature Application

I have project structure like
app :
InstantApp :
library:
dynamic-feature1 :
dynamic-feature2 :
dynamic-feature3 :
Now there is some dependency between dynamic-feature1 and dynamic-feature2. Now if i add it as dependency in dynamic-feature2 build.gradle then it will cause cyclic dependency.
Above is just one example there are many other cases too. How to handle such dependency conflicts properly or any suggestion ?
Communicate from the main app module to a dynamic feature module
You can do this by reflection in Java. Make sure your class and method names are not obfuscated in the dynamic modules.
Get a Fragment/Activity or other class from module:
Class class = Class.forName("your.dynamic.module.package.name.classname");
Get a method from your class:
Method method = class.getMethod("GenerateQuestion");
Invoke the method:
method.invoke(objectYouWantToInvokeTheMethodOn);
Communicate from dynamic feature module to main module
To get a parent activity of a dynamic feature module's fragment you can do:
#Override
public void onAttach(Context context) {
super.onAttach(context);
activity = (Activity) context;
}
Then you can call a method on that activity.

How to implement custom model manager in sonata admin

I have a entity in my symfony/sonata application which is not persisted via doctrine orm. Instead i would like to fill the data for these entities on demand by calling an api ...
How can i connect this custom managed entity with my existing model which is managed by doctrine orm?
Is there a way to exchange the doctrine managing part with my custom api calls? I know there are some repository functions like findAll() findBy() and so on ... is the solution to overwrite this functions?
Another approaches?
Thanks
I had a similar use case. I ended up with a solution which is roughly the following.
1) Create a custom ModelManager class. In my case I extended the one provided for Doctrine (Sonata\DoctrineORMAdminBundle\Model\ModelManager), but you can also directly implement Sonata\AdminBundle\Model\ModelManagerInterface.
Override the methdods you need to change (e.g. create(), update(), delete()).
# MyCustomModelManager.php
namespace AppBundle\Admin;
use Sonata\DoctrineORMAdminBundle\Model\ModelManager;
class MyCustomModelManager extends ModelManager {
public function create($object){ // Your custom implementation }
public function create($object){ // Your custom implementation }
public function create($object){ // Your custom implementation }
}
2) Register your new model manager as a service (not needed if you use Symfony 3.3 with the default container configuration, which includes automatic configuration of classes as services)
# services.yml
...
AppBundle\Admin\MyCustomModelManager:
arguments: [ '#doctrine' ]
...
3) Configure your admin class to use your custom model manager, for example:
app.admin.myadmin:
class: AppBundle\Admin\MyAdmin
tags:
- { name: sonata.admin, manager_type: orm }
arguments: [~, AppBundle\Entity\MyEntity, ~]
calls:
- [ setModelManager, [ '#AppBundle\Admin\MyCustomModelManager' ]]
Hope it helps :)

How to inject a custom (non-bean) value to JAX-RS resources?

With following JAX-RS resource class,
#Path("/myresource")
class MyResource {
#GET
public Response readSomeValue() {
// ...
}
#Inject
private int someValue;
}
How can I inject someValue?
I'm using org.glassfish.jersey.bundles:jaxrs-ri with Spring on Apache Tomcat. No EJBs.
I, so far, found this.
/**
* Is this gonna work?
*/
class SomeValueProducer {
#Produces
public int produceSomeValue() {
/// ...
}
}
Is this the only way? Using #Procudes?
Where can I place the producer class? Just alongside the resource class?
Do I need a beans.xml?
Do I need a qualifier annotation?
Thanks.
Is this the only way? Using #Procudes?
Yes
Where can I place the producer class? Just alongside the resource class?
doesnt matter, every jar with a valid beans.xml should be scanned from the framework if configured correct: including every package in your project.
Do I need a beans.xml?
yes
Do I need a qualifier annotation?
yes, without a qualififer every method which returns an int value is a possible source for an injection.

c# how to select test or prod class from config file

I have built a cloud project for a month. My problem is that:
I have 2 classes to connect with Ibm web service. First class is the main class and the second one is test class. I put a key-value to appSetting in Config File.
If value in configFile is "TEST", the project will use test class and if value is the "PROD", the project will use main class. When I change the value in config, I will not change everywhere.
My Manager gave me advice to use "interface" but I didn't understand.
How can I solve this problem basiclly?
Both your test class and prod class could implement the said interface. If you need to use the approach where you do the selection in the config file of which class to use you are probably better off creating a data factory class that returns the correct implementation of the interface. The data factory reads the config file and depending on the value in app settings returns the correct class that implements the interface.
Example of doing this in C# (the concept is the same in other oo languages as well):
From the calling class:
SomethingFactory factory = new SomethingFactory();
ISomething testOrProdObj = factory.GetCorrectImplementation();
var result = testOrProdObj.MyMethod();
And in the factory class:
public class SomethingFactory
{
public ISomething GetCorrectImplementation()
{
//Do a check in appsettings to decide which class (TESTSomething or PRODSomething) to instantiate and return
}
}
Implementation of the interface
public class TESTSomething : ISomething
or
public class PRODSomething : ISomething

wicket and AtUnit

I've started playing with Wicket and I've chosen Guice as dependency injection framework. Now I'm trying to learn how to write a unit test for a WebPage object.
I googled a bit and I've found this post but it mentioned AtUnit so I decided to give it a try.
My WebPage class looks like this
public class MyWebPage extends WebPage
{
#Inject MyService service;
public MyWebPage()
{
//here I build my components and use injected object.
service.get(id);
....
}
}
I created mock to replace any production MyServiceImpl with it and I guess that Guice in hand with AtUnit should inject it.
Now the problems are:
AtUnit expects that I mark target object with #Unit - that is all right as I can pass already created object to WicketTester
#Unit MyWebPage page = new MyWebPage();
wicketTester.startPage(page);
but usually I would call startPage with class name.
I think AtUnit expects as well that a target object is market with #Inject so AtUnit can create and manage it - but I get an org.apache.wicket.WicketRuntimeException: There is no application attached to current thread main. Can I instruct AtUnit to use application from wicketTester?
Because I don't use #Inject at MyWebPage (I think) all object that should be injected by Guice are null (in my example the service reference is null)
I really can't find anything about AtUnit inside Wicket environment. Am I doing something wrong, am I missing something?
I don't know AtUnit but I use wicket with guice and TestNG. I imagine that AtUnit should work the same way. The important point is the creation of the web application with the use of guice.
Here how I bind all this stuff together for my tests.
I have an abstract base class for all my tests:
public abstract class TesterWicket<T extends Component> {
#BeforeClass
public void buildMockedTester() {
System.out.println("TesterWww.buildMockedTester");
injector = Guice.createInjector(buildModules());
CoachWebApplicationFactory instance =
injector.getInstance(CoachWebApplicationFactory.class);
WebApplication application = instance.buildWebApplication();
tester = new WicketTester(application);
}
protected abstract List<Module> buildModules();
The initialization is done for every test class. The subclass defines the necessary modules for the test in the buildModules method.
In my IWebApplicationFactory I add the GuiceComponentInjector. That way, after all component instantiation, the fields annotated with #Inject are filled by Guice:
public class CoachWebApplicationFactory implements IWebApplicationFactory {
private static Logger LOG = LoggerFactory.getLogger(CoachWebApplicationFactory.class);
private final Injector injector;
#Inject
public CoachWebApplicationFactory(Injector injector) {
this.injector = injector;
}
public WebApplication createApplication(WicketFilter filter) {
WebApplication app = injector.getInstance(WebApplication.class);
Application.set(app);
app.addComponentInstantiationListener(new GuiceComponentInjector(app, injector));
return app;
}
}