New URLStreamHandlers in glassfish - url

I have a legacy application that I'm trying to port to Java EE. Presently this application calls URL.setURLStreamHandlerFactory() to register some custom URL protocol handlers. This call fails under Glassfish v 2.1 and 3 because glassfish has already registered a factory.
I've tried using the java.protocol.handler.pkgs system property, but that doesn't work for me due to classloader issues. The handler classes are all part of the application and I'm not keen on trying to extract them and put a jar in the container's classpath.
I've got a whiff of osgi bundles - apparently I could write a Bundle that'll deal with the new protocols. I'm not keen on making this web application an osgi bundle (one step at a time! EE first, then osgi if the need arises).
Is it possible to pop a bundle jar in to my WEB-INF/lib directory and have Glassfish load it as a bundle? The bundle will need to import packages from the web applications (another jar in WEB-INF/lib or in WEB-INF/classes). I'm willing to package this app as an EAR if that'll work, I just can't justify osgifying the entire application without knowing more.

I've solved my issue. Apparently I had some wires crossed as the java.protocol.handler.pkgs system property works fine.
For any one else tripping up, I put a jar with my handlers in $DOMAINDIR/lib/ext/ as well as in my WAR's WEB-INF/lib directory. In my application's configuration I've also put a jvm option -Djava.protocol.handler.pkgs=my.handlers.pkg.prefix
I've noticed in glassfish 2.1 it works without the jvm option if I put the prefix in to some startup code, but in glassfish 3 the jvm option is necessary because felix (the osgi implementation glassfish is using) only consults the property upon the server startup, not for each request.

Related

How to integrate moskito-java (monitoring-tool) into a grails application?

