/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.strecks.builder;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Properties;
import java.util.Set;
import org.strecks.interceptor.AfterInterceptor;
import org.strecks.interceptor.BeforeInterceptor;
import org.strecks.util.Assert;
import org.strecks.util.ReflectHelper;
/**
* Reads implementation classes for interceptors from the
* <code>strecks.properties</code> file located on the class path
* @author Phil Zoio
*/
public class InterceptorBuilderImpl implements InterceptorBuilder
{
private InterceptorConfig interceptorConfig;
private Collection<BeforeInterceptor> beforeInterceptors = new LinkedHashSet<BeforeInterceptor>();
private Collection<AfterInterceptor> afterInterceptors = new LinkedHashSet<AfterInterceptor>();
public void build(String prefix)
{
String fileName = getFileName(prefix);
Properties properties = ConfigUtils.loadProperties(fileName);
this.interceptorConfig = getInterceptorConfig(properties);
createInterceptors(this.interceptorConfig);
}
protected String getFileName(String prefix)
{
return ConfigUtils.getPropertiesFileName(prefix);
}
public InterceptorConfig getInterceptorConfig(Properties properties)
{
InterceptorConfig config = new InterceptorConfig();
int index = 1;
String beforeInterceptor;
while ((beforeInterceptor = properties.getProperty("before.interceptor." + index)) != null)
{
config.addBeforeInterceptor(beforeInterceptor);
index++;
}
index = 1;
String afterInterceptor;
while ((afterInterceptor = properties.getProperty("after.interceptor." + index)) != null)
{
config.addAfterInterceptor(afterInterceptor);
index++;
}
return config;
}
void createInterceptors(InterceptorConfig config)
{
Assert.notNull(config);
Set<String> beforeNames = config.getBeforeInterceptors();
for (String className : beforeNames)
{
BeforeInterceptor interceptor = ReflectHelper.createInstance(className, BeforeInterceptor.class);
beforeInterceptors.add(interceptor);
}
Set<String> afterNames = config.getAfterInterceptors();
for (String className : afterNames)
{
AfterInterceptor interceptor = ReflectHelper.createInstance(className, AfterInterceptor.class);
afterInterceptors.add(interceptor);
}
}
public Collection<BeforeInterceptor> getBeforeInterceptors()
{
return Collections.unmodifiableCollection(beforeInterceptors);
}
public Collection<AfterInterceptor> getAfterInterceptors()
{
return Collections.unmodifiableCollection(afterInterceptors);
}
}