View Javadoc
1   package org.apache.fulcrum.yaafi.framework.role;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  
24  import org.apache.avalon.framework.configuration.Configuration;
25  import org.apache.avalon.framework.configuration.ConfigurationException;
26  import org.apache.fulcrum.yaafi.framework.constant.AvalonFortressConstants;
27  import org.apache.fulcrum.yaafi.framework.constant.AvalonPhoenixConstants;
28  import org.apache.fulcrum.yaafi.framework.constant.AvalonYaafiConstants;
29  import org.apache.fulcrum.yaafi.framework.util.Validate;
30  
31  /**
32   * Parses the role configuration file of various Avalon containers.
33   *
34   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
35   */
36  
37  public class RoleConfigurationParserImpl implements RoleConfigurationParser {
38  	/** The flavour of Avalon container */
39  	private String containerFlavour;
40  
41  	/**
42  	 * Constructor
43  	 * 
44  	 * @param containerFlavour The flavour of Avalon container
45  	 */
46  	public RoleConfigurationParserImpl(String containerFlavour) {
47  		Validate.notEmpty(containerFlavour, "containerFlavour");
48  		this.containerFlavour = containerFlavour;
49  	}
50  
51  	/**
52  	 * Parses a role configuration file.
53  	 *
54  	 * @param roleConfiguration the role configuration file to parse
55  	 * @return the parsed RoleEntries
56  	 * @throws ConfigurationException the configuration couldn't be processsed
57  	 */
58  	public RoleEntry[] parse(Configuration roleConfiguration) throws ConfigurationException {
59  		Validate.notNull(roleConfiguration, "roleConfiguration");
60  
61  		if (AvalonYaafiConstants.AVALON_CONTAINER_YAAFI.equals(containerFlavour)) {
62  			return mapFromYaafi(roleConfiguration);
63  
64  		}
65  		if (AvalonPhoenixConstants.AVALON_CONTAINER_PHOENIX.equals(containerFlavour)) {
66  			return mapFromPhoenix(roleConfiguration);
67  
68  		} else if (AvalonFortressConstants.AVALON_CONTAINER_FORTESS.equals(containerFlavour)) {
69  			return mapFromFortress(roleConfiguration);
70  
71  		} else {
72  			String msg = "Don't know the following container flavour : " + containerFlavour;
73  			throw new IllegalArgumentException(msg);
74  		}
75  	}
76  
77  	/**
78  	 * Parses a YAAFI role configuration file.
79  	 *
80  	 * @param roleConfiguration the role configuration
81  	 * @return the role entries from the configuration file
82  	 * @throws ConfigurationException the configuration couldn't be processsed
83  	 */
84  	private RoleEntry[] mapFromYaafi(Configuration roleConfiguration) throws ConfigurationException {
85  		Validate.notNull(roleConfiguration, "roleConfiguration");
86  
87  		String clazzName = null;
88  		String name = null;
89  		String shorthand = null;
90  		boolean isEarlyInit = false;
91  		String description = null;
92  		String componentType = null;
93  		String componentFlavour = null;
94  		boolean hasProxy = false;
95  		ArrayList<String> interceptorList = null;
96  		String logCategory = null;
97  		RoleEntry roleEntry = null;
98  
99  		Configuration[] list = roleConfiguration.getChildren("role");
100 		RoleEntry[] result = new RoleEntry[list.length];
101 
102 		int roleIndex = 0;
103 		for (Configuration entry : list) {
104 			clazzName = entry.getAttribute("default-class");
105 			name = entry.getAttribute("name", clazzName);
106 			shorthand = entry.getAttribute("shorthand", name);
107 			isEarlyInit = entry.getAttributeAsBoolean("early-init", true);
108 			description = entry.getAttribute("description", null);
109 			componentType = entry.getAttribute("component-type", "avalon");
110 			componentFlavour = entry.getAttribute("component-flavour", AvalonYaafiConstants.AVALON_CONTAINER_YAAFI);
111 			hasProxy = entry.getAttributeAsBoolean("has-proxy", true);
112 			logCategory = entry.getAttribute("logger", shorthand);
113 
114 			// parse the list of defined interceptors
115 			Configuration[] interceptorConfigList = entry.getChild("interceptors").getChildren("interceptor");
116 			interceptorList = new ArrayList<String>();
117 
118 			for ( Configuration interceptorConfigEntry : interceptorConfigList )
119 			{
120 				interceptorList.add(interceptorConfigEntry.getValue("interceptor"));
121 			}
122 
123 			// create a role entry
124 
125 			roleEntry = new RoleEntryImpl(name, clazzName, shorthand, isEarlyInit, description, componentType,
126 					componentFlavour, hasProxy, interceptorList, logCategory);
127 
128 			result[roleIndex++] = roleEntry;
129 		}
130 
131 		return result;
132 	}
133 
134 	private RoleEntry[] mapFromPhoenix(Configuration roleConfiguration) throws ConfigurationException {
135 		Validate.notNull(roleConfiguration, "roleConfiguration");
136 		throw new ConfigurationException("Not supported yet");
137 	}
138 
139 	private RoleEntry[] mapFromFortress(Configuration roleConfiguration) throws ConfigurationException {
140 		Validate.notNull(roleConfiguration, "roleConfiguration");
141 		throw new ConfigurationException("Not supported yet");
142 	}
143 }