1 /***
2 *
3 * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.logicblaze.lingo;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 /***
24 * A helper class for working with {@link MetadataStrategy} instances
25 *
26 * @version $Revision$
27 */
28 public class MetadataStrategyHelper {
29
30 private static final Log log = LogFactory.getLog(MetadataStrategyHelper.class);
31 private static boolean initialised;
32 private static Class type;
33
34 /***
35 * Creates a new default instance of MetadataStrategy. On Java 5 if you have
36 * the <a href="http://lingo.codehaus.org/SCA+Support">SCA Annotations</a>
37 * on the classpath then this will use the SCAMetadataStrategy by default;
38 * otherwise the {@link SimpleMetadataStrategy} is used.
39 *
40 * @return a newly created instance
41 */
42 public static MetadataStrategy newInstance() {
43 synchronized (MetadataStrategy.class) {
44 if (!initialised) {
45 type = findMetadataStrategyClass();
46 initialised = true;
47 }
48 }
49
50 if (type != null) {
51 try {
52 return (MetadataStrategy) type.newInstance();
53 }
54 catch (Exception e) {
55 log.warn("Could not create instance of: " + type.getName() + ". Reason: " + e, e);
56 }
57 }
58 return new SimpleMetadataStrategy();
59 }
60
61 protected static Class findMetadataStrategyClass() {
62 return findClass("org.logicblaze.lingo.sca.SCAMetadataStrategy");
63 }
64
65 protected static Class findClass(String name) {
66 try {
67 return Thread.currentThread().getContextClassLoader().loadClass(name);
68 }
69 catch (ClassNotFoundException e) {
70 try {
71 return MetadataStrategy.class.getClassLoader().loadClass(name);
72 }
73 catch (ClassNotFoundException e1) {
74 log.debug("Failed to find class: " + name + " on classpath. Reason: " + e, e);
75 return null;
76 }
77 }
78 }
79 }