1/*2 * $Id: ModularTilesInitializer.java 1297705 2012-03-06 20:44:30Z nlebas $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122package org.apache.tiles.extras.module;
2324import java.io.IOException;
25import java.io.InputStream;
26import java.util.ArrayList;
27import java.util.Collection;
28import java.util.List;
29import java.util.jar.Attributes;
30import java.util.jar.Manifest;
3132import javax.servlet.ServletContext;
3334import org.apache.tiles.definition.DefinitionsFactoryException;
35import org.apache.tiles.request.ApplicationContext;
36import org.apache.tiles.request.ApplicationResource;
37import org.apache.tiles.request.reflect.ClassUtil;
38import org.apache.tiles.request.servlet.wildcard.WildcardServletApplicationContext;
39import org.apache.tiles.startup.TilesInitializer;
4041/**42 * Loads Tiles modules, initializes them and destroy them at the end.<br>43 * It loads all META-INF/MANIFEST.MF files, checks for the "Tiles-Initializer"44 * property that must contain a valid class name of a {@link TilesInitializer}.45 * After that, initializes all found initializers, one by one. When the46 * {@link #destroy()} method is called, all the initializers are then destroyed.47 *48 * @version $Rev: 1297705 $ $Date: 2012-03-07 07:44:30 +1100 (Wed, 07 Mar 2012) $49 * @since 2.2.150 */51publicclassModularTilesInitializerimplementsTilesInitializer {
5253/**54 * The initializers to use.55 */56private List<TilesInitializer> initializers;
5758/** {@inheritDoc} */59publicvoid initialize(ApplicationContext preliminaryContext) {
60 ApplicationContext applicationContext = new WildcardServletApplicationContext(
61 (ServletContext) preliminaryContext.getContext());
62 loadInitializers(applicationContext);
6364for (TilesInitializer initializer : initializers) {
65 initializer.initialize(preliminaryContext);
66 }
67 }
6869/** {@inheritDoc} */70publicvoid destroy() {
71for (TilesInitializer initializer : initializers) {
72 initializer.destroy();
73 }
74 }
7576/**77 * Load all the initializers from manifest files.78 *79 * @param applicationContext The application context.80 */81privatevoid loadInitializers(ApplicationContext applicationContext) {
82 initializers = new ArrayList<TilesInitializer>();
83try {
84 Collection<ApplicationResource> resources = applicationContext
85 .getResources("classpath*:META-INF/MANIFEST.MF");
86 ApplicationResource mainResource = applicationContext.getResource("/META-INF/MANIFEST.MF");
87if (mainResource != null) {
88 resources.add(mainResource);
89 }
90for (ApplicationResource resource : resources) {
91 InputStream stream = resource.getInputStream();
92try {
93 Manifest manifest = new Manifest(stream);
94 Attributes attributes = manifest.getMainAttributes();
95if (attributes != null) {
96 String initializerName = attributes.getValue("Tiles-Initializer");
97if (initializerName != null) {
98TilesInitializer initializer = (TilesInitializer) ClassUtil
99 .instantiate(initializerName);
100 initializers.add(initializer);
101 }
102 }
103 } finally {
104 stream.close();
105 }
106 }
107 } catch (IOException e) {
108thrownewDefinitionsFactoryException("Error getting manifest files", e);
109 }
110 }
111 }