View Javadoc

1   /**
2    * Copyright (C) 2010 Daniel Manzke <daniel.manzke@googlemail.com>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /**
17   * 
18   */
19  package org.apache.onami.autobind.integrations.commons.configuration;
20  
21  import java.io.File;
22  import java.lang.annotation.Annotation;
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  import java.util.Map;
26  import java.util.logging.Level;
27  import java.util.logging.Logger;
28  
29  import javax.inject.Named;
30  import javax.inject.Singleton;
31  
32  import org.apache.commons.configuration.ConfigurationException;
33  import org.apache.commons.configuration.FileConfiguration;
34  import org.apache.onami.autobind.configuration.Configuration;
35  import org.apache.onami.autobind.install.BindingStage;
36  import org.apache.onami.autobind.scanner.features.BindingScannerFeature;
37  
38  
39  /**
40   * This Class will be called for each Class, which is annotated with
41   * {@link Configuration} and which needs an Apache Commons-based Configuration.
42   * 
43   * @author Daniel Manzke
44   * 
45   */
46  @Singleton
47  public class CommonsConfigurationFeature extends BindingScannerFeature {
48  	private Logger _logger = Logger.getLogger(CommonsConfigurationFeature.class.getName());
49  
50  	@Override
51  	public BindingStage accept(Class<Object> annotatedClass, Map<String, Annotation> annotations) {
52  		if (annotations.containsKey(Configuration.class.getName())) {
53  			Configuration config = (Configuration) annotations.get(Configuration.class.getName());
54  			if (FileConfiguration.class.isAssignableFrom(config.to())) {
55  				return BindingStage.BOOT_BEFORE;
56  			}
57  		}
58  		return BindingStage.IGNORE;
59  	}
60  
61  	@SuppressWarnings("unchecked")
62  	@Override
63  	public void process(Class<Object> annotatedClass, Map<String, Annotation> annotations) {
64  		Configuration config = (Configuration) annotations.get(Configuration.class.getName());
65  		Named name = config.name();
66  
67  		// TODO Implement Location overriding
68  		URL url;
69  		switch (config.location().type()) {
70  		case FILE:
71  			File file = new File(config.location().value());
72  			if (!file.exists()) {
73  				_logger.log(Level.WARNING, "Ignoring Configuration " + name + " in "
74  						+ config.location() + ". In the Path " + file.getAbsolutePath()
75  						+ " no Configuration was found.");
76  				return;
77  			}
78  			try {
79  				url = file.toURI().toURL();
80  			} catch (MalformedURLException e) {
81  				_logger.log(Level.WARNING, "Ignoring Configuration " + name + " in "
82  						+ config.location() + ". It has an illegal URL-Format.", e);
83  				return;
84  			}
85  			break;
86  		case URL:
87  			try {
88  				url = new URL(config.location().value());
89  			} catch (MalformedURLException e) {
90  				_logger.log(Level.WARNING, "Ignoring Configuration " + name + " in "
91  						+ config.location() + ". It has an illegal URL-Format.", e);
92  				return;
93  			}
94  			break;
95  		case CLASSPATH:
96  		default:
97  			url = this.getClass().getResource(config.location().value());
98  			break;
99  		}
100 
101 		if (url == null) {
102 			_logger.log(Level.WARNING, "Ignoring Configuration " + name + " in "
103 					+ config.location() + ", because is couldn't be found in the Classpath.");
104 			// TODO Throw an exception if config doesn't exist?
105 			return;
106 		}
107 
108 		Named named = null;
109 		if (name.value().length() > 0) {
110 			named = name;
111 		}
112 
113 		FileConfiguration configuration;
114 		try {
115 			// Class<? extends FileConfiguration> interf =
116 			// config.to().asSubclass(FileConfiguration.class);
117 			Class<FileConfiguration> interf = (Class<FileConfiguration>) config.to();
118 			configuration = (FileConfiguration) injector.getInstance(interf);
119 			configuration.load(url);
120 
121 			bindInstance(configuration, interf, named, null);
122 			bindInstance(configuration, FileConfiguration.class, named, null);
123 			bindInstance(configuration, org.apache.commons.configuration.Configuration.class,
124 				named, null);
125 		} catch (ConfigurationException e) {
126 			_logger.log(Level.WARNING, "Configuration " + name + " couldn't be loaded/bound: "
127 					+ e.getMessage(), e);
128 		}
129 	}
130 }