001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.builder;
016:
017: import java.util.Collection;
018: import java.util.Collections;
019: import java.util.LinkedHashSet;
020: import java.util.Properties;
021: import java.util.Set;
022:
023: import org.strecks.interceptor.AfterInterceptor;
024: import org.strecks.interceptor.BeforeInterceptor;
025: import org.strecks.util.Assert;
026: import org.strecks.util.ReflectHelper;
027:
028: /**
029: * Reads implementation classes for interceptors from the
030: * <code>strecks.properties</code> file located on the class path
031: * @author Phil Zoio
032: */
033: public class InterceptorBuilderImpl implements InterceptorBuilder {
034:
035: private InterceptorConfig interceptorConfig;
036:
037: private Collection<BeforeInterceptor> beforeInterceptors = new LinkedHashSet<BeforeInterceptor>();
038:
039: private Collection<AfterInterceptor> afterInterceptors = new LinkedHashSet<AfterInterceptor>();
040:
041: public void build(String prefix) {
042: String fileName = getFileName(prefix);
043: Properties properties = ConfigUtils.loadProperties(fileName);
044: this .interceptorConfig = getInterceptorConfig(properties);
045: createInterceptors(this .interceptorConfig);
046: }
047:
048: protected String getFileName(String prefix) {
049: return ConfigUtils.getPropertiesFileName(prefix);
050: }
051:
052: public InterceptorConfig getInterceptorConfig(Properties properties) {
053: InterceptorConfig config = new InterceptorConfig();
054: int index = 1;
055: String beforeInterceptor;
056:
057: while ((beforeInterceptor = properties
058: .getProperty("before.interceptor." + index)) != null) {
059: config.addBeforeInterceptor(beforeInterceptor);
060: index++;
061: }
062:
063: index = 1;
064: String afterInterceptor;
065:
066: while ((afterInterceptor = properties
067: .getProperty("after.interceptor." + index)) != null) {
068: config.addAfterInterceptor(afterInterceptor);
069: index++;
070: }
071:
072: return config;
073: }
074:
075: void createInterceptors(InterceptorConfig config) {
076: Assert.notNull(config);
077:
078: Set<String> beforeNames = config.getBeforeInterceptors();
079: for (String className : beforeNames) {
080: BeforeInterceptor interceptor = ReflectHelper
081: .createInstance(className, BeforeInterceptor.class);
082: beforeInterceptors.add(interceptor);
083: }
084:
085: Set<String> afterNames = config.getAfterInterceptors();
086: for (String className : afterNames) {
087: AfterInterceptor interceptor = ReflectHelper
088: .createInstance(className, AfterInterceptor.class);
089: afterInterceptors.add(interceptor);
090: }
091: }
092:
093: public Collection<BeforeInterceptor> getBeforeInterceptors() {
094: return Collections.unmodifiableCollection(beforeInterceptors);
095: }
096:
097: public Collection<AfterInterceptor> getAfterInterceptors() {
098: return Collections.unmodifiableCollection(afterInterceptors);
099: }
100: }
|