1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2006 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.services.xmlmerge.factory;
18
19 import java.lang.reflect.Field;
20
21 import ch.elca.el4j.services.xmlmerge.ConfigurationException;
22 import ch.elca.el4j.services.xmlmerge.Operation;
23
24 /**
25 * Creates an operation instance given a short name (alias) or a class name.
26 *
27 * @svnLink $Revision: 3882 $;$Date: 2009-08-04 15:24:14 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/xml_merge/common/src/main/java/ch/elca/el4j/services/xmlmerge/factory/OperationResolver.java $
28 *
29 * @author Laurent Bovet (LBO)
30 * @author Alex Mathey (AMA)
31 */
32 public class OperationResolver {
33
34 /**
35 * Class representing an operation.
36 */
37 Class m_constantClass;
38
39 /**
40 * Creates an OperationResolver given the class representing an operation.
41 *
42 * @param class1
43 * The class of an operation
44 */
45 public OperationResolver(Class class1) {
46 m_constantClass = class1;
47 }
48
49 /**
50 * Resolves an alias or an operation class name to an operation.
51 *
52 * @param aliasOrClassName
53 * an alias or class name representing an operation
54 * @return The resolved operation
55 * @throws ConfigurationException
56 * If an error occurred during the resolving process
57 */
58 public Operation resolve(String aliasOrClassName)
59 throws ConfigurationException {
60 Field field = null;
61 try {
62 field = m_constantClass.getField(aliasOrClassName.toUpperCase());
63 } catch (NoSuchFieldException e) {
64
65 try {
66 return (Operation) Class.forName(aliasOrClassName)
67 .newInstance();
68 } catch (InstantiationException e1) {
69 throw new ConfigurationException(
70 "Cannot instanciate object from class " + aliasOrClassName);
71 } catch (IllegalAccessException e1) {
72 throw new ConfigurationException(
73 "Cannot access constructor or class " + aliasOrClassName);
74 } catch (ClassNotFoundException e1) {
75 throw new ConfigurationException(
76 "Verb not found or class not found:" + aliasOrClassName);
77 } catch (ClassCastException e1) {
78 throw new ConfigurationException(
79 "Class does not implement Operation :" + aliasOrClassName);
80 }
81
82 }
83 try {
84 return (Operation) field.get(null);
85 } catch (IllegalAccessException e) {
86 // should not happen
87 throw new ConfigurationException(e);
88 }
89 }
90
91 }