Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011, the original authors
   *
   * ====================================================================
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * ====================================================================
  */
 
 package org.jclouds.karaf.core.internal;
 
 
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 public class MetadataBundleListener implements BundleListener {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(MetadataBundleListener.class);
 
     private Map<LongApiMetadataapiMetadataMap = new HashMap<LongApiMetadata>();
 
 
 
     private Set<ApiMetadatacomputeApis = new HashSet<ApiMetadata>();
     private Set<ApiMetadatablobStoreApis = new HashSet<ApiMetadata>();
 
 
 
     private BundleContext bundleContext;
 
     public MetadataBundleListener() {
     }
 
     public void init() {
         .addBundleListener(this);
         for (Bundle bundle : .getBundles()) {
             if (bundle.getState() == .) {
                 ProviderMetadata providerMetadata = getProviderMetadata(bundle);
                 if (providerMetadata != null) {
                     notifyInstallation(providerMetadata);
                     .put(bundle.getBundleId(), providerMetadata);
                 }
 
                 ApiMetadata apiMetadata = getApiMetadata(bundle);
                 if (apiMetadata != null) {
                     notifyInstallation(apiMetadata);
                     .put(bundle.getBundleId(), apiMetadata);
                 }
             }
         }
     }
 
     public void destroy() {
         .removeBundleListener(this);
     }
 
 
     @Override
     public void bundleChanged(BundleEvent event) {
         ProviderMetadata providerMetadata;
         ApiMetadata apiMetadata;
         switch (event.getType()) {
             case .:
                 providerMetadata = getProviderMetadata(event.getBundle());
                apiMetadata = getApiMetadata(event.getBundle());
                if (providerMetadata != null) {
                    notifyInstallation(providerMetadata);
                    .put(event.getBundle().getBundleId(), providerMetadata);
                }
                if (apiMetadata != null) {
                    notifyInstallation(apiMetadata);
                    .put(event.getBundle().getBundleId(), apiMetadata);
                }
                break;
            case .:
            case .:
                providerMetadata = .remove(event.getBundle().getBundleId());
                apiMetadata = .remove(event.getBundle().getBundleId());
                if (providerMetadata != null) {
                    notifyRemoval(providerMetadata);
                }
                if (apiMetadata != null) {
                    notifyRemoval(apiMetadata);
                }
                break;
        }
    }

    
Creates an instance of org.jclouds.providers.ProviderMetadata from the org.osgi.framework.Bundle.

Parameters:
bundle
Returns:
    public ProviderMetadata getProviderMetadata(Bundle bundle) {
        ProviderMetadata metadata = null;
        String className = getProviderMetadataClassName(bundle);
        if (className != null && !className.isEmpty()) {
            try {
                Class<? extends ProviderMetadataproviderMetadataClass = bundle.loadClass(className);
                metadata = providerMetadataClass.newInstance();
            } catch (ClassNotFoundException e) {
                // ignore
            } catch (InstantiationException e) {
                // ignore
            } catch (IllegalAccessException e) {
                // ignore
            }
        }
        return metadata;
    }

    
Creates an instance of org.jclouds.apis.ApiMetadata from the org.osgi.framework.Bundle.

Parameters:
bundle
Returns:
    public ApiMetadata getApiMetadata(Bundle bundle) {
        ApiMetadata metadata = null;
        String className = getApiMetadataClassName(bundle);
        if (className != null && !className.isEmpty()) {
            try {
                Class<? extends ApiMetadataapiMetadataClass = bundle.loadClass(className);
                metadata = apiMetadataClass.newInstance();
            } catch (ClassNotFoundException e) {
                // ignore
            } catch (InstantiationException e) {
                // ignore
            } catch (IllegalAccessException e) {
                // ignore
            }
        }
        return metadata;
    }
    public String getMetadataClassName(Bundle bundleString pathToMetadata) {
        URL resource = bundle.getEntry(pathToMetadata);
        InputStream is = null;
        InputStreamReader reader = null;
        BufferedReader bufferedReader = null;
        StringBuilder sb = new StringBuilder();
        try {
            is = resource.openStream();
            reader = new InputStreamReader(is"UTF-8");
            bufferedReader = new BufferedReader(reader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line).append("\n");
            }
        } catch (Throwable e) {
        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (Throwable e) {
            }
            try {
                if (bufferedReader != null)
                    bufferedReader.close();
            } catch (Throwable e) {
            }
            try {
                is.close();
            } catch (Throwable e) {
            }
        }
        return sb.toString().trim();
    }

    
Retrieves the org.jclouds.providers.ProviderMetadata class name for the bundle if it exists.

Parameters:
bundle
Returns:
    public String getProviderMetadataClassName(Bundle bundle) {
        return getMetadataClassName(bundle"/META-INF/services/org.jclouds.providers.ProviderMetadata");
    }

    
Retrieves the org.jclouds.providers.ProviderMetadata class name for the bundle if it exists.

Parameters:
bundle
Returns:
    public String getApiMetadataClassName(Bundle bundle) {
        return getMetadataClassName(bundle"/META-INF/services/org.jclouds.apis.ApiMetadata");
    }


    
