Poorman jnlp dynamic servlet

I was tired from changing jnlp codebase address between test and production environment. So I wrote this servlet that serves a jnlp file, but with the right codebase address, in accord with the server address.

Dont forget to set the jnlp mime-type (application/x-java-jnlp-file) in your jee server. I hope you like it.

=== JnlpServlet.java
package org.veiogaga;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JnlpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String httpScheme = request.getScheme();
		String serverAdress = request.getServerName();
		int serverPort = request.getServerPort();
		String contextRoot = request.getContextPath();		

		response.setContentType("application/x-java-jnlp-file");
        PrintWriter out = response.getWriter();
        out.println(getJnlp(httpScheme,serverAdress,serverPort,contextRoot));
        out.close();

	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		execute(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		execute(request, response);
	}

	//adjust accordingly with your requirements
	private String getJnlp(String httpScheme, String  serverAdress, int serverPort, String contextRoot){

		//String codebase = "http://localhost:8080/MyApp/";
		String codebase = httpScheme+"://"+serverAdress+":"+serverPort+"/"+contextRoot+"/";
		String href =  "myapp.jnlp";

		        String jnlp = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"

                + "<jnlp codebase=\"%s\" href=\"%s\" spec=\"1.0+\">\n" //codebase

                + "<information>\n"
                + "<title>MyApp title </title>\n"
                + "<vendor>veioGaga Inc.</vendor>\n"
                + "<homepage href=\"http://www.example.org\" />\n"
                + "<description>This app will ghange the world</description>\n"
                + "<description kind=\"short\">just a little bit</description>\n"
                + "<offline-allowed />\n"
                + "</information>\n"
                + "<security>\n"
                + "<all-permissions />\n"
                + "</security>\n"
                + "<resources>\n"
                + "<j2se version=\"1.5+\" />\n"
                + "<jar eager=\"true\" href=\"jnlp/myapp.jar\" main=\"true\" />\n" 

                + "<jar href=\"jnlp/myapp.jar\" />\n"
                + "</resources>\n"
                + "    <application-desc main-class=\"org.veiogaga.app.WorldGui\" />\n"
                + "</jnlp>";

		return String.format(jnlp, codebase,href,httpScheme,serverAdress,serverPort,contextRoot);
	}
}
=== servlet

=== web.xml:
<servlet>
    <description> Generate JNLP dinamicaly</description>
    <display-name>JnlpServlet</display-name>
    <servlet-name>JnlpServlet</servlet-name>
    <servlet-class>org.veiogaga.JnlpServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>JnlpServlet</servlet-name>
    <url-pattern>/myapp.jnlp</url-pattern>
</servlet-mapping>

===
Advertisements

java plugin in ubuntu 12.04

IcedTea java plugin  is broken right now in ubuntu 12.04.
So I’ve downloaded java 7 from oracle and did:

sudo rm  /etc/alternatives/mozilla-javaplugin.so

sudo ln  -s /home/user/opt/jdk1.7.0_05/jre/lib/i386/libnpjp2.so /etc/alternatives/mozilla-javaplugin.so

And it works!
Test it with about:plugins in firefox.Hope it helps.

source: http://www.oracle.com/technetwork/java/javase/manual-plugin-install-linux-136395.html

jaxb, xjc with java 1.4

 

