View Javadoc
1   package org.argeo.osgi.a2;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.nio.file.FileVisitResult;
6   import java.nio.file.Files;
7   import java.nio.file.Path;
8   import java.nio.file.Paths;
9   import java.nio.file.SimpleFileVisitor;
10  import java.nio.file.attribute.BasicFileAttributes;
11  
12  import org.argeo.osgi.boot.OsgiBootUtils;
13  import org.osgi.framework.Version;
14  
15  /** A file system {@link AbstractProvisioningSource} in Maven 2 format. */
16  public class FsM2Source extends AbstractProvisioningSource {
17  	private final Path base;
18  
19  	public FsM2Source(Path base) {
20  		super();
21  		this.base = base;
22  	}
23  
24  	void load() throws IOException {
25  		Files.walkFileTree(base, new ArtifactFileVisitor());
26  	}
27  
28  	class ArtifactFileVisitor extends SimpleFileVisitor<Path> {
29  
30  		@Override
31  		public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
32  			// OsgiBootUtils.debug("Processing " + file);
33  			if (file.toString().endsWith(".jar")) {
34  				Version version;
35  				try {
36  					version = new Version(readVersionFromModule(file));
37  				} catch (Exception e) {
38  					// ignore non OSGi
39  					return FileVisitResult.CONTINUE;
40  				}
41  				String moduleName = readSymbolicNameFromModule(file);
42  				Path groupPath = file.getParent().getParent().getParent();
43  				Path relGroupPath = base.relativize(groupPath);
44  				String contributionName = relGroupPath.toString().replace(File.separatorChar, '.');
45  				A2Contribution contribution = getOrAddContribution(contributionName);
46  				A2Component component = contribution.getOrAddComponent(moduleName);
47  				A2Module module = component.getOrAddModule(version, file);
48  				if (OsgiBootUtils.isDebug())
49  					OsgiBootUtils.debug("Registered " + module);
50  			}
51  			return super.visitFile(file, attrs);
52  		}
53  
54  	}
55  
56  	public static void main(String[] args) {
57  		try {
58  			FsM2Source context = new FsM2Source(Paths.get("/home/mbaudier/.m2/repository"));
59  			context.load();
60  			context.asTree();
61  		} catch (Exception e) {
62  			e.printStackTrace();
63  		}
64  	}
65  
66  }