1 50 51 package org.apache.avalon.meta.info.builder; 52 53 import java.io.InputStream ; 54 import org.apache.avalon.excalibur.i18n.ResourceManager; 55 import org.apache.avalon.excalibur.i18n.Resources; 56 import org.apache.avalon.meta.info.Service; 57 58 68 public final class ServiceBuilder 69 { 70 private static final Resources REZ = 71 ResourceManager.getPackageResources( ServiceBuilder.class ); 72 73 private final ServiceCreator m_xmlServiceCreator = createXMLServiceCreator(); 74 private final ServiceCreator m_serialServiceCreator = new SerializedServiceCreator(); 75 76 83 public Service build( final Class clazz ) 84 throws Exception 85 { 86 return build( clazz.getName(), clazz.getClassLoader() ); 87 } 88 89 98 public Service build( final String classname, 99 final ClassLoader classLoader ) 100 throws Exception 101 { 102 final Service info = buildFromSerDescriptor( classname, classLoader ); 103 if( null != info ) 104 { 105 return info; 106 } 107 else 108 { 109 return buildFromXMLDescriptor( classname, classLoader ); 110 } 111 } 112 113 121 private Service buildFromSerDescriptor( final String classname, 122 final ClassLoader classLoader ) 123 throws Exception 124 { 125 final String xinfo = 126 classname.replace( '.', '/' ) + ".sinfo"; 127 final InputStream inputStream = 128 classLoader.getResourceAsStream( xinfo ); 129 if( null == inputStream ) 130 { 131 return null; 132 } 133 134 return m_serialServiceCreator.createService( classname, inputStream ); 135 } 136 137 145 private Service buildFromXMLDescriptor( final String classname, 146 final ClassLoader classLoader ) 147 throws Exception 148 { 149 153 final String xservice = 154 classname.replace( '.', '/' ) + ".xservice"; 155 final InputStream inputStream = 156 classLoader.getResourceAsStream( xservice ); 157 158 if( null == inputStream ) 159 { 160 final String message = 161 REZ.getString( "builder.missing-info.error", 162 classname ); 163 throw new Exception ( message ); 164 } 165 166 170 final ServiceCreator xmlServiceCreator = getXMLServiceCreator( classname ); 171 return xmlServiceCreator.createService( classname, inputStream ); 172 } 173 174 180 private ServiceCreator getXMLServiceCreator( final String classname ) 181 throws Exception 182 { 183 if( null != m_xmlServiceCreator ) 184 { 185 return m_xmlServiceCreator; 186 } 187 else 188 { 189 final String message = 190 REZ.getString( "builder.missing-xml-creator.error", 191 classname ); 192 throw new Exception ( message ); 193 } 194 } 195 196 202 private static ServiceCreator createXMLServiceCreator() 203 { 204 ServiceCreator xmlServiceCreator = null; 205 try 206 { 207 xmlServiceCreator = new XMLServiceCreator(); 208 } 209 catch( final Exception e ) 210 { 211 } 214 return xmlServiceCreator; 215 } 216 } 217 | Popular Tags |