1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- /*
- This file is part of karl-marx.
- karl-marx is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- karl-marx is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
- You should have received a copy of the GNU Affero General Public License
- along with karl-marx. If not, see <https://www.gnu.org/licenses/>.
- */
- package org.congressodeiradicali.karlmarx
- import java.io.File
- import java.net.URL
- import java.util.Properties
- import java.util.jar.JarFile
- import scala.reflect.internal.util.ScalaClassLoader.URLClassLoader
- import scala.util.{Failure, Success, Try}
- class JARPluginLoader extends PluginLoader {
- final val MANIFEST_LOCATION = "plugin-manifest.properties"
- // manifest structure is
- // package=some.package
- // class=PluginClassName
- override def loadFromFile(file: File): Try[Plugin] = {
- val jarFile = new JarFile(file)
- val manifestInputStream = jarFile.getInputStream(jarFile.getEntry(MANIFEST_LOCATION))
- val props = new Properties()
- props.load(manifestInputStream)
- manifestInputStream.close()
- val `package` = props.getProperty("package")
- val `class` = props.getProperty("class")
- val fullClassName = s"${`package`}.${`class`}"
- try {
- val url = file.toURI.toURL
- val jarUrl = s"jar:${url.toString}!/"
- val urls = Seq[URL](new URL(jarUrl))
- val ucl = new URLClassLoader(urls, null)
- // TODO deprecation
- val plugin = Class.forName(fullClassName, true, ucl).newInstance().asInstanceOf[Plugin]
- Success(plugin)
- } catch {
- case e: Throwable => Failure(e)
- }
- }
- }
|