Update YangModuleInfo and its implementations 42/4842/4
authorRobert Varga <rovarga@cisco.com>
Mon, 27 Jan 2014 06:57:29 +0000 (07:57 +0100)
committerRobert Varga <rovarga@cisco.com>
Mon, 27 Jan 2014 07:46:17 +0000 (08:46 +0100)
- Use Set, not ImmutableSet in return
- getModuleSourceStream() throws IOException when it fails to find the
  resource
- Force instantiation of imported modules on class load
- Force a call to getModuleSourceStream() on class load

Change-Id: I4f5454d92c049cecc834b37ef14784d70b036a8c
Signed-off-by: Robert Varga <rovarga@cisco.com>
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/YangModuleInfoTemplate.xtend
yang/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/YangModuleInfo.java

index 3eaf2168f554718ec229b7c217c032111e1d2047..fd14d830f2e1cf1fddc0e77ce44c0b10f119d875 100644 (file)
@@ -7,25 +7,29 @@
  */
 package org.opendaylight.yangtools.sal.java.api.generator
 
-import org.opendaylight.yangtools.yang.model.api.Module
-import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil
-import org.opendaylight.yangtools.yang.binding.YangModuleInfo
 import java.io.InputStream
-import com.google.common.collect.ImmutableSet
-import java.util.Map
+import java.io.IOException
+import java.text.DateFormat
+import java.text.SimpleDateFormat
+
+import java.util.Collections
+import java.util.Date
+import java.util.HashSet
 import java.util.LinkedHashMap
+import java.util.Map
+import java.util.Set
+import java.util.TreeMap
+
+import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil
 import org.opendaylight.yangtools.binding.generator.util.Types
-import org.opendaylight.yangtools.sal.binding.model.api.Type
 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
+import org.opendaylight.yangtools.sal.binding.model.api.Type
 import org.opendaylight.yangtools.sal.binding.model.api.WildcardType
-import java.io.IOException
-import java.util.Set
-import java.util.HashSet
+import org.opendaylight.yangtools.yang.binding.YangModuleInfo
+import org.opendaylight.yangtools.yang.model.api.Module
 import org.opendaylight.yangtools.yang.model.api.SchemaContext
-import java.util.Date
-import java.util.TreeMap
-import java.text.DateFormat
-import java.text.SimpleDateFormat
+
+import com.google.common.collect.ImmutableSet
 
 class YangModuleInfoTemplate {
     val CLASS = "$YangModuleInfoImpl"
@@ -54,24 +58,61 @@ class YangModuleInfoTemplate {
     }
 
     def body() '''
-        public class «CLASS» implements «YangModuleInfo.importedName» {
+        public final class «CLASS» implements «YangModuleInfo.importedName» {
 
             private static final «YangModuleInfo.importedName» INSTANCE = new «CLASS»();
 
-            private «CLASS»() {}
+            private final Set<YangModuleInfo> importedModules;
 
             public static «YangModuleInfo.importedName» getInstance() {
                 return INSTANCE;
             }
 
             «module.classBody»
-
         }
     '''
 
     private def CharSequence classBody(Module m) '''
+        private «CLASS»() {
+            «IF m.imports.size != 0»
+                «Set.importedName»<«YangModuleInfo.importedName»> set = new «HashSet.importedName»<>();
+                «FOR imp : m.imports»
+                    «val name = imp.moduleName»
+                    «val rev = imp.revision»
+                    «IF rev == null»
+                        «val Set<Module> modules = ctx.modules»
+                        «val TreeMap<Date, Module> sorted = new TreeMap()»
+                        «FOR module : modules»
+                            «IF module.name.equals(name)»
+                                «sorted.put(module.revision, module)»
+                            «ENDIF»
+                        «ENDFOR»
+                        set.add(«BindingGeneratorUtil.moduleNamespaceToPackageName(sorted.lastEntry().value)».«CLASS».getInstance());
+                    «ELSE»
+                        set.add(«BindingGeneratorUtil.moduleNamespaceToPackageName(ctx.findModuleByName(name, rev))».«CLASS».getInstance());
+                    «ENDIF»
+                «ENDFOR»
+                importedModules = «ImmutableSet.importedName».copyOf(set);
+            «ELSE»
+                importedModules = «Collections.importedName».emptySet();
+            «ENDIF»
+
+            «val path = m.moduleSourcePath»
+            «IF path != null»
+                «InputStream.importedName» stream = «CLASS».class.getResourceAsStream("«path»");
+                if (stream == null) {
+                    throw new IllegalStateException("Resource «path» is missing");
+                }
+                try {
+                    stream.close();
+                } catch («IOException.importedName» e) {
+                    // Resource leak, but there's nothing we can do
+                }
+            «ENDIF»
+        }
+
         @Override
-            public «String.importedName» getName() {
+        public «String.importedName» getName() {
             return "«m.name»";
         }
 
@@ -87,35 +128,21 @@ class YangModuleInfoTemplate {
         }
 
         @Override
-        public «InputStream.importedName» getModuleSourceStream() throws «IOException.importedName» {
-            «val path = m.moduleSourcePath»
+        public «InputStream.importedName» getModuleSourceStream() throws IOException {
             «IF path == null»
                 return null;
             «ELSE»
-                return «CLASS».class.getResourceAsStream("«path»");
+                «InputStream.importedName» stream = «CLASS».class.getResourceAsStream("«path»");
+                if (stream == null) {
+                    throw new «IOException.importedName»("Resource «path» is missing");
+                }
+                return stream;
             «ENDIF»
         }
 
         @Override
-        public «ImmutableSet.importedName»<«YangModuleInfo.importedName»> getImportedModules() {
-            «Set.importedName»<«YangModuleInfo.importedName»> set = new «HashSet.importedName»<>();
-            «FOR imp : m.imports»
-                «val name = imp.moduleName»
-                «val rev = imp.revision»
-                «IF rev == null»
-                    «val Set<Module> modules = ctx.modules»
-                    «val TreeMap<Date, Module> sorted = new TreeMap()»
-                    «FOR module : modules»
-                        «IF module.name.equals(name)»
-                            «sorted.put(module.revision, module)»
-                        «ENDIF»
-                    «ENDFOR»
-                    set.add(«BindingGeneratorUtil.moduleNamespaceToPackageName(sorted.lastEntry().value)».«CLASS».getInstance());
-                «ELSE»
-                    set.add(«BindingGeneratorUtil.moduleNamespaceToPackageName(ctx.findModuleByName(name, rev))».«CLASS».getInstance());
-                «ENDIF»
-            «ENDFOR»
-            return «ImmutableSet.importedName».copyOf(set);
+        public «Set.importedName»<«YangModuleInfo.importedName»> getImportedModules() {
+            return importedModules;
         }
     '''
 
index 1e772f851e5bd41651a80cf7a4d7b685655452d1..be32812e358a8f3431f5a8cd84ab0e8726c45f26 100644 (file)
@@ -7,10 +7,9 @@
  */
 package org.opendaylight.yangtools.yang.binding;
 
-import java.io.IOException;
 import java.io.InputStream;
-
-import com.google.common.collect.ImmutableSet;
+import java.io.IOException;
+import java.util.Set;
 
 public interface YangModuleInfo {
 
@@ -38,6 +37,6 @@ public interface YangModuleInfo {
 
     InputStream getModuleSourceStream() throws IOException;
 
-    ImmutableSet<YangModuleInfo> getImportedModules();
+    Set<YangModuleInfo> getImportedModules();
 
 }