01: /*
02: * $Id: SpiUtils.java 10790 2008-02-12 20:53:32Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.util;
12:
13: import org.mule.api.registry.ServiceDescriptorFactory;
14: import org.mule.config.ExceptionHelper;
15: import org.mule.transport.service.TransportFactory;
16:
17: import java.io.IOException;
18: import java.io.InputStream;
19: import java.util.Properties;
20:
21: import org.apache.commons.logging.Log;
22: import org.apache.commons.logging.LogFactory;
23:
24: /**
25: * @deprecated Mule 2.x will use the OSGi Service Registry for locating services
26: */
27: // @ThreadSafe
28: public class SpiUtils {
29: private static final Log logger = LogFactory.getLog(SpiUtils.class);
30:
31: public static final String SERVICE_ROOT = "META-INF/services/";
32: public static final String PROVIDER_SERVICE_PATH = "org/mule/providers/";
33: public static final String EXCEPTION_SERVICE_PATH = "org/mule/config/";
34:
35: public static Properties findServiceDescriptor(String type,
36: String name) {
37: if (type.equals(ServiceDescriptorFactory.PROVIDER_SERVICE_TYPE)) {
38: return findServiceDescriptor(PROVIDER_SERVICE_PATH, name,
39: TransportFactory.class);
40: } else if (type
41: .equals(ServiceDescriptorFactory.EXCEPTION_SERVICE_TYPE)) {
42: return findServiceDescriptor(EXCEPTION_SERVICE_PATH, name,
43: ExceptionHelper.class);
44: } else {
45: logger.warn("Attempt to lookup unrecognized service type: "
46: + type);
47: return null;
48: }
49:
50: }
51:
52: public static Properties findServiceDescriptor(String path,
53: String name, Class currentClass) {
54: if (!name.endsWith(".properties")) {
55: name += ".properties";
56: }
57:
58: if (path.startsWith("/")) {
59: path = path.substring(1);
60: }
61: if (!path.endsWith("/")) {
62: path += "/";
63: }
64: if (path.startsWith(SERVICE_ROOT)) {
65: path += name;
66: } else {
67: path = SERVICE_ROOT + path + name;
68: }
69: try {
70: InputStream is = IOUtils.getResourceAsStream(path,
71: currentClass, false, false);
72: if (is != null) {
73: Properties props = new Properties();
74: try {
75: props.load(is);
76: return props;
77: } catch (IOException e) {
78: logger
79: .warn("Descriptor found but unable to load properties for service "
80: + name);
81: return null;
82: }
83: } else {
84: return null;
85: }
86: } catch (IOException e) {
87: return null;
88: }
89: }
90: }
|