⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 activator.java

📁 OSGI这是一个中间件,与UPNP齐名,是用于移植到嵌入式平台之上
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if (dla != null) {            for (int i = 0; i < dla.length; i++) {                try {                    DriverLocator dl = dla[i];                    String[] dria = dl.findDrivers(props);                    for (int j = 0; j < dria.length; j++) {                        String dri = dria[j];                        MatchImpl m = null;                        for (int k = 0; k < matches.size(); k++) {                            m = (MatchImpl) matches.elementAt(k);                            if (m.equals(dri))                                break;                            m = null;                        }                        if (m == null) {                            m = new MatchImpl(this, dev, dri);                            matches.addElement(m);                        }                        m.addDriverLocator(dl);                    }                } catch (Exception e) {                }            }        }        for (;;) {            // Add current drivers to matches            for (int i = 0; i < drivers.size(); i++) {                DriverRef dr = (DriverRef) drivers.elementAt(i);                MatchImpl m = null;                for (int k = 0; k < matches.size(); k++) {                    m = (MatchImpl) matches.elementAt(k);                    if (m.connect(dr))                        break;                    m = null;                }                if (m == null) {                    m = new MatchImpl(this, dev, dr);                    matches.addElement(m);                }            }            int n = 0;            boolean loading = false;            // Count good matches and trigger loading            for (int i = 0; i < matches.size(); i++) {                MatchImpl m = (MatchImpl) matches.elementAt(i);                int match = m.getMatchValue();                if (match == MatchImpl.UNKNOWN)                    loading = true;                else if (match > Device.MATCH_NONE)                    n++;            }            // If not finished loading continue            if (loading)                continue;            // If nothing matches we're done            if (n == 0) {                tellNotFound(dev);                return;            }            // Filter out good matches and select default best match            MatchImpl best = null;            Match[] sel = new Match[n];            n = 0;            for (int i = 0; i < matches.size(); i++) {                MatchImpl m = (MatchImpl) matches.elementAt(i);                if (m.getMatchValue() > Device.MATCH_NONE) {                    sel[n++] = m;                    if (best == null || best.compare(m) < 0)                        best = m;                }            }            // Maybe use driver selector instead            DriverSelector ds = selector;            if (ds != null) {                int ix = ds.select(dev, sel);                if (ix == DriverSelector.SELECT_NONE) {                    tellNotFound(dev);                    return;                }                try {                    best = (MatchImpl) sel[ix];                } catch (Exception e) {                }            }            // If attach succeeds we're done            String ref = null;            try {                ref = best.attach();            } catch (Exception e) {                error("failed attach " + showDriver(best.getDriver()) + " -> "                        + showDevice(dev));                continue;            }            if (best.getMatchValue() > Device.MATCH_NONE) {                // Just loaded, go around and pick up the driver ref                continue;            }            if (ref == null) {                info("attached " + showDriver(best.getDriver()) + " -> "                        + showDevice(dev));                Bundle b = best.getBundle();                if (b != null)                    updateLife(b, LONG_LIFE);                return;            }            info(showDriver(best.getDriver()) + " refers to " + ref);            // Append the referred match            MatchImpl m = null;            for (int i = 0; i < matches.size(); i++) {                m = (MatchImpl) matches.elementAt(i);                if (m.equals(ref))                    break;                m = null;            }            if (m == null) {                m = new MatchImpl(this, dev, ref);                matches.addElement(m);            }        }    }    private void updateLife(Bundle b, long t) {        tempDrivers.put(b, new Long(System.currentTimeMillis() + t));    }    private boolean isUsed(ServiceReference sr) {        Bundle[] ba = sr.getUsingBundles();        if (ba != null) {            for (int i = 0; i < ba.length; i++) {                Bundle b = ba[i];                try {                    for (int j = 0; j < drivers.size(); j++) {                        DriverRef dr = (DriverRef) drivers.elementAt(j);                        if (dr.sr.getBundle() == b)                            return true;                    }                } catch (Exception e) {                    return true;                }            }        }        return false;    }    private void reapDrivers() {        long now = System.currentTimeMillis();        Bundle[] ba = bc.getBundles();        if (ba != null) {            for (int i = 0; i < ba.length; i++) {                try {                    Bundle b = ba[i];                    if (b.getLocation().startsWith(DYNAMIC_DRIVER_TAG)) {                        Long expire = (Long) tempDrivers.get(b);                        boolean inUse = false;                        ServiceReference[] sra = b.getServicesInUse();                        if (sra != null) {                            for (int j = 0; j < sra.length; j++) {                                if (isDevice.match(sra[j])) {                                    inUse = true;                                    break;                                }                            }                        }                        if (inUse) {                            updateLife(b, LONG_LIFE);                        } else if (expire == null) {                            updateLife(b, SHORT_LIFE);                        } else if (expire.longValue() < now) {                            info("uninstalling " + b.getLocation());                            b.uninstall();                        }                    }                } catch (Exception e) {                }            }        }    }    private void touchDevice(ServiceReference dev) {        if (newDevices.put(dev, dev) == null) {            synchronized (this) {                notifyAll();            }        }    }    private void touchAllDevices() {        boolean added = false;        try {            ServiceReference[] sra = bc.getServiceReferences(null,                    DEVICE_FILTER);            if (sra != null) {                for (int i = 0; i < sra.length; i++) {                    ServiceReference dev = sra[i];                    if (newDevices.put(dev, dev) == null)                        added = true;                }            }        } catch (Exception e) {        }        if (added) {            synchronized (this) {                notifyAll();            }        }    }    private void tellNotFound(ServiceReference dev) {        // NB: Should we avoid repeating the call to the same device?        info("no driver for " + showDevice(dev));        Object d = null;        try {            d = bc.getService(dev);            ((Device) d).noDriverFound();        } catch (Exception e) {        } finally {            try {                bc.ungetService(dev);            } catch (Exception e1) {            }        }    }    private Dictionary /* String->Object */collectProperties(            ServiceReference sr) {        Dictionary props = new Hashtable();        String[] keys = sr.getPropertyKeys();        if (keys != null) {            for (int i = 0; i < keys.length; i++) {                String key = keys[i];                props.put(key, sr.getProperty(key));            }        }        return props;    }    Bundle installBundle(String name, InputStream is) {        Bundle b = null;        try {            info("installing " + name);            b = bc.installBundle(name, is);            b.start();            updateLife(b, SHORT_LIFE);            return b;        } catch (Exception e) {            error("failed to install " + name);            try {                b.uninstall();            } catch (Exception e1) {            }            return null;        } finally {            try {                is.close();            } catch (Exception e1) {            }        }    }    void removeSRCachedMatch(ServiceReference sr) {        // NB: Index to speed up this process?        String pid = null;        try {            pid = (String) sr                    .getProperty(org.osgi.framework.Constants.SERVICE_PID);        } catch (Exception e) {        }        if (pid != null)            return;        for (Enumeration e = cache.keys(); e.hasMoreElements();) {            Integer k = (Integer) e.nextElement();            MatchValue mv0 = (MatchValue) cache.get(k);            MatchValue mv = mv0;            while (mv != null) {                if (mv.dev != sr) {                    // Keep it                    mv0 = mv;                    mv = mv.next;                } else if (mv == mv0) {                    // Update hash table entry                    mv0 = mv = mv.next;                    if (mv != null)                        cache.put(k, mv);                    else                        cache.remove(k);                } else {                    // Link past this one                    mv = mv.next;                    mv0.next = mv;                }            }        }    }    int getCachedMatch(String drvid, ServiceReference dev) {        MatchValue mv = findMatch(drvid, dev, false);        return mv != null ? mv.match : MatchImpl.UNKNOWN;    }    void putCachedMatch(String drvid, ServiceReference dev, int match) {        MatchValue mv = findMatch(drvid, dev, true);        mv.match = match;    }    private MatchValue findMatch(String drvid, ServiceReference dev,            boolean create) {        String pid = null;        try {            pid = (String) dev                    .getProperty(org.osgi.framework.Constants.SERVICE_PID);        } catch (Exception e) {        }        int k1 = pid != null ? pid.hashCode() : dev.hashCode();        int k2 = drvid.hashCode();        Integer key = new Integer(k1 + k2);        MatchValue mv0 = (MatchValue) cache.get(key);        MatchValue mv = mv0;        while (mv != null) {            if (drvid.equals(mv.drvid) && pid != null ? pid.equals(mv.pid)                    : dev == mv.dev)                return mv;            mv = mv.next;        }        if (!create)            return null;        mv = new MatchValue();        mv.next = mv0;        mv.key = key;        mv.drvid = drvid;        mv.pid = pid;        if (pid == null)            mv.dev = dev;        cache.put(key, mv);        return mv;    }    private String showDevice(ServiceReference sr) {        StringBuffer sb = new StringBuffer();        Object o = sr                .getProperty(org.osgi.service.device.Constants.DEVICE_CATEGORY);        if (o instanceof String) {            sb.append(o);        } else if (o instanceof String[]) {            String[] dca = (String[]) o;            for (int i = 0; i < dca.length; i++) {                if (i > 0)                    sb.append('_');                sb.append(dca[i]);            }        }        o = sr.getProperty(org.osgi.framework.Constants.SERVICE_ID);        if (o != null)            sb.append(o);        Bundle b = sr.getBundle();        if (b != null) {            sb.append('/');            sb.append(b.getBundleId());        }        return sb.toString();    }    private String showDriver(ServiceReference sr) {        StringBuffer sb = new StringBuffer();        String s = (String) sr                .getProperty(org.osgi.service.device.Constants.DRIVER_ID);        sb.append(s != null ? s : "driver");        Bundle b = sr.getBundle();        if (b != null) {            sb.append('/');            sb.append(b.getBundleId());        }        return sb.toString();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -