1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 2 | public class ModularTilesInitializer implements TilesInitializer { |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
private List<TilesInitializer> initializers; |
57 | |
|
58 | |
|
59 | |
public void initialize(ApplicationContext preliminaryContext) { |
60 | 1 | ApplicationContext applicationContext = new WildcardServletApplicationContext( |
61 | |
(ServletContext) preliminaryContext.getContext()); |
62 | 1 | loadInitializers(applicationContext); |
63 | |
|
64 | 1 | for (TilesInitializer initializer : initializers) { |
65 | 2 | initializer.initialize(preliminaryContext); |
66 | 2 | } |
67 | 1 | } |
68 | |
|
69 | |
|
70 | |
public void destroy() { |
71 | 1 | for (TilesInitializer initializer : initializers) { |
72 | 2 | initializer.destroy(); |
73 | 2 | } |
74 | 1 | } |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
private void loadInitializers(ApplicationContext applicationContext) { |
82 | 1 | initializers = new ArrayList<TilesInitializer>(); |
83 | |
try { |
84 | 1 | Collection<ApplicationResource> resources = applicationContext |
85 | |
.getResources("classpath*:META-INF/MANIFEST.MF"); |
86 | 1 | ApplicationResource mainResource = applicationContext.getResource("/META-INF/MANIFEST.MF"); |
87 | 1 | if (mainResource != null) { |
88 | 1 | resources.add(mainResource); |
89 | |
} |
90 | 1 | for (ApplicationResource resource : resources) { |
91 | 48 | InputStream stream = resource.getInputStream(); |
92 | |
try { |
93 | 48 | Manifest manifest = new Manifest(stream); |
94 | 48 | Attributes attributes = manifest.getMainAttributes(); |
95 | 48 | if (attributes != null) { |
96 | 48 | String initializerName = attributes.getValue("Tiles-Initializer"); |
97 | 48 | if (initializerName != null) { |
98 | 2 | TilesInitializer initializer = (TilesInitializer) ClassUtil |
99 | |
.instantiate(initializerName); |
100 | 2 | initializers.add(initializer); |
101 | |
} |
102 | |
} |
103 | |
} finally { |
104 | 48 | stream.close(); |
105 | 48 | } |
106 | 48 | } |
107 | 0 | } catch (IOException e) { |
108 | 0 | throw new DefinitionsFactoryException("Error getting manifest files", e); |
109 | 1 | } |
110 | 1 | } |
111 | |
} |