1   package org.daisy.pipeline.modules.impl;
2   
3   import java.net.URI;
4   import java.net.URL;
5   import java.nio.file.NoSuchFileException;
6   import java.util.LinkedList;
7   
8   import org.daisy.pipeline.modules.Component;
9   import org.daisy.pipeline.modules.Module;
10  import org.daisy.pipeline.modules.ModuleRegistry;
11  import org.daisy.pipeline.modules.ResolutionException;
12  import org.daisy.pipeline.xmlcatalog.XmlCatalogParser;
13  import org.osgi.service.component.annotations.Activate;
14  import org.osgi.service.component.annotations.Reference;
15  import org.osgi.service.component.annotations.ReferenceCardinality;
16  import org.osgi.service.component.annotations.ReferencePolicy;
17  import org.slf4j.Logger;
18  import org.slf4j.LoggerFactory;
19  import org.slf4j.helpers.NOPLogger;
20  
21  @org.osgi.service.component.annotations.Component(
22      name = "org.daisy.pipeline.modules.impl.Module_epub3_to_daisy3",
23      service = { Module.class }
24  )
25  public class Module_epub3_to_daisy3 extends Module {
26  
27      private static final Logger logger = LoggerFactory.getLogger(Module_epub3_to_daisy3.class);
28  
29      private boolean initialized = false;
30      private XmlCatalogParser catalogParser;
31      private ModuleRegistry moduleRegistry;
32      private final LinkedList<Component> componentsToAdd;
33      private final LinkedList<Component> componentsBeingAdded;
34  
35      public Module_epub3_to_daisy3() {
36          super("epub3-to-daisy3",
37                "1.0.10",
38                "DAISY Pipeline 2 module :: EPUB 3 to DAISY 3");
39          componentsToAdd = new LinkedList<>();
40          componentsBeingAdded = new LinkedList<>();
41      }
42  
43      @Reference(
44          name = "XmlCatalogParser",
45          unbind = "-",
46          service = XmlCatalogParser.class,
47          cardinality = ReferenceCardinality.MANDATORY,
48          policy = ReferencePolicy.STATIC
49      )
50      public void setParser(XmlCatalogParser parser) {
51          catalogParser = parser;
52      }
53  
54      @Reference(
55          name = "ModuleRegistry",
56          unbind = "-",
57          service = ModuleRegistry.class,
58          cardinality = ReferenceCardinality.MANDATORY,
59          policy = ReferencePolicy.STATIC
60      )
61      public void setModuleRegistry(ModuleRegistry registry) {
62          moduleRegistry = registry;
63      }
64  
65      private void tryResolveComponent(String uri, String versionRange) throws ResolutionException {
66          if (moduleRegistry.getModuleByComponent(URI.create(uri), versionRange) == null)
67              throw new ResolutionException("Unresolved dependency " + uri + " with version=" + versionRange);
68      }
69  
70      @Activate
71      protected void init() {
72          logger.debug("Initializing module " + getName());
73          Module tmpModule = new Module(getName(), getVersion(), getTitle()) {
74              @Override public void resolveDependencies() {}
75              @Override public Logger getLogger() { return NOPLogger.NOP_LOGGER; }
76          };
77          Module.parseCatalog(tmpModule, catalogParser);
78          for (String e : tmpModule.getEntities()) {
79              addEntity(tmpModule.getEntity(e));
80              logger.debug("Loaded entity: " + e);
81          }
82          for (URI c : tmpModule.getComponents()) {
83              components.put(c, () -> null); // will be replaced during resolveDependencies()
84              componentsToAdd.add(tmpModule.getComponent(c));
85          }
86          logger.debug("Module " + getName() + " initialized, but dependencies have not been resolved yet");
87      }
88  
89      // Not calling resolveDependencies() during object construction yet because it depends
90      // on other modules, and other modules may depend on this module. resolveDependencies()
91      // will be called when the object gets bound in ModuleRegistry. The method may recursively
92      // call itself. It copes with that by activating components in a different order
93      // when called recursively. ModuleRegistry has protection against endless recursion.
94      @Override
95      public void resolveDependencies() {
96          if (!initialized) {
97              boolean recursive = !componentsBeingAdded.isEmpty();
98              if (recursive)
99                  logger.debug("Resolving dependencies of module " + getName() + " (continuing in recursive call)");
100             else
101                 logger.debug("Resolving dependencies of module " + getName());
102             while (true) {
103                 if (componentsToAdd.isEmpty()) {
104                     if (componentsBeingAdded.isEmpty())
105                         break;
106                     componentsToAdd.addAll(componentsBeingAdded);
107                     componentsBeingAdded.clear();
108                 }
109                 Component c = componentsToAdd.poll();
110                 componentsBeingAdded.add(c);
111                 if (addComponent(c)) // may call resolveDependencies() recursively
112                     logger.debug("Loaded component: " + c.getURI());
113                 componentsBeingAdded.remove(c);
114                 componentsToAdd.remove(c);
115             }
116             if (!initialized)
117                 logger.debug("Done resolving dependencies of module " + getName());
118             initialized = true;
119         }
120     }
121 
122     @Override
123     protected boolean addComponent(Component component) {
124         String name = component.getURI().toString();
125         try {
126             if ("http://www.daisy.org/pipeline/modules/epub3-to-daisy3/library.xpl".equals(name)) {
127                 tryResolveComponent("http://www.daisy.org/pipeline/modules/epub3-to-html/library.xpl",
128                                     "[1.0.1,2.0.0)");
129                 tryResolveComponent("http://www.daisy.org/pipeline/modules/dtbook-utils/library.xpl",
130                                     "[5.1.1,6.0.0)");
131                 tryResolveComponent("http://www.daisy.org/pipeline/modules/file-utils/library.xpl",
132                                     "[4.3.1,5.0.0)");
133                 tryResolveComponent("http://www.daisy.org/pipeline/modules/file-utils/library.xsl",
134                                     "[4.3.1,5.0.0)");
135                 tryResolveComponent("http://www.daisy.org/pipeline/modules/common-utils/generate-id.xsl",
136                                     "[3.1.2,4.0.0)");
137                 tryResolveComponent("http://www.daisy.org/pipeline/modules/fileset-utils/library.xpl",
138                                     "[6.1.4,7.0.0)");
139                 tryResolveComponent("http://www.daisy.org/pipeline/modules/epub-utils/library.xpl",
140                                     "[2.2.1,3.0.0)");
141                 tryResolveComponent("http://www.daisy.org/pipeline/modules/html-utils/library.xpl",
142                                     "[6.4.1,7.0.0)");
143                 tryResolveComponent("http://www.daisy.org/pipeline/modules/html-to-dtbook/library.xpl",
144                                     "[2.0.7,3.0.0)");
145                 tryResolveComponent("http://www.daisy.org/pipeline/modules/smil-utils/library.xpl",
146                                     "[4.0.2,5.0.0)");
147                 tryResolveComponent("http://www.daisy.org/pipeline/modules/common-utils/library.xpl",
148                                     "[3.1.2,4.0.0)");
149                 tryResolveComponent("http://www.daisy.org/pipeline/modules/daisy3-utils/library.xpl",
150                                     "[4.1.6,5.0.0)");
151             }
152         } catch (ResolutionException re) {
153             logger.warn("Component " + component.getURI() + " can not be loaded: " + re.getMessage());
154             return false;
155         }
156         return super.addComponent(component);
157     }
158 
159     private URL resource1 = null;
160 
161     @Override
162     public URL getResource(String path) throws NoSuchFileException {
163         URL url = super.getResource(path);
164         try {
165             if (resource1 == null)
166                 resource1 = super.getResource("../xml/epub3-to-daisy3.script.xpl");
167             if (url.equals(resource1)) {
168                 tryResolveComponent("http://www.daisy.org/pipeline/modules/epub3-to-html/library.xpl",
169                                     "[1.0.1,2.0.0)");
170                 tryResolveComponent("http://www.daisy.org/pipeline/modules/dtbook-utils/library.xpl",
171                                     "[5.1.1,6.0.0)");
172                 tryResolveComponent("http://www.daisy.org/pipeline/modules/file-utils/library.xpl",
173                                     "[4.3.1,5.0.0)");
174                 tryResolveComponent("http://www.daisy.org/pipeline/modules/file-utils/library.xsl",
175                                     "[4.3.1,5.0.0)");
176                 tryResolveComponent("http://www.daisy.org/pipeline/modules/common-utils/generate-id.xsl",
177                                     "[3.1.2,4.0.0)");
178                 tryResolveComponent("http://www.daisy.org/pipeline/modules/fileset-utils/library.xpl",
179                                     "[6.1.4,7.0.0)");
180                 tryResolveComponent("http://www.daisy.org/pipeline/modules/epub-utils/library.xpl",
181                                     "[2.2.1,3.0.0)");
182                 tryResolveComponent("http://www.daisy.org/pipeline/modules/html-utils/library.xpl",
183                                     "[6.4.1,7.0.0)");
184                 tryResolveComponent("http://www.daisy.org/pipeline/modules/html-to-dtbook/library.xpl",
185                                     "[2.0.7,3.0.0)");
186                 tryResolveComponent("http://www.daisy.org/pipeline/modules/smil-utils/library.xpl",
187                                     "[4.0.2,5.0.0)");
188                 tryResolveComponent("http://www.daisy.org/pipeline/modules/common-utils/library.xpl",
189                                     "[3.1.2,4.0.0)");
190                 tryResolveComponent("http://www.daisy.org/pipeline/modules/daisy3-utils/library.xpl",
191                                     "[4.1.6,5.0.0)");
192                 return url;
193             }
194         } catch (ResolutionException re) {
195             throw new NoSuchFileException(
196                 "Resource " + path + " in module " + getName() + " can not be loaded: "                + re.getMessage());
197         }
198         return url;
199     }
200 }