View Javadoc

1   /*
2    * Copyright (C) The MetaClass Group. All rights reserved.
3    *
4    * This software is published under the terms of the Spice
5    * Software License version 1.1, a copy of which has been included
6    * with this distribution in the LICENSE.txt file.
7    */
8   package org.codehaus.metaclass.introspector;
9   
10  import java.io.File;
11  import java.io.InputStream;
12  import org.codehaus.metaclass.io.MetaClassIO;
13  import org.codehaus.metaclass.io.MetaClassIOASM;
14  import org.codehaus.metaclass.io.MetaClassIOBinary;
15  import org.codehaus.metaclass.io.MetaClassIOXml;
16  import org.codehaus.metaclass.model.ClassDescriptor;
17  
18  /***
19   * This is the default mechanism for loading ClassDescriptor objects. This class
20   * follows the following steps to locate the ClassDescriptor.
21   * 
22   * <ul> <li>Look for a file with extension "-meta.binary" sitting side-by-side
23   * the class file. ie If class is named com.biz.Foo it has a class file named
24   * <tt>com/biz/Foo.class</tt> and an descriptor file named
25   * <tt>com/biz/Foo-meta.binary</tt>. Assume the descriptor is in binary
26   * format.</li> <li>Look for a file with extension "-meta.xml" sitting
27   * side-by-side the class file. ie If class is named com.biz.Foo it has a class
28   * file named <tt>com/biz/Foo.class</tt> and an descriptor file named
29   * <tt>com/biz/Foo-meta.xml</tt>. Assume the descriptor is in xml format.</li>
30   * <li>If unable to locate descriptor throw a <tt>MetaClassException</tt>.</li>
31   * </ul>
32   * 
33   * @author Peter Donald
34   * @version $Revision: 1.12 $ $Date: 2004/01/16 00:55:41 $
35   */
36  public class DefaultMetaClassAccessor
37      implements MetaClassAccessor
38  {
39      /*** Class based IO. May be null if the ASM toolkit is not in classloader. */
40      private static final MetaClassIO ASM_IO = getMetaClassIOASM();
41  
42      /***
43       * @see MetaClassAccessor#getClassDescriptor
44       */
45      public ClassDescriptor
46          getClassDescriptor( final String classname,
47                              final ClassLoader classLoader,
48                              final MetaClassAccessor accessor )
49          throws MetaClassException
50      {
51          ClassDescriptor descriptor;
52  
53          descriptor = loadClassDescriptor( MetaClassIOXml.IO,
54                                            classname,
55                                            classLoader );
56          if( null != descriptor )
57          {
58              return descriptor;
59          }
60          descriptor = loadClassDescriptor( MetaClassIOBinary.IO,
61                                            classname,
62                                            classLoader );
63          if( null != descriptor )
64          {
65              return descriptor;
66          }
67  
68          if( null != ASM_IO )
69          {
70              descriptor = loadClassDescriptor( ASM_IO,
71                                                classname,
72                                                classLoader );
73              if( null != descriptor )
74              {
75                  return descriptor;
76              }
77          }
78  
79          final String message =
80              "Missing Attributes for " + classname;
81          throw new MetaClassException( message );
82      }
83  
84      /***
85       * Attempt to load ClassDescriptor using specified MetaClassIOXml.
86       * 
87       * @param io the MetaClassIOXml
88       * @param classname the classname of class
89       * @param classLoader the classloader to load resources from
90       * @return the ClassDescriptor if descriptor is found, else null
91       * @throws MetaClassException if error loading descriptor
92       */
93      private ClassDescriptor loadClassDescriptor( final MetaClassIO io,
94                                                   final String classname,
95                                                   final ClassLoader classLoader )
96          throws MetaClassException
97      {
98          final String resourceName =
99              io.getResourceName( classname ).
100             replace( File.separatorChar, '/' );
101         final InputStream inputStream =
102             classLoader.getResourceAsStream( resourceName );
103         if( null == inputStream )
104         {
105             return null;
106         }
107         try
108         {
109             return io.deserializeClass( inputStream );
110         }
111         catch( final Exception e )
112         {
113             final String message =
114                 "Unable to load Attributes for " + classname;
115             throw new MetaClassException( message, e );
116         }
117     }
118 
119     /***
120      * Return ASM IO if it can be created (and thus ASM toolkit in
121      * classloader).
122      */
123     private static MetaClassIO getMetaClassIOASM()
124     {
125         try
126         {
127             return MetaClassIOASM.IO;
128         }
129         catch( final Exception e )
130         {
131             //ie ClassNotFoundException
132             return null;
133         }
134     }
135 
136 }