mapserver java servlets

Chris test at STIGASCORP.COM
Wed Nov 24 17:03:37 PST 2004


The safest way to write a singleton is described below.  You could also put
a synchronized code segment within the getInstance() call, but this way is
easier.

public class JavaMapscript {

    private static JavaMapscript instance = new JavaMapscript();

    // constructor
    private JavaMapscript() {
        ... your code here
    }

    public static JavaMapscript getInstance(){
        return instance;
    }
}


  -----Original Message-----
  From: Dan Barron [mailto:dbarron at dswllc.com]
  Sent: Wednesday, November 24, 2004 10:47 AM
  To: Chris; Jose Luis Gonzalez; MAPSERVER-USERS at LISTS.UMN.EDU
  Subject: RE: [UMN_MAPSERVER-USERS] mapserver java servlets


  Jose,

  Here's what works well for me and can be used in a servlet or any jsp
file - courtesy of Umberto btw.

  1) >> file JavaMpascript.java <<

  package mapserver.java.mapscript;

  /**
   * Created by user: umberto at unicolet.org
   * Date: May 23, 2004
   * License: GPL
   */

  public class JavaMapscript {

      private static JavaMapscript instance = null;

      private JavaMapscript() {
          try {
              System.loadLibrary("mapscript");
          } catch (Exception e) {
              e.printStackTrace();
              System.err.println("* error loading native library *");
              System.err.println("Error is: "+e);
          }
          System.out.println(" * mapscript native library loaded *");
      }

      public static JavaMapscript getInstance() {
          if ( instance == null ) {
              instance = new JavaMapscript();
          }
          return instance;
      }
  }

  2) >> Make an instance of this class somewhere else.

    mapserver.java.mapscript.JavaMapscript.getInstance();

  Your library will only be loaded once, at least that is how it behaves for
me and has been reliable so far.

  Dan


  At 06:34 AM 11/24/2004, Chris wrote:

    Jose, what servlet container are you using?

    Try initializing the servlet first without calling it -- you can do this
by putting it in web.xml like this:

    <servlet>
        <servlet-name>test</servlet-name>
        <servlet-class> {your class name} </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    This will force the init() to run without the service().  Also you
should avoid lazy initialization in Singleton classes.

    Regards,

    Chris

      -----Original Message-----

      From: Jose Luis Gonzalez [mailto:joseluismapserver at YAHOO.ES]

      Sent: Wednesday, November 24, 2004 4:51 AM

      To: MAPSERVER-USERS at LISTS.UMN.EDU

      Subject: Re: [UMN_MAPSERVER-USERS] mapserver java servlets


      Hi Oliver,

      I have just done what you recomended,  leaving the JavaMapscript-Class
so the only thing he does is to load the library.

      Then in another class, the one where I have my servlet, I wote in the:
public void init (ServletConfig cfg){



      JavaMapscript.getInstance();



      and later in the:

      public void service (HttpServletRequest req, HttpServletResponse res){



      I began writing my mapscript code, and it still gives me the same
error:



      javax.servlet.ServletException: Native Library
/opt/SUNappserver/domains/domain1/lib/ext/libmapscript.so already loaded in
another classloader



      Do you know why?



      Thank you



      Jose Luis


      Oliver Wesp <wesp at gdv.com> wrote:

      Hi,


      you're on the right way to avoid the "Native library cannot be loaded

      twice" error. Your JavaMapscript-Class implements a singleton so it's

      loaded only once in Tomcat. But you should not add your mapscript code

      to this class. Whenever you change the class Tomcat tries to reload
and

      complains about the library already loaded.

      Write a second class and load the library with


      JavaMapscript.getInstance();


      Add your mapscript code to that class and you should be fine.


      best regards

      Oliver


      Jose Luis Gonzalez wrote:

      > Hi to all

      > I would implement mapserver in tomcat as servlet but I have found a
lot of

      > problems in start with it!

      >

      > So:

      > 1) is there sites that implement mapserver as servlet in tomcat that
I can

      > view?

      > 2) Some one have a piece of basic code that I can compile as servlet
in

      > tomcat for view how it run? I have load the java Howto.

      >

      > Do you have examples so I can see how can I work

      > with mapserver functions and servlets?

      > I have been seeing the wiki, but I can not really find what I want.

      >

      > I have done the example of loading a library that apears in:

      >

      > http://www.unicolet.org/mapserver/tomcat.html

      >

      >

      > and it works, but when I want to complete the JavaMapscript.java
code

      > with other

      > mapserver comands, it doesn't work:

      >

      > import edu.umn.gis.mapscript.*;

      >

      > public class JavaMapscript {

      > private static JavaMapscript instance=null;

      >

      > private JavaMapscript() {

      > try {

      > System.loadLibrary("mapscript");

      > } catch (Exception e) {

      > e.printStackTrace();

      > System.err.println("* error loading native library *");

      > System.err.println("Error is: "+e);

      > }

      > System.out.println(" * mapscript native library loaded *");

      > mapObj map = new mapObj("test.map");

      > imageObj img = map.draw();

      > //img.save("result", map);*/

      > }

      >

      > public static JavaMapscript getInstance() {

      > System.out.println("Try to instantiate Mapscript");

      > System.out.flush();

      > if ( instance == null ) {

      > instance = new JavaMapscript();

      > }

      > return instance;

      > }

      > }

      >

      > And even worse, when I do any modifications and I put the new .class

      > file in the

      > server, I have an error because it says that the library was already

      > loaded, and

      > my only solution now to solve it is to turn off the server and turn
it

      > on again.

      >

      > Hope someone can help me

      >

      > Thank you

      >

      > Jose Luis

      >

      > --------------------------------------------------------------------
----

      >

      > Nuevo Correo Yahoo!

      >

      >



      --

      Dipl.-Geogr. Oliver Wesp

      Gesellschaft fuer geografische Datenverarbeitung

      Binger Strasse 49-51

      D-55218 Ingelheim

      fon: +49 6132 714818

      fax: +49 6132 714828

      http: www.gdv.com





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/mapserver-users/attachments/20041124/c44479f7/attachment.htm>


More information about the MapServer-users mailing list