001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc;
016:
017: import static org.apache.tapestry.ioc.IOCConstants.MODULE_BUILDER_MANIFEST_ENTRY_NAME;
018:
019: import java.io.Closeable;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.net.URL;
023: import java.util.Enumeration;
024: import java.util.jar.Manifest;
025:
026: /**
027: * A collection of utility methods for a couple of different areas, including creating the initial
028: * {@link org.apache.tapestry.ioc.Registry}.
029: */
030: public final class IOCUtilities {
031: private IOCUtilities() {
032: }
033:
034: /**
035: * Construct a default registry, including modules identify via the Tapestry-Module-Classes
036: * Manifest entry.
037: *
038: * @return constructed Registry, after eager loading of services
039: */
040: public static Registry buildDefaultRegistry() {
041: RegistryBuilder builder = new RegistryBuilder();
042:
043: addDefaultModules(builder);
044:
045: Registry registry = builder.build();
046:
047: registry.eagerLoadServices();
048:
049: return registry;
050: }
051:
052: /**
053: * Scans the classpath for JAR Manifests that contain the Tapestry-Module-Classes attribute and
054: * adds each corresponding class to the RegistryBuilder.
055: *
056: * @param builder
057: * the builder to which modules will be added
058: */
059: public static void addDefaultModules(RegistryBuilder builder) {
060: try {
061: Enumeration<URL> urls = builder.getClassLoader()
062: .getResources("META-INF/MANIFEST.MF");
063:
064: while (urls.hasMoreElements()) {
065: URL url = urls.nextElement();
066:
067: addModulesInManifest(builder, url);
068: }
069: } catch (Exception ex) {
070: throw new RuntimeException(ex.getMessage(), ex);
071: }
072: }
073:
074: private static void addModulesInManifest(RegistryBuilder builder,
075: URL url) throws IOException {
076: InputStream in = null;
077:
078: try {
079: in = url.openStream();
080:
081: Manifest mf = new Manifest(in);
082:
083: in.close();
084:
085: in = null;
086:
087: addModulesInManifest(builder, mf);
088: } finally {
089: close(in);
090: }
091: }
092:
093: static void addModulesInManifest(RegistryBuilder builder,
094: Manifest mf) {
095: String list = mf.getMainAttributes().getValue(
096: MODULE_BUILDER_MANIFEST_ENTRY_NAME);
097:
098: if (list == null)
099: return;
100:
101: String[] classnames = list.split(",");
102:
103: for (String classname : classnames) {
104: builder.add(classname.trim());
105: }
106: }
107:
108: /**
109: * Closes an input stream (or other Closeable), ignoring any exception.
110: *
111: * @param closeable
112: * the thing to close, or null to close nothing
113: */
114: private static void close(Closeable closeable) {
115: if (closeable != null) {
116: try {
117: closeable.close();
118: } catch (IOException ex) {
119: // Ignore.
120: }
121: }
122: }
123: }
|