[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

Detecting Container (Web or Desktop Application)

I have common java code library which can be used in a web application or in a desktop application.

It needs to do certain things if it is running in a  web application and other things in desktop application.

How can I find out in which coontainer( Or type of application) I am running.

e.g             if( isWebApplication() ){
                       .....
                  else
                      ....

Please let me know how to find it.
0
harshalsk
Asked:
harshalsk
  • 5
  • 4
  • 4
  • +1
3 Solutions
 
CEHJCommented:
Try getting the ServletContext. It will fail in desktop
0
 
objectsCommented:
theres no real way to be sure without implementing a frame work that is used by all your apps.
What would go close would be to attempt to load a J2EE class, most probably would not be available to a desktop app (though theres no guarantee).

boolean webapp = true;
try
{
   Class.forName("javax.servlet.Servlet");
   // probably a webapp
}
catch (Exception ex)
{
   // definitely not a webapp
   webapp = false;
}
0
 
CEHJCommented:
>>What would go close would be to attempt to load a J2EE class, most probably would not be available to a desktop app

If that's the case, it's going to be more difficult to compile it in the first place and indeed to get it to run it at all. So i suggest you *do* make them available
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
objectsCommented:

harshalsk,

Let me know if you have any questions. That approach will not cause any problems compiling or running.

0
 
CEHJCommented:
It's the other parts of the code that could have problems, if you need to use web-app-specific classes
0
 
harshalskAuthor Commented:
Even if you try to get Servletcontext or Load the Servlet class it does load it in both the environments. I did think of these options earlier but was not sure if there is better way to find out.

I am surprised that how little information out there on this topic. Not even Java handles this kind of utility.
Any other soultion is welcome.
0
 
CEHJCommented:
>>it does load it in both the environments

How could you get a valid ServletContext returned in a desktop application?
0
 
objectsCommented:
> Even if you try to get Servletcontext or Load the Servlet class it does load it in both the environments.

yes mentioned above that may happen, though the servlet classes  should really not be available to the desktop application. If you have control over the environment then you could just remove them.
0
 
harshalskAuthor Commented:
Unfortunately following method always returns a WebApplication as true.

I commented the code (as Objects mentioned in reply)

Let me know I must be dead wrong in the code below.

    public void chekIfWebApplication(){
            try{
                MyFakeServlet dx = new MyFakeServlet();
                if(dx != null){
                    System.out.println("dx is not null");
                      isWebApplication = true;
                }
                ServletContext sc = dx.getServletContext();
                if(sc != null){
                      System.out.println(sc.getMajorVersion());
                      isWebApplication = true;
                      System.out.println("sc is not null");
                }else{
                      System.out.println("sc is null");
                      isWebApplication = false;
                }
//                Class.forName( "javax.servlet.Servlet" );
//                    isWebApplication = true;
//                  } catch (ClassNotFoundException e) {
//                        isWebApplication = false;
                  
            }catch(Exception e1){
                System.out.println(e1.getMessage());
            }
            System.out.println("isWebApplication : "+isWebApplication);
    }
0
 
objectsCommented:
that won't work.

the method i suggested has a better chance of working you just need to ensure the classes are not available in the j2ee classpath.
Take the j2ee classes out of the classpath and try what I suggested.
0
 
KuldeepchaturvediCommented:
isn't it will a far simpler way to have some kind of property setting or something of this kind that you can put in your jar file ( I am assuming this is how you will be distributing it )... which can hold a flag for you which will determine from where the code is running.

Or if running from a webserver, then have a servlet load on the start-up of the webserver, which can set a global var in application context/in a property file or something...

just trying to see a simpler solution for issue here.
0
 
harshalskAuthor Commented:
Guys,

I really appreciate all your replies but finally I was able to figure this out. Well, It may not be applicable to all sorts of environments but here is the deal,
At my clients place we use Websphere Application Server 6.0 and IBM Webspher Application Client for developing the code. We rarely use raw java application code for any enterprise level applications.

Unfortunaley solution suggested by CEHJ and or Objects does not work with Both the above environments because both these environment uses web containers as internal resources and hence it is available in class paths. So it always returns default  ServletContext or loads the servlet class.

Hence I started thinking what is un-common between all the environemtns (web server, app client and or normal java code)   and answer was in system properties. I also see that Kuldeep Chaturvedi is  thinking on same lines.  Also my criteria is java code inside my jar file should be independant of any settings to be done from outside hence relieving the developer from setting something which he intentionally might forgot.

So here are the properties I look for:
For Application client I look for System property "com.ibm.ws.client.installedConnector"
And for Web application I look for "server.root"

These solutions are working for me now but truly it is not the perfect solution. I wish java had provided something simple to find out the container.  
On the web I found that following code which could be better option. Since I am running out of time I took my own approach which was simpler and quicker.

This approach might be better.
http://jonas.objectweb.org/doc/jonas-dox/AbsModifierFactory_8java-source.html


I am awarding equal points to all three of you. Enjoy and good luck.
0
 
objectsCommented:
> both these environment uses web containers as internal resources and hence it is available in class paths.

they shouldn't have it in the classpath, its inecessary :)
0
 
harshalskAuthor Commented:
I know but IBM WAS Application Client loads whole bunch of things in memory even before it loads an application and ends up being added in classpath by its own batch files to launch it.

This kind of sucks but what the heck I did not create the rules.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now