Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you 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.apache.aries.application.resolver.obr;
 
 import java.io.File;
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
 import  org.osgi.service.obr.RepositoryAdmin;
 import  org.osgi.service.obr.Requirement;
 import  org.osgi.service.obr.Resolver;
 import  org.osgi.service.obr.Resource;

Version:
$Rev: 917781 $ $Date: 2010-03-01 23:00:12 +0000 (Mon, 01 Mar 2010) $
 
 public class OBRAriesResolver implements AriesApplicationResolver
 {
   private static Logger log = LoggerFactory.getLogger(OBRAriesResolver.class);
 
   private final RepositoryAdmin repositoryAdmin;
 
   public OBRAriesResolver(RepositoryAdmin repositoryAdmin)
   {
     this. = repositoryAdmin;
   }

  
This method is synchronized because it changes the repositories understood by OBR, and we don't want one apps by value content being used to resolve another. I'll ask for an OBR enhancement.
 
   public synchronized Set<BundleInforesolve(AriesApplication appResolveConstraint... constraintsthrows ResolverException
   {
     .trace("resolving {}"app);
     Resolver obrResolver = .resolver();
     
     ApplicationMetadata appMeta = app.getApplicationMetadata();
     
     String appName = appMeta.getApplicationSymbolicName();
     Version appVersion = appMeta.getApplicationVersion();
     List<ContentappContent = appMeta.getApplicationContents();
 
     // add a resource describing the requirements of the application metadata.
     obrResolver.add(new ApplicationResourceImpl(appNameappVersionappContent));
 
     URL appRepoURL = null;
     
     try {
       Document doc = RepositoryDescriptorGenerator.generateRepositoryDescriptor(appName + "_" + appVersionapp.getBundleInfo());
       
       File f = File.createTempFile(appName + "_" + appVersion"repository.xml");
       TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), new StreamResult(f));
       
       appRepoURL = f.toURI().toURL();
       
       .addRepository(appRepoURL);
       f.delete();
     } catch (Exception e) {
      throw new ResolverException(e);
    } 
    
    try {
      if (obrResolver.resolve()) {
        Set<BundleInforesult = new HashSet<BundleInfo>();
        for (Resource resourceobrResolver.getRequiredResources()) {
          BundleInfo bundleInfo = toBundleInfo(resource);
          result.add(bundleInfo);
        }
        for (Resource resourceobrResolver.getOptionalResources()) {
          BundleInfo bundleInfo = toBundleInfo(resource);
          result.add(bundleInfo);
        }
        return result;
      } else {
        throw new ResolverException("Could not resolve requirements: " + getUnsatisfiedRequirements(obrResolver));
      }
    } finally {
      if (appRepoURL != null) {
        .removeRepository(appRepoURL);
      }
    }
  }
  public BundleInfo getBundleInfo(String bundleSymbolicNameVersion bundleVersion)
  {
    Map<StringStringattribs = new HashMap<StringString>();
    attribs.put(Resource.VERSION, bundleVersion.toString());
    String filterString = ManifestHeaderProcessor.generateFilter(Resource.SYMBOLIC_NAME, bundleSymbolicNameattribs);
    Resource[] resources = .discoverResources(filterString);
    if (resources != null && resources.length > 0) {
      return toBundleInfo(resources[0]);
    } else {
      return null;
    }
  }
  private String getUnsatisfiedRequirements(Resolver resolver)
  {
    Requirement[] reqs = resolver.getUnsatisfiedRequirements();
    if (reqs != null) {
      StringBuilder sb = new StringBuilder();
      for (int reqIdx = 0; reqIdx < reqs.length; reqIdx++) {
        sb.append("   " + reqs[reqIdx].getFilter()).append("\n");
        Resource[] resources = resolver.getResources(reqs[reqIdx]);
        for (int resIdx = 0; resIdx < resources.length; resIdx++) {
          sb.append("      " + resources[resIdx].getPresentationName()).append("\n");
        }
      }
      return sb.toString();
    }
    return null;
  }
  private BundleInfo toBundleInfo(Resource resource)
  {
    String location = resource.getURL().toExternalForm();
    return new OBRBundleInfo(resource.getSymbolicName(),
            resource.getVersion(),
            location,
            null,
            null,
            null,
            null,
            null
            null,
            null,
            null);
  }
New to GrepCode? Check out our FAQ X