Package aQute.bnd.osgi

Examples of aQute.bnd.osgi.Analyzer


     * Executes this mojo. We'll use the bnd library in order to scan classes
     * from our target bundle.
     */
    public void execute() throws MojoExecutionException
    {
        Analyzer analyzer = null;
        Jar jar = null;

        try
        {
            // Get the name of our target bundle we are parsing for annotations.
            File target = getBundleName();
            getLog().info("Generating DM component descriptors for bundle " + target);

            // Create a bnd analyzer and analyze our target bundle classes.
            analyzer = new Analyzer();
            analyzer.setJar(target);
            analyzer.analyze();

            // This helper class will parse classes using the analyzer we just created.
            DescriptorGenerator generator = new DescriptorGenerator(analyzer, new MvnLogger(getLog(), m_log));

            // Start scanning
            if (generator.execute())
            {
                // Some annotations have been parsed.
                // Add the list of generated component descriptors in our
                // special header.
                jar = analyzer.getJar();
                jar.getManifest().getMainAttributes()
                    .putValue( "DependencyManager-Component", generator.getDescriptorPaths() );

                // Add generated descriptors into the target bundle (we'll use a
                // temp file).
View Full Code Here


                    warning("File has no manifest");
            }

            if ((options & (USES | USEDBY)) != 0) {
                out.println();
                Analyzer analyzer = new Analyzer();
                analyzer.setPedantic(isPedantic());
                analyzer.setJar(jar);
                analyzer.analyze();
                if ((options & USES) != 0) {
                    out.println("[USES]");
                    printMultiMap(analyzer.getUses());
                    out.println();
                }
                if ((options & USEDBY) != 0) {
                    out.println("[USEDBY]");
                    Map<PackageRef,Set<PackageRef>> usedBy = CollectionUtil.invertMapOfCollection(analyzer.getUses());
                    printMultiMap(usedBy);
                }
                out.println();
            }
View Full Code Here

                    warning("File has no manifest");
            }

            if ((options & (USES | USEDBY)) != 0) {
                out.println();
                Analyzer analyzer = new Analyzer();
                try {
                    analyzer.setPedantic(isPedantic());
                    analyzer.setJar(jar);
                    analyzer.analyze();
                    if ((options & USES) != 0) {
                        out.println("[USES]");
                        printMultiMap(analyzer.getUses());
                        out.println();
                    }
                    if ((options & USEDBY) != 0) {
                        out.println("[USEDBY]");
                        Map<PackageRef,Set<PackageRef>> usedBy = CollectionUtil.invertMapOfCollection(analyzer.getUses());
                        printMultiMap(usedBy);
                    }
                    analyzer.setJar((Jar) null);
                } finally {
                    analyzer.close();
                }
                out.println();
            }

            if ((options & COMPONENT) != 0) {
View Full Code Here

            String importPackages = bundleManifest.getMainAttributes().getValue( "Import-Package" );
            if ( importPackages != null )
            {
                Set optionalPackages = getOptionalPackages( currentProject );

                Map<String, ? extends Map<String, String>> values = new Analyzer().parseHeader( importPackages );
                for ( Map.Entry<String, ? extends Map<String, String>> entry : values.entrySet() )
                {
                    String pkg = entry.getKey();
                    Map<String, String> options = entry.getValue();
                    if ( !options.containsKey( "resolution:" ) && optionalPackages.contains( pkg ) )
View Full Code Here

        new Verifier(builder).verify();
    }

    public void testAnalyzer() throws Exception
    {
        Analyzer analyzer = new Analyzer();
        Manifest manifest = new Manifest();
        manifest.read(getClass().getResourceAsStream("/test.mf"));
        Jar jar = new Jar("name");
        jar.setManifest(manifest);
        analyzer.setJar(jar);
        analyzer.analyze();
        new Verifier(analyzer).verify();
    }
View Full Code Here

     */
    public String getBundleSymbolicName( Artifact artifact )
    {
        if ( ( artifact.getFile() != null ) && artifact.getFile().isFile() )
        {
            Analyzer analyzer = new Analyzer();

            JarFile jar = null;
            try
            {
                jar = new JarFile( artifact.getFile(), false );

                if ( jar.getManifest() != null )
                {
                    String symbolicNameAttribute = jar.getManifest().getMainAttributes()
                        .getValue( Analyzer.BUNDLE_SYMBOLICNAME );
                    Map bundleSymbolicNameHeader = analyzer.parseHeader( symbolicNameAttribute );

                    Iterator it = bundleSymbolicNameHeader.keySet().iterator();
                    if ( it.hasNext() )
                    {
                        return ( String ) it.next();
View Full Code Here


    public Manifest getManifest( MavenProject project, Map instructions, Properties properties, Jar[] classpath )
        throws IOException, MojoFailureException, MojoExecutionException, Exception
    {
        Analyzer analyzer = getAnalyzer( project, instructions, properties, classpath );
        boolean hasErrors = reportErrors( "Manifest " + project.getArtifact(), analyzer );
        if ( hasErrors )
        {
            String failok = analyzer.getProperty( "-failok" );
            if ( null == failok || "false".equalsIgnoreCase( failok ) )
            {
                throw new MojoFailureException( "Error(s) found in manifest configuration" );
            }
        }

        Jar jar = analyzer.getJar();

        if ( unpackBundle )
        {
            File outputFile = getOutputDirectory();
            for ( Entry<String, Resource> entry : jar.getResources().entrySet() )
            {
                File entryFile = new File( outputFile, entry.getKey() );
                if ( !entryFile.exists() || entry.getValue().lastModified() == 0 )
                {
                    entryFile.getParentFile().mkdirs();
                    OutputStream os = new FileOutputStream( entryFile );
                    entry.getValue().write( os );
                    os.close();
                }
            }
        }

        Manifest manifest = jar.getManifest();

        // cleanup...
        analyzer.close();

        return manifest;
    }
View Full Code Here

                //                                    + outputFile );
                //                throw new IllegalStateException( "Trying to read and write " + artifact
                //                    + " to the same file, try cleaning: " + outputFile );
            }

            Analyzer analyzer = getAnalyzer( project, instructions, new Properties(), getClasspath( project ) );

            Jar osgiJar = new Jar( project.getArtifactId(), project.getArtifact().getFile() );

            outputFile.getAbsoluteFile().getParentFile().mkdirs();

            Collection exportedPackages;
            if ( isOsgi( osgiJar ) )
            {
                /* if it is already an OSGi jar copy it as is */
                getLog().info(
                    "Using existing OSGi bundle for " + project.getGroupId() + ":" + project.getArtifactId() + ":"
                        + project.getVersion() );
                String exportHeader = osgiJar.getManifest().getMainAttributes().getValue( Analyzer.EXPORT_PACKAGE );
                exportedPackages = analyzer.parseHeader( exportHeader ).keySet();
                FileUtils.copyFile( project.getArtifact().getFile(), outputFile );
            }
            else
            {
                /* else generate the manifest from the packages */
                exportedPackages = analyzer.getExports().keySet();
                Manifest manifest = analyzer.getJar().getManifest();
                osgiJar.setManifest( manifest );
                osgiJar.write( outputFile );
            }

            BundleInfo bundleInfo = addExportedPackages( project, exportedPackages );

            // cleanup...
            analyzer.close();
            osgiJar.close();

            return bundleInfo;
        }
        /* too bad Jar.write throws Exception */
 
View Full Code Here

    if (!FileUtil.createParentDirs(outputJar)) {
      throw new OsgiBuildException("Can't create output directory for '" + outputJar + "'");
    }

    try {
      Analyzer analyzer = new ReportingAnalyzer(myReporter);
      analyzer.setPedantic(false);
      analyzer.setJar(inputJar);
      analyzer.putAll(properties, false);

      if (analyzer.getProperty(Constants.IMPORT_PACKAGE) == null) {
        analyzer.setProperty(Constants.IMPORT_PACKAGE, "*;resolution:=optional");
      }

      if (analyzer.getProperty(Constants.BUNDLE_SYMBOLICNAME) == null) {
        Pattern p = Pattern.compile("(" + Verifier.SYMBOLICNAME.pattern() + ")(-[0-9])?.*\\.jar");
        Matcher m = p.matcher(inputJar.getName());
        if (!m.matches()) {
          throw new OsgiBuildException("Can't calculate output bundle name for '" + inputJar + "' - rename file or use -properties");
        }
        analyzer.setProperty(Constants.BUNDLE_SYMBOLICNAME, m.group(1));
      }

      if (analyzer.getProperty(Constants.EXPORT_PACKAGE) == null) {
        analyzer.setProperty(Constants.EXPORT_PACKAGE, "*");
      }

      JarFile jarFile = new JarFile(inputJar);
      try {
        analyzer.mergeManifest(jarFile.getManifest());
      }
      finally {
        jarFile.close();
      }

      String version = analyzer.getProperty(Constants.BUNDLE_VERSION);
      if (version != null) {
        version = Analyzer.cleanupVersion(version);
        analyzer.setProperty(Constants.BUNDLE_VERSION, version);
      }

      analyzer.calcManifest();

      Jar jar = analyzer.getJar();
      jar.write(outputJar);
      jar.close();
      analyzer.close();
    }
    catch (OsgiBuildException e) {
      throw e;
    }
    catch (Exception e) {
View Full Code Here

   * Adds the local packages to the headers in the given manifest.
   * @param outputDirectory the output directory where the compiled classes are located
   * @param currentManifest the currently calculated manifest contents.
   */
  public static void addLocalPackages(File outputDirectory, Map<String, String> currentManifest) {
    Analyzer fakeAnalyzer = new FakeAnalyzer(currentManifest);
    addLocalPackages(outputDirectory, fakeAnalyzer);
  }
View Full Code Here

TOP

Related Classes of aQute.bnd.osgi.Analyzer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.