You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tiles.apache.org by ap...@apache.org on 2009/09/26 21:38:30 UTC
svn commit: r819196 - in /tiles/framework/trunk/tiles-extras/src:
main/java/org/apache/tiles/extras/module/
test/java/org/apache/tiles/extras/module/ test/resources/
test/resources/META-INF/
Author: apetrelli
Date: Sat Sep 26 19:38:30 2009
New Revision: 819196
URL: http://svn.apache.org/viewvc?rev=819196&view=rev
Log:
TILES-457
Added modular initializer.
Added:
tiles/framework/trunk/tiles-extras/src/main/java/org/apache/tiles/extras/module/
tiles/framework/trunk/tiles-extras/src/main/java/org/apache/tiles/extras/module/ModularTilesInitializer.java (with props)
tiles/framework/trunk/tiles-extras/src/main/java/org/apache/tiles/extras/module/package.html (with props)
tiles/framework/trunk/tiles-extras/src/test/java/org/apache/tiles/extras/module/
tiles/framework/trunk/tiles-extras/src/test/java/org/apache/tiles/extras/module/ModularTilesInitializerTest.java (with props)
tiles/framework/trunk/tiles-extras/src/test/resources/FAKE-MANIFEST.MF
tiles/framework/trunk/tiles-extras/src/test/resources/META-INF/
tiles/framework/trunk/tiles-extras/src/test/resources/META-INF/MANIFEST.MF
Added: tiles/framework/trunk/tiles-extras/src/main/java/org/apache/tiles/extras/module/ModularTilesInitializer.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-extras/src/main/java/org/apache/tiles/extras/module/ModularTilesInitializer.java?rev=819196&view=auto
==============================================================================
--- tiles/framework/trunk/tiles-extras/src/main/java/org/apache/tiles/extras/module/ModularTilesInitializer.java (added)
+++ tiles/framework/trunk/tiles-extras/src/main/java/org/apache/tiles/extras/module/ModularTilesInitializer.java Sat Sep 26 19:38:30 2009
@@ -0,0 +1,123 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tiles.extras.module;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+
+import javax.servlet.ServletContext;
+
+import org.apache.tiles.TilesApplicationContext;
+import org.apache.tiles.definition.DefinitionsFactoryException;
+import org.apache.tiles.reflect.ClassUtil;
+import org.apache.tiles.servlet.wildcard.WildcardServletTilesApplicationContext;
+import org.apache.tiles.startup.TilesInitializer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Loads Tiles modules, initializes them and destroy them at the end.<br>
+ * It loads all META-INF/MANIFEST.MF files, checks for the "Tiles-Initializer"
+ * property that must contain a valid class name of a {@link TilesInitializer}.
+ * After that, initializes all found initializers, one by one. When the
+ * {@link #destroy()} method is called, all the initializers are then destroyed.
+ *
+ * @version $Rev$ $Date$
+ * @since 2.2.1
+ */
+public class ModularTilesInitializer implements TilesInitializer {
+
+ /**
+ * The logging object.
+ */
+ private Logger logger = LoggerFactory.getLogger(getClass());
+
+ /**
+ * The initializers to use.
+ */
+ private List<TilesInitializer> initializers;
+
+ /** {@inheritDoc} */
+ public void initialize(TilesApplicationContext preliminaryContext) {
+ TilesApplicationContext applicationContext = new WildcardServletTilesApplicationContext(
+ (ServletContext) preliminaryContext.getContext());
+ loadInitializers(applicationContext);
+
+ for (TilesInitializer initializer : initializers) {
+ initializer.initialize(preliminaryContext);
+ }
+ }
+
+ /** {@inheritDoc} */
+ public void destroy() {
+ for (TilesInitializer initializer : initializers) {
+ initializer.destroy();
+ }
+ }
+
+ /**
+ * Load all the initializers from manifest files.
+ *
+ * @param applicationContext The application context.
+ */
+ private void loadInitializers(TilesApplicationContext applicationContext) {
+ initializers = new ArrayList<TilesInitializer>();
+ try {
+ Set<URL> urls = applicationContext
+ .getResources("classpath*:META-INF/MANIFEST.MF");
+ try {
+ URL mainUrl = applicationContext.getResource("/META-INF/MANIFEST.MF");
+ if (mainUrl != null) {
+ urls.add(mainUrl);
+ }
+ } catch (FileNotFoundException e) {
+ logger.debug("Cannot find main manifest, ignoring the problem", e);
+ }
+ for (URL url : urls) {
+ InputStream stream = url.openStream();
+ try {
+ Manifest manifest = new Manifest(stream);
+ Attributes attributes = manifest.getMainAttributes();
+ if (attributes != null) {
+ String initializerName = attributes.getValue("Tiles-Initializer");
+ if (initializerName != null) {
+ TilesInitializer initializer = (TilesInitializer) ClassUtil
+ .instantiate(initializerName);
+ initializers.add(initializer);
+ }
+ }
+ } finally {
+ stream.close();
+ }
+ }
+ } catch (IOException e) {
+ throw new DefinitionsFactoryException("Error getting manifest files", e);
+ }
+ }
+}
Propchange: tiles/framework/trunk/tiles-extras/src/main/java/org/apache/tiles/extras/module/ModularTilesInitializer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: tiles/framework/trunk/tiles-extras/src/main/java/org/apache/tiles/extras/module/ModularTilesInitializer.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: tiles/framework/trunk/tiles-extras/src/main/java/org/apache/tiles/extras/module/package.html
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-extras/src/main/java/org/apache/tiles/extras/module/package.html?rev=819196&view=auto
==============================================================================
--- tiles/framework/trunk/tiles-extras/src/main/java/org/apache/tiles/extras/module/package.html (added)
+++ tiles/framework/trunk/tiles-extras/src/main/java/org/apache/tiles/extras/module/package.html Sat Sep 26 19:38:30 2009
@@ -0,0 +1,30 @@
+<!--
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+-->
+<html>
+<head>
+ <title>Modular initialization of Tiles</title>
+</head>
+<body>
+These classes allow to initialize independent module of Tiles.
+</body>
+</html>
\ No newline at end of file
Propchange: tiles/framework/trunk/tiles-extras/src/main/java/org/apache/tiles/extras/module/package.html
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: tiles/framework/trunk/tiles-extras/src/main/java/org/apache/tiles/extras/module/package.html
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: tiles/framework/trunk/tiles-extras/src/test/java/org/apache/tiles/extras/module/ModularTilesInitializerTest.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-extras/src/test/java/org/apache/tiles/extras/module/ModularTilesInitializerTest.java?rev=819196&view=auto
==============================================================================
--- tiles/framework/trunk/tiles-extras/src/test/java/org/apache/tiles/extras/module/ModularTilesInitializerTest.java (added)
+++ tiles/framework/trunk/tiles-extras/src/test/java/org/apache/tiles/extras/module/ModularTilesInitializerTest.java Sat Sep 26 19:38:30 2009
@@ -0,0 +1,124 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tiles.extras.module;
+
+import static org.junit.Assert.*;
+import static org.easymock.EasyMock.*;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import javax.servlet.ServletContext;
+
+import org.apache.tiles.TilesApplicationContext;
+import org.apache.tiles.startup.TilesInitializer;
+import org.junit.Test;
+
+/**
+ * Tests {@link ModularTilesInitializer}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class ModularTilesInitializerTest {
+
+ /**
+ * Tests {@link ModularTilesInitializer#initialize(TilesApplicationContext)}
+ * and {@link ModularTilesInitializer#destroy()}.
+ *
+ * @throws MalformedURLException Never thrown.
+ */
+ @Test
+ public void testInitialize() throws MalformedURLException {
+ TilesApplicationContext preliminaryContext = createMock(TilesApplicationContext.class);
+ ServletContext servletContext = createMock(ServletContext.class);
+ URL manifestUrl = getClass().getResource("/FAKE-MANIFEST.MF");
+
+ expect(preliminaryContext.getContext()).andReturn(servletContext);
+ expect(servletContext.getResource("/META-INF/MANIFEST.MF")).andReturn(manifestUrl);
+
+ replay(preliminaryContext, servletContext);
+ ModularTilesInitializer initializer = new ModularTilesInitializer();
+ initializer.initialize(preliminaryContext);
+ assertTrue(TilesInitializer1.initialized);
+ assertTrue(TilesInitializer2.initialized);
+ initializer.destroy();
+ assertTrue(TilesInitializer1.destroyed);
+ assertTrue(TilesInitializer2.destroyed);
+ verify(preliminaryContext, servletContext);
+ }
+
+ /**
+ * A mock {@link TilesInitializer} with probes.
+ *
+ * @version $Rev$ $Date$
+ */
+ public static class TilesInitializer1 implements TilesInitializer {
+
+ /**
+ * A probe to see if the initializer has been initialized.
+ */
+ private static boolean initialized = false;
+
+ /**
+ * A probe to see if the initializer has been destroyed.
+ */
+ private static boolean destroyed = false;
+
+ /** {@inheritDoc} */
+ public void initialize(TilesApplicationContext preliminaryContext) {
+ initialized = true;
+ }
+
+ /** {@inheritDoc} */
+ public void destroy() {
+ destroyed = true;
+ }
+ }
+
+ /**
+ * A second mock {@link TilesInitializer} with probes.
+ *
+ * @version $Rev$ $Date$
+ */
+ public static class TilesInitializer2 implements TilesInitializer {
+
+ /**
+ * A probe to see if the initializer has been initialized.
+ */
+ private static boolean initialized = false;
+
+ /**
+ * A probe to see if the initializer has been destroyed.
+ */
+ private static boolean destroyed = false;
+
+ /** {@inheritDoc} */
+ public void initialize(TilesApplicationContext preliminaryContext) {
+ initialized = true;
+ }
+
+ /** {@inheritDoc} */
+ public void destroy() {
+ destroyed = true;
+ }
+ }
+}
Propchange: tiles/framework/trunk/tiles-extras/src/test/java/org/apache/tiles/extras/module/ModularTilesInitializerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: tiles/framework/trunk/tiles-extras/src/test/java/org/apache/tiles/extras/module/ModularTilesInitializerTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: tiles/framework/trunk/tiles-extras/src/test/resources/FAKE-MANIFEST.MF
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-extras/src/test/resources/FAKE-MANIFEST.MF?rev=819196&view=auto
==============================================================================
--- tiles/framework/trunk/tiles-extras/src/test/resources/FAKE-MANIFEST.MF (added)
+++ tiles/framework/trunk/tiles-extras/src/test/resources/FAKE-MANIFEST.MF Sat Sep 26 19:38:30 2009
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Tiles-Initializer: org.apache.tiles.extras.module.ModularTilesInitializerTest$TilesInitializer2
+
Added: tiles/framework/trunk/tiles-extras/src/test/resources/META-INF/MANIFEST.MF
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-extras/src/test/resources/META-INF/MANIFEST.MF?rev=819196&view=auto
==============================================================================
--- tiles/framework/trunk/tiles-extras/src/test/resources/META-INF/MANIFEST.MF (added)
+++ tiles/framework/trunk/tiles-extras/src/test/resources/META-INF/MANIFEST.MF Sat Sep 26 19:38:30 2009
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Tiles-Initializer: org.apache.tiles.extras.module.ModularTilesInitializerTest$TilesInitializer1
+