Eclipse equinox implementation of OSGiEclipse equinox implementation of OSGi

  • Bundle
  •  1 package org.osgi.framework;
     2 public interface Bundle extends Comparable<Bundle> {    
     3     int    UNINSTALLED                = 0x00000001;
     4     int    INSTALLED                = 0x00000002;
     5     int    RESOLVED                = 0x00000004;
     6     int    STARTING                = 0x00000008;
     7     int    STOPPING                = 0x00000010;
     8     int    ACTIVE                    = 0x00000020;
     9     int    START_TRANSIENT            = 0x00000001;
    10     int    START_ACTIVATION_POLICY    = 0x00000002;
    11     int    STOP_TRANSIENT            = 0x00000001;
    12     int    SIGNERS_ALL                = 1;
    13     int    SIGNERS_TRUSTED            = 2;
    14     int getState();
    15     voidstart(int options) throws BundleException;
    16     void start() throws BundleException;
    17     voidstop(int options) throws BundleException;
    18     void stop() throws BundleException;
    19     void update(InputStream input) throws BundleException;
    20     void update() throws BundleException;
    21     void uninstall() throws BundleException;
    22     Dictionary<String, String> getHeaders();
    23     long getBundleId();
    24     String getLocation();
    25     ServiceReference<?>[] getRegisteredServices();
    26     ServiceReference<?>[] getServicesInUse();
    27     boolean hasPermission(Object permission);
    28     URL getResource(String name);
    29     Enumeration<String> getEntryPaths(String path);
    30     URL getEntry(String path);
    31     long getLastModified();
    32     Enumeration<URL> findEntries(String path, String filePattern, boolean recurse);
    33     BundleContext getBundleContext();
    34     Map<X509Certificate, List<X509Certificate>> getSignerCertificates(int signersType);
    35     Version getVersion();
    36     <A> A adapt(Class<A> type);
    37     File getDataFile(String filename);
    38 }
  • Framework: also known as Bundle
  •  1 package org.osgi.framework.launch;
     2 
     3 import java.io.InputStream;
     4 import java.net.URL;
     5 import java.util.Enumeration;
     6 import org.osgi.framework.Bundle;
     7 import org.osgi.framework.BundleException;
     8 import org.osgi.framework.Constants;
     9 import org.osgi.framework.FrameworkEvent;
    10 public interface Framework extends Bundle {
    11     void init() throws BundleException;
    12     FrameworkEvent waitForStop(long timeout) throws InterruptedException;
    13     void start() throws BundleException;
    14     void start(int options) throws BundleException;
    15     void stop() throws BundleException;
    16     void stop(int options) throws BundleException;
    17     void uninstall() throws BundleException;
    18     void update() throws BundleException;
    19     void update(InputStream in) throws BundleException;
    20     long getBundleId();
    21     String getLocation();
    22     String getSymbolicName();
    23     Enumeration<String> getEntryPaths(String path);
    24     URL getEntry(String path);
    25     Enumeration<URL> findEntries(String path, String filePattern, boolean recurse);
    26     <A> A adapt(Class<A> type);
    27 }
  • FrameworkFactory
  •  1 package org.osgi.framework.launch;
     2 import java.util.Map;
     3 import org.osgi.framework.Bundle;
     4 /**
     5  * A factory for creating {@link Framework} instances. 
     6  * @ThreadSafe
     7  * @noimplement
     8  * @version $Id: 1684e14aa98a1f6e1ff3e0f3afa2c55982210f72 $
     9  */
    10 public interface FrameworkFactory {
    11     Framework newFramework(Map<String, String> configuration);
    12 }
  • EquinoxLauncher: implement the interface of Framework

Configuration

Eclipse equinox implementation of OSGi

 

dev.properties

1 #
2 #Wed Sep 11 08:37:19 CST 2013
3 com.dragon.osgi.hello=bin
4 @ignoredot@=true

           config.ini          

