Last active
          October 25, 2016 01:37 
        
      - 
      
- 
        Save caiorss/89404800d889f514514ae661c0b7947d to your computer and use it in GitHub Desktop. 
    Scala - Java interoperability and integration exploration.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // Show all root directory files. | |
| //----------------------------------------------- | |
| scala> val file = new java.io.File("/") | |
| file: java.io.File = / | |
| scala> file.list | |
| list listFiles | |
| scala> file.listFiles() | |
| res14: Array[java.io.File] = Array(/etc, /proc, /sys, /lost+found, /var, /.manjaro-tools, /sbin, /mnt, /tmp, /root, /usr, /lib, /opt, /dev, /nix, /run, /bin, /root-image-pkgs.txt, /boot, /home, /srv, /xfce-openrc-image-pkgs.txt, /lib64) | |
| scala> file.listFiles().foreach(println) | |
| /etc | |
| /proc | |
| /sys | |
| /lost+found | |
| /var | |
| /.manjaro-tools | |
| /sbin | |
| /mnt | |
| /tmp | |
| /root | |
| /usr | |
| /lib | |
| /opt | |
| /dev | |
| /nix | |
| /run | |
| /bin | |
| /root-image-pkgs.txt | |
| /boot | |
| /home | |
| /srv | |
| /xfce-openrc-image-pkgs.txt | |
| /lib64 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | scala> Class.forName("java.io.File").getDeclaredMethods().take(10).foreach(println) | |
| public boolean java.io.File.equals(java.lang.Object) | |
| public java.lang.String java.io.File.toString() | |
| public int java.io.File.hashCode() | |
| public int java.io.File.compareTo(java.lang.Object) | |
| public int java.io.File.compareTo(java.io.File) | |
| public java.lang.String java.io.File.getName() | |
| public long java.io.File.length() | |
| public java.lang.String java.io.File.getParent() | |
| public boolean java.io.File.isAbsolute() | |
| public java.lang.String java.io.File.getCanonicalPath() throws java.io.IOException | |
| def show_class_methods (classname: String) { | |
| Class | |
| .forName(classname) | |
| .getDeclaredMethods() | |
| .foreach(println) | |
| } | |
| show_class_methods: (classname: String)Unit | |
| scala> show_class_constructors("javax.swing.JFrame") | |
| protected void javax.swing.JFrame.frameInit() | |
| protected javax.swing.JRootPane javax.swing.JFrame.createRootPane() | |
| protected void javax.swing.JFrame.processWindowEvent(java.awt.event.WindowEvent) | |
| public void javax.swing.JFrame.setDefaultCloseOperation(int) | |
| public int javax.swing.JFrame.getDefaultCloseOperation() | |
| public void javax.swing.JFrame.setTransferHandler(javax.swing.TransferHandler) | |
| public javax.swing.TransferHandler javax.swing.JFrame.getTransferHandler() | |
| public void javax.swing.JFrame.setJMenuBar(javax.swing.JMenuBar) | |
| public javax.swing.JMenuBar javax.swing.JFrame.getJMenuBar() | |
| ... | |
| def show_class_constructors (classname: String) { | |
| Class | |
| .forName(classname) | |
| .getDeclaredConstructors() | |
| .foreach(println) | |
| } | |
| scala> show_class_constructors("java.io.File") | |
| public java.io.File(java.lang.String,java.lang.String) | |
| public java.io.File(java.lang.String) | |
| private java.io.File(java.lang.String,java.io.File) | |
| public java.io.File(java.io.File,java.lang.String) | |
| public java.io.File(java.net.URI) | |
| private java.io.File(java.lang.String,int) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // Scala interface with JDBC / Sqlite Database | |
| // Database Driver available at: https://github.com/xerial/sqlite-jdbc | |
| // | |
| import java.sql.Connection | |
| import java.sql.DriverManager | |
| import java.sql.ResultSet | |
| import java.sql.SQLException | |
| import java.sql.Statement | |
| scala> :require sqlite-jdbc-3.14.2.1.jar | |
| Added '/home/arch/sqlite-jdbc-3.14.2.1.jar' to classpath. | |
| scala> val db = "jdbc:sqlite:/home/arch/.mozilla/firefox/mwad0hks.zotero/zotero/zotero.sqlite" | |
| db: String = jdbc:sqlite:/home/arch/.mozilla/firefox/mwad0hks.zotero/zotero/zotero.sqlite | |
| scala> val conn = DriverManager.getConnection(db) | |
| conn: java.sql.Connection = org.sqlite.SQLiteConnection@1f7030a6 | |
| scala> val st = conn.createStatement() | |
| st: java.sql.Statement = org.sqlite.jdbc4.JDBC4Statement@3234e239 | |
| scala> val rs = st.executeQuery("SELECT value FROM itemDataValues WHERE valueID=11872") | |
| rs: java.sql.ResultSet = org.sqlite.jdbc4.JDBC4ResultSet@530612ba | |
| scala> rs.next() | |
| res0: Boolean = true | |
| scala> rs | |
| res1: java.sql.ResultSet = org.sqlite.jdbc4.JDBC4ResultSet@530612ba | |
| scala> rs.getString("value") | |
| res2: String = Arrows, Robots, and Functional Reactive Programming | |
| scala> rs.getString("value") | |
| res3: String = Arrows, Robots, and Functional Reactive Programming | |
| scala> rs.next() | |
| res4: Boolean = false | |
| scala> rs.getString("value") | |
| java.sql.SQLException: ResultSet closed | |
| at org.sqlite.core.CoreResultSet.checkOpen(CoreResultSet.java:69) | |
| at org.sqlite.jdbc3.JDBC3ResultSet.findColumn(JDBC3ResultSet.java:38) | |
| at org.sqlite.jdbc3.JDBC3ResultSet.getString(JDBC3ResultSet.java:443) | |
| ... 32 elided | |
| def queryOne[A](query: String, selector: ResultSet => A, conn: Connection): Option[A] = { | |
| val st = conn.createStatement() | |
| val rs = st.executeQuery(query) | |
| rs.next() match { | |
| case false => None | |
| case true => Some(selector(rs)) | |
| } | |
| } | |
| scala> queryOne _ | |
| res11: (String, java.sql.ResultSet => Nothing, java.sql.Connection) => Option[Nothing] = <function3> | |
| scala> queryOne("SELECT value FROM itemDataValues WHERE valueID=11872", | |
| (rs: ResultSet) => rs.getString("value"), | |
| conn) | |
| res18: Option[String] = Some(Arrows, Robots, and Functional Reactive Programming) | |
| scala> | |
| def resultString(field: String) (res: ResultSet): String = | |
| res.getString(field) | |
| def resultInt(field: String) (res: ResultSet): Int = | |
| res.getInt(field) | |
| def resultDouble(field: String) (res: ResultSet): Double = | |
| res.getDouble(field) | |
| scala> queryOne("SELECT value FROM itemDataValues WHERE valueID=11872", | |
| resultString("value"), | |
| conn) | |
| res19: Option[String] = Some(Arrows, Robots, and Functional Reactive Programming) | |
| scala> queryOne("SELECT * FROM itemDataValues WHERE valueID=11872", | |
| resultInt("valueID"), | |
| conn) | |
| res23: Option[Int] = Some(11872) | |
| def queryForeach[A](query: String, action: ResultSet => Unit, conn: Connection): Unit = { | |
| val st = conn.createStatement() | |
| val rs = st.executeQuery(query) | |
| while (rs.next()){ | |
| action(rs) | |
| } | |
| } | |
| queryForeach( | |
| "SELECT * FROM tags LIMIT 10", | |
| res => println("tagID = %d, name = %s".format(resultInt("tagID")(res), resultString("name")(res))), | |
| conn | |
| ) | |
| // Output: | |
| // | |
| tagID = 1, name = python | |
| tagID = 2, name = hacker | |
| tagID = 3, name = forensic | |
| tagID = 4, name = pentest | |
| tagID = 5, name = security | |
| tagID = 6, name = numerical | |
| tagID = 7, name = method | |
| tagID = 8, name = engineering | |
| tagID = 9, name = advanced | |
| tagID = 10, name = framework | |
| def queryPrint[A](query: String, conn: Connection): Unit = { | |
| val st = conn.createStatement() | |
| val rs = st.executeQuery(query) | |
| val md = rs.getMetaData() | |
| val ncols = md.getColumnCount() | |
| (1 to ncols).foreach(i => print(md.getColumnName(i) + "\t")) | |
| (1 to ncols).foreach(i => print(md.getColumnTypeName(i) + "\t")) | |
| println("") | |
| while (rs.next()){ | |
| (1 to ncols).foreach(i => print(rs.getString(i) + "\t")) | |
| println("") | |
| } | |
| } | |
| scala> queryPrint("SELECT * FROM tags LIMIT 10", conn) | |
| tagID name type dateAdded dateModified clientDateModified libraryID key INTEGER TEXT INT TIMESTAMP TIMESTAMP TIMESTAMP INT TEXT | |
| 1 python 0 2014-08-25 13:43:40 2016-05-15 00:34:34 2016-05-15 00:34:34 null QE8ARQ6S | |
| 2 hacker 0 2014-08-25 13:43:48 2014-08-25 13:44:19 2014-08-25 13:44:19 null F4832H6P | |
| 3 forensic 0 2014-08-25 13:43:52 2014-08-25 13:44:41 2014-08-25 13:44:41 null S6AJ5ZGN | |
| 4 pentest 0 2014-08-25 13:44:26 2014-08-25 13:44:26 2014-08-25 13:44:26 null AMPGN8ZN | |
| 5 security 0 2014-08-25 13:44:37 2016-05-18 20:28:10 2016-05-18 20:28:10 null 7DJK9BR4 | |
| 6 numerical 0 2014-08-25 13:44:58 2014-12-07 03:22:05 2014-12-07 03:22:05 null 9WA457NP | |
| 7 method 0 2014-08-25 13:45:00 2014-12-07 03:22:08 2014-12-07 03:22:08 null IHT65CMM | |
| 8 engineering 0 2014-08-25 13:45:04 2014-12-12 17:36:04 2014-12-12 17:36:04 null TWRJK7H4 | |
| 9 advanced 0 2014-08-25 13:45:22 2014-08-25 13:45:22 2014-08-25 13:45:22 null DZS9W84G | |
| 10 framework 0 2014-08-25 13:45:38 2014-08-25 13:45:38 2014-08-25 13:45:38 null SX75KZSU | |
| sealed abstract class DBColumn | |
| case class ColInt (value: Int) extends DBColumn | |
| case class ColText (value: String) extends DBColumn | |
| case class ColDouble (value: Double) extends DBColumn | |
| case class ColTimeStamp (value: String) extends DBColumn | |
| case class ColUnknow (value: String, tcol: String) extends DBColumn | |
| def getResultCell (res: ResultSet, i: Int) = { | |
| val md = res.getMetaData() | |
| val colType = md.getColumnTypeName(i) | |
| colType match { | |
| case "INTEGER" => ColInt(res.getInt(i)) | |
| case "INT" => ColInt(res.getInt(i)) | |
| case "TEXT" => ColText(res.getString(i)) | |
| case "DOUBLE" => ColDouble(res.getDouble(i)) | |
| case "TIMESTAMP" => ColTimeStamp(res.getString(i)) | |
| case _ => ColUnknow(res.getString(i), colType) | |
| } | |
| } | |
| def resultToRowFn (res: ResultSet) = { | |
| val md = res.getMetaData() | |
| val ncols = md.getColumnCount() | |
| (res: ResultSet) => for (i <- 1 to ncols) yield res.getString(i) | |
| } | |
| def queryCollect[A](query: String, selector: (ResultSet, Int) => A) (conn: Connection) = { | |
| val st = conn.createStatement() | |
| val rs = st.executeQuery(query) | |
| val md = rs.getMetaData() | |
| val ncols = md.getColumnCount() | |
| def aux(rs: ResultSet, | |
| acc: List[scala.collection.immutable.IndexedSeq[A]] | |
| ): List[scala.collection.immutable.IndexedSeq[A]] = { | |
| rs.next() match { | |
| case false => acc.reverse | |
| case true => val xs = (1 to ncols).map(i => selector(rs, i)) | |
| aux(rs, xs::acc) | |
| } | |
| } | |
| aux(rs, List()) | |
| } | |
| scala> queryCollect(q, (res, i) => res.getString(i), conn).foreach(println) | |
| scala> def queryDB(query: String) = queryCollect(query, getResultCell) _ | |
| queryDB: (query: String)java.sql.Connection => List[scala.collection.immutable.IndexedSeq[Product with Serializable with DBColumn]] | |
| Vector(ColInt(1), ColText(python), ColInt(0), ColTimeStamp(2014-08-25 13:43:40), ColTimeStamp(2016-05-15 00:34:34), ColTimeStamp(2016-05-15 00:34:34), ColInt(0), ColText(QE8ARQ6S)) | |
| Vector(ColInt(2), ColText(hacker), ColInt(0), ColTimeStamp(2014-08-25 13:43:48), ColTimeStamp(2014-08-25 13:44:19), ColTimeStamp(2014-08-25 13:44:19), ColInt(0), ColText(F4832H6P)) ... | |
| queryCollect(q, (res, i) => res.getString(i))(conn).foreach(println) | |
| scala> Vector(1, python, 0, 2014-08-25 13:43:40, 2016-05-15 00:34:34, 2016-05-15 00:34:34, null, QE8ARQ6S) | |
| Vector(2, hacker, 0, 2014-08-25 13:43:48, 2014-08-25 13:44:19, 2014-08-25 13:44:19, null, F4832H6P) | |
| Vector(3, forensic, 0, 2014-08-25 13:43:52, 2014-08-25 13:44:41, 2014-08-25 13:44:41, null, S6AJ5ZGN) | |
| Vector(4, pentest, 0, 2014-08-25 13:44:26, 2014-08-25 13:44:26, 2014-08-25 13:44:26, null, AMPGN8ZN) | |
| Vector(5, security, 0, 2014-08-25 13:44:37, 2016-05-18 20:28:10, 2016-05-18 20:28:10, null, 7DJK9BR4) | |
| Vector(6, numerical, 0, 2014-08-25 13:44:58, 2014-12-07 03:22:05, 2014-12-07 03:22:05, null, 9WA457NP) | |
| Vector(7, method, 0, 2014-08-25 13:45:00, 2014-12-07 03:22:08, 2014-12-07 03:22:08, null, IHT65CMM) | |
| Vector(8, engineering, 0, 2014-08-25 13:45:04, 2014-12-12 17:36:04, 2014-12-12 17:36:04, null, TWRJK7H4) | |
| Vector(9, advanced, 0, 2014-08-25 13:45:22, 2014-08-25 13:45:22, 2014-08-25 13:45:22, null, DZS9W84G) | |
| Vector(10, framework, 0, 2014-08-25 13:45:38, 2014-08-25 13:45:38, 2014-08-25 13:45:38, null, SX75KZSU) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // Print available timezones | |
| //--------------------------------------- | |
| cala> java.util.TimeZone.getAvailableIDs.filter(t => t.contains("Europe/")).foreach(println) | |
| Europe/Amsterdam | |
| Europe/Andorra | |
| Europe/Athens | |
| Europe/Belfast | |
| Europe/Belgrade | |
| Europe/Berlin | |
| Europe/Bratislava | |
| Europe/Brussels | |
| Europe/Bucharest | |
| Europe/Budapest | |
| Europe/Busingen | |
| Europe/Chisinau | |
| ... | |
| scala> :paste | |
| // Entering paste mode (ctrl-D to finish) | |
| java.util.TimeZone.getAvailableIDs | |
| .filter(t => t.contains("Europe/")) | |
| .foreach(println) | |
| // Exiting paste mode, now interpreting. | |
| Europe/Amsterdam | |
| Europe/Andorra | |
| Europe/Athens | |
| Europe/Belfast | |
| Europe/Belgrade | |
| Europe/Berlin | |
| Europe/Bratislava | |
| Europe/Brussels | |
| Europe/Bucharest | |
| Europe/Budapest | |
| Europe/Busingen | |
| Europe/Chisinau | |
| Europe/Copenhagen | |
| Europe/Dublin | |
| Europe/Gibraltar | |
| Europe/Guernsey | |
| Europe/Helsinki | |
| Europe/Isle_of_Man | |
| Europe/Istanbul | |
| ... | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // Print Systme properties | |
| // | |
| // | |
| scala> System.getProperty("path.separator") | |
| res29: String = : | |
| scala> System.getProperty("os.name") | |
| res30: String = Linux | |
| scala> System.getProperty("os.arch") | |
| res31: String = amd64 | |
| scala> System.getProperty("os.version") | |
| res32: String = 4.4.21-1-MANJARO | |
| scala> System.getProperty("user.dir") | |
| res33: String = /home/arch | |
| scala> System.getProperty("user.home") | |
| res34: String = /home/arch | |
| // Show all system properties | |
| //------------------------------------------- | |
| scala> val p = System.getProperties() | |
| p: java.util.Properties = | |
| {env.emacs=, java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=/home/arch/opt/jdk1.8.0_74/jre/lib/amd64, java.vm.version=25.74-b02, java.vm.vendor=Oracle Corporation, java.vendor.url=http://java.oracle.com/, path.separator=:, java.vm.name=Java HotSpot(TM) 64-Bit Server VM, file.encoding.pkg=sun.io, user.country=US, sun.java.launcher=SUN_STANDARD, sun.os.patch.level=unknown, java.vm.specification.name=Java Virtual Machine Specification, user.dir=/home/arch, java.runtime.version=1.8.0_74-b02, java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment, java.endorsed.dirs=/home/arch/opt/jdk1.8.0_74/jre/lib/endorsed, os.arch=amd64, java.io.tmpdir=/tmp, line.separator= | |
| , java.vm.specification.vendor=Oracle Corporation, os.name=Linux, sun.jnu.encoding=... | |
| scala> | |
| scala> p.list(System.out) | |
| -- listing properties -- | |
| env.emacs= | |
| java.runtime.name=Java(TM) SE Runtime Environment | |
| sun.boot.library.path=/home/arch/opt/jdk1.8.0_74/jre/lib/amd64 | |
| java.vm.version=25.74-b02 | |
| java.vm.vendor=Oracle Corporation | |
| java.vendor.url=http://java.oracle.com/ | |
| path.separator=: | |
| java.vm.name=Java HotSpot(TM) 64-Bit Server VM | |
| file.encoding.pkg=sun.io | |
| user.country=US | |
| sun.java.launcher=SUN_STANDARD | |
| sun.os.patch.level=unknown | |
| java.vm.specification.name=Java Virtual Machine Specification | |
| user.dir=/home/arch | |
| java.runtime.version=1.8.0_74-b02 | |
| java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment | |
| java.endorsed.dirs=/home/arch/opt/jdk1.8.0_74/jre/lib/en... | |
| os.arch=amd64 | |
| java.io.tmpdir=/tmp | |
| line.separator= | |
| java.vm.specification.vendor=Oracle Corporation | |
| os.name=Linux | |
| sun.jnu.encoding=UTF-8 | |
| java.library.path=/usr/java/packages/lib/amd64:/usr/lib... | |
| java.specification.name=Java Platform API Specification | |
| java.class.version=52.0 | |
| sun.management.compiler=HotSpot 64-Bit Tiered Compilers | |
| os.version=4.4.21-1-MANJARO | |
| user.home=/home/arch | |
| user.timezone= | |
| scala.home=/home/arch/opt/scala-2.11.8 | |
| java.awt.printerjob=sun.print.PSPrinterJob | |
| file.encoding=UTF-8 | |
| java.specification.version=1.8 | |
| scala.usejavacp=true | |
| user.name=arch | |
| java.class.path="" | |
| java.vm.specification.version=1.8 | |
| sun.arch.data.model=64 | |
| java.home=/home/arch/opt/jdk1.8.0_74/jre | |
| sun.java.command=scala.tools.nsc.MainGenericRunner | |
| java.specification.vendor=Oracle Corporation | |
| user.language=en | |
| awt.toolkit=sun.awt.X11.XToolkit | |
| java.vm.info=mixed mode | |
| java.version=1.8.0_74 | |
| java.ext.dirs=/home/arch/opt/jdk1.8.0_74/jre/lib/ex... | |
| sun.boot.class.path=/home/arch/opt/jdk1.8.0_74/jre/lib/re... | |
| java.vendor=Oracle Corporation | |
| file.separator=/ | |
| java.vendor.url.bug=http://bugreport.sun.com/bugreport/ | |
| sun.cpu.endian=little | |
| sun.io.unicode.encoding=UnicodeLittle | |
| sun.cpu.isalist= | |
| def showSystemProperties () = { | |
| val p = System.getProperties() | |
| val keys = p.keys | |
| while (keys.hasMoreElements()){ | |
| var key = keys.nextElement().asInstanceOf[String] | |
| var value = p.get(key) | |
| println("key: " + key) | |
| println("val: " + value) | |
| println("") | |
| } | |
| } | |
| scala> def showSystemProperties () = { | |
| | val p = System.getProperties() | |
| | val keys = p.keys | |
| | while (keys.hasMoreElements()){ | |
| | var key = keys.nextElement().asInstanceOf[String] | |
| | var value = p.get(key) | |
| | println("key: " + key) | |
| | println("val: " + value) | |
| | println("") | |
| | } | |
| | } | |
| showSystemProperties: ()Unit | |
| scala> showSystemProperties() | |
| key: env.emacs | |
| val: | |
| key: java.runtime.name | |
| val: Java(TM) SE Runtime Environment | |
| key: sun.boot.library.path | |
| val: /home/arch/opt/jdk1.8.0_74/jre/lib/amd64 | |
| key: java.vm.version | |
| val: 25.74-b02 | |
| key: java.vm.vendor | |
| val: Oracle Corporation | |
| key: java.vendor.url | |
| val: http://java.oracle.com/ | |
| key: path.separator | |
| val: : | |
| key: java.vm.name | |
| val: Java HotSpot(TM) 64-Bit Server VM | |
| key: file.encoding.pkg | |
| val: sun.io | |
| key: user.country | |
| val: US | |
| .... | |
| key: java.vm.specification.vendor | |
| val: Oracle Corporation | |
| key: os.name | |
| val: Linux | |
| key: sun.jnu.encoding | |
| val: UTF-8 | |
| key: java.library.path | |
| val: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib | |
| key: java.specification.name | |
| val: Java Platform API Specification | |
| key: java.class.version | |
| val: 52.0 | |
| key: sun.management.compiler | |
| val: HotSpot 64-Bit Tiered Compilers | |
| key: os.version | |
| val: 4.4.21-1-MANJARO | |
| key: user.home | |
| val: /home/arch | |
| key: user.timezone | |
| val: | |
| key: scala.home | |
| val: /home/arch/opt/scala-2.11.8 | |
| key: java.awt.printerjob | |
| val: sun.print.PSPrinterJob | |
| key: file.encoding | |
| val: UTF-8 | |
| key: java.specification.version | |
| val: 1.8 | |
| key: scala.usejavacp | |
| val: true | |
| key: java.class.path | |
| val: "" | |
| key: user.name | |
| val: arch | |
| key: java.vm.specification.version | |
| val: 1.8 | |
| key: sun.java.command | |
| val: scala.tools.nsc.MainGenericRunner | |
| key: java.home | |
| val: /home/arch/opt/jdk1.8.0_74/jre | |
| ... | |
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment