forked from matadon/mizuno
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'java-output-extension' into fishwife-dev
- Loading branch information
Showing
10 changed files
with
274 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -2,3 +2,8 @@ | ||
doc/ | doc/ | ||
pkg/ | pkg/ | ||
.bundle/ | .bundle/ | ||
target/ | |||
.classpath | |||
.project | |||
.settings | |||
lib/fishwife/*.jar |
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,17 @@ | |||
History.rdoc | |||
LICENSE | |||
Manifest.txt | |||
README.rdoc | |||
Rakefile | |||
bin/fishwife | |||
example/config.ru | |||
lib/fishwife/base.rb | |||
lib/fishwife.rb | |||
lib/fishwife/http_server.rb | |||
lib/fishwife/rack_servlet.rb | |||
lib/rack/handler/fishwife.rb | |||
spec/fishwife_spec.rb | |||
spec/spec_helper.rb | |||
spec/test_app.rb | |||
spec/data/hello.txt | |||
spec/data/reddit-icon.png |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,52 @@ | |||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |||
|
|||
<modelVersion>4.0.0</modelVersion> | |||
<groupId>fishwife</groupId> | |||
<artifactId>fishwife</artifactId> | |||
<packaging>jar</packaging> | |||
<version>1.3.0</version> | |||
<name>Fishwife Java Extension</name> | |||
|
|||
<properties> | |||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||
</properties> | |||
|
|||
<dependencies> | |||
|
|||
<dependency> | |||
<groupId>org.jruby.extras</groupId> | |||
<artifactId>bytelist</artifactId> | |||
<version>1.0.10</version> | |||
<optional>true</optional> | |||
</dependency> | |||
|
|||
<dependency> | |||
<groupId>org.jruby</groupId> | |||
<artifactId>jruby-core</artifactId> | |||
<version>1.6.6</version> | |||
<optional>true</optional> | |||
</dependency> | |||
|
|||
</dependencies> | |||
|
|||
<build> | |||
<plugins> | |||
|
|||
<plugin> | |||
<artifactId>maven-compiler-plugin</artifactId> | |||
<version>2.3.2</version> | |||
<configuration> | |||
<source>1.6</source> | |||
<target>1.6</target> | |||
<optimize>true</optimize> | |||
<debug>true</debug> | |||
<encoding>UTF-8</encoding> | |||
<showDeprecation>true</showDeprecation> | |||
<showWarnings>true</showWarnings> | |||
</configuration> | |||
</plugin> | |||
|
|||
</plugins> | |||
</build> | |||
|
|||
</project> |
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,40 @@ | |||
/* | |||
* Copyright (c) 2012 David Kellum | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); you | |||
* may not use this file except in compliance with the License. You may | |||
* obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | |||
* implied. See the License for the specific language governing | |||
* permissions and limitations under the License. | |||
*/ | |||
|
|||
package fishwife; | |||
|
|||
import org.jruby.Ruby; | |||
import org.jruby.RubyClass; | |||
import org.jruby.RubyModule; | |||
import org.jruby.runtime.ObjectAllocator; | |||
import org.jruby.runtime.load.BasicLibraryService; | |||
|
|||
public class JRubyService implements BasicLibraryService | |||
{ | |||
public boolean basicLoad( Ruby runtime ) | |||
{ | |||
RubyModule fmod = runtime.defineModule( "Fishwife" ); | |||
|
|||
RubyClass ouClass = | |||
fmod.defineClassUnder( "OutputUtil", | |||
runtime.getObject(), | |||
ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR ); | |||
|
|||
ouClass.defineAnnotatedMethods( OutputUtil.class ); | |||
|
|||
return true; | |||
} | |||
} |
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,137 @@ | |||
/* | |||
* Copyright (c) 2012 David Kellum | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); you | |||
* may not use this file except in compliance with the License. You may | |||
* obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | |||
* implied. See the License for the specific language governing | |||
* permissions and limitations under the License. | |||
*/ | |||
|
|||
package fishwife; | |||
|
|||
import static org.jruby.exceptions.RaiseException.createNativeRaiseException; | |||
|
|||
import java.io.FileInputStream; | |||
import java.io.FileNotFoundException; | |||
import java.io.IOException; | |||
import java.io.OutputStream; | |||
|
|||
import org.jruby.Ruby; | |||
import org.jruby.RubyObject; | |||
import org.jruby.RubyString; | |||
import org.jruby.anno.JRubyClass; | |||
import org.jruby.anno.JRubyMethod; | |||
import org.jruby.javasupport.util.RuntimeHelpers; | |||
import org.jruby.runtime.Arity; | |||
import org.jruby.runtime.Block; | |||
import org.jruby.runtime.BlockCallback; | |||
import org.jruby.runtime.CallBlock; | |||
import org.jruby.runtime.ThreadContext; | |||
import org.jruby.runtime.builtin.IRubyObject; | |||
import org.jruby.util.ByteList; | |||
|
|||
@JRubyClass( name="Fishwife::OutputUtil" ) | |||
public class OutputUtil | |||
{ | |||
@JRubyMethod( name = "write_file", | |||
meta = true, | |||
required = 2, | |||
argTypes = { RubyObject.class, | |||
OutputStream.class } ) | |||
public static IRubyObject writeFile( ThreadContext tc, | |||
IRubyObject klazz, | |||
IRubyObject file, | |||
IRubyObject ostr ) | |||
{ | |||
FileInputStream in = null; | |||
try { | |||
try { | |||
String filePath = file.convertToString().asJavaString(); | |||
in = new FileInputStream( filePath ); | |||
OutputStream out = | |||
(OutputStream) ostr.toJava( OutputStream.class ); | |||
|
|||
final byte[] buff = new byte[ 8 * 1024 ]; | |||
while( true ) { | |||
final int len = in.read( buff ); | |||
if( len > 0 ) { | |||
out.write( buff, 0, len ); | |||
} | |||
else break; | |||
} | |||
return tc.getRuntime().getNil(); | |||
} | |||
finally { | |||
if( in != null ) in.close(); | |||
} | |||
} | |||
catch( FileNotFoundException x ) { | |||
throw createNativeRaiseException( tc.getRuntime(), x ); | |||
} | |||
catch( IOException x ) { | |||
throw createNativeRaiseException( tc.getRuntime(), x ); | |||
} | |||
} | |||
|
|||
@JRubyMethod( name = "write_body", | |||
meta = true, | |||
required = 2, | |||
argTypes = { RubyObject.class, | |||
OutputStream.class } ) | |||
public static IRubyObject writeBody( ThreadContext tc, | |||
IRubyObject klazz, | |||
IRubyObject body, | |||
IRubyObject out ) | |||
{ | |||
OutputStream ostream = (OutputStream) out.toJava( OutputStream.class ); | |||
|
|||
RuntimeHelpers.invoke( tc, body, "each", | |||
CallBlock.newCallClosure( klazz, | |||
tc.getRuntime().getEnumerable(), | |||
Arity.ONE_ARGUMENT, | |||
new AppendBlockCallback( tc.getRuntime(), | |||
ostream ), | |||
tc ) ); | |||
|
|||
return tc.getRuntime().getNil(); | |||
} | |||
|
|||
public static final class AppendBlockCallback implements BlockCallback | |||
{ | |||
public AppendBlockCallback(Ruby runtime, OutputStream out) | |||
{ | |||
_runtime = runtime; | |||
_out = out; | |||
} | |||
|
|||
public IRubyObject call( ThreadContext context, | |||
IRubyObject[] args, | |||
Block blk ) | |||
{ | |||
try { | |||
final RubyString str = args[0].convertToString(); | |||
final ByteList blist = str.getByteList(); | |||
|
|||
_out.write( blist.unsafeBytes(), | |||
blist.begin(), | |||
blist.length() ); | |||
|
|||
return _runtime.getNil(); | |||
} | |||
catch( IOException x ) { | |||
throw createNativeRaiseException( _runtime, x ); | |||
} | |||
} | |||
|
|||
private final Ruby _runtime; | |||
private final OutputStream _out; | |||
} | |||
|
|||
} |