Karl Pauls | 9647e84 | 2007-02-12 23:46:56 +0000 | [diff] [blame] | 1 | /* |
Richard S. Hall | b395167 | 2006-09-19 17:04:53 +0000 | [diff] [blame] | 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 9 | * |
Richard S. Hall | b395167 | 2006-09-19 17:04:53 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 11 | * |
Richard S. Hall | b395167 | 2006-09-19 17:04:53 +0000 | [diff] [blame] | 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 18 | */ |
| 19 | package org.apache.felix.framework.cache; |
| 20 | |
Carsten Ziegeler | 37d2c29 | 2007-08-15 10:03:15 +0000 | [diff] [blame] | 21 | import java.io.File; |
| 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.List; |
| 26 | import java.util.Map; |
| 27 | import java.util.jar.Manifest; |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 28 | |
| 29 | import org.apache.felix.framework.Logger; |
Richard S. Hall | 7860542 | 2006-12-18 20:47:55 +0000 | [diff] [blame] | 30 | import org.apache.felix.framework.util.FelixConstants; |
| 31 | import org.apache.felix.framework.util.StringMap; |
| 32 | import org.apache.felix.framework.util.manifestparser.ManifestParser; |
Carsten Ziegeler | 37d2c29 | 2007-08-15 10:03:15 +0000 | [diff] [blame] | 33 | import org.apache.felix.moduleloader.DirectoryContent; |
| 34 | import org.apache.felix.moduleloader.IContent; |
| 35 | import org.apache.felix.moduleloader.JarContent; |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * <p> |
| 39 | * This class implements a bundle archive revision for exploded bundle |
| 40 | * JAR files. It uses the specified location directory "in-place" to |
| 41 | * execute the bundle and does not copy the bundle content at all. |
| 42 | * </p> |
| 43 | **/ |
| 44 | class DirectoryRevision extends BundleRevision |
| 45 | { |
Karl Pauls | 836bb40 | 2006-08-24 12:39:46 +0000 | [diff] [blame] | 46 | private static final transient String BUNDLE_JAR_FILE = "bundle.jar"; |
| 47 | |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 48 | private File m_refDir = null; |
| 49 | private Map m_header = null; |
| 50 | |
| 51 | public DirectoryRevision( |
Karl Pauls | 836bb40 | 2006-08-24 12:39:46 +0000 | [diff] [blame] | 52 | Logger logger, File revisionRootDir, String location) throws Exception |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 53 | { |
| 54 | super(logger, revisionRootDir, location); |
Carsten Ziegeler | 7cbcef5 | 2007-08-15 11:37:08 +0000 | [diff] [blame^] | 55 | m_refDir = new File(location.substring( |
Richard S. Hall | 04bdbb1 | 2006-03-15 14:26:15 +0000 | [diff] [blame] | 56 | location.indexOf(BundleArchive.FILE_PROTOCOL) |
| 57 | + BundleArchive.FILE_PROTOCOL.length())); |
Richard S. Hall | 5d9b924 | 2006-07-20 13:20:05 +0000 | [diff] [blame] | 58 | |
| 59 | // If the revision directory exists, then we don't |
| 60 | // need to initialize since it has already been done. |
Carsten Ziegeler | 37d2c29 | 2007-08-15 10:03:15 +0000 | [diff] [blame] | 61 | if (BundleCache.getSecureAction().fileExists(this.getRevisionRootDir())) |
Richard S. Hall | 5d9b924 | 2006-07-20 13:20:05 +0000 | [diff] [blame] | 62 | { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | // Create revision directory, we only need this to store the |
| 67 | // revision location, since nothing else needs to be extracted |
| 68 | // since we are referencing a read directory already. |
Carsten Ziegeler | 37d2c29 | 2007-08-15 10:03:15 +0000 | [diff] [blame] | 69 | if (!BundleCache.getSecureAction().mkdir(this.getRevisionRootDir())) |
Richard S. Hall | 5d9b924 | 2006-07-20 13:20:05 +0000 | [diff] [blame] | 70 | { |
Carsten Ziegeler | 37d2c29 | 2007-08-15 10:03:15 +0000 | [diff] [blame] | 71 | this.getLogger().log( |
Richard S. Hall | 5d9b924 | 2006-07-20 13:20:05 +0000 | [diff] [blame] | 72 | Logger.LOG_ERROR, |
Carsten Ziegeler | 37d2c29 | 2007-08-15 10:03:15 +0000 | [diff] [blame] | 73 | this.getClass().getName() + ": Unable to create revision directory."); |
Richard S. Hall | 5d9b924 | 2006-07-20 13:20:05 +0000 | [diff] [blame] | 74 | throw new IOException("Unable to create archive directory."); |
| 75 | } |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | public synchronized Map getManifestHeader() |
| 79 | throws Exception |
| 80 | { |
Carsten Ziegeler | 7cbcef5 | 2007-08-15 11:37:08 +0000 | [diff] [blame^] | 81 | if (m_header != null) |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 82 | { |
Carsten Ziegeler | 7cbcef5 | 2007-08-15 11:37:08 +0000 | [diff] [blame^] | 83 | return m_header; |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // Read the header file from the reference directory. |
| 87 | InputStream is = null; |
| 88 | |
| 89 | try |
| 90 | { |
| 91 | // Open manifest file. |
Richard S. Hall | 04bdbb1 | 2006-03-15 14:26:15 +0000 | [diff] [blame] | 92 | is = BundleCache.getSecureAction() |
Carsten Ziegeler | 7cbcef5 | 2007-08-15 11:37:08 +0000 | [diff] [blame^] | 93 | .getFileInputStream(new File(m_refDir, "META-INF/MANIFEST.MF")); |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 94 | // Error if no jar file. |
| 95 | if (is == null) |
| 96 | { |
| 97 | throw new IOException("No manifest file found."); |
| 98 | } |
| 99 | |
| 100 | // Get manifest. |
| 101 | Manifest mf = new Manifest(is); |
| 102 | // Create a case insensitive map of manifest attributes. |
Carsten Ziegeler | 7cbcef5 | 2007-08-15 11:37:08 +0000 | [diff] [blame^] | 103 | m_header = new StringMap(mf.getMainAttributes(), false); |
| 104 | return m_header; |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 105 | } |
| 106 | finally |
| 107 | { |
| 108 | if (is != null) is.close(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | public IContent getContent() throws Exception |
| 113 | { |
Carsten Ziegeler | 7cbcef5 | 2007-08-15 11:37:08 +0000 | [diff] [blame^] | 114 | return new DirectoryContent(m_refDir); |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | public synchronized IContent[] getContentPath() throws Exception |
| 118 | { |
| 119 | // Creating the content path entails examining the bundle's |
| 120 | // class path to determine whether the bundle JAR file itself |
| 121 | // is on the bundle's class path and then creating content |
| 122 | // objects for everything on the class path. |
Karl Pauls | 836bb40 | 2006-08-24 12:39:46 +0000 | [diff] [blame] | 123 | |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 124 | // Get the bundle's manifest header. |
Carsten Ziegeler | 37d2c29 | 2007-08-15 10:03:15 +0000 | [diff] [blame] | 125 | Map map = this.getManifestHeader(); |
Karl Pauls | 836bb40 | 2006-08-24 12:39:46 +0000 | [diff] [blame] | 126 | |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 127 | // Find class path meta-data. |
| 128 | String classPath = (map == null) |
| 129 | ? null : (String) map.get(FelixConstants.BUNDLE_CLASSPATH); |
Karl Pauls | 836bb40 | 2006-08-24 12:39:46 +0000 | [diff] [blame] | 130 | |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 131 | // Parse the class path into strings. |
Richard S. Hall | 7860542 | 2006-12-18 20:47:55 +0000 | [diff] [blame] | 132 | String[] classPathStrings = ManifestParser.parseDelimitedString( |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 133 | classPath, FelixConstants.CLASS_PATH_SEPARATOR); |
Karl Pauls | 836bb40 | 2006-08-24 12:39:46 +0000 | [diff] [blame] | 134 | |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 135 | if (classPathStrings == null) |
| 136 | { |
| 137 | classPathStrings = new String[0]; |
| 138 | } |
Karl Pauls | 836bb40 | 2006-08-24 12:39:46 +0000 | [diff] [blame] | 139 | |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 140 | // Create the bundles class path. |
Carsten Ziegeler | 7cbcef5 | 2007-08-15 11:37:08 +0000 | [diff] [blame^] | 141 | IContent self = new DirectoryContent(m_refDir); |
Richard S. Hall | 77db181 | 2006-10-17 15:05:23 +0000 | [diff] [blame] | 142 | List contentList = new ArrayList(); |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 143 | for (int i = 0; i < classPathStrings.length; i++) |
| 144 | { |
| 145 | if (classPathStrings[i].equals(FelixConstants.CLASS_PATH_DOT)) |
| 146 | { |
Richard S. Hall | 77db181 | 2006-10-17 15:05:23 +0000 | [diff] [blame] | 147 | contentList.add(self); |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 148 | } |
| 149 | else |
| 150 | { |
| 151 | // Determine if the class path entry is a file or directory. |
Carsten Ziegeler | 7cbcef5 | 2007-08-15 11:37:08 +0000 | [diff] [blame^] | 152 | File file = new File(m_refDir, classPathStrings[i]); |
Richard S. Hall | 04bdbb1 | 2006-03-15 14:26:15 +0000 | [diff] [blame] | 153 | if (BundleCache.getSecureAction().isFileDirectory(file)) |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 154 | { |
Richard S. Hall | 77db181 | 2006-10-17 15:05:23 +0000 | [diff] [blame] | 155 | contentList.add(new DirectoryContent(file)); |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 156 | } |
| 157 | else |
| 158 | { |
Richard S. Hall | 77db181 | 2006-10-17 15:05:23 +0000 | [diff] [blame] | 159 | // Ignore any entries that do not exist per the spec. |
| 160 | if (BundleCache.getSecureAction().fileExists(file)) |
| 161 | { |
| 162 | contentList.add(new JarContent(file)); |
| 163 | } |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | } |
Karl Pauls | 836bb40 | 2006-08-24 12:39:46 +0000 | [diff] [blame] | 167 | |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 168 | // If there is nothing on the class path, then include |
| 169 | // "." by default, as per the spec. |
Richard S. Hall | 77db181 | 2006-10-17 15:05:23 +0000 | [diff] [blame] | 170 | if (contentList.size() == 0) |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 171 | { |
Richard S. Hall | 4f9905e | 2006-10-18 14:52:59 +0000 | [diff] [blame] | 172 | contentList.add(self); |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 173 | } |
Karl Pauls | 836bb40 | 2006-08-24 12:39:46 +0000 | [diff] [blame] | 174 | |
Richard S. Hall | 77db181 | 2006-10-17 15:05:23 +0000 | [diff] [blame] | 175 | return (IContent[]) contentList.toArray(new IContent[contentList.size()]); |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // TODO: This will need to consider security. |
| 179 | public String findLibrary(String libName) throws Exception |
| 180 | { |
Carsten Ziegeler | 37d2c29 | 2007-08-15 10:03:15 +0000 | [diff] [blame] | 181 | return BundleCache.getSecureAction().getAbsolutePath(new File(this.m_refDir, libName)); |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | public void dispose() throws Exception |
| 185 | { |
| 186 | // Nothing to dispose of, since we don't maintain any state outside |
| 187 | // of the revision directory, which will be automatically deleted |
| 188 | // by the parent bundle archive. |
| 189 | } |
Richard S. Hall | 4f9905e | 2006-10-18 14:52:59 +0000 | [diff] [blame] | 190 | } |