1 #Configuration File
2 #Wed Sep 11 08:37:19 CST 2013
3 osgi.bundles=reference\:file\:G\:/eclipse/eclipse/plugins/org.apache.felix.gogo.runtime_0.8.0.v201108120515.jar@start,reference\:file\:G\:/eclipse/eclipse/plugins/org.apache.felix.gogo.shell_0.8.0.v201110170705.jar@start,reference\:file\:G\:/eclipse/eclipse/plugins/org.apache.felix.gogo.command_0.8.0.v201108120515.jar@start,reference\:file\:G\:/eclipse/Workspace/com.dragon.osgi.hello@start,reference\:file\:G\:/eclipse/eclipse/plugins/org.eclipse.equinox.console_1.0.0.v20120522-1841.jar@start
4 osgi.bundles.defaultStartLevel=4
5 osgi.install.area=file\:G\:\\eclipse\\eclipse
6 osgi.framework=file\:G\:/eclipse/eclipse/plugins/org.eclipse.osgi_3.8.0.v20120529-1548.jar
7 osgi.configuration.cascaded=false

 

  •   1 /*******************************************************************************
      2  * Copyright (c) 2006, 2011 Cognos Incorporated, IBM Corporation and others.
      3  * All rights reserved. This program and the accompanying materials
      4  * are made available under the terms of the Eclipse Public License v1.0
      5  * which accompanies this distribution, and is available at
      6  * http://www.eclipse.org/legal/epl-v10.html
      7  * 
      8  *******************************************************************************/
      9 package org.eclipse.osgi.framework.internal.core;
     10 
     11 import java.io.UnsupportedEncodingException;
     12 import java.lang.reflect.Method;
     13 import java.net.URL;
     14 import java.net.URLDecoder;
     15 import java.security.CodeSource;
     16 import java.util.*;
     17 import org.eclipse.core.runtime.internal.adaptor.EclipseAdaptorMsg;
     18 import org.eclipse.osgi.util.NLS;
     19 
     20 /*
     21  * This class should be used in ALL places in the framework implementation to get "system" properties.
     22  * The static methods on this class should be used instead of the System#getProperty, System#setProperty etc methods.
     23  */
     24 public class FrameworkProperties {
     25 
     26     /**@GuardedBy FrameworkProperties.class*/
     27     private static Properties properties;
     28 
     29     // A flag of some sort will have to be supported. 
     30     // Many existing plugins get framework propeties directly from System instead of BundleContext. 
     31     // Note that the OSGi TCK is one example where this property MUST be set to false because many TCK bundles set and read system properties.
     32     private static final String USING_SYSTEM_PROPERTIES_KEY = "osgi.framework.useSystemProperties"; //$NON-NLS-1$
     33     private static final String PROP_FRAMEWORK = "osgi.framework"; //$NON-NLS-1$
     34     private static final String PROP_INSTALL_AREA = "osgi.install.area"; //$NON-NLS-1$
     35 
     36     public static Properties getProperties() {
     37         SecurityManager sm = System.getSecurityManager();
     38         if (sm != null)
     39             sm.checkPropertiesAccess();
     40         return internalGetProperties(null);
     41     }
     42 
     43     public static String getProperty(String key) {
     44         return getProperty(key, null);
     45     }
     46 
     47     public static String getProperty(String key, String defaultValue) {
     48         SecurityManager sm = System.getSecurityManager();
     49         if (sm != null)
     50             sm.checkPropertyAccess(key);
     51         return internalGetProperties(null).getProperty(key, defaultValue);
     52     }
     53 
     54     public static String setProperty(String key, String value) {
     55         SecurityManager sm = System.getSecurityManager();
     56         if (sm != null)
     57             sm.checkPermission(new PropertyPermission(key, "write")); //$NON-NLS-1$
     58         return (String) internalGetProperties(null).put(key, value);
     59     }
     60 
     61     public static String clearProperty(String key) {
     62         SecurityManager sm = System.getSecurityManager();
     63         if (sm != null)
     64             sm.checkPermission(new PropertyPermission(key, "write")); //$NON-NLS-1$
     65         return (String) internalGetProperties(null).remove(key);
     66     }
     67 
     68     private static synchronized Properties internalGetProperties(String usingSystemProperties) {
     69         if (properties == null) {
     70             Properties systemProperties = System.getProperties();
     71             if (usingSystemProperties == null)
     72                 usingSystemProperties = systemProperties.getProperty(USING_SYSTEM_PROPERTIES_KEY);
     73             if (usingSystemProperties == null || usingSystemProperties.equalsIgnoreCase(Boolean.TRUE.toString())) {
     74                 properties = systemProperties;
     75             } else {
     76                 // use systemProperties for a snapshot
     77                 // also see requirements in Bundlecontext.getProperty(...))
     78                 properties = new Properties();
     79                 // snapshot of System properties for uses of getProperties who expect to see framework properties set as System properties
     80                 // we need to do this for all system properties because the properties object is used to back
     81                 // BundleContext#getProperty method which expects all system properties to be available
     82                 synchronized (systemProperties) {
     83                     // bug 360198 - must synchronize on systemProperties to avoid concurrent modification exception
     84                     properties.putAll(systemProperties);
     85                 }
     86             }
     87         }
     88         return properties;
     89     }
     90 
     91     public static synchronized void setProperties(Map<String, String> input) {
     92         if (input == null) {
     93             // just use internal props;  note that this will reuse a previous set of properties if they were set
     94             internalGetProperties("false"); //$NON-NLS-1$
     95             return;
     96         }
     97         properties = null;
     98         Properties toSet = internalGetProperties("false"); //$NON-NLS-1$
     99         for (Iterator<String> keys = input.keySet().iterator(); keys.hasNext();) {
    100             String key = keys.next();
    101             Object value = input.get(key);
    102             if (value instanceof String) {
    103                 toSet.setProperty(key, (String) value);
    104                 continue;
    105             }
    106             value = input.get(key);
    107             if (value != null)
    108                 toSet.put(key, value);
    109             else
    110                 toSet.remove(key);
    111         }
    112     }
    113 
    114     public static synchronized boolean inUse() {
    115         return properties != null;
    116     }
    117 
    118     public static void initializeProperties() {
    119         // initialize some framework properties that must always be set
    120         if (getProperty(PROP_FRAMEWORK) == null || getProperty(PROP_INSTALL_AREA) == null) {
    121             CodeSource cs = FrameworkProperties.class.getProtectionDomain().getCodeSource();
    122             if (cs == null)
    123                 throw new IllegalArgumentException(NLS.bind(EclipseAdaptorMsg.ECLIPSE_STARTUP_PROPS_NOT_SET, PROP_FRAMEWORK + ", " + PROP_INSTALL_AREA)); //$NON-NLS-1$
    124             URL url = cs.getLocation();
    125             // allow props to be preset
    126             if (getProperty(PROP_FRAMEWORK) == null)
    127                 setProperty(PROP_FRAMEWORK, url.toExternalForm());
    128             if (getProperty(PROP_INSTALL_AREA) == null) {
    129                 String filePart = url.getFile();
    130                 setProperty(PROP_INSTALL_AREA, filePart.substring(0, filePart.lastIndexOf('/')));
    131             }
    132         }
    133         // always decode these properties
    134         setProperty(PROP_FRAMEWORK, decode(getProperty(PROP_FRAMEWORK)));
    135         setProperty(PROP_INSTALL_AREA, decode(getProperty(PROP_INSTALL_AREA)));
    136     }
    137 
    138     public static String decode(String urlString) {
    139         //try to use Java 1.4 method if available
    140         try {
    141             Class<? extends URLDecoder> clazz = URLDecoder.class;
    142             Method method = clazz.getDeclaredMethod("decode", new Class[] {String.class, String.class}); //$NON-NLS-1$
    143             //first encode '+' characters, because URLDecoder incorrectly converts 
    144             //them to spaces on certain class library implementations.
    145             if (urlString.indexOf('+') >= 0) {
    146                 int len = urlString.length();
    147                 StringBuffer buf = new StringBuffer(len);
    148                 for (int i = 0; i < len; i++) {
    149                     char c = urlString.charAt(i);
    150                     if (c == '+')
    151                         buf.append("%2B"); //$NON-NLS-1$
    152                     else
    153                         buf.append(c);
    154                 }
    155                 urlString = buf.toString();
    156             }
    157             Object result = method.invoke(null, new Object[] {urlString, "UTF-8"}); //$NON-NLS-1$
    158             if (result != null)
    159                 return (String) result;
    160         } catch (Exception e) {
    161             //JDK 1.4 method not found -- fall through and decode by hand
    162         }
    163         //decode URL by hand
    164         boolean replaced = false;
    165         byte[] encodedBytes = urlString.getBytes();
    166         int encodedLength = encodedBytes.length;
    167         byte[] decodedBytes = new byte[encodedLength];
    168         int decodedLength = 0;
    169         for (int i = 0; i < encodedLength; i++) {
    170             byte b = encodedBytes[i];
    171             if (b == '%') {
    172                 byte enc1 = encodedBytes[++i];
    173                 byte enc2 = encodedBytes[++i];
    174                 b = (byte) ((hexToByte(enc1) << 4) + hexToByte(enc2));
    175                 replaced = true;
    176             }
    177             decodedBytes[decodedLength++] = b;
    178         }
    179         if (!replaced)
    180             return urlString;
    181         try {
    182             return new String(decodedBytes, 0, decodedLength, "UTF-8"); //$NON-NLS-1$
    183         } catch (UnsupportedEncodingException e) {
    184             //use default encoding
    185             return new String(decodedBytes, 0, decodedLength);
    186         }
    187     }
    188 
    189     private static int hexToByte(byte b) {
    190         switch (b) {
    191             case '0' :
    192                 return 0;
    193             case '1' :
    194                 return 1;
    195             case '2' :
    196                 return 2;
    197             case '3' :
    198                 return 3;
    199             case '4' :
    200                 return 4;
    201             case '5' :
    202                 return 5;
    203             case '6' :
    204                 return 6;
    205             case '7' :
    206                 return 7;
    207             case '8' :
    208                 return 8;
    209             case '9' :
    210                 return 9;
    211             case 'A' :
    212             case 'a' :
    213                 return 10;
    214             case 'B' :
    215             case 'b' :
    216                 return 11;
    217             case 'C' :
    218             case 'c' :
    219                 return 12;
    220             case 'D' :
    221             case 'd' :
    222                 return 13;
    223             case 'E' :
    224             case 'e' :
    225                 return 14;
    226             case 'F' :
    227             case 'f' :
    228                 return 15;
    229             default :
    230                 throw new IllegalArgumentException("Switch error decoding URL"); //$NON-NLS-1$
    231         }
    232     }
    233 }
    FrameworkProperties

相关文章: