aspose file tools*
The moose likes Web Services and the fly likes Creating Web service dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Creating Web service dynamically" Watch "Creating Web service dynamically" New topic
Author

Creating Web service dynamically

Eunice Teo
Greenhorn

Joined: Mar 15, 2010
Posts: 6
Hi,

I am working on a project which requires a web application to create/generate a web service according to what the user specify in the UI and then deploy it on the tomcat server. May I know if it is possible to do that? If so can some one give me some tips to start. I've been searching everywhere but nothing seem to be similar to what I need.

Thank you.
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
First of all, I assume that you want to generate SOAP web services. If this is not the case, please disregard the rest of this post.
From my limited knowledge, I come to think of the following:

First suggestion
- Implement an endpoint implementation class that implements javax.xml.ws.Provider<T>.
This kind of endpoint implementation class accepts either any kind of XML document or a SOAP message with any kind of contents.
- In the endpoint implementation, implement the following functionality:
Use the namespace of the incoming XML document as a key to a map and retrieve a Spring bean that is to handle the request.
Invoke the retrieved bean passing on the XML document to it.
- Depending on the user input in the UI, generate an appropriate script (for instance Groovy or BeanShell).
- Dynamically load the script as a Spring bean and associate the bean with, for instance, the namespace of the XML document it is to process.
- Deploy the web service to Tomcat with Metro.

Second suggestion
- Generate the code for the web service and annotate it with JAX-WS.
- Compile and package the web service in a WAR.
- Using the Tomcat deployer (http://tomcat.apache.org/tomcat-6.0-doc/deployer-howto.html#Deploying%20using%20the%20Client%20Deployer%20Package), which I assume can be controlled programmatically, deploy the WAR containing the web service to an instance of Tomcat with Metro installed.

Best wishes!
Eunice Teo
Greenhorn

Joined: Mar 15, 2010
Posts: 6
Oh no.. I have no idea how to start. Would it be possible to provide me with more info? I am very noob at this. So far I've tried to create a Web service bottom up and try to create a client to run it.. it is very different from what you hv suggested. Hence I am lost.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
You may find this a lot easier with the REST style of service since it is generally a LOT simpler, but you really need to tell us more about the problem.

Is this an assignment where you are required to use a particular technology?

How about some "use case" examples of what you want a user to be able to do.

Bill

Eunice Teo
Greenhorn

Joined: Mar 15, 2010
Posts: 6
I am basically building an app for user to customise what they want to show on a web page.

Use case: User has a database. Database contains the links of images and videos. He will use my app to define which table he wants to call and what values he wants to be returned (ie. String or images). After he has done this, my app will generate a web service to meet his definition and this web service will be deployed onto our server. Another group will be doing the client part to consume the web service.
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
If you want to user RESTful web services, as recommended by mr Brogden, then I think RESTlet (http://www.restlet.org/) may be a good choice.
With RESTlet you can programmatically set up resources. RESTful web services are also well-suited to implement web services that retrieve some form of resources, like data from a database, images in a directory hierarchy etc etc.
Best wishes!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
oooo oooo I know, I know - time for a Domain Specific Language

Sorry, my enthusiasm for languages got away from me there.

Actually that could be kind of cool.... and by restricting the range of what the language could do you avoid the dangers of - say - allowing the user to write Java, compile and execute it.

In any case, the creation of a new web service resouce would be a PUT operation and the later retrieval would be a GET in RESTful terms.

Roy Fielding's PhD dissertation is the primary reference for REST but you should start with the Wikipedia article.

SOAP would be a disasterous mess for this project IMHO.

Bill
Eunice Teo
Greenhorn

Joined: Mar 15, 2010
Posts: 6
Hey all.. its me again.. no i cant use rest.. gotta use soap..

This is how I think I should go about it

1) Write a .java file using InputBuffer/FileIO

2) Use command prompt to complie the .java file into a webservice.

Do you think that is feasible?

Thanks.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
Well, it is possible for an application to write one or more Java source code files and compile them, putting the resulting .class files in the correct directory for the SOAP server.

The main problem would be getting the SOAP server to recognize the new service so that it could route requests properly. Normally that takes a server restart.

A second problem is how do you cope with the inevitable compilation and deployment errors?

A third problem is that you may have totally opened your system to malicious coders. A recipe for disaster if I ever heard of one.

Bill
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
Hee hee - the simplest thing would be to combine my domain specific language idea with a SOAP server.

Everybody would use the same SOAP service but the input would be a String[] consisting of the domain specific instructions for building the web page out of the links etc provided by the client. No change to the SOAP server required but everybody's output would be different.

Bill


Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
To further elaborate on my earlier suggestion:
- Specify an interface, similar to the following, that specifies the properties of a handler that handles requests in which the contents of the SOAP body belongs to a specific XML namespace:

- Implement an endpoint class that implements the Provider interface and that is annotated with @WebServiceProvider.
This class contains a Map<String, XMLNamespaceRequestHandler>, where the key is the XML namespace that the corresponding handler handles requests for.
- Implement a mechanism using the Grails bean builder that periodically reads all Groovy scripts, which all are to implement XMLNamespaceRequestHandler, from a certain directory, creates an instance of the handlers and registers them in the endpoint class map mentioned above.
References for how to dynamically create Spring beans from Groovy scripts:
http://www.ibm.com/developerworks/java/library/j-groovierspring1.html
http://www.ibm.com/developerworks/java/library/j-groovierspring2.html

Later, when the application is running, to add a handler for requests belonging to a specific XML namespace, simply write a new Groovy script and save it to the designated directory. The new handler will be picked up by the program, without having to stop it.
Of course, you do not have to store the handler scripts as files in a directory - you can store them in a database table etc etc.
Hope this is of any help!

Update: The above does not solve how the code of the handler is generated. Perhaps one have to use a DSL-approach, after all, in combination with the above approach.
Eunice Teo
Greenhorn

Joined: Mar 15, 2010
Posts: 6
Thank you for your inputs but I am totally confused now.. is there a tutorial which I can follow?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
Thank you for your inputs but I am totally confused now.. is there a tutorial which I can follow?


As you have seen from the responses, what you say you want is pretty far from a simple SOAP web service. Maybe out enthusiasm has led us astray.

I am basically building an app for user to customise what they want to show on a web page.


Suggests that you don't really want to build a "web service" in the SOAP sense. This sounds like a "portlet" to me

Here are some more questions;

Which version of Java?
Operating system?
Are you using one of the many "framework" tools such as Spring?
Which version of Tomcat?

Bill
Eunice Teo
Greenhorn

Joined: Mar 15, 2010
Posts: 6
William Brogden wrote:
Thank you for your inputs but I am totally confused now.. is there a tutorial which I can follow?


As you have seen from the responses, what you say you want is pretty far from a simple SOAP web service. Maybe out enthusiasm has led us astray.

I am basically building an app for user to customise what they want to show on a web page.


Suggests that you don't really want to build a "web service" in the SOAP sense. This sounds like a "portlet" to me

Here are some more questions;

Which version of Java?
Operating system?
Are you using one of the many "framework" tools such as Spring?
Which version of Tomcat?

Bill


I might not need something too complicated..
Any version of Java..
Windows XP
No specific framework..
Tomcat 6.0
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
What would you like as the sequence of events from the first contact the user has with your system to the point at which the user is able to get the customized web page?

How will the user address a url to get the customized web page?

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating Web service dynamically