You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Daniel Watrous <dw...@gmail.com> on 2005/05/06 18:52:03 UTC

dynamic reference to files on server

Hello,

I am writing an application which needs to load font files.  The font
files are deployed with other classes on the server (i.e. under the
WEB-INF/classes directory).   Currently I have the reference built in
like this

    //private static final String pathToWebapp =
"/var/tomcat/jakarta-tomcat-5.0.25/webapps/words2walls";
    private static final String pathToWebapp = "C:\\Program
Files\\Apache Software Foundation\\Tomcat 5.0\\webapps\\words2walls";
    //private static final String pathToPackage =
"/WEB-INF/classes/com/words2walls/fonts/";
    private static final String pathToPackage =
"\\WEB-INF\\classes\\com\\words2walls\\fonts\\";

and I use the above variables as follows:

            File fontFile = new File (pathToWebapp+pathToPackage+filename);
            FileInputStream fis = new FileInputStream(fontFile);
            font = Font.createFont(Font.TRUETYPE_FONT, fis);

I tried unsuccessfully to use the class loader, but this might just be
my ineptitude.  Can someone suggest a way for me to reference these
font files dynamically so that I can more easily deploy it in
different locations?  Thanks!

Daniel

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: dynamic reference to files on server

Posted by Daniel Watrous <dw...@gmail.com>.
I'm sending my solution to my original post:

// Here I get the ClassLoader specific to this web application
java.lang.ClassLoader loader =
com.words2walls.customquote.QuoteFontType.class.getClassLoader();
// This is the package specific reference to the font (or resource)
String pathToFontFile = "com/words2walls/fonts/" + filename;
// use the loader and the file reference to retrieve an InputStream to
the specific font
java.io.InputStream fis = loader.getResourceAsStream(pathToFontFile);
// Use the InputStream to create the Font object
this.font = Font.createFont(Font.TRUETYPE_FONT, fis);

Below is additional explanation:
The static method Font.createFont(Font.TRUETYPE_FONT, inputStream)
takes a constant Font.TRUETYPE_FONT and a java.io.InputStream.  My
trouble was
obtaining the input stream.  In my example in the first e-mail you
might notice that my initial work around was to create a File object
using the absolute path to my font file:
          File fontFile = new File (pathToWebapp+pathToPackage+filename);

This is why I had drive information hard coded in to my first example.
This was not desirable since This would change every time I deployed
this app to a new server.  I then used the File object to create a
FileInputStream to then create my Font object:
          FileInputStream fis = new FileInputStream(fontFile);
          font = Font.createFont(Font.TRUETYPE_FONT, fis);

So what I wanted was to load a file using the ClassLoader local to
this application.  Since the fonts are distributed with the
application There is no more need for generalization.  The tomcat
documentation was helpful to me
(http://localhost:8080/tomcat-docs/class-loader-howto.html) since it
showed that there are multiple ClassLoaders corresponding to different
tiers in tomcat:

     Bootstrap
         |
      System
         |
      Common
     /      \
Catalina   Shared
               /   \
       Webapp1  Webapp2 ...

This being the case I knew that if I could get the classloader
associated with the QuoteFontType class then this, being in the same
application, would enable be to load the fonts which were also
packaged in this application:
com.words2walls.customquote.QuoteFontType
com.words2walls.fonts.{all_font_files}

So you see my trouble was not in finding the classpath as much as it
was finding some mechanism, local to my application, that would permit
me to obtain an InputStream to fonts stored within the same
application.  As far as portability I'm not sure that other servlet
containers provide ClassLoaders following the same structure.
However, it is likely that they do since the functionality described
follows the "Servlet Specification, version 2.4 -- in particular,
Sections 9.4 and 9.6.".

Daniel

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: dynamic reference to files on server

Posted by Dakota Jack <da...@gmail.com>.
Your code looks pretty good.  What is the trouble?  Do you have
something like the following?


      fontStream = new FileInputStream(file);
      font = Font.createFont(Font.TRUETYPE_FONT,fontStream);
      font = font.deriveFont(attributes);

where attributes sets the logical and family font names?

On 5/6/05, Daniel Watrous <dw...@gmail.com> wrote:
> Hello,
> 
> I am writing an application which needs to load font files.  The font
> files are deployed with other classes on the server (i.e. under the
> WEB-INF/classes directory).   Currently I have the reference built in
> like this
> 
>     //private static final String pathToWebapp =
> "/var/tomcat/jakarta-tomcat-5.0.25/webapps/words2walls";
>     private static final String pathToWebapp = "C:\\Program
> Files\\Apache Software Foundation\\Tomcat 5.0\\webapps\\words2walls";
>     //private static final String pathToPackage =
> "/WEB-INF/classes/com/words2walls/fonts/";
>     private static final String pathToPackage =
> "\\WEB-INF\\classes\\com\\words2walls\\fonts\\";
> 
> and I use the above variables as follows:
> 
>             File fontFile = new File (pathToWebapp+pathToPackage+filename);
>             FileInputStream fis = new FileInputStream(fontFile);
>             font = Font.createFont(Font.TRUETYPE_FONT, fis);
> 
> I tried unsuccessfully to use the class loader, but this might just be
> my ineptitude.  Can someone suggest a way for me to reference these
> font files dynamically so that I can more easily deploy it in
> different locations?  Thanks!
> 
> Daniel
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
> 


-- 
"You can lead a horse to water but you cannot make it float on its back."
~Dakota Jack~

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org