Skip to content
Snippets Groups Projects
Verified Commit ba434a6c authored by Rico Bergmann's avatar Rico Bergmann
Browse files

#8 - Add basic exception handling to IO operations

Closes #8.
parent 6b04722a
No related branches found
No related tags found
1 merge request!2Publish v0.1
......@@ -4,7 +4,7 @@ import net.liftweb.json._
import org.eclipse.emf.ecore._
import org.eclipse.emf.ecore.resource.Resource
import org.rosi_project.model_sync.generator.conversion.SModelGenerator
import org.rosi_project.model_sync.generator.io.SModelFSWriter
import org.rosi_project.model_sync.generator.io.{ClassWritingException, ModelImagePreparationException, SModelFSWriter}
import org.rosi_project.model_sync.generator.sync.{GetterSetterGeneratingVisitor, SyncEnhancingVisitor}
import scopt.OptionParser
import scroll.internal.ecore.ECoreImporter
......@@ -13,6 +13,7 @@ import scala.reflect.io.{Directory, File}
import java.{io => jio}
import org.rosi_project.model_sync.generator.acr_model.SModel
import org.rosi_project.model_sync.generator.env.{CompilationException, JarPackaginException}
import scala.io.Source
......@@ -53,6 +54,7 @@ object Generator extends App {
).text("Remove the generated .scala files and only keep the compiled .class files")
}
try {
parser.parse(args, GeneratorConfig()) match {
case Some(config) =>
......@@ -60,7 +62,13 @@ object Generator extends App {
implicit val jsonFormats: Formats = DefaultFormats
val modelCfg = parse(modelJson).extract[ModelConfig]
val ecoreModel = loadEcore(config.source)
var ecoreModel: EPackage = null
try {
ecoreModel = loadEcore(config.source)
} catch {
case e: Exception => throw new EcoreLoadException(e)
}
val sModel = new SModelGenerator convert ecoreModel
prepareModel(sModel, modelCfg)
// write the model and create the JAR
......@@ -84,6 +92,19 @@ object Generator extends App {
}
case None =>
}
}
catch {
case ele: EcoreLoadException =>
println(s"** ERROR ** could not load ecore model: $ele")
case mipe: ModelImagePreparationException =>
println(s"** ERROR ** could not prepare model images: $mipe")
case cwe: ClassWritingException =>
println(s"** ERROR ** could not write classes: $cwe")
case ce: CompilationException =>
println(s"** ERROR ** could not compile classes: $ce")
case jpe: JarPackaginException =>
println(s"** ERROR ** could not package JAR: $jpe")
}
/** Fetches an ecore model from XML.
*
......@@ -113,3 +134,5 @@ object Generator extends App {
}
}
class EcoreLoadException(cause: Exception) extends RuntimeException(cause)
......@@ -17,6 +17,7 @@ class FilesCompiler(outDir: File) {
*/
def run(filesToCompile: List[File]): Unit = {
// see: https://stackoverflow.com/a/20323371/5161760
try {
val out = new PrintWriter(System.out)
val compilationSettings: Settings = new GenericRunnerSettings(out.println)
// just re-use the whole classpath
......@@ -27,6 +28,15 @@ class FilesCompiler(outDir: File) {
val compiler = new Global(compilationSettings, reporter)
val runner = new compiler.Run
runner.compile(filesToCompile.map(_.toAbsolute.toString))
} catch {
case e: Exception => throw new CompilationException(e)
}
}
}
/** Exception to indicate that the compilation process failed.
*
* @param cause the causing exception
*/
class CompilationException(cause: Exception) extends RuntimeException(cause)
......@@ -21,6 +21,7 @@ class JarPackager(inputDir: File, outputDir: File, jarName: String = "model.jar"
*/
def run(): Unit = {
// see: https://stackoverflow.com/a/1281295/5161760
try {
val mf = new Manifest
mf.getMainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0")
val targetJar = new JarOutputStream(new FileOutputStream(outputDir + File.separator + jarName), mf)
......@@ -64,6 +65,15 @@ class JarPackager(inputDir: File, outputDir: File, jarName: String = "model.jar"
}
targetJar.close()
} catch {
case e: Exception => throw new JarPackaginException(e)
}
}
}
/** Exception to indicate that the packaging process failed.
*
* @param cause the causing exception
*/
class JarPackaginException(cause: Exception) extends RuntimeException(cause)
package org.rosi_project.model_sync.generator.io
import java.io.{BufferedInputStream, FileInputStream, FileOutputStream, PrintWriter}
import java.nio.file.{Files, StandardCopyOption}
import java.{io => jio}
import java.net.URLClassLoader
import java.nio.file.{CopyOption, Files, StandardCopyOption}
import java.util.jar.{Attributes, JarEntry, JarOutputStream, Manifest}
import org.rosi_project.model_sync.generator.ModelConfig
import org.rosi_project.model_sync.generator.acr_model._
import org.rosi_project.model_sync.generator.env.{FilesCompiler, JarPackager}
import scala.reflect.io.{File, Path}
import scala.tools.nsc.reporters.ConsoleReporter
import scala.tools.nsc.{GenericRunnerSettings, Global, Settings}
import scala.util.control.Breaks._
import scala.reflect.io.Directory
import scala.reflect.io.{Directory, File, Path}
/** The `FSWriter` writes a [[SModel]] as a single compiled ''JAR'' file to the File System.
*
......@@ -40,12 +33,19 @@ class SModelFSWriter(
println(s"Output dir is $outputDir")
println("... Copying model images")
private val images = collectAllModelImages(modelCfg, currentDir)
private val imagesTargetDir = new jio.File(workingDir.toAbsolute.toString() + File.separator + "res")
private var images: List[jio.File] = _
private var imagesTargetDir: jio.File = _
try {
images = collectAllModelImages(modelCfg, currentDir)
imagesTargetDir = new jio.File(workingDir.toAbsolute.toString() + File.separator + "res")
imagesTargetDir.mkdirs()
images.foreach(img => {
Files.copy(img.toPath, imagesTargetDir.toPath.resolve(img.getName), StandardCopyOption.REPLACE_EXISTING)
})
} catch {
case e: Exception => throw new ModelImagePreparationException(e)
}
override def visit(sModel: SModel): Unit = {
println(s"... Wrote files (sources) $sFilesToCompile")
......@@ -69,6 +69,7 @@ class SModelFSWriter(
}
override def visit(sClass: SClass): Unit = {
try {
println(s"Writing class $sClass")
val classNameWithPath = workingDir.toAbsolute.toString() + File.separator + pckg2Path(sClass.getPackage) + File.separator + s"${sClass.name}.scala"
val writer = new SClassWriter(sClass)
......@@ -78,6 +79,9 @@ class SModelFSWriter(
classFile.jfile.getParentFile.mkdirs()
classFile.writeAll(writer.stringify)
sFilesToCompile ::= classFile
} catch {
case e: Exception => throw new ClassWritingException(e)
}
}
override def visit(sAttr: SAttribute): Unit = {
......@@ -125,3 +129,15 @@ class SModelFSWriter(
}
}
/** Exception to indicate that the model images may not be copied into the JAR.
*
* @param cause the causing exception
*/
class ModelImagePreparationException(cause: Exception) extends RuntimeException(cause)
/** Exception to indicate that a class may not be written.
*
* @param cause the causing exception
*/
class ClassWritingException(cause: Exception) extends RuntimeException(cause)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment