Programmatically Stopping a Windows Service from a Web App - windows-services

I have a web application running in a GlassFish 3.1 service on Windows 7 and Server 2008. I would like this application to be able to control a number of other services on the same machine. In particular I want it to be able to start and stop Tomcat services.
Essentially the application is acting as a controller for other applications I deploy on the machine so I can set any security necessary on the controlled applications.
I notice that .Net has a ServiceController class so I'm assuming that programmatic control is possible but I would very strongly prefer a pure Java solution if possible.
All the searches I've done turn up a lot of information about running Java as a service and not much on controlling services from Java. Is what I'm trying to do possible? What security hoops will I need to jump through?

You can do Runtime.exec("net stop tomcat6"). Of course tomcat6 can be replaced with any other service name.
Also check out this tutorial
http://www.java-samples.com/showtutorial.php?tutorialid=8

Related

Why is it not recommended to host receive endpoints in a web application using MassTransit?

I am working on an ASP.NET MVC 5 application (based on nopCommerce). I want to use MassTransit to communicate with another application that is used for inventory management, billing, etc. It would be easier if I could add receive endpoints directly into the web application and not have to create a Windows service for that. But the MassTransit documentation says it is not recommended and there is no explanation as to why that is.
MassTransit in a web application
Configuring a bus in a web site is typically done to publish events,
send commands, as well as engage in request/response conversations.
Hosting receive endpoints and persistent consumers is not recommended
(use a service as shown above).
Does anyone know the reasoning behind it? Is it not safe to add receive endpoints in a web application? Will they not work properly?
Hosting endpoints in a web application is not recommended because web applications often restart for various reasons. These reasons are typically outside the control of the application itself.
While using a standalone Windows service is highly recommended, as long as the bus is properly started and stopped using the Application_Start and Application_End methods, it can be okay if you have no other options available.

Silverlight 3 and System.ServiceProcess - Doesn't allow the reference to be added

I'm trying to write a program that can stop and start services using SilverLight 3 and VS2010.
I can't add the reference to the System.ServiceProcess dll.
C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.ServiceProcess.dll
1st why? - It it just a security precaution?
2- Is there a work around? Does SilverLight have a different library that I can use to start and stop services.
Also note that this is a desktop application and not a web app. It will be sitting on my desktop to toggle services on and off.
Yes, it's a security precaution. Can you imagine the havoc Silverlight apps could cause on a system if they could start/stop services at will?
No, there is no work around, and for that I rest easier at night.
Have you considered writing a full-trust XBAP instead? I don't know if that would work for your situation, but I'd imagine a full-trust XBAP would be able to start/stop services. Or even a standard WPF desktop app? Why use Silverlight for this?
Edit: I'm really not sure if a full-trust XBAP would be able to do that, it's just something worth investigating, I think.
The only way to achieve this, if you absolutely need silverlight is to:
Create a WCF Service that must be installed on the machine you wish to start/stop services on.
Then through silverlight call the WCF service and ask it to start/stop services.

Is it possible to host a asp.net mvc website from a winforms application?

I have a winform application that controls some transmitters and sound cards. There is a requirement to be able to provide a web interface for controlling those devices.
Currently I use WCF to communicate from the controllers in my asp.net MVC site to the winform app. That works well, but there is now a desire to move the hardware to another machine when needed and that means that IIS has to be installed and set-up on that machine.
I know it isn't that hard, but I won't be the one actually doing the moving. It will be the users. If I could host the site from the winform app them it would basically be portable besides the hardware drivers need for a usb to serial converter we use.
Can you use a windows service? The whole winforms app as a service doesn't seem right to me. It assumes that the app is always running. I would create a windows service and expose WCF endpoints from that.
You will need to install IIS or Cassini to host the MVC web site. There's no way around that.
Huh, I would go with Greg's answers.
Also, making your app IIS dependant is not that bad. Or Cassini dependant. You don't want to end up writing your own webserver, which could easily happen when you continue to add features to the app.
I don't know about you but it just feels you are taking all the load on yourself, you are going to spend possibly dozens of hours to implement it to spare an hour or two for someone who doesn't want to install real webserver.
if you want to host MVC under winforms then i would look into the upcoming .netcore 3 version which should allow this combination.
though you'll have to wait until 2019 Q1
https://github.com/dotnet/core/blob/master/roadmap.md
otherwise i've mostly hosted simper stuff using nhttp library if i want it in a winform app.
(NHTTP is a library that gives you very simple crude http request functionality so no mvc sadly but it works for simnpler stuff)

Is it possible to deploy Sharepoint.dll to a server not running MOSS 2007

I have a web service app - that I need to connect to a MOSS server.
for example:
using (SPSite site = new SPSite(this.SiteAddress ))
{
using (SPWeb web = site.OpenWeb())
MOSS is not running on the same server as this web service app. Should this be a problem trying to use the object model. Or do I really have to go the web service route?
As far as I know you need SharePoint installed to use the object model (you may be able to fudge it, but I wouldn't fancy your chances :-)). You options are to either use the built in SharePoint webservices, expose your own "friendly" service and consume it from your other server or move your webservice to the Sharepoint box.
At the very minimum you will need to deploy Microsoft.SharePoint.dll to the machine hosting the web service. If it does require MOSS classes (the one's you've listed only require WSS) then you will also need to deploy the MOSS assembly (I believe it's Microsoft.Office.Server.dll). After that, it's going to become a deploy and pray situation in my experience :)

EJB JNDI lookup on different WebSphere servers

I have two separate installs of WebSphere. (Actually one is WebSphere Application Server V6.1 with EJB 3.0 and Web Services feature packs, and the other server is WebSphere ESB Server V6.2). However, I know that ESB is really built on top of WAS, so it has all the configuration settings that a regualr WAS server has.
In my ESB server, I am trying to expose a service written as EJB 3.0 that will be deployed to the WAS 6.1 server. My question is not how to get EJB 2.1 calls to call into an EJB 3.0. We've done that already. My question is how to call across physical VM's. The WebSphere Application Server is running in its own cell/node/server from the ESB Server. From what I've read in IBM documentation, it is possible to set up a namespace binding on WAS to point to a remote EJB on another WAS instance. Thus you could use JNDI to lookup a bean on one WAS instance that really resides in another WAS instance. The beauty of this method is the location of the EJB you want is abstracted to the container level, and you don't have to drag around properties files of the IP addresses and ports that you need to access the bean should it change servers, etc. You just make a standard JNDI lookup to a remote EJB and you get it.
Sounds like it can be done. (See the following links:
http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=/com.ibm.websphere.express.doc/info/exp/ae/tnam_view_bindings.html) Especially follow the links on EJB and Indirect namespace bindings.
But I've been hitting my head against this for a while. It makes sense. It looks like it can be done. And the Indirect namespace binding looks the most promising. But I can't get it to work quite right. My ESB server keeps complaining about not finding comp/env/ejb in the context in which I am asking for it. Very puzzled by this one.
Just wondering if anybody has done this kind of thing before. Can you give me a concrete example of how you set this up in WAS to do so? Any help is appreciated
Well, I have since talked with IBM on how to do this and was surprised by their answer. They answered that if you are talking EJB to EJB within the same server or server cluster, then use EJB RMI via IIOP. With JNDI this abstracts where the bean is actually running (in a clustered environment).
If you are going from one server (or server cluster) across into a different server (or server cluster) regardless of whether or not the target and source are in the same cell, IBM recommended that you use messaging or web services. They felt that was a better method of abstraction between applications to keep them from being "tied" to each other. They did say that you could get EJB's to talk RMI via CORBA, but said to do that ONLY if absolutely necessary. And of course, you would need to know the IP and port number for coming in over CORBA (and that times each cluster member if in a clustered environment).
Again, this kind of surprised me, but it does make sense. Just thought I'd share these thoughts with the world, especially if you are working with WebSphere.
how to lookup from tomcat
use IBM JDK as runtime for tomcat
find bootstab port , use iiop in PROVIDER_URL
I was stuck with the same problem. After trying to include all the websphere and ibm orb jars found this article at ibm
How to lookup an EJB and other Resources in WebSphere Application Server using a Oracle JDK client - http://www-01.ibm.com/support/docview.wss?uid=swg21382740
basically used the CNCtxFactory instead of WsnInitialContextFactory
//props.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
Hashtable env = new Hashtable();
env.put("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory");
env.put("java.naming.provider.url", iioppath);

Resources