KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > oscar > OSGiLibrarySource


1 /*
2  * Oscar - An implementation of the OSGi framework.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.oscar;
37
38 import org.osgi.framework.Constants;
39 import org.ungoverned.moduleloader.LibrarySource;
40
41 public class OSGiLibrarySource implements LibrarySource
42 {
43     private boolean m_opened = false;
44     private BundleCache m_cache = null;
45     private long m_bundleId = -1;
46     private int m_revision = -1;
47     private String JavaDoc m_os = null;
48     private String JavaDoc m_processor = null;
49     private LibraryInfo[] m_libraries = null;
50
51     public OSGiLibrarySource(
52         BundleCache cache, long bundleId, int revision,
53         String JavaDoc os, String JavaDoc processor, LibraryInfo[] libraries)
54     {
55         m_cache = cache;
56         m_bundleId = bundleId;
57         m_revision = revision;
58         m_os = normalizePropertyValue(Constants.FRAMEWORK_OS_NAME, os);
59         m_processor = normalizePropertyValue(Constants.FRAMEWORK_PROCESSOR, processor);
60         m_libraries = libraries;
61     }
62
63     public void open()
64     {
65         m_opened = true;
66     }
67
68     public void close()
69     {
70         m_opened = false;
71     }
72
73     public String JavaDoc getPath(String JavaDoc name) throws IllegalStateException JavaDoc
74     {
75         if (!m_opened)
76         {
77             throw new IllegalStateException JavaDoc("OSGiLibrarySource is not open");
78         }
79
80         if (m_libraries != null)
81         {
82             String JavaDoc libname = System.mapLibraryName(name);
83
84             // Check to see if we have a matching library.
85
// TODO: This "matching" algorithm does not fully
86
// match the spec and should be improved.
87
LibraryInfo library = null;
88             for (int i = 0; (library == null) && (i < m_libraries.length); i++)
89             {
90                 boolean osOkay = checkOS(m_libraries[i].getOSNames());
91                 boolean procOkay = checkProcessor(m_libraries[i].getProcessors());
92                 if (m_libraries[i].getName().endsWith(libname) && osOkay && procOkay)
93                 {
94                     library = m_libraries[i];
95                 }
96             }
97
98             if (library != null)
99             {
100                 try {
101                     return m_cache.getArchive(m_bundleId)
102                         .findLibrary(m_revision, library.getName());
103                 } catch (Exception JavaDoc ex) {
104                     Oscar.error("OSGiLibrarySource: Error finding library.", ex);
105                 }
106             }
107         }
108
109         return null;
110     }
111
112     private boolean checkOS(String JavaDoc[] osnames)
113     {
114         for (int i = 0; (osnames != null) && (i < osnames.length); i++)
115         {
116             String JavaDoc osname =
117                 normalizePropertyValue(Constants.FRAMEWORK_OS_NAME, osnames[i]);
118             if (m_os.equals(osname))
119             {
120                 return true;
121             }
122         }
123         return false;
124     }
125
126     private boolean checkProcessor(String JavaDoc[] processors)
127     {
128         for (int i = 0; (processors != null) && (i < processors.length); i++)
129         {
130             String JavaDoc processor =
131                 normalizePropertyValue(Constants.FRAMEWORK_PROCESSOR, processors[i]);
132             if (m_processor.equals(processor))
133             {
134                 return true;
135             }
136         }
137         return false;
138     }
139
140     /**
141      * This is simply a hack to try to create some standardized
142      * property values, since there seems to be many possible
143      * values for each JVM implementation. Currently, this
144      * focuses on Windows and Linux and will certainly need
145      * to be changed in the future or at least edited.
146     **/

147     private String JavaDoc normalizePropertyValue(String JavaDoc prop, String JavaDoc value)
148     {
149         prop = prop.toLowerCase();
150         value = value.toLowerCase();
151
152         if (prop.equals(Constants.FRAMEWORK_OS_NAME))
153         {
154             if (value.startsWith("linux"))
155             {
156                 return "linux";
157             }
158             else if (value.startsWith("win"))
159             {
160                 String JavaDoc os = "win";
161                 if (value.indexOf("95") >= 0)
162                 {
163                     os = "win95";
164                 }
165                 else if (value.indexOf("98") >= 0)
166                 {
167                     os = "win98";
168                 }
169                 else if (value.indexOf("NT") >= 0)
170                 {
171                     os = "winnt";
172                 }
173                 else if (value.indexOf("2000") >= 0)
174                 {
175                     os = "win2000";
176                 }
177                 else if (value.indexOf("xp") >= 0)
178                 {
179                     os = "winxp";
180                 }
181                 return os;
182             }
183         }
184         else if (prop.equals(Constants.FRAMEWORK_PROCESSOR))
185         {
186             if (value.endsWith("86"))
187             {
188                 return "x86";
189             }
190         }
191
192         return value;
193     }
194 }
Popular Tags