You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Thomas Polliard <th...@polliard.com> on 2007/04/12 03:04:59 UTC

Singleton in Tomcat

I have a singleton class that I want to load  The code is like such:

<%
    Pam pam = Pam.getInstance();
%>

I get a ClassNotFoundException which is odd seeing as how the following
code works fine

<%
    Pam pam = null;
%>

The fact that it finds Pam in the second example makes me wonder if
there is something Im doing wrong by using the standard getInstance()
method.  I have deployed the class files to the standard WEB-INF/classes
directory.

Is there a trick to getting it to load?

Thanks for any help

Thomas


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Singleton in Tomcat

Posted by Thomas Polliard <th...@polliard.com>.
The problem is where someone stated earlier in the load of  
pam.conf.   So different question:

How does one either modify web.xml or read a config file from within  
the WEB-INF directory?

Thanks

Thomas
On Apr 12, 2007, at 7:45 PM, Thomas Polliard wrote:

> Ok, so if the constructor is empty it works fine, but as soon as  
> you put something in the constructor it fails.
>
> Code:
> package net.digitalassembly.auth;
>
> import java.util.ArrayList;
> import java.util.List;
> import java.util.Properties;
> import java.io.FileInputStream;
> import java.io.IOException;
> import net.digitalassembly.auth.PamModule;
> import net.digitalassembly.auth.PamHibernateModule; // May need to  
> have this in the config file to make it plugable
> import org.apache.log4j.Logger;
>
>
> public class Pam {
>         /* Pam has a list of modules that it can support
>          * There is a interface called PamModule that allows
>          * other "modules" to interact with Pam
>          * Pam should be a singleton
>          * Does PAM ONLY allow one mechanism? I think Not
>         */
>         private static Pam instance = new Pam();
>         private String pamConfigFile = "pam.conf";
>         private Properties pamProperties = new Properties();
>         private List<PamModule> pamModules = new  
> ArrayList<PamModule>();
>         private static Logger logger = Logger.getLogger(Pam.class);
>
>         private  Pam() {
>                 logger.debug("Pam private constructor called");
> 	}
>         public boolean authenticate(String username, String  
> password) {
>                 logger.debug("Running Authenticate");
>                 return false;
>         }
>
>         public static Pam getInstance() {
>                 return instance;
>         }
>
> }
>
> If I comment out the logger statement in the constructor it works  
> fine, with it there it fails...
> This appears to be something im not aware of in the way Tomcat  
> handles this. Even changing the constructor to public it fails.
>
> Any other advice would be appreciated.
>
> Thomas
>
>
>
>
>
>
> On Apr 11, 2007, at 10:36 PM, Rashmi Rubdi wrote:
>
>> I tried a smaller version of your code since I don't have the
>> dependent Classes as follows:
>>
>> --------------------------------------------------------------------- 
>> -----------
>> ........\MyProject\WEB-INF\classes\net\digitalassembly\auth\Pam.class
>> --------------------------------------------------------------------- 
>> -----------
>>
>> package net.digitalassembly.auth;
>>
>> public class Pam {
>>
>>    private static Pam instance = new Pam();
>>
>>    private Pam() {
>>    }
>>
>>    public boolean authenticate(String username, String password) {
>>        return false;
>>    }
>>
>>    public static Pam getInstance() {
>>        return instance;
>>    }
>> }
>>
>> --------------------------------------------------------------------- 
>> -------------------
>> .....\MyProject\somefolder\authenticate.jsp
>> --------------------------------------------------------------------- 
>> -------------------
>> <%@ page import="net.digitalassembly.auth.Pam" %>
>> <%
>>    Pam pam = Pam.getInstance();
>>    if (pam != null) {
>>        out.println(" pam not null ");
>>    } else {
>>        out.println("Pam failed to initialize");
>>    }
>> %>
>>
>>
>> And it printed "pam not null" on the screen.
>>
>> So it worked.
>>
>> -Rashmi
>>
>> ---------------------------------------------------------------------
>> To start a new topic, e-mail: users@tomcat.apache.org
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: unecessary JAR files

Posted by Tim Lucia <ti...@yahoo.com>.
mv + java should do the trick.  If you get a ClassNotFoundException, then
the jar file is utilized.

Tim

