1   //========================================================================
2   //Copyright 2004-2008 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //Licensed under the Apache License, Version 2.0 (the "License");
5   //you may not use this file except in compliance with the License.
6   //You may obtain a copy of the License at 
7   //http://www.apache.org/licenses/LICENSE-2.0
8   //Unless required by applicable law or agreed to in writing, software
9   //distributed under the License is distributed on an "AS IS" BASIS,
10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  //See the License for the specific language governing permissions and
12  //limitations under the License.
13  //========================================================================
14  
15  /**
16   * 
17   */
18  package org.mortbay.jetty.servlet;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import org.mortbay.io.Buffer;
25  import org.mortbay.io.nio.DirectNIOBuffer;
26  import org.mortbay.io.nio.IndirectNIOBuffer;
27  import org.mortbay.io.nio.NIOBuffer;
28  import org.mortbay.jetty.Connector;
29  import org.mortbay.jetty.HttpConnection;
30  import org.mortbay.jetty.MimeTypes;
31  import org.mortbay.jetty.ResourceCache;
32  import org.mortbay.jetty.nio.NIOConnector;
33  import org.mortbay.log.Log;
34  import org.mortbay.resource.Resource;
35  
36  class NIOResourceCache extends ResourceCache
37  {
38      boolean _useFileMappedBuffer;
39      
40      /* ------------------------------------------------------------ */
41      public NIOResourceCache(MimeTypes mimeTypes)
42      {
43          super(mimeTypes);
44      }
45  
46      /* ------------------------------------------------------------ */
47      protected void fill(Content content) throws IOException
48      {
49          Buffer buffer=null;
50          Resource resource=content.getResource();
51          long length=resource.length();
52  
53          if (_useFileMappedBuffer && resource.getFile()!=null) 
54          {    
55              File file = resource.getFile();
56              if (file != null) 
57  	        buffer = new DirectNIOBuffer(file);
58          } 
59          else 
60          {
61              InputStream is = resource.getInputStream();
62              try
63              {
64                  Connector connector = HttpConnection.getCurrentConnection().getConnector();
65  		 buffer = ((NIOConnector)connector).getUseDirectBuffers()?
66  			 (NIOBuffer)new DirectNIOBuffer((int)length):
67  			 (NIOBuffer)new IndirectNIOBuffer((int)length);
68              }
69              catch(OutOfMemoryError e)
70              {
71                  Log.warn(e.toString());
72                  Log.debug(e);
73  		buffer = new IndirectNIOBuffer((int) length);
74              }
75              buffer.readFrom(is,(int)length);
76              is.close();
77          }
78          content.setBuffer(buffer);
79      }
80  
81      public boolean isUseFileMappedBuffer()
82      {
83          return _useFileMappedBuffer;
84      }
85  
86      public void setUseFileMappedBuffer(boolean useFileMappedBuffer)
87      {
88          _useFileMappedBuffer = useFileMappedBuffer;
89      }
90  }