Notifies org.jclouds.karaf.core.ProviderOrApiListeners with the installation of a org.jclouds.providers.ProviderMetadata.

Parameters:
metadata
    public void notifyInstallation(ProviderMetadata metadata) {
        if (metadata != null) {
            if (ProviderPredicates.viewableAs(TypeToken.of(ComputeServiceContext.class)).apply(metadata)) {
                .add(metadata);
                for (ProviderOrApiListener providerOrApiListener : ) {
                    providerOrApiListener.providerInstalled(metadata);
                }
            } else if (ProviderPredicates.viewableAs(TypeToken.of(BlobStoreContext.class)).apply(metadata)) {
                .add(metadata);
                for (ProviderOrApiListener providerOrApiListener : ) {
                    providerOrApiListener.providerInstalled(metadata);
                }
            }
        }
    }

    
Notifies org.jclouds.karaf.core.ProviderOrApiListeners with the installation of a org.jclouds.apis.ApiMetadata.

Parameters:
metadata
    public void notifyInstallation(ApiMetadata metadata) {
        if (metadata != null) {
            if (ApiPredicates.viewableAs(TypeToken.of(ComputeServiceContext.class)).apply(metadata)) {
                .add(metadata);
                for (ProviderOrApiListener providerOrApiListener : ) {
                    providerOrApiListener.apiInstalled(metadata);
                }
            } else if (ApiPredicates.viewableAs(TypeToken.of(BlobStoreContext.class)).apply(metadata)) {
                .add(metadata);
                for (ProviderOrApiListener providerOrApiListener : ) {
                    providerOrApiListener.apiInstalled(metadata);
                }
            }
        }
    }

    
    private void notifyRemoval(ProviderMetadata metadata) {
        if (metadata != null) {
            if (ProviderPredicates.viewableAs(TypeToken.of(ComputeServiceContext.class)).apply(metadata)) {
                .remove(metadata);
                for (ProviderOrApiListener providerOrApiListener : ) {
                    providerOrApiListener.providerUninstalled(metadata);
                }
            } else if (ProviderPredicates.viewableAs(TypeToken.of(BlobStoreContext.class)).apply(metadata)) {
                .remove(metadata);
                for (ProviderOrApiListener providerOrApiListener : ) {
                    providerOrApiListener.providerUninstalled(metadata);
                }
            }
        }
    }

    
Notifies org.jclouds.karaf.core.ProviderOrApiListeners with the removal of org.jclouds.apis.ApiMetadata.

Parameters:
metadata
    private void notifyRemoval(ApiMetadata metadata) {
        if (metadata != null) {
            if (ApiPredicates.viewableAs(TypeToken.of(ComputeServiceContext.class)).apply(metadata)) {
                .remove(metadata);
                for (ProviderOrApiListener providerOrApiListener : ) {
                    providerOrApiListener.apiUninstalled(metadata);
                }
            } else if (ApiPredicates.viewableAs(TypeToken.of(BlobStoreContext.class)).apply(metadata)) {
                .remove(metadata);
                for (ProviderOrApiListener providerOrApiListener : ) {
                    providerOrApiListener.apiUninstalled(metadata);
                }
            }
        }
    }


    
Registers a org.jclouds.karaf.core.ComputeProviderOrApiListener

Parameters:
providerOrApiListener
    public void registerComputeListener(ComputeProviderOrApiListener providerOrApiListener) {
        this..add(providerOrApiListener);
        for (ProviderMetadata provider : ) {
            providerOrApiListener.providerInstalled(provider);
        }
        for (ApiMetadata api : ) {
            providerOrApiListener.apiInstalled(api);
        }
    }
    public void unregisterComputeListener(ComputeProviderOrApiListener providerOrApiListener) {
        if (providerOrApiListener != null) {
            this..remove(providerOrApiListener);
            for (ProviderMetadata provider : ) {
                providerOrApiListener.providerUninstalled(provider);
            }
            for (ApiMetadata api : ) {
                providerOrApiListener.apiUninstalled(api);
            }
        }
    }

    
Registers a org.jclouds.karaf.core.ComputeProviderOrApiListener

Parameters:
providerOrApiListener
    public void registerBlobStoreListener(BlobStoreProviderOrApiListener providerOrApiListener) {
        this..add(providerOrApiListener);
        for (ProviderMetadata provider : ) {
            providerOrApiListener.providerInstalled(provider);
        }
        for (ApiMetadata api : ) {
            providerOrApiListener.apiInstalled(api);
        }
    }
    public void unregisterBlobStoreListener(BlobStoreProviderOrApiListener providerOrApiListener) {
        if (providerOrApiListener != null) {
            this..remove(providerOrApiListener);
            for (ProviderMetadata provider : ) {
                providerOrApiListener.providerUninstalled(provider);
            }
            for (ApiMetadata api : ) {
                providerOrApiListener.apiUninstalled(api);
            }
        }
    }
    public BundleContext getBundleContext() {
        return ;
    }
    public void setBundleContext(BundleContext bundleContext) {
        this. = bundleContext;
    }
New to GrepCode? Check out our FAQ X