001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.config;
017:
018: import org.apache.openejb.OpenEJBException;
019: import org.apache.openejb.loader.SystemInstance;
020: import org.apache.openejb.loader.FileUtils;
021: import org.apache.openejb.config.sys.ServiceProvider;
022: import org.apache.openejb.config.sys.ServicesJar;
023: import org.apache.openejb.config.sys.JaxbOpenejb;
024: import org.apache.openejb.util.LogCategory;
025: import org.apache.openejb.util.Logger;
026: import org.apache.openejb.util.Messages;
027:
028: import java.io.File;
029: import java.io.FileInputStream;
030: import java.io.FileNotFoundException;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.util.HashMap;
034: import java.util.Map;
035: import java.util.Properties;
036: import java.util.List;
037: import java.util.ArrayList;
038:
039: public class ServiceUtils {
040: public static final String ANY = ServiceUtils.class.getName()
041: + "@ANY";
042: public static final String NONE = ServiceUtils.class.getName()
043: + "@NONE";
044:
045: /**
046: * Default service provider package. This value is choosen as follows:
047: * </p>
048: * 1. System property "openejb.provider.default" </br>
049: * 2. If in a full server containing a "conf" directory "org.apache.openejb" </br>
050: * 3. Embedded mode "org.apache.openejb.embedded" </br>
051: */
052: public static final String defaultProviderURL;
053: static {
054: String defaultValue = "org.apache.openejb";
055: try {
056: SystemInstance system = SystemInstance.get();
057: FileUtils base = system.getBase();
058: File confDir = base.getDirectory("conf");
059: if (!confDir.exists()) {
060: defaultValue = "org.apache.openejb.embedded";
061: }
062: } catch (Exception ignored) {
063: }
064: defaultProviderURL = System.getProperty(
065: "openejb.provider.default", defaultValue);
066: }
067:
068: private static Map<String, List<ServiceProvider>> loadedServiceJars = new HashMap<String, List<ServiceProvider>>();
069: public static Messages messages = new Messages(
070: "org.apache.openejb.util.resources");
071: public static Logger logger = Logger.getInstance(
072: LogCategory.OPENEJB, "org.apache.openejb.util.resources");
073:
074: public static class ProviderInfo {
075: private final String packageName;
076: private final String serviceName;
077:
078: public ProviderInfo(String providerName, String serviceName) {
079: this .packageName = providerName;
080: this .serviceName = serviceName;
081: }
082:
083: public String getPackageName() {
084: return packageName;
085: }
086:
087: public String getServiceName() {
088: return serviceName;
089: }
090: }
091:
092: public static boolean hasServiceProvider(String id) {
093: try {
094: ProviderInfo info = getProviderInfo(id);
095:
096: List<ServiceProvider> services = getServiceProviders(info
097: .getPackageName());
098:
099: for (ServiceProvider service : services) {
100: if (service.getId().equals(id)) {
101: return true;
102: }
103: }
104: } catch (OpenEJBException ignored) {
105: // someone else will load the file and get the exception
106: }
107: return false;
108: }
109:
110: public static ServiceProvider getServiceProvider(String id)
111: throws OpenEJBException {
112: ProviderInfo info = getProviderInfo(id);
113:
114: List<ServiceProvider> services = getServiceProviders(info
115: .getPackageName());
116:
117: for (ServiceProvider service : services) {
118: if (service.getId().equals(info.getServiceName())) {
119: return service;
120: }
121: }
122: throw new NoSuchProviderException(messages.format("conf.4901",
123: info.getServiceName(), info.getPackageName()));
124: }
125:
126: public static String getServiceProviderId(String type)
127: throws OpenEJBException {
128: return getServiceProviderId(type, null);
129: }
130:
131: public static String getServiceProviderId(String type,
132: Properties required) throws OpenEJBException {
133: ServiceProvider provider = getServiceProviderByType(type,
134: required);
135:
136: return provider != null ? provider.getId() : null;
137: }
138:
139: public static List<ServiceProvider> getServiceProvidersByServiceType(
140: String type) throws OpenEJBException {
141: ArrayList<ServiceProvider> providers = new ArrayList<ServiceProvider>();
142: if (type == null)
143: return providers;
144:
145: List<ServiceProvider> services = getServiceProviders(defaultProviderURL);
146:
147: for (ServiceProvider service : services) {
148: if (service.getService().equals(type)) {
149: providers.add(service);
150: }
151: }
152:
153: return providers;
154: }
155:
156: public static ServiceProvider getServiceProviderByType(String type)
157: throws OpenEJBException {
158: return getServiceProviderByType(type, (Properties) null);
159: }
160:
161: public static ServiceProvider getServiceProviderByType(String type,
162: Properties required) throws OpenEJBException {
163: if (type == null)
164: return null;
165: if (required == null)
166: required = new Properties();
167:
168: List<ServiceProvider> services = getServiceProviders(defaultProviderURL);
169:
170: for (ServiceProvider service : services) {
171: if (service.getTypes().contains(type)
172: && implies(required, service.getProperties())) {
173: return service;
174: }
175: }
176:
177: return null;
178: }
179:
180: public static boolean implies(Properties a, Properties b) {
181: for (Map.Entry<Object, Object> entry : a.entrySet()) {
182: Object value = b.get(entry.getKey());
183:
184: Object expected = entry.getValue();
185:
186: if (expected.equals(NONE)) {
187: if (value != null)
188: return false;
189: } else if (expected.equals(ANY)) {
190: if (value == null)
191: return false;
192: } else if (!expected.equals(value))
193: return false;
194: }
195: return true;
196: }
197:
198: public static ServiceProvider getServiceProviderByType(
199: String providerType, String serviceType)
200: throws OpenEJBException {
201: if (serviceType == null)
202: return null;
203:
204: List<ServiceProvider> services = getServiceProvidersByServiceType(providerType);
205:
206: for (ServiceProvider service : services) {
207: if (service.getTypes().contains(serviceType)) {
208: return service;
209: }
210: }
211:
212: return null;
213: }
214:
215: public static List<ServiceProvider> getServiceProviders()
216: throws OpenEJBException {
217: return getServiceProviders(defaultProviderURL);
218: }
219:
220: public static List<ServiceProvider> getServiceProviders(
221: String packageName) throws OpenEJBException {
222: List<ServiceProvider> services = loadedServiceJars
223: .get(packageName);
224: if (services == null) {
225: ServicesJar servicesJar = JaxbOpenejb
226: .readServicesJar(packageName);
227:
228: // index services by provider id
229: List<ServiceProvider> serviceProviders = servicesJar
230: .getServiceProvider();
231: services = new ArrayList<ServiceProvider>(serviceProviders);
232:
233: loadedServiceJars.put(packageName, services);
234: }
235: return services;
236: }
237:
238: private static ProviderInfo getProviderInfo(String id) {
239: String providerName = null;
240: String serviceName = null;
241:
242: if (id.indexOf("#") != -1) {
243: providerName = id.substring(0, id.indexOf("#"));
244: serviceName = id.substring(id.indexOf("#") + 1);
245: } else if (id.indexOf(":") != -1) {
246: providerName = id.substring(0, id.indexOf(":"));
247: serviceName = id.substring(id.indexOf(":") + 1);
248: } else {
249: providerName = defaultProviderURL;
250: serviceName = id;
251: }
252:
253: return new ProviderInfo(providerName, serviceName);
254: }
255:
256: public static Properties loadProperties(String pFile)
257: throws OpenEJBException {
258: return loadProperties(pFile, new Properties());
259: }
260:
261: public static Properties loadProperties(String propertiesFile,
262: Properties defaults) throws OpenEJBException {
263: try {
264: File pfile = new File(propertiesFile);
265: InputStream in = new FileInputStream(pfile);
266:
267: try {
268: /*
269: This may not work as expected. The desired effect is that
270: the load method will read in the properties and overwrite
271: the values of any properties that may have previously been
272: defined.
273: */
274: defaults.load(in);
275: } catch (IOException ex) {
276: throw new OpenEJBException(messages.format("conf.0012",
277: ex.getLocalizedMessage()), ex);
278: }
279:
280: return defaults;
281: } catch (FileNotFoundException ex) {
282: throw new OpenEJBException(messages.format("conf.0006",
283: propertiesFile, ex.getLocalizedMessage()), ex);
284: } catch (IOException ex) {
285: throw new OpenEJBException(messages.format("conf.0007",
286: propertiesFile, ex.getLocalizedMessage()), ex);
287: } catch (SecurityException ex) {
288: throw new OpenEJBException(messages.format("conf.0005",
289: propertiesFile, ex.getLocalizedMessage()), ex);
290: }
291: }
292:
293: }
|