I am struggling with how to dynamically add some webservices. I am using Scalatra for the webservice framework.
I want to allow the developer to be able to change authentication, for example, so that rather than using hard-coded credentials, instead use a database or password file or whatever they need.
I also want to allow them to add new webservices inside the servlet.
So, what I want to do is in the bootstrap code have it load up and recompile the class and then use that version.
I have looked at this, but I need to recompile an entire class, not snippets.
Generating a class from string and instantiating it in Scala 2.10
This is what I have tried, but I added a "/help" webservice but it isn't found, so the new class isn't being used yet.
class ScalatraBootstrap extends LifeCycle {
override def init(context: ServletContext) {
val sourceDir = new java.io.File("C:/Temp/MyServlet.scala")
val sse = ScalaScriptEngine.onChangeRefresh(sourceDir)
sse.refresh
println("*** - " + sse.compilationStatus.startTime + " " + sse.compilationStatus.stopTime)
context.mount(sse.get[MyServlet]("test.proj.MyServlet"), "/*")
I am using scalascriptengine (https://code.google.com/p/scalascriptengine/) at the moment.
So, how can I recompile the class file for the webservice, when it may have case classes, annotations and object classes in the same file, on the fly?
I am wondering if I need to have the webservice in Groovy instead, but I would prefer to keep it functional.
UPDATE
I had thought about plugins first, but ran into a problem with how would I add new webservices that way, and it may be that Scalatra is not going to be the right choice, that I may need to change my REST service framework.
Eventually I want to be able to change the webservices on the fly without having to restart the application, and recompiling the source would allow that.
Realizing a plug-in affordance is not too hard, at least for reasonably simple cases. The essential elements are:
A trait or abstract class defining the obligations of realizations of the plug-in.
A means to get the code for plug-ins onto the class-path. Alternatively, if you're familiar with working with classloaders, you can do it dynamically. I don't have much experience with that.
Once you have an instance of java.lang.Class[P <: PlugInType] it's trivial to get an instance, provided you don't need constructor parameters.
A protocol in the plug-in trait that allows the plug-in to, e.g., reserve a top-level URL path segment from which you derive a Scalatra route that covers all those paths. You then dispatch requests that match that leading path segment via the plug-in instance. All you have to do is make sure you don't let two plug-ins claim the same path or if you do you have some further means of resolving them.
Thanks to #RandallSchultz I found a solution that works.
override def init(context: ServletContext) {
val sourceDir = new java.io.File("C:/Temp/HelpServlet.scala")
val sse = ScalaScriptEngine.onChangeRefresh(sourceDir)
sse.refresh
println("*** - " + sse.compilationStatus.startTime + " " + sse.compilationStatus.stopTime)
context.mount(new MyServlet, "/*")
context.mount(sse.get("org.myproject.rest.HelpServlet"), "/help/*")
So when I went to "/help/help" I got my help page as expected, so the new webservice was added.
By reading it from a file I was able to add it while the application was running.
Related
Previously when I developed ember application I used App as my global object and every class was stored in this big object.
Like this
window.App.MyModel = Em.Object.extend({});
And in the browser console, I was able to do
App.MyModel.create();
So it was really easy for me to access MyModel class.
Now I started experiments with Ember-CLI, I don't have much experience with this kind of tools. I followed the documentations and I created my model Service like this.
var Service = Ember.Object.extend({});
export default Service
But now, how to access Service class from browser console?
Only way I found was:
App.__container__.resolve('model:service')
But I don't like it much. Is there better way? Btw, can you please explain how export works? Or is there some source (documentation, article) where I can study it?
Thanks a lot for response.
If you're aiming to have something available in most places throughout your application you'll typically want to register it on the container.
There are multiple ways to access the container, and you're correct in that the one you found was less than ideal. The running joke is "any time you directly access __container__ we need to add more underscores."
To directly control how things are registered on the container and injected into the container you typically want to use an initializer. Using initializers in ember-cli is super-easy, they're executed for you automatically.
Checking out the documentation you can see that you get access to the application's container as an argument which allows you to manipulate it in a safe manner.
Once you have access to the container you can use register and inject to make content easily available in particular locations. This was introduced here. One note, accessing things inside of the container from outside the context of your app (browser console) will require the usage of App.__container__ and that is the expected use pattern.
And the export you're running into is an ES6 module system construct, it's not Ember-specific. Playing with the ES6 module transpiler can give you a good sense of what goes in and what comes out in "we can do this today" type of JavaScript.
For ember 3.22, application classes can be accessed like so:
Ember.Namespace.NAMESPACES[1]._applicationInstances.values().next().value.lookup('service:state-events')
Note, you may need to modify the index in NAMESPACES[1] to be something other than 1. You can determine which namespace is your application when this returns true:
Ember.Namespace.NAMESPACES[1] instanceof Application
This approach is how ember-inspector accesses ember applications: https://github.com/emberjs/ember-inspector/blob/50db91b7bd26b12098cae774a307208fe0a47d75/ember_debug/main.js#L163-L168
how can I use the controllers created at
/administrator/components/com_mycom/controllers/*
in
/components/com_mycom/mycom.php
In detail:
I have a »log« controller with an »add« method, and I would like to use this from the frontend. I one is not logged in in the backend the task is not executed and a 500 error rises. So just would like to include the backend controllerpath in the frontend, so that JController::getInstance( 'Mycom' ) still works.
Greetings…
EDIT:
After a long time of searching I could find a more or less undocumented Parameter of the:
JController::getInstance() method, namely the second one: $config = array(). Going through the source code I found out that there is one key of the »config-array« that is of interest, which is: »base_path«.
The call of:
JController:getInstance( 'Mycom, array('base_path' =>JPATH_ADMINISTRATOR.DS.'components'.DS.'com_mycom')' );
always delivers the backend controller and one can use them safely in the frontend, BUT one must take care that then also the views are taken from the backend side of the component. In my case, I just use it to make ajax-calls so it does not matter, but one needs to be careful with using this method when planning to create »frontend views« with »backend controller«.
Greetings…
I had recently a similar problem where I wanted to use the whole CRUD system form back-end also in front-end.
This is the method that worked for me (and I am not saying that this is recommended or best practice):
I've just modeled the folders / file structure from backend. PHP files contained something like:
require_once JPATH_ADMINISTRATOR . '/components/com_mycom/controllers/log.php';
When using sinatra-r18n to handle internationalisation, the r18n lib exposes a variable t for use within your helpers, routes and templates, as per these instructions.
I have written a simple unit test using rack-unit to confirm that some of my pluralisations work but the test throws an error claiming t is nil.
I've tried referencing it via app.t, MySillyApp.t (where MySillyApp is the name of my Sinatra app), MySillyApp.settings.t etc and none of them give me access to the t I need.
What I am trying to achieve is a confirmation that my translation files include all the keys I need corresponding to plurals of various metric units my app needs to understand. Perhaps there is a more direct way of testing this without going via the Sinatra app itself. I'd welcome any insight here.
I had similar task to check localized strings in my Cucumber scenarios.
I've made working example.
Here you can find how strings got translated.
This file halps to understand how to add R18n support to your testing framework:
require 'r18n-core'
...
class SinCucR18nWorld
...
include R18n::Helpers
end
As you can see instead of rack/unit I'm using RSpec/Cucumber, sorry.
I just recently read about "Mocking objects" for unit testing and currently I'm having a difficulties implementing this approach in my application. Please let me explain my problem.
I have a User model class, which is dependent on 2 data sources (database and facebook web service). The controller class simply use this User model as an interface to access data and it doesn't care about where the data came from.
Currently I never done any unit test to this User model because it is dependent on an external web service. But just a while ago, I read about object mocking and now I know that it is a common approach to unit test a class that depends on external resources (like in my case).
Now I want to create a unit test for the User model, but then I encountered a design issue:
In order for the User model to use a mocked Facebook SDK, I have to inject this mocked Facebook SDK to the User object (probably using a setter). Therefore I can't construct the Facebook SDK inside the User object. I have to construct it outside the User object, and inject the SDK into the User object.
The real client of my User model is the application's controller. Therefore I have to construct the Facebook SDK inside the controller and inject it to the user object. Well, this is a problem because I want my controller to be as clean as possible. I want my controller to be ignorant about the application's data source.
I'm not good at explaining something systematically, so you'll probably sleeping before reading this last paragraph. But anyway, I want to ask if anyone here ever encountered the same problem as mine? How do you solve this problem?
Regards,
Andree
P.S: I'm using Zend framework, PHP 5.3.
One way to solve this problem is to create two constructors in User: the default one instantiates the real-life data sources, the other one gets them as parameters. This way your controller can use the default constructor, while your tests use the parameterized one to pass in mock data sources.
Since you haven't specified your language, I show you an example in Java, hopefully this helps get the idea:
class User {
private DataBase database;
private WebService webService;
// default constructor
public User() {
database = new OracleDataBase();
webService = new FacebookWebService();
}
// constructor for unit testing
public User(DataBase database, WebService webService) {
this.database = database;
this.webService = webService;
}
}
This isn't really a question about mocking, but about dependencies--and trying to unit test has forced the issue. It sounds like currently you create your User object within your Controller.
If the User and Controller have the same lifetime (they're created at the same time), then you can pass the User into the Controller's constructor, which is where you can make the substitution.
If there is a User object per call, then perhaps the User object should be passed in by the environment, or returned from some Context object.
If you make the Facebook SDK Object(s) publicly accessible, your User object can still create it when it sets up but you can replace it with a mock before you actually do anything on the User object.
[Test]
public void Test()
{
User u = new User(); // let's say that the object on User gets created in the ctor
u.FacebookObj = new DynamicMock(typeof(FacebookSDK)).MockInstance;
Assert.That(u.Method(), Does.Stuff, "u.Method didn't do stuff");
}
You don't say what language you are using, but I use Ruby and Mocha for mocking objects, and it's quite easy.
See http://mocha.rubyforge.org/.
here the fourth example shows that any call to Product.name from the unit under test is intercepted and the value 'stubbed_name' is returned.
I guess there are similar mechanisms in Java, etc.
Since you will be Unit Testing your test class will play the role of the Controller, so that one should not be touched. So that can remain clean.
You are well underway of reinventing Dependency Injection. So it may be worthwhile to look at Spring or Guice to help you with the plumbing. (If you are in Java land).
Your test can indeed do the injection of the Mocked Facebook SDK and your Database service using setter or constructor arguments.
your tests will now do what the controllers (and maybe views) will do and verify the proper routines are called in the SDK's and that the resources are properly obtained and disposed of.
I am working with a webservice that provides basic CRUD functionality. The Retrieve is easy enough to work with, but I'm having trouble working with the Create (I've not yet messed with the Update or Delete functions).
The update function takes a single argument. This is a zObject in the WSDL. However, this is a generic object extended by what I actually need to pass. If I want to create an account, for example, I pass an Account object which extends the zObject definition.
I cannot for the life of my figure out how to get CF to allow me to do this.
ColdFusion implements the Apache Axis engine for its web service functionality.
Unfortunately CF does not take full advantage of the SOAP object model and allow
CF developers to "new" the different objects that make up a service (or subclass them).
Thankfully there is something we can do about that. When you first access a WSDL,
Axis generates a set of stub objects. These are regular java classes that contain
getters and setters for the base properties of the object. We need to use these
stubs to build our object.
In order to use these stubs however, we need to add them to the ColdFusion
class path:
Step 1) Access the WSDL in any way with coldfusion.
Step 2) Look in the CF app directory for the stubs. They are in a "subs"
directory, organized by WSDL.like:
c:\ColdFusion8\stubs\WS\WS-21028249\com\foo\bar\
Step 3) Copy everything from "com" on down into a new directory that exists in
the CF class path. or we can make one like:
c:\ColdFusion8\MyStubs\com\foo\bar\
Step 4) If you created a new directory add it to the class path.
A, open CF administrator
B. click on Server settings >> Java and JVM
C. add the path to "ColdFusion Class Path". and click submit
D. Restart CF services.
Step 5) Use them like any other java object with <CFObject /> or CreateObject()
MyObj = CreateObject("java","com.foo.bar.MyObject");
Remember that you can CFDump the object to see the available methods.
<cfdump var="#MyObj#" />
Your account object SHOULD be in the stubs. If you need to create it for some reason you would need to do that in a new Java class file
Generally when working with this much Java, cfscript is the way to go.
finally, the code would look like this:
<cfscript>
// create the web service
ArgStruct = StructNew();
ArgStruct.refreshWSDL = True;
ArgStruct.username = 'TestUserAccount';
ArgStruct.password = 'MyP#ssw0r3';
ws = createObject("webservice", "http://localhost/services.asmx?WSDL",ArgStruct);
account = CreateObject("java","com.foo.bar.Account");
account.SetBaz("hello world");
ws.Update(account);
</cfscript>
I agree with the critique of ColdFusion, however, the posted solution also does not respond well to wsdl changes.
Thankfully CF does all access to all the underlying Java methods on objects. This includes 'reflection'. While CreateObject does not know about the stub objects, the classloader that created the webservice does.
ws = createObject("webservice", "http://localhost/services.asmx?WSDL",ArgStruct);
account = ws.getClass().getClassLoader().loadClass('com.foo.bar.Account').newInstance();