> -----Original Message-----
> From: Pierre Goupil [mailto:goupilpierre@gmail.com]
> Sent: Friday, April 13, 2007 11:28 AM
> To: Tomcat Users List
> Subject: Re: unecessary JAR files
> 
> Hello, World !
> 
> 
> Another question asked by the OP which is of interest to me :
> 
> does anyone have a tool that can profile whether or not a jar file gets
> utilized during an application?
> 
> 
> Cheers,
> 
> 
> Pierre
> 
> 
> 
> 2007/4/13, Peter Crowther <Pe...@melandra.com>:
> >
> > > From: Rashmi Rubdi [mailto:rashmi.sub@gmail.com]
> > > On 4/12/07, Kevin Gutch <kg...@protechemail.com> wrote:
> > > > Do unnecessary jar files affect performance of an application?
> > >
> > > Only if the classes in the JAR files are called, but then that
> > > wouldn't make the JAR file unnecessary :-) .
> >
> > There are a couple of other minor effects:
> >
> > - More jars mean more files to scan every 10 seconds if Tomcat is
> > configured to check for changed files and reload the webapp
> > automatically;
> >
> > - Class loading typically requires examining more files to load the same
> > number of classes.
> >
> > Both are typically minor - the number of extra jars is generally small
> > compared to the total number of files in the application, and class
> > loading tends to happen at or near webapp startup.  So you'll get
> > slightly slower startup, but little observable effect once loaded.
> >
> >                 - Peter
> >
> > ---------------------------------------------------------------------
> > To start a new topic, e-mail: users@tomcat.apache.org
> > To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> > For additional commands, e-mail: users-help@tomcat.apache.org
> >
> >
> 
> 
> --
> A celle qui se souvient de chaque parole prononcée,
> Du premier cri du bébé au serment du héros.
> Tu es mon témoin éternel.



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: unecessary JAR files

Posted by Pierre Goupil <go...@gmail.com>.
Hello, World !


Another question asked by the OP which is of interest to me :

does anyone have a tool that can profile whether or not a jar file gets
utilized during an application?


Cheers,


Pierre



2007/4/13, Peter Crowther <Pe...@melandra.com>:
>
> > From: Rashmi Rubdi [mailto:rashmi.sub@gmail.com]
> > On 4/12/07, Kevin Gutch <kg...@protechemail.com> wrote:
> > > Do unnecessary jar files affect performance of an application?
> >
> > Only if the classes in the JAR files are called, but then that
> > wouldn't make the JAR file unnecessary :-) .
>
> There are a couple of other minor effects:
>
> - More jars mean more files to scan every 10 seconds if Tomcat is
> configured to check for changed files and reload the webapp
> automatically;
>
> - Class loading typically requires examining more files to load the same
> number of classes.
>
> Both are typically minor - the number of extra jars is generally small
> compared to the total number of files in the application, and class
> loading tends to happen at or near webapp startup.  So you'll get
> slightly slower startup, but little observable effect once loaded.
>
>                 - Peter
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


-- 
A celle qui se souvient de chaque parole prononcée,
Du premier cri du bébé au serment du héros.
Tu es mon témoin éternel.

RE: unecessary JAR files

Posted by Peter Crowther <Pe...@melandra.com>.
> From: Rashmi Rubdi [mailto:rashmi.sub@gmail.com] 
> On 4/12/07, Kevin Gutch <kg...@protechemail.com> wrote:
> > Do unnecessary jar files affect performance of an application?
> 
> Only if the classes in the JAR files are called, but then that
> wouldn't make the JAR file unnecessary :-) .

There are a couple of other minor effects:

- More jars mean more files to scan every 10 seconds if Tomcat is
configured to check for changed files and reload the webapp
automatically;

- Class loading typically requires examining more files to load the same
number of classes.

Both are typically minor - the number of extra jars is generally small
compared to the total number of files in the application, and class
loading tends to happen at or near webapp startup.  So you'll get
slightly slower startup, but little observable effect once loaded.

		- Peter

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: unecessary JAR files

Posted by Rashmi Rubdi <ra...@gmail.com>.
On 4/12/07, Kevin Gutch <kg...@protechemail.com> wrote:
> Hi,
>
> Do unnecessary jar files affect performance of an application?

Only if the classes in the JAR files are called, but then that
wouldn't make the JAR file unnecessary :-) .

-Rashmi

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


unecessary JAR files

Posted by Kevin Gutch <kg...@protechemail.com>.
Hi,

