View Javadoc

1   /**********************************************************************
2    * ClassHandler.java
3    * created on 14.08.2004 by netseeker
4    * $Source: /cvsroot/ejoe/EJOE/src/de/netseeker/ejoe/handler/ClassHandler.java,v $
5    * $Date: 2007/11/17 10:57:00 $
6    * $Revision: 1.17 $
7    *
8    * ====================================================================
9    *
10   *  Copyright 2005-2006 netseeker aka Michael Manske
11   *
12   *  Licensed under the Apache License, Version 2.0 (the "License");
13   *  you may not use this file except in compliance with the License.
14   *  You may obtain a copy of the License at
15   *
16   *      http://www.apache.org/licenses/LICENSE-2.0
17   *
18   *  Unless required by applicable law or agreed to in writing, software
19   *  distributed under the License is distributed on an "AS IS" BASIS,
20   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   *  See the License for the specific language governing permissions and
22   *  limitations under the License.
23   * ====================================================================
24   *
25   * This file is part of the ejoe framework.
26   * For more information on the author, please see
27   * <http://www.manskes.de/>.
28   *
29   *********************************************************************/
30  
31  package de.netseeker.ejoe.handler;
32  
33  import java.io.ByteArrayOutputStream;
34  import java.io.IOException;
35  import java.io.InputStream;
36  import java.util.Map;
37  import java.util.logging.Level;
38  import java.util.logging.Logger;
39  
40  import de.netseeker.ejoe.EJConstants;
41  import de.netseeker.ejoe.cache.LRUMap;
42  import de.netseeker.ejoe.io.IOUtil;
43  
44  /***
45   * A ServerHandler which handles class loader requests. It reads the .class file of the requested class and returns the
46   * content as an ByteArray. A cache is used to store the last thousand returned classes.
47   * 
48   * @author netseeker aka Michael Manske
49   * @since 0.3.0
50   */
51  public final class ClassHandler implements ServerHandler
52  {
53      /***
54       * 
55       */
56      private static final long serialVersionUID = 1L;
57  
58      private static final Logger log         = Logger.getLogger( ClassHandler.class.getName() );
59  
60      private static final Map    _classCache = new LRUMap();
61  
62      /*
63       * (non-Javadoc)
64       * 
65       * @see de.netseeker.ejoe.ServerHandler#handle(java.lang.Object)
66       */
67      public Object handle( Object obj )
68      {
69          byte[] buf = (byte[]) _classCache.get( obj );
70  
71          if ( buf == null )
72          {
73              InputStream input = null;
74  
75              try
76              {
77                  String name = ((String) obj).replaceAll( "//.", "/" );
78                  input = ClassHandler.class.getResourceAsStream( '/' + name + ".class" );
79                  if ( input != null )
80                  {
81                      ByteArrayOutputStream bos = new ByteArrayOutputStream( EJConstants.BUFFERED_STREAM_SIZE );
82                      byte[] buffer = new byte[EJConstants.BUFFERED_STREAM_SIZE];
83                      int count = 0;
84                      int n = 0;
85                      while ( -1 != (n = input.read( buffer )) )
86                      {
87                          bos.write( buffer, 0, n );
88                          count += n;
89                      }
90  
91                      buf = bos.toByteArray();
92                      _classCache.put( obj, buf );
93                  }
94                  else
95                  {
96                      throw new RuntimeException( "Class " + obj + "not found." );
97                  }
98              }
99              catch ( IOException e )
100             {
101                 log.log( Level.SEVERE, "IOException occured while serializing class " + obj + '.', e );
102                 throw new RuntimeException( e );
103             }
104             finally
105             {
106                 IOUtil.closeQuiet( input );
107             }
108         }
109 
110         return buf;
111     }
112 }