RE: Taglib loading classes of web application
Oh, that works great! Thanks Yoav and Dov! -Mikee [EMAIL PROTECTED] 10/22/2004 9:54:57 AM Hi, Yeah, using instanceof or isAssignableFrom are much better approaches for this. You cannot simply trim the [L from an array class name and get a valid class name: that's not guaranteed by the JLS and it's cruising for a bruising, as you've already found out. Yoav Shapira http://www.yoavshapira.com -Original Message- From: Dov Rosenberg [mailto:[EMAIL PROTECTED] Sent: Friday, October 22, 2004 9:52 AM To: Tomcat Users List Subject: Re: Taglib loading classes of web application Instead of checking the type of class by doing getClass().getName() I would recommend using If (myclass instanceof com.name.webapp.entities.MetaDataEntity){ ... } else { ... } This is better because you can check the presence of a class at compile time instead of runtime when you do things like Class.forName(className) -- Dov Rosenberg Conviveon Corporation http://www.conviveon.com On 10/21/04 7:49 PM, Michael Wille [EMAIL PROTECTED] wrote: Hello all, I have a strange question. I need to load a class that is inside a packkage of a web application from within a taglib that the web application is using within its context. Let me try and explain this: I have a bunch of entities in the package: com.name.webapp.entities.. In this package, there are two base classes: Entity and MetaDataEntity. There are quite a few entity classes that are derived from these two clases. I've setup a taglib to take an array of either Entity or MetaDataEntity from the request's attribute map and loop through the array do some processing. The problem I have is that different processing needs to be done if the array is of a type derived from Entity or derived from MetaDataEntity. To do this, I get the first element in the array and get that object's superclass. So the code I have in the taglib to discover the super class is: Object entities[] = (Object []) pageContext.getRequest().getAttribute(list); Object entity = entities[0]; className = entity.getClass().getSuperclass().getName(); if(className.startsWith(com.name.webapp.entities.Entity)) // do one thing... else if(className.startsWith(com.name.webapp.entities.MetaDataEntity)) // do another... else // throw exception. (realizing that this will only work with direct descendants of the 2 base classes) Which works fine if the array is not empty. However, when entities.length == 0, I have to use a different method: Object entities[] = (Object []) pageContext.getRequest().getAttribute(list); // class name from the array is [Lcom.name.webapp.entities.entityname, get rid of the [L... className = entities.getClass().getName().substring(2); className = Class.forName(className).getSuperclass().getName(); However, I get a ClassNotFoundException. I believe that is because the taglib is using a different class loader than my webapp, which makes sense.. But is there any other way (short of creating my own class loader for the webapp and accessing that loader from the taglib) to do this? I'd rather avoid a custom classloader at all costs. Thanks for any advice. -Mike - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] This e-mail, including any attachments, is a confidential business communication, and may contain information that is confidential, proprietary and/or privileged. This e-mail is intended only for the individual(s) to whom it is addressed, and may not be saved, copied, printed, disclosed or used by anyone else. If you are not the(an) intended recipient, please immediately delete this e-mail from your computer system and notify the sender. Thank you. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Taglib loading classes of web application
Hello all, I have a strange question. I need to load a class that is inside a packkage of a web application from within a taglib that the web application is using within its context. Let me try and explain this: I have a bunch of entities in the package: com.name.webapp.entities. In this package, there are two base classes: Entity and MetaDataEntity. There are quite a few entity classes that are derived from these two clases. I've setup a taglib to take an array of either Entity or MetaDataEntity from the request's attribute map and loop through the array do some processing. The problem I have is that different processing needs to be done if the array is of a type derived from Entity or derived from MetaDataEntity. To do this, I get the first element in the array and get that object's superclass. So the code I have in the taglib to discover the super class is: Object entities[] = (Object []) pageContext.getRequest().getAttribute(list); Object entity = entities[0]; className = entity.getClass().getSuperclass().getName(); if(className.startsWith(com.name.webapp.entities.Entity)) // do one thing... else if(className.startsWith(com.name.webapp.entities.MetaDataEntity)) // do another... else // throw exception. (realizing that this will only work with direct descendants of the 2 base classes) Which works fine if the array is not empty. However, when entities.length == 0, I have to use a different method: Object entities[] = (Object []) pageContext.getRequest().getAttribute(list); // class name from the array is [Lcom.name.webapp.entities.entityname, get rid of the [L... className = entities.getClass().getName().substring(2); className = Class.forName(className).getSuperclass().getName(); However, I get a ClassNotFoundException. I believe that is because the taglib is using a different class loader than my webapp, which makes sense. But is there any other way (short of creating my own class loader for the webapp and accessing that loader from the taglib) to do this? I'd rather avoid a custom classloader at all costs. Thanks for any advice. -Mike
Subclassing WebdavServlet
Hello All, I would like to know if its possible to subclass the webdav servlet that comes with Tomcat 5.x. We have need of webdav in our application, but need to provide a different JNDI InitialContext then the file system. The thought was we could just override the getResources() method in WebdavServlet to return our own context. What follows is a description of what we are doing: It seems there is at least one obstacle to doing this. We would have to move/copy the jar files: servlets-default.jar, servlets-common.jar, and servlets-webdav.jar to the common/lib folder. It seemed when we attempted to move the files to common/lib, all of the default webapps couldn't be loaded as well as some of our application servlets. When we leave the servlets*.jar files in both server/lib and common/lib, an exception is thrown when accessing our servlet: javax.servlet.ServletException: Error instantiating servlet class com.test.webdav.XDav And this is caused by: java.lang.NoClassDefFoundError: org.apache.catalina.servlets.DefaultServlet This seems strange considering the app should have access to the servlets-default.jar now that it is in common. Or is there an issue with libs in both server and common? Any help is hugely appreciated. Thanks! -Mike Wille Our subclassed servlet (with only test code for the InitialContext): package com.test.webdav; import javax.servlet.*; import javax.servlet.http.*; import javax.naming.directory.InitialDirContext; import javax.naming.directory.DirContext; import org.apache.catalina.servlets.*; public class XDav extends WebdavServlet { // need to dup this here because these have // private access in apaches webdavservlet protected static final String METHOD_HEAD = HEAD; protected static final String METHOD_PROPFIND = PROPFIND; protected static final String METHOD_PROPPATCH = PROPPATCH; protected static final String METHOD_MKCOL = MKCOL; protected static final String METHOD_COPY = COPY; protected static final String METHOD_MOVE = MOVE; protected static final String METHOD_LOCK = LOCK; protected static final String METHOD_UNLOCK = UNLOCK; protected Logger logger; public void init (ServletConfig config) throws ServletException { // wake your parents super.init(config); } public void destroy() { // kill your parents super.destroy(); } /** * Get resources. This method will try to retrieve the resources through * JNDI first, then in the servlet context if JNDI has failed (it could be * disabled). It will return null. * * @return A JNDI DirContext, or null. */ protected DirContext getResources() { InitialDirContext result = null; // Try the servlet context try { //result = new IntitialLibraryContext(); result.bind(directory 1, directory 1); result.bind(directory 2, directory 2); result.bind(directory 3, directory 3); result.bind(directory 4, directory 4); result.bind(directory 3/subdirectory 5, subdirectory 5); } catch(Exception e) { logger.warning(UtilityTank.getErrorDetail(e)); } return result; } }
Re: Subclassing WebdavServlet
Just on a whim I copied everything beneath server/lib to common/lib and restarted. That worked! So including servlets*.jar was not enough. Thanks, -Mike Wille On Jul 26, 2004, at 2:38 PM, Shapira, Yoav wrote: Hi, You can subclass it, should be fine. Yoav Shapira Millennium Research Informatics -Original Message- From: Michael Wille [mailto:[EMAIL PROTECTED] Sent: Monday, July 26, 2004 2:26 PM To: [EMAIL PROTECTED] Subject: Subclassing WebdavServlet Hello All, I would like to know if its possible to subclass the webdav servlet that comes with Tomcat 5.x. We have need of webdav in our application, but need to provide a different JNDI InitialContext then the file system. The thought was we could just override the getResources() method in WebdavServlet to return our own context. What follows is a description of what we are doing: It seems there is at least one obstacle to doing this. We would have to move/copy the jar files: servlets-default.jar, servlets-common.jar, and servlets-webdav.jar to the common/lib folder. It seemed when we attempted to move the files to common/lib, all of the default webapps couldn't be loaded as well as some of our application servlets. When we leave the servlets*.jar files in both server/lib and common/lib, an exception is thrown when accessing our servlet: javax.servlet.ServletException: Error instantiating servlet class com.test.webdav.XDav And this is caused by: java.lang.NoClassDefFoundError: org.apache.catalina.servlets.DefaultServlet This seems strange considering the app should have access to the servlets-default.jar now that it is in common. Or is there an issue with libs in both server and common? Any help is hugely appreciated. Thanks! -Mike Wille Our subclassed servlet (with only test code for the InitialContext): package com.test.webdav; import javax.servlet.*; import javax.servlet.http.*; import javax.naming.directory.InitialDirContext; import javax.naming.directory.DirContext; import org.apache.catalina.servlets.*; public class XDav extends WebdavServlet { // need to dup this here because these have // private access in apaches webdavservlet protected static final String METHOD_HEAD = HEAD; protected static final String METHOD_PROPFIND = PROPFIND; protected static final String METHOD_PROPPATCH = PROPPATCH; protected static final String METHOD_MKCOL = MKCOL; protected static final String METHOD_COPY = COPY; protected static final String METHOD_MOVE = MOVE; protected static final String METHOD_LOCK = LOCK; protected static final String METHOD_UNLOCK = UNLOCK; protected Logger logger; public void init (ServletConfig config) throws ServletException { // wake your parents super.init(config); } public void destroy() { // kill your parents super.destroy(); } /** * Get resources. This method will try to retrieve the resources through * JNDI first, then in the servlet context if JNDI has failed (it could be * disabled). It will return null. * * @return A JNDI DirContext, or null. */ protected DirContext getResources() { InitialDirContext result = null; // Try the servlet context try { //result = new IntitialLibraryContext(); result.bind(directory 1, directory 1); result.bind(directory 2, directory 2); result.bind(directory 3, directory 3); result.bind(directory 4, directory 4); result.bind(directory 3/subdirectory 5, subdirectory 5); } catch(Exception e) { logger.warning(UtilityTank.getErrorDetail(e)); } return result; } } This e-mail, including any attachments, is a confidential business communication, and may contain information that is confidential, proprietary and/or privileged. This e-mail is intended only for the individual(s) to whom it is addressed, and may not be saved, copied, printed, disclosed or used by anyone else. If you are not the(an) intended recipient, please immediately delete this e-mail from your computer system and notify the sender. Thank you. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Client prematurely closes connection = junk in reponse
[EMAIL PROTECTED] 07/09/03 10:44PM See below... On Wed, 2003-07-09 at 15:10, Michael Wille wrote: Hello All, byte[] buffer = new byte[1024]; int numBytes = 0; int totalBytes = 0; while(totalBytes fileLength) { numBytes = fis.read(buffer); totalBytes += numBytes; outStream.write(buffer); } You don't want to write the whole buffer, just what was read. Try write(buffer, 0, numBytes ) I've made that change you suggested, but I doubt that would cause the problem we are seeing. Wouldn't it simply corrupt the file itself? Here is a screenshot of the junk response we get: http://phatness.com/~kmw/error.gif In it, you can see the garbage being sent to the client, then the actual HTTP headers. Thanks, Mike
Client prematurely closes connection = junk in reponse
Hello All, I am experiencing a very strange problem and don't know exactly how to fix it. The problem is garbled junk characters are being prepended to the response output stream that the browser receives. This only happens for Internet Explorer and it seems very random. It is very difficult to reproduce. I've never seen it happen under Mozilla. Before I launch into my questions, let me explain our setup. We have Tomcat 4.1.12 behind Apache 1.3.27. Running on tomcat is a custom application. Part of what it does is maintain a library of digital assets. These assets can be movies, pdfs, images, etc. When browsing the library of assets, you have several options: view thumbnail, view fullsize, download, etc. While all static images used in the web pages are served up by Apache, all of these assets are served by a special 'proxy' servlet in tomcat. This is done primarily for security. All access to this restricted material is monitored via this proxy servlet. The code for the proxy is basically: File file = new File(filePath); FileInputStream fis = new FileInputStream(file); ServletOutputStream outStream = response.getOutputStream(); long fileLength = file.length(); response.setContentLength((int) fileLength); response.addHeader(Pragma, no-cache); if(action.equals(download)) { // We need to set the attachment header that only some browsers listen to. (the good ones) response.setHeader(Content-Disposition,attachment; filename= + fileName + ;); // Then because some browsers don't follow standards (IE) we have to set the mime type as a bogus value response.setContentType(doner/x-fileshare-native); } else { // if we are just viewing, let the browser know the correct mime type. response.setContentType(asset.getMimeType()); } byte[] buffer = new byte[1024]; int numBytes = 0; int totalBytes = 0; while(totalBytes fileLength) { numBytes = fis.read(buffer); totalBytes += numBytes; outStream.write(buffer); } fis.read(buffer); fis.close(); The reason I post this code is because I believe that the garbled junk is the remains of an aborted file download. My guess is that sometime during the while loop that writes the contents of the file to the stream, the user opened another page (possibly without the browser closing the connection?). This problem is reproducible (though not very easily) when a large file is opened for viewing or download. While the transfer is occuring, click on another link. 1 out of 3 times you see the garbled junk. Sometimes it happens more often, sometimes less. So, am I going off in the wrong direction with this? If not, is there anyway to check to see if the connection was closed or aborted during servlet processing? Does anyone have any ideas or thoughts about this? Thanks for any help! -Mike