package io.mypojo.framework;

import io.mypojo.framework.launch.ClasspathScanner;
import io.mypojo.framework.launch.PojoServiceRegistry;
import io.mypojo.framework.launch.impl.PojoServiceRegistryFactoryImpl;
import org.osgi.framework.*;
import org.osgi.framework.launch.Framework;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class PojoSRFrameworkImpl implements Framework {
    private final String m_filter;
    private volatile Bundle m_bundle = null;
    private volatile PojoServiceRegistry m_reg = null;

    public PojoSRFrameworkImpl(Map configuration) {
        m_filter = (String) configuration.get("pojosr.filter");
    }

    public void init() throws BundleException {
        try {
            m_reg = new PojoServiceRegistryFactoryImpl()
                    .newPojoServiceRegistry(new HashMap());
            m_bundle = m_reg.getBundleContext()
                    .getBundle();
        } catch (Exception ex) {
            throw new BundleException("Unable to scan classpath", ex);
        }
    }

    public int getState() {
        return (m_bundle == null) ? Bundle.INSTALLED : m_bundle.getState();
    }

    public void start(int options) throws BundleException {
        start();
    }

    public void start() throws BundleException {
        try {
            m_reg.startBundles((m_filter != null) ? new ClasspathScanner()
                    .scanForBundles(m_filter)
                    : new ClasspathScanner().scanForBundles());
        } catch (Exception e) {
            throw new BundleException("Error starting framework", e);
        }
    }

    public void stop(int options) throws BundleException {
        m_bundle.stop(options);
    }

    public void stop() throws BundleException {
        m_bundle.stop();
    }

    public void update(InputStream input) throws BundleException {
        m_bundle.update(input);
    }

    public void update() throws BundleException {
        m_bundle.update();
    }

    public void uninstall() throws BundleException {
        m_bundle.uninstall();
    }

    public Dictionary getHeaders() {
        return m_bundle.getHeaders();
    }

    public long getBundleId() {
        return m_bundle.getBundleId();
    }

    public String getLocation() {
        return m_bundle.getLocation();
    }

    public ServiceReference[] getRegisteredServices() {
        return m_bundle.getRegisteredServices();
    }

    public ServiceReference[] getServicesInUse() {
        return m_bundle.getServicesInUse();
    }

    public boolean hasPermission(Object permission) {
        return m_bundle.hasPermission(permission);
    }

    public URL getResource(String name) {
        return m_bundle.getResource(name);
    }

    public Dictionary getHeaders(String locale) {
        return m_bundle.getHeaders(locale);
    }

    public String getSymbolicName() {
        return m_bundle.getSymbolicName();
    }

    public Class loadClass(String name) throws ClassNotFoundException {
        return m_bundle.loadClass(name);
    }

    public Enumeration getResources(String name) throws IOException {
        return m_bundle.getResources(name);
    }

    public Enumeration getEntryPaths(String path) {
        return m_bundle.getEntryPaths(path);
    }

    public URL getEntry(String path) {
        return m_bundle.getEntry(path);
    }

    public long getLastModified() {
        return m_bundle.getLastModified();
    }

    public Enumeration findEntries(String path, String filePattern,
                                   boolean recurse) {
        return m_bundle.findEntries(path, filePattern, recurse);
    }

    public BundleContext getBundleContext() {
        return m_bundle.getBundleContext();
    }

    public Map getSignerCertificates(int signersType) {
        return m_bundle.getSignerCertificates(signersType);
    }

    public Version getVersion() {
        return m_bundle.getVersion();
    }

    public FrameworkEvent waitForStop(long timeout)
            throws InterruptedException {
        final Object lock = new Object();

        m_bundle.getBundleContext().addBundleListener(new SynchronousBundleListener() {

            public void bundleChanged(BundleEvent event) {
                if ((event.getBundle() == m_bundle) && (event.getType() == BundleEvent.STOPPED)) {
                    synchronized (lock) {
                        lock.notifyAll();
                    }
                }
            }
        });
        synchronized (lock) {
            while (m_bundle.getState() != Bundle.RESOLVED) {
                if (m_bundle.getState() == Bundle.STOPPING) {
                    lock.wait(100);
                } else {
                    lock.wait();
                }
            }
        }
        return new FrameworkEvent(FrameworkEvent.STOPPED, m_bundle, null);
    }

    public File getDataFile(String filename) {
        return m_bundle.getDataFile(filename);
    }

    public int compareTo(Bundle o) {
        if (o == this) {
            return 0;
        }
        return m_bundle.compareTo(o);
    }

    public <A> A adapt(Class<A> type) {
        return m_bundle.adapt(type);
    }

}

@@ -389,79 +389,37 @@
 ava.
-util.Dictionary;%0Aimport java.util.Enum
+security.cert.X509C
 er
-a
 ti
-on;%0Aimport java.util.HashMap
+ficate
 ;%0Aim
@@ -433,19 +433,17 @@
 va.util.
-Map
+*
 ;%0A%0Apubli
@@ -870,37 +870,16 @@
 ryImpl()
-%0A                    
 .newPojo
@@ -957,37 +957,16 @@
 ontext()
-%0A                    
 .getBund
@@ -2125,32 +2125,48 @@
 ublic Dictionary
+%3CString, String%3E
  getHeaders() %7B%0A
@@ -2807,16 +2807,32 @@
 ctionary
+%3CString, String%3E
  getHead
@@ -3129,24 +3129,29 @@
  Enumeration
+%3CURL%3E
  getResource
@@ -3259,16 +3259,24 @@
 meration
+%3CString%3E
  getEntr
@@ -3543,16 +3543,21 @@
 meration
+%3CURL%3E
  findEnt
@@ -3831,16 +3831,56 @@
 blic Map
+%3CX509Certificate, List%3CX509Certificate%3E%3E
  getSign
@@ -4110,28 +4110,16 @@
 timeout)
-%0A           
  throws 
@@ -4272,17 +4272,16 @@
 ner() %7B%0A
-%0A
         
@@ -4579,16 +4579,88 @@
      %7D);
+%0A%0A        //noinspection SynchronizationOnLocalVariableOrMethodParameter
 %0A       

Fix generics as a result of OSGi API upgrade