1 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 m_os = null; 48 private String m_processor = null; 49 private LibraryInfo[] m_libraries = null; 50 51 public OSGiLibrarySource( 52 BundleCache cache, long bundleId, int revision, 53 String os, String 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 getPath(String name) throws IllegalStateException 74 { 75 if (!m_opened) 76 { 77 throw new IllegalStateException ("OSGiLibrarySource is not open"); 78 } 79 80 if (m_libraries != null) 81 { 82 String libname = System.mapLibraryName(name); 83 84 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 ex) { 104 Oscar.error("OSGiLibrarySource: Error finding library.", ex); 105 } 106 } 107 } 108 109 return null; 110 } 111 112 private boolean checkOS(String [] osnames) 113 { 114 for (int i = 0; (osnames != null) && (i < osnames.length); i++) 115 { 116 String 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 [] processors) 127 { 128 for (int i = 0; (processors != null) && (i < processors.length); i++) 129 { 130 String 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 147 private String normalizePropertyValue(String prop, String 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 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 |