<!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 write a singleton is described below.  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> </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>    <BR>    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>    <FONT 
face=Arial color=#0000ff size=2>// constructor</FONT><BR>    
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>        ... your code 
here<BR>    }</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> </DIV>
<DIV><SPAN class=349555600-25112004><FONT face=Arial color=#0000ff 
size=2>    <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>        <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>    <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) >> file JavaMpascript.java <<<BR><BR>package 
  mapserver.java.mapscript;<BR><BR>/**<BR> * Created by user: umberto at 
  unicolet.org<BR> * Date: May 23, 2004<BR> * License: 
  GPL<BR> */<BR><BR>public class JavaMapscript {<BR>    
  <BR>    private static JavaMapscript instance = 
  null;<BR><BR>    private JavaMapscript() 
  {<BR>        try 
  {<BR>            
  System.loadLibrary("mapscript");<BR>        
  } catch (Exception e) 
  {<BR>            
  e.printStackTrace();<BR>            
  System.err.println("* error loading native library 
  *");<BR>            
  System.err.println("Error is: 
  "+e);<BR>        
  }<BR>        System.out.println(" * 
  mapscript native library loaded *");<BR>    
  }<BR><BR>    public static JavaMapscript getInstance() 
  {<BR>        if ( instance == null ) 
  {<BR>            
  instance = new JavaMapscript();<BR>        
  }<BR>        return 
  instance;<BR>    }<BR>}   <BR><BR>2) >> Make an 
  instance of this class somewhere else.<BR><BR>  
  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> <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> <BR><FONT face=arial color=#0000ff 
    size=2><servlet><BR></FONT>    <FONT face=arial 
    color=#0000ff 
    size=2><servlet-name>test</servlet-name><BR></FONT>    
    <FONT face=arial color=#0000ff size=2><servlet-class> {your class 
    name} </servlet-class><BR>    
    <load-on-startup>1</load-on-startup><BR></servlet><BR></FONT><BR><FONT 
    face=arial color=#0000ff size=2>This will force the init() to run without 
    the service().  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,  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> 
      <DD>JavaMapscript.getInstance();<BR>
      <DD><BR> 
      <DD>and later in the:<BR>
      <DD>public void service (HttpServletRequest req, HttpServletResponse 
      res){<BR>
      <DD><BR> 
      <DD>I began writing my mapscript code, and it still gives me the same 
      error:<BR>
      <DD><BR> 
      <DD>javax.servlet.ServletException: Native Library 
      /opt/SUNappserver/domains/domain1/lib/ext/libmapscript.so already loaded 
      in another classloader<BR>
      <DD><BR> 
      <DD>Do you know why?<BR>
      <DD><BR> 
      <DD>Thank you<BR>
      <DD><BR> 
      <DD>Jose Luis<BR><BR>
      <DD>Oliver Wesp <wesp@gdv.com></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>> Hi to all<BR>
      <DD>> I would implement mapserver in tomcat as servlet but I have found 
      a lot of<BR>
      <DD>> problems in start with it!<BR>
      <DD>> <BR>
      <DD>> So:<BR>
      <DD>> 1) is there sites that implement mapserver as servlet in tomcat 
      that I can<BR>
      <DD>> view?<BR>
      <DD>> 2) Some one have a piece of basic code that I can compile as 
      servlet in<BR>
      <DD>> tomcat for view how it run? I have load the java Howto.<BR>
      <DD>> <BR>
      <DD>> Do you have examples so I can see how can I work<BR>
      <DD>> with mapserver functions and servlets?<BR>
      <DD>> I have been seeing the wiki, but I can not really find what I 
      want.<BR>
      <DD>> <BR>
      <DD>> I have done the example of loading a library that apears in:<BR>
      <DD>> <BR>
      <DD>> <A href="http://www.unicolet.org/mapserver/tomcat.html" 
      eudora="autourl">http://www.unicolet.org/mapserver/tomcat.html</A> <BR>
      <DD>> <BR>
      <DD>> <BR>
      <DD>> and it works, but when I want to complete the JavaMapscript.java 
      code <BR>
      <DD>> with other<BR>
      <DD>> mapserver comands, it doesn't work:<BR>
      <DD>> <BR>
      <DD>> import edu.umn.gis.mapscript.*;<BR>
      <DD>> <BR>
      <DD>> public class JavaMapscript {<BR>
      <DD>> private static JavaMapscript instance=null;<BR>
      <DD>> <BR>
      <DD>> private JavaMapscript() {<BR>
      <DD>> try {<BR>
      <DD>> System.loadLibrary("mapscript");<BR>
      <DD>> } catch (Exception e) {<BR>
      <DD>> e.printStackTrace();<BR>
      <DD>> System.err.println("* error loading native library *");<BR>
      <DD>> System.err.println("Error is: "+e);<BR>
      <DD>> }<BR>
      <DD>> System.out.println(" * mapscript native library loaded *");<BR>
      <DD>> mapObj map = new mapObj("test.map");<BR>
      <DD>> imageObj img = map.draw();<BR>
      <DD>> //img.save("result", map);*/<BR>
      <DD>> }<BR>
      <DD>> <BR>
      <DD>> public static JavaMapscript getInstance() {<BR>
      <DD>> System.out.println("Try to instantiate Mapscript");<BR>
      <DD>> System.out.flush();<BR>
      <DD>> if ( instance == null ) {<BR>
      <DD>> instance = new JavaMapscript();<BR>
      <DD>> }<BR>
      <DD>> return instance;<BR>
      <DD>> }<BR>
      <DD>> }<BR>
      <DD>> <BR>
      <DD>> And even worse, when I do any modifications and I put the new 
      .class <BR>
      <DD>> file in the<BR>
      <DD>> server, I have an error because it says that the library was 
      already <BR>
      <DD>> loaded, and<BR>
      <DD>> my only solution now to solve it is to turn off the server and 
      turn it <BR>
      <DD>> on again.<BR>
      <DD>> <BR>
      <DD>> Hope someone can help me<BR>
      <DD>> <BR>
      <DD>> Thank you<BR>
      <DD>> <BR>
      <DD>> Jose Luis<BR>
      <DD>> <BR>
      <DD>> 
      ------------------------------------------------------------------------<BR>
      <DD>> <BR>
      <DD>> Nuevo Correo Yahoo! <BR>
      <DD>> <BR>
      <DD>> <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>