001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.felix.framework.util.manifestparser;
020    
021    import java.util.Map;
022    import org.osgi.framework.Constants;
023    
024    public class R4Library
025    {
026        private String m_libraryFile;
027        private String[] m_osnames;
028        private String[] m_processors;
029        private String[] m_osversions;
030        private String[] m_languages;
031        private String m_selectionFilter;
032    
033        public R4Library(
034            String libraryFile, String[] osnames, String[] processors, String[] osversions,
035            String[] languages, String selectionFilter) throws Exception
036        {
037            m_libraryFile = libraryFile;
038            m_osnames = osnames;
039            m_processors = processors;
040            m_osversions = osversions;
041            m_languages = languages;
042            m_selectionFilter = selectionFilter;
043        }
044    
045        public String getEntryName()
046        {
047            return m_libraryFile;
048        }
049    
050        public String[] getOSNames()
051        {
052            return m_osnames;
053        }
054    
055        public String[] getProcessors()
056        {
057            return m_processors;
058        }
059    
060        public String[] getOSVersions()
061        {
062            return m_osversions;
063        }
064    
065        public String[] getLanguages()
066        {
067            return m_languages;
068        }
069    
070        public String getSelectionFilter()
071        {
072            return m_selectionFilter;
073        }
074    
075        /**
076         * <p>
077         * Determines if the specified native library name matches this native
078         * library definition.
079         * </p>
080         * @param name the native library name to try to match.
081         * @return <tt>true</tt> if this native library name matches this native
082         *         library definition; <tt>false</tt> otherwise.
083        **/
084        public boolean match(Map configMap, String name)
085        {
086            String libname = System.mapLibraryName(name);
087            String[] exts = ManifestParser.parseDelimitedString(
088                (String) configMap.get(Constants.FRAMEWORK_LIBRARY_EXTENSIONS), ",");
089            int extIdx = 0;
090    
091            // First try to match the default name, then try to match any additionally
092            // specified library extensions.
093            do
094            {
095                if (m_libraryFile.equals(libname) || m_libraryFile.endsWith("/" + libname))
096                {
097                    return true;
098                }
099                else if (libname.endsWith(".jnilib") && m_libraryFile.endsWith(".dylib"))
100                {
101                    libname = libname.substring(0, libname.length() - 6) + "dylib";
102                    if (m_libraryFile.equals(libname) || m_libraryFile.endsWith("/" + libname))
103                    {
104                        return true;
105                    }
106                }
107                else if (m_libraryFile.equals(name) || m_libraryFile.endsWith("/" + name))
108                {
109                    return true;
110                }
111    
112                // If we have other native library extensions to try, then
113                // calculate the new native library name.
114                if ((exts != null) && (extIdx < exts.length))
115                {
116                    int idx = libname.lastIndexOf(".");
117                    libname = (idx < 0)
118                        ? libname + "." + exts[extIdx++]
119                        : libname.substring(0, idx) + "." + exts[extIdx++];
120                }
121            }
122            while ((exts != null) && (extIdx < exts.length));
123    
124            return false;
125        }
126    
127        public String toString()
128        {
129            if (m_libraryFile != null)
130            {
131                StringBuffer sb = new StringBuffer();
132                sb.append(m_libraryFile);
133                for (int i = 0; (m_osnames != null) && (i < m_osnames.length); i++)
134                {
135                    sb.append(';');
136                    sb.append(Constants.BUNDLE_NATIVECODE_OSNAME);
137                    sb.append('=');
138                    sb.append(m_osnames[i]);
139                }
140                for (int i = 0; (m_processors != null) && (i < m_processors.length); i++)
141                {
142                    sb.append(';');
143                    sb.append(Constants.BUNDLE_NATIVECODE_PROCESSOR);
144                    sb.append('=');
145                    sb.append(m_processors[i]);
146                }
147                for (int i = 0; (m_osversions != null) && (i < m_osversions.length); i++)
148                {
149                    sb.append(';');
150                    sb.append(Constants.BUNDLE_NATIVECODE_OSVERSION);
151                    sb.append('=');
152                    sb.append(m_osversions[i]);
153                }
154                for (int i = 0; (m_languages != null) && (i < m_languages.length); i++)
155                {
156                    sb.append(';');
157                    sb.append(Constants.BUNDLE_NATIVECODE_LANGUAGE);
158                    sb.append('=');
159                    sb.append(m_languages[i]);
160                }
161                if (m_selectionFilter != null)
162                {
163                    sb.append(';');
164                    sb.append(Constants.SELECTION_FILTER_ATTRIBUTE);
165                    sb.append('=');
166                    sb.append('\'');
167                    sb.append(m_selectionFilter);
168                }
169    
170                return sb.toString();
171            }
172            return "*";
173        }
174    }