13. Wrapping a Library(1)
require 'java'
# create objects
pdf = com.itextpdf.text.Document.new
para =
com.itextpdf.text.Paragraph.new
'Using Java resources from JRuby'
file = java.io.FileOutputStream.new
'pdf_demo.pdf'
https://gist.github.com/e4b1d813394e58188e59
14. Wrapping a Library(2)
# get_instance is an alias for
getInstance
com.itextpdf.text.pdf.PdfWriter.
get_instance(pdf, file)
pdf.open
pdf.add para
pdf.close
https://gist.github.com/e4b1d813394e58188e59
21. Loading Classes(3)
module IText
include_package "com.itextpdf.text"
end
...
pdf = IText::Document.new
para = IText::Paragraph.new 'Loading...'
...
https://gist.github.com/da54b499d1ce8e2d38e3
22. Loading Classes(4)
#for java string
java_import "java.lang.String" do
"JString"
end
...
data = JString.new('avoid class name
conflict')
para = Paragraph.new(data)
https://gist.github.com/f4acbe684e4c1d9982b1
23. Using Objects’ Static
Methods(1)
API: com.itextpdf.text.PageSize
http://api.itextpdf.com/itext/com/itextpdf/text/PageSize.html
24. Using Objects’ Static
Methods(2)
#for itext
java_import com.itextpdf.text.Document
java_import com.itextpdf.text.PageSize
...
pdf = Document.new(
PageSize.getRectangle("A5"))
https://gist.github.com/a680fe7f47ab00967914
25. Using Objects’ Static
Fields
java_import com.itextpdf.text.Document
java_import com.itextpdf.text.PageSize
...
pdf = Document.new(PageSize::A5)
https://gist.github.com/a680fe7f47ab00967914
26. Using Objects’
constructor(1)
API: com.itextpdf.text.Document
http://api.itextpdf.com/itext/com/itextpdf/text/Document.html
27. Using Objects’
constructor(2)
java_import com.itextpdf.text.Document
java_import com.itextpdf.text.PageSize
...
pdf = Document.new
pdf = Document.new PageSize::A5
pdf = Document.new(PageSize::A5, 30,
10, 100, 20)
29. Get/Set Methods(2)
pdf = Document.new
pdf.setPageSize(PageSize::A5)
pdf.set_page_size(PageSize::A5)
#pdf.page_size = PageSize::A5 is not work
puts pdf.getPageSize
puts pdf.page_size
https://gist.github.com/50f2d2c82f43db1a886f
30. Using Objects’ Instance
Fields(1)
// PrivateField.java
public class PrivateField{
private int pField = 0;
public PrivateField(int i){
pField = i;
}
}
https://gist.github.com/c078f579c92e0a11fa43
31. Using Objects’ Instance
Fields(2)
#private_field.rb
class PrivateField
field_accessor :pField
end
obj = PrivateField.new(5)
puts obj.pField #=> 5
obj.pField = 10
puts obj.pField #=> 10
https://gist.github.com/6ae88c3a4472a5b91140
34. Data Type Conversion
(Array)
public class ArrayDemo {
public static String getType(Object o){
return o.getClass().getName();
}
}
https://gist.github.com/1172412
35. Data Type Conversion
(Array)
require 'java'
java_import "ArrayDemo"
puts ArrayDemo.get_type(["a", "b"])
#=>org.jruby.RubyArray
puts ArrayDemo. get_type(["a", "b"].to_java)
#=> [Ljava.lang.Object;
https://gist.github.com/717f4e5061b8f3f042d5
44. Application Server
Trinidad
• Simple library to run rails and rackup
applications into an embedded Apache
Tomcat
• https://github.com/trinidad/trinidad
• add gem ‘trinidad’ to Gemfile
• jruby -S trinidad
45. Running Trinidad as
Windows Service(1)
• add gem ‘trinidad_init_services’ to Gemfile
• https://github.com/trinidad/
trinidad_init_services
• It’s base on Apache Commons Daemon
46. Running Trinidad as
Windows Service(2)
• If you are using 64-bit Server, you should
replace procrun.exe to 64-bit version.
• jruby -S trinidad_init_service
47. IIS Proxy to Trinidad
• using ISAPI_Rewrite 3
• config:
RewriteEngine on
RewriteBase /
RewriteLog "C:myapplogrewrite.log"
RewriteLogLevel 9
RewriteProxy (.*) http://localhost:3000/$1
• Single server license: $99
57. Redis and Resque are running
on the “Command Prompt”.
hmm.....
It’s not a good idea.
58. Using FireDaemon
• Windows Service Creation, Management,
Monitoring and Control
• http://www.firedaemon.com/purchase-
firedaemon.php
• Single License:
$49