I would like to use moskito monitoring within my grails 2.3.9 application.
I already added the dependencies for moskito and the webui into BuildConfig.groovy
compile 'net.anotheria:moskito-core:2.4.2'
compile 'net.anotheria:moskito-aop:2.4.2'
runtime 'net.anotheria:moskito-webui:2.4.2'
runtime 'net.anotheria:moskito-web:2.4.2'
runtime 'net.anotheria:moskito-webui-jersey:2.4.2'
I also marked the classes i want to monitor with the #Monitor annotation.
Now i need to get access to the moskito-webui. I need to add a servlet filter to the web.xml of the grails application (regarding to the documentation). I just installed the grails templates with grails install-templates and modified the web.xml file but i can't get access to the webui of moskito.
Does somebody know how to create an urlmapping or servlet filter for moskito webui within grails? How to integrate it correctly?
if you upgrade to 2.5.0 you don't need an embedded webui, instead you can use a standalone tool called MoSKito Inspect (actually same as WebUI, nicer name).
First add
moskito-inspect-remote
to your dependencies. This jar includes a web-fragment that starts a local listener on RMI port 9041 (port can be altered later).
Second: Download or build your own MoSKito Inspect (take tomcat7 and drop in http://search.maven.org/remotecontent?filepath=net/anotheria/moskito-inspect-standalone/2.5.0/moskito-inspect-standalone-2.5.0.war or build from github source).
Download link is:
http://www.moskito.org/download.html
Third: start your application and MoSKito Inspect. Enter in quickconnect localhost and port 9401. You should see your annotated classes now.
If you have further problems you can also use moskito mailing list: moskito-users#lists.anotheria.net
regards
Leon

How to correctly export a JSF 2.0 web application into war?

I have a JSF 2.0 web project and I use Glassfish 3.1.2 server. In this project, I have among other things :
a class called AjaxServlet annotated with #javax.servlet.annotation.WebServlet
a class called Session annotated with #javax.ejb.Stateful
When I deploy this project to Glassfish from eclipse, everything runs perfectly : in the administration console of Glassfish, the engines associated with the application are [ejb, jpa, web, weld]. But when I first export the project as a war, then from the admin console I deploy the war, the application doesn't work : the engines associated to the application are then [web] only.
How can I export an application to a war without loosing any information (all the engines)? I didn't declare my Servlet in web.xml because in JSF 2.0, we don't need that: the annotations are enough. Does anyone have an idea about what is going on here? I'm quite sure it is not a problem of Glassfish distributions (Web Profile Vs Full Platform) as the same Glassfish server is used for the 2 scenarios above.
Note for those who erroneously think my question is off-topic : Jsf is a framework for programming Java EE web applications. war is a way to package a Jsf application, in the same as jar is a way to package java classes. So my question is totally a programming topic! It is totally legitimate to have it here in StackOverflow. If exporting JSF application into war is not a programming issue, then compiling java classes into .class should not be a programming topic neither! So Pleaze don't downgrade for this reason!
You have to understand the difference between your eclipse environment and your glassfish environment. Not all your libraries that are available in eclipse might be included in your external environment. There are cases where programs compile in eclipse, but not when run from console.
Here's a tip:
Right click on project -> Navigate to Properties > Java Build Path > Order and Export, and ensure that all libraries you require are going to be exported.

OSGi in embedded mode exposing jars which are present in main application

I have one struts2 application which is running Apache Felix OSGi in embedded mode. Is it possible to expose jar files in main webapp to the OSGi bundles? Otherwise I will have to deploy same jar file twice once include in webapp classpath for the main application and once more deployed as bundle inside embedded Felix OSGi container.
Yes. You can certainly do this. But, there are known issues with embedding an OSGi container within a webapp. The arise from the fact that the classloader context of the webapp is non-standard. I've been working with an app that does exactly the same thing. If I was able to write the app from scratch, I would NOT do it this way. I would, instead, deploy Struts2 as an OSGi bundle itself, along with everything else. In other words, I'd embrace the OSGi modularized runtime completely.
With that being said, the OSGi container is itself a bundle and can export packages just like any bundle. It's known as the "system bundle" and you can specify packages from the "host" application's classloader as packages to export into the OSGi container via the system bundle.
See the example on this page, and search for this configuration parameter.
Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA
This config parameter contains a list of packages from the host app's classloader that should be available to your osgi bundles.
As for the "issues", see these links as a start:
A good description of the embedded in a webapp dangers.
Specifics about classloader issues.

Why to use JBoss for Grails deployment?

I'm working on the project that uses Grails as Web framework and JBoss for deploying web-applications. JBoss allows to configure connection to database and then Grails could use JNDI datasource. However in our project we don't use JNDI datasources, we configure data sources for both development and production in DataSource.groovy. Other JBoss services are not used also.
I understand that if we are using JNDI datasource connections then we may benefit if several grails applications are deployed, because in this case there is no need for each grails application to establish its own connection.
So I wonder is there any sense to use JBoss instead of, say, Tomcat or Jetty, if not using it's services?
JBoss is a J2EE container. Compared to Tomcat which is a Servlet and JSP container only, the JBoss AS has a lot more features. Thus JBoss is also "heavier" than Tomcat, and depending on the size of your project this may not fit well with your development team since it has a longer development cycle (I heard there were a lot of improvements in JBoss 7, haven't tried it yet though.)
If you are not using any of the J2EE container features (JMS, EJB, etc.) then you could be fine going with Tomcat using a grails application. We use Tomcat at work for hosting one of our web applications.
In regards to connection pooling managed by JBoss, Tomcat can also do that. Take a look at this documentation page. Hope this helps.

Different log files for Grails applications deployed within one servlet container

I have 2 grails applications packed with --nojars parameter in grails war command. All grails framework libraries are moved to server and they are loader in shared.loader line in tomcat servlet container.
My question is, how can I set different loggers for this different application.
Now log4j = {..} produce the static variable which is shared between two applications.
I'd like to have different logs for each one.
When deploying the war files produced by grails build command without --nojars everithing went well, and each application had its own log file.
Any suggestions ?
I think there should be two different web applications as well. If you do not want the duplicate libraries packaged again, just give them a 'provided' scope and make sure they are available in your instance of Tomcat.

Resources