Do unnecessary jar files affect performance of an application? If so 
does anyone have a tool that can profile whether or not a jar file gets 
utilized during an application?

Thanks

-Kevin

Re: Singleton in Tomcat

Posted by Thomas Polliard <th...@polliard.com>.
Ok, so if the constructor is empty it works fine, but as soon as you  
put something in the constructor it fails.

Code:
package net.digitalassembly.auth;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
import net.digitalassembly.auth.PamModule;
import net.digitalassembly.auth.PamHibernateModule; // May need to  
have this in the config file to make it plugable
import org.apache.log4j.Logger;


public class Pam {
         /* Pam has a list of modules that it can support
          * There is a interface called PamModule that allows
          * other "modules" to interact with Pam
          * Pam should be a singleton
          * Does PAM ONLY allow one mechanism? I think Not
         */
         private static Pam instance = new Pam();
         private String pamConfigFile = "pam.conf";
         private Properties pamProperties = new Properties();
         private List<PamModule> pamModules = new ArrayList<PamModule> 
();
         private static Logger logger = Logger.getLogger(Pam.class);

         private  Pam() {
                 logger.debug("Pam private constructor called");
	}
         public boolean authenticate(String username, String password) {
                 logger.debug("Running Authenticate");
                 return false;
         }

         public static Pam getInstance() {
                 return instance;
         }

}

If I comment out the logger statement in the constructor it works  
fine, with it there it fails...
This appears to be something im not aware of in the way Tomcat  
handles this. Even changing the constructor to public it fails.

Any other advice would be appreciated.

Thomas






On Apr 11, 2007, at 10:36 PM, Rashmi Rubdi wrote:

> I tried a smaller version of your code since I don't have the
> dependent Classes as follows:
>
> ---------------------------------------------------------------------- 
> ----------
> ........\MyProject\WEB-INF\classes\net\digitalassembly\auth\Pam.class
> ---------------------------------------------------------------------- 
> ----------
>
> package net.digitalassembly.auth;
>
> public class Pam {
>
>    private static Pam instance = new Pam();
>
>    private Pam() {
>    }
>
>    public boolean authenticate(String username, String password) {
>        return false;
>    }
>
>    public static Pam getInstance() {
>        return instance;
>    }
> }
>
> ---------------------------------------------------------------------- 
> ------------------
> .....\MyProject\somefolder\authenticate.jsp
> ---------------------------------------------------------------------- 
> ------------------
> <%@ page import="net.digitalassembly.auth.Pam" %>
> <%
>    Pam pam = Pam.getInstance();
>    if (pam != null) {
>        out.println(" pam not null ");
>    } else {
>        out.println("Pam failed to initialize");
>    }
> %>
>
>
> And it printed "pam not null" on the screen.
>
> So it worked.
>
> -Rashmi
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Singleton in Tomcat

Posted by Rashmi Rubdi <ra...@gmail.com>.
I tried a smaller version of your code since I don't have the
dependent Classes as follows:

--------------------------------------------------------------------------------
........\MyProject\WEB-INF\classes\net\digitalassembly\auth\Pam.class
--------------------------------------------------------------------------------

package net.digitalassembly.auth;

public class Pam {

    private static Pam instance = new Pam();

    private Pam() {
    }

    public boolean authenticate(String username, String password) {
        return false;
    }

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

----------------------------------------------------------------------------------------
.....\MyProject\somefolder\authenticate.jsp
----------------------------------------------------------------------------------------
<%@ page import="net.digitalassembly.auth.Pam" %>
<%
    Pam pam = Pam.getInstance();
    if (pam != null) {
        out.println(" pam not null ");
    } else {
        out.println("Pam failed to initialize");
    }
%>


And it printed "pam not null" on the screen.

So it worked.

-Rashmi

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Singleton in Tomcat

Posted by Rashmi Rubdi <ra...@gmail.com>.
It looks like the problem is in this snippet of code:

On 4/11/07, Thomas Polliard <th...@polliard.com> wrote:

Check if you placed pam.conf under /WEB-INF/classes/ --- I think
that's where Properties files are loaded from.

>         private String pamConfigFile = "pam.conf"; // Static file for all modules

I think it is unable to locate the pam.conf, so it's probably throwing
File not found --- and setting pam to null as here:

>
>         private Pam() {
>                 try {
>                         pamProperties.load(new FileInputStream(pamConfigFile));
>                 } catch (IOException ioe) {
>                         System.out.println("File not found");
>                         this.instance = null;
>                 }

Or perhaps some other class is not being loaded.

-Rashmi

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Singleton in Tomcat

Posted by Thomas Polliard <th...@polliard.com>.
Calendar example worked (with page import java.util.Calendar) :)


This is the Pam.java class the class compiles to
WEB-INF/classes/net/digitalassembly/auth/Pam.class

package net.digitalassembly.auth;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
import net.digitalassembly.auth.PamModule;
import net.digitalassembly.auth.PamHibernateModule; // May need to have this in the config file to make it plugable

public class Pam {
	/* Pam has a list of modules that it can support
	 * There is a interface called PamModule that allows
	 * other "modules" to interact with Pam
	 * Pam should be a singleton
	 * Does PAM ONLY allow one mechanism? I think Not
	*/
	private static Pam instance = new Pam();
        private String pamConfigFile = "pam.conf"; // Static file for all modules
	private List<PamModule> pamModules = new ArrayList();
        private Properties pamProperties = new Properties();
	
