1 88 89 package org.openejb.alt.config; 90 91 import org.exolab.castor.xml.MarshalException; 92 import org.exolab.castor.xml.ValidationException; 93 import org.openejb.OpenEJBException; 94 import org.openejb.alt.config.sys.ServiceProvider; 95 import org.openejb.alt.config.sys.ServicesJar; 96 import org.openejb.util.Logger; 97 import org.openejb.util.Messages; 98 99 import java.io.*; 100 import java.net.MalformedURLException ; 101 import java.net.URL ; 102 import java.util.HashMap ; 103 import java.util.Map ; 104 import java.util.Properties ; 105 106 111 public class ServiceUtils { 112 113 public static final String defaultProviderURL = "org.openejb"; 114 private static Map loadedServiceJars = new HashMap (); 115 public static Messages messages = new Messages("org.openejb.util.resources"); 116 public static Logger logger = Logger.getInstance("OpenEJB", "org.openejb.util.resources"); 117 118 119 public static ServiceProvider getServiceProvider(Service service) throws OpenEJBException { 120 return getServiceProvider(service.getProvider()); 121 } 122 123 134 public static ServiceProvider getServiceProvider(String id) throws OpenEJBException { 135 136 String providerName = null; 137 String serviceName = null; 138 139 if (id.indexOf("#") == -1) { 140 providerName = defaultProviderURL; 141 serviceName = id; 142 } else { 143 providerName = id.substring(0, id.indexOf("#")); 144 serviceName = id.substring(id.indexOf("#") + 1); 145 } 146 147 ServiceProvider service = null; 148 149 if (loadedServiceJars.get(providerName) == null) { 150 ServicesJar sj = readServicesJar(providerName); 151 ServiceProvider[] sp = sj.getServiceProvider(); 152 HashMap services = new HashMap (sj.getServiceProviderCount()); 153 154 for (int i = 0; i < sp.length; i++) { 155 services.put(sp[i].getId(), sp[i]); 156 } 157 158 loadedServiceJars.put(providerName, services); 159 service = (ServiceProvider) services.get(serviceName); 162 } else { 163 Map provider = (Map ) loadedServiceJars.get(providerName); 164 service = (ServiceProvider) provider.get(serviceName); 165 } 166 167 if (service == null) { 168 throw new OpenEJBException(messages.format("conf.4901", serviceName, providerName)); 169 } 170 171 return service; 172 } 173 174 185 public static ServicesJar readServicesJar(String providerName) throws OpenEJBException { 186 try { 187 Unmarshaller unmarshaller = new Unmarshaller(ServicesJar.class, "service-jar.xml"); 188 URL serviceURL = new URL ("resource:/" + providerName.replace('.', '/')+"/"); 189 return (ServicesJar) unmarshaller.unmarshal(serviceURL); 190 } catch (MalformedURLException e) { 191 throw new OpenEJBException(e); 192 } 193 } 194 195 public static void writeServicesJar(String xmlFile, ServicesJar servicesJarObject) throws OpenEJBException { 196 197 203 Writer writer = null; 204 205 try { 206 File file = new File(xmlFile); 207 writer = new FileWriter(file); 208 servicesJarObject.marshal(writer); 209 } catch (IOException e) { 210 throw new OpenEJBException(messages.format("conf.4040", xmlFile, e.getLocalizedMessage())); 211 } catch (MarshalException e) { 212 if (e.getException() instanceof IOException) { 213 throw new OpenEJBException(messages.format("conf.4040", xmlFile, e.getLocalizedMessage())); 214 } else { 215 throw new OpenEJBException(messages.format("conf.4050", xmlFile, e.getLocalizedMessage())); 216 } 217 } catch (ValidationException e) { 218 219 226 227 231 232 throw new OpenEJBException(messages.format("conf.4060", xmlFile, e.getLocalizedMessage())); 233 } 234 235 try { 236 writer.close(); 237 } catch (Exception e) { 238 throw new OpenEJBException(messages.format("file.0020", xmlFile, e.getLocalizedMessage())); 239 } 240 } 241 242 public static Properties assemblePropertiesFor(String confItem, String itemId, String itemContent, 243 String confFile, ServiceProvider service) throws OpenEJBException { 244 245 Properties props = new Properties (); 246 247 try { 248 252 if (service.getPropertiesFile() != null) { 253 props = loadProperties(service.getPropertiesFile().getFile()); 254 } 255 256 260 261 if (service.getContent() != null) { 262 StringBufferInputStream in = new StringBufferInputStream(service.getContent()); 263 props = loadProperties(in, props); 264 } 265 } catch (OpenEJBException ex) { 266 throw new OpenEJBException(messages.format("conf.0013", service.getId(), null, ex.getLocalizedMessage())); 267 } 268 269 272 try { 273 if (itemContent != null) { 274 StringBufferInputStream in = new StringBufferInputStream(itemContent); 275 props = loadProperties(in, props); 276 } 277 } catch (OpenEJBException ex) { 278 throw new OpenEJBException(messages.format("conf.0014", confItem, itemId, confFile, ex.getLocalizedMessage())); 279 } 280 281 return props; 282 } 283 284 public static Properties loadProperties(String pFile) throws OpenEJBException { 285 return loadProperties(pFile, new Properties ()); 286 } 287 288 public static Properties loadProperties(String propertiesFile, Properties defaults) throws OpenEJBException { 289 try { 290 File pfile = new File(propertiesFile); 291 InputStream in = new FileInputStream(pfile); 292 return loadProperties(in, defaults); 293 } catch (FileNotFoundException ex) { 294 throw new OpenEJBException(messages.format("conf.0006", propertiesFile, ex.getLocalizedMessage())); 295 } catch (IOException ex) { 296 throw new OpenEJBException(messages.format("conf.0007", propertiesFile, ex.getLocalizedMessage())); 297 } catch (SecurityException ex) { 298 throw new OpenEJBException(messages.format("conf.0005", propertiesFile, ex.getLocalizedMessage())); 299 } 300 } 301 302 public static Properties loadProperties(InputStream in, Properties defaults) 303 throws OpenEJBException { 304 305 try { 306 312 defaults.load(in); 313 } catch (IOException ex) { 314 throw new OpenEJBException(messages.format("conf.0012", ex.getLocalizedMessage())); 315 } 316 317 return defaults; 318 } 319 320 } 321 | Popular Tags |