]> git.argeo.org Git - gpl/argeo-slc.git/blob - ArgeoOsgiDistributionImpl.java
b47da8b887fcdae29253c1ea8f592f39cfc03adc
[gpl/argeo-slc.git] / ArgeoOsgiDistributionImpl.java
1 package org.argeo.slc.repo.osgi;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.SortedSet;
7 import java.util.TreeSet;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.argeo.slc.ModuleSet;
12 import org.argeo.slc.NameVersion;
13 import org.argeo.slc.build.Distribution;
14 import org.argeo.slc.execution.ExecutionFlow;
15 import org.argeo.slc.repo.ArgeoOsgiDistribution;
16 import org.argeo.slc.repo.ArtifactDistribution;
17
18 /** A consistent and versioned OSGi distribution, which can be built and tested. */
19 public class ArgeoOsgiDistributionImpl extends ArtifactDistribution implements
20 ArgeoOsgiDistribution {
21 private final static Log log = LogFactory
22 .getLog(ArgeoOsgiDistributionImpl.class);
23
24 private List<Object> modules = new ArrayList<Object>();
25
26 public ArgeoOsgiDistributionImpl(String coords) {
27 super(coords);
28 }
29
30 public void init() {
31 if (log.isDebugEnabled()) {
32 SortedSet<String> sort = new TreeSet<String>();
33 Iterator<? extends NameVersion> nvIt = nameVersions();
34 while (nvIt.hasNext()) {
35 NameVersion nv = nvIt.next();
36 sort.add(nv.getName() + ":" + nv.getVersion());
37 }
38
39 StringBuffer buf = new StringBuffer(
40 "## OSGi FACTORY MANAGED MODULES : \n");
41 for (String str : sort) {
42 buf.append(str).append('\n');
43 }
44
45 log.debug(buf);
46 }
47 }
48
49 // private static void print(ModuleSet moduleSet, Integer depth) {
50 // StringBuilder prefix = new StringBuilder();
51 // for (int i = 0; i < depth; i++) {
52 // prefix.append(' ');
53 // }
54 // String p = prefix.toString();
55 // prefix.setLength(0);
56 // log.debug(p + "## " + moduleSet.toString());
57 // Iterator<? extends NameVersion> nvIt = moduleSet.nameVersions();
58 // while (nvIt.hasNext()) {
59 // NameVersion nv = nvIt.next();
60 // if (nv instanceof ModuleSet)
61 // print((ModuleSet) nv, depth + 1);
62 // else
63 // log.debug(p + nv);
64 //
65 // }
66 // }
67
68 public void destroy() {
69
70 }
71
72 public Iterator<NameVersion> nameVersions() {
73 List<NameVersion> nameVersions = new ArrayList<NameVersion>();
74 for (Object module : modules) {
75 // extract runnable from execution flow
76 if (module instanceof ExecutionFlow) {
77 for (Iterator<Runnable> it = ((ExecutionFlow) module)
78 .runnables(); it.hasNext();) {
79 processModule(nameVersions, it.next());
80 }
81 }
82 // module = ((ExecutionFlow) module).getRunnable();
83 else {
84 processModule(nameVersions, module);
85 }
86 // if (module instanceof ModuleSet)
87 // addNameVersions(nameVersions, (ModuleSet) module);
88 // else if (module instanceof NameVersion) {
89 // NameVersion nv = (NameVersion) module;
90 // if (!nameVersions.contains(nv))
91 // nameVersions.add(nv);
92 // } else
93 // log.warn("Ignored " + module);
94 }
95 return nameVersions.iterator();
96 }
97
98 private void processModule(List<NameVersion> nameVersions, Object module) {
99 if (module instanceof ModuleSet)
100 addNameVersions(nameVersions, (ModuleSet) module);
101 else if (module instanceof NameVersion) {
102 NameVersion nv = (NameVersion) module;
103 if (!nameVersions.contains(nv))
104 nameVersions.add(nv);
105 } else
106 log.warn("Ignored " + module);
107 }
108
109 private void addNameVersions(List<NameVersion> nameVersions,
110 ModuleSet moduleSet) {
111 Iterator<? extends NameVersion> it = moduleSet.nameVersions();
112 while (it.hasNext()) {
113 NameVersion nv = it.next();
114 if (!nameVersions.contains(nv))
115 nameVersions.add(nv);
116 }
117 }
118
119 // Modular distribution interface methods. Not yet used.
120 public Distribution getModuleDistribution(String moduleName,
121 String moduleVersion) {
122 // NameVersion searched = new DefaultNameVersion(moduleName,
123 // moduleVersion);
124 // for (Distribution ad : modules) {
125 // if (ad.equals(searched))
126 // return ad;
127 // }
128 return null;
129 }
130
131 public Object getModulesDescriptor(String descriptorType) {
132 // TODO Auto-generated method stub
133 return null;
134 }
135
136 /* DEPENDENCY INJECTION */
137 public void setModules(List<Object> modules) {
138 this.modules = modules;
139 }
140 }