	private Pam() {
		try {
        		pamProperties.load(new FileInputStream(pamConfigFile));
		} catch (IOException ioe) {
			System.out.println("File not found");
			this.instance = null;
		}
		System.out.println(pamProperties.getProperty("pam.hibernate.plugin"));
		if (pamProperties.getProperty("pam.hibernate.plugin").equalsIgnoreCase("true")) {
			pamModules.add(PamHibernateModule.getInstance());
		}
		if (pamProperties.getProperty("pam.db.plugin").equalsIgnoreCase("true")) {
			pamModules.add(PamHibernateModule.getInstance());
		}
		if (pamProperties.getProperty("pam.ldap.plugin").equalsIgnoreCase("true")) {
			pamModules.add(PamHibernateModule.getInstance());
		}
		if (pamProperties.getProperty("pam.file.plugin").equalsIgnoreCase("true")) {
			pamModules.add(PamHibernateModule.getInstance());
		}
	}

	public boolean authenticate(String username, String password) {
		System.out.println("Running Authenticate");
		for (PamModule module: pamModules) {
			System.out.println(module);
			boolean pass = false;
			pass = module.authenticate(username,password);	
			if (pass == true) {
				return true;
			}
		}
		return false;
	}
	public static Pam getInstance() {
		return instance;
	}
}


The file that is trying to use it is authenticate.jsp which contains the
following:

<%@ page import="net.digitalassembly.auth.Pam" %>
<%
	Pam pam = Pam.getInstance();
	if (pam != null) {
        	if (pam.authenticate(request.getParameter("username"),request.getParameter("password"))) {
                	session.setAttribute("validUser",request.getParameter("username"));      
        	}
		response.sendRedirect("/CharacterForge/forge/index.jsp");
	} else {
		out.println("Pam failed to initialize");
	}
%>
*
The error I get is:*

org.apache.jasper.JasperException: Exception in JSP: /authenticate.jsp:3

1: <%@ page import="net.digitalassembly.auth.Pam" %>
2: <%
3: 	Pam pam = Pam.getInstance();
4: 	if (pam != null) {
5:         	if (pam.authenticate(request.getParameter("username"),request.getParameter("password"))) {
6:                 	session.setAttribute("validUser",request.getParameter("username"));      


Stacktrace:
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:506)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

*root cause*

javax.servlet.ServletException
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:843)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:776)
org.apache.jsp.authenticate_jsp._jspService(authenticate_jsp.java:63)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

*root cause*

java.lang.NoClassDefFoundError
org.apache.jsp.authenticate_jsp._jspService(authenticate_jsp.java:46)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


Any help would be appreciated.  I have other classes (not implementing singleton pattern) that don't have any issues.
Hence why I was thinking it was a singleton/tomcat issue I was causing.

Thanks

Thomas



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Singleton in Tomcat

Posted by Rashmi Rubdi <ra...@gmail.com>.
There's no trick to getting it to load.

There must be something wrong in the class, like incorrect package
name or incorrect package directory structure etc.

Try this small test,

<%
Calendar calendar = Calendar.getInstance();
%>
<%=calendar.getFirstDayOfWeek()%>

In the Pam class check if the package is defined and the class is
located under proper folder structure under /WEB-INF/classes/ .

-Rashmi

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org