1 36 package org.ungoverned.oscar; 37 38 import java.io.InputStream ; 39 import java.security.AccessController ; 40 import java.security.PrivilegedAction ; 41 import java.util.*; 42 43 import org.osgi.framework.*; 44 import org.ungoverned.moduleloader.LibrarySource; 45 import org.ungoverned.moduleloader.ResourceSource; 46 import org.ungoverned.moduleloader.SystemResourceSource; 47 import org.ungoverned.moduleloader.search.ImportSearchPolicy; 48 import org.ungoverned.oscar.util.CaseInsensitiveMap; 49 import org.ungoverned.oscar.util.OscarConstants; 50 51 class SystemBundle extends BundleImpl 52 { 53 private List m_activatorList = null; 54 private BundleActivator m_activator = null; 55 private Thread m_shutdownThread = null; 56 private Object [][] m_attributes = null; 57 private ResourceSource[] m_resSources = null; 58 private LibrarySource[] m_libSources = null; 59 60 protected SystemBundle(Oscar oscar, BundleInfo info, List activatorList) 61 throws BundleException 62 { 63 super(oscar, info); 64 65 if (activatorList == null) 67 { 68 activatorList = new ArrayList(); 69 } 70 71 activatorList.add(new PackageAdminActivator(oscar)); 73 74 activatorList.add(new StartLevelActivator(oscar)); 76 77 m_activatorList = activatorList; 78 79 int[] version120 = { 1, 2, 0 }; 80 int[] version100 = { 1, 0, 0 }; 81 Object [][] exports = new Object [][] { 82 { "org.osgi.framework", version120, null }, 83 { "org.osgi.service.packageadmin", version120, null }, 84 { "org.osgi.service.startlevel", version100, null } 85 }; 86 m_attributes = new Object [][] { 87 new Object [] { "exports", exports }, 88 new Object [] { "imports", new Object [0][0] }, 89 new Object [] { "propagates", new Object [0] } 90 }; 91 m_resSources = new ResourceSource[] { 92 new SystemResourceSource() 93 }; 94 m_libSources = null; 95 96 String exportString = ""; 97 for (int i = 0; i < exports.length; i++) 98 { 99 if (i < (exports.length - 1)) 100 { 101 exportString = exportString + 102 exports[i][ImportSearchPolicy.IDENTIFIER_IDX] 103 + "; specification-version=\"" 104 + ((int[])exports[i][ImportSearchPolicy.VERSION_IDX])[0] + "." 105 + ((int[])exports[i][ImportSearchPolicy.VERSION_IDX])[1] + "." 106 + ((int[])exports[i][ImportSearchPolicy.VERSION_IDX])[2] + "\", "; 107 } 108 else 109 { 110 exportString = exportString + 111 exports[i][ImportSearchPolicy.IDENTIFIER_IDX] 112 + "; specification-version=\"" 113 + ((int[])exports[i][ImportSearchPolicy.VERSION_IDX])[0] + "." 114 + ((int[])exports[i][ImportSearchPolicy.VERSION_IDX])[1] + "." 115 + ((int[])exports[i][ImportSearchPolicy.VERSION_IDX])[2] + "\""; 116 } 117 } 118 119 Map map = new CaseInsensitiveMap(); 121 map.put(OscarConstants.BUNDLE_VERSION, OscarConstants.OSCAR_VERSION_VALUE); 122 map.put(OscarConstants.BUNDLE_NAME, "System Bundle"); 123 map.put(OscarConstants.BUNDLE_DESCRIPTION, 124 "This bundle is system specific; it implements various system services."); 125 map.put(OscarConstants.EXPORT_PACKAGE, exportString); 126 ((SystemBundleArchive) getInfo().getArchive()).setManifestHeader(map); 127 } 128 129 public Object [][] getAttributes() 130 { 131 return m_attributes; 132 } 133 134 public ResourceSource[] getResourceSources() 135 { 136 return m_resSources; 137 } 138 139 public LibrarySource[] getLibrarySources() 140 { 141 return m_libSources; 142 } 143 144 public synchronized void start() throws BundleException 145 { 146 Oscar.debug("SystemBundle.start()"); 147 148 if (getState() == Bundle.ACTIVE) 151 { 152 throw new BundleException("Cannot start the system bundle."); 153 } 154 155 getInfo().setState(Bundle.STARTING); 156 157 try { 158 getInfo().setContext(new BundleContextImpl(getOscar(), this)); 159 getActivator().start(getInfo().getContext()); 160 } catch (Throwable throwable) { 161 throwable.printStackTrace(); 162 throw new BundleException( 163 "Unable to start system bundle.", throwable); 164 } 165 166 } 170 171 public synchronized void stop() throws BundleException 172 { 173 Oscar.debug("SystemBundle.stop()"); 174 175 if (System.getSecurityManager() != null) 176 { 177 AccessController.checkPermission(new AdminPermission()); 178 } 179 180 if (getOscar().getFrameworkStatus() == Oscar.RUNNING_STATUS) 183 { 184 m_shutdownThread = new Thread ("OscarShutdown") { 186 public void run() 187 { 188 try { 189 getOscar().shutdown(); 190 } catch (Exception ex) { 191 Oscar.error("SystemBundle: Error while shutting down."); 192 Oscar.error("SystemBundle: " + ex); 193 } 194 195 String embedded = getOscar().getConfigProperty( 197 OscarConstants.EMBEDDED_EXECUTION_PROP); 198 boolean isEmbedded = (embedded == null) 199 ? false : embedded.equals("true"); 200 if (!isEmbedded) 201 { 202 if (System.getSecurityManager() != null) 203 { 204 AccessController.doPrivileged(new PrivilegedAction () { 205 public Object run() 206 { 207 System.exit(0); 208 return null; 209 } 210 }); 211 } 212 else 213 { 214 System.exit(0); 215 } 216 } 217 } 218 }; 219 getInfo().setState(Bundle.STOPPING); 220 m_shutdownThread.start(); 221 } 222 else if ((getOscar().getFrameworkStatus() == Oscar.STOPPING_STATUS) && 223 (Thread.currentThread() == m_shutdownThread)) 224 { 225 try { 227 getActivator().stop(getInfo().getContext()); 228 } catch (Throwable throwable) { 229 throw new BundleException( 230 "Unable to stop system bundle.", throwable); 231 } 232 } 233 } 234 235 public synchronized void uninstall() throws BundleException 236 { 237 Oscar.debug("SystemBundle.uninstall()"); 238 throw new BundleException("Cannot uninstall the system bundle."); 239 } 240 241 public synchronized void update() throws BundleException 242 { 243 update(null); 244 } 245 246 public synchronized void update(InputStream is) throws BundleException 247 { 248 Oscar.debug("SystemBundle.update()"); 249 250 if (System.getSecurityManager() != null) 251 { 252 AccessController.checkPermission(new AdminPermission()); 253 } 254 255 throw new BundleException("System bundle update not implemented yet."); 257 } 258 259 protected BundleActivator getActivator() 260 throws Exception 261 { 262 if (m_activator == null) 263 { 264 m_activator = new SystemBundleActivator(getOscar(), m_activatorList); 265 } 266 return m_activator; 267 } 268 } 269 | Popular Tags |