com.crsn.maven.utils.osgirepo.osgi.OsgiRepository Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kar-packager Show documentation
Show all versions of kar-packager Show documentation
The kar-packager is a small Maven Plugin to create Apache Karaf Archives (.kar) from a given folder. Everything the
folder needs to contain are the bundles to make up the archive.
The newest version!
package com.crsn.maven.utils.osgirepo.osgi;
import java.io.File;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;
public class OsgiRepository {
private List bundles;
private OsgiRepository( List bundles ) {
this.bundles = bundles;
}
public static OsgiRepository createRepository( File directory ) {
LinkedList bundles = new LinkedList();
if( directory == null ) {
throw new NullPointerException( "Null directory." );
}
File[] listFiles = directory.listFiles();
for( File file : listFiles ) {
addBundleToRepository( bundles, file );
}
return new OsgiRepository( bundles );
}
private static void addBundleToRepository( LinkedList bundles, File file ) {
try {
boolean isJarBundle = file.isFile() && file.getName().toLowerCase().endsWith( "jar" );
if( isJarBundle ) {
JarOsgiBundle osgiBundle = JarOsgiBundle.createBundle( file );
addBundle( bundles, osgiBundle );
} else if( file.isDirectory() ) {
DirectoryOsgiBundle osgiBundle;
osgiBundle = DirectoryOsgiBundle.createBundle( file );
addBundle( bundles, osgiBundle );
}
} catch( IsNotBundleException e ) {
throw new IllegalStateException( "Could not create bundle.", e );
}
}
private static void addBundle( LinkedList plugins, OsgiBundle osgiPlugin ) {
plugins.add( osgiPlugin );
}
public static OsgiRepository createRepository( List plugins ) {
return new OsgiRepository( new LinkedList( plugins ) );
}
public List getPlugins() {
return Collections.unmodifiableList( bundles );
}
public OsgiBundle resolveDependency( OsgiDependency dependency ) {
if( dependency == null ) {
throw new NullPointerException( "Null dependency." );
}
TreeSet resolved = new TreeSet( new Comparator() {
@Override
public int compare( OsgiBundle o1, OsgiBundle o2 ) {
return o1.getVersion().compareTo( o2.getVersion() );
}
} );
for( OsgiBundle osgiPlugin : this.bundles ) {
if( dependency.isResolvedBy( osgiPlugin ) ) {
resolved.add( osgiPlugin );
}
}
if( resolved.isEmpty() ) {
return null;
}
return resolved.last();
}
}