While doing a xml project, I was stuck with an old java 1.4 that I could not  update :-(.

So I used jaxb  with java 1.4 which lacks annotation. Little information on the intertubs so I made this post on howto use jaxb with java 1.4 . Hope it is useful.

You will need  old [JAXB1] libs , compatible with java 1.4.

 

//// ANT COMMAND

$ pwd
/xjc-java14

$ant xjc-compile-1
Buildfile: build.xml

xjc-compile-1:
[echo] Compiling the schema (source version 1.0)…
[mkdir] Created dir: /xjc-java14/src/org/xjctest1
[xjc] Compiling file:/xjc-java14/xsd/sample.xsd
[xjc] removing old output files
[xjc] Writing output to /xjc-java14/src

BUILD SUCCESSFUL
Total time: 3 seconds

///// ANT FILE – please: edit it to your tastes.

<?xml version=”1.0″ standalone=”yes”?>
<project basedir=”.” default=”xjc-compile-1″>
<description>
test xjc: generate class suitable for older java, without annotations (eg 1.4)
</description>
<property name=”jaxb.home” value=”jaxb/” />
<path id=”classpath”>
<pathelement path=”src” />
<pathelement path=”classes” />
<pathelement path=”schemas” />
<fileset dir=”${jaxb.home}” includes=”lib/*.jar” />
</path>
<taskdef name=”xjc” classname=”com.sun.tools.xjc.XJCTask”>
<classpath refid=”classpath” />
</taskdef>

<!–xjc compile source version 1–>
<target name=”xjc-compile-1″ description=”Compile xsd 1.0″>
<echo message=”Compiling the schema (source version 1.0)…” />
<mkdir dir=”src/org/xjctest1″ />
<xjc extension=”true” schema=”xsd/sample.xsd” source=”1.0″ removeOldOutput=”yes” package=”org.xjctest1″ target=”src”>
<produces dir=”src/org/xjctest1″ includes=”* impl/*” />
</xjc>
</target>

<!–xjc compile source version 2–>
<target name=”xjc-compile-2″ description=”Compile xsd 2.0″>
<echo message=”Compiling the schema (source version 2.0)…” />
<mkdir dir=”src/org/xjctest2″ />
<xjc extension=”true” schema=”xsd/sample.xsd” source=”2.0″ removeOldOutput=”yes” package=”org.xjctest2″ destdir=”src”>
<produces dir=”src/org/xjctest2″ />
</xjc>
</target>

<target name=”clean” description=”Deletes all the generated artifacts.”>
</target>
</project>

 

//// XSD –  sample.xsd

<?xml version=”1.0″?>
<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”&gt;
<xsd:element name=”helloWorld” type=”HelloWorldType”/>
<xsd:complexType name=”HelloWorldType”>
<xsd:attribute name=”msg” type=”xsd:string”/>
</xsd:complexType>
</xsd:schema>

//// TEST CALSS – App1.java

package org.xjctest;

import java.io.File;
import java.util.Date;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.xjctest1.HelloWorld;
import org.xjctest1.ObjectFactory;

//simple class for testing jaxb/xjc
public class App1 {

/**
* @param args
*/
public static void main(String[] args) {
try {

testVersion1();

} catch (Exception e) {
e.printStackTrace();
}

}

//depends on java >= 1.4
private static void testVersion1() throws JAXBException {
JAXBContext context = JAXBContext.newInstance(“org.xjctest1”);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
marshaller.setProperty(Marshaller.JAXB_ENCODING, “ISO-8859-1”);

HelloWorld hello = new ObjectFactory().createHelloWorld();
hello.setMsg(“hello world 1.0″);

String outFile=”bin/out1.xml”;
marshaller.marshal(hello, new File(outFile));
p(“xjc version 1.0 saved in “+ outFile);
}

//poorman logger
public static void p(Object o){
System.out.println(“[“+new Date()+”] “+o.toString());
}
}

//// RESULT – XJC

/xjc-java14/src/org/xjctest1$ ls
bgm.ser  HelloWorld.java  HelloWorldType.java  impl  jaxb.properties  ObjectFactory.java

//// RESULT – XML

out1.xml

<?xml version=”1.0″ encoding=”ISO-8859-1″ standalone=”yes”?>
<helloWorld msg=”hello world 1.0″/>

//// JAXB DIR
$ ls xjc-java14/jaxb/lib
activation.jar  jaxb1-impl.jar  jaxb-api.jar  jaxb-impl.jar  jaxb-xjc.jar  jsr173_1.0_api.jar

////LINKS:

[XJC-ANT] http://jaxb.java.net/nonav/2.0.2/docs/xjcTask.html

[JAXB1] download old jaxb libs: http://jaxb.java.net/2.1.2/ (version I used)