<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2900.2523" name=GENERATOR></HEAD>
<BODY>
<DIV><SPAN class=349555600-25112004><FONT face=Arial color=#0000ff size=2>The 
safest way to&nbsp;write a&nbsp;singleton is&nbsp;described below.&nbsp; You 
could also put a synchronized code segment within the getInstance() call, but 
this way is easier.</FONT></SPAN></DIV>
<DIV><SPAN class=349555600-25112004><FONT face=Arial color=#0000ff 
size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=349555600-25112004><FONT face=Arial color=#0000ff size=2><FONT 
face="Times New Roman" color=#000000 size=3>public class JavaMapscript 
{<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; private static JavaMapscript 
instance = new JavaMapscript();<BR></FONT></FONT></SPAN></DIV>
<DIV><SPAN class=349555600-25112004><FONT face=Arial color=#0000ff size=2><FONT 
face="Times New Roman" color=#000000 size=3>&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
face=Arial color=#0000ff size=2>// constructor</FONT><BR>&nbsp;&nbsp;&nbsp; 
private JavaMapscript() {</FONT></FONT></SPAN></DIV>
<DIV><SPAN class=349555600-25112004><FONT face=Arial color=#0000ff size=2><FONT 
face="Times New Roman" color=#000000 
size=3>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;your code 
here<BR>&nbsp;&nbsp;&nbsp; }</FONT></FONT></SPAN></DIV>
<DIV><SPAN class=349555600-25112004><FONT face=Arial color=#0000ff size=2><FONT 
face=Arial color=#0000ff size=2></FONT></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=349555600-25112004><FONT face=Arial color=#0000ff 
size=2>&nbsp;&nbsp;&nbsp; <FONT face="Times New Roman" color=#000000 
size=3>public static JavaMapscript getInstance(){</FONT></FONT></SPAN></DIV>
<DIV><SPAN class=349555600-25112004><FONT face=Arial color=#0000ff 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT face="Times New Roman" 
color=#000000 size=3>return instance;</FONT></FONT></SPAN></DIV>
<DIV><SPAN class=349555600-25112004><FONT face=Arial color=#0000ff 
size=2>&nbsp;&nbsp;&nbsp; <FONT face="Times New Roman" color=#000000 
size=3>}</FONT></FONT></SPAN></DIV>
<DIV><SPAN class=349555600-25112004><FONT face=Arial color=#0000ff size=2><FONT 
face="Times New Roman" color=#000000 size=3>}</FONT></DIV>
<DIV><BR></DIV></FONT></SPAN>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
  <DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Tahoma 
  size=2>-----Original Message-----<BR><B>From:</B> Dan Barron 
  [mailto:dbarron@dswllc.com]<BR><B>Sent:</B> Wednesday, November 24, 2004 10:47 
  AM<BR><B>To:</B> Chris; Jose Luis Gonzalez; 
  MAPSERVER-USERS@LISTS.UMN.EDU<BR><B>Subject:</B> RE: [UMN_MAPSERVER-USERS] 
  mapserver java servlets<BR><BR></FONT></DIV>Jose,<BR><BR>Here's what works 
  well for me and can be used in a servlet or any jsp file - courtesy of Umberto 
  btw.<BR><BR>1) &gt;&gt; file JavaMpascript.java &lt;&lt;<BR><BR>package 
  mapserver.java.mapscript;<BR><BR>/**<BR>&nbsp;* Created by user: umberto at 
  unicolet.org<BR>&nbsp;* Date: May 23, 2004<BR>&nbsp;* License: 
  GPL<BR>&nbsp;*/<BR><BR>public class JavaMapscript {<BR>&nbsp;&nbsp;&nbsp; 
  <BR>&nbsp;&nbsp;&nbsp; private static JavaMapscript instance = 
  null;<BR><BR>&nbsp;&nbsp;&nbsp; private JavaMapscript() 
  {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try 
  {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  System.loadLibrary("mapscript");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  } catch (Exception e) 
  {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  e.printStackTrace();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  System.err.println("* error loading native library 
  *");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  System.err.println("Error is: 
  "+e);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(" * 
  mapscript native library loaded *");<BR>&nbsp;&nbsp;&nbsp; 
  }<BR><BR>&nbsp;&nbsp;&nbsp; public static JavaMapscript getInstance() 
  {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( instance == null ) 
  {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  instance = new JavaMapscript();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 
  instance;<BR>&nbsp;&nbsp;&nbsp; }<BR>}&nbsp;&nbsp; <BR><BR>2) &gt;&gt; Make an 
  instance of this class somewhere else.<BR><BR>&nbsp; 
  mapserver.java.mapscript.JavaMapscript.getInstance();<BR><BR>Your library will 
  only be loaded once, at least that is how it behaves for me and has been 
  reliable so far.<BR><BR>Dan<BR><BR><BR>At 06:34 AM 11/24/2004, Chris 
wrote:<BR>
  <BLOCKQUOTE class=cite cite="" type="cite"><FONT face=arial color=#0000ff 
    size=2>Jose, what servlet container are you using?<BR></FONT>&nbsp;<BR><FONT 
    face=arial color=#0000ff size=2>Try initializing the servlet first without 
    calling it -- you can do this by putting it in web.xml like 
    this:<BR></FONT>&nbsp;<BR><FONT face=arial color=#0000ff 
    size=2>&lt;servlet&gt;<BR></FONT>&nbsp;&nbsp;&nbsp; <FONT face=arial 
    color=#0000ff 
    size=2>&lt;servlet-name&gt;test&lt;/servlet-name&gt;<BR></FONT>&nbsp;&nbsp;&nbsp; 
    <FONT face=arial color=#0000ff size=2>&lt;servlet-class&gt; {your class 
    name} &lt;/servlet-class&gt;<BR>&nbsp;&nbsp;&nbsp; 
    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;<BR>&lt;/servlet&gt;<BR></FONT><BR><FONT 
    face=arial color=#0000ff size=2>This will force the init() to run without 
    the service().&nbsp; Also you should avoid lazy initialization in Singleton 
    classes. <BR></FONT><BR><FONT face=arial color=#0000ff 
    size=2>Regards,<BR></FONT><BR><FONT face=arial color=#0000ff 
    size=2>Chris<BR></FONT>
    <DL>
      <DD><FONT face=tahoma size=2>-----Original Message-----<BR>
      <DD>From:</B> Jose Luis Gonzalez [<A 
      href="mailto:joseluismapserver@YAHOO.ES" 
      eudora="autourl">mailto:joseluismapserver@YAHOO.ES</A>]<BR>
      <DD>Sent:</B> Wednesday, November 24, 2004 4:51 AM<BR>
      <DD>To:</B> MAPSERVER-USERS@LISTS.UMN.EDU<BR>
      <DD>Subject:</B> Re: [UMN_MAPSERVER-USERS] mapserver java 
      servlets<BR><BR></FONT>
      <DD>Hi Oliver,<BR>
      <DD>I have just done what you recomended,&nbsp; leaving the 
      JavaMapscript-Class so the only thing he does is to load the library.<BR>
      <DD>Then in another class, the one where I have my servlet, I wote in the: 
      public void init (ServletConfig cfg){<BR>
      <DD><BR>&nbsp;
      <DD>JavaMapscript.getInstance();<BR>
      <DD><BR>&nbsp;
      <DD>and later in the:<BR>
      <DD>public void service (HttpServletRequest req, HttpServletResponse 
      res){<BR>
      <DD><BR>&nbsp;
      <DD>I began writing my mapscript code, and it still gives me the same 
      error:<BR>
      <DD><BR>&nbsp;
      <DD>javax.servlet.ServletException: Native Library 
      /opt/SUNappserver/domains/domain1/lib/ext/libmapscript.so already loaded 
      in another classloader<BR>
      <DD><BR>&nbsp;
      <DD>Do you know why?<BR>
      <DD><BR>&nbsp;
      <DD>Thank you<BR>
      <DD><BR>&nbsp;
      <DD>Jose Luis<BR><BR>
      <DD>Oliver Wesp &lt;wesp@gdv.com&gt;</I></B> wrote:<BR>
      <DD>Hi,<BR><BR>
      <DD>you're on the right way to avoid the "Native library cannot be loaded 
      <BR>
      <DD>twice" error. Your JavaMapscript-Class implements a singleton so it's 
      <BR>
      <DD>loaded only once in Tomcat. But you should not add your mapscript code 
      <BR>
      <DD>to this class. Whenever you change the class Tomcat tries to reload 
      and <BR>
      <DD>complains about the library already loaded.<BR>
      <DD>Write a second class and load the library with<BR><BR>
      <DD>JavaMapscript.getInstance();<BR><BR>
      <DD>Add your mapscript code to that class and you should be fine.<BR><BR>
      <DD>best regards<BR>
      <DD>Oliver<BR><BR>
      <DD>Jose Luis Gonzalez wrote:<BR>
      <DD>&gt; Hi to all<BR>
      <DD>&gt; I would implement mapserver in tomcat as servlet but I have found 
      a lot of<BR>
      <DD>&gt; problems in start with it!<BR>
      <DD>&gt; <BR>
      <DD>&gt; So:<BR>
      <DD>&gt; 1) is there sites that implement mapserver as servlet in tomcat 
      that I can<BR>
      <DD>&gt; view?<BR>
      <DD>&gt; 2) Some one have a piece of basic code that I can compile as 
      servlet in<BR>
      <DD>&gt; tomcat for view how it run? I have load the java Howto.<BR>
      <DD>&gt; <BR>
      <DD>&gt; Do you have examples so I can see how can I work<BR>
      <DD>&gt; with mapserver functions and servlets?<BR>
      <DD>&gt; I have been seeing the wiki, but I can not really find what I 
      want.<BR>
      <DD>&gt; <BR>
      <DD>&gt; I have done the example of loading a library that apears in:<BR>
      <DD>&gt; <BR>
      <DD>&gt; <A href="http://www.unicolet.org/mapserver/tomcat.html" 
      eudora="autourl">http://www.unicolet.org/mapserver/tomcat.html</A> <BR>
      <DD>&gt; <BR>
      <DD>&gt; <BR>
      <DD>&gt; and it works, but when I want to complete the JavaMapscript.java 
      code <BR>
      <DD>&gt; with other<BR>
      <DD>&gt; mapserver comands, it doesn't work:<BR>
      <DD>&gt; <BR>
      <DD>&gt; import edu.umn.gis.mapscript.*;<BR>
      <DD>&gt; <BR>
      <DD>&gt; public class JavaMapscript {<BR>
      <DD>&gt; private static JavaMapscript instance=null;<BR>
      <DD>&gt; <BR>
      <DD>&gt; private JavaMapscript() {<BR>
      <DD>&gt; try {<BR>
      <DD>&gt; System.loadLibrary("mapscript");<BR>
      <DD>&gt; } catch (Exception e) {<BR>
      <DD>&gt; e.printStackTrace();<BR>
      <DD>&gt; System.err.println("* error loading native library *");<BR>
      <DD>&gt; System.err.println("Error is: "+e);<BR>
      <DD>&gt; }<BR>
      <DD>&gt; System.out.println(" * mapscript native library loaded *");<BR>
      <DD>&gt; mapObj map = new mapObj("test.map");<BR>
      <DD>&gt; imageObj img = map.draw();<BR>
      <DD>&gt; //img.save("result", map);*/<BR>
      <DD>&gt; }<BR>
      <DD>&gt; <BR>
      <DD>&gt; public static JavaMapscript getInstance() {<BR>
      <DD>&gt; System.out.println("Try to instantiate Mapscript");<BR>
      <DD>&gt; System.out.flush();<BR>
      <DD>&gt; if ( instance == null ) {<BR>
      <DD>&gt; instance = new JavaMapscript();<BR>
      <DD>&gt; }<BR>
      <DD>&gt; return instance;<BR>
      <DD>&gt; }<BR>
      <DD>&gt; }<BR>
      <DD>&gt; <BR>
      <DD>&gt; And even worse, when I do any modifications and I put the new 
      .class <BR>
      <DD>&gt; file in the<BR>
      <DD>&gt; server, I have an error because it says that the library was 
      already <BR>
      <DD>&gt; loaded, and<BR>
      <DD>&gt; my only solution now to solve it is to turn off the server and 
      turn it <BR>
      <DD>&gt; on again.<BR>
      <DD>&gt; <BR>
      <DD>&gt; Hope someone can help me<BR>
      <DD>&gt; <BR>
      <DD>&gt; Thank you<BR>
      <DD>&gt; <BR>
      <DD>&gt; Jose Luis<BR>
      <DD>&gt; <BR>
      <DD>&gt; 
      ------------------------------------------------------------------------<BR>
      <DD>&gt; <BR>
      <DD>&gt; Nuevo Correo Yahoo! <BR>
      <DD>&gt; <BR>
      <DD>&gt; <BR><BR><BR>
      <DD>-- <BR>
      <DD>Dipl.-Geogr. Oliver Wesp<BR>
      <DD>Gesellschaft fuer geografische Datenverarbeitung<BR>
      <DD>Binger Strasse 49-51<BR>
      <DD>D-55218 Ingelheim<BR>
      <DD>fon: +49 6132 714818<BR>
      <DD>fax: +49 6132 714828<BR>
      <DD>http: <A href="http://www.gdv.com/" 
      eudora="autourl">www.gdv.com</A><BR><BR></DD></DL><BR><BR><A 
    href="http://es.rd.yahoo.com/mail_es/tagline/mail/image/*http://es.mail.yahoo.com"><IMG 
    height=80 alt="Nuevo Correo Yahoo!" 
    src="http://eur.i1.yimg.com/eur.yimg.com/i/es/mail/mails.gif" width=230 
    NOSEND="1"></A><BR></BLOCKQUOTE></BLOCKQUOTE></BODY></HTML>