View Javadoc

1   /*
2    * $Id: ModularTilesInitializer.java 1297705 2012-03-06 20:44:30Z nlebas $
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  package org.apache.tiles.extras.module;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.List;
29  import java.util.jar.Attributes;
30  import java.util.jar.Manifest;
31  
32  import javax.servlet.ServletContext;
33  
34  import org.apache.tiles.definition.DefinitionsFactoryException;
35  import org.apache.tiles.request.ApplicationContext;
36  import org.apache.tiles.request.ApplicationResource;
37  import org.apache.tiles.request.reflect.ClassUtil;
38  import org.apache.tiles.request.servlet.wildcard.WildcardServletApplicationContext;
39  import org.apache.tiles.startup.TilesInitializer;
40  
41  /**
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 the
46   * {@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.1
50   */
51  public class ModularTilesInitializer implements TilesInitializer {
52  
53      /**
54       * The initializers to use.
55       */
56      private List<TilesInitializer> initializers;
57  
58      /** {@inheritDoc} */
59      public void initialize(ApplicationContext preliminaryContext) {
60          ApplicationContext applicationContext = new WildcardServletApplicationContext(
61                  (ServletContext) preliminaryContext.getContext());
62          loadInitializers(applicationContext);
63  
64          for (TilesInitializer initializer : initializers) {
65              initializer.initialize(preliminaryContext);
66          }
67      }
68  
69      /** {@inheritDoc} */
70      public void destroy() {
71          for (TilesInitializer initializer : initializers) {
72              initializer.destroy();
73          }
74      }
75  
76      /**
77       * Load all the initializers from manifest files.
78       *
79       * @param applicationContext The application context.
80       */
81      private void loadInitializers(ApplicationContext applicationContext) {
82          initializers = new ArrayList<TilesInitializer>();
83          try {
84              Collection<ApplicationResource> resources = applicationContext
85                      .getResources("classpath*:META-INF/MANIFEST.MF");
86              ApplicationResource mainResource = applicationContext.getResource("/META-INF/MANIFEST.MF");
87              if (mainResource != null) {
88                  resources.add(mainResource);
89              }
90              for (ApplicationResource resource : resources) {
91                  InputStream stream = resource.getInputStream();
92                  try {
93                      Manifest manifest = new Manifest(stream);
94                      Attributes attributes = manifest.getMainAttributes();
95                      if (attributes != null) {
96                          String initializerName = attributes.getValue("Tiles-Initializer");
97                          if (initializerName != null) {
98                              TilesInitializer initializer = (TilesInitializer) ClassUtil
99                                      .instantiate(initializerName);
100                             initializers.add(initializer);
101                         }
102                     }
103                 } finally {
104                     stream.close();
105                 }
106             }
107         } catch (IOException e) {
108             throw new DefinitionsFactoryException("Error getting manifest files", e);
109         }
110     }
111 }